blob: afde87bc4990357eb6093827e9fa415442191f90 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
Reinette Chatreeb7ae892008-03-11 16:17:17 -07003 * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
Zhu Yib481de92007-09-25 17:54:57 -07004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Zhu Yib481de92007-09-25 17:54:57 -070026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
Zhu Yib481de92007-09-25 17:54:57 -070030#include <linux/kernel.h>
31#include <linux/module.h>
Zhu Yib481de92007-09-25 17:54:57 -070032#include <linux/init.h>
33#include <linux/pci.h>
34#include <linux/dma-mapping.h>
35#include <linux/delay.h>
36#include <linux/skbuff.h>
37#include <linux/netdevice.h>
38#include <linux/wireless.h>
39#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070040#include <linux/etherdevice.h>
41#include <linux/if_arp.h>
42
43#include <net/ieee80211_radiotap.h>
John W. Linville7e272fc2008-09-24 18:13:14 -040044#include <net/lib80211.h>
Zhu Yib481de92007-09-25 17:54:57 -070045#include <net/mac80211.h>
46
47#include <asm/div64.h>
48
Samuel Ortiza3139c52008-12-19 10:37:09 +080049#define DRV_NAME "iwl3945"
50
Winkler, Tomasdbb66542008-12-22 11:31:14 +080051#include "iwl-fh.h"
52#include "iwl-3945-fh.h"
Tomas Winkler600c0e12008-12-19 10:37:04 +080053#include "iwl-commands.h"
Zhu Yib481de92007-09-25 17:54:57 -070054#include "iwl-3945.h"
55#include "iwl-helpers.h"
Kolekar, Abhijeet5747d472008-12-19 10:37:18 +080056#include "iwl-core.h"
Samuel Ortizd20b3c62008-12-19 10:37:15 +080057#include "iwl-dev.h"
Zhu Yib481de92007-09-25 17:54:57 -070058
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +080059static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +080060 struct iwl_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080061
Zhu Yib481de92007-09-25 17:54:57 -070062/*
63 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070064 */
65
66#define DRV_DESCRIPTION \
67"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
68
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080069#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070070#define VD "d"
71#else
72#define VD
73#endif
74
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080075#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070076#define VS "s"
77#else
78#define VS
79#endif
80
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080081#define IWL39_VERSION "1.2.26k" VD VS
Reinette Chatreeb7ae892008-03-11 16:17:17 -070082#define DRV_COPYRIGHT "Copyright(c) 2003-2008 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080083#define DRV_AUTHOR "<ilw@linux.intel.com>"
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080084#define DRV_VERSION IWL39_VERSION
Zhu Yib481de92007-09-25 17:54:57 -070085
Zhu Yib481de92007-09-25 17:54:57 -070086
87MODULE_DESCRIPTION(DRV_DESCRIPTION);
88MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -080089MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -070090MODULE_LICENSE("GPL");
91
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080092 /* module parameters */
93struct iwl_mod_params iwl3945_mod_params = {
94 .num_of_queues = IWL39_MAX_NUM_QUEUES,
95 /* the rest are 0 by default */
96};
97
Zhu Yib481de92007-09-25 17:54:57 -070098/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
99 * DMA services
100 *
101 * Theory of operation
102 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800103 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
104 * of buffer descriptors, each of which points to one or more data buffers for
105 * the device to read from or fill. Driver and device exchange status of each
106 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
107 * entries in each circular buffer, to protect against confusing empty and full
108 * queue states.
109 *
110 * The device reads or writes the data in the queues via the device's several
111 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700112 *
113 * For Tx queue, there are low mark and high mark limits. If, after queuing
114 * the packet for Tx, free space become < low mark, Tx queue stopped. When
115 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
116 * Tx queue resumed.
117 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800118 * The 3945 operates with six queues: One receive queue, one transmit queue
119 * (#4) for sending commands to the device firmware, and four transmit queues
120 * (#0-3) for data tx via EDCA. An additional 2 HCCA queues are unused.
Zhu Yib481de92007-09-25 17:54:57 -0700121 ***************************************************/
122
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800123int iwl3945_x2_queue_used(const struct iwl_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700124{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800125 return q->write_ptr > q->read_ptr ?
126 (i >= q->read_ptr && i < q->write_ptr) :
127 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700128}
129
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800130/**
131 * iwl3945_queue_init - Initialize queue's high/low-water and read/write indexes
132 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800133static int iwl3945_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700134 int count, int slots_num, u32 id)
135{
136 q->n_bd = count;
137 q->n_window = slots_num;
138 q->id = id;
139
Tomas Winklerc54b6792008-03-06 17:36:53 -0800140 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
141 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700142 BUG_ON(!is_power_of_2(count));
143
144 /* slots_num must be power-of-two size, otherwise
145 * get_cmd_index is broken. */
146 BUG_ON(!is_power_of_2(slots_num));
147
148 q->low_mark = q->n_window / 4;
149 if (q->low_mark < 4)
150 q->low_mark = 4;
151
152 q->high_mark = q->n_window / 8;
153 if (q->high_mark < 2)
154 q->high_mark = 2;
155
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800156 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700157
158 return 0;
159}
160
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800161/**
162 * iwl3945_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
163 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800164static int iwl3945_tx_queue_alloc(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800165 struct iwl_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700166{
167 struct pci_dev *dev = priv->pci_dev;
168
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800169 /* Driver private data, only for Tx (not command) queues,
170 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700171 if (id != IWL_CMD_QUEUE_NUM) {
172 txq->txb = kmalloc(sizeof(txq->txb[0]) *
173 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
174 if (!txq->txb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800175 IWL_ERR(priv, "kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700176 "structures failed\n");
177 goto error;
178 }
179 } else
180 txq->txb = NULL;
181
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800182 /* Circular buffer of transmit frame descriptors (TFDs),
183 * shared with device */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800184 txq->tfds39 = pci_alloc_consistent(dev,
185 sizeof(txq->tfds39[0]) * TFD_QUEUE_SIZE_MAX,
Zhu Yib481de92007-09-25 17:54:57 -0700186 &txq->q.dma_addr);
187
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800188 if (!txq->tfds39) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800189 IWL_ERR(priv, "pci_alloc_consistent(%zd) failed\n",
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800190 sizeof(txq->tfds39[0]) * TFD_QUEUE_SIZE_MAX);
Zhu Yib481de92007-09-25 17:54:57 -0700191 goto error;
192 }
193 txq->q.id = id;
194
195 return 0;
196
197 error:
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300198 kfree(txq->txb);
199 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700200
201 return -ENOMEM;
202}
203
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800204/**
205 * iwl3945_tx_queue_init - Allocate and initialize one tx/cmd queue
206 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800207int iwl3945_tx_queue_init(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800208 struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700209{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800210 int len, i;
Zhu Yib481de92007-09-25 17:54:57 -0700211 int rc = 0;
212
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800213 /*
214 * Alloc buffer array for commands (Tx or other types of commands).
215 * For the command queue (#4), allocate command space + one big
216 * command for scan, since scan command is very huge; the system will
217 * not have two scans at the same time, so only one is needed.
218 * For data Tx queues (all other queues), no super-size command
219 * space is needed.
220 */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800221 len = sizeof(struct iwl_cmd);
222 for (i = 0; i <= slots_num; i++) {
223 if (i == slots_num) {
224 if (txq_id == IWL_CMD_QUEUE_NUM)
225 len += IWL_MAX_SCAN_SIZE;
226 else
227 continue;
228 }
229
230 txq->cmd[i] = kmalloc(len, GFP_KERNEL);
231 if (!txq->cmd[i])
232 goto err;
233 }
Zhu Yib481de92007-09-25 17:54:57 -0700234
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800235 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800236 rc = iwl3945_tx_queue_alloc(priv, txq, txq_id);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800237 if (rc)
238 goto err;
Zhu Yib481de92007-09-25 17:54:57 -0700239
Zhu Yib481de92007-09-25 17:54:57 -0700240 txq->need_update = 0;
241
242 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800243 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700244 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800245
246 /* Initialize queue high/low-water, head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800247 iwl3945_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700248
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800249 /* Tell device where to find queue, enable DMA channel. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800250 iwl3945_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700251
252 return 0;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800253err:
254 for (i = 0; i < slots_num; i++) {
255 kfree(txq->cmd[i]);
256 txq->cmd[i] = NULL;
257 }
258
259 if (txq_id == IWL_CMD_QUEUE_NUM) {
260 kfree(txq->cmd[slots_num]);
261 txq->cmd[slots_num] = NULL;
262 }
263 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -0700264}
265
266/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800267 * iwl3945_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700268 * @txq: Transmit queue to deallocate.
269 *
270 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800271 * Free all buffers.
272 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700273 */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800274void iwl3945_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700275{
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800276 struct iwl_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700277 struct pci_dev *dev = priv->pci_dev;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800278 int len, i;
Zhu Yib481de92007-09-25 17:54:57 -0700279
280 if (q->n_bd == 0)
281 return;
282
283 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800284 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800285 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800286 iwl3945_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700287
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800288 len = sizeof(struct iwl_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700289 if (q->id == IWL_CMD_QUEUE_NUM)
290 len += IWL_MAX_SCAN_SIZE;
291
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800292 /* De-alloc array of command/tx buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800293 for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
294 kfree(txq->cmd[i]);
Zhu Yib481de92007-09-25 17:54:57 -0700295
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800296 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700297 if (txq->q.n_bd)
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800298 pci_free_consistent(dev, sizeof(struct iwl3945_tfd) *
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800299 txq->q.n_bd, txq->tfds39, txq->q.dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700300
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800301 /* De-alloc array of per-TFD driver data */
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300302 kfree(txq->txb);
303 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700304
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800305 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700306 memset(txq, 0, sizeof(*txq));
307}
308
Zhu Yib481de92007-09-25 17:54:57 -0700309/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800310 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700311 * the functionality provided here
312 */
313
314/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800315#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800316/**
317 * iwl3945_remove_station - Remove driver's knowledge of station.
318 *
319 * NOTE: This does not remove station from device's station table.
320 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800321static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700322{
323 int index = IWL_INVALID_STATION;
324 int i;
325 unsigned long flags;
326
327 spin_lock_irqsave(&priv->sta_lock, flags);
328
329 if (is_ap)
330 index = IWL_AP_ID;
331 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800332 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700333 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800334 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800335 if (priv->stations_39[i].used &&
336 !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700337 addr)) {
338 index = i;
339 break;
340 }
341
342 if (unlikely(index == IWL_INVALID_STATION))
343 goto out;
344
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800345 if (priv->stations_39[index].used) {
346 priv->stations_39[index].used = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700347 priv->num_stations--;
348 }
349
350 BUG_ON(priv->num_stations < 0);
351
352out:
353 spin_unlock_irqrestore(&priv->sta_lock, flags);
354 return 0;
355}
Zhu Yi556f8db2007-09-27 11:27:33 +0800356#endif
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800357
358/**
359 * iwl3945_clear_stations_table - Clear the driver's station table
360 *
361 * NOTE: This does not clear or otherwise alter the device's station table.
362 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800363static void iwl3945_clear_stations_table(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700364{
365 unsigned long flags;
366
367 spin_lock_irqsave(&priv->sta_lock, flags);
368
369 priv->num_stations = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800370 memset(priv->stations_39, 0, sizeof(priv->stations_39));
Zhu Yib481de92007-09-25 17:54:57 -0700371
372 spin_unlock_irqrestore(&priv->sta_lock, flags);
373}
374
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800375/**
376 * iwl3945_add_station - Add station to station tables in driver and device
377 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800378u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700379{
380 int i;
381 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800382 struct iwl3945_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700383 unsigned long flags_spin;
Zhu Yic14c5212007-09-27 11:27:35 +0800384 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700385
386 spin_lock_irqsave(&priv->sta_lock, flags_spin);
387 if (is_ap)
388 index = IWL_AP_ID;
389 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800390 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700391 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800392 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800393 if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700394 addr)) {
395 index = i;
396 break;
397 }
398
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800399 if (!priv->stations_39[i].used &&
Zhu Yib481de92007-09-25 17:54:57 -0700400 index == IWL_INVALID_STATION)
401 index = i;
402 }
403
Ian Schram01ebd062007-10-25 17:15:22 +0800404 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700405 since they have different meaning */
406 if (unlikely(index == IWL_INVALID_STATION)) {
407 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
408 return index;
409 }
410
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800411 if (priv->stations_39[index].used &&
412 !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
Zhu Yib481de92007-09-25 17:54:57 -0700413 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
414 return index;
415 }
416
Johannes Berge1749612008-10-27 15:59:26 -0700417 IWL_DEBUG_ASSOC("Add STA ID %d: %pM\n", index, addr);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800418 station = &priv->stations_39[index];
Zhu Yib481de92007-09-25 17:54:57 -0700419 station->used = 1;
420 priv->num_stations++;
421
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800422 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800423 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700424 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
425 station->sta.mode = 0;
426 station->sta.sta.sta_id = index;
427 station->sta.station_flags = 0;
428
Johannes Berg8318d782008-01-24 19:38:38 +0100429 if (priv->band == IEEE80211_BAND_5GHZ)
Tomas Winkler69946332007-10-25 17:15:27 +0800430 rate = IWL_RATE_6M_PLCP;
431 else
432 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800433
434 /* Turn on both antennas for the station... */
435 station->sta.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800436 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
Zhu Yic14c5212007-09-27 11:27:35 +0800437
Zhu Yib481de92007-09-25 17:54:57 -0700438 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800439
440 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800441 iwl3945_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700442 return index;
443
444}
445
Zhu Yib481de92007-09-25 17:54:57 -0700446
447/*************** HOST COMMAND QUEUE FUNCTIONS *****/
448
Abhijeet Kolekarc3056062008-11-12 13:14:08 -0800449#define IWL_CMD(x) case x: return #x
Zhu Yib481de92007-09-25 17:54:57 -0700450#define HOST_COMPLETE_TIMEOUT (HZ / 2)
451
452/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800453 * iwl3945_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700454 * @priv: device private data point
455 * @cmd: a point to the ucode command structure
456 *
457 * The function returns < 0 values to indicate the operation is
458 * failed. On success, it turns the index (> 0) of command in the
459 * command queue.
460 */
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800461static int iwl3945_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700462{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800463 struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800464 struct iwl_queue *q = &txq->q;
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800465 struct iwl3945_tfd *tfd;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800466 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700467 u32 idx;
468 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
469 dma_addr_t phys_addr;
470 int pad;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800471 int ret, len;
Zhu Yib481de92007-09-25 17:54:57 -0700472 unsigned long flags;
473
474 /* If any of the command structures end up being larger than
475 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
476 * we will need to increase the size of the TFD entries */
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800477 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
Zhu Yib481de92007-09-25 17:54:57 -0700478 !(cmd->meta.flags & CMD_SIZE_HUGE));
479
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800480
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800481 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800482 IWL_DEBUG_INFO("Not sending command - RF KILL");
483 return -EIO;
484 }
485
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800486 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800487 IWL_ERR(priv, "No space for Tx\n");
Zhu Yib481de92007-09-25 17:54:57 -0700488 return -ENOSPC;
489 }
490
491 spin_lock_irqsave(&priv->hcmd_lock, flags);
492
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800493 tfd = &txq->tfds39[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700494 memset(tfd, 0, sizeof(*tfd));
495
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800496 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800497 out_cmd = txq->cmd[idx];
Zhu Yib481de92007-09-25 17:54:57 -0700498
499 out_cmd->hdr.cmd = cmd->id;
500 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
501 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
502
503 /* At this point, the out_cmd now has all of the incoming cmd
504 * information */
505
506 out_cmd->hdr.flags = 0;
507 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800508 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700509 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
Tomas Winkler600c0e12008-12-19 10:37:04 +0800510 out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
Zhu Yib481de92007-09-25 17:54:57 -0700511
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800512 len = (idx == TFD_CMD_SLOTS) ?
513 IWL_MAX_SCAN_SIZE : sizeof(struct iwl_cmd);
514
515 phys_addr = pci_map_single(priv->pci_dev, out_cmd,
516 len, PCI_DMA_TODEVICE);
517 pci_unmap_addr_set(&out_cmd->meta, mapping, phys_addr);
518 pci_unmap_len_set(&out_cmd->meta, len, len);
519 phys_addr += offsetof(struct iwl_cmd, hdr);
520
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800521 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700522
523 pad = U32_PAD(cmd->len);
Winkler, Tomasdbb66542008-12-22 11:31:14 +0800524 tfd->control_flags |= cpu_to_le32(TFD_CTL_PAD_SET(pad));
Zhu Yib481de92007-09-25 17:54:57 -0700525
526 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
527 "%d bytes at %d[%d]:%d\n",
528 get_cmd_string(out_cmd->hdr.cmd),
529 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800530 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700531
532 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800533
534 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800535 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800536 ret = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700537
538 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
539 return ret ? ret : idx;
540}
541
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800542static int iwl3945_send_cmd_async(struct iwl_priv *priv,
543 struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700544{
545 int ret;
546
547 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
548
549 /* An asynchronous command can not expect an SKB to be set. */
550 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
551
552 /* An asynchronous command MUST have a callback. */
553 BUG_ON(!cmd->meta.u.callback);
554
555 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
556 return -EBUSY;
557
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800558 ret = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700559 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800560 IWL_ERR(priv,
561 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
562 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700563 return ret;
564 }
565 return 0;
566}
567
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800568static int iwl3945_send_cmd_sync(struct iwl_priv *priv,
569 struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700570{
571 int cmd_idx;
572 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700573
574 BUG_ON(cmd->meta.flags & CMD_ASYNC);
575
576 /* A synchronous command can not have a callback set. */
577 BUG_ON(cmd->meta.u.callback != NULL);
578
Tomas Winklere5472972008-03-28 16:21:12 -0700579 if (test_and_set_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800580 IWL_ERR(priv,
581 "Error sending %s: Already sending a host command\n",
582 get_cmd_string(cmd->id));
Tomas Winklere5472972008-03-28 16:21:12 -0700583 ret = -EBUSY;
584 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700585 }
586
587 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
588
589 if (cmd->meta.flags & CMD_WANT_SKB)
590 cmd->meta.source = &cmd->meta;
591
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800592 cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700593 if (cmd_idx < 0) {
594 ret = cmd_idx;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800595 IWL_ERR(priv,
596 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
597 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700598 goto out;
599 }
600
601 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
602 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
603 HOST_COMPLETE_TIMEOUT);
604 if (!ret) {
605 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800606 IWL_ERR(priv, "Error sending %s: time out after %dms\n",
Zhu Yib481de92007-09-25 17:54:57 -0700607 get_cmd_string(cmd->id),
608 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
609
610 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
611 ret = -ETIMEDOUT;
612 goto cancel;
613 }
614 }
615
616 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
617 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
618 get_cmd_string(cmd->id));
619 ret = -ECANCELED;
620 goto fail;
621 }
622 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
623 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
624 get_cmd_string(cmd->id));
625 ret = -EIO;
626 goto fail;
627 }
628 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800629 IWL_ERR(priv, "Error: Response NULL in '%s'\n",
Zhu Yib481de92007-09-25 17:54:57 -0700630 get_cmd_string(cmd->id));
631 ret = -EIO;
Zhu Yi73e1a652009-01-08 10:19:58 -0800632 goto cancel;
Zhu Yib481de92007-09-25 17:54:57 -0700633 }
634
635 ret = 0;
636 goto out;
637
638cancel:
639 if (cmd->meta.flags & CMD_WANT_SKB) {
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800640 struct iwl_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700641
642 /* Cancel the CMD_WANT_SKB flag for the cmd in the
643 * TX cmd queue. Otherwise in case the cmd comes
644 * in later, it will possibly set an invalid
645 * address (cmd->meta.source). */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +0800646 qcmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
Zhu Yib481de92007-09-25 17:54:57 -0700647 qcmd->meta.flags &= ~CMD_WANT_SKB;
648 }
649fail:
650 if (cmd->meta.u.skb) {
651 dev_kfree_skb_any(cmd->meta.u.skb);
652 cmd->meta.u.skb = NULL;
653 }
654out:
Tomas Winklere5472972008-03-28 16:21:12 -0700655 clear_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -0700656 return ret;
657}
658
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800659int iwl3945_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700660{
Zhu Yib481de92007-09-25 17:54:57 -0700661 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800662 return iwl3945_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700663
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800664 return iwl3945_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700665}
666
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800667int iwl3945_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700668{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800669 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700670 .id = id,
671 .len = len,
672 .data = data,
673 };
674
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800675 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700676}
677
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800678static int __must_check iwl3945_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700679{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800680 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700681 .id = id,
682 .len = sizeof(val),
683 .data = &val,
684 };
685
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800686 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700687}
688
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800689int iwl3945_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700690{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800691 return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700692}
693
694/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800695 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Johannes Berg8318d782008-01-24 19:38:38 +0100696 * @band: 2.4 or 5 GHz band
697 * @channel: Any channel valid for the requested band
Zhu Yib481de92007-09-25 17:54:57 -0700698
Johannes Berg8318d782008-01-24 19:38:38 +0100699 * In addition to setting the staging RXON, priv->band is also set.
Zhu Yib481de92007-09-25 17:54:57 -0700700 *
701 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
Johannes Berg8318d782008-01-24 19:38:38 +0100702 * in the staging RXON flag structure based on the band
Zhu Yib481de92007-09-25 17:54:57 -0700703 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800704static int iwl3945_set_rxon_channel(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100705 enum ieee80211_band band,
706 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700707{
Johannes Berg8318d782008-01-24 19:38:38 +0100708 if (!iwl3945_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700709 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100710 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700711 return -EINVAL;
712 }
713
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800714 if ((le16_to_cpu(priv->staging39_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100715 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700716 return 0;
717
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800718 priv->staging39_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100719 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800720 priv->staging39_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700721 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800722 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700723
Johannes Berg8318d782008-01-24 19:38:38 +0100724 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700725
Johannes Berg8318d782008-01-24 19:38:38 +0100726 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700727
728 return 0;
729}
730
731/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800732 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700733 *
734 * NOTE: This is really only useful during development and can eventually
735 * be #ifdef'd out once the driver is stable and folks aren't actively
736 * making changes
737 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800738static int iwl3945_check_rxon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700739{
740 int error = 0;
741 int counter = 1;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800742 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700743
744 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
745 error |= le32_to_cpu(rxon->flags &
746 (RXON_FLG_TGJ_NARROW_BAND_MSK |
747 RXON_FLG_RADAR_DETECT_MSK));
748 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800749 IWL_WARN(priv, "check 24G fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700750 counter++, error);
751 } else {
752 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
753 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
754 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800755 IWL_WARN(priv, "check 52 fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700756 counter++, error);
757 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
758 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800759 IWL_WARN(priv, "check 52 CCK %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700760 counter++, error);
761 }
762 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
763 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800764 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700765
766 /* make sure basic rates 6Mbps and 1Mbps are supported */
767 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
768 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
769 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800770 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700771
772 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
773 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800774 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700775
776 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
777 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
778 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800779 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700780 counter++, error);
781
782 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
783 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
784 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800785 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700786 counter++, error);
787
788 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
789 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
790 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800791 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700792 counter++, error);
793
794 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
795 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
796 RXON_FLG_ANT_A_MSK)) == 0);
797 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800798 IWL_WARN(priv, "check antenna %d %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700799
800 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800801 IWL_WARN(priv, "Tuning to channel %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700802 le16_to_cpu(rxon->channel));
803
804 if (error) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800805 IWL_ERR(priv, "Not a valid rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700806 return -1;
807 }
808 return 0;
809}
810
811/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800812 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800813 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700814 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800815 * If the RXON structure is changing enough to require a new tune,
816 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
817 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700818 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800819static int iwl3945_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700820{
821
822 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800823 if (!(iwl3945_is_associated(priv)) ||
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800824 compare_ether_addr(priv->staging39_rxon.bssid_addr,
825 priv->active39_rxon.bssid_addr) ||
826 compare_ether_addr(priv->staging39_rxon.node_addr,
827 priv->active39_rxon.node_addr) ||
828 compare_ether_addr(priv->staging39_rxon.wlap_bssid_addr,
829 priv->active39_rxon.wlap_bssid_addr) ||
830 (priv->staging39_rxon.dev_type != priv->active39_rxon.dev_type) ||
831 (priv->staging39_rxon.channel != priv->active39_rxon.channel) ||
832 (priv->staging39_rxon.air_propagation !=
833 priv->active39_rxon.air_propagation) ||
834 (priv->staging39_rxon.assoc_id != priv->active39_rxon.assoc_id))
Zhu Yib481de92007-09-25 17:54:57 -0700835 return 1;
836
837 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
838 * be updated with the RXON_ASSOC command -- however only some
839 * flag transitions are allowed using RXON_ASSOC */
840
841 /* Check if we are not switching bands */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800842 if ((priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
843 (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700844 return 1;
845
846 /* Check if we are switching association toggle */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800847 if ((priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
848 (priv->active39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700849 return 1;
850
851 return 0;
852}
853
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800854static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700855{
856 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800857 struct iwl_rx_packet *res = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800858 struct iwl3945_rxon_assoc_cmd rxon_assoc;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800859 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700860 .id = REPLY_RXON_ASSOC,
861 .len = sizeof(rxon_assoc),
862 .meta.flags = CMD_WANT_SKB,
863 .data = &rxon_assoc,
864 };
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800865 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging39_rxon;
866 const struct iwl3945_rxon_cmd *rxon2 = &priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700867
868 if ((rxon1->flags == rxon2->flags) &&
869 (rxon1->filter_flags == rxon2->filter_flags) &&
870 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
871 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
872 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
873 return 0;
874 }
875
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800876 rxon_assoc.flags = priv->staging39_rxon.flags;
877 rxon_assoc.filter_flags = priv->staging39_rxon.filter_flags;
878 rxon_assoc.ofdm_basic_rates = priv->staging39_rxon.ofdm_basic_rates;
879 rxon_assoc.cck_basic_rates = priv->staging39_rxon.cck_basic_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700880 rxon_assoc.reserved = 0;
881
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800882 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700883 if (rc)
884 return rc;
885
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800886 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700887 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800888 IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700889 rc = -EIO;
890 }
891
892 priv->alloc_rxb_skb--;
893 dev_kfree_skb_any(cmd.meta.u.skb);
894
895 return rc;
896}
897
898/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800899 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700900 *
Ian Schram01ebd062007-10-25 17:15:22 +0800901 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700902 * the active_rxon structure is updated with the new data. This
903 * function correctly transitions out of the RXON_ASSOC_MSK state if
904 * a HW tune is required based on the RXON structure changes.
905 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800906static int iwl3945_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700907{
908 /* cast away the const for active_rxon in this function */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800909 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700910 int rc = 0;
911
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800912 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700913 return -1;
914
915 /* always get timestamp with Rx frame */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800916 priv->staging39_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700917
918 /* select antenna */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800919 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -0700920 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800921 priv->staging39_rxon.flags |= iwl3945_get_antenna_flags(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700922
Samuel Ortiza3139c52008-12-19 10:37:09 +0800923 rc = iwl3945_check_rxon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700924 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800925 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700926 return -EINVAL;
927 }
928
929 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800930 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700931 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800932 if (!iwl3945_full_rxon_required(priv)) {
933 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700934 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800935 IWL_ERR(priv, "Error setting RXON_ASSOC "
Zhu Yib481de92007-09-25 17:54:57 -0700936 "configuration (%d).\n", rc);
937 return rc;
938 }
939
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800940 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700941
942 return 0;
943 }
944
945 /* If we are currently associated and the new config requires
946 * an RXON_ASSOC and the new config wants the associated mask enabled,
947 * we must clear the associated from the active configuration
948 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800949 if (iwl3945_is_associated(priv) &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800950 (priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700951 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
952 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
953
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800954 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
955 sizeof(struct iwl3945_rxon_cmd),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800956 &priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700957
958 /* If the mask clearing failed then we set
959 * active_rxon back to what it was previously */
960 if (rc) {
961 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800962 IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
Zhu Yib481de92007-09-25 17:54:57 -0700963 "configuration (%d).\n", rc);
964 return rc;
965 }
Zhu Yib481de92007-09-25 17:54:57 -0700966 }
967
968 IWL_DEBUG_INFO("Sending RXON\n"
969 "* with%s RXON_FILTER_ASSOC_MSK\n"
970 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -0700971 "* bssid = %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800972 ((priv->staging39_rxon.filter_flags &
Zhu Yib481de92007-09-25 17:54:57 -0700973 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800974 le16_to_cpu(priv->staging39_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -0700975 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700976
977 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800978 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800979 sizeof(struct iwl3945_rxon_cmd), &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700980 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800981 IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700982 return rc;
983 }
984
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800985 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700986
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800987 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800988
Zhu Yib481de92007-09-25 17:54:57 -0700989 /* If we issue a new RXON command which required a tune then we must
990 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800991 rc = iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700992 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800993 IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700994 return rc;
995 }
996
997 /* Add the broadcast address so we can send broadcast frames */
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +0800998 if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700999 IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001000 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001001 return -EIO;
1002 }
1003
1004 /* If we have set the ASSOC_MSK and we are in BSS mode then
1005 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001006 if (iwl3945_is_associated(priv) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02001007 (priv->iw_mode == NL80211_IFTYPE_STATION))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001008 if (iwl3945_add_station(priv, priv->active39_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -07001009 == IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001010 IWL_ERR(priv, "Error adding AP address for transmit\n");
Zhu Yib481de92007-09-25 17:54:57 -07001011 return -EIO;
1012 }
1013
Johannes Berg8318d782008-01-24 19:38:38 +01001014 /* Init the hardware's rate fallback order based on the band */
Zhu Yib481de92007-09-25 17:54:57 -07001015 rc = iwl3945_init_hw_rate_table(priv);
1016 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001017 IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07001018 return -EIO;
1019 }
1020
1021 return 0;
1022}
1023
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001024static int iwl3945_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001025{
Tomas Winkler4c897252008-12-19 10:37:05 +08001026 struct iwl_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001027 .flags = 3,
1028 .lead_time = 0xAA,
1029 .max_kill = 1,
1030 .kill_ack_mask = 0,
1031 .kill_cts_mask = 0,
1032 };
1033
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001034 return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Tomas Winkler4c897252008-12-19 10:37:05 +08001035 sizeof(bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001036}
1037
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001038static int iwl3945_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001039{
1040 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001041 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001042 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001043 .id = REPLY_SCAN_ABORT_CMD,
1044 .meta.flags = CMD_WANT_SKB,
1045 };
1046
1047 /* If there isn't a scan actively going on in the hardware
1048 * then we are in between scan bands and not actually
1049 * actively scanning, so don't send the abort command */
1050 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1051 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1052 return 0;
1053 }
1054
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001055 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001056 if (rc) {
1057 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1058 return rc;
1059 }
1060
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001061 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001062 if (res->u.status != CAN_ABORT_STATUS) {
1063 /* The scan abort will return 1 for success or
1064 * 2 for "failure". A failure condition can be
1065 * due to simply not being in an active scan which
1066 * can occur if we send the scan abort before we
1067 * the microcode has notified us that a scan is
1068 * completed. */
1069 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1070 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1071 clear_bit(STATUS_SCAN_HW, &priv->status);
1072 }
1073
1074 dev_kfree_skb_any(cmd.meta.u.skb);
1075
1076 return rc;
1077}
1078
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001079static int iwl3945_add_sta_sync_callback(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001080 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001081{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001082 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001083
1084 if (!skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001085 IWL_ERR(priv, "Error: Response NULL in REPLY_ADD_STA.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001086 return 1;
1087 }
1088
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001089 res = (struct iwl_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001090 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001091 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001092 res->hdr.flags);
1093 return 1;
1094 }
1095
1096 switch (res->u.add_sta.status) {
1097 case ADD_STA_SUCCESS_MSK:
1098 break;
1099 default:
1100 break;
1101 }
1102
1103 /* We didn't cache the SKB; let the caller free it */
1104 return 1;
1105}
1106
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001107int iwl3945_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001108 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001109{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001110 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001111 int rc = 0;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001112 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001113 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001114 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001115 .meta.flags = flags,
1116 .data = sta,
1117 };
1118
1119 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001120 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001121 else
1122 cmd.meta.flags |= CMD_WANT_SKB;
1123
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001124 rc = iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001125
1126 if (rc || (flags & CMD_ASYNC))
1127 return rc;
1128
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001129 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001130 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001131 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001132 res->hdr.flags);
1133 rc = -EIO;
1134 }
1135
1136 if (rc == 0) {
1137 switch (res->u.add_sta.status) {
1138 case ADD_STA_SUCCESS_MSK:
1139 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1140 break;
1141 default:
1142 rc = -EIO;
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001143 IWL_WARN(priv, "REPLY_ADD_STA failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07001144 break;
1145 }
1146 }
1147
1148 priv->alloc_rxb_skb--;
1149 dev_kfree_skb_any(cmd.meta.u.skb);
1150
1151 return rc;
1152}
1153
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001154static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001155 struct ieee80211_key_conf *keyconf,
1156 u8 sta_id)
1157{
1158 unsigned long flags;
1159 __le16 key_flags = 0;
1160
1161 switch (keyconf->alg) {
1162 case ALG_CCMP:
1163 key_flags |= STA_KEY_FLG_CCMP;
1164 key_flags |= cpu_to_le16(
1165 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1166 key_flags &= ~STA_KEY_FLG_INVALID;
1167 break;
1168 case ALG_TKIP:
1169 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001170 default:
1171 return -EINVAL;
1172 }
1173 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001174 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
1175 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
1176 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001177 keyconf->keylen);
1178
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001179 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001180 keyconf->keylen);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001181 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
1182 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1183 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001184
1185 spin_unlock_irqrestore(&priv->sta_lock, flags);
1186
1187 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001188 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001189 return 0;
1190}
1191
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001192static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001193{
1194 unsigned long flags;
1195
1196 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001197 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
1198 memset(&priv->stations_39[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +08001199 sizeof(struct iwl4965_keyinfo));
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001200 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1201 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1202 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001203 spin_unlock_irqrestore(&priv->sta_lock, flags);
1204
1205 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001206 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001207 return 0;
1208}
1209
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001210static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001211{
1212 struct list_head *element;
1213
1214 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1215 priv->frames_count);
1216
1217 while (!list_empty(&priv->free_frames)) {
1218 element = priv->free_frames.next;
1219 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001220 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001221 priv->frames_count--;
1222 }
1223
1224 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001225 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -07001226 priv->frames_count);
1227 priv->frames_count = 0;
1228 }
1229}
1230
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001231static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001232{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001233 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001234 struct list_head *element;
1235 if (list_empty(&priv->free_frames)) {
1236 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1237 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001238 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -07001239 return NULL;
1240 }
1241
1242 priv->frames_count++;
1243 return frame;
1244 }
1245
1246 element = priv->free_frames.next;
1247 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001248 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001249}
1250
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001251static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001252{
1253 memset(frame, 0, sizeof(*frame));
1254 list_add(&frame->list, &priv->free_frames);
1255}
1256
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001257unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001258 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +02001259 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001260{
1261
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001262 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001263 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
1264 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -07001265 return 0;
1266
1267 if (priv->ibss_beacon->len > left)
1268 return 0;
1269
1270 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1271
1272 return priv->ibss_beacon->len;
1273}
1274
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001275static u8 iwl3945_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001276{
1277 u8 i;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001278 int rate_mask;
1279
1280 /* Set rate mask*/
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001281 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001282 rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001283 else
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001284 rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001285
1286 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001287 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001288 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001289 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001290 }
1291
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001292 /* No valid rate was found. Assign the lowest one */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001293 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001294 return IWL_RATE_1M_PLCP;
1295 else
1296 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001297}
1298
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001299static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001300{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001301 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001302 unsigned int frame_size;
1303 int rc;
1304 u8 rate;
1305
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001306 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001307
1308 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001309 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -07001310 "command.\n");
1311 return -ENOMEM;
1312 }
1313
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001314 rate = iwl3945_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001315
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001316 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001317
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001318 rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001319 &frame->u.cmd[0]);
1320
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001321 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001322
1323 return rc;
1324}
1325
1326/******************************************************************************
1327 *
1328 * EEPROM related functions
1329 *
1330 ******************************************************************************/
1331
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001332static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001333{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001334 memcpy(mac, priv->eeprom39.mac_address, 6);
Zhu Yib481de92007-09-25 17:54:57 -07001335}
1336
Reinette Chatre74a3a252008-01-23 10:15:19 -08001337/*
1338 * Clear the OWNER_MSK, to establish driver (instead of uCode running on
1339 * embedded controller) as EEPROM reader; each read is a series of pulses
1340 * to/from the EEPROM chip, not a single event, so even reads could conflict
1341 * if they weren't arbitrated by some ownership mechanism. Here, the driver
1342 * simply claims ownership, which should be safe when this function is called
1343 * (i.e. before loading uCode!).
1344 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001345static inline int iwl3945_eeprom_acquire_semaphore(struct iwl_priv *priv)
Reinette Chatre74a3a252008-01-23 10:15:19 -08001346{
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001347 _iwl_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
Reinette Chatre74a3a252008-01-23 10:15:19 -08001348 return 0;
1349}
1350
Zhu Yib481de92007-09-25 17:54:57 -07001351/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001352 * iwl3945_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001353 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001354 * Load the EEPROM contents from adapter into priv->eeprom39
Zhu Yib481de92007-09-25 17:54:57 -07001355 *
1356 * NOTE: This routine uses the non-debug IO access functions.
1357 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001358int iwl3945_eeprom_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001359{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001360 u16 *e = (u16 *)&priv->eeprom39;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001361 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001362 int sz = sizeof(priv->eeprom39);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001363 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001364 u16 addr;
1365
1366 /* The EEPROM structure has several padding buffers within it
1367 * and when adding new EEPROM maps is subject to programmer errors
1368 * which may be very difficult to identify without explicitly
1369 * checking the resulting size of the eeprom map. */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001370 BUILD_BUG_ON(sizeof(priv->eeprom39) != IWL_EEPROM_IMAGE_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07001371
1372 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001373 IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
Zhu Yib481de92007-09-25 17:54:57 -07001374 return -ENOENT;
1375 }
1376
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001377 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001378 ret = iwl3945_eeprom_acquire_semaphore(priv);
1379 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001380 IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001381 return -ENOENT;
1382 }
1383
1384 /* eeprom is an array of 16bit values */
1385 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001386 u32 r;
1387
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001388 _iwl_write32(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001389 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001390 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1391 ret = iwl_poll_direct_bit(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001392 CSR_EEPROM_REG_READ_VALID_MSK,
1393 IWL_EEPROM_ACCESS_TIMEOUT);
1394 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001395 IWL_ERR(priv, "Time out reading EEPROM[%d]\n", addr);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001396 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001397 }
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001398
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001399 r = _iwl_read_direct32(priv, CSR_EEPROM_REG);
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001400 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001401 }
1402
1403 return 0;
1404}
1405
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001406static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001407{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001408 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -07001409 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001410 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001411 priv->shared_virt,
1412 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -07001413}
1414
1415/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001416 * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001417 *
1418 * return : set the bit for each supported rate insert in ie
1419 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001420static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001421 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001422{
1423 u16 ret_rates = 0, bit;
1424 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001425 u8 *cnt = ie;
1426 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001427
1428 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1429 if (bit & supported_rate) {
1430 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001431 rates[*cnt] = iwl3945_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001432 ((bit & basic_rate) ? 0x80 : 0x00);
1433 (*cnt)++;
1434 (*left)--;
1435 if ((*left <= 0) ||
1436 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001437 break;
1438 }
1439 }
1440
1441 return ret_rates;
1442}
1443
1444/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001445 * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001446 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001447static u16 iwl3945_fill_probe_req(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001448 struct ieee80211_mgmt *frame,
Johannes Berg430cfe92008-10-28 18:06:02 +01001449 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001450{
1451 int len = 0;
1452 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001453 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001454
1455 /* Make sure there is enough space for the probe request,
1456 * two mandatory IEs and the data */
1457 left -= 24;
1458 if (left < 0)
1459 return 0;
1460 len += 24;
1461
1462 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001463 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001464 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001465 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001466 frame->seq_ctrl = 0;
1467
1468 /* fill in our indirect SSID IE */
1469 /* ...next IE... */
1470
1471 left -= 2;
1472 if (left < 0)
1473 return 0;
1474 len += 2;
1475 pos = &(frame->u.probe_req.variable[0]);
1476 *pos++ = WLAN_EID_SSID;
1477 *pos++ = 0;
1478
Zhu Yib481de92007-09-25 17:54:57 -07001479 /* fill in supported rate */
1480 /* ...next IE... */
1481 left -= 2;
1482 if (left < 0)
1483 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001484
Zhu Yib481de92007-09-25 17:54:57 -07001485 /* ... fill it in... */
1486 *pos++ = WLAN_EID_SUPP_RATES;
1487 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001488
1489 priv->active_rate = priv->rates_mask;
1490 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001491 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1492
Tomas Winklerc7c46672007-10-18 02:04:15 +02001493 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001494 ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001495 priv->active_rate_basic, &left);
1496 active_rates &= ~ret_rates;
1497
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001498 ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001499 priv->active_rate_basic, &left);
1500 active_rates &= ~ret_rates;
1501
Zhu Yib481de92007-09-25 17:54:57 -07001502 len += 2 + *pos;
1503 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001504 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001505 goto fill_end;
1506
1507 /* fill in supported extended rate */
1508 /* ...next IE... */
1509 left -= 2;
1510 if (left < 0)
1511 return 0;
1512 /* ... fill it in... */
1513 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1514 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001515 iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001516 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001517 if (*pos > 0)
1518 len += 2 + *pos;
1519
1520 fill_end:
1521 return (u16)len;
1522}
1523
1524/*
1525 * QoS support
1526*/
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001527static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
Tomas Winkler4c897252008-12-19 10:37:05 +08001528 struct iwl_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001529{
1530
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001531 return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Tomas Winkler4c897252008-12-19 10:37:05 +08001532 sizeof(struct iwl_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001533}
1534
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001535static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001536{
1537 unsigned long flags;
1538
Zhu Yib481de92007-09-25 17:54:57 -07001539 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1540 return;
1541
Zhu Yib481de92007-09-25 17:54:57 -07001542 spin_lock_irqsave(&priv->lock, flags);
1543 priv->qos_data.def_qos_parm.qos_flags = 0;
1544
1545 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1546 !priv->qos_data.qos_cap.q_AP.txop_request)
1547 priv->qos_data.def_qos_parm.qos_flags |=
1548 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1549
1550 if (priv->qos_data.qos_active)
1551 priv->qos_data.def_qos_parm.qos_flags |=
1552 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1553
1554 spin_unlock_irqrestore(&priv->lock, flags);
1555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001556 if (force || iwl3945_is_associated(priv)) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07001557 IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
Zhu Yib481de92007-09-25 17:54:57 -07001558 priv->qos_data.qos_active);
1559
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001560 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001561 &(priv->qos_data.def_qos_parm));
1562 }
1563}
1564
Zhu Yib481de92007-09-25 17:54:57 -07001565/*
1566 * Power management (not Tx power!) functions
1567 */
1568#define MSEC_TO_USEC 1024
1569
Tomas Winkler600c0e12008-12-19 10:37:04 +08001570
1571#define NOSLP __constant_cpu_to_le16(0), 0, 0
1572#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -07001573#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1574#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1575 __constant_cpu_to_le32(X1), \
1576 __constant_cpu_to_le32(X2), \
1577 __constant_cpu_to_le32(X3), \
1578 __constant_cpu_to_le32(X4)}
1579
Zhu Yib481de92007-09-25 17:54:57 -07001580/* default power management (not Tx power) table values */
Tomas Winklera96a27f2008-10-23 23:48:56 -07001581/* for TIM 0-10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001582static struct iwl_power_vec_entry range_0[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001583 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1584 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1585 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1586 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1587 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1588 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1589};
1590
Tomas Winklera96a27f2008-10-23 23:48:56 -07001591/* for TIM > 10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001592static struct iwl_power_vec_entry range_1[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001593 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1594 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1595 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1596 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1597 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1598 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1599 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1600 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1601 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1602 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1603};
1604
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001605int iwl3945_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001606{
1607 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001608 struct iwl3945_power_mgr *pow_data;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001609 int size = sizeof(struct iwl_power_vec_entry) * IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001610 u16 pci_pm;
1611
1612 IWL_DEBUG_POWER("Initialize power \n");
1613
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001614 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001615
1616 memset(pow_data, 0, sizeof(*pow_data));
1617
1618 pow_data->active_index = IWL_POWER_RANGE_0;
1619 pow_data->dtim_val = 0xffff;
1620
1621 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1622 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1623
1624 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1625 if (rc != 0)
1626 return 0;
1627 else {
Tomas Winkler600c0e12008-12-19 10:37:04 +08001628 struct iwl_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001629
1630 IWL_DEBUG_POWER("adjust power command flags\n");
1631
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001632 for (i = 0; i < IWL39_POWER_AC; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07001633 cmd = &pow_data->pwr_range_0[i].cmd;
1634
1635 if (pci_pm & 0x1)
1636 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1637 else
1638 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1639 }
1640 }
1641 return rc;
1642}
1643
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001644static int iwl3945_update_power_cmd(struct iwl_priv *priv,
Tomas Winkler600c0e12008-12-19 10:37:04 +08001645 struct iwl_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001646{
1647 int rc = 0, i;
1648 u8 skip;
1649 u32 max_sleep = 0;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001650 struct iwl_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001651 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001652 struct iwl3945_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001653
1654 if (mode > IWL_POWER_INDEX_5) {
1655 IWL_DEBUG_POWER("Error invalid power mode \n");
1656 return -1;
1657 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001658 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001659
1660 if (pow_data->active_index == IWL_POWER_RANGE_0)
1661 range = &pow_data->pwr_range_0[0];
1662 else
1663 range = &pow_data->pwr_range_1[1];
1664
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001665 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001666
1667#ifdef IWL_MAC80211_DISABLE
1668 if (priv->assoc_network != NULL) {
1669 unsigned long flags;
1670
1671 period = priv->assoc_network->tim.tim_period;
1672 }
1673#endif /*IWL_MAC80211_DISABLE */
1674 skip = range[mode].no_dtim;
1675
1676 if (period == 0) {
1677 period = 1;
1678 skip = 0;
1679 }
1680
1681 if (skip == 0) {
1682 max_sleep = period;
1683 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1684 } else {
1685 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1686 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1687 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1688 }
1689
1690 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1691 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1692 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1693 }
1694
1695 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1696 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1697 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1698 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1699 le32_to_cpu(cmd->sleep_interval[0]),
1700 le32_to_cpu(cmd->sleep_interval[1]),
1701 le32_to_cpu(cmd->sleep_interval[2]),
1702 le32_to_cpu(cmd->sleep_interval[3]),
1703 le32_to_cpu(cmd->sleep_interval[4]));
1704
1705 return rc;
1706}
1707
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001708static int iwl3945_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001709{
John W. Linville9a62f732007-11-15 16:27:36 -05001710 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001711 int rc;
Tomas Winkler600c0e12008-12-19 10:37:04 +08001712 struct iwl_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001713
1714 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001715 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001716 * else user level */
1717 switch (mode) {
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001718 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07001719 final_mode = IWL_POWER_INDEX_3;
1720 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001721 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07001722 final_mode = IWL_POWER_MODE_CAM;
1723 break;
1724 default:
1725 final_mode = mode;
1726 break;
1727 }
1728
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001729 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001730
Tomas Winkler600c0e12008-12-19 10:37:04 +08001731 /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
1732 rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD,
1733 sizeof(struct iwl3945_powertable_cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001734
1735 if (final_mode == IWL_POWER_MODE_CAM)
1736 clear_bit(STATUS_POWER_PMI, &priv->status);
1737 else
1738 set_bit(STATUS_POWER_PMI, &priv->status);
1739
1740 return rc;
1741}
1742
Zhu Yib481de92007-09-25 17:54:57 -07001743/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001744 * iwl3945_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001745 *
1746 * NOTE: priv->mutex is not required before calling this function
1747 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001748static int iwl3945_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001749{
1750 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1751 clear_bit(STATUS_SCANNING, &priv->status);
1752 return 0;
1753 }
1754
1755 if (test_bit(STATUS_SCANNING, &priv->status)) {
1756 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1757 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1758 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1759 queue_work(priv->workqueue, &priv->abort_scan);
1760
1761 } else
1762 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1763
1764 return test_bit(STATUS_SCANNING, &priv->status);
1765 }
1766
1767 return 0;
1768}
1769
1770/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001771 * iwl3945_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001772 * @ms: amount of time to wait (in milliseconds) for scan to abort
1773 *
1774 * NOTE: priv->mutex must be held before calling this function
1775 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001776static int iwl3945_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001777{
1778 unsigned long now = jiffies;
1779 int ret;
1780
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001781 ret = iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001782 if (ret && ms) {
1783 mutex_unlock(&priv->mutex);
1784 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1785 test_bit(STATUS_SCANNING, &priv->status))
1786 msleep(1);
1787 mutex_lock(&priv->mutex);
1788
1789 return test_bit(STATUS_SCANNING, &priv->status);
1790 }
1791
1792 return ret;
1793}
1794
Zhu Yib481de92007-09-25 17:54:57 -07001795#define MAX_UCODE_BEACON_INTERVAL 1024
1796#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1797
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001798static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001799{
1800 u16 new_val = 0;
1801 u16 beacon_factor = 0;
1802
1803 beacon_factor =
1804 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1805 / MAX_UCODE_BEACON_INTERVAL;
1806 new_val = beacon_val / beacon_factor;
1807
1808 return cpu_to_le16(new_val);
1809}
1810
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001811static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001812{
1813 u64 interval_tm_unit;
1814 u64 tsf, result;
1815 unsigned long flags;
1816 struct ieee80211_conf *conf = NULL;
1817 u16 beacon_int = 0;
1818
1819 conf = ieee80211_get_hw_conf(priv->hw);
1820
1821 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +08001822 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -07001823 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1824
Tomas Winkler28afaf92008-12-19 10:37:06 +08001825 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001826
1827 beacon_int = priv->beacon_int;
1828 spin_unlock_irqrestore(&priv->lock, flags);
1829
Johannes Berg05c914f2008-09-11 00:01:58 +02001830 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -07001831 if (beacon_int == 0) {
1832 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1833 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1834 } else {
1835 priv->rxon_timing.beacon_interval =
1836 cpu_to_le16(beacon_int);
1837 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001838 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001839 le16_to_cpu(priv->rxon_timing.beacon_interval));
1840 }
1841
1842 priv->rxon_timing.atim_window = 0;
1843 } else {
1844 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001845 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001846 /* TODO: we need to get atim_window from upper stack
1847 * for now we set to 0 */
1848 priv->rxon_timing.atim_window = 0;
1849 }
1850
1851 interval_tm_unit =
1852 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1853 result = do_div(tsf, interval_tm_unit);
1854 priv->rxon_timing.beacon_init_val =
1855 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1856
1857 IWL_DEBUG_ASSOC
1858 ("beacon interval %d beacon timer %d beacon tim %d\n",
1859 le16_to_cpu(priv->rxon_timing.beacon_interval),
1860 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1861 le16_to_cpu(priv->rxon_timing.atim_window));
1862}
1863
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001864static int iwl3945_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001865{
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08001866 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001867 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1868 return -EIO;
1869 }
1870
1871 if (test_bit(STATUS_SCANNING, &priv->status)) {
1872 IWL_DEBUG_SCAN("Scan already in progress.\n");
1873 return -EAGAIN;
1874 }
1875
1876 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1877 IWL_DEBUG_SCAN("Scan request while abort pending. "
1878 "Queuing.\n");
1879 return -EAGAIN;
1880 }
1881
1882 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08001883 if (priv->cfg->sku & IWL_SKU_G)
1884 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
1885 if (priv->cfg->sku & IWL_SKU_A)
1886 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07001887 set_bit(STATUS_SCANNING, &priv->status);
1888 priv->scan_start = jiffies;
1889 priv->scan_pass_start = priv->scan_start;
1890
1891 queue_work(priv->workqueue, &priv->request_scan);
1892
1893 return 0;
1894}
1895
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001896static int iwl3945_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07001897{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001898 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001899
1900 if (hw_decrypt)
1901 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
1902 else
1903 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
1904
1905 return 0;
1906}
1907
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001908static void iwl3945_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001909 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001910{
Johannes Berg8318d782008-01-24 19:38:38 +01001911 if (band == IEEE80211_BAND_5GHZ) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001912 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07001913 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1914 | RXON_FLG_CCK_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001915 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001916 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001917 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001918 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001919 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001920 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001921 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001922
Johannes Berg05c914f2008-09-11 00:01:58 +02001923 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001924 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001925
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001926 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1927 priv->staging39_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1928 priv->staging39_rxon.flags &= ~RXON_FLG_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001929 }
1930}
1931
1932/*
Ian Schram01ebd062007-10-25 17:15:22 +08001933 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001934 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001935static void iwl3945_connection_init_rx_config(struct iwl_priv *priv,
Zhu, Yi60294de2008-10-29 14:05:45 -07001936 int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001937{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001938 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001939
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001940 memset(&priv->staging39_rxon, 0, sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001941
Zhu, Yi60294de2008-10-29 14:05:45 -07001942 switch (mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001943 case NL80211_IFTYPE_AP:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001944 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_AP;
Zhu Yib481de92007-09-25 17:54:57 -07001945 break;
1946
Johannes Berg05c914f2008-09-11 00:01:58 +02001947 case NL80211_IFTYPE_STATION:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001948 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_ESS;
1949 priv->staging39_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001950 break;
1951
Johannes Berg05c914f2008-09-11 00:01:58 +02001952 case NL80211_IFTYPE_ADHOC:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001953 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1954 priv->staging39_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1955 priv->staging39_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001956 RXON_FILTER_ACCEPT_GRP_MSK;
1957 break;
1958
Johannes Berg05c914f2008-09-11 00:01:58 +02001959 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001960 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1961 priv->staging39_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001962 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1963 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001964 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001965 IWL_ERR(priv, "Unsupported interface type %d\n", mode);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001966 break;
Zhu Yib481de92007-09-25 17:54:57 -07001967 }
1968
1969#if 0
1970 /* TODO: Figure out when short_preamble would be set and cache from
1971 * that */
1972 if (!hw_to_local(priv->hw)->short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001973 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001974 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001975 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001976#endif
1977
Johannes Berg8318d782008-01-24 19:38:38 +01001978 ch_info = iwl3945_get_channel_info(priv, priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001979 le16_to_cpu(priv->active39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001980
1981 if (!ch_info)
1982 ch_info = &priv->channel_info[0];
1983
1984 /*
1985 * in some case A channels are all non IBSS
1986 * in this case force B/G channel
1987 */
Zhu, Yi60294de2008-10-29 14:05:45 -07001988 if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
Zhu Yib481de92007-09-25 17:54:57 -07001989 ch_info = &priv->channel_info[0];
1990
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001991 priv->staging39_rxon.channel = cpu_to_le16(ch_info->channel);
Zhu Yib481de92007-09-25 17:54:57 -07001992 if (is_channel_a_band(ch_info))
Johannes Berg8318d782008-01-24 19:38:38 +01001993 priv->band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07001994 else
Johannes Berg8318d782008-01-24 19:38:38 +01001995 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07001996
Johannes Berg8318d782008-01-24 19:38:38 +01001997 iwl3945_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07001998
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001999 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002000 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002001 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002002 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2003}
2004
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002005static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002006{
Johannes Berg05c914f2008-09-11 00:01:58 +02002007 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002008 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002009
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002010 ch_info = iwl3945_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002011 priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002012 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002013
2014 if (!ch_info || !is_channel_ibss(ch_info)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002015 IWL_ERR(priv, "channel %d not IBSS channel\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002016 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002017 return -EINVAL;
2018 }
2019 }
2020
Zhu, Yi60294de2008-10-29 14:05:45 -07002021 iwl3945_connection_init_rx_config(priv, mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002022 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07002023
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002024 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002025
Tomas Winklera96a27f2008-10-23 23:48:56 -07002026 /* don't commit rxon if rf-kill is on*/
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002027 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08002028 return -EAGAIN;
2029
2030 cancel_delayed_work(&priv->scan_check);
2031 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002032 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08002033 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2034 return -EAGAIN;
2035 }
2036
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002037 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002038
2039 return 0;
2040}
2041
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002042static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +02002043 struct ieee80211_tx_info *info,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002044 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002045 struct sk_buff *skb_frag,
2046 int last_frag)
2047{
Ivo van Doorn1c014422008-04-17 19:41:02 +02002048 struct iwl3945_hw_key *keyinfo =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002049 &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002050
2051 switch (keyinfo->alg) {
2052 case ALG_CCMP:
2053 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2054 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002055 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002056 break;
2057
2058 case ALG_TKIP:
2059#if 0
2060 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2061
2062 if (last_frag)
2063 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2064 8);
2065 else
2066 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2067#endif
2068 break;
2069
2070 case ALG_WEP:
2071 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02002072 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07002073
2074 if (keyinfo->keylen == 13)
2075 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2076
2077 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2078
2079 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02002080 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002081 break;
2082
Zhu Yib481de92007-09-25 17:54:57 -07002083 default:
Tomas Winkler978785a2008-12-19 10:37:31 +08002084 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -07002085 break;
2086 }
2087}
2088
2089/*
2090 * handle build REPLY_TX command notification.
2091 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002092static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002093 struct iwl_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02002094 struct ieee80211_tx_info *info,
Zhu Yib481de92007-09-25 17:54:57 -07002095 struct ieee80211_hdr *hdr,
2096 int is_unicast, u8 std_id)
2097{
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002098 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002099 __le32 tx_flags = cmd->cmd.tx.tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +02002100 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07002101
2102 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02002103 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07002104 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002105 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002106 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002107 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07002108 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2109 tx_flags |= TX_CMD_FLG_TSF_MSK;
2110 } else {
2111 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2112 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2113 }
2114
2115 cmd->cmd.tx.sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002116 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002117 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2118
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002119 if (ieee80211_is_data_qos(fc)) {
2120 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +08002121 cmd->cmd.tx.tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07002122 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002123 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002124 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002125 }
Zhu Yib481de92007-09-25 17:54:57 -07002126
Johannes Berge6a98542008-10-21 12:40:02 +02002127 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07002128 tx_flags |= TX_CMD_FLG_RTS_MSK;
2129 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02002130 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07002131 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2132 tx_flags |= TX_CMD_FLG_CTS_MSK;
2133 }
2134
2135 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2136 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2137
2138 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002139 if (ieee80211_is_mgmt(fc)) {
2140 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Ian Schrambc434dd2007-10-25 17:15:29 +08002141 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002142 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002143 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002144 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002145 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002146#ifdef CONFIG_IWL3945_LEDS
2147 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
2148#endif
2149 }
Zhu Yib481de92007-09-25 17:54:57 -07002150
2151 cmd->cmd.tx.driver_txop = 0;
2152 cmd->cmd.tx.tx_flags = tx_flags;
2153 cmd->cmd.tx.next_frame_len = 0;
2154}
2155
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002156/**
2157 * iwl3945_get_sta_id - Find station's index within station table
2158 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002159static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002160{
2161 int sta_id;
2162 u16 fc = le16_to_cpu(hdr->frame_control);
2163
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002164 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002165 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2166 is_multicast_ether_addr(hdr->addr1))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002167 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002168
2169 switch (priv->iw_mode) {
2170
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002171 /* If we are a client station in a BSS network, use the special
2172 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02002173 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002174 return IWL_AP_ID;
2175
2176 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02002177 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002178 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002179 if (sta_id != IWL_INVALID_STATION)
2180 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002181 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002182
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002183 /* If this frame is going out to an IBSS network, find the station,
2184 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02002185 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002186 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002187 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002188 if (sta_id != IWL_INVALID_STATION)
2189 return sta_id;
2190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002191 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002192
2193 if (sta_id != IWL_INVALID_STATION)
2194 return sta_id;
2195
Johannes Berge1749612008-10-27 15:59:26 -07002196 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002197 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07002198 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002199 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002200 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002201 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08002202 /* If we are in monitor mode, use BCAST. This is required for
2203 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02002204 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002205 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +08002206
Zhu Yib481de92007-09-25 17:54:57 -07002207 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002208 IWL_WARN(priv, "Unknown mode of operation: %d\n",
2209 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002210 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002211 }
2212}
2213
2214/*
2215 * start REPLY_TX command process
2216 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002217static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002218{
2219 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02002220 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002221 struct iwl3945_tfd *tfd;
Johannes Berge2530082008-05-17 00:57:14 +02002222 int txq_id = skb_get_queue_mapping(skb);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002223 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002224 struct iwl_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002225 dma_addr_t phys_addr;
2226 dma_addr_t txcmd_phys;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002227 struct iwl_cmd *out_cmd = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002228 u16 len, idx, len_org, hdr_len;
2229 u8 id;
2230 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07002231 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002232 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002233 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002234 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07002235 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002236 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002237 unsigned long flags;
2238 int rc;
2239
2240 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002241 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002242 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2243 goto drop_unlock;
2244 }
2245
Johannes Berge039fa42008-05-15 12:55:29 +02002246 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002247 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002248 goto drop_unlock;
2249 }
2250
2251 unicast = !is_multicast_ether_addr(hdr->addr1);
2252 id = 0;
2253
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002254 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002255
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002256#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002257 if (ieee80211_is_auth(fc))
2258 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002259 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002260 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002261 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002262 IWL_DEBUG_TX("Sending REASSOC frame\n");
2263#endif
2264
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002265 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002266 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002267 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002268 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02002269 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002270 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002271 goto drop_unlock;
2272 }
2273
2274 spin_unlock_irqrestore(&priv->lock, flags);
2275
Harvey Harrison7294ec92008-07-15 18:43:59 -07002276 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002277
2278 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002279 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002280 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002281 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
2282 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002283 goto drop;
2284 }
2285
2286 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2287
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002288 if (ieee80211_is_data_qos(fc)) {
2289 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07002290 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002291 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -07002292 IEEE80211_SCTL_SEQ;
2293 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2294 (hdr->seq_ctrl &
2295 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2296 seq_number += 0x10;
2297 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002298
2299 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002300 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002301 q = &txq->q;
2302
2303 spin_lock_irqsave(&priv->lock, flags);
2304
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002305 /* Set up first empty TFD within this queue's circular TFD buffer */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002306 tfd = &txq->tfds39[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002307 memset(tfd, 0, sizeof(*tfd));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002308 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002309
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002310 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002311 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002312 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002313
2314 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002315 out_cmd = txq->cmd[idx];
Zhu Yib481de92007-09-25 17:54:57 -07002316 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2317 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002318
2319 /*
2320 * Set up the Tx-command (not MAC!) header.
2321 * Store the chosen Tx queue and TFD index within the sequence field;
2322 * after Tx, uCode's Tx response will return this value so driver can
2323 * locate the frame within the tx queue and do post-tx processing.
2324 */
Zhu Yib481de92007-09-25 17:54:57 -07002325 out_cmd->hdr.cmd = REPLY_TX;
2326 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002327 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002328
2329 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002330 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2331
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002332 /*
2333 * Use the first empty entry in this queue's command buffer array
2334 * to contain the Tx command and MAC header concatenated together
2335 * (payload data will be in another buffer).
2336 * Size of this varies, due to varying MAC header length.
2337 * If end is not dword aligned, we'll have 2 extra bytes at the end
2338 * of the MAC header (device reads on dword boundaries).
2339 * We'll tell device about this padding later.
2340 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002341 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +08002342 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002343
2344 len_org = len;
2345 len = (len + 3) & ~3;
2346
2347 if (len_org != len)
2348 len_org = 1;
2349 else
2350 len_org = 0;
2351
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002352 /* Physical address of this Tx command's header (not MAC header!),
2353 * within command buffer array. */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002354 txcmd_phys = pci_map_single(priv->pci_dev,
2355 out_cmd, sizeof(struct iwl_cmd),
2356 PCI_DMA_TODEVICE);
2357 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
2358 pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
2359 /* Add buffer containing Tx command and MAC(!) header to TFD's
2360 * first entry */
2361 txcmd_phys += offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002362
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002363 /* Add buffer containing Tx command and MAC(!) header to TFD's
2364 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002365 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002366
Johannes Bergd0f09802008-07-29 11:32:07 +02002367 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02002368 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002369
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002370 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2371 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002372 len = skb->len - hdr_len;
2373 if (len) {
2374 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2375 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002376 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002377 }
2378
Zhu Yib481de92007-09-25 17:54:57 -07002379 if (!len)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002380 /* If there is no payload, then we use only one Tx buffer */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002381 tfd->control_flags = cpu_to_le32(TFD_CTL_COUNT_SET(1));
Zhu Yib481de92007-09-25 17:54:57 -07002382 else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002383 /* Else use 2 buffers.
2384 * Tell 3945 about any padding after MAC header */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002385 tfd->control_flags = cpu_to_le32(TFD_CTL_COUNT_SET(2) |
2386 TFD_CTL_PAD_SET(U32_PAD(len)));
Zhu Yib481de92007-09-25 17:54:57 -07002387
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002388 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002389 len = (u16)skb->len;
2390 out_cmd->cmd.tx.len = cpu_to_le16(len);
2391
2392 /* TODO need this for burst mode later on */
Johannes Berge039fa42008-05-15 12:55:29 +02002393 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002394
2395 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02002396 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002397
2398 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2399 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
2400
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002401 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07002402 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002403 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002404 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07002405 } else {
2406 wait_write_ptr = 1;
2407 txq->need_update = 0;
2408 }
2409
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002410 iwl_print_hex_dump(priv, IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002411 sizeof(out_cmd->cmd.tx));
2412
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002413 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07002414 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07002415
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002416 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002417 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002418 rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002419 spin_unlock_irqrestore(&priv->lock, flags);
2420
2421 if (rc)
2422 return rc;
2423
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002424 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002425 && priv->mac80211_registered) {
2426 if (wait_write_ptr) {
2427 spin_lock_irqsave(&priv->lock, flags);
2428 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002429 iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002430 spin_unlock_irqrestore(&priv->lock, flags);
2431 }
2432
Johannes Berge2530082008-05-17 00:57:14 +02002433 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07002434 }
2435
2436 return 0;
2437
2438drop_unlock:
2439 spin_unlock_irqrestore(&priv->lock, flags);
2440drop:
2441 return -1;
2442}
2443
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002444static void iwl3945_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002445{
Johannes Berg8318d782008-01-24 19:38:38 +01002446 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002447 struct ieee80211_rate *rate;
2448 int i;
2449
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08002450 sband = iwl_get_hw_mode(priv, priv->band);
Johannes Berg8318d782008-01-24 19:38:38 +01002451 if (!sband) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002452 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002453 return;
2454 }
Zhu Yib481de92007-09-25 17:54:57 -07002455
2456 priv->active_rate = 0;
2457 priv->active_rate_basic = 0;
2458
Johannes Berg8318d782008-01-24 19:38:38 +01002459 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
2460 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07002461
Johannes Berg8318d782008-01-24 19:38:38 +01002462 for (i = 0; i < sband->n_bitrates; i++) {
2463 rate = &sband->bitrates[i];
2464 if ((rate->hw_value < IWL_RATE_COUNT) &&
2465 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
2466 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
2467 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
2468 priv->active_rate |= (1 << rate->hw_value);
2469 }
Zhu Yib481de92007-09-25 17:54:57 -07002470 }
2471
2472 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2473 priv->active_rate, priv->active_rate_basic);
2474
2475 /*
2476 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2477 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2478 * OFDM
2479 */
2480 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002481 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002482 ((priv->active_rate_basic &
2483 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2484 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002485 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002486 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2487
2488 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002489 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002490 ((priv->active_rate_basic &
2491 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2492 IWL_FIRST_OFDM_RATE) & 0xFF;
2493 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002494 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002495 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2496}
2497
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002498static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002499{
2500 unsigned long flags;
2501
2502 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2503 return;
2504
2505 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2506 disable_radio ? "OFF" : "ON");
2507
2508 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002509 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002510 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02002511 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002512 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002513 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002514 CSR_UCODE_SW_BIT_RFKILL);
2515 spin_unlock_irqrestore(&priv->lock, flags);
Samuel Ortizc4962942008-12-22 11:31:18 +08002516 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002517 set_bit(STATUS_RF_KILL_SW, &priv->status);
2518 }
2519 return;
2520 }
2521
2522 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002523 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002524
2525 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2526 spin_unlock_irqrestore(&priv->lock, flags);
2527
2528 /* wake up ucode */
2529 msleep(10);
2530
2531 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002532 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2533 if (!iwl_grab_nic_access(priv))
2534 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002535 spin_unlock_irqrestore(&priv->lock, flags);
2536
2537 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2538 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2539 "disabled by HW switch\n");
2540 return;
2541 }
2542
Zhu Yi808e72a2008-06-12 09:47:17 +08002543 if (priv->is_open)
2544 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002545 return;
2546}
2547
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002548void iwl3945_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002549 u32 decrypt_res, struct ieee80211_rx_status *stats)
2550{
2551 u16 fc =
2552 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2553
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002554 if (priv->active39_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002555 return;
2556
2557 if (!(fc & IEEE80211_FCTL_PROTECTED))
2558 return;
2559
2560 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2561 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2562 case RX_RES_STATUS_SEC_TYPE_TKIP:
2563 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2564 RX_RES_STATUS_BAD_ICV_MIC)
2565 stats->flag |= RX_FLAG_MMIC_ERROR;
2566 case RX_RES_STATUS_SEC_TYPE_WEP:
2567 case RX_RES_STATUS_SEC_TYPE_CCMP:
2568 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2569 RX_RES_STATUS_DECRYPT_OK) {
2570 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2571 stats->flag |= RX_FLAG_DECRYPTED;
2572 }
2573 break;
2574
2575 default:
2576 break;
2577 }
2578}
2579
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002580#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002581
2582#include "iwl-spectrum.h"
2583
2584#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2585#define BEACON_TIME_MASK_HIGH 0xFF000000
2586#define TIME_UNIT 1024
2587
2588/*
2589 * extended beacon time format
2590 * time in usec will be changed into a 32-bit value in 8:24 format
2591 * the high 1 byte is the beacon counts
2592 * the lower 3 bytes is the time in usec within one beacon interval
2593 */
2594
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002595static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002596{
2597 u32 quot;
2598 u32 rem;
2599 u32 interval = beacon_interval * 1024;
2600
2601 if (!interval || !usec)
2602 return 0;
2603
2604 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2605 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2606
2607 return (quot << 24) + rem;
2608}
2609
2610/* base is usually what we get from ucode with each received frame,
2611 * the same as HW timer counter counting down
2612 */
2613
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002614static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002615{
2616 u32 base_low = base & BEACON_TIME_MASK_LOW;
2617 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2618 u32 interval = beacon_interval * TIME_UNIT;
2619 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2620 (addon & BEACON_TIME_MASK_HIGH);
2621
2622 if (base_low > addon_low)
2623 res += base_low - addon_low;
2624 else if (base_low < addon_low) {
2625 res += interval + base_low - addon_low;
2626 res += (1 << 24);
2627 } else
2628 res += (1 << 24);
2629
2630 return cpu_to_le32(res);
2631}
2632
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002633static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002634 struct ieee80211_measurement_params *params,
2635 u8 type)
2636{
Tomas Winkler600c0e12008-12-19 10:37:04 +08002637 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002638 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002639 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002640 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2641 .data = (void *)&spectrum,
2642 .meta.flags = CMD_WANT_SKB,
2643 };
2644 u32 add_time = le64_to_cpu(params->start_time);
2645 int rc;
2646 int spectrum_resp_status;
2647 int duration = le16_to_cpu(params->duration);
2648
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002649 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002650 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002651 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002652 le64_to_cpu(params->start_time) - priv->last_tsf,
2653 le16_to_cpu(priv->rxon_timing.beacon_interval));
2654
2655 memset(&spectrum, 0, sizeof(spectrum));
2656
2657 spectrum.channel_count = cpu_to_le16(1);
2658 spectrum.flags =
2659 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2660 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2661 cmd.len = sizeof(spectrum);
2662 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2663
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002664 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002665 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002666 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002667 add_time,
2668 le16_to_cpu(priv->rxon_timing.beacon_interval));
2669 else
2670 spectrum.start_time = 0;
2671
2672 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2673 spectrum.channels[0].channel = params->channel;
2674 spectrum.channels[0].type = type;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002675 if (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002676 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2677 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2678
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002679 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002680 if (rc)
2681 return rc;
2682
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002683 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002684 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002685 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07002686 rc = -EIO;
2687 }
2688
2689 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2690 switch (spectrum_resp_status) {
2691 case 0: /* Command will be handled */
2692 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002693 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
2694 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07002695 priv->measurement_status &= ~MEASUREMENT_READY;
2696 }
2697 priv->measurement_status |= MEASUREMENT_ACTIVE;
2698 rc = 0;
2699 break;
2700
2701 case 1: /* Command will not be handled */
2702 rc = -EAGAIN;
2703 break;
2704 }
2705
2706 dev_kfree_skb_any(cmd.meta.u.skb);
2707
2708 return rc;
2709}
2710#endif
2711
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002712static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002713 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002714{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002715 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
2716 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002717 struct delayed_work *pwork;
2718
2719 palive = &pkt->u.alive_frame;
2720
2721 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2722 "0x%01X 0x%01X\n",
2723 palive->is_valid, palive->ver_type,
2724 palive->ver_subtype);
2725
2726 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2727 IWL_DEBUG_INFO("Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002728 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
2729 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002730 pwork = &priv->init_alive_start;
2731 } else {
2732 IWL_DEBUG_INFO("Runtime Alive received.\n");
2733 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002734 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002735 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002736 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002737 }
2738
2739 /* We delay the ALIVE response by 5ms to
2740 * give the HW RF Kill time to activate... */
2741 if (palive->is_valid == UCODE_VALID_OK)
2742 queue_delayed_work(priv->workqueue, pwork,
2743 msecs_to_jiffies(5));
2744 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002745 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002746}
2747
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002748static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002749 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002750{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002751 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002752
2753 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2754 return;
2755}
2756
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002757static void iwl3945_rx_reply_error(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002758 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002759{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002760 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002761
Winkler, Tomas15b16872008-12-19 10:37:33 +08002762 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
Zhu Yib481de92007-09-25 17:54:57 -07002763 "seq 0x%04X ser 0x%08X\n",
2764 le32_to_cpu(pkt->u.err_resp.error_type),
2765 get_cmd_string(pkt->u.err_resp.cmd_id),
2766 pkt->u.err_resp.cmd_id,
2767 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2768 le32_to_cpu(pkt->u.err_resp.error_info));
2769}
2770
2771#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2772
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002773static void iwl3945_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002774{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002775 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002776 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active39_rxon;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002777 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002778 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2779 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2780 rxon->channel = csa->channel;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002781 priv->staging39_rxon.channel = csa->channel;
Zhu Yib481de92007-09-25 17:54:57 -07002782}
2783
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002784static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002785 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002786{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002787#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002788 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002789 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002790
2791 if (!report->state) {
2792 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2793 "Spectrum Measure Notification: Start\n");
2794 return;
2795 }
2796
2797 memcpy(&priv->measure_report, report, sizeof(*report));
2798 priv->measurement_status |= MEASUREMENT_READY;
2799#endif
2800}
2801
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002802static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002803 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002804{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002805#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002806 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002807 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002808 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2809 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2810#endif
2811}
2812
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002813static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002814 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002815{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002816 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002817 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2818 "notification for %s:\n",
2819 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002820 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
2821 le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002822}
2823
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002824static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002825{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002826 struct iwl_priv *priv =
2827 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002828 struct sk_buff *beacon;
2829
2830 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02002831 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07002832
2833 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002834 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07002835 return;
2836 }
2837
2838 mutex_lock(&priv->mutex);
2839 /* new beacon skb is allocated every time; dispose previous.*/
2840 if (priv->ibss_beacon)
2841 dev_kfree_skb(priv->ibss_beacon);
2842
2843 priv->ibss_beacon = beacon;
2844 mutex_unlock(&priv->mutex);
2845
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002846 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002847}
2848
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002849static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002850 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002851{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002852#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002853 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002854 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07002855 u8 rate = beacon->beacon_notify_hdr.rate;
2856
2857 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2858 "tsf %d %d rate %d\n",
2859 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2860 beacon->beacon_notify_hdr.failure_frame,
2861 le32_to_cpu(beacon->ibss_mgr_status),
2862 le32_to_cpu(beacon->high_tsf),
2863 le32_to_cpu(beacon->low_tsf), rate);
2864#endif
2865
Johannes Berg05c914f2008-09-11 00:01:58 +02002866 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07002867 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2868 queue_work(priv->workqueue, &priv->beacon_update);
2869}
2870
2871/* Service response to REPLY_SCAN_CMD (0x80) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002872static void iwl3945_rx_reply_scan(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002873 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002874{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002875#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002876 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002877 struct iwl_scanreq_notification *notif =
2878 (struct iwl_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002879
2880 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2881#endif
2882}
2883
2884/* Service SCAN_START_NOTIFICATION (0x82) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002885static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002886 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002887{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002888 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002889 struct iwl_scanstart_notification *notif =
2890 (struct iwl_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002891 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2892 IWL_DEBUG_SCAN("Scan start: "
2893 "%d [802.11%s] "
2894 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2895 notif->channel,
2896 notif->band ? "bg" : "a",
2897 notif->tsf_high,
2898 notif->tsf_low, notif->status, notif->beacon_timer);
2899}
2900
2901/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002902static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002903 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002904{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002905 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002906 struct iwl_scanresults_notification *notif =
2907 (struct iwl_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002908
2909 IWL_DEBUG_SCAN("Scan ch.res: "
2910 "%d [802.11%s] "
2911 "(TSF: 0x%08X:%08X) - %d "
2912 "elapsed=%lu usec (%dms since last)\n",
2913 notif->channel,
2914 notif->band ? "bg" : "a",
2915 le32_to_cpu(notif->tsf_high),
2916 le32_to_cpu(notif->tsf_low),
2917 le32_to_cpu(notif->statistics[0]),
2918 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2919 jiffies_to_msecs(elapsed_jiffies
2920 (priv->last_scan_jiffies, jiffies)));
2921
2922 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002923 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002924}
2925
2926/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002927static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002928 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002929{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002930 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002931 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002932
2933 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2934 scan_notif->scanned_channels,
2935 scan_notif->tsf_low,
2936 scan_notif->tsf_high, scan_notif->status);
2937
2938 /* The HW is no longer scanning */
2939 clear_bit(STATUS_SCAN_HW, &priv->status);
2940
2941 /* The scan completion notification came in, so kill that timer... */
2942 cancel_delayed_work(&priv->scan_check);
2943
2944 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002945 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
2946 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07002947 jiffies_to_msecs(elapsed_jiffies
2948 (priv->scan_pass_start, jiffies)));
2949
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002950 /* Remove this scanned band from the list of pending
2951 * bands to scan, band G precedes A in order of scanning
2952 * as seen in iwl3945_bg_request_scan */
2953 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
2954 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
2955 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
2956 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002957
2958 /* If a request to abort was given, or the scan did not succeed
2959 * then we reset the scan state machine and terminate,
2960 * re-queuing another scan if one has been requested */
2961 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2962 IWL_DEBUG_INFO("Aborted scan completed.\n");
2963 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2964 } else {
2965 /* If there are more bands on this scan pass reschedule */
2966 if (priv->scan_bands > 0)
2967 goto reschedule;
2968 }
2969
2970 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002971 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002972 IWL_DEBUG_INFO("Setting scan to off\n");
2973
2974 clear_bit(STATUS_SCANNING, &priv->status);
2975
2976 IWL_DEBUG_INFO("Scan took %dms\n",
2977 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2978
2979 queue_work(priv->workqueue, &priv->scan_completed);
2980
2981 return;
2982
2983reschedule:
2984 priv->scan_pass_start = jiffies;
2985 queue_work(priv->workqueue, &priv->request_scan);
2986}
2987
2988/* Handle notification from uCode that card's power state is changing
2989 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002990static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002991 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002992{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002993 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002994 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2995 unsigned long status = priv->status;
2996
2997 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2998 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2999 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3000
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003001 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003002 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3003
3004 if (flags & HW_CARD_DISABLED)
3005 set_bit(STATUS_RF_KILL_HW, &priv->status);
3006 else
3007 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3008
3009
3010 if (flags & SW_CARD_DISABLED)
3011 set_bit(STATUS_RF_KILL_SW, &priv->status);
3012 else
3013 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3014
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003015 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003016
3017 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3018 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3019 (test_bit(STATUS_RF_KILL_SW, &status) !=
3020 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3021 queue_work(priv->workqueue, &priv->rf_kill);
3022 else
3023 wake_up_interruptible(&priv->wait_command_queue);
3024}
3025
3026/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003027 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003028 *
3029 * Setup the RX handlers for each of the reply types sent from the uCode
3030 * to the host.
3031 *
3032 * This function chains into the hardware specific files for them to setup
3033 * any hardware specific handlers as well.
3034 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003035static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003036{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003037 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
3038 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
3039 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
3040 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003041 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003042 iwl3945_rx_spectrum_measure_notif;
3043 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003044 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003045 iwl3945_rx_pm_debug_statistics_notif;
3046 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003047
Ben Cahill9fbab512007-11-29 11:09:47 +08003048 /*
3049 * The same handler is used for both the REPLY to a discrete
3050 * statistics request from the host as well as for the periodic
3051 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003052 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003053 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
3054 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003055
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003056 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
3057 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003058 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003059 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003060 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003061 iwl3945_rx_scan_complete_notif;
3062 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003063
Ben Cahill9fbab512007-11-29 11:09:47 +08003064 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003065 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003066}
3067
3068/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08003069 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
3070 * When FW advances 'R' index, all entries between old and new 'R' index
3071 * need to be reclaimed.
3072 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003073static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003074 int txq_id, int index)
3075{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003076 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003077 struct iwl_queue *q = &txq->q;
Tomas Winkler91c066f2008-03-06 17:36:55 -08003078 int nfreed = 0;
3079
3080 if ((index >= q->n_bd) || (iwl3945_x2_queue_used(q, index) == 0)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003081 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
Tomas Winkler91c066f2008-03-06 17:36:55 -08003082 "is out of range [0-%d] %d %d.\n", txq_id,
3083 index, q->n_bd, q->write_ptr, q->read_ptr);
3084 return;
3085 }
3086
3087 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
3088 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3089 if (nfreed > 1) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003090 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003091 q->write_ptr, q->read_ptr);
3092 queue_work(priv->workqueue, &priv->restart);
3093 break;
3094 }
3095 nfreed++;
3096 }
3097}
3098
3099
3100/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003101 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003102 * @rxb: Rx buffer to reclaim
3103 *
3104 * If an Rx buffer has an async callback associated with it the callback
3105 * will be executed. The attached skb (if present) will only be freed
3106 * if the callback returns 1
3107 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003108static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003109 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003110{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003111 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003112 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3113 int txq_id = SEQ_TO_QUEUE(sequence);
3114 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003115 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Zhu Yib481de92007-09-25 17:54:57 -07003116 int cmd_index;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08003117 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003118
Zhu Yib481de92007-09-25 17:54:57 -07003119 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3120
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003121 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3122 cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Zhu Yib481de92007-09-25 17:54:57 -07003123
3124 /* Input error checking is done when commands are added to queue. */
3125 if (cmd->meta.flags & CMD_WANT_SKB) {
3126 cmd->meta.source->u.skb = rxb->skb;
3127 rxb->skb = NULL;
3128 } else if (cmd->meta.u.callback &&
3129 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3130 rxb->skb = NULL;
3131
Tomas Winkler91c066f2008-03-06 17:36:55 -08003132 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003133
3134 if (!(cmd->meta.flags & CMD_ASYNC)) {
3135 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3136 wake_up_interruptible(&priv->wait_command_queue);
3137 }
3138}
3139
3140/************************** RX-FUNCTIONS ****************************/
3141/*
3142 * Rx theory of operation
3143 *
3144 * The host allocates 32 DMA target addresses and passes the host address
3145 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3146 * 0 to 31
3147 *
3148 * Rx Queue Indexes
3149 * The host/firmware share two index registers for managing the Rx buffers.
3150 *
3151 * The READ index maps to the first position that the firmware may be writing
3152 * to -- the driver can read up to (but not including) this position and get
3153 * good data.
3154 * The READ index is managed by the firmware once the card is enabled.
3155 *
3156 * The WRITE index maps to the last position the driver has read from -- the
3157 * position preceding WRITE is the last slot the firmware can place a packet.
3158 *
3159 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3160 * WRITE = READ.
3161 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003162 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003163 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3164 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003165 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003166 * and fire the RX interrupt. The driver can then query the READ index and
3167 * process as many packets as possible, moving the WRITE index forward as it
3168 * resets the Rx queue buffers with new memory.
3169 *
3170 * The management in the driver is as follows:
3171 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3172 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003173 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003174 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003175 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3176 * 'processed' and 'read' driver indexes as well)
3177 * + A received packet is processed and handed to the kernel network stack,
3178 * detached from the iwl->rxq. The driver 'processed' index is updated.
3179 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3180 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3181 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3182 * were enough free buffers and RX_STALLED is set it is cleared.
3183 *
3184 *
3185 * Driver sequence:
3186 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003187 * iwl3945_rx_queue_alloc() Allocates rx_free
3188 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003189 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003190 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003191 * queue, updates firmware pointers, and updates
3192 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003193 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003194 *
3195 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003196 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003197 * READ INDEX, detaching the SKB from the pool.
3198 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003199 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003200 * slots.
3201 * ...
3202 *
3203 */
3204
3205/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003206 * iwl3945_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003207 */
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003208static int iwl3945_rx_queue_space(const struct iwl_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003209{
3210 int s = q->read - q->write;
3211 if (s <= 0)
3212 s += RX_QUEUE_SIZE;
3213 /* keep some buffer to not confuse full and empty queue */
3214 s -= 2;
3215 if (s < 0)
3216 s = 0;
3217 return s;
3218}
3219
3220/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003221 * iwl3945_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003222 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003223int iwl3945_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003224{
3225 u32 reg = 0;
3226 int rc = 0;
3227 unsigned long flags;
3228
3229 spin_lock_irqsave(&q->lock, flags);
3230
3231 if (q->need_update == 0)
3232 goto exit_unlock;
3233
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003234 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003235 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003236 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003237
3238 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003239 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003240 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3241 goto exit_unlock;
3242 }
3243
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003244 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003245 if (rc)
3246 goto exit_unlock;
3247
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003248 /* Device expects a multiple of 8 */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003249 iwl_write_direct32(priv, FH39_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003250 q->write & ~0x7);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003251 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003252
3253 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003254 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003255 /* Device expects a multiple of 8 */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003256 iwl_write32(priv, FH39_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003257
3258
3259 q->need_update = 0;
3260
3261 exit_unlock:
3262 spin_unlock_irqrestore(&q->lock, flags);
3263 return rc;
3264}
3265
3266/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003267 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003268 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003269static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003270 dma_addr_t dma_addr)
3271{
3272 return cpu_to_le32((u32)dma_addr);
3273}
3274
3275/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003276 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003277 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003278 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003279 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003280 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003281 *
3282 * This moves the 'write' index forward to catch up with 'processed', and
3283 * also updates the memory address in the firmware to reference the new
3284 * target buffer.
3285 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003286static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003287{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003288 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003289 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003290 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003291 unsigned long flags;
3292 int write, rc;
3293
3294 spin_lock_irqsave(&rxq->lock, flags);
3295 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003296 while ((iwl3945_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003297 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003298 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003299 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003300 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003301
3302 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003303 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003304 rxq->queue[rxq->write] = rxb;
3305 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3306 rxq->free_count--;
3307 }
3308 spin_unlock_irqrestore(&rxq->lock, flags);
3309 /* If the pre-allocated buffer pool is dropping low, schedule to
3310 * refill it */
3311 if (rxq->free_count <= RX_LOW_WATERMARK)
3312 queue_work(priv->workqueue, &priv->rx_replenish);
3313
3314
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003315 /* If we've added more space for the firmware to place data, tell it.
3316 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003317 if ((write != (rxq->write & ~0x7))
3318 || (abs(rxq->write - rxq->read) > 7)) {
3319 spin_lock_irqsave(&rxq->lock, flags);
3320 rxq->need_update = 1;
3321 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003322 rc = iwl3945_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003323 if (rc)
3324 return rc;
3325 }
3326
3327 return 0;
3328}
3329
3330/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003331 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003332 *
3333 * When moving to rx_free an SKB is allocated for the slot.
3334 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003335 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003336 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003337 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003338static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003339{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003340 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003341 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003342 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003343 unsigned long flags;
3344 spin_lock_irqsave(&rxq->lock, flags);
3345 while (!list_empty(&rxq->rx_used)) {
3346 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003347 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003348
3349 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003350 rxb->skb =
3351 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
3352 if (!rxb->skb) {
3353 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08003354 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07003355 /* We don't reschedule replenish work here -- we will
3356 * call the restock method and if it still needs
3357 * more buffers it will schedule replenish */
3358 break;
3359 }
Zhu Yi12342c42007-12-20 11:27:32 +08003360
3361 /* If radiotap head is required, reserve some headroom here.
3362 * The physical head count is a variable rx_stats->phy_count.
3363 * We reserve 4 bytes here. Plus these extra bytes, the
3364 * headroom of the physical head should be enough for the
3365 * radiotap head that iwl3945 supported. See iwl3945_rt.
3366 */
3367 skb_reserve(rxb->skb, 4);
3368
Zhu Yib481de92007-09-25 17:54:57 -07003369 priv->alloc_rxb_skb++;
3370 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003371
3372 /* Get physical address of RB/SKB */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003373 rxb->real_dma_addr =
Zhu Yib481de92007-09-25 17:54:57 -07003374 pci_map_single(priv->pci_dev, rxb->skb->data,
3375 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3376 list_add_tail(&rxb->list, &rxq->rx_free);
3377 rxq->free_count++;
3378 }
3379 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003380}
3381
3382/*
3383 * this should be called while priv->lock is locked
3384 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003385static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003386{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003387 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003388
3389 iwl3945_rx_allocate(priv);
3390 iwl3945_rx_queue_restock(priv);
3391}
3392
3393
3394void iwl3945_rx_replenish(void *data)
3395{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003396 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003397 unsigned long flags;
3398
3399 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003400
3401 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003402 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003403 spin_unlock_irqrestore(&priv->lock, flags);
3404}
3405
3406/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003407 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003408 * This free routine walks the list of POOL entries and if SKB is set to
3409 * non NULL it is unmapped and freed
3410 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003411static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003412{
3413 int i;
3414 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3415 if (rxq->pool[i].skb != NULL) {
3416 pci_unmap_single(priv->pci_dev,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003417 rxq->pool[i].real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003418 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3419 dev_kfree_skb(rxq->pool[i].skb);
3420 }
3421 }
3422
3423 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3424 rxq->dma_addr);
3425 rxq->bd = NULL;
3426}
3427
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003428int iwl3945_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003429{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003430 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003431 struct pci_dev *dev = priv->pci_dev;
3432 int i;
3433
3434 spin_lock_init(&rxq->lock);
3435 INIT_LIST_HEAD(&rxq->rx_free);
3436 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003437
3438 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003439 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3440 if (!rxq->bd)
3441 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003442
Zhu Yib481de92007-09-25 17:54:57 -07003443 /* Fill the rx_used queue with _all_ of the Rx buffers */
3444 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3445 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003446
Zhu Yib481de92007-09-25 17:54:57 -07003447 /* Set us so that we have processed and used all buffers, but have
3448 * not restocked the Rx queue with fresh buffers */
3449 rxq->read = rxq->write = 0;
3450 rxq->free_count = 0;
3451 rxq->need_update = 0;
3452 return 0;
3453}
3454
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003455void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003456{
3457 unsigned long flags;
3458 int i;
3459 spin_lock_irqsave(&rxq->lock, flags);
3460 INIT_LIST_HEAD(&rxq->rx_free);
3461 INIT_LIST_HEAD(&rxq->rx_used);
3462 /* Fill the rx_used queue with _all_ of the Rx buffers */
3463 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3464 /* In the reset function, these buffers may have been allocated
3465 * to an SKB, so we need to unmap and free potential storage */
3466 if (rxq->pool[i].skb != NULL) {
3467 pci_unmap_single(priv->pci_dev,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003468 rxq->pool[i].real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003469 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3470 priv->alloc_rxb_skb--;
3471 dev_kfree_skb(rxq->pool[i].skb);
3472 rxq->pool[i].skb = NULL;
3473 }
3474 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3475 }
3476
3477 /* Set us so that we have processed and used all buffers, but have
3478 * not restocked the Rx queue with fresh buffers */
3479 rxq->read = rxq->write = 0;
3480 rxq->free_count = 0;
3481 spin_unlock_irqrestore(&rxq->lock, flags);
3482}
3483
3484/* Convert linear signal-to-noise ratio into dB */
3485static u8 ratio2dB[100] = {
3486/* 0 1 2 3 4 5 6 7 8 9 */
3487 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3488 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3489 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3490 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3491 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3492 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3493 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3494 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3495 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3496 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3497};
3498
3499/* Calculates a relative dB value from a ratio of linear
3500 * (i.e. not dB) signal levels.
3501 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003502int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003503{
Adrian Bunk221c80c2008-02-02 23:19:01 +02003504 /* 1000:1 or higher just report as 60 dB */
3505 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003506 return 60;
3507
Adrian Bunk221c80c2008-02-02 23:19:01 +02003508 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003509 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02003510 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003511 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07003512
3513 /* We shouldn't see this */
3514 if (sig_ratio < 1)
3515 return 0;
3516
3517 /* Use table for ratios 1:1 - 99:1 */
3518 return (int)ratio2dB[sig_ratio];
3519}
3520
3521#define PERFECT_RSSI (-20) /* dBm */
3522#define WORST_RSSI (-95) /* dBm */
3523#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3524
3525/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3526 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3527 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003528int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003529{
3530 int sig_qual;
3531 int degradation = PERFECT_RSSI - rssi_dbm;
3532
3533 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3534 * as indicator; formula is (signal dbm - noise dbm).
3535 * SNR at or above 40 is a great signal (100%).
3536 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3537 * Weakest usable signal is usually 10 - 15 dB SNR. */
3538 if (noise_dbm) {
3539 if (rssi_dbm - noise_dbm >= 40)
3540 return 100;
3541 else if (rssi_dbm < noise_dbm)
3542 return 0;
3543 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3544
3545 /* Else use just the signal level.
3546 * This formula is a least squares fit of data points collected and
3547 * compared with a reference system that had a percentage (%) display
3548 * for signal quality. */
3549 } else
3550 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3551 (15 * RSSI_RANGE + 62 * degradation)) /
3552 (RSSI_RANGE * RSSI_RANGE);
3553
3554 if (sig_qual > 100)
3555 sig_qual = 100;
3556 else if (sig_qual < 1)
3557 sig_qual = 0;
3558
3559 return sig_qual;
3560}
3561
3562/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003563 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003564 *
3565 * Uses the priv->rx_handlers callback function array to invoke
3566 * the appropriate handlers, including command responses,
3567 * frame-received notifications, and other notifications.
3568 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003569static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003570{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003571 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003572 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003573 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003574 u32 r, i;
3575 int reclaim;
3576 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003577 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003578 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003579
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003580 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3581 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08003582 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07003583 i = rxq->read;
3584
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003585 if (iwl3945_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3586 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003587 /* Rx interrupt, but nothing sent from uCode */
3588 if (i == r)
3589 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3590
3591 while (i != r) {
3592 rxb = rxq->queue[i];
3593
Ben Cahill9fbab512007-11-29 11:09:47 +08003594 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003595 * then a bug has been introduced in the queue refilling
3596 * routines -- catch it here */
3597 BUG_ON(rxb == NULL);
3598
3599 rxq->queue[i] = NULL;
3600
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003601 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003602 IWL_RX_BUF_SIZE,
3603 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003604 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003605
3606 /* Reclaim a command buffer only if this packet is a response
3607 * to a (driver-originated) command.
3608 * If the packet (e.g. Rx frame) originated from uCode,
3609 * there is no command buffer to reclaim.
3610 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3611 * but apparently a few don't get set; catch them here. */
3612 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3613 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3614 (pkt->hdr.cmd != REPLY_TX);
3615
3616 /* Based on type of command response or notification,
3617 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003618 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003619 if (priv->rx_handlers[pkt->hdr.cmd]) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003620 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003621 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3622 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3623 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3624 } else {
3625 /* No handling needed */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003626 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003627 "r %d i %d No handler needed for %s, 0x%02x\n",
3628 r, i, get_cmd_string(pkt->hdr.cmd),
3629 pkt->hdr.cmd);
3630 }
3631
3632 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003633 /* Invoke any callbacks, transfer the skb to caller, and
3634 * fire off the (possibly) blocking iwl3945_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003635 * as we reclaim the driver command queue */
3636 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003637 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003638 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003639 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07003640 }
3641
3642 /* For now we just don't re-use anything. We can tweak this
3643 * later to try and re-use notification packets and SKBs that
3644 * fail to Rx correctly */
3645 if (rxb->skb != NULL) {
3646 priv->alloc_rxb_skb--;
3647 dev_kfree_skb_any(rxb->skb);
3648 rxb->skb = NULL;
3649 }
3650
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003651 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003652 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3653 spin_lock_irqsave(&rxq->lock, flags);
3654 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3655 spin_unlock_irqrestore(&rxq->lock, flags);
3656 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003657 /* If there are a lot of unused frames,
3658 * restock the Rx queue so ucode won't assert. */
3659 if (fill_rx) {
3660 count++;
3661 if (count >= 8) {
3662 priv->rxq.read = i;
3663 __iwl3945_rx_replenish(priv);
3664 count = 0;
3665 }
3666 }
Zhu Yib481de92007-09-25 17:54:57 -07003667 }
3668
3669 /* Backtrack one entry */
3670 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003671 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003672}
3673
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003674/**
3675 * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
3676 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003677static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003678 struct iwl_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003679{
3680 u32 reg = 0;
3681 int rc = 0;
3682 int txq_id = txq->q.id;
3683
3684 if (txq->need_update == 0)
3685 return rc;
3686
3687 /* if we're trying to save power */
3688 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3689 /* wake up nic if it's powered down ...
3690 * uCode will wake up, and interrupt us again, so next
3691 * time we'll skip this part. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003692 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003693
3694 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3695 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003696 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003697 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3698 return rc;
3699 }
3700
3701 /* restore this queue's parameters in nic hardware. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003702 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003703 if (rc)
3704 return rc;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003705 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003706 txq->q.write_ptr | (txq_id << 8));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003707 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003708
3709 /* else not in power-save mode, uCode will never sleep when we're
3710 * trying to tx (during RFKILL, we're not trying to tx). */
3711 } else
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003712 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003713 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003714
3715 txq->need_update = 0;
3716
3717 return rc;
3718}
3719
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003720#ifdef CONFIG_IWL3945_DEBUG
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003721static void iwl3945_print_rx_config_cmd(struct iwl_priv *priv,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003722 struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003723{
3724 IWL_DEBUG_RADIO("RX CONFIG:\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003725 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003726 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3727 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3728 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3729 le32_to_cpu(rxon->filter_flags));
3730 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3731 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3732 rxon->ofdm_basic_rates);
3733 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07003734 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
3735 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003736 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3737}
3738#endif
3739
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003740static void iwl3945_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003741{
3742 IWL_DEBUG_ISR("Enabling interrupts\n");
3743 set_bit(STATUS_INT_ENABLED, &priv->status);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003744 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003745}
3746
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003747
3748/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003749static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003750{
Tomas Winklera96a27f2008-10-23 23:48:56 -07003751 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003752 synchronize_irq(priv->pci_dev->irq);
3753 tasklet_kill(&priv->irq_tasklet);
3754}
3755
3756
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003757static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003758{
3759 clear_bit(STATUS_INT_ENABLED, &priv->status);
3760
3761 /* disable interrupts from uCode/NIC to host */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003762 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003763
3764 /* acknowledge/clear/reset any interrupts still pending
3765 * from uCode or flow handler (Rx/Tx DMA) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003766 iwl_write32(priv, CSR_INT, 0xffffffff);
3767 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003768 IWL_DEBUG_ISR("Disabled interrupts\n");
3769}
3770
3771static const char *desc_lookup(int i)
3772{
3773 switch (i) {
3774 case 1:
3775 return "FAIL";
3776 case 2:
3777 return "BAD_PARAM";
3778 case 3:
3779 return "BAD_CHECKSUM";
3780 case 4:
3781 return "NMI_INTERRUPT";
3782 case 5:
3783 return "SYSASSERT";
3784 case 6:
3785 return "FATAL_ERROR";
3786 }
3787
3788 return "UNKNOWN";
3789}
3790
3791#define ERROR_START_OFFSET (1 * sizeof(u32))
3792#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3793
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003794static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003795{
3796 u32 i;
3797 u32 desc, time, count, base, data1;
3798 u32 blink1, blink2, ilink1, ilink2;
3799 int rc;
3800
3801 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3802
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003803 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003804 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003805 return;
3806 }
3807
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003808 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003809 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003810 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003811 return;
3812 }
3813
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003814 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003815
3816 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003817 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
3818 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
3819 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003820 }
3821
Winkler, Tomas15b16872008-12-19 10:37:33 +08003822 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07003823 "ilink1 nmiPC Line\n");
3824 for (i = ERROR_START_OFFSET;
3825 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
3826 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003827 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07003828 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003829 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003830 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003831 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003832 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003833 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003834 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003835 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003836 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003837 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003838 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003839 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003840
Winkler, Tomas15b16872008-12-19 10:37:33 +08003841 IWL_ERR(priv,
3842 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
3843 desc_lookup(desc), desc, time, blink1, blink2,
3844 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07003845 }
3846
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003847 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003848
3849}
3850
Ben Cahillf58177b2007-11-29 11:09:43 +08003851#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07003852
3853/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003854 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003855 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003856 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003857 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003858static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003859 u32 num_events, u32 mode)
3860{
3861 u32 i;
3862 u32 base; /* SRAM byte address of event log header */
3863 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3864 u32 ptr; /* SRAM byte address of log data */
3865 u32 ev, time, data; /* event log data */
3866
3867 if (num_events == 0)
3868 return;
3869
3870 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3871
3872 if (mode == 0)
3873 event_size = 2 * sizeof(u32);
3874 else
3875 event_size = 3 * sizeof(u32);
3876
3877 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3878
3879 /* "time" is actually "data" for mode 0 (no timestamp).
3880 * place event id # at far right for easier visual parsing. */
3881 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003882 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003883 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003884 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003885 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003886 if (mode == 0) {
3887 /* data, ev */
3888 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
3889 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003890 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003891 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003892 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07003893 }
3894 }
3895}
3896
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003897static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003898{
3899 int rc;
3900 u32 base; /* SRAM byte address of event log header */
3901 u32 capacity; /* event log capacity in # entries */
3902 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3903 u32 num_wraps; /* # times uCode wrapped to top of log */
3904 u32 next_entry; /* index of next entry to be written by uCode */
3905 u32 size; /* # entries that we'll print */
3906
3907 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003908 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003909 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003910 return;
3911 }
3912
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003913 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003914 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003915 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003916 return;
3917 }
3918
3919 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003920 capacity = iwl_read_targ_mem(priv, base);
3921 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3922 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3923 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003924
3925 size = num_wraps ? capacity : next_entry;
3926
3927 /* bail out if nothing in log */
3928 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003929 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003930 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003931 return;
3932 }
3933
Winkler, Tomas15b16872008-12-19 10:37:33 +08003934 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003935 size, num_wraps);
3936
3937 /* if uCode has wrapped back to top of log, start at the oldest entry,
3938 * i.e the next one that uCode would fill. */
3939 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003940 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003941 capacity - next_entry, mode);
3942
3943 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003944 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003945
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003946 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003947}
3948
3949/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003950 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003951 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003952static void iwl3945_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003953{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003954 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07003955 set_bit(STATUS_FW_ERROR, &priv->status);
3956
3957 /* Cancel currently queued command. */
3958 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3959
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003960#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003961 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003962 iwl3945_dump_nic_error_log(priv);
3963 iwl3945_dump_nic_event_log(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003964 iwl3945_print_rx_config_cmd(priv, &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07003965 }
3966#endif
3967
3968 wake_up_interruptible(&priv->wait_command_queue);
3969
3970 /* Keep the restart process from trying to send host
3971 * commands by clearing the INIT status bit */
3972 clear_bit(STATUS_READY, &priv->status);
3973
3974 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3975 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
3976 "Restarting adapter due to uCode error.\n");
3977
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003978 if (iwl3945_is_associated(priv)) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003979 memcpy(&priv->recovery39_rxon, &priv->active39_rxon,
3980 sizeof(priv->recovery39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003981 priv->error_recovering = 1;
3982 }
3983 queue_work(priv->workqueue, &priv->restart);
3984 }
3985}
3986
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003987static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003988{
3989 unsigned long flags;
3990
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003991 memcpy(&priv->staging39_rxon, &priv->recovery39_rxon,
3992 sizeof(priv->staging39_rxon));
3993 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003994 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003995
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003996 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003997
3998 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003999 priv->assoc_id = le16_to_cpu(priv->staging39_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07004000 priv->error_recovering = 0;
4001 spin_unlock_irqrestore(&priv->lock, flags);
4002}
4003
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004004static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004005{
4006 u32 inta, handled = 0;
4007 u32 inta_fh;
4008 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004009#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004010 u32 inta_mask;
4011#endif
4012
4013 spin_lock_irqsave(&priv->lock, flags);
4014
4015 /* Ack/clear/reset pending uCode interrupts.
4016 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4017 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004018 inta = iwl_read32(priv, CSR_INT);
4019 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004020
4021 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4022 * Any new interrupts that happen after this, either while we're
4023 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004024 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4025 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004026
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004027#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004028 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004029 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004030 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004031 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4032 inta, inta_mask, inta_fh);
4033 }
4034#endif
4035
4036 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4037 * atomic, make sure that inta covers all the interrupts that
4038 * we've discovered, even if FH interrupt came in just after
4039 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004040 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004041 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004042 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004043 inta |= CSR_INT_BIT_FH_TX;
4044
4045 /* Now service all interrupt bits discovered above. */
4046 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004047 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004048
4049 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004050 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004051
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004052 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004053
4054 handled |= CSR_INT_BIT_HW_ERR;
4055
4056 spin_unlock_irqrestore(&priv->lock, flags);
4057
4058 return;
4059 }
4060
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004061#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004062 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004063 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004064 if (inta & CSR_INT_BIT_SCD)
4065 IWL_DEBUG_ISR("Scheduler finished to transmit "
4066 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004067
4068 /* Alive notification via Rx interrupt will do the real work */
4069 if (inta & CSR_INT_BIT_ALIVE)
4070 IWL_DEBUG_ISR("Alive interrupt\n");
4071 }
4072#endif
4073 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004074 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004075
Zhu Yib481de92007-09-25 17:54:57 -07004076 /* Error detected by uCode */
4077 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004078 IWL_ERR(priv, "Microcode SW error detected. "
4079 "Restarting 0x%X.\n", inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004080 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004081 handled |= CSR_INT_BIT_SW_ERR;
4082 }
4083
4084 /* uCode wakes up after power-down sleep */
4085 if (inta & CSR_INT_BIT_WAKEUP) {
4086 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004087 iwl3945_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08004088 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4089 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4090 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4091 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4092 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4093 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004094
4095 handled |= CSR_INT_BIT_WAKEUP;
4096 }
4097
4098 /* All uCode command responses, including Tx command responses,
4099 * Rx "responses" (frame-received notification), and other
4100 * notifications from uCode come through here*/
4101 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004102 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004103 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4104 }
4105
4106 if (inta & CSR_INT_BIT_FH_TX) {
4107 IWL_DEBUG_ISR("Tx interrupt\n");
4108
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004109 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
4110 if (!iwl_grab_nic_access(priv)) {
4111 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08004112 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004113 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004114 }
4115 handled |= CSR_INT_BIT_FH_TX;
4116 }
4117
4118 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004119 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07004120
4121 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004122 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07004123 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004124 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004125 }
4126
4127 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004128 /* only Re-enable if disabled by irq */
4129 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4130 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004131
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004132#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004133 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004134 inta = iwl_read32(priv, CSR_INT);
4135 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4136 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004137 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4138 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4139 }
4140#endif
4141 spin_unlock_irqrestore(&priv->lock, flags);
4142}
4143
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004144static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004145{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004146 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004147 u32 inta, inta_mask;
4148 u32 inta_fh;
4149 if (!priv)
4150 return IRQ_NONE;
4151
4152 spin_lock(&priv->lock);
4153
4154 /* Disable (but don't clear!) interrupts here to avoid
4155 * back-to-back ISRs and sporadic interrupts from our NIC.
4156 * If we have something to service, the tasklet will re-enable ints.
4157 * If we *don't* have something, we'll re-enable before leaving here. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004158 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4159 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004160
4161 /* Discover which interrupts are active/pending */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004162 inta = iwl_read32(priv, CSR_INT);
4163 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004164
4165 /* Ignore interrupt if there's nothing in NIC to service.
4166 * This may be due to IRQ shared with another device,
4167 * or due to sporadic interrupts thrown from our NIC. */
4168 if (!inta && !inta_fh) {
4169 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4170 goto none;
4171 }
4172
4173 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4174 /* Hardware disappeared */
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004175 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004176 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004177 }
4178
4179 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4180 inta, inta_mask, inta_fh);
4181
Joonwoo Park25c03d82008-01-23 10:15:20 -08004182 inta &= ~CSR_INT_BIT_SCD;
4183
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004184 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004185 if (likely(inta || inta_fh))
4186 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004187unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07004188 spin_unlock(&priv->lock);
4189
4190 return IRQ_HANDLED;
4191
4192 none:
4193 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004194 /* only Re-enable if disabled by irq */
4195 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4196 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004197 spin_unlock(&priv->lock);
4198 return IRQ_NONE;
4199}
4200
4201/************************** EEPROM BANDS ****************************
4202 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004203 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07004204 * EEPROM contents to the specific channel number supported for each
4205 * band.
4206 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004207 * For example, iwl3945_priv->eeprom39.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07004208 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4209 * The specific geography and calibration information for that channel
4210 * is contained in the eeprom map itself.
4211 *
4212 * During init, we copy the eeprom information and channel map
4213 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4214 *
4215 * channel_map_24/52 provides the index in the channel_info array for a
4216 * given channel. We have to have two separate maps as there is channel
4217 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4218 * band_2
4219 *
4220 * A value of 0xff stored in the channel_map indicates that the channel
4221 * is not supported by the hardware at all.
4222 *
4223 * A value of 0xfe in the channel_map indicates that the channel is not
4224 * valid for Tx with the current hardware. This means that
4225 * while the system can tune and receive on a given channel, it may not
4226 * be able to associate or transmit any frames on that
4227 * channel. There is no corresponding channel information for that
4228 * entry.
4229 *
4230 *********************************************************************/
4231
4232/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004233static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07004234 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4235};
4236
4237/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08004238static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004239 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4240};
4241
Ben Cahill9fbab512007-11-29 11:09:47 +08004242static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004243 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4244};
4245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004246static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004247 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4248};
4249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004250static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004251 145, 149, 153, 157, 161, 165
4252};
4253
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004254static void iwl3945_init_band_reference(const struct iwl_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07004255 int *eeprom_ch_count,
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004256 const struct iwl_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07004257 **eeprom_ch_info,
4258 const u8 **eeprom_ch_index)
4259{
4260 switch (band) {
4261 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004262 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004263 *eeprom_ch_info = priv->eeprom39.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004264 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07004265 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004266 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004267 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004268 *eeprom_ch_info = priv->eeprom39.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004269 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07004270 break;
4271 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004272 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004273 *eeprom_ch_info = priv->eeprom39.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004274 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07004275 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004276 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004277 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004278 *eeprom_ch_info = priv->eeprom39.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004279 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07004280 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004281 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004282 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004283 *eeprom_ch_info = priv->eeprom39.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004284 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07004285 break;
4286 default:
4287 BUG();
4288 return;
4289 }
4290}
4291
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004292/**
4293 * iwl3945_get_channel_info - Find driver's private channel info
4294 *
4295 * Based on band and channel number.
4296 */
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004297const struct iwl_channel_info *
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004298iwl3945_get_channel_info(const struct iwl_priv *priv,
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004299 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07004300{
4301 int i;
4302
Johannes Berg8318d782008-01-24 19:38:38 +01004303 switch (band) {
4304 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004305 for (i = 14; i < priv->channel_count; i++) {
4306 if (priv->channel_info[i].channel == channel)
4307 return &priv->channel_info[i];
4308 }
4309 break;
4310
Johannes Berg8318d782008-01-24 19:38:38 +01004311 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004312 if (channel >= 1 && channel <= 14)
4313 return &priv->channel_info[channel - 1];
4314 break;
Johannes Berg8318d782008-01-24 19:38:38 +01004315 case IEEE80211_NUM_BANDS:
4316 WARN_ON(1);
Zhu Yib481de92007-09-25 17:54:57 -07004317 }
4318
4319 return NULL;
4320}
4321
4322#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4323 ? # x " " : "")
4324
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004325/**
4326 * iwl3945_init_channel_map - Set up driver's info for all possible channels
4327 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004328static int iwl3945_init_channel_map(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004329{
4330 int eeprom_ch_count = 0;
4331 const u8 *eeprom_ch_index = NULL;
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004332 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004333 int band, ch;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004334 struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004335
4336 if (priv->channel_count) {
4337 IWL_DEBUG_INFO("Channel map already initialized.\n");
4338 return 0;
4339 }
4340
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004341 if (priv->eeprom39.version < 0x2f) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004342 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004343 priv->eeprom39.version);
Zhu Yib481de92007-09-25 17:54:57 -07004344 return -EINVAL;
4345 }
4346
4347 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
4348
4349 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004350 ARRAY_SIZE(iwl3945_eeprom_band_1) +
4351 ARRAY_SIZE(iwl3945_eeprom_band_2) +
4352 ARRAY_SIZE(iwl3945_eeprom_band_3) +
4353 ARRAY_SIZE(iwl3945_eeprom_band_4) +
4354 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004355
4356 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
4357
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004358 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07004359 priv->channel_count, GFP_KERNEL);
4360 if (!priv->channel_info) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004361 IWL_ERR(priv, "Could not allocate channel_info\n");
Zhu Yib481de92007-09-25 17:54:57 -07004362 priv->channel_count = 0;
4363 return -ENOMEM;
4364 }
4365
4366 ch_info = priv->channel_info;
4367
4368 /* Loop through the 5 EEPROM bands adding them in order to the
4369 * channel map we maintain (that contains additional information than
4370 * what just in the EEPROM) */
4371 for (band = 1; band <= 5; band++) {
4372
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004373 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07004374 &eeprom_ch_info, &eeprom_ch_index);
4375
4376 /* Loop through each band adding each of the channels */
4377 for (ch = 0; ch < eeprom_ch_count; ch++) {
4378 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01004379 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
4380 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004381
4382 /* permanently store EEPROM's channel regulatory flags
4383 * and max power in channel info database. */
4384 ch_info->eeprom = eeprom_ch_info[ch];
4385
4386 /* Copy the run-time flags so they are there even on
4387 * invalid channels */
4388 ch_info->flags = eeprom_ch_info[ch].flags;
4389
4390 if (!(is_channel_valid(ch_info))) {
4391 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
4392 "No traffic\n",
4393 ch_info->channel,
4394 ch_info->flags,
4395 is_channel_a_band(ch_info) ?
4396 "5.2" : "2.4");
4397 ch_info++;
4398 continue;
4399 }
4400
4401 /* Initialize regulatory-based run-time data */
4402 ch_info->max_power_avg = ch_info->curr_txpow =
4403 eeprom_ch_info[ch].max_power_avg;
4404 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
4405 ch_info->min_power = 0;
4406
Guy Cohenfe7c4042008-04-21 15:41:56 -07004407 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07004408 " %ddBm): Ad-Hoc %ssupported\n",
4409 ch_info->channel,
4410 is_channel_a_band(ch_info) ?
4411 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02004412 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07004413 CHECK_AND_PRINT(IBSS),
4414 CHECK_AND_PRINT(ACTIVE),
4415 CHECK_AND_PRINT(RADAR),
4416 CHECK_AND_PRINT(WIDE),
Zhu Yib481de92007-09-25 17:54:57 -07004417 CHECK_AND_PRINT(DFS),
4418 eeprom_ch_info[ch].flags,
4419 eeprom_ch_info[ch].max_power_avg,
4420 ((eeprom_ch_info[ch].
4421 flags & EEPROM_CHANNEL_IBSS)
4422 && !(eeprom_ch_info[ch].
4423 flags & EEPROM_CHANNEL_RADAR))
4424 ? "" : "not ");
4425
4426 /* Set the user_txpower_limit to the highest power
4427 * supported by any channel */
4428 if (eeprom_ch_info[ch].max_power_avg >
4429 priv->user_txpower_limit)
4430 priv->user_txpower_limit =
4431 eeprom_ch_info[ch].max_power_avg;
4432
4433 ch_info++;
4434 }
4435 }
4436
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004437 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07004438 if (iwl3945_txpower_set_from_eeprom(priv))
4439 return -EIO;
4440
4441 return 0;
4442}
4443
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004444/*
4445 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
4446 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004447static void iwl3945_free_channel_map(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004448{
4449 kfree(priv->channel_info);
4450 priv->channel_count = 0;
4451}
4452
Zhu Yib481de92007-09-25 17:54:57 -07004453/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4454 * sending probe req. This should be set long enough to hear probe responses
4455 * from more than one AP. */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004456#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
4457#define IWL_ACTIVE_DWELL_TIME_52 (20)
4458
4459#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
4460#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Zhu Yib481de92007-09-25 17:54:57 -07004461
4462/* For faster active scanning, scan will move to the next channel if fewer than
4463 * PLCP_QUIET_THRESH packets are heard on this channel within
4464 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4465 * time if it's a quiet channel (nothing responded to our probe, and there's
4466 * no other traffic).
4467 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4468#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004469#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
Zhu Yib481de92007-09-25 17:54:57 -07004470
4471/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4472 * Must be set longer than active dwell time.
4473 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4474#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4475#define IWL_PASSIVE_DWELL_TIME_52 (10)
4476#define IWL_PASSIVE_DWELL_BASE (100)
4477#define IWL_CHANNEL_TUNE_TIME 5
4478
Kolekar, Abhijeete720ce92008-11-07 09:58:42 -08004479#define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004480
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004481static inline u16 iwl3945_get_active_dwell_time(struct iwl_priv *priv,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004482 enum ieee80211_band band,
4483 u8 n_probes)
Zhu Yib481de92007-09-25 17:54:57 -07004484{
Johannes Berg8318d782008-01-24 19:38:38 +01004485 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004486 return IWL_ACTIVE_DWELL_TIME_52 +
4487 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004488 else
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004489 return IWL_ACTIVE_DWELL_TIME_24 +
4490 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004491}
4492
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004493static u16 iwl3945_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004494 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004495{
Johannes Berg8318d782008-01-24 19:38:38 +01004496 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004497 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4498 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4499
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004500 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004501 /* If we're associated, we clamp the maximum passive
4502 * dwell time to be 98% of the beacon interval (minus
4503 * 2 * channel tune time) */
4504 passive = priv->beacon_int;
4505 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4506 passive = IWL_PASSIVE_DWELL_BASE;
4507 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4508 }
4509
Zhu Yib481de92007-09-25 17:54:57 -07004510 return passive;
4511}
4512
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004513static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004514 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004515 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004516 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004517{
4518 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004519 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004520 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004521 u16 passive_dwell = 0;
4522 u16 active_dwell = 0;
4523 int added, i;
4524
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08004525 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01004526 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004527 return 0;
4528
Johannes Berg8318d782008-01-24 19:38:38 +01004529 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004530
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004531 active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
Johannes Berg8318d782008-01-24 19:38:38 +01004532 passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004533
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08004534 if (passive_dwell <= active_dwell)
4535 passive_dwell = active_dwell + 1;
4536
Johannes Berg8318d782008-01-24 19:38:38 +01004537 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004538 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4539 continue;
4540
Johannes Berg8318d782008-01-24 19:38:38 +01004541 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07004542
Johannes Berg8318d782008-01-24 19:38:38 +01004543 ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004544 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004545 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004546 scan_ch->channel);
4547 continue;
4548 }
4549
Zhu Yib481de92007-09-25 17:54:57 -07004550 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4551 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08004552 /* If passive , set up for auto-switch
4553 * and use long active_dwell time.
4554 */
4555 if (!is_active || is_channel_passive(ch_info) ||
4556 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
4557 scan_ch->type = 0; /* passive */
4558 if (IWL_UCODE_API(priv->ucode_ver) == 1)
4559 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
4560 } else {
4561 scan_ch->type = 1; /* active */
4562 }
4563
4564 /* Set direct probe bits. These may be used both for active
4565 * scan channels (probes gets sent right away),
4566 * or for passive channels (probes get se sent only after
4567 * hearing clear Rx packet).*/
4568 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
4569 if (n_probes)
4570 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4571 } else {
4572 /* uCode v1 does not allow setting direct probe bits on
4573 * passive channel. */
4574 if ((scan_ch->type & 1) && n_probes)
4575 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4576 }
Zhu Yib481de92007-09-25 17:54:57 -07004577
Ben Cahill9fbab512007-11-29 11:09:47 +08004578 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004579 scan_ch->tpc.dsp_atten = 110;
4580 /* scan_pwr_info->tpc.dsp_atten; */
4581
4582 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004583 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004584 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4585 else {
4586 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4587 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004588 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004589 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004590 */
4591 }
4592
4593 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4594 scan_ch->channel,
4595 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4596 (scan_ch->type & 1) ?
4597 active_dwell : passive_dwell);
4598
4599 scan_ch++;
4600 added++;
4601 }
4602
4603 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4604 return added;
4605}
4606
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004607static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004608 struct ieee80211_rate *rates)
4609{
4610 int i;
4611
4612 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004613 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
4614 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4615 rates[i].hw_value_short = i;
4616 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08004617 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004618 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004619 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004620 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004621 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01004622 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004623 }
Zhu Yib481de92007-09-25 17:54:57 -07004624 }
4625}
4626
4627/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004628 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004629 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004630static int iwl3945_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004631{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004632 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004633 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004634 struct ieee80211_channel *channels;
4635 struct ieee80211_channel *geo_ch;
4636 struct ieee80211_rate *rates;
4637 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004638
Johannes Berg8318d782008-01-24 19:38:38 +01004639 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4640 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004641 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4642 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4643 return 0;
4644 }
4645
Zhu Yib481de92007-09-25 17:54:57 -07004646 channels = kzalloc(sizeof(struct ieee80211_channel) *
4647 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004648 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004649 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004650
Tomas Winkler8211ef72008-03-02 01:36:04 +02004651 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004652 GFP_KERNEL);
4653 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004654 kfree(channels);
4655 return -ENOMEM;
4656 }
4657
Zhu Yib481de92007-09-25 17:54:57 -07004658 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004659 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4660 sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
4661 /* just OFDM */
4662 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4663 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07004664
Tomas Winkler8211ef72008-03-02 01:36:04 +02004665 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4666 sband->channels = channels;
4667 /* OFDM & CCK */
4668 sband->bitrates = rates;
4669 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004670
4671 priv->ieee_channels = channels;
4672 priv->ieee_rates = rates;
4673
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004674 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004675
Tomas Winkler8211ef72008-03-02 01:36:04 +02004676 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004677 ch = &priv->channel_info[i];
4678
Tomas Winkler8211ef72008-03-02 01:36:04 +02004679 /* FIXME: might be removed if scan is OK*/
4680 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004681 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004682
4683 if (is_channel_a_band(ch))
Tomas Winkler8211ef72008-03-02 01:36:04 +02004684 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Johannes Berg8318d782008-01-24 19:38:38 +01004685 else
Tomas Winkler8211ef72008-03-02 01:36:04 +02004686 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004687
Tomas Winkler8211ef72008-03-02 01:36:04 +02004688 geo_ch = &sband->channels[sband->n_channels++];
4689
4690 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004691 geo_ch->max_power = ch->max_power_avg;
4692 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004693 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004694
4695 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004696 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4697 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004698
Johannes Berg8318d782008-01-24 19:38:38 +01004699 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4700 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004701
4702 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004703 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004704
4705 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4706 priv->max_channel_txpower_limit =
4707 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004708 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004709 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004710 }
4711
4712 /* Save flags for reg domain usage */
4713 geo_ch->orig_flags = geo_ch->flags;
4714
4715 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4716 ch->channel, geo_ch->center_freq,
4717 is_channel_a_band(ch) ? "5.2" : "2.4",
4718 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4719 "restricted" : "valid",
4720 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004721 }
4722
Tomas Winkler82b9a122008-03-04 18:09:30 -08004723 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4724 priv->cfg->sku & IWL_SKU_A) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004725 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
4726 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
4727 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004728 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004729 }
4730
Tomas Winkler978785a2008-12-19 10:37:31 +08004731 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004732 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4733 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004734
John W. Linvillee0e0a672008-03-25 15:58:40 -04004735 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4736 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4737 &priv->bands[IEEE80211_BAND_2GHZ];
4738 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4739 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4740 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004741
Zhu Yib481de92007-09-25 17:54:57 -07004742 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4743
4744 return 0;
4745}
4746
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004747/*
4748 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
4749 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004750static void iwl3945_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004751{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004752 kfree(priv->ieee_channels);
4753 kfree(priv->ieee_rates);
4754 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4755}
4756
Zhu Yib481de92007-09-25 17:54:57 -07004757/******************************************************************************
4758 *
4759 * uCode download functions
4760 *
4761 ******************************************************************************/
4762
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004763static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004764{
Tomas Winkler98c92212008-01-14 17:46:20 -08004765 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4766 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4767 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4768 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4769 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4770 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004771}
4772
4773/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004774 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004775 * looking at all data.
4776 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004777static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004778{
4779 u32 val;
4780 u32 save_len = len;
4781 int rc = 0;
4782 u32 errcnt;
4783
4784 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4785
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004786 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004787 if (rc)
4788 return rc;
4789
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004790 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004791 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004792
4793 errcnt = 0;
4794 for (; len > 0; len -= sizeof(u32), image++) {
4795 /* read data comes through single port, auto-incr addr */
4796 /* NOTE: Use the debugless read so we don't flood kernel log
4797 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004798 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004799 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004800 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004801 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4802 save_len - len, val, le32_to_cpu(*image));
4803 rc = -EIO;
4804 errcnt++;
4805 if (errcnt >= 20)
4806 break;
4807 }
4808 }
4809
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004810 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004811
4812 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08004813 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07004814
4815 return rc;
4816}
4817
4818
4819/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004820 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004821 * using sample data 100 bytes apart. If these sample points are good,
4822 * it's a pretty good bet that everything between them is good, too.
4823 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004824static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004825{
4826 u32 val;
4827 int rc = 0;
4828 u32 errcnt = 0;
4829 u32 i;
4830
4831 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4832
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004833 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004834 if (rc)
4835 return rc;
4836
4837 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4838 /* read data comes through single port, auto-incr addr */
4839 /* NOTE: Use the debugless read so we don't flood kernel log
4840 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004841 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004842 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004843 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004844 if (val != le32_to_cpu(*image)) {
4845#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08004846 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004847 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4848 i, val, *image);
4849#endif
4850 rc = -EIO;
4851 errcnt++;
4852 if (errcnt >= 3)
4853 break;
4854 }
4855 }
4856
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004857 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004858
4859 return rc;
4860}
4861
4862
4863/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004864 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07004865 * and verify its contents
4866 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004867static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004868{
4869 __le32 *image;
4870 u32 len;
4871 int rc = 0;
4872
4873 /* Try bootstrap */
4874 image = (__le32 *)priv->ucode_boot.v_addr;
4875 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004876 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004877 if (rc == 0) {
4878 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
4879 return 0;
4880 }
4881
4882 /* Try initialize */
4883 image = (__le32 *)priv->ucode_init.v_addr;
4884 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004885 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004886 if (rc == 0) {
4887 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
4888 return 0;
4889 }
4890
4891 /* Try runtime/protocol */
4892 image = (__le32 *)priv->ucode_code.v_addr;
4893 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004894 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004895 if (rc == 0) {
4896 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
4897 return 0;
4898 }
4899
Winkler, Tomas15b16872008-12-19 10:37:33 +08004900 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07004901
Ben Cahill9fbab512007-11-29 11:09:47 +08004902 /* Since nothing seems to match, show first several data entries in
4903 * instruction SRAM, so maybe visual inspection will give a clue.
4904 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07004905 image = (__le32 *)priv->ucode_boot.v_addr;
4906 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004907 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004908
4909 return rc;
4910}
4911
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004912static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004913{
4914 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004915 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004916}
4917
4918/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004919 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07004920 *
4921 * Copy into buffers for card to fetch via bus-mastering
4922 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004923static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004924{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08004925 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08004926 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07004927 const struct firmware *ucode_raw;
4928 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08004929 const char *name_pre = priv->cfg->fw_name_pre;
4930 const unsigned int api_max = priv->cfg->ucode_api_max;
4931 const unsigned int api_min = priv->cfg->ucode_api_min;
4932 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07004933 u8 *src;
4934 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08004935 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07004936
4937 /* Ask kernel firmware_class module to get the boot firmware off disk.
4938 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08004939 for (index = api_max; index >= api_min; index--) {
4940 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
4941 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
4942 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004943 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08004944 buf, ret);
4945 if (ret == -ENOENT)
4946 continue;
4947 else
4948 goto error;
4949 } else {
4950 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004951 IWL_ERR(priv, "Loaded firmware %s, "
4952 "which is deprecated. "
4953 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08004954 buf, api_max);
4955 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
4956 buf, ucode_raw->size);
4957 break;
4958 }
Zhu Yib481de92007-09-25 17:54:57 -07004959 }
4960
Reinette Chatrea0987a82008-12-02 12:14:06 -08004961 if (ret < 0)
4962 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07004963
4964 /* Make sure that we got at least our header! */
4965 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004966 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004967 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004968 goto err_release;
4969 }
4970
4971 /* Data from ucode file: header followed by uCode images */
4972 ucode = (void *)ucode_raw->data;
4973
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08004974 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08004975 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07004976 inst_size = le32_to_cpu(ucode->inst_size);
4977 data_size = le32_to_cpu(ucode->data_size);
4978 init_size = le32_to_cpu(ucode->init_size);
4979 init_data_size = le32_to_cpu(ucode->init_data_size);
4980 boot_size = le32_to_cpu(ucode->boot_size);
4981
Reinette Chatrea0987a82008-12-02 12:14:06 -08004982 /* api_ver should match the api version forming part of the
4983 * firmware filename ... but we don't check for that and only rely
4984 * on the API version read from firware header from here on forward */
4985
4986 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004987 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08004988 "Driver supports v%u, firmware is v%u.\n",
4989 api_max, api_ver);
4990 priv->ucode_ver = 0;
4991 ret = -EINVAL;
4992 goto err_release;
4993 }
4994 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004995 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08004996 "got %u. New firmware can be obtained "
4997 "from http://www.intellinuxwireless.org.\n",
4998 api_max, api_ver);
4999
Tomas Winkler978785a2008-12-19 10:37:31 +08005000 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
5001 IWL_UCODE_MAJOR(priv->ucode_ver),
5002 IWL_UCODE_MINOR(priv->ucode_ver),
5003 IWL_UCODE_API(priv->ucode_ver),
5004 IWL_UCODE_SERIAL(priv->ucode_ver));
5005
Reinette Chatrea0987a82008-12-02 12:14:06 -08005006 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
5007 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08005008 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
5009 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
5010 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
5011 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
5012 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07005013
Reinette Chatrea0987a82008-12-02 12:14:06 -08005014
Zhu Yib481de92007-09-25 17:54:57 -07005015 /* Verify size of file vs. image size info in file's header */
5016 if (ucode_raw->size < sizeof(*ucode) +
5017 inst_size + data_size + init_size +
5018 init_data_size + boot_size) {
5019
5020 IWL_DEBUG_INFO("uCode file size %d too small\n",
5021 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005022 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005023 goto err_release;
5024 }
5025
5026 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005027 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005028 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5029 inst_size);
5030 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005031 goto err_release;
5032 }
5033
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005034 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005035 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5036 data_size);
5037 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005038 goto err_release;
5039 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005040 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005041 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
5042 init_size);
5043 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005044 goto err_release;
5045 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005046 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005047 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
5048 init_data_size);
5049 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005050 goto err_release;
5051 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005052 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005053 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
5054 boot_size);
5055 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005056 goto err_release;
5057 }
5058
5059 /* Allocate ucode buffers for card's bus-master loading ... */
5060
5061 /* Runtime instructions and 2 copies of data:
5062 * 1) unmodified from disk
5063 * 2) backup cache for save/restore during power-downs */
5064 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005065 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005066
5067 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005068 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005069
5070 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005071 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005072
5073 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08005074 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07005075 goto err_pci_alloc;
5076
Tomas Winkler90e759d2007-11-29 11:09:41 +08005077 /* Initialization instructions and data */
5078 if (init_size && init_data_size) {
5079 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005080 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005081
5082 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005083 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005084
5085 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5086 goto err_pci_alloc;
5087 }
5088
5089 /* Bootstrap (instructions only, no data) */
5090 if (boot_size) {
5091 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005092 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005093
5094 if (!priv->ucode_boot.v_addr)
5095 goto err_pci_alloc;
5096 }
5097
Zhu Yib481de92007-09-25 17:54:57 -07005098 /* Copy images into buffers for card's bus-master reads ... */
5099
5100 /* Runtime instructions (first block of data in file) */
5101 src = &ucode->data[0];
5102 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005103 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005104 memcpy(priv->ucode_code.v_addr, src, len);
5105 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5106 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5107
5108 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005109 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005110 src = &ucode->data[inst_size];
5111 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005112 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005113 memcpy(priv->ucode_data.v_addr, src, len);
5114 memcpy(priv->ucode_data_backup.v_addr, src, len);
5115
5116 /* Initialization instructions (3rd block) */
5117 if (init_size) {
5118 src = &ucode->data[inst_size + data_size];
5119 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005120 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5121 len);
Zhu Yib481de92007-09-25 17:54:57 -07005122 memcpy(priv->ucode_init.v_addr, src, len);
5123 }
5124
5125 /* Initialization data (4th block) */
5126 if (init_data_size) {
5127 src = &ucode->data[inst_size + data_size + init_size];
5128 len = priv->ucode_init_data.len;
5129 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
5130 (int)len);
5131 memcpy(priv->ucode_init_data.v_addr, src, len);
5132 }
5133
5134 /* Bootstrap instructions (5th block) */
5135 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5136 len = priv->ucode_boot.len;
5137 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
5138 (int)len);
5139 memcpy(priv->ucode_boot.v_addr, src, len);
5140
5141 /* We have our copies now, allow OS release its copies */
5142 release_firmware(ucode_raw);
5143 return 0;
5144
5145 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005146 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005147 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005148 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005149
5150 err_release:
5151 release_firmware(ucode_raw);
5152
5153 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005154 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005155}
5156
5157
5158/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005159 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005160 *
5161 * Tell initialization uCode where to find runtime uCode.
5162 *
5163 * BSM registers initially contain pointers to initialization uCode.
5164 * We need to replace them to load runtime uCode inst and data,
5165 * and to save runtime data when powering down.
5166 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005167static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005168{
5169 dma_addr_t pinst;
5170 dma_addr_t pdata;
5171 int rc = 0;
5172 unsigned long flags;
5173
5174 /* bits 31:0 for 3945 */
5175 pinst = priv->ucode_code.p_addr;
5176 pdata = priv->ucode_data_backup.p_addr;
5177
5178 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005179 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005180 if (rc) {
5181 spin_unlock_irqrestore(&priv->lock, flags);
5182 return rc;
5183 }
5184
5185 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005186 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5187 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5188 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005189 priv->ucode_data.len);
5190
Tomas Winklera96a27f2008-10-23 23:48:56 -07005191 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07005192 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005193 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005194 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5195
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005196 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005197
5198 spin_unlock_irqrestore(&priv->lock, flags);
5199
5200 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5201
5202 return rc;
5203}
5204
5205/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005206 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005207 *
5208 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5209 *
Zhu Yib481de92007-09-25 17:54:57 -07005210 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08005211 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005212static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005213{
5214 /* Check alive response for "valid" sign from uCode */
5215 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5216 /* We had an error bringing up the hardware, so take it
5217 * all the way back down so we can try again */
5218 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5219 goto restart;
5220 }
5221
5222 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5223 * This is a paranoid check, because we would not have gotten the
5224 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005225 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005226 /* Runtime instruction load was bad;
5227 * take it all the way back down so we can try again */
5228 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5229 goto restart;
5230 }
5231
5232 /* Send pointers to protocol/runtime uCode image ... init code will
5233 * load and launch runtime uCode, which will send us another "Alive"
5234 * notification. */
5235 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005236 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005237 /* Runtime instruction load won't happen;
5238 * take it all the way back down so we can try again */
5239 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5240 goto restart;
5241 }
5242 return;
5243
5244 restart:
5245 queue_work(priv->workqueue, &priv->restart);
5246}
5247
5248
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005249/* temporary */
5250static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
5251 struct sk_buff *skb);
5252
Zhu Yib481de92007-09-25 17:54:57 -07005253/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005254 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005255 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005256 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005257 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005258static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005259{
5260 int rc = 0;
5261 int thermal_spin = 0;
5262 u32 rfkill;
5263
5264 IWL_DEBUG_INFO("Runtime Alive received.\n");
5265
5266 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5267 /* We had an error bringing up the hardware, so take it
5268 * all the way back down so we can try again */
5269 IWL_DEBUG_INFO("Alive failed.\n");
5270 goto restart;
5271 }
5272
5273 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5274 * This is a paranoid check, because we would not have gotten the
5275 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005276 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005277 /* Runtime instruction load was bad;
5278 * take it all the way back down so we can try again */
5279 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5280 goto restart;
5281 }
5282
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005283 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005284
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005285 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005286 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005287 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07005288 return;
5289 }
5290
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005291 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005292 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005293 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005294
5295 if (rfkill & 0x1) {
5296 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07005297 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07005298 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005299 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07005300 thermal_spin++;
5301 udelay(10);
5302 }
5303
5304 if (thermal_spin)
5305 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
5306 thermal_spin * 10);
5307 } else
5308 set_bit(STATUS_RF_KILL_HW, &priv->status);
5309
Ben Cahill9fbab512007-11-29 11:09:47 +08005310 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005311 set_bit(STATUS_ALIVE, &priv->status);
5312
5313 /* Clear out the uCode error bit if it is set */
5314 clear_bit(STATUS_FW_ERROR, &priv->status);
5315
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005316 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005317 return;
5318
Johannes Berg36d68252008-05-15 12:55:26 +02005319 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005320
5321 priv->active_rate = priv->rates_mask;
5322 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5323
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005324 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005325
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005326 if (iwl3945_is_associated(priv)) {
5327 struct iwl3945_rxon_cmd *active_rxon =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005328 (struct iwl3945_rxon_cmd *)(&priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005329
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005330 memcpy(&priv->staging39_rxon, &priv->active39_rxon,
5331 sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07005332 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5333 } else {
5334 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07005335 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005336 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07005337 }
5338
Ben Cahill9fbab512007-11-29 11:09:47 +08005339 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005340 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005341
5342 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005343 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005344
Zhu Yib481de92007-09-25 17:54:57 -07005345 iwl3945_reg_txpower_periodic(priv);
5346
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005347 iwl3945_led_register(priv);
5348
Zhu Yib481de92007-09-25 17:54:57 -07005349 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005350 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08005351 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005352
5353 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005354 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005355
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005356 /* reassociate for ADHOC mode */
5357 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
5358 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
5359 priv->vif);
5360 if (beacon)
5361 iwl3945_mac_beacon_update(priv->hw, beacon);
5362 }
5363
Zhu Yib481de92007-09-25 17:54:57 -07005364 return;
5365
5366 restart:
5367 queue_work(priv->workqueue, &priv->restart);
5368}
5369
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005370static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005371
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005372static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005373{
5374 unsigned long flags;
5375 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5376 struct ieee80211_conf *conf = NULL;
5377
5378 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5379
5380 conf = ieee80211_get_hw_conf(priv->hw);
5381
5382 if (!exit_pending)
5383 set_bit(STATUS_EXIT_PENDING, &priv->status);
5384
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005385 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005386 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005387
5388 /* Unblock any waiting calls */
5389 wake_up_interruptible_all(&priv->wait_command_queue);
5390
Zhu Yib481de92007-09-25 17:54:57 -07005391 /* Wipe out the EXIT_PENDING status bit if we are not actually
5392 * exiting the module */
5393 if (!exit_pending)
5394 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5395
5396 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005397 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005398
5399 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005400 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005401 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005402 spin_unlock_irqrestore(&priv->lock, flags);
5403 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005404
5405 if (priv->mac80211_registered)
5406 ieee80211_stop_queues(priv->hw);
5407
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005408 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005409 * clear all bits but the RF Kill and SUSPEND bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005410 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005411 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5412 STATUS_RF_KILL_HW |
5413 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5414 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005415 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5416 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005417 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005418 STATUS_IN_SUSPEND |
5419 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5420 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005421 goto exit;
5422 }
5423
5424 /* ...otherwise clear out all the status bits but the RF Kill and
5425 * SUSPEND bits and continue taking the NIC down. */
5426 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5427 STATUS_RF_KILL_HW |
5428 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5429 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005430 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5431 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005432 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5433 STATUS_IN_SUSPEND |
5434 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005435 STATUS_FW_ERROR |
5436 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5437 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005438
5439 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005440 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005441 spin_unlock_irqrestore(&priv->lock, flags);
5442
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005443 iwl3945_hw_txq_ctx_stop(priv);
5444 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005445
5446 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005447 if (!iwl_grab_nic_access(priv)) {
5448 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005449 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005450 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005451 }
5452 spin_unlock_irqrestore(&priv->lock, flags);
5453
5454 udelay(5);
5455
Kolekar, Abhijeet01ec6162008-12-19 10:37:38 +08005456 priv->cfg->ops->lib->apm_ops.reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005457 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08005458 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005459
5460 if (priv->ibss_beacon)
5461 dev_kfree_skb(priv->ibss_beacon);
5462 priv->ibss_beacon = NULL;
5463
5464 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005465 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005466}
5467
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005468static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005469{
5470 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005471 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005472 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005473
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005474 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005475}
5476
5477#define MAX_HW_RESTARTS 5
5478
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005479static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005480{
5481 int rc, i;
5482
5483 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005484 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07005485 return -EIO;
5486 }
5487
5488 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005489 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07005490 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005491 return -ENODEV;
5492 }
5493
Reinette Chatree903fbd2008-01-30 22:05:15 -08005494 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005495 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08005496 return -EIO;
5497 }
5498
Zhu Yie655b9f2008-01-24 02:19:38 -08005499 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005500 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005501 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5502 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5503 else {
5504 set_bit(STATUS_RF_KILL_HW, &priv->status);
5505 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005506 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005507 return -ENODEV;
5508 }
Zhu Yib481de92007-09-25 17:54:57 -07005509 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005510
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005511 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005512
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005513 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005514 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005515 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07005516 return rc;
5517 }
5518
5519 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005520 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5521 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005522 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5523
5524 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005525 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005526 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005527
5528 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005529 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5530 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005531
5532 /* Copy original ucode data image from disk into backup cache.
5533 * This will be used to initialize the on-board processor's
5534 * data SRAM for a clean start when the runtime program first loads. */
5535 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005536 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005537
Zhu Yie655b9f2008-01-24 02:19:38 -08005538 /* We return success when we resume from suspend and rf_kill is on. */
5539 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
5540 return 0;
5541
Zhu Yib481de92007-09-25 17:54:57 -07005542 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5543
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005544 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005545
5546 /* load bootstrap state machine,
5547 * load bootstrap program into processor's memory,
5548 * prepare to load the "initialize" uCode */
Kolekar, Abhijeet0164b9b2008-12-19 10:37:37 +08005549 priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005550
5551 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005552 IWL_ERR(priv,
5553 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07005554 continue;
5555 }
5556
5557 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005558 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005559
Zhu Yib481de92007-09-25 17:54:57 -07005560 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5561
5562 return 0;
5563 }
5564
5565 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005566 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005567 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005568
5569 /* tried to restart and config the device for as long as our
5570 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08005571 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07005572 return -EIO;
5573}
5574
5575
5576/*****************************************************************************
5577 *
5578 * Workqueue callbacks
5579 *
5580 *****************************************************************************/
5581
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005582static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005583{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005584 struct iwl_priv *priv =
5585 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005586
5587 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5588 return;
5589
5590 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005591 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005592 mutex_unlock(&priv->mutex);
5593}
5594
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005595static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005596{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005597 struct iwl_priv *priv =
5598 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005599
5600 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5601 return;
5602
5603 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005604 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005605 mutex_unlock(&priv->mutex);
5606}
5607
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005608static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005609{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005610 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005611
5612 wake_up_interruptible(&priv->wait_command_queue);
5613
5614 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5615 return;
5616
5617 mutex_lock(&priv->mutex);
5618
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005619 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005620 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5621 "HW and/or SW RF Kill no longer active, restarting "
5622 "device\n");
5623 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5624 queue_work(priv->workqueue, &priv->restart);
5625 } else {
5626
5627 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5628 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5629 "disabled by SW switch\n");
5630 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005631 IWL_WARN(priv, "Radio Frequency Kill Switch is On:\n"
Zhu Yib481de92007-09-25 17:54:57 -07005632 "Kill switch must be turned off for "
5633 "wireless networking to work.\n");
5634 }
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005635
Zhu Yib481de92007-09-25 17:54:57 -07005636 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005637 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005638}
5639
5640#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5641
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005642static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005643{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005644 struct iwl_priv *priv =
5645 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005646
5647 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5648 return;
5649
5650 mutex_lock(&priv->mutex);
5651 if (test_bit(STATUS_SCANNING, &priv->status) ||
5652 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5653 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5654 "Scan completion watchdog resetting adapter (%dms)\n",
5655 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005656
Zhu Yib481de92007-09-25 17:54:57 -07005657 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005658 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005659 }
5660 mutex_unlock(&priv->mutex);
5661}
5662
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005663static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005664{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005665 struct iwl_priv *priv =
5666 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08005667 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005668 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005669 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005670 .meta.flags = CMD_SIZE_HUGE,
5671 };
5672 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005673 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005674 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005675 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01005676 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04005677 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07005678
5679 conf = ieee80211_get_hw_conf(priv->hw);
5680
5681 mutex_lock(&priv->mutex);
5682
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005683 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005684 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005685 goto done;
5686 }
5687
Tomas Winklera96a27f2008-10-23 23:48:56 -07005688 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07005689 * was given the chance to run... */
5690 if (!test_bit(STATUS_SCANNING, &priv->status))
5691 goto done;
5692
5693 /* This should never be called or scheduled if there is currently
5694 * a scan active in the hardware. */
5695 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5696 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5697 "Ignoring second request.\n");
5698 rc = -EIO;
5699 goto done;
5700 }
5701
5702 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5703 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5704 goto done;
5705 }
5706
5707 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5708 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5709 goto done;
5710 }
5711
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005712 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005713 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5714 goto done;
5715 }
5716
5717 if (!test_bit(STATUS_READY, &priv->status)) {
5718 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5719 goto done;
5720 }
5721
5722 if (!priv->scan_bands) {
5723 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5724 goto done;
5725 }
5726
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005727 if (!priv->scan39) {
5728 priv->scan39 = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005729 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005730 if (!priv->scan39) {
Zhu Yib481de92007-09-25 17:54:57 -07005731 rc = -ENOMEM;
5732 goto done;
5733 }
5734 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005735 scan = priv->scan39;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005736 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005737
5738 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5739 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5740
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005741 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005742 u16 interval = 0;
5743 u32 extra;
5744 u32 suspend_time = 100;
5745 u32 scan_suspend_time = 100;
5746 unsigned long flags;
5747
5748 IWL_DEBUG_INFO("Scanning while associated...\n");
5749
5750 spin_lock_irqsave(&priv->lock, flags);
5751 interval = priv->beacon_int;
5752 spin_unlock_irqrestore(&priv->lock, flags);
5753
5754 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005755 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005756 if (!interval)
5757 interval = suspend_time;
5758 /*
5759 * suspend time format:
5760 * 0-19: beacon interval in usec (time before exec.)
5761 * 20-23: 0
5762 * 24-31: number of beacons (suspend between channels)
5763 */
5764
5765 extra = (suspend_time / interval) << 24;
5766 scan_suspend_time = 0xFF0FFFFF &
5767 (extra | ((suspend_time % interval) * 1024));
5768
5769 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5770 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5771 scan_suspend_time, interval);
5772 }
5773
5774 /* We should add the ability for user to lock to PASSIVE ONLY */
5775 if (priv->one_direct_scan) {
5776 IWL_DEBUG_SCAN
5777 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04005778 print_ssid(ssid, priv->direct_ssid,
5779 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07005780 scan->direct_scan[0].id = WLAN_EID_SSID;
5781 scan->direct_scan[0].len = priv->direct_ssid_len;
5782 memcpy(scan->direct_scan[0].ssid,
5783 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005784 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005785 } else
Bill Moss786b4552008-04-17 16:03:40 -07005786 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005787
5788 /* We don't build a direct scan probe request; the uCode will do
5789 * that based on the direct_mask added to each channel entry */
5790 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005791 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Johannes Berg430cfe92008-10-28 18:06:02 +01005792 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Zhu Yib481de92007-09-25 17:54:57 -07005793 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08005794 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07005795 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5796
5797 /* flags + rate selection */
5798
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005799 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005800 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5801 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
5802 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005803 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005804 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005805 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
5806 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005807 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005808 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005809 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07005810 goto done;
5811 }
5812
5813 /* select Rx antennas */
5814 scan->flags |= iwl3945_get_antenna_flags(priv);
5815
Johannes Berg05c914f2008-09-11 00:01:58 +02005816 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07005817 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5818
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005819 scan->channel_count =
5820 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
5821 n_probes,
5822 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07005823
Reinette Chatre14b54332008-11-04 12:21:35 -08005824 if (scan->channel_count == 0) {
5825 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
5826 goto done;
5827 }
5828
Zhu Yib481de92007-09-25 17:54:57 -07005829 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005830 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005831 cmd.data = scan;
5832 scan->len = cpu_to_le16(cmd.len);
5833
5834 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005835 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07005836 if (rc)
5837 goto done;
5838
5839 queue_delayed_work(priv->workqueue, &priv->scan_check,
5840 IWL_SCAN_CHECK_WATCHDOG);
5841
5842 mutex_unlock(&priv->mutex);
5843 return;
5844
5845 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08005846 /* can not perform scan make sure we clear scanning
5847 * bits from status so next scan request can be performed.
5848 * if we dont clear scanning status bit here all next scan
5849 * will fail
5850 */
5851 clear_bit(STATUS_SCAN_HW, &priv->status);
5852 clear_bit(STATUS_SCANNING, &priv->status);
5853
Ian Schram01ebd062007-10-25 17:15:22 +08005854 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005855 queue_work(priv->workqueue, &priv->scan_completed);
5856 mutex_unlock(&priv->mutex);
5857}
5858
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005859static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005860{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005861 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005862
5863 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5864 return;
5865
5866 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005867 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005868 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005869 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005870}
5871
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005872static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005873{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005874 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005875
5876 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5877 return;
5878
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005879 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005880 queue_work(priv->workqueue, &priv->up);
5881}
5882
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005883static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005884{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005885 struct iwl_priv *priv =
5886 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07005887
5888 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5889 return;
5890
5891 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005892 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005893 mutex_unlock(&priv->mutex);
5894}
5895
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005896#define IWL_DELAY_NEXT_SCAN (HZ*2)
5897
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005898static void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005899{
Zhu Yib481de92007-09-25 17:54:57 -07005900 int rc = 0;
5901 struct ieee80211_conf *conf = NULL;
5902
Johannes Berg05c914f2008-09-11 00:01:58 +02005903 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005904 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07005905 return;
5906 }
5907
5908
Johannes Berge1749612008-10-27 15:59:26 -07005909 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005910 priv->assoc_id, priv->active39_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07005911
5912 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5913 return;
5914
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08005915 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005916 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08005917
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005918 iwl3945_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005919
Zhu Yib481de92007-09-25 17:54:57 -07005920 conf = ieee80211_get_hw_conf(priv->hw);
5921
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005922 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005923 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005924
Tomas Winkler28afaf92008-12-19 10:37:06 +08005925 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005926 iwl3945_setup_rxon_timing(priv);
5927 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005928 sizeof(priv->rxon_timing), &priv->rxon_timing);
5929 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005930 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07005931 "Attempting to continue.\n");
5932
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005933 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005934
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005935 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07005936
5937 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
5938 priv->assoc_id, priv->beacon_int);
5939
5940 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005941 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005942 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005943 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005944
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005945 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07005946 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005947 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005948 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005949 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005950
Johannes Berg05c914f2008-09-11 00:01:58 +02005951 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005952 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005953
5954 }
5955
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005956 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005957
5958 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02005959 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005960 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07005961 break;
5962
Johannes Berg05c914f2008-09-11 00:01:58 +02005963 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07005964
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08005965 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005966 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005967 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01005968 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07005969 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
5970 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005971 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
5972 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005973
5974 break;
5975
5976 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005977 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005978 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07005979 break;
5980 }
5981
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005982 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08005983
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005984 /* we have just associated, don't start scan too early */
5985 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005986}
5987
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005988static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005989{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005990 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07005991
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005992 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005993 return;
5994
5995 mutex_lock(&priv->mutex);
5996
5997 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005998 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005999
6000 mutex_unlock(&priv->mutex);
6001}
6002
Johannes Berge8975582008-10-09 12:18:51 +02006003static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006004
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006005static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006006{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006007 struct iwl_priv *priv =
6008 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006009
6010 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6011
6012 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6013 return;
6014
Zhu Yia0646472007-12-20 14:10:01 +08006015 if (test_bit(STATUS_CONF_PENDING, &priv->status))
Johannes Berge8975582008-10-09 12:18:51 +02006016 iwl3945_mac_config(priv->hw, 0);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006017
Zhu Yib481de92007-09-25 17:54:57 -07006018 ieee80211_scan_completed(priv->hw);
6019
6020 /* Since setting the TXPOWER may have been deferred while
6021 * performing the scan, fire one off */
6022 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006023 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006024 mutex_unlock(&priv->mutex);
6025}
6026
6027/*****************************************************************************
6028 *
6029 * mac80211 entry point functions
6030 *
6031 *****************************************************************************/
6032
Zhu Yi5a669262008-01-14 17:46:18 -08006033#define UCODE_READY_TIMEOUT (2 * HZ)
6034
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006035static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006036{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006037 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08006038 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006039
6040 IWL_DEBUG_MAC80211("enter\n");
6041
Zhu Yi5a669262008-01-14 17:46:18 -08006042 if (pci_enable_device(priv->pci_dev)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006043 IWL_ERR(priv, "Fail to pci_enable_device\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006044 return -ENODEV;
6045 }
6046 pci_restore_state(priv->pci_dev);
6047 pci_enable_msi(priv->pci_dev);
6048
6049 ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
6050 DRV_NAME, priv);
6051 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006052 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
Zhu Yi5a669262008-01-14 17:46:18 -08006053 goto out_disable_msi;
6054 }
6055
Zhu Yib481de92007-09-25 17:54:57 -07006056 /* we should be verifying the device is ready to be opened */
6057 mutex_lock(&priv->mutex);
6058
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006059 memset(&priv->staging39_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
Zhu Yi5a669262008-01-14 17:46:18 -08006060 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6061 * ucode filename and max sizes are card-specific. */
6062
6063 if (!priv->ucode_code.len) {
6064 ret = iwl3945_read_ucode(priv);
6065 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006066 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08006067 mutex_unlock(&priv->mutex);
6068 goto out_release_irq;
6069 }
6070 }
6071
Zhu Yie655b9f2008-01-24 02:19:38 -08006072 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08006073
Zhu Yib481de92007-09-25 17:54:57 -07006074 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08006075
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006076 iwl3945_rfkill_set_hw_state(priv);
6077
Zhu Yie655b9f2008-01-24 02:19:38 -08006078 if (ret)
6079 goto out_release_irq;
6080
6081 IWL_DEBUG_INFO("Start UP work.\n");
6082
6083 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6084 return 0;
6085
Zhu Yi5a669262008-01-14 17:46:18 -08006086 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6087 * mac80211 will not be run successfully. */
6088 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6089 test_bit(STATUS_READY, &priv->status),
6090 UCODE_READY_TIMEOUT);
6091 if (!ret) {
6092 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006093 IWL_ERR(priv,
6094 "Wait for START_ALIVE timeout after %dms.\n",
6095 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08006096 ret = -ETIMEDOUT;
6097 goto out_release_irq;
6098 }
6099 }
6100
Zhu Yie655b9f2008-01-24 02:19:38 -08006101 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006102 IWL_DEBUG_MAC80211("leave\n");
6103 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006104
6105out_release_irq:
6106 free_irq(priv->pci_dev->irq, priv);
6107out_disable_msi:
6108 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006109 pci_disable_device(priv->pci_dev);
6110 priv->is_open = 0;
6111 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006112 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006113}
6114
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006115static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006116{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006117 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006118
6119 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006120
Zhu Yie655b9f2008-01-24 02:19:38 -08006121 if (!priv->is_open) {
6122 IWL_DEBUG_MAC80211("leave - skip\n");
6123 return;
6124 }
6125
Zhu Yib481de92007-09-25 17:54:57 -07006126 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006127
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006128 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006129 /* stop mac, cancel any scan request and clear
6130 * RXON_FILTER_ASSOC_MSK BIT
6131 */
Zhu Yi5a669262008-01-14 17:46:18 -08006132 mutex_lock(&priv->mutex);
6133 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006134 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006135 }
6136
Zhu Yi5a669262008-01-14 17:46:18 -08006137 iwl3945_down(priv);
6138
6139 flush_workqueue(priv->workqueue);
6140 free_irq(priv->pci_dev->irq, priv);
6141 pci_disable_msi(priv->pci_dev);
6142 pci_save_state(priv->pci_dev);
6143 pci_disable_device(priv->pci_dev);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006144
Zhu Yib481de92007-09-25 17:54:57 -07006145 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006146}
6147
Johannes Berge039fa42008-05-15 12:55:29 +02006148static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006149{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006150 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006151
6152 IWL_DEBUG_MAC80211("enter\n");
6153
Zhu Yib481de92007-09-25 17:54:57 -07006154 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02006155 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006156
Johannes Berge039fa42008-05-15 12:55:29 +02006157 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07006158 dev_kfree_skb_any(skb);
6159
6160 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08006161 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07006162}
6163
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006164static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006165 struct ieee80211_if_init_conf *conf)
6166{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006167 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006168 unsigned long flags;
6169
Johannes Berg32bfd352007-12-19 01:31:26 +01006170 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006171
Johannes Berg32bfd352007-12-19 01:31:26 +01006172 if (priv->vif) {
6173 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02006174 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006175 }
6176
6177 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006178 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07006179 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07006180
6181 spin_unlock_irqrestore(&priv->lock, flags);
6182
6183 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006184
6185 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07006186 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02006187 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6188 }
6189
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006190 if (iwl_is_ready(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006191 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006192
Zhu Yib481de92007-09-25 17:54:57 -07006193 mutex_unlock(&priv->mutex);
6194
Zhu Yi5a669262008-01-14 17:46:18 -08006195 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006196 return 0;
6197}
6198
6199/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006200 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006201 *
6202 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6203 * be set inappropriately and the driver currently sets the hardware up to
6204 * use it whenever needed.
6205 */
Johannes Berge8975582008-10-09 12:18:51 +02006206static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07006207{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006208 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006209 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02006210 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07006211 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006212 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006213
6214 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006215 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006216
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006217 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006218 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006219 ret = -EIO;
6220 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006221 }
6222
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006223 if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006224 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006225 IWL_DEBUG_MAC80211("leave - scanning\n");
6226 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006227 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006228 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006229 }
6230
6231 spin_lock_irqsave(&priv->lock, flags);
6232
Johannes Berg8318d782008-01-24 19:38:38 +01006233 ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
6234 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006235 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006236 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01006237 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006238 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6239 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006240 ret = -EINVAL;
6241 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006242 }
6243
Johannes Berg8318d782008-01-24 19:38:38 +01006244 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006245
Johannes Berg8318d782008-01-24 19:38:38 +01006246 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006247
6248 /* The list of supported rates and rate mask can be different
6249 * for each phymode; since the phymode may have changed, reset
6250 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006251 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006252
6253 spin_unlock_irqrestore(&priv->lock, flags);
6254
6255#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6256 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006257 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006258 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006259 }
6260#endif
6261
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006262 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006263
6264 if (!conf->radio_enabled) {
6265 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006266 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006267 }
6268
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006269 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006270 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006271 ret = -EIO;
6272 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006273 }
6274
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006275 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006276
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006277 if (memcmp(&priv->active39_rxon,
6278 &priv->staging39_rxon, sizeof(priv->staging39_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006279 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006280 else
6281 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6282
6283 IWL_DEBUG_MAC80211("leave\n");
6284
Zhu Yi76bb77e2007-11-22 10:53:22 +08006285out:
Zhu Yia0646472007-12-20 14:10:01 +08006286 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006287 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006288 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006289}
6290
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006291static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006292{
6293 int rc = 0;
6294
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006295 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006296 return;
6297
6298 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08006299 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07006300
6301 /* RXON - unassoc (to set timing command) */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006302 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006303 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006304
6305 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08006306 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006307 iwl3945_setup_rxon_timing(priv);
6308 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006309 sizeof(priv->rxon_timing), &priv->rxon_timing);
6310 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006311 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006312 "Attempting to continue.\n");
6313
6314 /* FIXME: what should be the assoc_id for AP? */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006315 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006316 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006317 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006318 RXON_FLG_SHORT_PREAMBLE_MSK;
6319 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006320 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006321 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6322
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006323 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006324 if (priv->assoc_capability &
6325 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006326 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006327 RXON_FLG_SHORT_SLOT_MSK;
6328 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006329 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006330 ~RXON_FLG_SHORT_SLOT_MSK;
6331
Johannes Berg05c914f2008-09-11 00:01:58 +02006332 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006333 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006334 ~RXON_FLG_SHORT_SLOT_MSK;
6335 }
6336 /* restore RXON assoc */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006337 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006338 iwl3945_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08006339 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08006340 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006341 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006342
6343 /* FIXME - we need to add code here to detect a totally new
6344 * configuration, reset the AP, unassoc, rxon timing, assoc,
6345 * clear sta table, add BCAST sta... */
6346}
6347
Johannes Berg32bfd352007-12-19 01:31:26 +01006348static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
6349 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006350 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006351{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006352 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006353 int rc;
6354
6355 if (conf == NULL)
6356 return -EIO;
6357
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006358 if (priv->vif != vif) {
6359 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006360 return 0;
6361 }
6362
Johannes Berg9d139c82008-07-09 14:40:37 +02006363 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02006364 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02006365 conf->changed & IEEE80211_IFCC_BEACON) {
6366 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
6367 if (!beacon)
6368 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006369 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006370 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006371 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006372 if (rc)
6373 return rc;
6374 }
6375
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006376 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006377 return -EAGAIN;
6378
Zhu Yib481de92007-09-25 17:54:57 -07006379 mutex_lock(&priv->mutex);
6380
Zhu Yib481de92007-09-25 17:54:57 -07006381 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07006382 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006383
Johannes Berg4150c572007-09-17 01:29:23 -04006384/*
6385 * very dubious code was here; the probe filtering flag is never set:
6386 *
Zhu Yib481de92007-09-25 17:54:57 -07006387 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6388 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006389 */
Zhu Yib481de92007-09-25 17:54:57 -07006390
Johannes Berg05c914f2008-09-11 00:01:58 +02006391 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07006392 if (!conf->bssid) {
6393 conf->bssid = priv->mac_addr;
6394 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07006395 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
6396 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006397 }
6398 if (priv->ibss_beacon)
6399 dev_kfree_skb(priv->ibss_beacon);
6400
Johannes Berg9d139c82008-07-09 14:40:37 +02006401 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07006402 }
6403
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006404 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006405 goto done;
6406
Zhu Yib481de92007-09-25 17:54:57 -07006407 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6408 !is_multicast_ether_addr(conf->bssid)) {
6409 /* If there is currently a HW scan going on in the background
6410 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006411 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006412 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07006413 "after 100ms\n");
6414 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6415 mutex_unlock(&priv->mutex);
6416 return -EAGAIN;
6417 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006418 memcpy(priv->staging39_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006419
6420 /* TODO: Audit driver for usage of these members and see
6421 * if mac80211 deprecates them (priv->bssid looks like it
6422 * shouldn't be there, but I haven't scanned the IBSS code
6423 * to verify) - jpk */
6424 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6425
Johannes Berg05c914f2008-09-11 00:01:58 +02006426 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006427 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006428 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006429 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02006430 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006431 iwl3945_add_station(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006432 priv->active39_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006433 }
6434
6435 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006436 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006437 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006438 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006439 }
6440
Mohamed Abbasfde35712007-11-29 11:10:15 +08006441 done:
Zhu Yib481de92007-09-25 17:54:57 -07006442 IWL_DEBUG_MAC80211("leave\n");
6443 mutex_unlock(&priv->mutex);
6444
6445 return 0;
6446}
6447
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006448static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006449 unsigned int changed_flags,
6450 unsigned int *total_flags,
6451 int mc_count, struct dev_addr_list *mc_list)
6452{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006453 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006454 __le32 *filter_flags = &priv->staging39_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08006455
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006456 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
6457 changed_flags, *total_flags);
6458
6459 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
6460 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
6461 *filter_flags |= RXON_FILTER_PROMISC_MSK;
6462 else
6463 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006464 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006465 if (changed_flags & FIF_ALLMULTI) {
6466 if (*total_flags & FIF_ALLMULTI)
6467 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
6468 else
6469 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
6470 }
6471 if (changed_flags & FIF_CONTROL) {
6472 if (*total_flags & FIF_CONTROL)
6473 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
6474 else
6475 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
6476 }
6477 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
6478 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6479 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
6480 else
6481 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
6482 }
6483
6484 /* We avoid iwl_commit_rxon here to commit the new filter flags
6485 * since mac80211 will call ieee80211_hw_config immediately.
6486 * (mc_list is not supported at this time). Otherwise, we need to
6487 * queue a background iwl_commit_rxon work.
6488 */
6489
6490 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08006491 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04006492}
6493
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006494static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006495 struct ieee80211_if_init_conf *conf)
6496{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006497 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006498
6499 IWL_DEBUG_MAC80211("enter\n");
6500
6501 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006502
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006503 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006504 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006505 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Mohamed Abbasfde35712007-11-29 11:10:15 +08006506 iwl3945_commit_rxon(priv);
6507 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006508 if (priv->vif == conf->vif) {
6509 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006510 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006511 }
6512 mutex_unlock(&priv->mutex);
6513
6514 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006515}
6516
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006517#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
6518
6519static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
6520 struct ieee80211_vif *vif,
6521 struct ieee80211_bss_conf *bss_conf,
6522 u32 changes)
6523{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006524 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006525
6526 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6527
6528 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6529 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6530 bss_conf->use_short_preamble);
6531 if (bss_conf->use_short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006532 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006533 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006534 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006535 }
6536
6537 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
6538 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
6539 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006540 priv->staging39_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006541 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006542 priv->staging39_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006543 }
6544
6545 if (changes & BSS_CHANGED_ASSOC) {
6546 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6547 /* This should never happen as this function should
6548 * never be called from interrupt context. */
6549 if (WARN_ON_ONCE(in_interrupt()))
6550 return;
6551 if (bss_conf->assoc) {
6552 priv->assoc_id = bss_conf->aid;
6553 priv->beacon_int = bss_conf->beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006554 priv->timestamp = bss_conf->timestamp;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006555 priv->assoc_capability = bss_conf->assoc_capability;
6556 priv->next_scan_jiffies = jiffies +
6557 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6558 mutex_lock(&priv->mutex);
6559 iwl3945_post_associate(priv);
6560 mutex_unlock(&priv->mutex);
6561 } else {
6562 priv->assoc_id = 0;
6563 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6564 }
6565 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
6566 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6567 iwl3945_send_rxon_assoc(priv);
6568 }
6569
6570}
6571
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006572static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006573{
6574 int rc = 0;
6575 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006576 struct iwl_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04006577 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07006578
6579 IWL_DEBUG_MAC80211("enter\n");
6580
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006581 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006582 spin_lock_irqsave(&priv->lock, flags);
6583
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006584 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006585 rc = -EIO;
6586 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6587 goto out_unlock;
6588 }
6589
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006590 /* we don't schedule scan within next_scan_jiffies period */
6591 if (priv->next_scan_jiffies &&
6592 time_after(priv->next_scan_jiffies, jiffies)) {
6593 rc = -EAGAIN;
6594 goto out_unlock;
6595 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08006596 /* if we just finished scan ask for delay for a broadcast scan */
6597 if ((len == 0) && priv->last_scan_jiffies &&
6598 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
6599 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006600 rc = -EAGAIN;
6601 goto out_unlock;
6602 }
6603 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006604 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04006605 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006606
6607 priv->one_direct_scan = 1;
6608 priv->direct_ssid_len = (u8)
6609 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6610 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006611 } else
6612 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006613
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006614 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006615
6616 IWL_DEBUG_MAC80211("leave\n");
6617
6618out_unlock:
6619 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006620 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006621
6622 return rc;
6623}
6624
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006625static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006626 const u8 *local_addr, const u8 *addr,
6627 struct ieee80211_key_conf *key)
6628{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006629 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006630 int rc = 0;
6631 u8 sta_id;
6632
6633 IWL_DEBUG_MAC80211("enter\n");
6634
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006635 if (iwl3945_mod_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006636 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6637 return -EOPNOTSUPP;
6638 }
6639
6640 if (is_zero_ether_addr(addr))
6641 /* only support pairwise keys */
6642 return -EOPNOTSUPP;
6643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006644 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07006645 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07006646 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
6647 addr);
Zhu Yib481de92007-09-25 17:54:57 -07006648 return -EINVAL;
6649 }
6650
6651 mutex_lock(&priv->mutex);
6652
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006653 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006654
Zhu Yib481de92007-09-25 17:54:57 -07006655 switch (cmd) {
6656 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006657 rc = iwl3945_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006658 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006659 iwl3945_set_rxon_hwcrypto(priv, 1);
6660 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006661 key->hw_key_idx = sta_id;
6662 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
6663 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
6664 }
6665 break;
6666 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006667 rc = iwl3945_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006668 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006669 iwl3945_set_rxon_hwcrypto(priv, 0);
6670 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006671 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
6672 }
6673 break;
6674 default:
6675 rc = -EINVAL;
6676 }
6677
6678 IWL_DEBUG_MAC80211("leave\n");
6679 mutex_unlock(&priv->mutex);
6680
6681 return rc;
6682}
6683
Johannes Berge100bb62008-04-30 18:51:21 +02006684static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07006685 const struct ieee80211_tx_queue_params *params)
6686{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006687 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006688 unsigned long flags;
6689 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006690
6691 IWL_DEBUG_MAC80211("enter\n");
6692
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006693 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006694 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6695 return -EIO;
6696 }
6697
6698 if (queue >= AC_NUM) {
6699 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6700 return 0;
6701 }
6702
Zhu Yib481de92007-09-25 17:54:57 -07006703 q = AC_NUM - 1 - queue;
6704
6705 spin_lock_irqsave(&priv->lock, flags);
6706
6707 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6708 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6709 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6710 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7be2008-02-10 16:49:38 +01006711 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006712
6713 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6714 priv->qos_data.qos_active = 1;
6715
6716 spin_unlock_irqrestore(&priv->lock, flags);
6717
6718 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02006719 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006720 iwl3945_activate_qos(priv, 1);
6721 else if (priv->assoc_id && iwl3945_is_associated(priv))
6722 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006723
6724 mutex_unlock(&priv->mutex);
6725
Zhu Yib481de92007-09-25 17:54:57 -07006726 IWL_DEBUG_MAC80211("leave\n");
6727 return 0;
6728}
6729
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006730static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006731 struct ieee80211_tx_queue_stats *stats)
6732{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006733 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006734 int i, avail;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08006735 struct iwl_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006736 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07006737 unsigned long flags;
6738
6739 IWL_DEBUG_MAC80211("enter\n");
6740
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006741 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006742 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6743 return -EIO;
6744 }
6745
6746 spin_lock_irqsave(&priv->lock, flags);
6747
6748 for (i = 0; i < AC_NUM; i++) {
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08006749 txq = &priv->txq[i];
Zhu Yib481de92007-09-25 17:54:57 -07006750 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006751 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07006752
Johannes Berg57ffc582008-04-29 17:18:59 +02006753 stats[i].len = q->n_window - avail;
6754 stats[i].limit = q->n_window - q->high_mark;
6755 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07006756
6757 }
6758 spin_unlock_irqrestore(&priv->lock, flags);
6759
6760 IWL_DEBUG_MAC80211("leave\n");
6761
6762 return 0;
6763}
6764
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006765static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006766{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006767 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006768 unsigned long flags;
6769
6770 mutex_lock(&priv->mutex);
6771 IWL_DEBUG_MAC80211("enter\n");
6772
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006773 iwl_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006774
Zhu Yib481de92007-09-25 17:54:57 -07006775 spin_lock_irqsave(&priv->lock, flags);
6776 priv->assoc_id = 0;
6777 priv->assoc_capability = 0;
6778 priv->call_post_assoc_from_beacon = 0;
6779
6780 /* new association get rid of ibss beacon skb */
6781 if (priv->ibss_beacon)
6782 dev_kfree_skb(priv->ibss_beacon);
6783
6784 priv->ibss_beacon = NULL;
6785
6786 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006787 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02006788 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07006789 priv->beacon_int = 0;
6790
6791 spin_unlock_irqrestore(&priv->lock, flags);
6792
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006793 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006794 IWL_DEBUG_MAC80211("leave - not ready\n");
6795 mutex_unlock(&priv->mutex);
6796 return;
6797 }
6798
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006799 /* we are restarting association process
6800 * clear RXON_FILTER_ASSOC_MSK bit
6801 */
Johannes Berg05c914f2008-09-11 00:01:58 +02006802 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006803 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006804 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006805 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006806 }
6807
Zhu Yib481de92007-09-25 17:54:57 -07006808 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02006809 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006810
Zhu Yib481de92007-09-25 17:54:57 -07006811 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
6812 mutex_unlock(&priv->mutex);
6813 return;
6814 }
6815
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006816 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006817
6818 mutex_unlock(&priv->mutex);
6819
6820 IWL_DEBUG_MAC80211("leave\n");
6821
6822}
6823
Johannes Berge039fa42008-05-15 12:55:29 +02006824static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006825{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006826 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006827 unsigned long flags;
6828
Zhu Yib481de92007-09-25 17:54:57 -07006829 IWL_DEBUG_MAC80211("enter\n");
6830
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006831 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006832 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07006833 return -EIO;
6834 }
6835
Johannes Berg05c914f2008-09-11 00:01:58 +02006836 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07006837 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07006838 return -EIO;
6839 }
6840
6841 spin_lock_irqsave(&priv->lock, flags);
6842
6843 if (priv->ibss_beacon)
6844 dev_kfree_skb(priv->ibss_beacon);
6845
6846 priv->ibss_beacon = skb;
6847
6848 priv->assoc_id = 0;
6849
6850 IWL_DEBUG_MAC80211("leave\n");
6851 spin_unlock_irqrestore(&priv->lock, flags);
6852
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006853 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006854
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08006855 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006856
Zhu Yib481de92007-09-25 17:54:57 -07006857
6858 return 0;
6859}
6860
6861/*****************************************************************************
6862 *
6863 * sysfs attributes
6864 *
6865 *****************************************************************************/
6866
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006867#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07006868
6869/*
6870 * The following adds a new attribute to the sysfs representation
6871 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
6872 * used for controlling the debug level.
6873 *
6874 * See the level definitions in iwl for details.
6875 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006876static ssize_t show_debug_level(struct device *d,
6877 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07006878{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006879 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006880
6881 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006882}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006883static ssize_t store_debug_level(struct device *d,
6884 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07006885 const char *buf, size_t count)
6886{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006887 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006888 unsigned long val;
6889 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006890
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006891 ret = strict_strtoul(buf, 0, &val);
6892 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08006893 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07006894 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006895 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07006896
6897 return strnlen(buf, count);
6898}
6899
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006900static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
6901 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006902
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006903#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07006904
Zhu Yib481de92007-09-25 17:54:57 -07006905static ssize_t show_temperature(struct device *d,
6906 struct device_attribute *attr, char *buf)
6907{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006908 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006909
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006910 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006911 return -EAGAIN;
6912
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006913 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006914}
6915
6916static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6917
Zhu Yib481de92007-09-25 17:54:57 -07006918static ssize_t show_tx_power(struct device *d,
6919 struct device_attribute *attr, char *buf)
6920{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006921 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006922 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6923}
6924
6925static ssize_t store_tx_power(struct device *d,
6926 struct device_attribute *attr,
6927 const char *buf, size_t count)
6928{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006929 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006930 char *p = (char *)buf;
6931 u32 val;
6932
6933 val = simple_strtoul(p, &p, 10);
6934 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08006935 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07006936 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006937 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006938
6939 return count;
6940}
6941
6942static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6943
6944static ssize_t show_flags(struct device *d,
6945 struct device_attribute *attr, char *buf)
6946{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006947 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006948
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006949 return sprintf(buf, "0x%04X\n", priv->active39_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07006950}
6951
6952static ssize_t store_flags(struct device *d,
6953 struct device_attribute *attr,
6954 const char *buf, size_t count)
6955{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006956 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006957 u32 flags = simple_strtoul(buf, NULL, 0);
6958
6959 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006960 if (le32_to_cpu(priv->staging39_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07006961 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006962 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006963 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006964 else {
6965 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6966 flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006967 priv->staging39_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006968 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006969 }
6970 }
6971 mutex_unlock(&priv->mutex);
6972
6973 return count;
6974}
6975
6976static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6977
6978static ssize_t show_filter_flags(struct device *d,
6979 struct device_attribute *attr, char *buf)
6980{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006981 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006982
6983 return sprintf(buf, "0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006984 le32_to_cpu(priv->active39_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07006985}
6986
6987static ssize_t store_filter_flags(struct device *d,
6988 struct device_attribute *attr,
6989 const char *buf, size_t count)
6990{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006991 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006992 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6993
6994 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006995 if (le32_to_cpu(priv->staging39_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07006996 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006997 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006998 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006999 else {
7000 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7001 "0x%04X\n", filter_flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007002 priv->staging39_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07007003 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007004 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007005 }
7006 }
7007 mutex_unlock(&priv->mutex);
7008
7009 return count;
7010}
7011
7012static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7013 store_filter_flags);
7014
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007015#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007016
7017static ssize_t show_measurement(struct device *d,
7018 struct device_attribute *attr, char *buf)
7019{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007020 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08007021 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007022 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007023 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007024 unsigned long flags;
7025
7026 spin_lock_irqsave(&priv->lock, flags);
7027 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7028 spin_unlock_irqrestore(&priv->lock, flags);
7029 return 0;
7030 }
7031 memcpy(&measure_report, &priv->measure_report, size);
7032 priv->measurement_status = 0;
7033 spin_unlock_irqrestore(&priv->lock, flags);
7034
7035 while (size && (PAGE_SIZE - len)) {
7036 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7037 PAGE_SIZE - len, 1);
7038 len = strlen(buf);
7039 if (PAGE_SIZE - len)
7040 buf[len++] = '\n';
7041
7042 ofs += 16;
7043 size -= min(size, 16U);
7044 }
7045
7046 return len;
7047}
7048
7049static ssize_t store_measurement(struct device *d,
7050 struct device_attribute *attr,
7051 const char *buf, size_t count)
7052{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007053 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007054 struct ieee80211_measurement_params params = {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007055 .channel = le16_to_cpu(priv->active39_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07007056 .start_time = cpu_to_le64(priv->last_tsf),
7057 .duration = cpu_to_le16(1),
7058 };
7059 u8 type = IWL_MEASURE_BASIC;
7060 u8 buffer[32];
7061 u8 channel;
7062
7063 if (count) {
7064 char *p = buffer;
7065 strncpy(buffer, buf, min(sizeof(buffer), count));
7066 channel = simple_strtoul(p, NULL, 0);
7067 if (channel)
7068 params.channel = channel;
7069
7070 p = buffer;
7071 while (*p && *p != ' ')
7072 p++;
7073 if (*p)
7074 type = simple_strtoul(p + 1, NULL, 0);
7075 }
7076
7077 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7078 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007079 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007080
7081 return count;
7082}
7083
7084static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7085 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007086#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007087
Zhu Yib481de92007-09-25 17:54:57 -07007088static ssize_t store_retry_rate(struct device *d,
7089 struct device_attribute *attr,
7090 const char *buf, size_t count)
7091{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007092 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007093
7094 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7095 if (priv->retry_rate <= 0)
7096 priv->retry_rate = 1;
7097
7098 return count;
7099}
7100
7101static ssize_t show_retry_rate(struct device *d,
7102 struct device_attribute *attr, char *buf)
7103{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007104 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007105 return sprintf(buf, "%d", priv->retry_rate);
7106}
7107
7108static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7109 store_retry_rate);
7110
7111static ssize_t store_power_level(struct device *d,
7112 struct device_attribute *attr,
7113 const char *buf, size_t count)
7114{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007115 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007116 int rc;
7117 int mode;
7118
7119 mode = simple_strtoul(buf, NULL, 0);
7120 mutex_lock(&priv->mutex);
7121
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007122 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007123 rc = -EAGAIN;
7124 goto out;
7125 }
7126
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007127 if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
7128 (mode == IWL39_POWER_AC))
7129 mode = IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07007130 else
7131 mode |= IWL_POWER_ENABLED;
7132
7133 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007134 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007135 if (rc) {
7136 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7137 goto out;
7138 }
7139 priv->power_mode = mode;
7140 }
7141
7142 rc = count;
7143
7144 out:
7145 mutex_unlock(&priv->mutex);
7146 return rc;
7147}
7148
7149#define MAX_WX_STRING 80
7150
7151/* Values are in microsecond */
7152static const s32 timeout_duration[] = {
7153 350000,
7154 250000,
7155 75000,
7156 37000,
7157 25000,
7158};
7159static const s32 period_duration[] = {
7160 400000,
7161 700000,
7162 1000000,
7163 1000000,
7164 1000000
7165};
7166
7167static ssize_t show_power_level(struct device *d,
7168 struct device_attribute *attr, char *buf)
7169{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007170 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007171 int level = IWL_POWER_LEVEL(priv->power_mode);
7172 char *p = buf;
7173
7174 p += sprintf(p, "%d ", level);
7175 switch (level) {
7176 case IWL_POWER_MODE_CAM:
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007177 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07007178 p += sprintf(p, "(AC)");
7179 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007180 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07007181 p += sprintf(p, "(BATTERY)");
7182 break;
7183 default:
7184 p += sprintf(p,
7185 "(Timeout %dms, Period %dms)",
7186 timeout_duration[level - 1] / 1000,
7187 period_duration[level - 1] / 1000);
7188 }
7189
7190 if (!(priv->power_mode & IWL_POWER_ENABLED))
7191 p += sprintf(p, " OFF\n");
7192 else
7193 p += sprintf(p, " \n");
7194
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007195 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07007196
7197}
7198
7199static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7200 store_power_level);
7201
7202static ssize_t show_channels(struct device *d,
7203 struct device_attribute *attr, char *buf)
7204{
Johannes Berg8318d782008-01-24 19:38:38 +01007205 /* all this shit doesn't belong into sysfs anyway */
7206 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007207}
7208
7209static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7210
7211static ssize_t show_statistics(struct device *d,
7212 struct device_attribute *attr, char *buf)
7213{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007214 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007215 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007216 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007217 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07007218 int rc = 0;
7219
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007220 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007221 return -EAGAIN;
7222
7223 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007224 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007225 mutex_unlock(&priv->mutex);
7226
7227 if (rc) {
7228 len = sprintf(buf,
7229 "Error sending statistics request: 0x%08X\n", rc);
7230 return len;
7231 }
7232
7233 while (size && (PAGE_SIZE - len)) {
7234 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7235 PAGE_SIZE - len, 1);
7236 len = strlen(buf);
7237 if (PAGE_SIZE - len)
7238 buf[len++] = '\n';
7239
7240 ofs += 16;
7241 size -= min(size, 16U);
7242 }
7243
7244 return len;
7245}
7246
7247static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7248
7249static ssize_t show_antenna(struct device *d,
7250 struct device_attribute *attr, char *buf)
7251{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007252 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007253
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007254 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007255 return -EAGAIN;
7256
7257 return sprintf(buf, "%d\n", priv->antenna);
7258}
7259
7260static ssize_t store_antenna(struct device *d,
7261 struct device_attribute *attr,
7262 const char *buf, size_t count)
7263{
7264 int ant;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007265 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007266
7267 if (count == 0)
7268 return 0;
7269
7270 if (sscanf(buf, "%1i", &ant) != 1) {
7271 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7272 return count;
7273 }
7274
7275 if ((ant >= 0) && (ant <= 2)) {
7276 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007277 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007278 } else
7279 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7280
7281
7282 return count;
7283}
7284
7285static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7286
7287static ssize_t show_status(struct device *d,
7288 struct device_attribute *attr, char *buf)
7289{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007290 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007291 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007292 return -EAGAIN;
7293 return sprintf(buf, "0x%08x\n", (int)priv->status);
7294}
7295
7296static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7297
7298static ssize_t dump_error_log(struct device *d,
7299 struct device_attribute *attr,
7300 const char *buf, size_t count)
7301{
7302 char *p = (char *)buf;
7303
7304 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007305 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007306
7307 return strnlen(buf, count);
7308}
7309
7310static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7311
7312static ssize_t dump_event_log(struct device *d,
7313 struct device_attribute *attr,
7314 const char *buf, size_t count)
7315{
7316 char *p = (char *)buf;
7317
7318 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007319 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007320
7321 return strnlen(buf, count);
7322}
7323
7324static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7325
7326/*****************************************************************************
7327 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07007328 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07007329 *
7330 *****************************************************************************/
7331
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007332static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007333{
7334 priv->workqueue = create_workqueue(DRV_NAME);
7335
7336 init_waitqueue_head(&priv->wait_command_queue);
7337
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007338 INIT_WORK(&priv->up, iwl3945_bg_up);
7339 INIT_WORK(&priv->restart, iwl3945_bg_restart);
7340 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
7341 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
7342 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
7343 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
7344 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
7345 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007346 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
7347 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
7348 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007349
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007350 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007351
7352 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007353 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007354}
7355
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007356static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007357{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007358 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007359
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09007360 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007361 cancel_delayed_work(&priv->scan_check);
7362 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007363 cancel_work_sync(&priv->beacon_update);
7364}
7365
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007366static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007367 &dev_attr_antenna.attr,
7368 &dev_attr_channels.attr,
7369 &dev_attr_dump_errors.attr,
7370 &dev_attr_dump_events.attr,
7371 &dev_attr_flags.attr,
7372 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007373#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007374 &dev_attr_measurement.attr,
7375#endif
7376 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007377 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007378 &dev_attr_statistics.attr,
7379 &dev_attr_status.attr,
7380 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007381 &dev_attr_tx_power.attr,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007382#ifdef CONFIG_IWL3945_DEBUG
7383 &dev_attr_debug_level.attr,
7384#endif
Zhu Yib481de92007-09-25 17:54:57 -07007385 NULL
7386};
7387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007388static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007389 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007390 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007391};
7392
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007393static struct ieee80211_ops iwl3945_hw_ops = {
7394 .tx = iwl3945_mac_tx,
7395 .start = iwl3945_mac_start,
7396 .stop = iwl3945_mac_stop,
7397 .add_interface = iwl3945_mac_add_interface,
7398 .remove_interface = iwl3945_mac_remove_interface,
7399 .config = iwl3945_mac_config,
7400 .config_interface = iwl3945_mac_config_interface,
7401 .configure_filter = iwl3945_configure_filter,
7402 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007403 .get_tx_stats = iwl3945_mac_get_tx_stats,
7404 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007405 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08007406 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007407 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007408};
7409
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007410int iwl3945_init_drv(struct iwl_priv *priv)
7411{
7412 int ret;
7413
7414 priv->retry_rate = 1;
7415 priv->ibss_beacon = NULL;
7416
7417 spin_lock_init(&priv->lock);
7418 spin_lock_init(&priv->power_data.lock);
7419 spin_lock_init(&priv->sta_lock);
7420 spin_lock_init(&priv->hcmd_lock);
7421
7422 INIT_LIST_HEAD(&priv->free_frames);
7423
7424 mutex_init(&priv->mutex);
7425
7426 /* Clear the driver's (not device's) station table */
7427 iwl3945_clear_stations_table(priv);
7428
7429 priv->data_retry_limit = -1;
7430 priv->ieee_channels = NULL;
7431 priv->ieee_rates = NULL;
7432 priv->band = IEEE80211_BAND_2GHZ;
7433
7434 priv->iw_mode = NL80211_IFTYPE_STATION;
7435
7436 iwl_reset_qos(priv);
7437
7438 priv->qos_data.qos_active = 0;
7439 priv->qos_data.qos_cap.val = 0;
7440
7441 priv->rates_mask = IWL_RATES_MASK;
7442 /* If power management is turned on, default to AC mode */
7443 priv->power_mode = IWL_POWER_AC;
7444 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
7445
7446 ret = iwl3945_init_channel_map(priv);
7447 if (ret) {
7448 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
7449 goto err;
7450 }
7451
7452 ret = iwl3945_init_geos(priv);
7453 if (ret) {
7454 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
7455 goto err_free_channel_map;
7456 }
7457
7458 return 0;
7459
7460err_free_channel_map:
7461 iwl3945_free_channel_map(priv);
7462err:
7463 return ret;
7464}
7465
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007466static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007467{
7468 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007469 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007470 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08007471 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007472 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007473
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007474 /***********************
7475 * 1. Allocating HW data
7476 * ********************/
7477
Zhu Yib481de92007-09-25 17:54:57 -07007478 /* mac80211 allocates memory for this device instance, including
7479 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007480 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07007481 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007482 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07007483 err = -ENOMEM;
7484 goto out;
7485 }
Zhu Yib481de92007-09-25 17:54:57 -07007486 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007487 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007488
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007489 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
7490 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007491 IWL_ERR(priv,
7492 "invalid queues_num, should be between %d and %d\n",
7493 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08007494 err = -EINVAL;
7495 goto out;
7496 }
7497
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007498 /*
7499 * Disabling hardware scan means that mac80211 will perform scans
7500 * "the hard way", rather than using device's scan.
7501 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007502 if (iwl3945_mod_params.disable_hw_scan) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007503 IWL_DEBUG_INFO("Disabling hw_scan\n");
7504 iwl3945_hw_ops.hw_scan = NULL;
7505 }
7506
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007507
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007508 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007509 priv->cfg = cfg;
7510 priv->pci_dev = pdev;
7511
7512#ifdef CONFIG_IWL3945_DEBUG
7513 priv->debug_level = iwl3945_mod_params.debug;
7514 atomic_set(&priv->restrict_refcnt, 0);
7515#endif
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007516 hw->rate_control_algorithm = "iwl-3945-rs";
7517 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
7518
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007519 /* Select antenna (may be helpful if only one antenna is connected) */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007520 priv->antenna = (enum iwl3945_antenna)iwl3945_mod_params.antenna;
Zhu Yib481de92007-09-25 17:54:57 -07007521
Bruno Randolf566bfe52008-05-08 19:15:40 +02007522 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02007523 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02007524 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07007525
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007526 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007527 BIT(NL80211_IFTYPE_STATION) |
7528 BIT(NL80211_IFTYPE_ADHOC);
7529
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08007530 hw->wiphy->fw_handles_regulatory = true;
7531
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007532 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07007533 hw->queues = 4;
7534
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007535 /***************************
7536 * 2. Initializing PCI bus
7537 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07007538 if (pci_enable_device(pdev)) {
7539 err = -ENODEV;
7540 goto out_ieee80211_free_hw;
7541 }
7542
7543 pci_set_master(pdev);
7544
Zhu Yib481de92007-09-25 17:54:57 -07007545 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7546 if (!err)
7547 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7548 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08007549 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007550 goto out_pci_disable_device;
7551 }
7552
7553 pci_set_drvdata(pdev, priv);
7554 err = pci_request_regions(pdev, DRV_NAME);
7555 if (err)
7556 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007557
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007558 /***********************
7559 * 3. Read REV Register
7560 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07007561 priv->hw_base = pci_iomap(pdev, 0, 0);
7562 if (!priv->hw_base) {
7563 err = -ENODEV;
7564 goto out_pci_release_regions;
7565 }
7566
7567 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7568 (unsigned long long) pci_resource_len(pdev, 0));
7569 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7570
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007571 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7572 * PCI Tx retries from interfering with C3 CPU state */
7573 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07007574
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007575 /* amp init */
7576 err = priv->cfg->ops->lib->apm_ops.init(priv);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007577 if (err < 0) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007578 IWL_DEBUG_INFO("Failed to init APMG\n");
7579 goto out_iounmap;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007580 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007581
7582 /***********************
7583 * 4. Read EEPROM
7584 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007585
Zhu Yi5a669262008-01-14 17:46:18 -08007586 /* Read the EEPROM */
7587 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007588 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007589 IWL_ERR(priv, "Unable to init EEPROM\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007590 goto out_remove_sysfs;
7591 }
7592 /* MAC Address location in EEPROM same for 3945/4965 */
7593 get_eeprom_mac(priv, priv->mac_addr);
Johannes Berge1749612008-10-27 15:59:26 -07007594 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08007595 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7596
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007597 /***********************
7598 * 5. Setup HW Constants
7599 * ********************/
7600 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007601 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007602 IWL_ERR(priv, "failed to set hw settings\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007603 goto out_iounmap;
7604 }
7605
7606 /***********************
7607 * 6. Setup priv
7608 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007609
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007610 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007611 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007612 IWL_ERR(priv, "initializing driver failed\n");
7613 goto out_free_geos;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007614 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007615
Tomas Winkler978785a2008-12-19 10:37:31 +08007616 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
7617 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007618
7619 /***********************************
7620 * 7. Initialize Module Parameters
7621 * **********************************/
7622
7623 /* Initialize module parameter values here */
7624 /* Disable radio (SW RF KILL) via parameter when loading driver */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007625 if (iwl3945_mod_params.disable) {
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007626 set_bit(STATUS_RF_KILL_SW, &priv->status);
7627 IWL_DEBUG_INFO("Radio disabled.\n");
7628 }
7629
7630
7631 /***********************
7632 * 8. Setup Services
7633 * ********************/
7634
7635 spin_lock_irqsave(&priv->lock, flags);
7636 iwl3945_disable_interrupts(priv);
7637 spin_unlock_irqrestore(&priv->lock, flags);
7638
7639 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
7640 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007641 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007642 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007643 }
7644
7645 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
7646 iwl3945_setup_deferred_work(priv);
7647 iwl3945_setup_rx_handlers(priv);
7648
7649 /***********************
7650 * 9. Conclude
7651 * ********************/
7652 pci_save_state(pdev);
7653 pci_disable_device(pdev);
7654
7655 /*********************************
7656 * 10. Setup and Register mac80211
7657 * *******************************/
7658
Zhu Yi5a669262008-01-14 17:46:18 -08007659 err = ieee80211_register_hw(priv->hw);
7660 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007661 IWL_ERR(priv, "Failed to register network device: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007662 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07007663 }
7664
Zhu Yi5a669262008-01-14 17:46:18 -08007665 priv->hw->conf.beacon_int = 100;
7666 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007667
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007668 err = iwl3945_rfkill_init(priv);
7669 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08007670 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007671 "Ignoring error: %d\n", err);
7672
Zhu Yib481de92007-09-25 17:54:57 -07007673 return 0;
7674
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007675 out_remove_sysfs:
7676 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007677 out_free_geos:
7678 iwl3945_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007679
7680 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07007681 destroy_workqueue(priv->workqueue);
7682 priv->workqueue = NULL;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007683 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007684
7685 out_iounmap:
7686 pci_iounmap(pdev, priv->hw_base);
7687 out_pci_release_regions:
7688 pci_release_regions(pdev);
7689 out_pci_disable_device:
7690 pci_disable_device(pdev);
7691 pci_set_drvdata(pdev, NULL);
7692 out_ieee80211_free_hw:
7693 ieee80211_free_hw(priv->hw);
7694 out:
7695 return err;
7696}
7697
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007698static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007699{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007700 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007701 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007702
7703 if (!priv)
7704 return;
7705
7706 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7707
Zhu Yib481de92007-09-25 17:54:57 -07007708 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007709
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08007710 if (priv->mac80211_registered) {
7711 ieee80211_unregister_hw(priv->hw);
7712 priv->mac80211_registered = 0;
7713 } else {
7714 iwl3945_down(priv);
7715 }
Zhu Yib481de92007-09-25 17:54:57 -07007716
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007717 /* make sure we flush any pending irq or
7718 * tasklet for the driver
7719 */
7720 spin_lock_irqsave(&priv->lock, flags);
7721 iwl3945_disable_interrupts(priv);
7722 spin_unlock_irqrestore(&priv->lock, flags);
7723
7724 iwl_synchronize_irq(priv);
7725
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007726 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007727
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007728 iwl3945_rfkill_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007729 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007730
7731 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007732 iwl3945_rx_queue_free(priv, &priv->rxq);
7733 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007734
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007735 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007736 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007737
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08007738 /*netif_stop_queue(dev); */
7739 flush_workqueue(priv->workqueue);
7740
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007741 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007742 * priv->workqueue... so we can't take down the workqueue
7743 * until now... */
7744 destroy_workqueue(priv->workqueue);
7745 priv->workqueue = NULL;
7746
Zhu Yib481de92007-09-25 17:54:57 -07007747 pci_iounmap(pdev, priv->hw_base);
7748 pci_release_regions(pdev);
7749 pci_disable_device(pdev);
7750 pci_set_drvdata(pdev, NULL);
7751
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007752 iwl3945_free_channel_map(priv);
7753 iwl3945_free_geos(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007754 kfree(priv->scan39);
Zhu Yib481de92007-09-25 17:54:57 -07007755 if (priv->ibss_beacon)
7756 dev_kfree_skb(priv->ibss_beacon);
7757
7758 ieee80211_free_hw(priv->hw);
7759}
7760
7761#ifdef CONFIG_PM
7762
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007763static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007764{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007765 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007766
Zhu Yie655b9f2008-01-24 02:19:38 -08007767 if (priv->is_open) {
7768 set_bit(STATUS_IN_SUSPEND, &priv->status);
7769 iwl3945_mac_stop(priv->hw);
7770 priv->is_open = 1;
7771 }
Zhu Yib481de92007-09-25 17:54:57 -07007772
Zhu Yib481de92007-09-25 17:54:57 -07007773 pci_set_power_state(pdev, PCI_D3hot);
7774
Zhu Yib481de92007-09-25 17:54:57 -07007775 return 0;
7776}
7777
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007778static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007779{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007780 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007781
Zhu Yib481de92007-09-25 17:54:57 -07007782 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07007783
Zhu Yie655b9f2008-01-24 02:19:38 -08007784 if (priv->is_open)
7785 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07007786
Zhu Yie655b9f2008-01-24 02:19:38 -08007787 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007788 return 0;
7789}
7790
7791#endif /* CONFIG_PM */
7792
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007793/*************** RFKILL FUNCTIONS **********/
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007794#ifdef CONFIG_IWL3945_RFKILL
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007795/* software rf-kill from user */
7796static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
7797{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007798 struct iwl_priv *priv = data;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007799 int err = 0;
7800
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007801 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007802 return 0;
7803
7804 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7805 return 0;
7806
Tomas Winklera96a27f2008-10-23 23:48:56 -07007807 IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007808 mutex_lock(&priv->mutex);
7809
7810 switch (state) {
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007811 case RFKILL_STATE_UNBLOCKED:
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007812 if (iwl_is_rfkill_hw(priv)) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007813 err = -EBUSY;
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007814 goto out_unlock;
7815 }
7816 iwl3945_radio_kill_sw(priv, 0);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007817 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007818 case RFKILL_STATE_SOFT_BLOCKED:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007819 iwl3945_radio_kill_sw(priv, 1);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007820 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007821 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007822 IWL_WARN(priv, "received unexpected RFKILL state %d\n", state);
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007823 break;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007824 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007825out_unlock:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007826 mutex_unlock(&priv->mutex);
7827
7828 return err;
7829}
7830
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007831int iwl3945_rfkill_init(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007832{
7833 struct device *device = wiphy_dev(priv->hw->wiphy);
7834 int ret = 0;
7835
7836 BUG_ON(device == NULL);
7837
7838 IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007839 priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
7840 if (!priv->rfkill) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007841 IWL_ERR(priv, "Unable to allocate rfkill device.\n");
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007842 ret = -ENOMEM;
7843 goto error;
7844 }
7845
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007846 priv->rfkill->name = priv->cfg->name;
7847 priv->rfkill->data = priv;
7848 priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
7849 priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
7850 priv->rfkill->user_claim_unsupported = 1;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007851
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007852 priv->rfkill->dev.class->suspend = NULL;
7853 priv->rfkill->dev.class->resume = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007854
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007855 ret = rfkill_register(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007856 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007857 IWL_ERR(priv, "Unable to register rfkill: %d\n", ret);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007858 goto freed_rfkill;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007859 }
7860
7861 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
7862 return ret;
7863
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007864freed_rfkill:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007865 if (priv->rfkill != NULL)
7866 rfkill_free(priv->rfkill);
7867 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007868
7869error:
7870 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
7871 return ret;
7872}
7873
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007874void iwl3945_rfkill_unregister(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007875{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007876 if (priv->rfkill)
7877 rfkill_unregister(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007878
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007879 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007880}
7881
7882/* set rf-kill to the right state. */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007883void iwl3945_rfkill_set_hw_state(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007884{
7885
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007886 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007887 return;
7888
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007889 if (iwl_is_rfkill_hw(priv)) {
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007890 rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
7891 return;
7892 }
7893
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007894 if (!iwl_is_rfkill_sw(priv))
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007895 rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007896 else
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007897 rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007898}
7899#endif
7900
Zhu Yib481de92007-09-25 17:54:57 -07007901/*****************************************************************************
7902 *
7903 * driver and module entry point
7904 *
7905 *****************************************************************************/
7906
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007907static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07007908 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007909 .id_table = iwl3945_hw_card_ids,
7910 .probe = iwl3945_pci_probe,
7911 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07007912#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007913 .suspend = iwl3945_pci_suspend,
7914 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07007915#endif
7916};
7917
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007918static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07007919{
7920
7921 int ret;
7922 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
7923 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007924
7925 ret = iwl3945_rate_control_register();
7926 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007927 printk(KERN_ERR DRV_NAME
7928 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007929 return ret;
7930 }
7931
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007932 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07007933 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007934 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007935 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07007936 }
Zhu Yib481de92007-09-25 17:54:57 -07007937
7938 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007939
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007940error_register:
7941 iwl3945_rate_control_unregister();
7942 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007943}
7944
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007945static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07007946{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007947 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007948 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07007949}
7950
Reinette Chatrea0987a82008-12-02 12:14:06 -08007951MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08007952
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007953module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007954MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007955module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007956MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007957module_param_named(hwcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007958MODULE_PARM_DESC(hwcrypto,
7959 "using hardware crypto engine (default 0 [software])\n");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007960module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007961MODULE_PARM_DESC(debug, "debug output mask");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007962module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007963MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
7964
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007965module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007966MODULE_PARM_DESC(queues_num, "number of hw queues.");
7967
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007968module_exit(iwl3945_exit);
7969module_init(iwl3945_init);