blob: 04466d30fe4bf7e667c04671c3b5e9fbe4d6d869 [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{
Winkler, Tomase52119c2008-12-22 11:31:19 +08002048 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
Ivo van Doorn1c014422008-04-17 19:41:02 +02002049 struct iwl3945_hw_key *keyinfo =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002050 &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002051
2052 switch (keyinfo->alg) {
2053 case ALG_CCMP:
Winkler, Tomase52119c2008-12-22 11:31:19 +08002054 tx->sec_ctl = TX_CMD_SEC_CCM;
2055 memcpy(tx->key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002056 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002057 break;
2058
2059 case ALG_TKIP:
2060#if 0
Winkler, Tomase52119c2008-12-22 11:31:19 +08002061 tx->sec_ctl = TX_CMD_SEC_TKIP;
Zhu Yib481de92007-09-25 17:54:57 -07002062
2063 if (last_frag)
Winkler, Tomase52119c2008-12-22 11:31:19 +08002064 memcpy(tx->tkip_mic.byte, skb_frag->tail - 8,
Zhu Yib481de92007-09-25 17:54:57 -07002065 8);
2066 else
Winkler, Tomase52119c2008-12-22 11:31:19 +08002067 memset(tx->tkip_mic.byte, 0, 8);
Zhu Yib481de92007-09-25 17:54:57 -07002068#endif
2069 break;
2070
2071 case ALG_WEP:
Winkler, Tomase52119c2008-12-22 11:31:19 +08002072 tx->sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02002073 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07002074
2075 if (keyinfo->keylen == 13)
Winkler, Tomase52119c2008-12-22 11:31:19 +08002076 tx->sec_ctl |= TX_CMD_SEC_KEY128;
Zhu Yib481de92007-09-25 17:54:57 -07002077
Winkler, Tomase52119c2008-12-22 11:31:19 +08002078 memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
Zhu Yib481de92007-09-25 17:54:57 -07002079
2080 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02002081 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002082 break;
2083
Zhu Yib481de92007-09-25 17:54:57 -07002084 default:
Tomas Winkler978785a2008-12-19 10:37:31 +08002085 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -07002086 break;
2087 }
2088}
2089
2090/*
2091 * handle build REPLY_TX command notification.
2092 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002093static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002094 struct iwl_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02002095 struct ieee80211_tx_info *info,
Winkler, Tomase52119c2008-12-22 11:31:19 +08002096 struct ieee80211_hdr *hdr, u8 std_id)
Zhu Yib481de92007-09-25 17:54:57 -07002097{
Winkler, Tomase52119c2008-12-22 11:31:19 +08002098 struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
2099 __le32 tx_flags = tx->tx_flags;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002100 __le16 fc = hdr->frame_control;
Johannes Berge6a98542008-10-21 12:40:02 +02002101 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07002102
Winkler, Tomase52119c2008-12-22 11:31:19 +08002103 tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02002104 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07002105 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002106 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002107 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002108 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07002109 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2110 tx_flags |= TX_CMD_FLG_TSF_MSK;
2111 } else {
2112 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2113 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2114 }
2115
Winkler, Tomase52119c2008-12-22 11:31:19 +08002116 tx->sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002117 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002118 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2119
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002120 if (ieee80211_is_data_qos(fc)) {
2121 u8 *qc = ieee80211_get_qos_ctl(hdr);
Winkler, Tomase52119c2008-12-22 11:31:19 +08002122 tx->tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07002123 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002124 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002125 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002126 }
Zhu Yib481de92007-09-25 17:54:57 -07002127
Johannes Berge6a98542008-10-21 12:40:02 +02002128 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07002129 tx_flags |= TX_CMD_FLG_RTS_MSK;
2130 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02002131 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07002132 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2133 tx_flags |= TX_CMD_FLG_CTS_MSK;
2134 }
2135
2136 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2137 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2138
2139 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002140 if (ieee80211_is_mgmt(fc)) {
2141 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Winkler, Tomase52119c2008-12-22 11:31:19 +08002142 tx->timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002143 else
Winkler, Tomase52119c2008-12-22 11:31:19 +08002144 tx->timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002145 } else {
Winkler, Tomase52119c2008-12-22 11:31:19 +08002146 tx->timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002147#ifdef CONFIG_IWL3945_LEDS
2148 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
2149#endif
2150 }
Zhu Yib481de92007-09-25 17:54:57 -07002151
Winkler, Tomase52119c2008-12-22 11:31:19 +08002152 tx->driver_txop = 0;
2153 tx->tx_flags = tx_flags;
2154 tx->next_frame_len = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002155}
2156
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002157/**
2158 * iwl3945_get_sta_id - Find station's index within station table
2159 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002160static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002161{
2162 int sta_id;
2163 u16 fc = le16_to_cpu(hdr->frame_control);
2164
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002165 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002166 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2167 is_multicast_ether_addr(hdr->addr1))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002168 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002169
2170 switch (priv->iw_mode) {
2171
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002172 /* If we are a client station in a BSS network, use the special
2173 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02002174 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002175 return IWL_AP_ID;
2176
2177 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02002178 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002179 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002180 if (sta_id != IWL_INVALID_STATION)
2181 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002182 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002183
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002184 /* If this frame is going out to an IBSS network, find the station,
2185 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02002186 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002187 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002188 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002189 if (sta_id != IWL_INVALID_STATION)
2190 return sta_id;
2191
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002192 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002193
2194 if (sta_id != IWL_INVALID_STATION)
2195 return sta_id;
2196
Johannes Berge1749612008-10-27 15:59:26 -07002197 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002198 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07002199 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002200 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002201 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002202 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08002203 /* If we are in monitor mode, use BCAST. This is required for
2204 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02002205 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002206 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +08002207
Zhu Yib481de92007-09-25 17:54:57 -07002208 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002209 IWL_WARN(priv, "Unknown mode of operation: %d\n",
2210 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002211 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002212 }
2213}
2214
2215/*
2216 * start REPLY_TX command process
2217 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002218static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002219{
2220 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02002221 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002222 struct iwl3945_tfd *tfd;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002223 struct iwl3945_tx_cmd *tx;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002224 struct iwl_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002225 struct iwl_queue *q = NULL;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002226 struct iwl_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002227 dma_addr_t phys_addr;
2228 dma_addr_t txcmd_phys;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002229 int txq_id = skb_get_queue_mapping(skb);
Tomas Winkler54dbb522008-05-15 13:54:06 +08002230 u16 len, idx, len_org, hdr_len;
2231 u8 id;
2232 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07002233 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002234 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002235 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002236 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07002237 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002238 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002239 unsigned long flags;
2240 int rc;
2241
2242 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002243 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002244 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2245 goto drop_unlock;
2246 }
2247
Johannes Berge039fa42008-05-15 12:55:29 +02002248 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002249 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002250 goto drop_unlock;
2251 }
2252
2253 unicast = !is_multicast_ether_addr(hdr->addr1);
2254 id = 0;
2255
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002256 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002257
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002258#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002259 if (ieee80211_is_auth(fc))
2260 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002261 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002262 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002263 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002264 IWL_DEBUG_TX("Sending REASSOC frame\n");
2265#endif
2266
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002267 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002268 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002269 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002270 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02002271 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002272 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002273 goto drop_unlock;
2274 }
2275
2276 spin_unlock_irqrestore(&priv->lock, flags);
2277
Harvey Harrison7294ec92008-07-15 18:43:59 -07002278 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002279
2280 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002281 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002282 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002283 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
2284 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002285 goto drop;
2286 }
2287
2288 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2289
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002290 if (ieee80211_is_data_qos(fc)) {
2291 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07002292 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002293 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -07002294 IEEE80211_SCTL_SEQ;
2295 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2296 (hdr->seq_ctrl &
2297 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2298 seq_number += 0x10;
2299 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002300
2301 /* Descriptor for chosen Tx queue */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002302 txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002303 q = &txq->q;
2304
2305 spin_lock_irqsave(&priv->lock, flags);
2306
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002307 /* Set up first empty TFD within this queue's circular TFD buffer */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002308 tfd = &txq->tfds39[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002309 memset(tfd, 0, sizeof(*tfd));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002310 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002311
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002312 /* Set up driver data for this TFD */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002313 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002314 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002315
2316 /* Init first empty entry in queue's array of Tx/cmd buffers */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002317 out_cmd = txq->cmd[idx];
Winkler, Tomase52119c2008-12-22 11:31:19 +08002318 tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
Zhu Yib481de92007-09-25 17:54:57 -07002319 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
Winkler, Tomase52119c2008-12-22 11:31:19 +08002320 memset(tx, 0, sizeof(*tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002321
2322 /*
2323 * Set up the Tx-command (not MAC!) header.
2324 * Store the chosen Tx queue and TFD index within the sequence field;
2325 * after Tx, uCode's Tx response will return this value so driver can
2326 * locate the frame within the tx queue and do post-tx processing.
2327 */
Zhu Yib481de92007-09-25 17:54:57 -07002328 out_cmd->hdr.cmd = REPLY_TX;
2329 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002330 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002331
2332 /* Copy MAC header from skb into command buffer */
Winkler, Tomase52119c2008-12-22 11:31:19 +08002333 memcpy(tx->hdr, hdr, hdr_len);
Zhu Yib481de92007-09-25 17:54:57 -07002334
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002335 /*
2336 * Use the first empty entry in this queue's command buffer array
2337 * to contain the Tx command and MAC header concatenated together
2338 * (payload data will be in another buffer).
2339 * Size of this varies, due to varying MAC header length.
2340 * If end is not dword aligned, we'll have 2 extra bytes at the end
2341 * of the MAC header (device reads on dword boundaries).
2342 * We'll tell device about this padding later.
2343 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002344 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +08002345 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002346
2347 len_org = len;
2348 len = (len + 3) & ~3;
2349
2350 if (len_org != len)
2351 len_org = 1;
2352 else
2353 len_org = 0;
2354
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002355 /* Physical address of this Tx command's header (not MAC header!),
2356 * within command buffer array. */
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08002357 txcmd_phys = pci_map_single(priv->pci_dev,
2358 out_cmd, sizeof(struct iwl_cmd),
2359 PCI_DMA_TODEVICE);
2360 pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
2361 pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
2362 /* Add buffer containing Tx command and MAC(!) header to TFD's
2363 * first entry */
2364 txcmd_phys += offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002365
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002366 /* Add buffer containing Tx command and MAC(!) header to TFD's
2367 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002368 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002369
Johannes Bergd0f09802008-07-29 11:32:07 +02002370 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02002371 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002372
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002373 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2374 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002375 len = skb->len - hdr_len;
2376 if (len) {
2377 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2378 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002379 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002380 }
2381
Zhu Yib481de92007-09-25 17:54:57 -07002382 if (!len)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002383 /* If there is no payload, then we use only one Tx buffer */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002384 tfd->control_flags = cpu_to_le32(TFD_CTL_COUNT_SET(1));
Zhu Yib481de92007-09-25 17:54:57 -07002385 else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002386 /* Else use 2 buffers.
2387 * Tell 3945 about any padding after MAC header */
Winkler, Tomasdbb66542008-12-22 11:31:14 +08002388 tfd->control_flags = cpu_to_le32(TFD_CTL_COUNT_SET(2) |
2389 TFD_CTL_PAD_SET(U32_PAD(len)));
Zhu Yib481de92007-09-25 17:54:57 -07002390
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002391 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002392 len = (u16)skb->len;
Winkler, Tomase52119c2008-12-22 11:31:19 +08002393 tx->len = cpu_to_le16(len);
Zhu Yib481de92007-09-25 17:54:57 -07002394
2395 /* TODO need this for burst mode later on */
Winkler, Tomase52119c2008-12-22 11:31:19 +08002396 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002397
2398 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02002399 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002400
Winkler, Tomase52119c2008-12-22 11:31:19 +08002401 tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2402 tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002403
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002404 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07002405 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002406 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002407 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07002408 } else {
2409 wait_write_ptr = 1;
2410 txq->need_update = 0;
2411 }
2412
Winkler, Tomase52119c2008-12-22 11:31:19 +08002413 iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
Zhu Yib481de92007-09-25 17:54:57 -07002414
Winkler, Tomase52119c2008-12-22 11:31:19 +08002415 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07002416 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07002417
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002418 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002419 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002420 rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002421 spin_unlock_irqrestore(&priv->lock, flags);
2422
2423 if (rc)
2424 return rc;
2425
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002426 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002427 && priv->mac80211_registered) {
2428 if (wait_write_ptr) {
2429 spin_lock_irqsave(&priv->lock, flags);
2430 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002431 iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002432 spin_unlock_irqrestore(&priv->lock, flags);
2433 }
2434
Johannes Berge2530082008-05-17 00:57:14 +02002435 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07002436 }
2437
2438 return 0;
2439
2440drop_unlock:
2441 spin_unlock_irqrestore(&priv->lock, flags);
2442drop:
2443 return -1;
2444}
2445
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002446static void iwl3945_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002447{
Johannes Berg8318d782008-01-24 19:38:38 +01002448 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002449 struct ieee80211_rate *rate;
2450 int i;
2451
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08002452 sband = iwl_get_hw_mode(priv, priv->band);
Johannes Berg8318d782008-01-24 19:38:38 +01002453 if (!sband) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002454 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002455 return;
2456 }
Zhu Yib481de92007-09-25 17:54:57 -07002457
2458 priv->active_rate = 0;
2459 priv->active_rate_basic = 0;
2460
Johannes Berg8318d782008-01-24 19:38:38 +01002461 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
2462 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07002463
Johannes Berg8318d782008-01-24 19:38:38 +01002464 for (i = 0; i < sband->n_bitrates; i++) {
2465 rate = &sband->bitrates[i];
2466 if ((rate->hw_value < IWL_RATE_COUNT) &&
2467 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
2468 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
2469 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
2470 priv->active_rate |= (1 << rate->hw_value);
2471 }
Zhu Yib481de92007-09-25 17:54:57 -07002472 }
2473
2474 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2475 priv->active_rate, priv->active_rate_basic);
2476
2477 /*
2478 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2479 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2480 * OFDM
2481 */
2482 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002483 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002484 ((priv->active_rate_basic &
2485 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2486 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002487 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002488 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2489
2490 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002491 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002492 ((priv->active_rate_basic &
2493 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2494 IWL_FIRST_OFDM_RATE) & 0xFF;
2495 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002496 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002497 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2498}
2499
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002500static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002501{
2502 unsigned long flags;
2503
2504 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2505 return;
2506
2507 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2508 disable_radio ? "OFF" : "ON");
2509
2510 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002511 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002512 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02002513 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002514 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002515 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002516 CSR_UCODE_SW_BIT_RFKILL);
2517 spin_unlock_irqrestore(&priv->lock, flags);
Samuel Ortizc4962942008-12-22 11:31:18 +08002518 iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002519 set_bit(STATUS_RF_KILL_SW, &priv->status);
2520 }
2521 return;
2522 }
2523
2524 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002525 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002526
2527 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2528 spin_unlock_irqrestore(&priv->lock, flags);
2529
2530 /* wake up ucode */
2531 msleep(10);
2532
2533 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002534 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2535 if (!iwl_grab_nic_access(priv))
2536 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002537 spin_unlock_irqrestore(&priv->lock, flags);
2538
2539 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2540 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2541 "disabled by HW switch\n");
2542 return;
2543 }
2544
Zhu Yi808e72a2008-06-12 09:47:17 +08002545 if (priv->is_open)
2546 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002547 return;
2548}
2549
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002550void iwl3945_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002551 u32 decrypt_res, struct ieee80211_rx_status *stats)
2552{
2553 u16 fc =
2554 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2555
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002556 if (priv->active39_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002557 return;
2558
2559 if (!(fc & IEEE80211_FCTL_PROTECTED))
2560 return;
2561
2562 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2563 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2564 case RX_RES_STATUS_SEC_TYPE_TKIP:
2565 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2566 RX_RES_STATUS_BAD_ICV_MIC)
2567 stats->flag |= RX_FLAG_MMIC_ERROR;
2568 case RX_RES_STATUS_SEC_TYPE_WEP:
2569 case RX_RES_STATUS_SEC_TYPE_CCMP:
2570 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2571 RX_RES_STATUS_DECRYPT_OK) {
2572 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2573 stats->flag |= RX_FLAG_DECRYPTED;
2574 }
2575 break;
2576
2577 default:
2578 break;
2579 }
2580}
2581
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002582#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002583
2584#include "iwl-spectrum.h"
2585
2586#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2587#define BEACON_TIME_MASK_HIGH 0xFF000000
2588#define TIME_UNIT 1024
2589
2590/*
2591 * extended beacon time format
2592 * time in usec will be changed into a 32-bit value in 8:24 format
2593 * the high 1 byte is the beacon counts
2594 * the lower 3 bytes is the time in usec within one beacon interval
2595 */
2596
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002597static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002598{
2599 u32 quot;
2600 u32 rem;
2601 u32 interval = beacon_interval * 1024;
2602
2603 if (!interval || !usec)
2604 return 0;
2605
2606 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2607 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2608
2609 return (quot << 24) + rem;
2610}
2611
2612/* base is usually what we get from ucode with each received frame,
2613 * the same as HW timer counter counting down
2614 */
2615
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002616static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002617{
2618 u32 base_low = base & BEACON_TIME_MASK_LOW;
2619 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2620 u32 interval = beacon_interval * TIME_UNIT;
2621 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2622 (addon & BEACON_TIME_MASK_HIGH);
2623
2624 if (base_low > addon_low)
2625 res += base_low - addon_low;
2626 else if (base_low < addon_low) {
2627 res += interval + base_low - addon_low;
2628 res += (1 << 24);
2629 } else
2630 res += (1 << 24);
2631
2632 return cpu_to_le32(res);
2633}
2634
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002635static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002636 struct ieee80211_measurement_params *params,
2637 u8 type)
2638{
Tomas Winkler600c0e12008-12-19 10:37:04 +08002639 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002640 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002641 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002642 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2643 .data = (void *)&spectrum,
2644 .meta.flags = CMD_WANT_SKB,
2645 };
2646 u32 add_time = le64_to_cpu(params->start_time);
2647 int rc;
2648 int spectrum_resp_status;
2649 int duration = le16_to_cpu(params->duration);
2650
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002651 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002652 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002653 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002654 le64_to_cpu(params->start_time) - priv->last_tsf,
2655 le16_to_cpu(priv->rxon_timing.beacon_interval));
2656
2657 memset(&spectrum, 0, sizeof(spectrum));
2658
2659 spectrum.channel_count = cpu_to_le16(1);
2660 spectrum.flags =
2661 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2662 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2663 cmd.len = sizeof(spectrum);
2664 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2665
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002666 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002667 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002668 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002669 add_time,
2670 le16_to_cpu(priv->rxon_timing.beacon_interval));
2671 else
2672 spectrum.start_time = 0;
2673
2674 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2675 spectrum.channels[0].channel = params->channel;
2676 spectrum.channels[0].type = type;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002677 if (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002678 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2679 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2680
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002681 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002682 if (rc)
2683 return rc;
2684
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002685 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002686 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002687 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07002688 rc = -EIO;
2689 }
2690
2691 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2692 switch (spectrum_resp_status) {
2693 case 0: /* Command will be handled */
2694 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002695 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
2696 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07002697 priv->measurement_status &= ~MEASUREMENT_READY;
2698 }
2699 priv->measurement_status |= MEASUREMENT_ACTIVE;
2700 rc = 0;
2701 break;
2702
2703 case 1: /* Command will not be handled */
2704 rc = -EAGAIN;
2705 break;
2706 }
2707
2708 dev_kfree_skb_any(cmd.meta.u.skb);
2709
2710 return rc;
2711}
2712#endif
2713
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002714static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002715 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002716{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002717 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
2718 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002719 struct delayed_work *pwork;
2720
2721 palive = &pkt->u.alive_frame;
2722
2723 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2724 "0x%01X 0x%01X\n",
2725 palive->is_valid, palive->ver_type,
2726 palive->ver_subtype);
2727
2728 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2729 IWL_DEBUG_INFO("Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002730 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
2731 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002732 pwork = &priv->init_alive_start;
2733 } else {
2734 IWL_DEBUG_INFO("Runtime Alive received.\n");
2735 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002736 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002737 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002738 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002739 }
2740
2741 /* We delay the ALIVE response by 5ms to
2742 * give the HW RF Kill time to activate... */
2743 if (palive->is_valid == UCODE_VALID_OK)
2744 queue_delayed_work(priv->workqueue, pwork,
2745 msecs_to_jiffies(5));
2746 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002747 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002748}
2749
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002750static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002751 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002752{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002753 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002754
2755 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2756 return;
2757}
2758
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002759static void iwl3945_rx_reply_error(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002760 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002761{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002762 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002763
Winkler, Tomas15b16872008-12-19 10:37:33 +08002764 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
Zhu Yib481de92007-09-25 17:54:57 -07002765 "seq 0x%04X ser 0x%08X\n",
2766 le32_to_cpu(pkt->u.err_resp.error_type),
2767 get_cmd_string(pkt->u.err_resp.cmd_id),
2768 pkt->u.err_resp.cmd_id,
2769 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2770 le32_to_cpu(pkt->u.err_resp.error_info));
2771}
2772
2773#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2774
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002775static void iwl3945_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002776{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002777 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002778 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active39_rxon;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002779 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002780 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2781 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2782 rxon->channel = csa->channel;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002783 priv->staging39_rxon.channel = csa->channel;
Zhu Yib481de92007-09-25 17:54:57 -07002784}
2785
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002786static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002787 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002788{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002789#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002790 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002791 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002792
2793 if (!report->state) {
2794 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2795 "Spectrum Measure Notification: Start\n");
2796 return;
2797 }
2798
2799 memcpy(&priv->measure_report, report, sizeof(*report));
2800 priv->measurement_status |= MEASUREMENT_READY;
2801#endif
2802}
2803
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002804static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002805 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002806{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002807#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002808 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002809 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002810 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2811 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2812#endif
2813}
2814
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002815static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002816 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002817{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002818 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002819 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2820 "notification for %s:\n",
2821 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002822 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
2823 le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002824}
2825
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002826static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002827{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002828 struct iwl_priv *priv =
2829 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002830 struct sk_buff *beacon;
2831
2832 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02002833 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07002834
2835 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002836 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07002837 return;
2838 }
2839
2840 mutex_lock(&priv->mutex);
2841 /* new beacon skb is allocated every time; dispose previous.*/
2842 if (priv->ibss_beacon)
2843 dev_kfree_skb(priv->ibss_beacon);
2844
2845 priv->ibss_beacon = beacon;
2846 mutex_unlock(&priv->mutex);
2847
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002848 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002849}
2850
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002851static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002852 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002853{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002854#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002855 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002856 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07002857 u8 rate = beacon->beacon_notify_hdr.rate;
2858
2859 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2860 "tsf %d %d rate %d\n",
2861 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2862 beacon->beacon_notify_hdr.failure_frame,
2863 le32_to_cpu(beacon->ibss_mgr_status),
2864 le32_to_cpu(beacon->high_tsf),
2865 le32_to_cpu(beacon->low_tsf), rate);
2866#endif
2867
Johannes Berg05c914f2008-09-11 00:01:58 +02002868 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07002869 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2870 queue_work(priv->workqueue, &priv->beacon_update);
2871}
2872
2873/* Service response to REPLY_SCAN_CMD (0x80) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002874static void iwl3945_rx_reply_scan(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002875 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002876{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002877#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002878 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002879 struct iwl_scanreq_notification *notif =
2880 (struct iwl_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002881
2882 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2883#endif
2884}
2885
2886/* Service SCAN_START_NOTIFICATION (0x82) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002887static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002888 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002889{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002890 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002891 struct iwl_scanstart_notification *notif =
2892 (struct iwl_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002893 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2894 IWL_DEBUG_SCAN("Scan start: "
2895 "%d [802.11%s] "
2896 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2897 notif->channel,
2898 notif->band ? "bg" : "a",
2899 notif->tsf_high,
2900 notif->tsf_low, notif->status, notif->beacon_timer);
2901}
2902
2903/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002904static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002905 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002906{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002907 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002908 struct iwl_scanresults_notification *notif =
2909 (struct iwl_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002910
2911 IWL_DEBUG_SCAN("Scan ch.res: "
2912 "%d [802.11%s] "
2913 "(TSF: 0x%08X:%08X) - %d "
2914 "elapsed=%lu usec (%dms since last)\n",
2915 notif->channel,
2916 notif->band ? "bg" : "a",
2917 le32_to_cpu(notif->tsf_high),
2918 le32_to_cpu(notif->tsf_low),
2919 le32_to_cpu(notif->statistics[0]),
2920 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2921 jiffies_to_msecs(elapsed_jiffies
2922 (priv->last_scan_jiffies, jiffies)));
2923
2924 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002925 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002926}
2927
2928/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002929static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002930 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002931{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002932 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002933 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002934
2935 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2936 scan_notif->scanned_channels,
2937 scan_notif->tsf_low,
2938 scan_notif->tsf_high, scan_notif->status);
2939
2940 /* The HW is no longer scanning */
2941 clear_bit(STATUS_SCAN_HW, &priv->status);
2942
2943 /* The scan completion notification came in, so kill that timer... */
2944 cancel_delayed_work(&priv->scan_check);
2945
2946 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002947 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
2948 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07002949 jiffies_to_msecs(elapsed_jiffies
2950 (priv->scan_pass_start, jiffies)));
2951
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002952 /* Remove this scanned band from the list of pending
2953 * bands to scan, band G precedes A in order of scanning
2954 * as seen in iwl3945_bg_request_scan */
2955 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
2956 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
2957 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
2958 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002959
2960 /* If a request to abort was given, or the scan did not succeed
2961 * then we reset the scan state machine and terminate,
2962 * re-queuing another scan if one has been requested */
2963 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2964 IWL_DEBUG_INFO("Aborted scan completed.\n");
2965 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2966 } else {
2967 /* If there are more bands on this scan pass reschedule */
2968 if (priv->scan_bands > 0)
2969 goto reschedule;
2970 }
2971
2972 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002973 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002974 IWL_DEBUG_INFO("Setting scan to off\n");
2975
2976 clear_bit(STATUS_SCANNING, &priv->status);
2977
2978 IWL_DEBUG_INFO("Scan took %dms\n",
2979 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2980
2981 queue_work(priv->workqueue, &priv->scan_completed);
2982
2983 return;
2984
2985reschedule:
2986 priv->scan_pass_start = jiffies;
2987 queue_work(priv->workqueue, &priv->request_scan);
2988}
2989
2990/* Handle notification from uCode that card's power state is changing
2991 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002992static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002993 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002994{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002995 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002996 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2997 unsigned long status = priv->status;
2998
2999 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3000 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3001 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3002
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003003 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003004 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3005
3006 if (flags & HW_CARD_DISABLED)
3007 set_bit(STATUS_RF_KILL_HW, &priv->status);
3008 else
3009 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3010
3011
3012 if (flags & SW_CARD_DISABLED)
3013 set_bit(STATUS_RF_KILL_SW, &priv->status);
3014 else
3015 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3016
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003017 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003018
3019 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3020 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3021 (test_bit(STATUS_RF_KILL_SW, &status) !=
3022 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3023 queue_work(priv->workqueue, &priv->rf_kill);
3024 else
3025 wake_up_interruptible(&priv->wait_command_queue);
3026}
3027
3028/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003029 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003030 *
3031 * Setup the RX handlers for each of the reply types sent from the uCode
3032 * to the host.
3033 *
3034 * This function chains into the hardware specific files for them to setup
3035 * any hardware specific handlers as well.
3036 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003037static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003038{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003039 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
3040 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
3041 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
3042 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003043 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003044 iwl3945_rx_spectrum_measure_notif;
3045 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003046 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003047 iwl3945_rx_pm_debug_statistics_notif;
3048 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003049
Ben Cahill9fbab512007-11-29 11:09:47 +08003050 /*
3051 * The same handler is used for both the REPLY to a discrete
3052 * statistics request from the host as well as for the periodic
3053 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003054 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003055 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
3056 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003057
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003058 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
3059 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003060 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003061 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003062 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003063 iwl3945_rx_scan_complete_notif;
3064 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003065
Ben Cahill9fbab512007-11-29 11:09:47 +08003066 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003067 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003068}
3069
3070/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08003071 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
3072 * When FW advances 'R' index, all entries between old and new 'R' index
3073 * need to be reclaimed.
3074 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003075static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003076 int txq_id, int index)
3077{
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003078 struct iwl_tx_queue *txq = &priv->txq[txq_id];
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003079 struct iwl_queue *q = &txq->q;
Tomas Winkler91c066f2008-03-06 17:36:55 -08003080 int nfreed = 0;
3081
3082 if ((index >= q->n_bd) || (iwl3945_x2_queue_used(q, index) == 0)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003083 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
Tomas Winkler91c066f2008-03-06 17:36:55 -08003084 "is out of range [0-%d] %d %d.\n", txq_id,
3085 index, q->n_bd, q->write_ptr, q->read_ptr);
3086 return;
3087 }
3088
3089 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
3090 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3091 if (nfreed > 1) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003092 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003093 q->write_ptr, q->read_ptr);
3094 queue_work(priv->workqueue, &priv->restart);
3095 break;
3096 }
3097 nfreed++;
3098 }
3099}
3100
3101
3102/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003103 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003104 * @rxb: Rx buffer to reclaim
3105 *
3106 * If an Rx buffer has an async callback associated with it the callback
3107 * will be executed. The attached skb (if present) will only be freed
3108 * if the callback returns 1
3109 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003110static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003111 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003112{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003113 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003114 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3115 int txq_id = SEQ_TO_QUEUE(sequence);
3116 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003117 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Zhu Yib481de92007-09-25 17:54:57 -07003118 int cmd_index;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08003119 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003120
Zhu Yib481de92007-09-25 17:54:57 -07003121 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3122
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003123 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3124 cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Zhu Yib481de92007-09-25 17:54:57 -07003125
3126 /* Input error checking is done when commands are added to queue. */
3127 if (cmd->meta.flags & CMD_WANT_SKB) {
3128 cmd->meta.source->u.skb = rxb->skb;
3129 rxb->skb = NULL;
3130 } else if (cmd->meta.u.callback &&
3131 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3132 rxb->skb = NULL;
3133
Tomas Winkler91c066f2008-03-06 17:36:55 -08003134 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003135
3136 if (!(cmd->meta.flags & CMD_ASYNC)) {
3137 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3138 wake_up_interruptible(&priv->wait_command_queue);
3139 }
3140}
3141
3142/************************** RX-FUNCTIONS ****************************/
3143/*
3144 * Rx theory of operation
3145 *
3146 * The host allocates 32 DMA target addresses and passes the host address
3147 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3148 * 0 to 31
3149 *
3150 * Rx Queue Indexes
3151 * The host/firmware share two index registers for managing the Rx buffers.
3152 *
3153 * The READ index maps to the first position that the firmware may be writing
3154 * to -- the driver can read up to (but not including) this position and get
3155 * good data.
3156 * The READ index is managed by the firmware once the card is enabled.
3157 *
3158 * The WRITE index maps to the last position the driver has read from -- the
3159 * position preceding WRITE is the last slot the firmware can place a packet.
3160 *
3161 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3162 * WRITE = READ.
3163 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003164 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003165 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3166 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003167 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003168 * and fire the RX interrupt. The driver can then query the READ index and
3169 * process as many packets as possible, moving the WRITE index forward as it
3170 * resets the Rx queue buffers with new memory.
3171 *
3172 * The management in the driver is as follows:
3173 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3174 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003175 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003176 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003177 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3178 * 'processed' and 'read' driver indexes as well)
3179 * + A received packet is processed and handed to the kernel network stack,
3180 * detached from the iwl->rxq. The driver 'processed' index is updated.
3181 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3182 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3183 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3184 * were enough free buffers and RX_STALLED is set it is cleared.
3185 *
3186 *
3187 * Driver sequence:
3188 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003189 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003190 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003191 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003192 * queue, updates firmware pointers, and updates
3193 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003194 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003195 *
3196 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003197 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003198 * READ INDEX, detaching the SKB from the pool.
3199 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003200 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003201 * slots.
3202 * ...
3203 *
3204 */
3205
3206/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003207 * iwl3945_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003208 */
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003209static int iwl3945_rx_queue_space(const struct iwl_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003210{
3211 int s = q->read - q->write;
3212 if (s <= 0)
3213 s += RX_QUEUE_SIZE;
3214 /* keep some buffer to not confuse full and empty queue */
3215 s -= 2;
3216 if (s < 0)
3217 s = 0;
3218 return s;
3219}
3220
3221/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003222 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003223 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003224static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003225 dma_addr_t dma_addr)
3226{
3227 return cpu_to_le32((u32)dma_addr);
3228}
3229
3230/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003231 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003232 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003233 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003234 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003235 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003236 *
3237 * This moves the 'write' index forward to catch up with 'processed', and
3238 * also updates the memory address in the firmware to reference the new
3239 * target buffer.
3240 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003241static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003242{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003243 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003244 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003245 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003246 unsigned long flags;
3247 int write, rc;
3248
3249 spin_lock_irqsave(&rxq->lock, flags);
3250 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003251 while ((iwl3945_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003252 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003253 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003254 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003255 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003256
3257 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003258 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003259 rxq->queue[rxq->write] = rxb;
3260 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3261 rxq->free_count--;
3262 }
3263 spin_unlock_irqrestore(&rxq->lock, flags);
3264 /* If the pre-allocated buffer pool is dropping low, schedule to
3265 * refill it */
3266 if (rxq->free_count <= RX_LOW_WATERMARK)
3267 queue_work(priv->workqueue, &priv->rx_replenish);
3268
3269
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003270 /* If we've added more space for the firmware to place data, tell it.
3271 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003272 if ((write != (rxq->write & ~0x7))
3273 || (abs(rxq->write - rxq->read) > 7)) {
3274 spin_lock_irqsave(&rxq->lock, flags);
3275 rxq->need_update = 1;
3276 spin_unlock_irqrestore(&rxq->lock, flags);
Winkler, Tomas141c43a2009-01-08 10:19:53 -08003277 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003278 if (rc)
3279 return rc;
3280 }
3281
3282 return 0;
3283}
3284
3285/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003286 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003287 *
3288 * When moving to rx_free an SKB is allocated for the slot.
3289 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003290 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003291 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003292 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003293static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003294{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003295 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003296 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003297 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003298 unsigned long flags;
3299 spin_lock_irqsave(&rxq->lock, flags);
3300 while (!list_empty(&rxq->rx_used)) {
3301 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003302 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003303
3304 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003305 rxb->skb =
3306 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
3307 if (!rxb->skb) {
3308 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08003309 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07003310 /* We don't reschedule replenish work here -- we will
3311 * call the restock method and if it still needs
3312 * more buffers it will schedule replenish */
3313 break;
3314 }
Zhu Yi12342c42007-12-20 11:27:32 +08003315
3316 /* If radiotap head is required, reserve some headroom here.
3317 * The physical head count is a variable rx_stats->phy_count.
3318 * We reserve 4 bytes here. Plus these extra bytes, the
3319 * headroom of the physical head should be enough for the
3320 * radiotap head that iwl3945 supported. See iwl3945_rt.
3321 */
3322 skb_reserve(rxb->skb, 4);
3323
Zhu Yib481de92007-09-25 17:54:57 -07003324 priv->alloc_rxb_skb++;
3325 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003326
3327 /* Get physical address of RB/SKB */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003328 rxb->real_dma_addr =
Zhu Yib481de92007-09-25 17:54:57 -07003329 pci_map_single(priv->pci_dev, rxb->skb->data,
3330 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3331 list_add_tail(&rxb->list, &rxq->rx_free);
3332 rxq->free_count++;
3333 }
3334 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003335}
3336
3337/*
3338 * this should be called while priv->lock is locked
3339 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003340static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003341{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003342 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003343
3344 iwl3945_rx_allocate(priv);
3345 iwl3945_rx_queue_restock(priv);
3346}
3347
3348
3349void iwl3945_rx_replenish(void *data)
3350{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003351 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003352 unsigned long flags;
3353
3354 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003355
3356 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003357 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003358 spin_unlock_irqrestore(&priv->lock, flags);
3359}
3360
Zhu Yib481de92007-09-25 17:54:57 -07003361/* Convert linear signal-to-noise ratio into dB */
3362static u8 ratio2dB[100] = {
3363/* 0 1 2 3 4 5 6 7 8 9 */
3364 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3365 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3366 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3367 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3368 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3369 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3370 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3371 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3372 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3373 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3374};
3375
3376/* Calculates a relative dB value from a ratio of linear
3377 * (i.e. not dB) signal levels.
3378 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003379int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003380{
Adrian Bunk221c80c2008-02-02 23:19:01 +02003381 /* 1000:1 or higher just report as 60 dB */
3382 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003383 return 60;
3384
Adrian Bunk221c80c2008-02-02 23:19:01 +02003385 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003386 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02003387 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003388 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07003389
3390 /* We shouldn't see this */
3391 if (sig_ratio < 1)
3392 return 0;
3393
3394 /* Use table for ratios 1:1 - 99:1 */
3395 return (int)ratio2dB[sig_ratio];
3396}
3397
3398#define PERFECT_RSSI (-20) /* dBm */
3399#define WORST_RSSI (-95) /* dBm */
3400#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3401
3402/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3403 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3404 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003405int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003406{
3407 int sig_qual;
3408 int degradation = PERFECT_RSSI - rssi_dbm;
3409
3410 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3411 * as indicator; formula is (signal dbm - noise dbm).
3412 * SNR at or above 40 is a great signal (100%).
3413 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3414 * Weakest usable signal is usually 10 - 15 dB SNR. */
3415 if (noise_dbm) {
3416 if (rssi_dbm - noise_dbm >= 40)
3417 return 100;
3418 else if (rssi_dbm < noise_dbm)
3419 return 0;
3420 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3421
3422 /* Else use just the signal level.
3423 * This formula is a least squares fit of data points collected and
3424 * compared with a reference system that had a percentage (%) display
3425 * for signal quality. */
3426 } else
3427 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3428 (15 * RSSI_RANGE + 62 * degradation)) /
3429 (RSSI_RANGE * RSSI_RANGE);
3430
3431 if (sig_qual > 100)
3432 sig_qual = 100;
3433 else if (sig_qual < 1)
3434 sig_qual = 0;
3435
3436 return sig_qual;
3437}
3438
3439/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003440 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003441 *
3442 * Uses the priv->rx_handlers callback function array to invoke
3443 * the appropriate handlers, including command responses,
3444 * frame-received notifications, and other notifications.
3445 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003446static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003447{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003448 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003449 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003450 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003451 u32 r, i;
3452 int reclaim;
3453 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003454 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003455 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003456
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003457 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3458 * buffer that the driver may process (last buffer filled by ucode). */
Winkler, Tomas8cd812b2008-12-19 10:37:43 +08003459 r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
Zhu Yib481de92007-09-25 17:54:57 -07003460 i = rxq->read;
3461
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003462 if (iwl3945_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3463 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003464 /* Rx interrupt, but nothing sent from uCode */
3465 if (i == r)
3466 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3467
3468 while (i != r) {
3469 rxb = rxq->queue[i];
3470
Ben Cahill9fbab512007-11-29 11:09:47 +08003471 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003472 * then a bug has been introduced in the queue refilling
3473 * routines -- catch it here */
3474 BUG_ON(rxb == NULL);
3475
3476 rxq->queue[i] = NULL;
3477
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003478 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003479 IWL_RX_BUF_SIZE,
3480 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003481 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003482
3483 /* Reclaim a command buffer only if this packet is a response
3484 * to a (driver-originated) command.
3485 * If the packet (e.g. Rx frame) originated from uCode,
3486 * there is no command buffer to reclaim.
3487 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3488 * but apparently a few don't get set; catch them here. */
3489 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3490 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3491 (pkt->hdr.cmd != REPLY_TX);
3492
3493 /* Based on type of command response or notification,
3494 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003495 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003496 if (priv->rx_handlers[pkt->hdr.cmd]) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003497 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003498 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3499 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3500 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3501 } else {
3502 /* No handling needed */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003503 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003504 "r %d i %d No handler needed for %s, 0x%02x\n",
3505 r, i, get_cmd_string(pkt->hdr.cmd),
3506 pkt->hdr.cmd);
3507 }
3508
3509 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003510 /* Invoke any callbacks, transfer the skb to caller, and
3511 * fire off the (possibly) blocking iwl3945_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003512 * as we reclaim the driver command queue */
3513 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003514 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003515 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003516 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07003517 }
3518
3519 /* For now we just don't re-use anything. We can tweak this
3520 * later to try and re-use notification packets and SKBs that
3521 * fail to Rx correctly */
3522 if (rxb->skb != NULL) {
3523 priv->alloc_rxb_skb--;
3524 dev_kfree_skb_any(rxb->skb);
3525 rxb->skb = NULL;
3526 }
3527
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003528 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003529 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3530 spin_lock_irqsave(&rxq->lock, flags);
3531 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3532 spin_unlock_irqrestore(&rxq->lock, flags);
3533 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003534 /* If there are a lot of unused frames,
3535 * restock the Rx queue so ucode won't assert. */
3536 if (fill_rx) {
3537 count++;
3538 if (count >= 8) {
3539 priv->rxq.read = i;
3540 __iwl3945_rx_replenish(priv);
3541 count = 0;
3542 }
3543 }
Zhu Yib481de92007-09-25 17:54:57 -07003544 }
3545
3546 /* Backtrack one entry */
3547 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003548 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003549}
3550
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003551/**
3552 * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
3553 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003554static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003555 struct iwl_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003556{
3557 u32 reg = 0;
3558 int rc = 0;
3559 int txq_id = txq->q.id;
3560
3561 if (txq->need_update == 0)
3562 return rc;
3563
3564 /* if we're trying to save power */
3565 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3566 /* wake up nic if it's powered down ...
3567 * uCode will wake up, and interrupt us again, so next
3568 * time we'll skip this part. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003569 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003570
3571 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3572 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003573 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003574 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3575 return rc;
3576 }
3577
3578 /* restore this queue's parameters in nic hardware. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003579 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003580 if (rc)
3581 return rc;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003582 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003583 txq->q.write_ptr | (txq_id << 8));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003584 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003585
3586 /* else not in power-save mode, uCode will never sleep when we're
3587 * trying to tx (during RFKILL, we're not trying to tx). */
3588 } else
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003589 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003590 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003591
3592 txq->need_update = 0;
3593
3594 return rc;
3595}
3596
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003597#ifdef CONFIG_IWL3945_DEBUG
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003598static void iwl3945_print_rx_config_cmd(struct iwl_priv *priv,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003599 struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003600{
3601 IWL_DEBUG_RADIO("RX CONFIG:\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003602 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003603 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3604 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3605 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3606 le32_to_cpu(rxon->filter_flags));
3607 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3608 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3609 rxon->ofdm_basic_rates);
3610 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07003611 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
3612 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003613 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3614}
3615#endif
3616
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003617static void iwl3945_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003618{
3619 IWL_DEBUG_ISR("Enabling interrupts\n");
3620 set_bit(STATUS_INT_ENABLED, &priv->status);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003621 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003622}
3623
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003624
3625/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003626static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003627{
Tomas Winklera96a27f2008-10-23 23:48:56 -07003628 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003629 synchronize_irq(priv->pci_dev->irq);
3630 tasklet_kill(&priv->irq_tasklet);
3631}
3632
3633
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003634static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003635{
3636 clear_bit(STATUS_INT_ENABLED, &priv->status);
3637
3638 /* disable interrupts from uCode/NIC to host */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003639 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003640
3641 /* acknowledge/clear/reset any interrupts still pending
3642 * from uCode or flow handler (Rx/Tx DMA) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003643 iwl_write32(priv, CSR_INT, 0xffffffff);
3644 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003645 IWL_DEBUG_ISR("Disabled interrupts\n");
3646}
3647
3648static const char *desc_lookup(int i)
3649{
3650 switch (i) {
3651 case 1:
3652 return "FAIL";
3653 case 2:
3654 return "BAD_PARAM";
3655 case 3:
3656 return "BAD_CHECKSUM";
3657 case 4:
3658 return "NMI_INTERRUPT";
3659 case 5:
3660 return "SYSASSERT";
3661 case 6:
3662 return "FATAL_ERROR";
3663 }
3664
3665 return "UNKNOWN";
3666}
3667
3668#define ERROR_START_OFFSET (1 * sizeof(u32))
3669#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3670
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003671static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003672{
3673 u32 i;
3674 u32 desc, time, count, base, data1;
3675 u32 blink1, blink2, ilink1, ilink2;
3676 int rc;
3677
3678 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3679
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003680 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003681 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003682 return;
3683 }
3684
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003685 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003686 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003687 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003688 return;
3689 }
3690
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003691 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003692
3693 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003694 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
3695 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
3696 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003697 }
3698
Winkler, Tomas15b16872008-12-19 10:37:33 +08003699 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07003700 "ilink1 nmiPC Line\n");
3701 for (i = ERROR_START_OFFSET;
3702 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
3703 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003704 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07003705 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003706 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003707 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003708 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003709 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003710 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003711 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003712 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003713 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003714 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003715 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003716 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003717
Winkler, Tomas15b16872008-12-19 10:37:33 +08003718 IWL_ERR(priv,
3719 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
3720 desc_lookup(desc), desc, time, blink1, blink2,
3721 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07003722 }
3723
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003724 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003725
3726}
3727
Ben Cahillf58177b2007-11-29 11:09:43 +08003728#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07003729
3730/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003731 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003732 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003733 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003734 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003735static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003736 u32 num_events, u32 mode)
3737{
3738 u32 i;
3739 u32 base; /* SRAM byte address of event log header */
3740 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3741 u32 ptr; /* SRAM byte address of log data */
3742 u32 ev, time, data; /* event log data */
3743
3744 if (num_events == 0)
3745 return;
3746
3747 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3748
3749 if (mode == 0)
3750 event_size = 2 * sizeof(u32);
3751 else
3752 event_size = 3 * sizeof(u32);
3753
3754 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3755
3756 /* "time" is actually "data" for mode 0 (no timestamp).
3757 * place event id # at far right for easier visual parsing. */
3758 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003759 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003760 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003761 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003762 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003763 if (mode == 0) {
3764 /* data, ev */
3765 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
3766 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003767 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003768 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003769 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07003770 }
3771 }
3772}
3773
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003774static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003775{
3776 int rc;
3777 u32 base; /* SRAM byte address of event log header */
3778 u32 capacity; /* event log capacity in # entries */
3779 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3780 u32 num_wraps; /* # times uCode wrapped to top of log */
3781 u32 next_entry; /* index of next entry to be written by uCode */
3782 u32 size; /* # entries that we'll print */
3783
3784 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003785 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003786 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003787 return;
3788 }
3789
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003790 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003791 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003792 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003793 return;
3794 }
3795
3796 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003797 capacity = iwl_read_targ_mem(priv, base);
3798 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3799 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3800 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003801
3802 size = num_wraps ? capacity : next_entry;
3803
3804 /* bail out if nothing in log */
3805 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003806 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003807 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003808 return;
3809 }
3810
Winkler, Tomas15b16872008-12-19 10:37:33 +08003811 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003812 size, num_wraps);
3813
3814 /* if uCode has wrapped back to top of log, start at the oldest entry,
3815 * i.e the next one that uCode would fill. */
3816 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003817 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003818 capacity - next_entry, mode);
3819
3820 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003821 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003822
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003823 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003824}
3825
3826/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003827 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003828 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003829static void iwl3945_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003830{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003831 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07003832 set_bit(STATUS_FW_ERROR, &priv->status);
3833
3834 /* Cancel currently queued command. */
3835 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3836
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003837#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003838 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003839 iwl3945_dump_nic_error_log(priv);
3840 iwl3945_dump_nic_event_log(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003841 iwl3945_print_rx_config_cmd(priv, &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07003842 }
3843#endif
3844
3845 wake_up_interruptible(&priv->wait_command_queue);
3846
3847 /* Keep the restart process from trying to send host
3848 * commands by clearing the INIT status bit */
3849 clear_bit(STATUS_READY, &priv->status);
3850
3851 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3852 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
3853 "Restarting adapter due to uCode error.\n");
3854
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003855 if (iwl3945_is_associated(priv)) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003856 memcpy(&priv->recovery39_rxon, &priv->active39_rxon,
3857 sizeof(priv->recovery39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003858 priv->error_recovering = 1;
3859 }
3860 queue_work(priv->workqueue, &priv->restart);
3861 }
3862}
3863
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003864static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003865{
3866 unsigned long flags;
3867
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003868 memcpy(&priv->staging39_rxon, &priv->recovery39_rxon,
3869 sizeof(priv->staging39_rxon));
3870 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003871 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003872
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003873 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003874
3875 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003876 priv->assoc_id = le16_to_cpu(priv->staging39_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07003877 priv->error_recovering = 0;
3878 spin_unlock_irqrestore(&priv->lock, flags);
3879}
3880
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003881static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003882{
3883 u32 inta, handled = 0;
3884 u32 inta_fh;
3885 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003886#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07003887 u32 inta_mask;
3888#endif
3889
3890 spin_lock_irqsave(&priv->lock, flags);
3891
3892 /* Ack/clear/reset pending uCode interrupts.
3893 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
3894 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003895 inta = iwl_read32(priv, CSR_INT);
3896 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07003897
3898 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
3899 * Any new interrupts that happen after this, either while we're
3900 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003901 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
3902 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07003903
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003904#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003905 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003906 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003907 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003908 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
3909 inta, inta_mask, inta_fh);
3910 }
3911#endif
3912
3913 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
3914 * atomic, make sure that inta covers all the interrupts that
3915 * we've discovered, even if FH interrupt came in just after
3916 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003917 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003918 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08003919 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07003920 inta |= CSR_INT_BIT_FH_TX;
3921
3922 /* Now service all interrupt bits discovered above. */
3923 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003924 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003925
3926 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003927 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003928
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003929 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003930
3931 handled |= CSR_INT_BIT_HW_ERR;
3932
3933 spin_unlock_irqrestore(&priv->lock, flags);
3934
3935 return;
3936 }
3937
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003938#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003939 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07003940 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003941 if (inta & CSR_INT_BIT_SCD)
3942 IWL_DEBUG_ISR("Scheduler finished to transmit "
3943 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003944
3945 /* Alive notification via Rx interrupt will do the real work */
3946 if (inta & CSR_INT_BIT_ALIVE)
3947 IWL_DEBUG_ISR("Alive interrupt\n");
3948 }
3949#endif
3950 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08003951 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07003952
Zhu Yib481de92007-09-25 17:54:57 -07003953 /* Error detected by uCode */
3954 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003955 IWL_ERR(priv, "Microcode SW error detected. "
3956 "Restarting 0x%X.\n", inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003957 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003958 handled |= CSR_INT_BIT_SW_ERR;
3959 }
3960
3961 /* uCode wakes up after power-down sleep */
3962 if (inta & CSR_INT_BIT_WAKEUP) {
3963 IWL_DEBUG_ISR("Wakeup interrupt\n");
Winkler, Tomas141c43a2009-01-08 10:19:53 -08003964 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08003965 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[0]);
3966 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[1]);
3967 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[2]);
3968 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[3]);
3969 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[4]);
3970 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07003971
3972 handled |= CSR_INT_BIT_WAKEUP;
3973 }
3974
3975 /* All uCode command responses, including Tx command responses,
3976 * Rx "responses" (frame-received notification), and other
3977 * notifications from uCode come through here*/
3978 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003979 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003980 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
3981 }
3982
3983 if (inta & CSR_INT_BIT_FH_TX) {
3984 IWL_DEBUG_ISR("Tx interrupt\n");
3985
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003986 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
3987 if (!iwl_grab_nic_access(priv)) {
3988 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08003989 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003990 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003991 }
3992 handled |= CSR_INT_BIT_FH_TX;
3993 }
3994
3995 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08003996 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07003997
3998 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003999 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07004000 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004001 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004002 }
4003
4004 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004005 /* only Re-enable if disabled by irq */
4006 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4007 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004008
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004009#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004010 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004011 inta = iwl_read32(priv, CSR_INT);
4012 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4013 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004014 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4015 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4016 }
4017#endif
4018 spin_unlock_irqrestore(&priv->lock, flags);
4019}
4020
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004021static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004022{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004023 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004024 u32 inta, inta_mask;
4025 u32 inta_fh;
4026 if (!priv)
4027 return IRQ_NONE;
4028
4029 spin_lock(&priv->lock);
4030
4031 /* Disable (but don't clear!) interrupts here to avoid
4032 * back-to-back ISRs and sporadic interrupts from our NIC.
4033 * If we have something to service, the tasklet will re-enable ints.
4034 * If we *don't* have something, we'll re-enable before leaving here. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004035 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4036 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004037
4038 /* Discover which interrupts are active/pending */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004039 inta = iwl_read32(priv, CSR_INT);
4040 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004041
4042 /* Ignore interrupt if there's nothing in NIC to service.
4043 * This may be due to IRQ shared with another device,
4044 * or due to sporadic interrupts thrown from our NIC. */
4045 if (!inta && !inta_fh) {
4046 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4047 goto none;
4048 }
4049
4050 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4051 /* Hardware disappeared */
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004052 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004053 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004054 }
4055
4056 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4057 inta, inta_mask, inta_fh);
4058
Joonwoo Park25c03d82008-01-23 10:15:20 -08004059 inta &= ~CSR_INT_BIT_SCD;
4060
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004061 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004062 if (likely(inta || inta_fh))
4063 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004064unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07004065 spin_unlock(&priv->lock);
4066
4067 return IRQ_HANDLED;
4068
4069 none:
4070 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004071 /* only Re-enable if disabled by irq */
4072 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4073 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004074 spin_unlock(&priv->lock);
4075 return IRQ_NONE;
4076}
4077
4078/************************** EEPROM BANDS ****************************
4079 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004080 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07004081 * EEPROM contents to the specific channel number supported for each
4082 * band.
4083 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004084 * For example, iwl3945_priv->eeprom39.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07004085 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4086 * The specific geography and calibration information for that channel
4087 * is contained in the eeprom map itself.
4088 *
4089 * During init, we copy the eeprom information and channel map
4090 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4091 *
4092 * channel_map_24/52 provides the index in the channel_info array for a
4093 * given channel. We have to have two separate maps as there is channel
4094 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4095 * band_2
4096 *
4097 * A value of 0xff stored in the channel_map indicates that the channel
4098 * is not supported by the hardware at all.
4099 *
4100 * A value of 0xfe in the channel_map indicates that the channel is not
4101 * valid for Tx with the current hardware. This means that
4102 * while the system can tune and receive on a given channel, it may not
4103 * be able to associate or transmit any frames on that
4104 * channel. There is no corresponding channel information for that
4105 * entry.
4106 *
4107 *********************************************************************/
4108
4109/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004110static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07004111 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4112};
4113
4114/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08004115static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004116 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4117};
4118
Ben Cahill9fbab512007-11-29 11:09:47 +08004119static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004120 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4121};
4122
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004123static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004124 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4125};
4126
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004127static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004128 145, 149, 153, 157, 161, 165
4129};
4130
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004131static void iwl3945_init_band_reference(const struct iwl_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07004132 int *eeprom_ch_count,
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004133 const struct iwl_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07004134 **eeprom_ch_info,
4135 const u8 **eeprom_ch_index)
4136{
4137 switch (band) {
4138 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004139 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004140 *eeprom_ch_info = priv->eeprom39.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004141 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07004142 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004143 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004144 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004145 *eeprom_ch_info = priv->eeprom39.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004146 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07004147 break;
4148 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004149 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004150 *eeprom_ch_info = priv->eeprom39.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004151 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07004152 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004153 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004154 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004155 *eeprom_ch_info = priv->eeprom39.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004156 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07004157 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004158 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004159 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004160 *eeprom_ch_info = priv->eeprom39.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004161 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07004162 break;
4163 default:
4164 BUG();
4165 return;
4166 }
4167}
4168
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004169/**
4170 * iwl3945_get_channel_info - Find driver's private channel info
4171 *
4172 * Based on band and channel number.
4173 */
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004174const struct iwl_channel_info *
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004175iwl3945_get_channel_info(const struct iwl_priv *priv,
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004176 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07004177{
4178 int i;
4179
Johannes Berg8318d782008-01-24 19:38:38 +01004180 switch (band) {
4181 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004182 for (i = 14; i < priv->channel_count; i++) {
4183 if (priv->channel_info[i].channel == channel)
4184 return &priv->channel_info[i];
4185 }
4186 break;
4187
Johannes Berg8318d782008-01-24 19:38:38 +01004188 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004189 if (channel >= 1 && channel <= 14)
4190 return &priv->channel_info[channel - 1];
4191 break;
Johannes Berg8318d782008-01-24 19:38:38 +01004192 case IEEE80211_NUM_BANDS:
4193 WARN_ON(1);
Zhu Yib481de92007-09-25 17:54:57 -07004194 }
4195
4196 return NULL;
4197}
4198
4199#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4200 ? # x " " : "")
4201
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004202/**
4203 * iwl3945_init_channel_map - Set up driver's info for all possible channels
4204 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004205static int iwl3945_init_channel_map(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004206{
4207 int eeprom_ch_count = 0;
4208 const u8 *eeprom_ch_index = NULL;
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004209 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004210 int band, ch;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004211 struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004212
4213 if (priv->channel_count) {
4214 IWL_DEBUG_INFO("Channel map already initialized.\n");
4215 return 0;
4216 }
4217
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004218 if (priv->eeprom39.version < 0x2f) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004219 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004220 priv->eeprom39.version);
Zhu Yib481de92007-09-25 17:54:57 -07004221 return -EINVAL;
4222 }
4223
4224 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
4225
4226 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004227 ARRAY_SIZE(iwl3945_eeprom_band_1) +
4228 ARRAY_SIZE(iwl3945_eeprom_band_2) +
4229 ARRAY_SIZE(iwl3945_eeprom_band_3) +
4230 ARRAY_SIZE(iwl3945_eeprom_band_4) +
4231 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004232
4233 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
4234
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004235 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07004236 priv->channel_count, GFP_KERNEL);
4237 if (!priv->channel_info) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004238 IWL_ERR(priv, "Could not allocate channel_info\n");
Zhu Yib481de92007-09-25 17:54:57 -07004239 priv->channel_count = 0;
4240 return -ENOMEM;
4241 }
4242
4243 ch_info = priv->channel_info;
4244
4245 /* Loop through the 5 EEPROM bands adding them in order to the
4246 * channel map we maintain (that contains additional information than
4247 * what just in the EEPROM) */
4248 for (band = 1; band <= 5; band++) {
4249
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004250 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07004251 &eeprom_ch_info, &eeprom_ch_index);
4252
4253 /* Loop through each band adding each of the channels */
4254 for (ch = 0; ch < eeprom_ch_count; ch++) {
4255 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01004256 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
4257 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004258
4259 /* permanently store EEPROM's channel regulatory flags
4260 * and max power in channel info database. */
4261 ch_info->eeprom = eeprom_ch_info[ch];
4262
4263 /* Copy the run-time flags so they are there even on
4264 * invalid channels */
4265 ch_info->flags = eeprom_ch_info[ch].flags;
4266
4267 if (!(is_channel_valid(ch_info))) {
4268 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
4269 "No traffic\n",
4270 ch_info->channel,
4271 ch_info->flags,
4272 is_channel_a_band(ch_info) ?
4273 "5.2" : "2.4");
4274 ch_info++;
4275 continue;
4276 }
4277
4278 /* Initialize regulatory-based run-time data */
4279 ch_info->max_power_avg = ch_info->curr_txpow =
4280 eeprom_ch_info[ch].max_power_avg;
4281 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
4282 ch_info->min_power = 0;
4283
Guy Cohenfe7c4042008-04-21 15:41:56 -07004284 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07004285 " %ddBm): Ad-Hoc %ssupported\n",
4286 ch_info->channel,
4287 is_channel_a_band(ch_info) ?
4288 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02004289 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07004290 CHECK_AND_PRINT(IBSS),
4291 CHECK_AND_PRINT(ACTIVE),
4292 CHECK_AND_PRINT(RADAR),
4293 CHECK_AND_PRINT(WIDE),
Zhu Yib481de92007-09-25 17:54:57 -07004294 CHECK_AND_PRINT(DFS),
4295 eeprom_ch_info[ch].flags,
4296 eeprom_ch_info[ch].max_power_avg,
4297 ((eeprom_ch_info[ch].
4298 flags & EEPROM_CHANNEL_IBSS)
4299 && !(eeprom_ch_info[ch].
4300 flags & EEPROM_CHANNEL_RADAR))
4301 ? "" : "not ");
4302
4303 /* Set the user_txpower_limit to the highest power
4304 * supported by any channel */
4305 if (eeprom_ch_info[ch].max_power_avg >
4306 priv->user_txpower_limit)
4307 priv->user_txpower_limit =
4308 eeprom_ch_info[ch].max_power_avg;
4309
4310 ch_info++;
4311 }
4312 }
4313
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004314 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07004315 if (iwl3945_txpower_set_from_eeprom(priv))
4316 return -EIO;
4317
4318 return 0;
4319}
4320
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004321/*
4322 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
4323 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004324static void iwl3945_free_channel_map(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004325{
4326 kfree(priv->channel_info);
4327 priv->channel_count = 0;
4328}
4329
Zhu Yib481de92007-09-25 17:54:57 -07004330/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4331 * sending probe req. This should be set long enough to hear probe responses
4332 * from more than one AP. */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004333#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
4334#define IWL_ACTIVE_DWELL_TIME_52 (20)
4335
4336#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
4337#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Zhu Yib481de92007-09-25 17:54:57 -07004338
4339/* For faster active scanning, scan will move to the next channel if fewer than
4340 * PLCP_QUIET_THRESH packets are heard on this channel within
4341 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4342 * time if it's a quiet channel (nothing responded to our probe, and there's
4343 * no other traffic).
4344 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4345#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004346#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
Zhu Yib481de92007-09-25 17:54:57 -07004347
4348/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4349 * Must be set longer than active dwell time.
4350 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4351#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4352#define IWL_PASSIVE_DWELL_TIME_52 (10)
4353#define IWL_PASSIVE_DWELL_BASE (100)
4354#define IWL_CHANNEL_TUNE_TIME 5
4355
Kolekar, Abhijeete720ce92008-11-07 09:58:42 -08004356#define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004357
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004358static inline u16 iwl3945_get_active_dwell_time(struct iwl_priv *priv,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004359 enum ieee80211_band band,
4360 u8 n_probes)
Zhu Yib481de92007-09-25 17:54:57 -07004361{
Johannes Berg8318d782008-01-24 19:38:38 +01004362 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004363 return IWL_ACTIVE_DWELL_TIME_52 +
4364 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004365 else
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004366 return IWL_ACTIVE_DWELL_TIME_24 +
4367 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004368}
4369
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004370static u16 iwl3945_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004371 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004372{
Johannes Berg8318d782008-01-24 19:38:38 +01004373 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004374 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4375 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4376
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004377 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004378 /* If we're associated, we clamp the maximum passive
4379 * dwell time to be 98% of the beacon interval (minus
4380 * 2 * channel tune time) */
4381 passive = priv->beacon_int;
4382 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4383 passive = IWL_PASSIVE_DWELL_BASE;
4384 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4385 }
4386
Zhu Yib481de92007-09-25 17:54:57 -07004387 return passive;
4388}
4389
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004390static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004391 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004392 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004393 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004394{
4395 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004396 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004397 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004398 u16 passive_dwell = 0;
4399 u16 active_dwell = 0;
4400 int added, i;
4401
Kolekar, Abhijeetcbba18c2008-12-19 10:37:42 +08004402 sband = iwl_get_hw_mode(priv, band);
Johannes Berg8318d782008-01-24 19:38:38 +01004403 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004404 return 0;
4405
Johannes Berg8318d782008-01-24 19:38:38 +01004406 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004407
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004408 active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
Johannes Berg8318d782008-01-24 19:38:38 +01004409 passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004410
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08004411 if (passive_dwell <= active_dwell)
4412 passive_dwell = active_dwell + 1;
4413
Johannes Berg8318d782008-01-24 19:38:38 +01004414 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004415 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4416 continue;
4417
Johannes Berg8318d782008-01-24 19:38:38 +01004418 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07004419
Johannes Berg8318d782008-01-24 19:38:38 +01004420 ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004421 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004422 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004423 scan_ch->channel);
4424 continue;
4425 }
4426
Zhu Yib481de92007-09-25 17:54:57 -07004427 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4428 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08004429 /* If passive , set up for auto-switch
4430 * and use long active_dwell time.
4431 */
4432 if (!is_active || is_channel_passive(ch_info) ||
4433 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
4434 scan_ch->type = 0; /* passive */
4435 if (IWL_UCODE_API(priv->ucode_ver) == 1)
4436 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
4437 } else {
4438 scan_ch->type = 1; /* active */
4439 }
4440
4441 /* Set direct probe bits. These may be used both for active
4442 * scan channels (probes gets sent right away),
4443 * or for passive channels (probes get se sent only after
4444 * hearing clear Rx packet).*/
4445 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
4446 if (n_probes)
4447 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4448 } else {
4449 /* uCode v1 does not allow setting direct probe bits on
4450 * passive channel. */
4451 if ((scan_ch->type & 1) && n_probes)
4452 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4453 }
Zhu Yib481de92007-09-25 17:54:57 -07004454
Ben Cahill9fbab512007-11-29 11:09:47 +08004455 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004456 scan_ch->tpc.dsp_atten = 110;
4457 /* scan_pwr_info->tpc.dsp_atten; */
4458
4459 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004460 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004461 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4462 else {
4463 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4464 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004465 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004466 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004467 */
4468 }
4469
4470 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4471 scan_ch->channel,
4472 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4473 (scan_ch->type & 1) ?
4474 active_dwell : passive_dwell);
4475
4476 scan_ch++;
4477 added++;
4478 }
4479
4480 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4481 return added;
4482}
4483
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004484static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004485 struct ieee80211_rate *rates)
4486{
4487 int i;
4488
4489 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004490 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
4491 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4492 rates[i].hw_value_short = i;
4493 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08004494 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004495 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004496 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004497 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004498 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01004499 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004500 }
Zhu Yib481de92007-09-25 17:54:57 -07004501 }
4502}
4503
4504/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004505 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004506 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004507static int iwl3945_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004508{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004509 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004510 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004511 struct ieee80211_channel *channels;
4512 struct ieee80211_channel *geo_ch;
4513 struct ieee80211_rate *rates;
4514 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004515
Johannes Berg8318d782008-01-24 19:38:38 +01004516 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4517 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004518 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4519 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4520 return 0;
4521 }
4522
Zhu Yib481de92007-09-25 17:54:57 -07004523 channels = kzalloc(sizeof(struct ieee80211_channel) *
4524 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004525 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004526 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004527
Tomas Winkler8211ef72008-03-02 01:36:04 +02004528 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004529 GFP_KERNEL);
4530 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004531 kfree(channels);
4532 return -ENOMEM;
4533 }
4534
Zhu Yib481de92007-09-25 17:54:57 -07004535 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004536 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4537 sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
4538 /* just OFDM */
4539 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4540 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07004541
Tomas Winkler8211ef72008-03-02 01:36:04 +02004542 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4543 sband->channels = channels;
4544 /* OFDM & CCK */
4545 sband->bitrates = rates;
4546 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004547
4548 priv->ieee_channels = channels;
4549 priv->ieee_rates = rates;
4550
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004551 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004552
Tomas Winkler8211ef72008-03-02 01:36:04 +02004553 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004554 ch = &priv->channel_info[i];
4555
Tomas Winkler8211ef72008-03-02 01:36:04 +02004556 /* FIXME: might be removed if scan is OK*/
4557 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004558 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004559
4560 if (is_channel_a_band(ch))
Tomas Winkler8211ef72008-03-02 01:36:04 +02004561 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Johannes Berg8318d782008-01-24 19:38:38 +01004562 else
Tomas Winkler8211ef72008-03-02 01:36:04 +02004563 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004564
Tomas Winkler8211ef72008-03-02 01:36:04 +02004565 geo_ch = &sband->channels[sband->n_channels++];
4566
4567 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004568 geo_ch->max_power = ch->max_power_avg;
4569 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004570 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004571
4572 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004573 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4574 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004575
Johannes Berg8318d782008-01-24 19:38:38 +01004576 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4577 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004578
4579 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004580 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004581
4582 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4583 priv->max_channel_txpower_limit =
4584 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004585 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004586 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004587 }
4588
4589 /* Save flags for reg domain usage */
4590 geo_ch->orig_flags = geo_ch->flags;
4591
4592 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4593 ch->channel, geo_ch->center_freq,
4594 is_channel_a_band(ch) ? "5.2" : "2.4",
4595 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4596 "restricted" : "valid",
4597 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004598 }
4599
Tomas Winkler82b9a122008-03-04 18:09:30 -08004600 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4601 priv->cfg->sku & IWL_SKU_A) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004602 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
4603 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
4604 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004605 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004606 }
4607
Tomas Winkler978785a2008-12-19 10:37:31 +08004608 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004609 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4610 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004611
John W. Linvillee0e0a672008-03-25 15:58:40 -04004612 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4613 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4614 &priv->bands[IEEE80211_BAND_2GHZ];
4615 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4616 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4617 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004618
Zhu Yib481de92007-09-25 17:54:57 -07004619 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4620
4621 return 0;
4622}
4623
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004624/*
4625 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
4626 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004627static void iwl3945_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004628{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004629 kfree(priv->ieee_channels);
4630 kfree(priv->ieee_rates);
4631 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4632}
4633
Zhu Yib481de92007-09-25 17:54:57 -07004634/******************************************************************************
4635 *
4636 * uCode download functions
4637 *
4638 ******************************************************************************/
4639
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004640static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004641{
Tomas Winkler98c92212008-01-14 17:46:20 -08004642 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4643 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4644 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4645 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4646 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4647 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004648}
4649
4650/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004651 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004652 * looking at all data.
4653 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004654static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004655{
4656 u32 val;
4657 u32 save_len = len;
4658 int rc = 0;
4659 u32 errcnt;
4660
4661 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4662
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004663 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004664 if (rc)
4665 return rc;
4666
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004667 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004668 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004669
4670 errcnt = 0;
4671 for (; len > 0; len -= sizeof(u32), image++) {
4672 /* read data comes through single port, auto-incr addr */
4673 /* NOTE: Use the debugless read so we don't flood kernel log
4674 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004675 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004676 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004677 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004678 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4679 save_len - len, val, le32_to_cpu(*image));
4680 rc = -EIO;
4681 errcnt++;
4682 if (errcnt >= 20)
4683 break;
4684 }
4685 }
4686
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004687 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004688
4689 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08004690 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07004691
4692 return rc;
4693}
4694
4695
4696/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004697 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004698 * using sample data 100 bytes apart. If these sample points are good,
4699 * it's a pretty good bet that everything between them is good, too.
4700 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004701static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004702{
4703 u32 val;
4704 int rc = 0;
4705 u32 errcnt = 0;
4706 u32 i;
4707
4708 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4709
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004710 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004711 if (rc)
4712 return rc;
4713
4714 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4715 /* read data comes through single port, auto-incr addr */
4716 /* NOTE: Use the debugless read so we don't flood kernel log
4717 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004718 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004719 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004720 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004721 if (val != le32_to_cpu(*image)) {
4722#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08004723 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004724 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4725 i, val, *image);
4726#endif
4727 rc = -EIO;
4728 errcnt++;
4729 if (errcnt >= 3)
4730 break;
4731 }
4732 }
4733
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004734 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004735
4736 return rc;
4737}
4738
4739
4740/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004741 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07004742 * and verify its contents
4743 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004744static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004745{
4746 __le32 *image;
4747 u32 len;
4748 int rc = 0;
4749
4750 /* Try bootstrap */
4751 image = (__le32 *)priv->ucode_boot.v_addr;
4752 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004753 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004754 if (rc == 0) {
4755 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
4756 return 0;
4757 }
4758
4759 /* Try initialize */
4760 image = (__le32 *)priv->ucode_init.v_addr;
4761 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004762 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004763 if (rc == 0) {
4764 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
4765 return 0;
4766 }
4767
4768 /* Try runtime/protocol */
4769 image = (__le32 *)priv->ucode_code.v_addr;
4770 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004771 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004772 if (rc == 0) {
4773 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
4774 return 0;
4775 }
4776
Winkler, Tomas15b16872008-12-19 10:37:33 +08004777 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07004778
Ben Cahill9fbab512007-11-29 11:09:47 +08004779 /* Since nothing seems to match, show first several data entries in
4780 * instruction SRAM, so maybe visual inspection will give a clue.
4781 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07004782 image = (__le32 *)priv->ucode_boot.v_addr;
4783 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004784 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004785
4786 return rc;
4787}
4788
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004789static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004790{
4791 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004792 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004793}
4794
4795/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004796 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07004797 *
4798 * Copy into buffers for card to fetch via bus-mastering
4799 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004800static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004801{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08004802 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08004803 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07004804 const struct firmware *ucode_raw;
4805 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08004806 const char *name_pre = priv->cfg->fw_name_pre;
4807 const unsigned int api_max = priv->cfg->ucode_api_max;
4808 const unsigned int api_min = priv->cfg->ucode_api_min;
4809 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07004810 u8 *src;
4811 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08004812 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07004813
4814 /* Ask kernel firmware_class module to get the boot firmware off disk.
4815 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08004816 for (index = api_max; index >= api_min; index--) {
4817 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
4818 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
4819 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004820 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08004821 buf, ret);
4822 if (ret == -ENOENT)
4823 continue;
4824 else
4825 goto error;
4826 } else {
4827 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004828 IWL_ERR(priv, "Loaded firmware %s, "
4829 "which is deprecated. "
4830 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08004831 buf, api_max);
4832 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
4833 buf, ucode_raw->size);
4834 break;
4835 }
Zhu Yib481de92007-09-25 17:54:57 -07004836 }
4837
Reinette Chatrea0987a82008-12-02 12:14:06 -08004838 if (ret < 0)
4839 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07004840
4841 /* Make sure that we got at least our header! */
4842 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004843 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08004844 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004845 goto err_release;
4846 }
4847
4848 /* Data from ucode file: header followed by uCode images */
4849 ucode = (void *)ucode_raw->data;
4850
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08004851 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08004852 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07004853 inst_size = le32_to_cpu(ucode->inst_size);
4854 data_size = le32_to_cpu(ucode->data_size);
4855 init_size = le32_to_cpu(ucode->init_size);
4856 init_data_size = le32_to_cpu(ucode->init_data_size);
4857 boot_size = le32_to_cpu(ucode->boot_size);
4858
Reinette Chatrea0987a82008-12-02 12:14:06 -08004859 /* api_ver should match the api version forming part of the
4860 * firmware filename ... but we don't check for that and only rely
4861 * on the API version read from firware header from here on forward */
4862
4863 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004864 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08004865 "Driver supports v%u, firmware is v%u.\n",
4866 api_max, api_ver);
4867 priv->ucode_ver = 0;
4868 ret = -EINVAL;
4869 goto err_release;
4870 }
4871 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004872 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08004873 "got %u. New firmware can be obtained "
4874 "from http://www.intellinuxwireless.org.\n",
4875 api_max, api_ver);
4876
Tomas Winkler978785a2008-12-19 10:37:31 +08004877 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
4878 IWL_UCODE_MAJOR(priv->ucode_ver),
4879 IWL_UCODE_MINOR(priv->ucode_ver),
4880 IWL_UCODE_API(priv->ucode_ver),
4881 IWL_UCODE_SERIAL(priv->ucode_ver));
4882
Reinette Chatrea0987a82008-12-02 12:14:06 -08004883 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
4884 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08004885 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
4886 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
4887 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
4888 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
4889 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07004890
Reinette Chatrea0987a82008-12-02 12:14:06 -08004891
Zhu Yib481de92007-09-25 17:54:57 -07004892 /* Verify size of file vs. image size info in file's header */
4893 if (ucode_raw->size < sizeof(*ucode) +
4894 inst_size + data_size + init_size +
4895 init_data_size + boot_size) {
4896
4897 IWL_DEBUG_INFO("uCode file size %d too small\n",
4898 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004899 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004900 goto err_release;
4901 }
4902
4903 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004904 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004905 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
4906 inst_size);
4907 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004908 goto err_release;
4909 }
4910
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004911 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004912 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
4913 data_size);
4914 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004915 goto err_release;
4916 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004917 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004918 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
4919 init_size);
4920 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004921 goto err_release;
4922 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004923 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004924 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
4925 init_data_size);
4926 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004927 goto err_release;
4928 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004929 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08004930 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
4931 boot_size);
4932 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07004933 goto err_release;
4934 }
4935
4936 /* Allocate ucode buffers for card's bus-master loading ... */
4937
4938 /* Runtime instructions and 2 copies of data:
4939 * 1) unmodified from disk
4940 * 2) backup cache for save/restore during power-downs */
4941 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004942 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07004943
4944 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004945 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07004946
4947 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004948 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07004949
4950 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08004951 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07004952 goto err_pci_alloc;
4953
Tomas Winkler90e759d2007-11-29 11:09:41 +08004954 /* Initialization instructions and data */
4955 if (init_size && init_data_size) {
4956 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004957 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004958
4959 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004960 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004961
4962 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
4963 goto err_pci_alloc;
4964 }
4965
4966 /* Bootstrap (instructions only, no data) */
4967 if (boot_size) {
4968 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08004969 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08004970
4971 if (!priv->ucode_boot.v_addr)
4972 goto err_pci_alloc;
4973 }
4974
Zhu Yib481de92007-09-25 17:54:57 -07004975 /* Copy images into buffers for card's bus-master reads ... */
4976
4977 /* Runtime instructions (first block of data in file) */
4978 src = &ucode->data[0];
4979 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004980 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004981 memcpy(priv->ucode_code.v_addr, src, len);
4982 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
4983 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
4984
4985 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004986 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07004987 src = &ucode->data[inst_size];
4988 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004989 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07004990 memcpy(priv->ucode_data.v_addr, src, len);
4991 memcpy(priv->ucode_data_backup.v_addr, src, len);
4992
4993 /* Initialization instructions (3rd block) */
4994 if (init_size) {
4995 src = &ucode->data[inst_size + data_size];
4996 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08004997 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
4998 len);
Zhu Yib481de92007-09-25 17:54:57 -07004999 memcpy(priv->ucode_init.v_addr, src, len);
5000 }
5001
5002 /* Initialization data (4th block) */
5003 if (init_data_size) {
5004 src = &ucode->data[inst_size + data_size + init_size];
5005 len = priv->ucode_init_data.len;
5006 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
5007 (int)len);
5008 memcpy(priv->ucode_init_data.v_addr, src, len);
5009 }
5010
5011 /* Bootstrap instructions (5th block) */
5012 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5013 len = priv->ucode_boot.len;
5014 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
5015 (int)len);
5016 memcpy(priv->ucode_boot.v_addr, src, len);
5017
5018 /* We have our copies now, allow OS release its copies */
5019 release_firmware(ucode_raw);
5020 return 0;
5021
5022 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005023 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005024 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005025 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005026
5027 err_release:
5028 release_firmware(ucode_raw);
5029
5030 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005031 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005032}
5033
5034
5035/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005036 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005037 *
5038 * Tell initialization uCode where to find runtime uCode.
5039 *
5040 * BSM registers initially contain pointers to initialization uCode.
5041 * We need to replace them to load runtime uCode inst and data,
5042 * and to save runtime data when powering down.
5043 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005044static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005045{
5046 dma_addr_t pinst;
5047 dma_addr_t pdata;
5048 int rc = 0;
5049 unsigned long flags;
5050
5051 /* bits 31:0 for 3945 */
5052 pinst = priv->ucode_code.p_addr;
5053 pdata = priv->ucode_data_backup.p_addr;
5054
5055 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005056 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005057 if (rc) {
5058 spin_unlock_irqrestore(&priv->lock, flags);
5059 return rc;
5060 }
5061
5062 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005063 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5064 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5065 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005066 priv->ucode_data.len);
5067
Tomas Winklera96a27f2008-10-23 23:48:56 -07005068 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07005069 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005070 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005071 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5072
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005073 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005074
5075 spin_unlock_irqrestore(&priv->lock, flags);
5076
5077 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5078
5079 return rc;
5080}
5081
5082/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005083 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005084 *
5085 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5086 *
Zhu Yib481de92007-09-25 17:54:57 -07005087 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08005088 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005089static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005090{
5091 /* Check alive response for "valid" sign from uCode */
5092 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5093 /* We had an error bringing up the hardware, so take it
5094 * all the way back down so we can try again */
5095 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5096 goto restart;
5097 }
5098
5099 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5100 * This is a paranoid check, because we would not have gotten the
5101 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005102 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005103 /* Runtime instruction load was bad;
5104 * take it all the way back down so we can try again */
5105 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5106 goto restart;
5107 }
5108
5109 /* Send pointers to protocol/runtime uCode image ... init code will
5110 * load and launch runtime uCode, which will send us another "Alive"
5111 * notification. */
5112 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005113 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005114 /* Runtime instruction load won't happen;
5115 * take it all the way back down so we can try again */
5116 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5117 goto restart;
5118 }
5119 return;
5120
5121 restart:
5122 queue_work(priv->workqueue, &priv->restart);
5123}
5124
5125
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005126/* temporary */
5127static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
5128 struct sk_buff *skb);
5129
Zhu Yib481de92007-09-25 17:54:57 -07005130/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005131 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005132 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005133 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005134 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005135static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005136{
5137 int rc = 0;
5138 int thermal_spin = 0;
5139 u32 rfkill;
5140
5141 IWL_DEBUG_INFO("Runtime Alive received.\n");
5142
5143 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5144 /* We had an error bringing up the hardware, so take it
5145 * all the way back down so we can try again */
5146 IWL_DEBUG_INFO("Alive failed.\n");
5147 goto restart;
5148 }
5149
5150 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5151 * This is a paranoid check, because we would not have gotten the
5152 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005153 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005154 /* Runtime instruction load was bad;
5155 * take it all the way back down so we can try again */
5156 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5157 goto restart;
5158 }
5159
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005160 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005161
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005162 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005163 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005164 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07005165 return;
5166 }
5167
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005168 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005169 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005170 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005171
5172 if (rfkill & 0x1) {
5173 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07005174 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07005175 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005176 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07005177 thermal_spin++;
5178 udelay(10);
5179 }
5180
5181 if (thermal_spin)
5182 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
5183 thermal_spin * 10);
5184 } else
5185 set_bit(STATUS_RF_KILL_HW, &priv->status);
5186
Ben Cahill9fbab512007-11-29 11:09:47 +08005187 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005188 set_bit(STATUS_ALIVE, &priv->status);
5189
5190 /* Clear out the uCode error bit if it is set */
5191 clear_bit(STATUS_FW_ERROR, &priv->status);
5192
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005193 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005194 return;
5195
Johannes Berg36d68252008-05-15 12:55:26 +02005196 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005197
5198 priv->active_rate = priv->rates_mask;
5199 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5200
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005201 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005202
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005203 if (iwl3945_is_associated(priv)) {
5204 struct iwl3945_rxon_cmd *active_rxon =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005205 (struct iwl3945_rxon_cmd *)(&priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005206
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005207 memcpy(&priv->staging39_rxon, &priv->active39_rxon,
5208 sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07005209 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5210 } else {
5211 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07005212 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005213 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07005214 }
5215
Ben Cahill9fbab512007-11-29 11:09:47 +08005216 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005217 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005218
5219 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005220 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005221
Zhu Yib481de92007-09-25 17:54:57 -07005222 iwl3945_reg_txpower_periodic(priv);
5223
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005224 iwl3945_led_register(priv);
5225
Zhu Yib481de92007-09-25 17:54:57 -07005226 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005227 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08005228 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005229
5230 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005231 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005232
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005233 /* reassociate for ADHOC mode */
5234 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
5235 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
5236 priv->vif);
5237 if (beacon)
5238 iwl3945_mac_beacon_update(priv->hw, beacon);
5239 }
5240
Zhu Yib481de92007-09-25 17:54:57 -07005241 return;
5242
5243 restart:
5244 queue_work(priv->workqueue, &priv->restart);
5245}
5246
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005247static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005248
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005249static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005250{
5251 unsigned long flags;
5252 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5253 struct ieee80211_conf *conf = NULL;
5254
5255 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5256
5257 conf = ieee80211_get_hw_conf(priv->hw);
5258
5259 if (!exit_pending)
5260 set_bit(STATUS_EXIT_PENDING, &priv->status);
5261
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005262 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005263 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005264
5265 /* Unblock any waiting calls */
5266 wake_up_interruptible_all(&priv->wait_command_queue);
5267
Zhu Yib481de92007-09-25 17:54:57 -07005268 /* Wipe out the EXIT_PENDING status bit if we are not actually
5269 * exiting the module */
5270 if (!exit_pending)
5271 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5272
5273 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005274 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005275
5276 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005277 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005278 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005279 spin_unlock_irqrestore(&priv->lock, flags);
5280 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005281
5282 if (priv->mac80211_registered)
5283 ieee80211_stop_queues(priv->hw);
5284
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005285 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005286 * clear all bits but the RF Kill and SUSPEND bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005287 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005288 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5289 STATUS_RF_KILL_HW |
5290 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5291 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005292 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5293 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005294 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005295 STATUS_IN_SUSPEND |
5296 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5297 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005298 goto exit;
5299 }
5300
5301 /* ...otherwise clear out all the status bits but the RF Kill and
5302 * SUSPEND bits and continue taking the NIC down. */
5303 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5304 STATUS_RF_KILL_HW |
5305 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5306 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005307 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5308 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005309 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5310 STATUS_IN_SUSPEND |
5311 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005312 STATUS_FW_ERROR |
5313 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5314 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005315
5316 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005317 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005318 spin_unlock_irqrestore(&priv->lock, flags);
5319
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005320 iwl3945_hw_txq_ctx_stop(priv);
5321 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005322
5323 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005324 if (!iwl_grab_nic_access(priv)) {
5325 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005326 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005327 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005328 }
5329 spin_unlock_irqrestore(&priv->lock, flags);
5330
5331 udelay(5);
5332
Kolekar, Abhijeet01ec6162008-12-19 10:37:38 +08005333 priv->cfg->ops->lib->apm_ops.reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005334 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08005335 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005336
5337 if (priv->ibss_beacon)
5338 dev_kfree_skb(priv->ibss_beacon);
5339 priv->ibss_beacon = NULL;
5340
5341 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005342 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005343}
5344
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005345static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005346{
5347 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005348 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005349 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005350
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005351 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005352}
5353
5354#define MAX_HW_RESTARTS 5
5355
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005356static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005357{
5358 int rc, i;
5359
5360 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005361 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07005362 return -EIO;
5363 }
5364
5365 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005366 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07005367 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005368 return -ENODEV;
5369 }
5370
Reinette Chatree903fbd2008-01-30 22:05:15 -08005371 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005372 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08005373 return -EIO;
5374 }
5375
Zhu Yie655b9f2008-01-24 02:19:38 -08005376 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005377 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005378 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5379 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5380 else {
5381 set_bit(STATUS_RF_KILL_HW, &priv->status);
5382 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005383 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005384 return -ENODEV;
5385 }
Zhu Yib481de92007-09-25 17:54:57 -07005386 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005387
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005388 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005389
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005390 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005391 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005392 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07005393 return rc;
5394 }
5395
5396 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005397 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5398 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005399 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5400
5401 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005402 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005403 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005404
5405 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005406 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5407 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005408
5409 /* Copy original ucode data image from disk into backup cache.
5410 * This will be used to initialize the on-board processor's
5411 * data SRAM for a clean start when the runtime program first loads. */
5412 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005413 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005414
Zhu Yie655b9f2008-01-24 02:19:38 -08005415 /* We return success when we resume from suspend and rf_kill is on. */
5416 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
5417 return 0;
5418
Zhu Yib481de92007-09-25 17:54:57 -07005419 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5420
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005421 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005422
5423 /* load bootstrap state machine,
5424 * load bootstrap program into processor's memory,
5425 * prepare to load the "initialize" uCode */
Kolekar, Abhijeet0164b9b2008-12-19 10:37:37 +08005426 priv->cfg->ops->lib->load_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005427
5428 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005429 IWL_ERR(priv,
5430 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07005431 continue;
5432 }
5433
5434 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005435 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005436
Zhu Yib481de92007-09-25 17:54:57 -07005437 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5438
5439 return 0;
5440 }
5441
5442 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005443 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005444 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005445
5446 /* tried to restart and config the device for as long as our
5447 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08005448 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07005449 return -EIO;
5450}
5451
5452
5453/*****************************************************************************
5454 *
5455 * Workqueue callbacks
5456 *
5457 *****************************************************************************/
5458
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005459static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005460{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005461 struct iwl_priv *priv =
5462 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005463
5464 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5465 return;
5466
5467 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005468 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005469 mutex_unlock(&priv->mutex);
5470}
5471
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005472static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005473{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005474 struct iwl_priv *priv =
5475 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005476
5477 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5478 return;
5479
5480 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005481 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005482 mutex_unlock(&priv->mutex);
5483}
5484
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005485static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005486{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005487 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005488
5489 wake_up_interruptible(&priv->wait_command_queue);
5490
5491 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5492 return;
5493
5494 mutex_lock(&priv->mutex);
5495
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005496 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005497 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5498 "HW and/or SW RF Kill no longer active, restarting "
5499 "device\n");
5500 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5501 queue_work(priv->workqueue, &priv->restart);
5502 } else {
5503
5504 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5505 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5506 "disabled by SW switch\n");
5507 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005508 IWL_WARN(priv, "Radio Frequency Kill Switch is On:\n"
Zhu Yib481de92007-09-25 17:54:57 -07005509 "Kill switch must be turned off for "
5510 "wireless networking to work.\n");
5511 }
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005512
Zhu Yib481de92007-09-25 17:54:57 -07005513 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005514 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005515}
5516
5517#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5518
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005519static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005520{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005521 struct iwl_priv *priv =
5522 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005523
5524 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5525 return;
5526
5527 mutex_lock(&priv->mutex);
5528 if (test_bit(STATUS_SCANNING, &priv->status) ||
5529 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5530 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5531 "Scan completion watchdog resetting adapter (%dms)\n",
5532 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005533
Zhu Yib481de92007-09-25 17:54:57 -07005534 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005535 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005536 }
5537 mutex_unlock(&priv->mutex);
5538}
5539
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005540static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005541{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005542 struct iwl_priv *priv =
5543 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08005544 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005545 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005546 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005547 .meta.flags = CMD_SIZE_HUGE,
5548 };
5549 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005550 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005551 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005552 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01005553 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04005554 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07005555
5556 conf = ieee80211_get_hw_conf(priv->hw);
5557
5558 mutex_lock(&priv->mutex);
5559
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005560 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005561 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005562 goto done;
5563 }
5564
Tomas Winklera96a27f2008-10-23 23:48:56 -07005565 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07005566 * was given the chance to run... */
5567 if (!test_bit(STATUS_SCANNING, &priv->status))
5568 goto done;
5569
5570 /* This should never be called or scheduled if there is currently
5571 * a scan active in the hardware. */
5572 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5573 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5574 "Ignoring second request.\n");
5575 rc = -EIO;
5576 goto done;
5577 }
5578
5579 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5580 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5581 goto done;
5582 }
5583
5584 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5585 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5586 goto done;
5587 }
5588
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005589 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005590 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5591 goto done;
5592 }
5593
5594 if (!test_bit(STATUS_READY, &priv->status)) {
5595 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5596 goto done;
5597 }
5598
5599 if (!priv->scan_bands) {
5600 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5601 goto done;
5602 }
5603
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005604 if (!priv->scan39) {
5605 priv->scan39 = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005606 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005607 if (!priv->scan39) {
Zhu Yib481de92007-09-25 17:54:57 -07005608 rc = -ENOMEM;
5609 goto done;
5610 }
5611 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005612 scan = priv->scan39;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005613 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005614
5615 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5616 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5617
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005618 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005619 u16 interval = 0;
5620 u32 extra;
5621 u32 suspend_time = 100;
5622 u32 scan_suspend_time = 100;
5623 unsigned long flags;
5624
5625 IWL_DEBUG_INFO("Scanning while associated...\n");
5626
5627 spin_lock_irqsave(&priv->lock, flags);
5628 interval = priv->beacon_int;
5629 spin_unlock_irqrestore(&priv->lock, flags);
5630
5631 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005632 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005633 if (!interval)
5634 interval = suspend_time;
5635 /*
5636 * suspend time format:
5637 * 0-19: beacon interval in usec (time before exec.)
5638 * 20-23: 0
5639 * 24-31: number of beacons (suspend between channels)
5640 */
5641
5642 extra = (suspend_time / interval) << 24;
5643 scan_suspend_time = 0xFF0FFFFF &
5644 (extra | ((suspend_time % interval) * 1024));
5645
5646 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5647 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5648 scan_suspend_time, interval);
5649 }
5650
5651 /* We should add the ability for user to lock to PASSIVE ONLY */
5652 if (priv->one_direct_scan) {
5653 IWL_DEBUG_SCAN
5654 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04005655 print_ssid(ssid, priv->direct_ssid,
5656 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07005657 scan->direct_scan[0].id = WLAN_EID_SSID;
5658 scan->direct_scan[0].len = priv->direct_ssid_len;
5659 memcpy(scan->direct_scan[0].ssid,
5660 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005661 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005662 } else
Bill Moss786b4552008-04-17 16:03:40 -07005663 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005664
5665 /* We don't build a direct scan probe request; the uCode will do
5666 * that based on the direct_mask added to each channel entry */
5667 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005668 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Johannes Berg430cfe92008-10-28 18:06:02 +01005669 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Zhu Yib481de92007-09-25 17:54:57 -07005670 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08005671 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07005672 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5673
5674 /* flags + rate selection */
5675
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005676 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005677 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5678 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
5679 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005680 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005681 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005682 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
5683 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005684 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005685 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005686 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07005687 goto done;
5688 }
5689
5690 /* select Rx antennas */
5691 scan->flags |= iwl3945_get_antenna_flags(priv);
5692
Johannes Berg05c914f2008-09-11 00:01:58 +02005693 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07005694 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5695
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005696 scan->channel_count =
5697 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
5698 n_probes,
5699 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07005700
Reinette Chatre14b54332008-11-04 12:21:35 -08005701 if (scan->channel_count == 0) {
5702 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
5703 goto done;
5704 }
5705
Zhu Yib481de92007-09-25 17:54:57 -07005706 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005707 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005708 cmd.data = scan;
5709 scan->len = cpu_to_le16(cmd.len);
5710
5711 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005712 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07005713 if (rc)
5714 goto done;
5715
5716 queue_delayed_work(priv->workqueue, &priv->scan_check,
5717 IWL_SCAN_CHECK_WATCHDOG);
5718
5719 mutex_unlock(&priv->mutex);
5720 return;
5721
5722 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08005723 /* can not perform scan make sure we clear scanning
5724 * bits from status so next scan request can be performed.
5725 * if we dont clear scanning status bit here all next scan
5726 * will fail
5727 */
5728 clear_bit(STATUS_SCAN_HW, &priv->status);
5729 clear_bit(STATUS_SCANNING, &priv->status);
5730
Ian Schram01ebd062007-10-25 17:15:22 +08005731 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07005732 queue_work(priv->workqueue, &priv->scan_completed);
5733 mutex_unlock(&priv->mutex);
5734}
5735
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005736static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005737{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005738 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07005739
5740 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5741 return;
5742
5743 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005744 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005745 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005746 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005747}
5748
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005749static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005750{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005751 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07005752
5753 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5754 return;
5755
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005756 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005757 queue_work(priv->workqueue, &priv->up);
5758}
5759
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005760static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005761{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005762 struct iwl_priv *priv =
5763 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07005764
5765 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5766 return;
5767
5768 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005769 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005770 mutex_unlock(&priv->mutex);
5771}
5772
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005773#define IWL_DELAY_NEXT_SCAN (HZ*2)
5774
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005775static void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005776{
Zhu Yib481de92007-09-25 17:54:57 -07005777 int rc = 0;
5778 struct ieee80211_conf *conf = NULL;
5779
Johannes Berg05c914f2008-09-11 00:01:58 +02005780 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005781 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07005782 return;
5783 }
5784
5785
Johannes Berge1749612008-10-27 15:59:26 -07005786 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005787 priv->assoc_id, priv->active39_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07005788
5789 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5790 return;
5791
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08005792 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005793 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08005794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005795 iwl3945_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005796
Zhu Yib481de92007-09-25 17:54:57 -07005797 conf = ieee80211_get_hw_conf(priv->hw);
5798
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005799 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005800 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005801
Tomas Winkler28afaf92008-12-19 10:37:06 +08005802 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005803 iwl3945_setup_rxon_timing(priv);
5804 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07005805 sizeof(priv->rxon_timing), &priv->rxon_timing);
5806 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005807 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07005808 "Attempting to continue.\n");
5809
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005810 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005811
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005812 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07005813
5814 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
5815 priv->assoc_id, priv->beacon_int);
5816
5817 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005818 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005819 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005820 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005821
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005822 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07005823 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005824 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005825 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005826 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005827
Johannes Berg05c914f2008-09-11 00:01:58 +02005828 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005829 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07005830
5831 }
5832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005833 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005834
5835 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02005836 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005837 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07005838 break;
5839
Johannes Berg05c914f2008-09-11 00:01:58 +02005840 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07005841
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08005842 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005843 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005844 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01005845 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07005846 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
5847 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005848 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
5849 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005850
5851 break;
5852
5853 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005854 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03005855 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07005856 break;
5857 }
5858
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005859 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08005860
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08005861 /* we have just associated, don't start scan too early */
5862 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08005863}
5864
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005865static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005866{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005867 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07005868
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005869 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005870 return;
5871
5872 mutex_lock(&priv->mutex);
5873
5874 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005875 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005876
5877 mutex_unlock(&priv->mutex);
5878}
5879
Johannes Berge8975582008-10-09 12:18:51 +02005880static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005881
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005882static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005883{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005884 struct iwl_priv *priv =
5885 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07005886
5887 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
5888
5889 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5890 return;
5891
Zhu Yia0646472007-12-20 14:10:01 +08005892 if (test_bit(STATUS_CONF_PENDING, &priv->status))
Johannes Berge8975582008-10-09 12:18:51 +02005893 iwl3945_mac_config(priv->hw, 0);
Zhu Yi76bb77e2007-11-22 10:53:22 +08005894
Zhu Yib481de92007-09-25 17:54:57 -07005895 ieee80211_scan_completed(priv->hw);
5896
5897 /* Since setting the TXPOWER may have been deferred while
5898 * performing the scan, fire one off */
5899 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005900 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005901 mutex_unlock(&priv->mutex);
5902}
5903
5904/*****************************************************************************
5905 *
5906 * mac80211 entry point functions
5907 *
5908 *****************************************************************************/
5909
Zhu Yi5a669262008-01-14 17:46:18 -08005910#define UCODE_READY_TIMEOUT (2 * HZ)
5911
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005912static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005913{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005914 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08005915 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005916
5917 IWL_DEBUG_MAC80211("enter\n");
5918
Zhu Yi5a669262008-01-14 17:46:18 -08005919 if (pci_enable_device(priv->pci_dev)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005920 IWL_ERR(priv, "Fail to pci_enable_device\n");
Zhu Yi5a669262008-01-14 17:46:18 -08005921 return -ENODEV;
5922 }
5923 pci_restore_state(priv->pci_dev);
5924 pci_enable_msi(priv->pci_dev);
5925
5926 ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
5927 DRV_NAME, priv);
5928 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005929 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
Zhu Yi5a669262008-01-14 17:46:18 -08005930 goto out_disable_msi;
5931 }
5932
Zhu Yib481de92007-09-25 17:54:57 -07005933 /* we should be verifying the device is ready to be opened */
5934 mutex_lock(&priv->mutex);
5935
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005936 memset(&priv->staging39_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
Zhu Yi5a669262008-01-14 17:46:18 -08005937 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
5938 * ucode filename and max sizes are card-specific. */
5939
5940 if (!priv->ucode_code.len) {
5941 ret = iwl3945_read_ucode(priv);
5942 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005943 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08005944 mutex_unlock(&priv->mutex);
5945 goto out_release_irq;
5946 }
5947 }
5948
Zhu Yie655b9f2008-01-24 02:19:38 -08005949 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08005950
Zhu Yib481de92007-09-25 17:54:57 -07005951 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08005952
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005953 iwl3945_rfkill_set_hw_state(priv);
5954
Zhu Yie655b9f2008-01-24 02:19:38 -08005955 if (ret)
5956 goto out_release_irq;
5957
5958 IWL_DEBUG_INFO("Start UP work.\n");
5959
5960 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
5961 return 0;
5962
Zhu Yi5a669262008-01-14 17:46:18 -08005963 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
5964 * mac80211 will not be run successfully. */
5965 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
5966 test_bit(STATUS_READY, &priv->status),
5967 UCODE_READY_TIMEOUT);
5968 if (!ret) {
5969 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005970 IWL_ERR(priv,
5971 "Wait for START_ALIVE timeout after %dms.\n",
5972 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08005973 ret = -ETIMEDOUT;
5974 goto out_release_irq;
5975 }
5976 }
5977
Zhu Yie655b9f2008-01-24 02:19:38 -08005978 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07005979 IWL_DEBUG_MAC80211("leave\n");
5980 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08005981
5982out_release_irq:
5983 free_irq(priv->pci_dev->irq, priv);
5984out_disable_msi:
5985 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08005986 pci_disable_device(priv->pci_dev);
5987 priv->is_open = 0;
5988 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08005989 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005990}
5991
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005992static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07005993{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005994 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07005995
5996 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08005997
Zhu Yie655b9f2008-01-24 02:19:38 -08005998 if (!priv->is_open) {
5999 IWL_DEBUG_MAC80211("leave - skip\n");
6000 return;
6001 }
6002
Zhu Yib481de92007-09-25 17:54:57 -07006003 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006004
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006005 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006006 /* stop mac, cancel any scan request and clear
6007 * RXON_FILTER_ASSOC_MSK BIT
6008 */
Zhu Yi5a669262008-01-14 17:46:18 -08006009 mutex_lock(&priv->mutex);
6010 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006011 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006012 }
6013
Zhu Yi5a669262008-01-14 17:46:18 -08006014 iwl3945_down(priv);
6015
6016 flush_workqueue(priv->workqueue);
6017 free_irq(priv->pci_dev->irq, priv);
6018 pci_disable_msi(priv->pci_dev);
6019 pci_save_state(priv->pci_dev);
6020 pci_disable_device(priv->pci_dev);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006021
Zhu Yib481de92007-09-25 17:54:57 -07006022 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006023}
6024
Johannes Berge039fa42008-05-15 12:55:29 +02006025static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006026{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006027 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006028
6029 IWL_DEBUG_MAC80211("enter\n");
6030
Zhu Yib481de92007-09-25 17:54:57 -07006031 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02006032 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006033
Johannes Berge039fa42008-05-15 12:55:29 +02006034 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07006035 dev_kfree_skb_any(skb);
6036
6037 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08006038 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07006039}
6040
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006041static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006042 struct ieee80211_if_init_conf *conf)
6043{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006044 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006045 unsigned long flags;
6046
Johannes Berg32bfd352007-12-19 01:31:26 +01006047 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006048
Johannes Berg32bfd352007-12-19 01:31:26 +01006049 if (priv->vif) {
6050 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02006051 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006052 }
6053
6054 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006055 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07006056 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07006057
6058 spin_unlock_irqrestore(&priv->lock, flags);
6059
6060 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006061
6062 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07006063 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02006064 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6065 }
6066
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006067 if (iwl_is_ready(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006068 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006069
Zhu Yib481de92007-09-25 17:54:57 -07006070 mutex_unlock(&priv->mutex);
6071
Zhu Yi5a669262008-01-14 17:46:18 -08006072 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006073 return 0;
6074}
6075
6076/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006077 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006078 *
6079 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6080 * be set inappropriately and the driver currently sets the hardware up to
6081 * use it whenever needed.
6082 */
Johannes Berge8975582008-10-09 12:18:51 +02006083static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07006084{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006085 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006086 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02006087 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07006088 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006089 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006090
6091 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006092 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006093
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006094 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006095 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006096 ret = -EIO;
6097 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006098 }
6099
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006100 if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006101 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006102 IWL_DEBUG_MAC80211("leave - scanning\n");
6103 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006104 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006105 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006106 }
6107
6108 spin_lock_irqsave(&priv->lock, flags);
6109
Johannes Berg8318d782008-01-24 19:38:38 +01006110 ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
6111 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006112 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006113 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01006114 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006115 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6116 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006117 ret = -EINVAL;
6118 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006119 }
6120
Johannes Berg8318d782008-01-24 19:38:38 +01006121 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006122
Johannes Berg8318d782008-01-24 19:38:38 +01006123 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006124
6125 /* The list of supported rates and rate mask can be different
6126 * for each phymode; since the phymode may have changed, reset
6127 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006128 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006129
6130 spin_unlock_irqrestore(&priv->lock, flags);
6131
6132#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6133 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006134 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006135 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006136 }
6137#endif
6138
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006139 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006140
6141 if (!conf->radio_enabled) {
6142 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006143 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006144 }
6145
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006146 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006147 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006148 ret = -EIO;
6149 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006150 }
6151
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006152 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006153
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006154 if (memcmp(&priv->active39_rxon,
6155 &priv->staging39_rxon, sizeof(priv->staging39_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006156 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006157 else
6158 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6159
6160 IWL_DEBUG_MAC80211("leave\n");
6161
Zhu Yi76bb77e2007-11-22 10:53:22 +08006162out:
Zhu Yia0646472007-12-20 14:10:01 +08006163 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006164 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006165 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006166}
6167
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006168static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006169{
6170 int rc = 0;
6171
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006172 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006173 return;
6174
6175 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08006176 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07006177
6178 /* RXON - unassoc (to set timing command) */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006179 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006180 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006181
6182 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08006183 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006184 iwl3945_setup_rxon_timing(priv);
6185 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006186 sizeof(priv->rxon_timing), &priv->rxon_timing);
6187 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006188 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006189 "Attempting to continue.\n");
6190
6191 /* FIXME: what should be the assoc_id for AP? */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006192 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006193 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006194 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006195 RXON_FLG_SHORT_PREAMBLE_MSK;
6196 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006197 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006198 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6199
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006200 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006201 if (priv->assoc_capability &
6202 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006203 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006204 RXON_FLG_SHORT_SLOT_MSK;
6205 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006206 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006207 ~RXON_FLG_SHORT_SLOT_MSK;
6208
Johannes Berg05c914f2008-09-11 00:01:58 +02006209 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006210 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006211 ~RXON_FLG_SHORT_SLOT_MSK;
6212 }
6213 /* restore RXON assoc */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006214 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006215 iwl3945_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08006216 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08006217 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006218 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006219
6220 /* FIXME - we need to add code here to detect a totally new
6221 * configuration, reset the AP, unassoc, rxon timing, assoc,
6222 * clear sta table, add BCAST sta... */
6223}
6224
Johannes Berg32bfd352007-12-19 01:31:26 +01006225static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
6226 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006227 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006228{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006229 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006230 int rc;
6231
6232 if (conf == NULL)
6233 return -EIO;
6234
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006235 if (priv->vif != vif) {
6236 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006237 return 0;
6238 }
6239
Johannes Berg9d139c82008-07-09 14:40:37 +02006240 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02006241 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02006242 conf->changed & IEEE80211_IFCC_BEACON) {
6243 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
6244 if (!beacon)
6245 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006246 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006247 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006248 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006249 if (rc)
6250 return rc;
6251 }
6252
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006253 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006254 return -EAGAIN;
6255
Zhu Yib481de92007-09-25 17:54:57 -07006256 mutex_lock(&priv->mutex);
6257
Zhu Yib481de92007-09-25 17:54:57 -07006258 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07006259 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006260
Johannes Berg4150c572007-09-17 01:29:23 -04006261/*
6262 * very dubious code was here; the probe filtering flag is never set:
6263 *
Zhu Yib481de92007-09-25 17:54:57 -07006264 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6265 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006266 */
Zhu Yib481de92007-09-25 17:54:57 -07006267
Johannes Berg05c914f2008-09-11 00:01:58 +02006268 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07006269 if (!conf->bssid) {
6270 conf->bssid = priv->mac_addr;
6271 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07006272 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
6273 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006274 }
6275 if (priv->ibss_beacon)
6276 dev_kfree_skb(priv->ibss_beacon);
6277
Johannes Berg9d139c82008-07-09 14:40:37 +02006278 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07006279 }
6280
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006281 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006282 goto done;
6283
Zhu Yib481de92007-09-25 17:54:57 -07006284 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6285 !is_multicast_ether_addr(conf->bssid)) {
6286 /* If there is currently a HW scan going on in the background
6287 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006288 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006289 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07006290 "after 100ms\n");
6291 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6292 mutex_unlock(&priv->mutex);
6293 return -EAGAIN;
6294 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006295 memcpy(priv->staging39_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006296
6297 /* TODO: Audit driver for usage of these members and see
6298 * if mac80211 deprecates them (priv->bssid looks like it
6299 * shouldn't be there, but I haven't scanned the IBSS code
6300 * to verify) - jpk */
6301 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6302
Johannes Berg05c914f2008-09-11 00:01:58 +02006303 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006304 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006305 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006306 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02006307 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006308 iwl3945_add_station(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006309 priv->active39_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006310 }
6311
6312 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006313 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006314 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006315 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006316 }
6317
Mohamed Abbasfde35712007-11-29 11:10:15 +08006318 done:
Zhu Yib481de92007-09-25 17:54:57 -07006319 IWL_DEBUG_MAC80211("leave\n");
6320 mutex_unlock(&priv->mutex);
6321
6322 return 0;
6323}
6324
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006325static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006326 unsigned int changed_flags,
6327 unsigned int *total_flags,
6328 int mc_count, struct dev_addr_list *mc_list)
6329{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006330 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006331 __le32 *filter_flags = &priv->staging39_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08006332
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006333 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
6334 changed_flags, *total_flags);
6335
6336 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
6337 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
6338 *filter_flags |= RXON_FILTER_PROMISC_MSK;
6339 else
6340 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006341 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006342 if (changed_flags & FIF_ALLMULTI) {
6343 if (*total_flags & FIF_ALLMULTI)
6344 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
6345 else
6346 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
6347 }
6348 if (changed_flags & FIF_CONTROL) {
6349 if (*total_flags & FIF_CONTROL)
6350 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
6351 else
6352 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
6353 }
6354 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
6355 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6356 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
6357 else
6358 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
6359 }
6360
6361 /* We avoid iwl_commit_rxon here to commit the new filter flags
6362 * since mac80211 will call ieee80211_hw_config immediately.
6363 * (mc_list is not supported at this time). Otherwise, we need to
6364 * queue a background iwl_commit_rxon work.
6365 */
6366
6367 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08006368 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04006369}
6370
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006371static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006372 struct ieee80211_if_init_conf *conf)
6373{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006374 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006375
6376 IWL_DEBUG_MAC80211("enter\n");
6377
6378 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006379
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006380 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006381 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006382 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Mohamed Abbasfde35712007-11-29 11:10:15 +08006383 iwl3945_commit_rxon(priv);
6384 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006385 if (priv->vif == conf->vif) {
6386 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006387 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006388 }
6389 mutex_unlock(&priv->mutex);
6390
6391 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006392}
6393
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006394#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
6395
6396static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
6397 struct ieee80211_vif *vif,
6398 struct ieee80211_bss_conf *bss_conf,
6399 u32 changes)
6400{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006401 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006402
6403 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6404
6405 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6406 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6407 bss_conf->use_short_preamble);
6408 if (bss_conf->use_short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006409 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006410 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006411 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006412 }
6413
6414 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
6415 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
6416 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006417 priv->staging39_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006418 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006419 priv->staging39_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006420 }
6421
6422 if (changes & BSS_CHANGED_ASSOC) {
6423 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6424 /* This should never happen as this function should
6425 * never be called from interrupt context. */
6426 if (WARN_ON_ONCE(in_interrupt()))
6427 return;
6428 if (bss_conf->assoc) {
6429 priv->assoc_id = bss_conf->aid;
6430 priv->beacon_int = bss_conf->beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006431 priv->timestamp = bss_conf->timestamp;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006432 priv->assoc_capability = bss_conf->assoc_capability;
6433 priv->next_scan_jiffies = jiffies +
6434 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6435 mutex_lock(&priv->mutex);
6436 iwl3945_post_associate(priv);
6437 mutex_unlock(&priv->mutex);
6438 } else {
6439 priv->assoc_id = 0;
6440 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6441 }
6442 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
6443 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6444 iwl3945_send_rxon_assoc(priv);
6445 }
6446
6447}
6448
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006449static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006450{
6451 int rc = 0;
6452 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006453 struct iwl_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04006454 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07006455
6456 IWL_DEBUG_MAC80211("enter\n");
6457
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006458 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006459 spin_lock_irqsave(&priv->lock, flags);
6460
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006461 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006462 rc = -EIO;
6463 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6464 goto out_unlock;
6465 }
6466
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006467 /* we don't schedule scan within next_scan_jiffies period */
6468 if (priv->next_scan_jiffies &&
6469 time_after(priv->next_scan_jiffies, jiffies)) {
6470 rc = -EAGAIN;
6471 goto out_unlock;
6472 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08006473 /* if we just finished scan ask for delay for a broadcast scan */
6474 if ((len == 0) && priv->last_scan_jiffies &&
6475 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
6476 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006477 rc = -EAGAIN;
6478 goto out_unlock;
6479 }
6480 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006481 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04006482 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006483
6484 priv->one_direct_scan = 1;
6485 priv->direct_ssid_len = (u8)
6486 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6487 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006488 } else
6489 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006490
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006491 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006492
6493 IWL_DEBUG_MAC80211("leave\n");
6494
6495out_unlock:
6496 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006497 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006498
6499 return rc;
6500}
6501
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006502static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Johannes Bergdc822b52008-12-29 12:55:09 +01006503 struct ieee80211_vif *vif,
6504 struct ieee80211_sta *sta,
6505 struct ieee80211_key_conf *key)
Zhu Yib481de92007-09-25 17:54:57 -07006506{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006507 struct iwl_priv *priv = hw->priv;
Johannes Bergdc822b52008-12-29 12:55:09 +01006508 const u8 *addr;
Zhu Yib481de92007-09-25 17:54:57 -07006509 int rc = 0;
6510 u8 sta_id;
Johannes Bergdc822b52008-12-29 12:55:09 +01006511 static const u8 bcast_addr[ETH_ALEN] =
6512 { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
Zhu Yib481de92007-09-25 17:54:57 -07006513
6514 IWL_DEBUG_MAC80211("enter\n");
6515
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006516 if (iwl3945_mod_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006517 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6518 return -EOPNOTSUPP;
6519 }
6520
Johannes Bergdc822b52008-12-29 12:55:09 +01006521 addr = sta ? sta->addr : bcast_addr;
Zhu Yib481de92007-09-25 17:54:57 -07006522
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006523 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07006524 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07006525 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
6526 addr);
Zhu Yib481de92007-09-25 17:54:57 -07006527 return -EINVAL;
6528 }
6529
6530 mutex_lock(&priv->mutex);
6531
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006532 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006533
Zhu Yib481de92007-09-25 17:54:57 -07006534 switch (cmd) {
6535 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006536 rc = iwl3945_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006537 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006538 iwl3945_set_rxon_hwcrypto(priv, 1);
6539 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006540 key->hw_key_idx = sta_id;
6541 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
6542 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
6543 }
6544 break;
6545 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006546 rc = iwl3945_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006547 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006548 iwl3945_set_rxon_hwcrypto(priv, 0);
6549 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006550 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
6551 }
6552 break;
6553 default:
6554 rc = -EINVAL;
6555 }
6556
6557 IWL_DEBUG_MAC80211("leave\n");
6558 mutex_unlock(&priv->mutex);
6559
6560 return rc;
6561}
6562
Johannes Berge100bb62008-04-30 18:51:21 +02006563static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07006564 const struct ieee80211_tx_queue_params *params)
6565{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006566 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006567 unsigned long flags;
6568 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006569
6570 IWL_DEBUG_MAC80211("enter\n");
6571
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006572 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006573 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6574 return -EIO;
6575 }
6576
6577 if (queue >= AC_NUM) {
6578 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6579 return 0;
6580 }
6581
Zhu Yib481de92007-09-25 17:54:57 -07006582 q = AC_NUM - 1 - queue;
6583
6584 spin_lock_irqsave(&priv->lock, flags);
6585
6586 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6587 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6588 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6589 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7be2008-02-10 16:49:38 +01006590 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006591
6592 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6593 priv->qos_data.qos_active = 1;
6594
6595 spin_unlock_irqrestore(&priv->lock, flags);
6596
6597 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02006598 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006599 iwl3945_activate_qos(priv, 1);
6600 else if (priv->assoc_id && iwl3945_is_associated(priv))
6601 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006602
6603 mutex_unlock(&priv->mutex);
6604
Zhu Yib481de92007-09-25 17:54:57 -07006605 IWL_DEBUG_MAC80211("leave\n");
6606 return 0;
6607}
6608
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006609static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006610 struct ieee80211_tx_queue_stats *stats)
6611{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006612 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006613 int i, avail;
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08006614 struct iwl_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006615 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07006616 unsigned long flags;
6617
6618 IWL_DEBUG_MAC80211("enter\n");
6619
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006620 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006621 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6622 return -EIO;
6623 }
6624
6625 spin_lock_irqsave(&priv->lock, flags);
6626
6627 for (i = 0; i < AC_NUM; i++) {
Samuel Ortiz188cf6c2008-12-22 11:31:16 +08006628 txq = &priv->txq[i];
Zhu Yib481de92007-09-25 17:54:57 -07006629 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006630 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07006631
Johannes Berg57ffc582008-04-29 17:18:59 +02006632 stats[i].len = q->n_window - avail;
6633 stats[i].limit = q->n_window - q->high_mark;
6634 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07006635
6636 }
6637 spin_unlock_irqrestore(&priv->lock, flags);
6638
6639 IWL_DEBUG_MAC80211("leave\n");
6640
6641 return 0;
6642}
6643
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006644static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006645{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006646 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006647 unsigned long flags;
6648
6649 mutex_lock(&priv->mutex);
6650 IWL_DEBUG_MAC80211("enter\n");
6651
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006652 iwl_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006653
Zhu Yib481de92007-09-25 17:54:57 -07006654 spin_lock_irqsave(&priv->lock, flags);
6655 priv->assoc_id = 0;
6656 priv->assoc_capability = 0;
6657 priv->call_post_assoc_from_beacon = 0;
6658
6659 /* new association get rid of ibss beacon skb */
6660 if (priv->ibss_beacon)
6661 dev_kfree_skb(priv->ibss_beacon);
6662
6663 priv->ibss_beacon = NULL;
6664
6665 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006666 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02006667 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07006668 priv->beacon_int = 0;
6669
6670 spin_unlock_irqrestore(&priv->lock, flags);
6671
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006672 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006673 IWL_DEBUG_MAC80211("leave - not ready\n");
6674 mutex_unlock(&priv->mutex);
6675 return;
6676 }
6677
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006678 /* we are restarting association process
6679 * clear RXON_FILTER_ASSOC_MSK bit
6680 */
Johannes Berg05c914f2008-09-11 00:01:58 +02006681 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006682 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006683 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006684 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006685 }
6686
Zhu Yib481de92007-09-25 17:54:57 -07006687 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02006688 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006689
Zhu Yib481de92007-09-25 17:54:57 -07006690 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
6691 mutex_unlock(&priv->mutex);
6692 return;
6693 }
6694
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006695 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006696
6697 mutex_unlock(&priv->mutex);
6698
6699 IWL_DEBUG_MAC80211("leave\n");
6700
6701}
6702
Johannes Berge039fa42008-05-15 12:55:29 +02006703static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006704{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006705 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006706 unsigned long flags;
6707
Zhu Yib481de92007-09-25 17:54:57 -07006708 IWL_DEBUG_MAC80211("enter\n");
6709
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006710 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006711 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07006712 return -EIO;
6713 }
6714
Johannes Berg05c914f2008-09-11 00:01:58 +02006715 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07006716 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07006717 return -EIO;
6718 }
6719
6720 spin_lock_irqsave(&priv->lock, flags);
6721
6722 if (priv->ibss_beacon)
6723 dev_kfree_skb(priv->ibss_beacon);
6724
6725 priv->ibss_beacon = skb;
6726
6727 priv->assoc_id = 0;
6728
6729 IWL_DEBUG_MAC80211("leave\n");
6730 spin_unlock_irqrestore(&priv->lock, flags);
6731
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006732 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006733
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08006734 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006735
Zhu Yib481de92007-09-25 17:54:57 -07006736
6737 return 0;
6738}
6739
6740/*****************************************************************************
6741 *
6742 * sysfs attributes
6743 *
6744 *****************************************************************************/
6745
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006746#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07006747
6748/*
6749 * The following adds a new attribute to the sysfs representation
6750 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
6751 * used for controlling the debug level.
6752 *
6753 * See the level definitions in iwl for details.
6754 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006755static ssize_t show_debug_level(struct device *d,
6756 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07006757{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006758 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006759
6760 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006761}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006762static ssize_t store_debug_level(struct device *d,
6763 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07006764 const char *buf, size_t count)
6765{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006766 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006767 unsigned long val;
6768 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006769
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006770 ret = strict_strtoul(buf, 0, &val);
6771 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08006772 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07006773 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006774 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07006775
6776 return strnlen(buf, count);
6777}
6778
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08006779static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
6780 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07006781
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006782#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07006783
Zhu Yib481de92007-09-25 17:54:57 -07006784static ssize_t show_temperature(struct device *d,
6785 struct device_attribute *attr, char *buf)
6786{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006787 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006788
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006789 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006790 return -EAGAIN;
6791
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006792 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07006793}
6794
6795static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
6796
Zhu Yib481de92007-09-25 17:54:57 -07006797static ssize_t show_tx_power(struct device *d,
6798 struct device_attribute *attr, char *buf)
6799{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006800 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006801 return sprintf(buf, "%d\n", priv->user_txpower_limit);
6802}
6803
6804static ssize_t store_tx_power(struct device *d,
6805 struct device_attribute *attr,
6806 const char *buf, size_t count)
6807{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006808 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006809 char *p = (char *)buf;
6810 u32 val;
6811
6812 val = simple_strtoul(p, &p, 10);
6813 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08006814 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07006815 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006816 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07006817
6818 return count;
6819}
6820
6821static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
6822
6823static ssize_t show_flags(struct device *d,
6824 struct device_attribute *attr, char *buf)
6825{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006826 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006827
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006828 return sprintf(buf, "0x%04X\n", priv->active39_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07006829}
6830
6831static ssize_t store_flags(struct device *d,
6832 struct device_attribute *attr,
6833 const char *buf, size_t count)
6834{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006835 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006836 u32 flags = simple_strtoul(buf, NULL, 0);
6837
6838 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006839 if (le32_to_cpu(priv->staging39_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07006840 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006841 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006842 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006843 else {
6844 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
6845 flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006846 priv->staging39_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006847 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006848 }
6849 }
6850 mutex_unlock(&priv->mutex);
6851
6852 return count;
6853}
6854
6855static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
6856
6857static ssize_t show_filter_flags(struct device *d,
6858 struct device_attribute *attr, char *buf)
6859{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006860 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006861
6862 return sprintf(buf, "0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006863 le32_to_cpu(priv->active39_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07006864}
6865
6866static ssize_t store_filter_flags(struct device *d,
6867 struct device_attribute *attr,
6868 const char *buf, size_t count)
6869{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006870 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07006871 u32 filter_flags = simple_strtoul(buf, NULL, 0);
6872
6873 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006874 if (le32_to_cpu(priv->staging39_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07006875 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006876 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006877 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07006878 else {
6879 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
6880 "0x%04X\n", filter_flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006881 priv->staging39_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07006882 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006883 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006884 }
6885 }
6886 mutex_unlock(&priv->mutex);
6887
6888 return count;
6889}
6890
6891static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
6892 store_filter_flags);
6893
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006894#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07006895
6896static ssize_t show_measurement(struct device *d,
6897 struct device_attribute *attr, char *buf)
6898{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006899 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08006900 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006901 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006902 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07006903 unsigned long flags;
6904
6905 spin_lock_irqsave(&priv->lock, flags);
6906 if (!(priv->measurement_status & MEASUREMENT_READY)) {
6907 spin_unlock_irqrestore(&priv->lock, flags);
6908 return 0;
6909 }
6910 memcpy(&measure_report, &priv->measure_report, size);
6911 priv->measurement_status = 0;
6912 spin_unlock_irqrestore(&priv->lock, flags);
6913
6914 while (size && (PAGE_SIZE - len)) {
6915 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
6916 PAGE_SIZE - len, 1);
6917 len = strlen(buf);
6918 if (PAGE_SIZE - len)
6919 buf[len++] = '\n';
6920
6921 ofs += 16;
6922 size -= min(size, 16U);
6923 }
6924
6925 return len;
6926}
6927
6928static ssize_t store_measurement(struct device *d,
6929 struct device_attribute *attr,
6930 const char *buf, size_t count)
6931{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006932 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006933 struct ieee80211_measurement_params params = {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006934 .channel = le16_to_cpu(priv->active39_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07006935 .start_time = cpu_to_le64(priv->last_tsf),
6936 .duration = cpu_to_le16(1),
6937 };
6938 u8 type = IWL_MEASURE_BASIC;
6939 u8 buffer[32];
6940 u8 channel;
6941
6942 if (count) {
6943 char *p = buffer;
6944 strncpy(buffer, buf, min(sizeof(buffer), count));
6945 channel = simple_strtoul(p, NULL, 0);
6946 if (channel)
6947 params.channel = channel;
6948
6949 p = buffer;
6950 while (*p && *p != ' ')
6951 p++;
6952 if (*p)
6953 type = simple_strtoul(p + 1, NULL, 0);
6954 }
6955
6956 IWL_DEBUG_INFO("Invoking measurement of type %d on "
6957 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006958 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07006959
6960 return count;
6961}
6962
6963static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
6964 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006965#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07006966
Zhu Yib481de92007-09-25 17:54:57 -07006967static ssize_t store_retry_rate(struct device *d,
6968 struct device_attribute *attr,
6969 const char *buf, size_t count)
6970{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006971 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006972
6973 priv->retry_rate = simple_strtoul(buf, NULL, 0);
6974 if (priv->retry_rate <= 0)
6975 priv->retry_rate = 1;
6976
6977 return count;
6978}
6979
6980static ssize_t show_retry_rate(struct device *d,
6981 struct device_attribute *attr, char *buf)
6982{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006983 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006984 return sprintf(buf, "%d", priv->retry_rate);
6985}
6986
6987static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
6988 store_retry_rate);
6989
6990static ssize_t store_power_level(struct device *d,
6991 struct device_attribute *attr,
6992 const char *buf, size_t count)
6993{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006994 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07006995 int rc;
6996 int mode;
6997
6998 mode = simple_strtoul(buf, NULL, 0);
6999 mutex_lock(&priv->mutex);
7000
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007001 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007002 rc = -EAGAIN;
7003 goto out;
7004 }
7005
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007006 if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
7007 (mode == IWL39_POWER_AC))
7008 mode = IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07007009 else
7010 mode |= IWL_POWER_ENABLED;
7011
7012 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007013 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007014 if (rc) {
7015 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7016 goto out;
7017 }
7018 priv->power_mode = mode;
7019 }
7020
7021 rc = count;
7022
7023 out:
7024 mutex_unlock(&priv->mutex);
7025 return rc;
7026}
7027
7028#define MAX_WX_STRING 80
7029
7030/* Values are in microsecond */
7031static const s32 timeout_duration[] = {
7032 350000,
7033 250000,
7034 75000,
7035 37000,
7036 25000,
7037};
7038static const s32 period_duration[] = {
7039 400000,
7040 700000,
7041 1000000,
7042 1000000,
7043 1000000
7044};
7045
7046static ssize_t show_power_level(struct device *d,
7047 struct device_attribute *attr, char *buf)
7048{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007049 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007050 int level = IWL_POWER_LEVEL(priv->power_mode);
7051 char *p = buf;
7052
7053 p += sprintf(p, "%d ", level);
7054 switch (level) {
7055 case IWL_POWER_MODE_CAM:
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007056 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07007057 p += sprintf(p, "(AC)");
7058 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007059 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07007060 p += sprintf(p, "(BATTERY)");
7061 break;
7062 default:
7063 p += sprintf(p,
7064 "(Timeout %dms, Period %dms)",
7065 timeout_duration[level - 1] / 1000,
7066 period_duration[level - 1] / 1000);
7067 }
7068
7069 if (!(priv->power_mode & IWL_POWER_ENABLED))
7070 p += sprintf(p, " OFF\n");
7071 else
7072 p += sprintf(p, " \n");
7073
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007074 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07007075
7076}
7077
7078static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7079 store_power_level);
7080
7081static ssize_t show_channels(struct device *d,
7082 struct device_attribute *attr, char *buf)
7083{
Johannes Berg8318d782008-01-24 19:38:38 +01007084 /* all this shit doesn't belong into sysfs anyway */
7085 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007086}
7087
7088static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7089
7090static ssize_t show_statistics(struct device *d,
7091 struct device_attribute *attr, char *buf)
7092{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007093 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007094 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007095 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007096 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07007097 int rc = 0;
7098
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007099 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007100 return -EAGAIN;
7101
7102 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007103 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007104 mutex_unlock(&priv->mutex);
7105
7106 if (rc) {
7107 len = sprintf(buf,
7108 "Error sending statistics request: 0x%08X\n", rc);
7109 return len;
7110 }
7111
7112 while (size && (PAGE_SIZE - len)) {
7113 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7114 PAGE_SIZE - len, 1);
7115 len = strlen(buf);
7116 if (PAGE_SIZE - len)
7117 buf[len++] = '\n';
7118
7119 ofs += 16;
7120 size -= min(size, 16U);
7121 }
7122
7123 return len;
7124}
7125
7126static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7127
7128static ssize_t show_antenna(struct device *d,
7129 struct device_attribute *attr, char *buf)
7130{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007131 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007132
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007133 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007134 return -EAGAIN;
7135
7136 return sprintf(buf, "%d\n", priv->antenna);
7137}
7138
7139static ssize_t store_antenna(struct device *d,
7140 struct device_attribute *attr,
7141 const char *buf, size_t count)
7142{
7143 int ant;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007144 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007145
7146 if (count == 0)
7147 return 0;
7148
7149 if (sscanf(buf, "%1i", &ant) != 1) {
7150 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7151 return count;
7152 }
7153
7154 if ((ant >= 0) && (ant <= 2)) {
7155 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007156 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007157 } else
7158 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7159
7160
7161 return count;
7162}
7163
7164static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7165
7166static ssize_t show_status(struct device *d,
7167 struct device_attribute *attr, char *buf)
7168{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007169 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007170 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007171 return -EAGAIN;
7172 return sprintf(buf, "0x%08x\n", (int)priv->status);
7173}
7174
7175static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7176
7177static ssize_t dump_error_log(struct device *d,
7178 struct device_attribute *attr,
7179 const char *buf, size_t count)
7180{
7181 char *p = (char *)buf;
7182
7183 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007184 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007185
7186 return strnlen(buf, count);
7187}
7188
7189static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7190
7191static ssize_t dump_event_log(struct device *d,
7192 struct device_attribute *attr,
7193 const char *buf, size_t count)
7194{
7195 char *p = (char *)buf;
7196
7197 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007198 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007199
7200 return strnlen(buf, count);
7201}
7202
7203static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7204
7205/*****************************************************************************
7206 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07007207 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07007208 *
7209 *****************************************************************************/
7210
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007211static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007212{
7213 priv->workqueue = create_workqueue(DRV_NAME);
7214
7215 init_waitqueue_head(&priv->wait_command_queue);
7216
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007217 INIT_WORK(&priv->up, iwl3945_bg_up);
7218 INIT_WORK(&priv->restart, iwl3945_bg_restart);
7219 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
7220 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
7221 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
7222 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
7223 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
7224 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007225 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
7226 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
7227 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007228
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007229 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007230
7231 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007232 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007233}
7234
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007235static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007236{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007237 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007238
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09007239 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007240 cancel_delayed_work(&priv->scan_check);
7241 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007242 cancel_work_sync(&priv->beacon_update);
7243}
7244
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007245static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007246 &dev_attr_antenna.attr,
7247 &dev_attr_channels.attr,
7248 &dev_attr_dump_errors.attr,
7249 &dev_attr_dump_events.attr,
7250 &dev_attr_flags.attr,
7251 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007252#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007253 &dev_attr_measurement.attr,
7254#endif
7255 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007256 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007257 &dev_attr_statistics.attr,
7258 &dev_attr_status.attr,
7259 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007260 &dev_attr_tx_power.attr,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007261#ifdef CONFIG_IWL3945_DEBUG
7262 &dev_attr_debug_level.attr,
7263#endif
Zhu Yib481de92007-09-25 17:54:57 -07007264 NULL
7265};
7266
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007267static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007268 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007269 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007270};
7271
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007272static struct ieee80211_ops iwl3945_hw_ops = {
7273 .tx = iwl3945_mac_tx,
7274 .start = iwl3945_mac_start,
7275 .stop = iwl3945_mac_stop,
7276 .add_interface = iwl3945_mac_add_interface,
7277 .remove_interface = iwl3945_mac_remove_interface,
7278 .config = iwl3945_mac_config,
7279 .config_interface = iwl3945_mac_config_interface,
7280 .configure_filter = iwl3945_configure_filter,
7281 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007282 .get_tx_stats = iwl3945_mac_get_tx_stats,
7283 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007284 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08007285 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007286 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007287};
7288
Winkler, Tomase52119c2008-12-22 11:31:19 +08007289static int iwl3945_init_drv(struct iwl_priv *priv)
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007290{
7291 int ret;
7292
7293 priv->retry_rate = 1;
7294 priv->ibss_beacon = NULL;
7295
7296 spin_lock_init(&priv->lock);
7297 spin_lock_init(&priv->power_data.lock);
7298 spin_lock_init(&priv->sta_lock);
7299 spin_lock_init(&priv->hcmd_lock);
7300
7301 INIT_LIST_HEAD(&priv->free_frames);
7302
7303 mutex_init(&priv->mutex);
7304
7305 /* Clear the driver's (not device's) station table */
7306 iwl3945_clear_stations_table(priv);
7307
7308 priv->data_retry_limit = -1;
7309 priv->ieee_channels = NULL;
7310 priv->ieee_rates = NULL;
7311 priv->band = IEEE80211_BAND_2GHZ;
7312
7313 priv->iw_mode = NL80211_IFTYPE_STATION;
7314
7315 iwl_reset_qos(priv);
7316
7317 priv->qos_data.qos_active = 0;
7318 priv->qos_data.qos_cap.val = 0;
7319
7320 priv->rates_mask = IWL_RATES_MASK;
7321 /* If power management is turned on, default to AC mode */
7322 priv->power_mode = IWL_POWER_AC;
7323 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
7324
7325 ret = iwl3945_init_channel_map(priv);
7326 if (ret) {
7327 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
7328 goto err;
7329 }
7330
7331 ret = iwl3945_init_geos(priv);
7332 if (ret) {
7333 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
7334 goto err_free_channel_map;
7335 }
7336
7337 return 0;
7338
7339err_free_channel_map:
7340 iwl3945_free_channel_map(priv);
7341err:
7342 return ret;
7343}
7344
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007345static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007346{
7347 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007348 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007349 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08007350 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007351 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007352
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007353 /***********************
7354 * 1. Allocating HW data
7355 * ********************/
7356
Zhu Yib481de92007-09-25 17:54:57 -07007357 /* mac80211 allocates memory for this device instance, including
7358 * space for this driver's private structure */
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007359 hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07007360 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007361 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07007362 err = -ENOMEM;
7363 goto out;
7364 }
Zhu Yib481de92007-09-25 17:54:57 -07007365 priv = hw->priv;
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007366 SET_IEEE80211_DEV(hw, &pdev->dev);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007367
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007368 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
7369 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007370 IWL_ERR(priv,
7371 "invalid queues_num, should be between %d and %d\n",
7372 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08007373 err = -EINVAL;
7374 goto out;
7375 }
7376
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007377 /*
7378 * Disabling hardware scan means that mac80211 will perform scans
7379 * "the hard way", rather than using device's scan.
7380 */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007381 if (iwl3945_mod_params.disable_hw_scan) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007382 IWL_DEBUG_INFO("Disabling hw_scan\n");
7383 iwl3945_hw_ops.hw_scan = NULL;
7384 }
7385
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007386
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007387 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007388 priv->cfg = cfg;
7389 priv->pci_dev = pdev;
7390
7391#ifdef CONFIG_IWL3945_DEBUG
7392 priv->debug_level = iwl3945_mod_params.debug;
7393 atomic_set(&priv->restrict_refcnt, 0);
7394#endif
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007395 hw->rate_control_algorithm = "iwl-3945-rs";
7396 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
7397
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007398 /* Select antenna (may be helpful if only one antenna is connected) */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007399 priv->antenna = (enum iwl3945_antenna)iwl3945_mod_params.antenna;
Zhu Yib481de92007-09-25 17:54:57 -07007400
Bruno Randolf566bfe52008-05-08 19:15:40 +02007401 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02007402 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02007403 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07007404
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007405 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007406 BIT(NL80211_IFTYPE_STATION) |
7407 BIT(NL80211_IFTYPE_ADHOC);
7408
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08007409 hw->wiphy->fw_handles_regulatory = true;
7410
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007411 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07007412 hw->queues = 4;
7413
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007414 /***************************
7415 * 2. Initializing PCI bus
7416 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07007417 if (pci_enable_device(pdev)) {
7418 err = -ENODEV;
7419 goto out_ieee80211_free_hw;
7420 }
7421
7422 pci_set_master(pdev);
7423
Zhu Yib481de92007-09-25 17:54:57 -07007424 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7425 if (!err)
7426 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7427 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08007428 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007429 goto out_pci_disable_device;
7430 }
7431
7432 pci_set_drvdata(pdev, priv);
7433 err = pci_request_regions(pdev, DRV_NAME);
7434 if (err)
7435 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007436
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007437 /***********************
7438 * 3. Read REV Register
7439 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07007440 priv->hw_base = pci_iomap(pdev, 0, 0);
7441 if (!priv->hw_base) {
7442 err = -ENODEV;
7443 goto out_pci_release_regions;
7444 }
7445
7446 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7447 (unsigned long long) pci_resource_len(pdev, 0));
7448 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7449
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007450 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7451 * PCI Tx retries from interfering with C3 CPU state */
7452 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07007453
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007454 /* amp init */
7455 err = priv->cfg->ops->lib->apm_ops.init(priv);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007456 if (err < 0) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007457 IWL_DEBUG_INFO("Failed to init APMG\n");
7458 goto out_iounmap;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007459 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007460
7461 /***********************
7462 * 4. Read EEPROM
7463 * ********************/
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007464
Zhu Yi5a669262008-01-14 17:46:18 -08007465 /* Read the EEPROM */
7466 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007467 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007468 IWL_ERR(priv, "Unable to init EEPROM\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007469 goto out_remove_sysfs;
7470 }
7471 /* MAC Address location in EEPROM same for 3945/4965 */
7472 get_eeprom_mac(priv, priv->mac_addr);
Johannes Berge1749612008-10-27 15:59:26 -07007473 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08007474 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7475
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007476 /***********************
7477 * 5. Setup HW Constants
7478 * ********************/
7479 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007480 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007481 IWL_ERR(priv, "failed to set hw settings\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007482 goto out_iounmap;
7483 }
7484
7485 /***********************
7486 * 6. Setup priv
7487 * ********************/
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007488
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007489 err = iwl3945_init_drv(priv);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007490 if (err) {
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007491 IWL_ERR(priv, "initializing driver failed\n");
7492 goto out_free_geos;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007493 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007494
Tomas Winkler978785a2008-12-19 10:37:31 +08007495 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
7496 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007497
7498 /***********************************
7499 * 7. Initialize Module Parameters
7500 * **********************************/
7501
7502 /* Initialize module parameter values here */
7503 /* Disable radio (SW RF KILL) via parameter when loading driver */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007504 if (iwl3945_mod_params.disable) {
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007505 set_bit(STATUS_RF_KILL_SW, &priv->status);
7506 IWL_DEBUG_INFO("Radio disabled.\n");
7507 }
7508
7509
7510 /***********************
7511 * 8. Setup Services
7512 * ********************/
7513
7514 spin_lock_irqsave(&priv->lock, flags);
7515 iwl3945_disable_interrupts(priv);
7516 spin_unlock_irqrestore(&priv->lock, flags);
7517
7518 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
7519 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007520 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeet90a30a02008-12-19 10:37:40 +08007521 goto out_release_irq;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007522 }
7523
7524 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
7525 iwl3945_setup_deferred_work(priv);
7526 iwl3945_setup_rx_handlers(priv);
7527
7528 /***********************
7529 * 9. Conclude
7530 * ********************/
7531 pci_save_state(pdev);
7532 pci_disable_device(pdev);
7533
7534 /*********************************
7535 * 10. Setup and Register mac80211
7536 * *******************************/
7537
Zhu Yi5a669262008-01-14 17:46:18 -08007538 err = ieee80211_register_hw(priv->hw);
7539 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007540 IWL_ERR(priv, "Failed to register network device: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007541 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07007542 }
7543
Zhu Yi5a669262008-01-14 17:46:18 -08007544 priv->hw->conf.beacon_int = 100;
7545 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007546
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007547 err = iwl3945_rfkill_init(priv);
7548 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08007549 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007550 "Ignoring error: %d\n", err);
7551
Zhu Yib481de92007-09-25 17:54:57 -07007552 return 0;
7553
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007554 out_remove_sysfs:
7555 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007556 out_free_geos:
7557 iwl3945_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007558
7559 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07007560 destroy_workqueue(priv->workqueue);
7561 priv->workqueue = NULL;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007562 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007563
7564 out_iounmap:
7565 pci_iounmap(pdev, priv->hw_base);
7566 out_pci_release_regions:
7567 pci_release_regions(pdev);
7568 out_pci_disable_device:
7569 pci_disable_device(pdev);
7570 pci_set_drvdata(pdev, NULL);
7571 out_ieee80211_free_hw:
7572 ieee80211_free_hw(priv->hw);
7573 out:
7574 return err;
7575}
7576
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007577static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007578{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007579 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007580 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007581
7582 if (!priv)
7583 return;
7584
7585 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7586
Zhu Yib481de92007-09-25 17:54:57 -07007587 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007588
Kolekar, Abhijeetd552bfb2008-12-19 10:37:41 +08007589 if (priv->mac80211_registered) {
7590 ieee80211_unregister_hw(priv->hw);
7591 priv->mac80211_registered = 0;
7592 } else {
7593 iwl3945_down(priv);
7594 }
Zhu Yib481de92007-09-25 17:54:57 -07007595
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007596 /* make sure we flush any pending irq or
7597 * tasklet for the driver
7598 */
7599 spin_lock_irqsave(&priv->lock, flags);
7600 iwl3945_disable_interrupts(priv);
7601 spin_unlock_irqrestore(&priv->lock, flags);
7602
7603 iwl_synchronize_irq(priv);
7604
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007605 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007606
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007607 iwl3945_rfkill_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007608 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007609
7610 if (priv->rxq.bd)
Winkler, Tomas51af3d32008-12-22 11:31:23 +08007611 iwl_rx_queue_free(priv, &priv->rxq);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007612 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007613
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007614 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007615 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007616
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08007617 /*netif_stop_queue(dev); */
7618 flush_workqueue(priv->workqueue);
7619
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007620 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007621 * priv->workqueue... so we can't take down the workqueue
7622 * until now... */
7623 destroy_workqueue(priv->workqueue);
7624 priv->workqueue = NULL;
7625
Zhu Yib481de92007-09-25 17:54:57 -07007626 pci_iounmap(pdev, priv->hw_base);
7627 pci_release_regions(pdev);
7628 pci_disable_device(pdev);
7629 pci_set_drvdata(pdev, NULL);
7630
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007631 iwl3945_free_channel_map(priv);
7632 iwl3945_free_geos(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007633 kfree(priv->scan39);
Zhu Yib481de92007-09-25 17:54:57 -07007634 if (priv->ibss_beacon)
7635 dev_kfree_skb(priv->ibss_beacon);
7636
7637 ieee80211_free_hw(priv->hw);
7638}
7639
7640#ifdef CONFIG_PM
7641
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007642static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007643{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007644 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007645
Zhu Yie655b9f2008-01-24 02:19:38 -08007646 if (priv->is_open) {
7647 set_bit(STATUS_IN_SUSPEND, &priv->status);
7648 iwl3945_mac_stop(priv->hw);
7649 priv->is_open = 1;
7650 }
Zhu Yib481de92007-09-25 17:54:57 -07007651
Zhu Yib481de92007-09-25 17:54:57 -07007652 pci_set_power_state(pdev, PCI_D3hot);
7653
Zhu Yib481de92007-09-25 17:54:57 -07007654 return 0;
7655}
7656
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007657static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007658{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007659 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007660
Zhu Yib481de92007-09-25 17:54:57 -07007661 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07007662
Zhu Yie655b9f2008-01-24 02:19:38 -08007663 if (priv->is_open)
7664 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07007665
Zhu Yie655b9f2008-01-24 02:19:38 -08007666 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007667 return 0;
7668}
7669
7670#endif /* CONFIG_PM */
7671
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007672/*************** RFKILL FUNCTIONS **********/
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007673#ifdef CONFIG_IWL3945_RFKILL
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007674/* software rf-kill from user */
7675static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
7676{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007677 struct iwl_priv *priv = data;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007678 int err = 0;
7679
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007680 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007681 return 0;
7682
7683 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7684 return 0;
7685
Tomas Winklera96a27f2008-10-23 23:48:56 -07007686 IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007687 mutex_lock(&priv->mutex);
7688
7689 switch (state) {
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007690 case RFKILL_STATE_UNBLOCKED:
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007691 if (iwl_is_rfkill_hw(priv)) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007692 err = -EBUSY;
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007693 goto out_unlock;
7694 }
7695 iwl3945_radio_kill_sw(priv, 0);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007696 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007697 case RFKILL_STATE_SOFT_BLOCKED:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007698 iwl3945_radio_kill_sw(priv, 1);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007699 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007700 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007701 IWL_WARN(priv, "received unexpected RFKILL state %d\n", state);
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007702 break;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007703 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007704out_unlock:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007705 mutex_unlock(&priv->mutex);
7706
7707 return err;
7708}
7709
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007710int iwl3945_rfkill_init(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007711{
7712 struct device *device = wiphy_dev(priv->hw->wiphy);
7713 int ret = 0;
7714
7715 BUG_ON(device == NULL);
7716
7717 IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007718 priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
7719 if (!priv->rfkill) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007720 IWL_ERR(priv, "Unable to allocate rfkill device.\n");
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007721 ret = -ENOMEM;
7722 goto error;
7723 }
7724
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007725 priv->rfkill->name = priv->cfg->name;
7726 priv->rfkill->data = priv;
7727 priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
7728 priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
7729 priv->rfkill->user_claim_unsupported = 1;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007730
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007731 priv->rfkill->dev.class->suspend = NULL;
7732 priv->rfkill->dev.class->resume = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007733
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007734 ret = rfkill_register(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007735 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007736 IWL_ERR(priv, "Unable to register rfkill: %d\n", ret);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007737 goto freed_rfkill;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007738 }
7739
7740 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
7741 return ret;
7742
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007743freed_rfkill:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007744 if (priv->rfkill != NULL)
7745 rfkill_free(priv->rfkill);
7746 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007747
7748error:
7749 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
7750 return ret;
7751}
7752
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007753void iwl3945_rfkill_unregister(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007754{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007755 if (priv->rfkill)
7756 rfkill_unregister(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007757
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007758 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007759}
7760
7761/* set rf-kill to the right state. */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007762void iwl3945_rfkill_set_hw_state(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007763{
7764
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007765 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007766 return;
7767
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007768 if (iwl_is_rfkill_hw(priv)) {
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007769 rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
7770 return;
7771 }
7772
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007773 if (!iwl_is_rfkill_sw(priv))
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007774 rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007775 else
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007776 rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007777}
7778#endif
7779
Zhu Yib481de92007-09-25 17:54:57 -07007780/*****************************************************************************
7781 *
7782 * driver and module entry point
7783 *
7784 *****************************************************************************/
7785
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007786static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07007787 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007788 .id_table = iwl3945_hw_card_ids,
7789 .probe = iwl3945_pci_probe,
7790 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07007791#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007792 .suspend = iwl3945_pci_suspend,
7793 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07007794#endif
7795};
7796
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007797static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07007798{
7799
7800 int ret;
7801 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
7802 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007803
7804 ret = iwl3945_rate_control_register();
7805 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007806 printk(KERN_ERR DRV_NAME
7807 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007808 return ret;
7809 }
7810
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007811 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07007812 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007813 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007814 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07007815 }
Zhu Yib481de92007-09-25 17:54:57 -07007816
7817 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007818
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007819error_register:
7820 iwl3945_rate_control_unregister();
7821 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007822}
7823
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007824static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07007825{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007826 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07007827 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07007828}
7829
Reinette Chatrea0987a82008-12-02 12:14:06 -08007830MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08007831
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007832module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007833MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007834module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007835MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007836module_param_named(hwcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007837MODULE_PARM_DESC(hwcrypto,
7838 "using hardware crypto engine (default 0 [software])\n");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007839module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007840MODULE_PARM_DESC(debug, "debug output mask");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007841module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007842MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
7843
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007844module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07007845MODULE_PARM_DESC(queues_num, "number of hw queues.");
7846
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007847module_exit(iwl3945_exit);
7848module_init(iwl3945_init);