blob: 52c03cf2908939e0e2c74d71de5e498c3637e088 [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
Tomas Winkler600c0e12008-12-19 10:37:04 +080051#include "iwl-commands.h"
Zhu Yib481de92007-09-25 17:54:57 -070052#include "iwl-3945.h"
Tomas Winklerbddadf82008-12-19 10:37:01 +080053#include "iwl-3945-fh.h"
Zhu Yib481de92007-09-25 17:54:57 -070054#include "iwl-helpers.h"
Kolekar, Abhijeet5747d472008-12-19 10:37:18 +080055#include "iwl-core.h"
Samuel Ortizd20b3c62008-12-19 10:37:15 +080056#include "iwl-dev.h"
Zhu Yib481de92007-09-25 17:54:57 -070057
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +080058static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080059 struct iwl3945_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080060
Zhu Yib481de92007-09-25 17:54:57 -070061/*
62 * module name, copyright, version, etc.
Zhu Yib481de92007-09-25 17:54:57 -070063 */
64
65#define DRV_DESCRIPTION \
66"Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
67
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080068#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070069#define VD "d"
70#else
71#define VD
72#endif
73
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080074#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070075#define VS "s"
76#else
77#define VS
78#endif
79
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080080#define IWL39_VERSION "1.2.26k" VD VS
Reinette Chatreeb7ae892008-03-11 16:17:17 -070081#define DRV_COPYRIGHT "Copyright(c) 2003-2008 Intel Corporation"
Tomas Winklera7b75202008-12-11 10:33:41 -080082#define DRV_AUTHOR "<ilw@linux.intel.com>"
Kolekar, Abhijeeteaa686c2008-12-19 10:37:17 +080083#define DRV_VERSION IWL39_VERSION
Zhu Yib481de92007-09-25 17:54:57 -070084
Zhu Yib481de92007-09-25 17:54:57 -070085
86MODULE_DESCRIPTION(DRV_DESCRIPTION);
87MODULE_VERSION(DRV_VERSION);
Tomas Winklera7b75202008-12-11 10:33:41 -080088MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
Zhu Yib481de92007-09-25 17:54:57 -070089MODULE_LICENSE("GPL");
90
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +080091 /* module parameters */
92struct iwl_mod_params iwl3945_mod_params = {
93 .num_of_queues = IWL39_MAX_NUM_QUEUES,
94 /* the rest are 0 by default */
95};
96
Johannes Berg8318d782008-01-24 19:38:38 +010097static const struct ieee80211_supported_band *iwl3945_get_band(
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +080098 struct iwl_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -070099{
Johannes Berg8318d782008-01-24 19:38:38 +0100100 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700101}
102
Zhu Yib481de92007-09-25 17:54:57 -0700103/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
104 * DMA services
105 *
106 * Theory of operation
107 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800108 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
109 * of buffer descriptors, each of which points to one or more data buffers for
110 * the device to read from or fill. Driver and device exchange status of each
111 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
112 * entries in each circular buffer, to protect against confusing empty and full
113 * queue states.
114 *
115 * The device reads or writes the data in the queues via the device's several
116 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700117 *
118 * For Tx queue, there are low mark and high mark limits. If, after queuing
119 * the packet for Tx, free space become < low mark, Tx queue stopped. When
120 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
121 * Tx queue resumed.
122 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800123 * The 3945 operates with six queues: One receive queue, one transmit queue
124 * (#4) for sending commands to the device firmware, and four transmit queues
125 * (#0-3) for data tx via EDCA. An additional 2 HCCA queues are unused.
Zhu Yib481de92007-09-25 17:54:57 -0700126 ***************************************************/
127
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800128int iwl3945_x2_queue_used(const struct iwl_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700129{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800130 return q->write_ptr > q->read_ptr ?
131 (i >= q->read_ptr && i < q->write_ptr) :
132 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700133}
134
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800135/**
136 * iwl3945_queue_init - Initialize queue's high/low-water and read/write indexes
137 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800138static int iwl3945_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700139 int count, int slots_num, u32 id)
140{
141 q->n_bd = count;
142 q->n_window = slots_num;
143 q->id = id;
144
Tomas Winklerc54b6792008-03-06 17:36:53 -0800145 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
146 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700147 BUG_ON(!is_power_of_2(count));
148
149 /* slots_num must be power-of-two size, otherwise
150 * get_cmd_index is broken. */
151 BUG_ON(!is_power_of_2(slots_num));
152
153 q->low_mark = q->n_window / 4;
154 if (q->low_mark < 4)
155 q->low_mark = 4;
156
157 q->high_mark = q->n_window / 8;
158 if (q->high_mark < 2)
159 q->high_mark = 2;
160
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800161 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700162
163 return 0;
164}
165
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800166/**
167 * iwl3945_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
168 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800169static int iwl3945_tx_queue_alloc(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800170 struct iwl3945_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700171{
172 struct pci_dev *dev = priv->pci_dev;
173
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800174 /* Driver private data, only for Tx (not command) queues,
175 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700176 if (id != IWL_CMD_QUEUE_NUM) {
177 txq->txb = kmalloc(sizeof(txq->txb[0]) *
178 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
179 if (!txq->txb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800180 IWL_ERR(priv, "kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700181 "structures failed\n");
182 goto error;
183 }
184 } else
185 txq->txb = NULL;
186
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800187 /* Circular buffer of transmit frame descriptors (TFDs),
188 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700189 txq->bd = pci_alloc_consistent(dev,
190 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
191 &txq->q.dma_addr);
192
193 if (!txq->bd) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800194 IWL_ERR(priv, "pci_alloc_consistent(%zd) failed\n",
Zhu Yib481de92007-09-25 17:54:57 -0700195 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
196 goto error;
197 }
198 txq->q.id = id;
199
200 return 0;
201
202 error:
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300203 kfree(txq->txb);
204 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700205
206 return -ENOMEM;
207}
208
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800209/**
210 * iwl3945_tx_queue_init - Allocate and initialize one tx/cmd queue
211 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800212int iwl3945_tx_queue_init(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800213 struct iwl3945_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700214{
215 struct pci_dev *dev = priv->pci_dev;
216 int len;
217 int rc = 0;
218
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800219 /*
220 * Alloc buffer array for commands (Tx or other types of commands).
221 * For the command queue (#4), allocate command space + one big
222 * command for scan, since scan command is very huge; the system will
223 * not have two scans at the same time, so only one is needed.
224 * For data Tx queues (all other queues), no super-size command
225 * space is needed.
226 */
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800227 len = sizeof(struct iwl_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700228 if (txq_id == IWL_CMD_QUEUE_NUM)
229 len += IWL_MAX_SCAN_SIZE;
230 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
231 if (!txq->cmd)
232 return -ENOMEM;
233
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800234 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800235 rc = iwl3945_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700236 if (rc) {
237 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
238
239 return -ENOMEM;
240 }
241 txq->need_update = 0;
242
243 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800244 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700245 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800246
247 /* Initialize queue high/low-water, head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800248 iwl3945_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700249
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800250 /* Tell device where to find queue, enable DMA channel. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800251 iwl3945_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700252
253 return 0;
254}
255
256/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800257 * iwl3945_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700258 * @txq: Transmit queue to deallocate.
259 *
260 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800261 * Free all buffers.
262 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700263 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800264void iwl3945_tx_queue_free(struct iwl_priv *priv, struct iwl3945_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700265{
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800266 struct iwl_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700267 struct pci_dev *dev = priv->pci_dev;
268 int len;
269
270 if (q->n_bd == 0)
271 return;
272
273 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800274 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800275 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800276 iwl3945_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700277
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800278 len = sizeof(struct iwl_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700279 if (q->id == IWL_CMD_QUEUE_NUM)
280 len += IWL_MAX_SCAN_SIZE;
281
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800282 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700283 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
284
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800285 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700286 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800287 pci_free_consistent(dev, sizeof(struct iwl3945_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700288 txq->q.n_bd, txq->bd, txq->q.dma_addr);
289
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800290 /* De-alloc array of per-TFD driver data */
Tomas Winkler3ac7f142008-07-21 02:40:14 +0300291 kfree(txq->txb);
292 txq->txb = NULL;
Zhu Yib481de92007-09-25 17:54:57 -0700293
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800294 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700295 memset(txq, 0, sizeof(*txq));
296}
297
Zhu Yib481de92007-09-25 17:54:57 -0700298/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800299 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700300 * the functionality provided here
301 */
302
303/**************************************************************/
Ian Schram01ebd062007-10-25 17:15:22 +0800304#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800305/**
306 * iwl3945_remove_station - Remove driver's knowledge of station.
307 *
308 * NOTE: This does not remove station from device's station table.
309 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800310static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700311{
312 int index = IWL_INVALID_STATION;
313 int i;
314 unsigned long flags;
315
316 spin_lock_irqsave(&priv->sta_lock, flags);
317
318 if (is_ap)
319 index = IWL_AP_ID;
320 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800321 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700322 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800323 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800324 if (priv->stations_39[i].used &&
325 !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700326 addr)) {
327 index = i;
328 break;
329 }
330
331 if (unlikely(index == IWL_INVALID_STATION))
332 goto out;
333
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800334 if (priv->stations_39[index].used) {
335 priv->stations_39[index].used = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700336 priv->num_stations--;
337 }
338
339 BUG_ON(priv->num_stations < 0);
340
341out:
342 spin_unlock_irqrestore(&priv->sta_lock, flags);
343 return 0;
344}
Zhu Yi556f8db2007-09-27 11:27:33 +0800345#endif
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800346
347/**
348 * iwl3945_clear_stations_table - Clear the driver's station table
349 *
350 * NOTE: This does not clear or otherwise alter the device's station table.
351 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800352static void iwl3945_clear_stations_table(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700353{
354 unsigned long flags;
355
356 spin_lock_irqsave(&priv->sta_lock, flags);
357
358 priv->num_stations = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800359 memset(priv->stations_39, 0, sizeof(priv->stations_39));
Zhu Yib481de92007-09-25 17:54:57 -0700360
361 spin_unlock_irqrestore(&priv->sta_lock, flags);
362}
363
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800364/**
365 * iwl3945_add_station - Add station to station tables in driver and device
366 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800367u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700368{
369 int i;
370 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800371 struct iwl3945_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700372 unsigned long flags_spin;
Zhu Yic14c5212007-09-27 11:27:35 +0800373 u8 rate;
Zhu Yib481de92007-09-25 17:54:57 -0700374
375 spin_lock_irqsave(&priv->sta_lock, flags_spin);
376 if (is_ap)
377 index = IWL_AP_ID;
378 else if (is_broadcast_ether_addr(addr))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800379 index = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700380 else
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +0800381 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800382 if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
Zhu Yib481de92007-09-25 17:54:57 -0700383 addr)) {
384 index = i;
385 break;
386 }
387
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800388 if (!priv->stations_39[i].used &&
Zhu Yib481de92007-09-25 17:54:57 -0700389 index == IWL_INVALID_STATION)
390 index = i;
391 }
392
Ian Schram01ebd062007-10-25 17:15:22 +0800393 /* These two conditions has the same outcome but keep them separate
Zhu Yib481de92007-09-25 17:54:57 -0700394 since they have different meaning */
395 if (unlikely(index == IWL_INVALID_STATION)) {
396 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
397 return index;
398 }
399
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800400 if (priv->stations_39[index].used &&
401 !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
Zhu Yib481de92007-09-25 17:54:57 -0700402 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
403 return index;
404 }
405
Johannes Berge1749612008-10-27 15:59:26 -0700406 IWL_DEBUG_ASSOC("Add STA ID %d: %pM\n", index, addr);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800407 station = &priv->stations_39[index];
Zhu Yib481de92007-09-25 17:54:57 -0700408 station->used = 1;
409 priv->num_stations++;
410
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800411 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800412 memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700413 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
414 station->sta.mode = 0;
415 station->sta.sta.sta_id = index;
416 station->sta.station_flags = 0;
417
Johannes Berg8318d782008-01-24 19:38:38 +0100418 if (priv->band == IEEE80211_BAND_5GHZ)
Tomas Winkler69946332007-10-25 17:15:27 +0800419 rate = IWL_RATE_6M_PLCP;
420 else
421 rate = IWL_RATE_1M_PLCP;
Zhu Yic14c5212007-09-27 11:27:35 +0800422
423 /* Turn on both antennas for the station... */
424 station->sta.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800425 iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
Zhu Yic14c5212007-09-27 11:27:35 +0800426
Zhu Yib481de92007-09-25 17:54:57 -0700427 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800428
429 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800430 iwl3945_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700431 return index;
432
433}
434
Zhu Yib481de92007-09-25 17:54:57 -0700435
436/*************** HOST COMMAND QUEUE FUNCTIONS *****/
437
Abhijeet Kolekarc3056062008-11-12 13:14:08 -0800438#define IWL_CMD(x) case x: return #x
Zhu Yib481de92007-09-25 17:54:57 -0700439#define HOST_COMPLETE_TIMEOUT (HZ / 2)
440
441/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800442 * iwl3945_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700443 * @priv: device private data point
444 * @cmd: a point to the ucode command structure
445 *
446 * The function returns < 0 values to indicate the operation is
447 * failed. On success, it turns the index (> 0) of command in the
448 * command queue.
449 */
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800450static int iwl3945_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700451{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800452 struct iwl3945_tx_queue *txq = &priv->txq39[IWL_CMD_QUEUE_NUM];
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800453 struct iwl_queue *q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800454 struct iwl3945_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700455 u32 *control_flags;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800456 struct iwl_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700457 u32 idx;
458 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
459 dma_addr_t phys_addr;
460 int pad;
461 u16 count;
462 int ret;
463 unsigned long flags;
464
465 /* If any of the command structures end up being larger than
466 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
467 * we will need to increase the size of the TFD entries */
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800468 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
Zhu Yib481de92007-09-25 17:54:57 -0700469 !(cmd->meta.flags & CMD_SIZE_HUGE));
470
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800471
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800472 if (iwl_is_rfkill(priv)) {
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800473 IWL_DEBUG_INFO("Not sending command - RF KILL");
474 return -EIO;
475 }
476
Samuel Ortizd20b3c62008-12-19 10:37:15 +0800477 if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800478 IWL_ERR(priv, "No space for Tx\n");
Zhu Yib481de92007-09-25 17:54:57 -0700479 return -ENOSPC;
480 }
481
482 spin_lock_irqsave(&priv->hcmd_lock, flags);
483
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800484 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700485 memset(tfd, 0, sizeof(*tfd));
486
487 control_flags = (u32 *) tfd;
488
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800489 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700490 out_cmd = &txq->cmd[idx];
491
492 out_cmd->hdr.cmd = cmd->id;
493 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
494 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
495
496 /* At this point, the out_cmd now has all of the incoming cmd
497 * information */
498
499 out_cmd->hdr.flags = 0;
500 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800501 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700502 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
Tomas Winkler600c0e12008-12-19 10:37:04 +0800503 out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
Zhu Yib481de92007-09-25 17:54:57 -0700504
505 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800506 offsetof(struct iwl_cmd, hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800507 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700508
509 pad = U32_PAD(cmd->len);
510 count = TFD_CTL_COUNT_GET(*control_flags);
511 *control_flags = TFD_CTL_COUNT_SET(count) | TFD_CTL_PAD_SET(pad);
512
513 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
514 "%d bytes at %d[%d]:%d\n",
515 get_cmd_string(out_cmd->hdr.cmd),
516 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800517 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700518
519 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800520
521 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800522 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800523 ret = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700524
525 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
526 return ret ? ret : idx;
527}
528
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800529static int iwl3945_send_cmd_async(struct iwl_priv *priv,
530 struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700531{
532 int ret;
533
534 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
535
536 /* An asynchronous command can not expect an SKB to be set. */
537 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
538
539 /* An asynchronous command MUST have a callback. */
540 BUG_ON(!cmd->meta.u.callback);
541
542 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
543 return -EBUSY;
544
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800545 ret = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700546 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800547 IWL_ERR(priv,
548 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
549 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700550 return ret;
551 }
552 return 0;
553}
554
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800555static int iwl3945_send_cmd_sync(struct iwl_priv *priv,
556 struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700557{
558 int cmd_idx;
559 int ret;
Zhu Yib481de92007-09-25 17:54:57 -0700560
561 BUG_ON(cmd->meta.flags & CMD_ASYNC);
562
563 /* A synchronous command can not have a callback set. */
564 BUG_ON(cmd->meta.u.callback != NULL);
565
Tomas Winklere5472972008-03-28 16:21:12 -0700566 if (test_and_set_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800567 IWL_ERR(priv,
568 "Error sending %s: Already sending a host command\n",
569 get_cmd_string(cmd->id));
Tomas Winklere5472972008-03-28 16:21:12 -0700570 ret = -EBUSY;
571 goto out;
Zhu Yib481de92007-09-25 17:54:57 -0700572 }
573
574 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
575
576 if (cmd->meta.flags & CMD_WANT_SKB)
577 cmd->meta.source = &cmd->meta;
578
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800579 cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700580 if (cmd_idx < 0) {
581 ret = cmd_idx;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800582 IWL_ERR(priv,
583 "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
584 get_cmd_string(cmd->id), ret);
Zhu Yib481de92007-09-25 17:54:57 -0700585 goto out;
586 }
587
588 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
589 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
590 HOST_COMPLETE_TIMEOUT);
591 if (!ret) {
592 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800593 IWL_ERR(priv, "Error sending %s: time out after %dms\n",
Zhu Yib481de92007-09-25 17:54:57 -0700594 get_cmd_string(cmd->id),
595 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
596
597 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
598 ret = -ETIMEDOUT;
599 goto cancel;
600 }
601 }
602
603 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
604 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
605 get_cmd_string(cmd->id));
606 ret = -ECANCELED;
607 goto fail;
608 }
609 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
610 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
611 get_cmd_string(cmd->id));
612 ret = -EIO;
613 goto fail;
614 }
615 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800616 IWL_ERR(priv, "Error: Response NULL in '%s'\n",
Zhu Yib481de92007-09-25 17:54:57 -0700617 get_cmd_string(cmd->id));
618 ret = -EIO;
Zhu Yi73e1a652009-01-08 10:19:58 -0800619 goto cancel;
Zhu Yib481de92007-09-25 17:54:57 -0700620 }
621
622 ret = 0;
623 goto out;
624
625cancel:
626 if (cmd->meta.flags & CMD_WANT_SKB) {
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800627 struct iwl_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700628
629 /* Cancel the CMD_WANT_SKB flag for the cmd in the
630 * TX cmd queue. Otherwise in case the cmd comes
631 * in later, it will possibly set an invalid
632 * address (cmd->meta.source). */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800633 qcmd = &priv->txq39[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
Zhu Yib481de92007-09-25 17:54:57 -0700634 qcmd->meta.flags &= ~CMD_WANT_SKB;
635 }
636fail:
637 if (cmd->meta.u.skb) {
638 dev_kfree_skb_any(cmd->meta.u.skb);
639 cmd->meta.u.skb = NULL;
640 }
641out:
Tomas Winklere5472972008-03-28 16:21:12 -0700642 clear_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -0700643 return ret;
644}
645
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800646int iwl3945_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700647{
Zhu Yib481de92007-09-25 17:54:57 -0700648 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800649 return iwl3945_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700650
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800651 return iwl3945_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700652}
653
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800654int iwl3945_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700655{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800656 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700657 .id = id,
658 .len = len,
659 .data = data,
660 };
661
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800662 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700663}
664
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800665static int __must_check iwl3945_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700666{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800667 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700668 .id = id,
669 .len = sizeof(val),
670 .data = &val,
671 };
672
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800673 return iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700674}
675
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800676int iwl3945_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700677{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800678 return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700679}
680
681/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800682 * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
Johannes Berg8318d782008-01-24 19:38:38 +0100683 * @band: 2.4 or 5 GHz band
684 * @channel: Any channel valid for the requested band
Zhu Yib481de92007-09-25 17:54:57 -0700685
Johannes Berg8318d782008-01-24 19:38:38 +0100686 * In addition to setting the staging RXON, priv->band is also set.
Zhu Yib481de92007-09-25 17:54:57 -0700687 *
688 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
Johannes Berg8318d782008-01-24 19:38:38 +0100689 * in the staging RXON flag structure based on the band
Zhu Yib481de92007-09-25 17:54:57 -0700690 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800691static int iwl3945_set_rxon_channel(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100692 enum ieee80211_band band,
693 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700694{
Johannes Berg8318d782008-01-24 19:38:38 +0100695 if (!iwl3945_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700696 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100697 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700698 return -EINVAL;
699 }
700
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800701 if ((le16_to_cpu(priv->staging39_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100702 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700703 return 0;
704
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800705 priv->staging39_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100706 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800707 priv->staging39_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700708 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800709 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700710
Johannes Berg8318d782008-01-24 19:38:38 +0100711 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700712
Johannes Berg8318d782008-01-24 19:38:38 +0100713 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700714
715 return 0;
716}
717
718/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800719 * iwl3945_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700720 *
721 * NOTE: This is really only useful during development and can eventually
722 * be #ifdef'd out once the driver is stable and folks aren't actively
723 * making changes
724 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800725static int iwl3945_check_rxon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700726{
727 int error = 0;
728 int counter = 1;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800729 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700730
731 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
732 error |= le32_to_cpu(rxon->flags &
733 (RXON_FLG_TGJ_NARROW_BAND_MSK |
734 RXON_FLG_RADAR_DETECT_MSK));
735 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800736 IWL_WARN(priv, "check 24G fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700737 counter++, error);
738 } else {
739 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
740 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
741 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800742 IWL_WARN(priv, "check 52 fields %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700743 counter++, error);
744 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
745 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800746 IWL_WARN(priv, "check 52 CCK %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700747 counter++, error);
748 }
749 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
750 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800751 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700752
753 /* make sure basic rates 6Mbps and 1Mbps are supported */
754 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
755 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
756 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800757 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700758
759 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
760 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800761 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700762
763 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
764 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
765 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800766 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700767 counter++, error);
768
769 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
770 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
771 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800772 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700773 counter++, error);
774
775 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
776 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
777 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800778 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700779 counter++, error);
780
781 if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
782 error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
783 RXON_FLG_ANT_A_MSK)) == 0);
784 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800785 IWL_WARN(priv, "check antenna %d %d\n", counter++, error);
Zhu Yib481de92007-09-25 17:54:57 -0700786
787 if (error)
Winkler, Tomas39aadf82008-12-19 10:37:32 +0800788 IWL_WARN(priv, "Tuning to channel %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700789 le16_to_cpu(rxon->channel));
790
791 if (error) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800792 IWL_ERR(priv, "Not a valid rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700793 return -1;
794 }
795 return 0;
796}
797
798/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800799 * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800800 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700801 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800802 * If the RXON structure is changing enough to require a new tune,
803 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
804 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700805 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800806static int iwl3945_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700807{
808
809 /* These items are only settable from the full RXON command */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +0800810 if (!(iwl3945_is_associated(priv)) ||
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800811 compare_ether_addr(priv->staging39_rxon.bssid_addr,
812 priv->active39_rxon.bssid_addr) ||
813 compare_ether_addr(priv->staging39_rxon.node_addr,
814 priv->active39_rxon.node_addr) ||
815 compare_ether_addr(priv->staging39_rxon.wlap_bssid_addr,
816 priv->active39_rxon.wlap_bssid_addr) ||
817 (priv->staging39_rxon.dev_type != priv->active39_rxon.dev_type) ||
818 (priv->staging39_rxon.channel != priv->active39_rxon.channel) ||
819 (priv->staging39_rxon.air_propagation !=
820 priv->active39_rxon.air_propagation) ||
821 (priv->staging39_rxon.assoc_id != priv->active39_rxon.assoc_id))
Zhu Yib481de92007-09-25 17:54:57 -0700822 return 1;
823
824 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
825 * be updated with the RXON_ASSOC command -- however only some
826 * flag transitions are allowed using RXON_ASSOC */
827
828 /* Check if we are not switching bands */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800829 if ((priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
830 (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700831 return 1;
832
833 /* Check if we are switching association toggle */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800834 if ((priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
835 (priv->active39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
Zhu Yib481de92007-09-25 17:54:57 -0700836 return 1;
837
838 return 0;
839}
840
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800841static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700842{
843 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800844 struct iwl_rx_packet *res = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800845 struct iwl3945_rxon_assoc_cmd rxon_assoc;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +0800846 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700847 .id = REPLY_RXON_ASSOC,
848 .len = sizeof(rxon_assoc),
849 .meta.flags = CMD_WANT_SKB,
850 .data = &rxon_assoc,
851 };
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800852 const struct iwl3945_rxon_cmd *rxon1 = &priv->staging39_rxon;
853 const struct iwl3945_rxon_cmd *rxon2 = &priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700854
855 if ((rxon1->flags == rxon2->flags) &&
856 (rxon1->filter_flags == rxon2->filter_flags) &&
857 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
858 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
859 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
860 return 0;
861 }
862
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800863 rxon_assoc.flags = priv->staging39_rxon.flags;
864 rxon_assoc.filter_flags = priv->staging39_rxon.filter_flags;
865 rxon_assoc.ofdm_basic_rates = priv->staging39_rxon.ofdm_basic_rates;
866 rxon_assoc.cck_basic_rates = priv->staging39_rxon.cck_basic_rates;
Zhu Yib481de92007-09-25 17:54:57 -0700867 rxon_assoc.reserved = 0;
868
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800869 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700870 if (rc)
871 return rc;
872
Tomas Winkler3d24a9f2008-12-19 10:37:07 +0800873 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -0700874 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800875 IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -0700876 rc = -EIO;
877 }
878
879 priv->alloc_rxb_skb--;
880 dev_kfree_skb_any(cmd.meta.u.skb);
881
882 return rc;
883}
884
885/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800886 * iwl3945_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -0700887 *
Ian Schram01ebd062007-10-25 17:15:22 +0800888 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -0700889 * the active_rxon structure is updated with the new data. This
890 * function correctly transitions out of the RXON_ASSOC_MSK state if
891 * a HW tune is required based on the RXON structure changes.
892 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +0800893static int iwl3945_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700894{
895 /* cast away the const for active_rxon in this function */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800896 struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -0700897 int rc = 0;
898
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +0800899 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700900 return -1;
901
902 /* always get timestamp with Rx frame */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800903 priv->staging39_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
Zhu Yib481de92007-09-25 17:54:57 -0700904
905 /* select antenna */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800906 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -0700907 ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800908 priv->staging39_rxon.flags |= iwl3945_get_antenna_flags(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700909
Samuel Ortiza3139c52008-12-19 10:37:09 +0800910 rc = iwl3945_check_rxon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700911 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800912 IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700913 return -EINVAL;
914 }
915
916 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800917 * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -0700918 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800919 if (!iwl3945_full_rxon_required(priv)) {
920 rc = iwl3945_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700921 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800922 IWL_ERR(priv, "Error setting RXON_ASSOC "
Zhu Yib481de92007-09-25 17:54:57 -0700923 "configuration (%d).\n", rc);
924 return rc;
925 }
926
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800927 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700928
929 return 0;
930 }
931
932 /* If we are currently associated and the new config requires
933 * an RXON_ASSOC and the new config wants the associated mask enabled,
934 * we must clear the associated from the active configuration
935 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800936 if (iwl3945_is_associated(priv) &&
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800937 (priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
Zhu Yib481de92007-09-25 17:54:57 -0700938 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
939 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
940
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800941 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
942 sizeof(struct iwl3945_rxon_cmd),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800943 &priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700944
945 /* If the mask clearing failed then we set
946 * active_rxon back to what it was previously */
947 if (rc) {
948 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
Winkler, Tomas15b16872008-12-19 10:37:33 +0800949 IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
Zhu Yib481de92007-09-25 17:54:57 -0700950 "configuration (%d).\n", rc);
951 return rc;
952 }
Zhu Yib481de92007-09-25 17:54:57 -0700953 }
954
955 IWL_DEBUG_INFO("Sending RXON\n"
956 "* with%s RXON_FILTER_ASSOC_MSK\n"
957 "* channel = %d\n"
Johannes Berge1749612008-10-27 15:59:26 -0700958 "* bssid = %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800959 ((priv->staging39_rxon.filter_flags &
Zhu Yib481de92007-09-25 17:54:57 -0700960 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800961 le16_to_cpu(priv->staging39_rxon.channel),
Johannes Berge1749612008-10-27 15:59:26 -0700962 priv->staging_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -0700963
964 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800965 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800966 sizeof(struct iwl3945_rxon_cmd), &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -0700967 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800968 IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700969 return rc;
970 }
971
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800972 memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
Zhu Yib481de92007-09-25 17:54:57 -0700973
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800974 iwl3945_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +0800975
Zhu Yib481de92007-09-25 17:54:57 -0700976 /* If we issue a new RXON command which required a tune then we must
977 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800978 rc = iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700979 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800980 IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -0700981 return rc;
982 }
983
984 /* Add the broadcast address so we can send broadcast frames */
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +0800985 if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -0700986 IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800987 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
Zhu Yib481de92007-09-25 17:54:57 -0700988 return -EIO;
989 }
990
991 /* If we have set the ASSOC_MSK and we are in BSS mode then
992 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800993 if (iwl3945_is_associated(priv) &&
Johannes Berg05c914f2008-09-11 00:01:58 +0200994 (priv->iw_mode == NL80211_IFTYPE_STATION))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +0800995 if (iwl3945_add_station(priv, priv->active39_rxon.bssid_addr, 1, 0)
Zhu Yib481de92007-09-25 17:54:57 -0700996 == IWL_INVALID_STATION) {
Winkler, Tomas15b16872008-12-19 10:37:33 +0800997 IWL_ERR(priv, "Error adding AP address for transmit\n");
Zhu Yib481de92007-09-25 17:54:57 -0700998 return -EIO;
999 }
1000
Johannes Berg8318d782008-01-24 19:38:38 +01001001 /* Init the hardware's rate fallback order based on the band */
Zhu Yib481de92007-09-25 17:54:57 -07001002 rc = iwl3945_init_hw_rate_table(priv);
1003 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001004 IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07001005 return -EIO;
1006 }
1007
1008 return 0;
1009}
1010
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001011static int iwl3945_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001012{
Tomas Winkler4c897252008-12-19 10:37:05 +08001013 struct iwl_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001014 .flags = 3,
1015 .lead_time = 0xAA,
1016 .max_kill = 1,
1017 .kill_ack_mask = 0,
1018 .kill_cts_mask = 0,
1019 };
1020
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001021 return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
Tomas Winkler4c897252008-12-19 10:37:05 +08001022 sizeof(bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001023}
1024
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001025static int iwl3945_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001026{
1027 int rc = 0;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001028 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001029 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001030 .id = REPLY_SCAN_ABORT_CMD,
1031 .meta.flags = CMD_WANT_SKB,
1032 };
1033
1034 /* If there isn't a scan actively going on in the hardware
1035 * then we are in between scan bands and not actually
1036 * actively scanning, so don't send the abort command */
1037 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1038 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1039 return 0;
1040 }
1041
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001042 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001043 if (rc) {
1044 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1045 return rc;
1046 }
1047
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001048 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001049 if (res->u.status != CAN_ABORT_STATUS) {
1050 /* The scan abort will return 1 for success or
1051 * 2 for "failure". A failure condition can be
1052 * due to simply not being in an active scan which
1053 * can occur if we send the scan abort before we
1054 * the microcode has notified us that a scan is
1055 * completed. */
1056 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1057 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1058 clear_bit(STATUS_SCAN_HW, &priv->status);
1059 }
1060
1061 dev_kfree_skb_any(cmd.meta.u.skb);
1062
1063 return rc;
1064}
1065
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001066static int iwl3945_card_state_sync_callback(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001067 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001068 struct sk_buff *skb)
1069{
1070 return 1;
1071}
1072
1073/*
1074 * CARD_STATE_CMD
1075 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001076 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001077 *
1078 * When in the 'enable' state the card operates as normal.
1079 * When in the 'disable' state, the card enters into a low power mode.
1080 * When in the 'halt' state, the card is shut down and must be fully
1081 * restarted to come back on.
1082 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001083static int iwl3945_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001084{
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001085 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001086 .id = REPLY_CARD_STATE_CMD,
1087 .len = sizeof(u32),
1088 .data = &flags,
1089 .meta.flags = meta_flag,
1090 };
1091
1092 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001093 cmd.meta.u.callback = iwl3945_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001094
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001095 return iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001096}
1097
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001098static int iwl3945_add_sta_sync_callback(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001099 struct iwl_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001100{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001101 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001102
1103 if (!skb) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001104 IWL_ERR(priv, "Error: Response NULL in REPLY_ADD_STA.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001105 return 1;
1106 }
1107
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001108 res = (struct iwl_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001109 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001110 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001111 res->hdr.flags);
1112 return 1;
1113 }
1114
1115 switch (res->u.add_sta.status) {
1116 case ADD_STA_SUCCESS_MSK:
1117 break;
1118 default:
1119 break;
1120 }
1121
1122 /* We didn't cache the SKB; let the caller free it */
1123 return 1;
1124}
1125
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001126int iwl3945_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001127 struct iwl3945_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001128{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001129 struct iwl_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001130 int rc = 0;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08001131 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001132 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001133 .len = sizeof(struct iwl3945_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001134 .meta.flags = flags,
1135 .data = sta,
1136 };
1137
1138 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001139 cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001140 else
1141 cmd.meta.flags |= CMD_WANT_SKB;
1142
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001143 rc = iwl3945_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001144
1145 if (rc || (flags & CMD_ASYNC))
1146 return rc;
1147
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08001148 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001149 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001150 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
Zhu Yib481de92007-09-25 17:54:57 -07001151 res->hdr.flags);
1152 rc = -EIO;
1153 }
1154
1155 if (rc == 0) {
1156 switch (res->u.add_sta.status) {
1157 case ADD_STA_SUCCESS_MSK:
1158 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1159 break;
1160 default:
1161 rc = -EIO;
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001162 IWL_WARN(priv, "REPLY_ADD_STA failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07001163 break;
1164 }
1165 }
1166
1167 priv->alloc_rxb_skb--;
1168 dev_kfree_skb_any(cmd.meta.u.skb);
1169
1170 return rc;
1171}
1172
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001173static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001174 struct ieee80211_key_conf *keyconf,
1175 u8 sta_id)
1176{
1177 unsigned long flags;
1178 __le16 key_flags = 0;
1179
1180 switch (keyconf->alg) {
1181 case ALG_CCMP:
1182 key_flags |= STA_KEY_FLG_CCMP;
1183 key_flags |= cpu_to_le16(
1184 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1185 key_flags &= ~STA_KEY_FLG_INVALID;
1186 break;
1187 case ALG_TKIP:
1188 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001189 default:
1190 return -EINVAL;
1191 }
1192 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001193 priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
1194 priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
1195 memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001196 keyconf->keylen);
1197
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001198 memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
Zhu Yib481de92007-09-25 17:54:57 -07001199 keyconf->keylen);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001200 priv->stations_39[sta_id].sta.key.key_flags = key_flags;
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
1204 spin_unlock_irqrestore(&priv->sta_lock, flags);
1205
1206 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001207 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001208 return 0;
1209}
1210
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001211static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001212{
1213 unsigned long flags;
1214
1215 spin_lock_irqsave(&priv->sta_lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001216 memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
1217 memset(&priv->stations_39[sta_id].sta.key, 0,
Tomas Winkler4c897252008-12-19 10:37:05 +08001218 sizeof(struct iwl4965_keyinfo));
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001219 priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1220 priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1221 priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001222 spin_unlock_irqrestore(&priv->sta_lock, flags);
1223
1224 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001225 iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001226 return 0;
1227}
1228
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001229static void iwl3945_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001230{
1231 struct list_head *element;
1232
1233 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1234 priv->frames_count);
1235
1236 while (!list_empty(&priv->free_frames)) {
1237 element = priv->free_frames.next;
1238 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001239 kfree(list_entry(element, struct iwl3945_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001240 priv->frames_count--;
1241 }
1242
1243 if (priv->frames_count) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08001244 IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
Zhu Yib481de92007-09-25 17:54:57 -07001245 priv->frames_count);
1246 priv->frames_count = 0;
1247 }
1248}
1249
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001250static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001251{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001252 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001253 struct list_head *element;
1254 if (list_empty(&priv->free_frames)) {
1255 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1256 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001257 IWL_ERR(priv, "Could not allocate frame!\n");
Zhu Yib481de92007-09-25 17:54:57 -07001258 return NULL;
1259 }
1260
1261 priv->frames_count++;
1262 return frame;
1263 }
1264
1265 element = priv->free_frames.next;
1266 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001267 return list_entry(element, struct iwl3945_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001268}
1269
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001270static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001271{
1272 memset(frame, 0, sizeof(*frame));
1273 list_add(&frame->list, &priv->free_frames);
1274}
1275
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001276unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001277 struct ieee80211_hdr *hdr,
Rami Rosen73ec1cc2008-12-16 09:37:07 +02001278 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001279{
1280
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001281 if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
Johannes Berg05c914f2008-09-11 00:01:58 +02001282 ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
1283 (priv->iw_mode != NL80211_IFTYPE_AP)))
Zhu Yib481de92007-09-25 17:54:57 -07001284 return 0;
1285
1286 if (priv->ibss_beacon->len > left)
1287 return 0;
1288
1289 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1290
1291 return priv->ibss_beacon->len;
1292}
1293
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001294static u8 iwl3945_rate_get_lowest_plcp(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001295{
1296 u8 i;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001297 int rate_mask;
1298
1299 /* Set rate mask*/
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001300 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001301 rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001302 else
Chatre, Reinettedbce56a2008-11-12 13:14:07 -08001303 rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001304
1305 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001306 i = iwl3945_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001307 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001308 return iwl3945_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001309 }
1310
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001311 /* No valid rate was found. Assign the lowest one */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001312 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001313 return IWL_RATE_1M_PLCP;
1314 else
1315 return IWL_RATE_6M_PLCP;
Zhu Yib481de92007-09-25 17:54:57 -07001316}
1317
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001318static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001319{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001320 struct iwl3945_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001321 unsigned int frame_size;
1322 int rc;
1323 u8 rate;
1324
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001325 frame = iwl3945_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001326
1327 if (!frame) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001328 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
Zhu Yib481de92007-09-25 17:54:57 -07001329 "command.\n");
1330 return -ENOMEM;
1331 }
1332
Kolekar, Abhijeetc24f0812008-11-07 09:58:44 -08001333 rate = iwl3945_rate_get_lowest_plcp(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001334
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001335 frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001336
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001337 rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001338 &frame->u.cmd[0]);
1339
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001340 iwl3945_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001341
1342 return rc;
1343}
1344
1345/******************************************************************************
1346 *
1347 * EEPROM related functions
1348 *
1349 ******************************************************************************/
1350
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001351static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001352{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001353 memcpy(mac, priv->eeprom39.mac_address, 6);
Zhu Yib481de92007-09-25 17:54:57 -07001354}
1355
Reinette Chatre74a3a252008-01-23 10:15:19 -08001356/*
1357 * Clear the OWNER_MSK, to establish driver (instead of uCode running on
1358 * embedded controller) as EEPROM reader; each read is a series of pulses
1359 * to/from the EEPROM chip, not a single event, so even reads could conflict
1360 * if they weren't arbitrated by some ownership mechanism. Here, the driver
1361 * simply claims ownership, which should be safe when this function is called
1362 * (i.e. before loading uCode!).
1363 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001364static inline int iwl3945_eeprom_acquire_semaphore(struct iwl_priv *priv)
Reinette Chatre74a3a252008-01-23 10:15:19 -08001365{
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001366 _iwl_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
Reinette Chatre74a3a252008-01-23 10:15:19 -08001367 return 0;
1368}
1369
Zhu Yib481de92007-09-25 17:54:57 -07001370/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001371 * iwl3945_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001372 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001373 * Load the EEPROM contents from adapter into priv->eeprom39
Zhu Yib481de92007-09-25 17:54:57 -07001374 *
1375 * NOTE: This routine uses the non-debug IO access functions.
1376 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001377int iwl3945_eeprom_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001378{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001379 u16 *e = (u16 *)&priv->eeprom39;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001380 u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001381 int sz = sizeof(priv->eeprom39);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001382 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07001383 u16 addr;
1384
1385 /* The EEPROM structure has several padding buffers within it
1386 * and when adding new EEPROM maps is subject to programmer errors
1387 * which may be very difficult to identify without explicitly
1388 * checking the resulting size of the eeprom map. */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001389 BUILD_BUG_ON(sizeof(priv->eeprom39) != IWL_EEPROM_IMAGE_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07001390
1391 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001392 IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
Zhu Yib481de92007-09-25 17:54:57 -07001393 return -ENOENT;
1394 }
1395
Cahill, Ben M6440adb2007-11-29 11:09:55 +08001396 /* Make sure driver (instead of uCode) is allowed to read EEPROM */
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001397 ret = iwl3945_eeprom_acquire_semaphore(priv);
1398 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001399 IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001400 return -ENOENT;
1401 }
1402
1403 /* eeprom is an array of 16bit values */
1404 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001405 u32 r;
1406
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001407 _iwl_write32(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001408 CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001409 _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1410 ret = iwl_poll_direct_bit(priv, CSR_EEPROM_REG,
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001411 CSR_EEPROM_REG_READ_VALID_MSK,
1412 IWL_EEPROM_ACCESS_TIMEOUT);
1413 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08001414 IWL_ERR(priv, "Time out reading EEPROM[%d]\n", addr);
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001415 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07001416 }
Zhu, Yi3d5717a2008-12-11 10:33:36 -08001417
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08001418 r = _iwl_read_direct32(priv, CSR_EEPROM_REG);
Tomas Winkler58ff6d42008-02-13 02:47:54 +02001419 e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
Zhu Yib481de92007-09-25 17:54:57 -07001420 }
1421
1422 return 0;
1423}
1424
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001425static void iwl3945_unset_hw_params(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001426{
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001427 if (priv->shared_virt)
Zhu Yib481de92007-09-25 17:54:57 -07001428 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001429 sizeof(struct iwl3945_shared),
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08001430 priv->shared_virt,
1431 priv->shared_phys);
Zhu Yib481de92007-09-25 17:54:57 -07001432}
1433
1434/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001435 * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001436 *
1437 * return : set the bit for each supported rate insert in ie
1438 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001439static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001440 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001441{
1442 u16 ret_rates = 0, bit;
1443 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001444 u8 *cnt = ie;
1445 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001446
1447 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1448 if (bit & supported_rate) {
1449 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001450 rates[*cnt] = iwl3945_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001451 ((bit & basic_rate) ? 0x80 : 0x00);
1452 (*cnt)++;
1453 (*left)--;
1454 if ((*left <= 0) ||
1455 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001456 break;
1457 }
1458 }
1459
1460 return ret_rates;
1461}
1462
1463/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001464 * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001465 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001466static u16 iwl3945_fill_probe_req(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001467 struct ieee80211_mgmt *frame,
Johannes Berg430cfe92008-10-28 18:06:02 +01001468 int left)
Zhu Yib481de92007-09-25 17:54:57 -07001469{
1470 int len = 0;
1471 u8 *pos = NULL;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001472 u16 active_rates, ret_rates, cck_rates;
Zhu Yib481de92007-09-25 17:54:57 -07001473
1474 /* Make sure there is enough space for the probe request,
1475 * two mandatory IEs and the data */
1476 left -= 24;
1477 if (left < 0)
1478 return 0;
1479 len += 24;
1480
1481 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001482 memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001483 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08001484 memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001485 frame->seq_ctrl = 0;
1486
1487 /* fill in our indirect SSID IE */
1488 /* ...next IE... */
1489
1490 left -= 2;
1491 if (left < 0)
1492 return 0;
1493 len += 2;
1494 pos = &(frame->u.probe_req.variable[0]);
1495 *pos++ = WLAN_EID_SSID;
1496 *pos++ = 0;
1497
Zhu Yib481de92007-09-25 17:54:57 -07001498 /* fill in supported rate */
1499 /* ...next IE... */
1500 left -= 2;
1501 if (left < 0)
1502 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001503
Zhu Yib481de92007-09-25 17:54:57 -07001504 /* ... fill it in... */
1505 *pos++ = WLAN_EID_SUPP_RATES;
1506 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001507
1508 priv->active_rate = priv->rates_mask;
1509 active_rates = priv->active_rate;
Zhu Yib481de92007-09-25 17:54:57 -07001510 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
1511
Tomas Winklerc7c46672007-10-18 02:04:15 +02001512 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001513 ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001514 priv->active_rate_basic, &left);
1515 active_rates &= ~ret_rates;
1516
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001517 ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001518 priv->active_rate_basic, &left);
1519 active_rates &= ~ret_rates;
1520
Zhu Yib481de92007-09-25 17:54:57 -07001521 len += 2 + *pos;
1522 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001523 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001524 goto fill_end;
1525
1526 /* fill in supported extended rate */
1527 /* ...next IE... */
1528 left -= 2;
1529 if (left < 0)
1530 return 0;
1531 /* ... fill it in... */
1532 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1533 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001534 iwl3945_supported_rate_to_ie(pos, active_rates,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001535 priv->active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001536 if (*pos > 0)
1537 len += 2 + *pos;
1538
1539 fill_end:
1540 return (u16)len;
1541}
1542
1543/*
1544 * QoS support
1545*/
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001546static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
Tomas Winkler4c897252008-12-19 10:37:05 +08001547 struct iwl_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001548{
1549
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001550 return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
Tomas Winkler4c897252008-12-19 10:37:05 +08001551 sizeof(struct iwl_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001552}
1553
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001554static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001555{
1556 unsigned long flags;
1557
Zhu Yib481de92007-09-25 17:54:57 -07001558 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1559 return;
1560
Zhu Yib481de92007-09-25 17:54:57 -07001561 spin_lock_irqsave(&priv->lock, flags);
1562 priv->qos_data.def_qos_parm.qos_flags = 0;
1563
1564 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1565 !priv->qos_data.qos_cap.q_AP.txop_request)
1566 priv->qos_data.def_qos_parm.qos_flags |=
1567 QOS_PARAM_FLG_TXOP_TYPE_MSK;
1568
1569 if (priv->qos_data.qos_active)
1570 priv->qos_data.def_qos_parm.qos_flags |=
1571 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1572
1573 spin_unlock_irqrestore(&priv->lock, flags);
1574
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001575 if (force || iwl3945_is_associated(priv)) {
Tomas Winklera96a27f2008-10-23 23:48:56 -07001576 IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
Zhu Yib481de92007-09-25 17:54:57 -07001577 priv->qos_data.qos_active);
1578
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001579 iwl3945_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001580 &(priv->qos_data.def_qos_parm));
1581 }
1582}
1583
Zhu Yib481de92007-09-25 17:54:57 -07001584/*
1585 * Power management (not Tx power!) functions
1586 */
1587#define MSEC_TO_USEC 1024
1588
Tomas Winkler600c0e12008-12-19 10:37:04 +08001589
1590#define NOSLP __constant_cpu_to_le16(0), 0, 0
1591#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
Zhu Yib481de92007-09-25 17:54:57 -07001592#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1593#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1594 __constant_cpu_to_le32(X1), \
1595 __constant_cpu_to_le32(X2), \
1596 __constant_cpu_to_le32(X3), \
1597 __constant_cpu_to_le32(X4)}
1598
Zhu Yib481de92007-09-25 17:54:57 -07001599/* default power management (not Tx power) table values */
Tomas Winklera96a27f2008-10-23 23:48:56 -07001600/* for TIM 0-10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001601static struct iwl_power_vec_entry range_0[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001602 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1603 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1604 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1605 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1606 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1607 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1608};
1609
Tomas Winklera96a27f2008-10-23 23:48:56 -07001610/* for TIM > 10 */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001611static struct iwl_power_vec_entry range_1[IWL39_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001612 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1613 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1614 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1615 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1616 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1617 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1618 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1619 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1620 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1621 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1622};
1623
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001624int iwl3945_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001625{
1626 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001627 struct iwl3945_power_mgr *pow_data;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001628 int size = sizeof(struct iwl_power_vec_entry) * IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001629 u16 pci_pm;
1630
1631 IWL_DEBUG_POWER("Initialize power \n");
1632
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001633 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001634
1635 memset(pow_data, 0, sizeof(*pow_data));
1636
1637 pow_data->active_index = IWL_POWER_RANGE_0;
1638 pow_data->dtim_val = 0xffff;
1639
1640 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1641 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1642
1643 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1644 if (rc != 0)
1645 return 0;
1646 else {
Tomas Winkler600c0e12008-12-19 10:37:04 +08001647 struct iwl_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001648
1649 IWL_DEBUG_POWER("adjust power command flags\n");
1650
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001651 for (i = 0; i < IWL39_POWER_AC; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07001652 cmd = &pow_data->pwr_range_0[i].cmd;
1653
1654 if (pci_pm & 0x1)
1655 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1656 else
1657 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1658 }
1659 }
1660 return rc;
1661}
1662
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001663static int iwl3945_update_power_cmd(struct iwl_priv *priv,
Tomas Winkler600c0e12008-12-19 10:37:04 +08001664 struct iwl_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001665{
1666 int rc = 0, i;
1667 u8 skip;
1668 u32 max_sleep = 0;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001669 struct iwl_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001670 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001671 struct iwl3945_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001672
1673 if (mode > IWL_POWER_INDEX_5) {
1674 IWL_DEBUG_POWER("Error invalid power mode \n");
1675 return -1;
1676 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001677 pow_data = &(priv->power_data_39);
Zhu Yib481de92007-09-25 17:54:57 -07001678
1679 if (pow_data->active_index == IWL_POWER_RANGE_0)
1680 range = &pow_data->pwr_range_0[0];
1681 else
1682 range = &pow_data->pwr_range_1[1];
1683
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001684 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001685
1686#ifdef IWL_MAC80211_DISABLE
1687 if (priv->assoc_network != NULL) {
1688 unsigned long flags;
1689
1690 period = priv->assoc_network->tim.tim_period;
1691 }
1692#endif /*IWL_MAC80211_DISABLE */
1693 skip = range[mode].no_dtim;
1694
1695 if (period == 0) {
1696 period = 1;
1697 skip = 0;
1698 }
1699
1700 if (skip == 0) {
1701 max_sleep = period;
1702 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1703 } else {
1704 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1705 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1706 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1707 }
1708
1709 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1710 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1711 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1712 }
1713
1714 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1715 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1716 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1717 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1718 le32_to_cpu(cmd->sleep_interval[0]),
1719 le32_to_cpu(cmd->sleep_interval[1]),
1720 le32_to_cpu(cmd->sleep_interval[2]),
1721 le32_to_cpu(cmd->sleep_interval[3]),
1722 le32_to_cpu(cmd->sleep_interval[4]));
1723
1724 return rc;
1725}
1726
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001727static int iwl3945_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001728{
John W. Linville9a62f732007-11-15 16:27:36 -05001729 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001730 int rc;
Tomas Winkler600c0e12008-12-19 10:37:04 +08001731 struct iwl_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001732
1733 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08001734 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07001735 * else user level */
1736 switch (mode) {
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001737 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07001738 final_mode = IWL_POWER_INDEX_3;
1739 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08001740 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07001741 final_mode = IWL_POWER_MODE_CAM;
1742 break;
1743 default:
1744 final_mode = mode;
1745 break;
1746 }
1747
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001748 iwl3945_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07001749
Tomas Winkler600c0e12008-12-19 10:37:04 +08001750 /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
1751 rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD,
1752 sizeof(struct iwl3945_powertable_cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001753
1754 if (final_mode == IWL_POWER_MODE_CAM)
1755 clear_bit(STATUS_POWER_PMI, &priv->status);
1756 else
1757 set_bit(STATUS_POWER_PMI, &priv->status);
1758
1759 return rc;
1760}
1761
Zhu Yib481de92007-09-25 17:54:57 -07001762/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001763 * iwl3945_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001764 *
1765 * NOTE: priv->mutex is not required before calling this function
1766 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001767static int iwl3945_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001768{
1769 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1770 clear_bit(STATUS_SCANNING, &priv->status);
1771 return 0;
1772 }
1773
1774 if (test_bit(STATUS_SCANNING, &priv->status)) {
1775 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1776 IWL_DEBUG_SCAN("Queuing scan abort.\n");
1777 set_bit(STATUS_SCAN_ABORTING, &priv->status);
1778 queue_work(priv->workqueue, &priv->abort_scan);
1779
1780 } else
1781 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
1782
1783 return test_bit(STATUS_SCANNING, &priv->status);
1784 }
1785
1786 return 0;
1787}
1788
1789/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001790 * iwl3945_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07001791 * @ms: amount of time to wait (in milliseconds) for scan to abort
1792 *
1793 * NOTE: priv->mutex must be held before calling this function
1794 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001795static int iwl3945_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07001796{
1797 unsigned long now = jiffies;
1798 int ret;
1799
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001800 ret = iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001801 if (ret && ms) {
1802 mutex_unlock(&priv->mutex);
1803 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
1804 test_bit(STATUS_SCANNING, &priv->status))
1805 msleep(1);
1806 mutex_lock(&priv->mutex);
1807
1808 return test_bit(STATUS_SCANNING, &priv->status);
1809 }
1810
1811 return ret;
1812}
1813
Zhu Yib481de92007-09-25 17:54:57 -07001814#define MAX_UCODE_BEACON_INTERVAL 1024
1815#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
1816
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001817static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07001818{
1819 u16 new_val = 0;
1820 u16 beacon_factor = 0;
1821
1822 beacon_factor =
1823 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1824 / MAX_UCODE_BEACON_INTERVAL;
1825 new_val = beacon_val / beacon_factor;
1826
1827 return cpu_to_le16(new_val);
1828}
1829
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001830static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001831{
1832 u64 interval_tm_unit;
1833 u64 tsf, result;
1834 unsigned long flags;
1835 struct ieee80211_conf *conf = NULL;
1836 u16 beacon_int = 0;
1837
1838 conf = ieee80211_get_hw_conf(priv->hw);
1839
1840 spin_lock_irqsave(&priv->lock, flags);
Tomas Winkler28afaf92008-12-19 10:37:06 +08001841 priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
Zhu Yib481de92007-09-25 17:54:57 -07001842 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1843
Tomas Winkler28afaf92008-12-19 10:37:06 +08001844 tsf = priv->timestamp;
Zhu Yib481de92007-09-25 17:54:57 -07001845
1846 beacon_int = priv->beacon_int;
1847 spin_unlock_irqrestore(&priv->lock, flags);
1848
Johannes Berg05c914f2008-09-11 00:01:58 +02001849 if (priv->iw_mode == NL80211_IFTYPE_STATION) {
Zhu Yib481de92007-09-25 17:54:57 -07001850 if (beacon_int == 0) {
1851 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1852 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1853 } else {
1854 priv->rxon_timing.beacon_interval =
1855 cpu_to_le16(beacon_int);
1856 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001857 iwl3945_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07001858 le16_to_cpu(priv->rxon_timing.beacon_interval));
1859 }
1860
1861 priv->rxon_timing.atim_window = 0;
1862 } else {
1863 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001864 iwl3945_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07001865 /* TODO: we need to get atim_window from upper stack
1866 * for now we set to 0 */
1867 priv->rxon_timing.atim_window = 0;
1868 }
1869
1870 interval_tm_unit =
1871 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1872 result = do_div(tsf, interval_tm_unit);
1873 priv->rxon_timing.beacon_init_val =
1874 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1875
1876 IWL_DEBUG_ASSOC
1877 ("beacon interval %d beacon timer %d beacon tim %d\n",
1878 le16_to_cpu(priv->rxon_timing.beacon_interval),
1879 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1880 le16_to_cpu(priv->rxon_timing.atim_window));
1881}
1882
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001883static int iwl3945_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001884{
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08001885 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07001886 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1887 return -EIO;
1888 }
1889
1890 if (test_bit(STATUS_SCANNING, &priv->status)) {
1891 IWL_DEBUG_SCAN("Scan already in progress.\n");
1892 return -EAGAIN;
1893 }
1894
1895 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1896 IWL_DEBUG_SCAN("Scan request while abort pending. "
1897 "Queuing.\n");
1898 return -EAGAIN;
1899 }
1900
1901 IWL_DEBUG_INFO("Starting scan...\n");
Ron Rindjunsky66b50042008-06-25 16:46:31 +08001902 if (priv->cfg->sku & IWL_SKU_G)
1903 priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
1904 if (priv->cfg->sku & IWL_SKU_A)
1905 priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07001906 set_bit(STATUS_SCANNING, &priv->status);
1907 priv->scan_start = jiffies;
1908 priv->scan_pass_start = priv->scan_start;
1909
1910 queue_work(priv->workqueue, &priv->request_scan);
1911
1912 return 0;
1913}
1914
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001915static int iwl3945_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07001916{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001917 struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001918
1919 if (hw_decrypt)
1920 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
1921 else
1922 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
1923
1924 return 0;
1925}
1926
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001927static void iwl3945_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01001928 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07001929{
Johannes Berg8318d782008-01-24 19:38:38 +01001930 if (band == IEEE80211_BAND_5GHZ) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001931 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07001932 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1933 | RXON_FLG_CCK_MSK);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001934 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001935 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001936 /* Copied from iwl3945_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07001937 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001938 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001939 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001940 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001941
Johannes Berg05c914f2008-09-11 00:01:58 +02001942 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001943 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001944
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001945 priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1946 priv->staging39_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1947 priv->staging39_rxon.flags &= ~RXON_FLG_CCK_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001948 }
1949}
1950
1951/*
Ian Schram01ebd062007-10-25 17:15:22 +08001952 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07001953 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08001954static void iwl3945_connection_init_rx_config(struct iwl_priv *priv,
Zhu, Yi60294de2008-10-29 14:05:45 -07001955 int mode)
Zhu Yib481de92007-09-25 17:54:57 -07001956{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08001957 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07001958
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001959 memset(&priv->staging39_rxon, 0, sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07001960
Zhu, Yi60294de2008-10-29 14:05:45 -07001961 switch (mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02001962 case NL80211_IFTYPE_AP:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001963 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_AP;
Zhu Yib481de92007-09-25 17:54:57 -07001964 break;
1965
Johannes Berg05c914f2008-09-11 00:01:58 +02001966 case NL80211_IFTYPE_STATION:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001967 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_ESS;
1968 priv->staging39_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001969 break;
1970
Johannes Berg05c914f2008-09-11 00:01:58 +02001971 case NL80211_IFTYPE_ADHOC:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001972 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1973 priv->staging39_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1974 priv->staging39_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001975 RXON_FILTER_ACCEPT_GRP_MSK;
1976 break;
1977
Johannes Berg05c914f2008-09-11 00:01:58 +02001978 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001979 priv->staging39_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1980 priv->staging39_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
Zhu Yib481de92007-09-25 17:54:57 -07001981 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1982 break;
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001983 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08001984 IWL_ERR(priv, "Unsupported interface type %d\n", mode);
Tomas Winkler69dc5d92008-03-25 16:33:41 -07001985 break;
Zhu Yib481de92007-09-25 17:54:57 -07001986 }
1987
1988#if 0
1989 /* TODO: Figure out when short_preamble would be set and cache from
1990 * that */
1991 if (!hw_to_local(priv->hw)->short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001992 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001993 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001994 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001995#endif
1996
Johannes Berg8318d782008-01-24 19:38:38 +01001997 ch_info = iwl3945_get_channel_info(priv, priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08001998 le16_to_cpu(priv->active39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07001999
2000 if (!ch_info)
2001 ch_info = &priv->channel_info[0];
2002
2003 /*
2004 * in some case A channels are all non IBSS
2005 * in this case force B/G channel
2006 */
Zhu, Yi60294de2008-10-29 14:05:45 -07002007 if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
Zhu Yib481de92007-09-25 17:54:57 -07002008 ch_info = &priv->channel_info[0];
2009
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002010 priv->staging39_rxon.channel = cpu_to_le16(ch_info->channel);
Zhu Yib481de92007-09-25 17:54:57 -07002011 if (is_channel_a_band(ch_info))
Johannes Berg8318d782008-01-24 19:38:38 +01002012 priv->band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002013 else
Johannes Berg8318d782008-01-24 19:38:38 +01002014 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07002015
Johannes Berg8318d782008-01-24 19:38:38 +01002016 iwl3945_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002017
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002018 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002019 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002020 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002021 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2022}
2023
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002024static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002025{
Johannes Berg05c914f2008-09-11 00:01:58 +02002026 if (mode == NL80211_IFTYPE_ADHOC) {
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002027 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002028
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002029 ch_info = iwl3945_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002030 priv->band,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002031 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002032
2033 if (!ch_info || !is_channel_ibss(ch_info)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002034 IWL_ERR(priv, "channel %d not IBSS channel\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002035 le16_to_cpu(priv->staging39_rxon.channel));
Zhu Yib481de92007-09-25 17:54:57 -07002036 return -EINVAL;
2037 }
2038 }
2039
Zhu, Yi60294de2008-10-29 14:05:45 -07002040 iwl3945_connection_init_rx_config(priv, mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002041 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07002042
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002043 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002044
Tomas Winklera96a27f2008-10-23 23:48:56 -07002045 /* don't commit rxon if rf-kill is on*/
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002046 if (!iwl_is_ready_rf(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08002047 return -EAGAIN;
2048
2049 cancel_delayed_work(&priv->scan_check);
2050 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002051 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
Mohamed Abbasfde35712007-11-29 11:10:15 +08002052 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2053 return -EAGAIN;
2054 }
2055
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002056 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002057
2058 return 0;
2059}
2060
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002061static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Johannes Berge039fa42008-05-15 12:55:29 +02002062 struct ieee80211_tx_info *info,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002063 struct iwl_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002064 struct sk_buff *skb_frag,
2065 int last_frag)
2066{
Ivo van Doorn1c014422008-04-17 19:41:02 +02002067 struct iwl3945_hw_key *keyinfo =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002068 &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002069
2070 switch (keyinfo->alg) {
2071 case ALG_CCMP:
2072 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2073 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Tomas Winklera96a27f2008-10-23 23:48:56 -07002074 IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
Zhu Yib481de92007-09-25 17:54:57 -07002075 break;
2076
2077 case ALG_TKIP:
2078#if 0
2079 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2080
2081 if (last_frag)
2082 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2083 8);
2084 else
2085 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2086#endif
2087 break;
2088
2089 case ALG_WEP:
2090 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
Johannes Berge039fa42008-05-15 12:55:29 +02002091 (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
Zhu Yib481de92007-09-25 17:54:57 -07002092
2093 if (keyinfo->keylen == 13)
2094 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2095
2096 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2097
2098 IWL_DEBUG_TX("Configuring packet for WEP encryption "
Johannes Berge039fa42008-05-15 12:55:29 +02002099 "with key %d\n", info->control.hw_key->hw_key_idx);
Zhu Yib481de92007-09-25 17:54:57 -07002100 break;
2101
Zhu Yib481de92007-09-25 17:54:57 -07002102 default:
Tomas Winkler978785a2008-12-19 10:37:31 +08002103 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
Zhu Yib481de92007-09-25 17:54:57 -07002104 break;
2105 }
2106}
2107
2108/*
2109 * handle build REPLY_TX command notification.
2110 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002111static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002112 struct iwl_cmd *cmd,
Johannes Berge039fa42008-05-15 12:55:29 +02002113 struct ieee80211_tx_info *info,
Zhu Yib481de92007-09-25 17:54:57 -07002114 struct ieee80211_hdr *hdr,
2115 int is_unicast, u8 std_id)
2116{
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002117 __le16 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002118 __le32 tx_flags = cmd->cmd.tx.tx_flags;
Johannes Berge6a98542008-10-21 12:40:02 +02002119 u8 rc_flags = info->control.rates[0].flags;
Zhu Yib481de92007-09-25 17:54:57 -07002120
2121 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
Johannes Berge039fa42008-05-15 12:55:29 +02002122 if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
Zhu Yib481de92007-09-25 17:54:57 -07002123 tx_flags |= TX_CMD_FLG_ACK_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002124 if (ieee80211_is_mgmt(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002125 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002126 if (ieee80211_is_probe_resp(fc) &&
Zhu Yib481de92007-09-25 17:54:57 -07002127 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2128 tx_flags |= TX_CMD_FLG_TSF_MSK;
2129 } else {
2130 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2131 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2132 }
2133
2134 cmd->cmd.tx.sta_id = std_id;
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002135 if (ieee80211_has_morefrags(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002136 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2137
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002138 if (ieee80211_is_data_qos(fc)) {
2139 u8 *qc = ieee80211_get_qos_ctl(hdr);
Tomas Winkler54dbb522008-05-15 13:54:06 +08002140 cmd->cmd.tx.tid_tspec = qc[0] & 0xf;
Zhu Yib481de92007-09-25 17:54:57 -07002141 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002142 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002143 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002144 }
Zhu Yib481de92007-09-25 17:54:57 -07002145
Johannes Berge6a98542008-10-21 12:40:02 +02002146 if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
Zhu Yib481de92007-09-25 17:54:57 -07002147 tx_flags |= TX_CMD_FLG_RTS_MSK;
2148 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
Johannes Berge6a98542008-10-21 12:40:02 +02002149 } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
Zhu Yib481de92007-09-25 17:54:57 -07002150 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2151 tx_flags |= TX_CMD_FLG_CTS_MSK;
2152 }
2153
2154 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2155 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2156
2157 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002158 if (ieee80211_is_mgmt(fc)) {
2159 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
Ian Schrambc434dd2007-10-25 17:15:29 +08002160 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002161 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002162 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002163 } else {
Zhu Yib481de92007-09-25 17:54:57 -07002164 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07002165#ifdef CONFIG_IWL3945_LEDS
2166 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
2167#endif
2168 }
Zhu Yib481de92007-09-25 17:54:57 -07002169
2170 cmd->cmd.tx.driver_txop = 0;
2171 cmd->cmd.tx.tx_flags = tx_flags;
2172 cmd->cmd.tx.next_frame_len = 0;
2173}
2174
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002175/**
2176 * iwl3945_get_sta_id - Find station's index within station table
2177 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002178static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002179{
2180 int sta_id;
2181 u16 fc = le16_to_cpu(hdr->frame_control);
2182
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002183 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002184 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2185 is_multicast_ether_addr(hdr->addr1))
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002186 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002187
2188 switch (priv->iw_mode) {
2189
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002190 /* If we are a client station in a BSS network, use the special
2191 * AP station entry (that's the only station we communicate with) */
Johannes Berg05c914f2008-09-11 00:01:58 +02002192 case NL80211_IFTYPE_STATION:
Zhu Yib481de92007-09-25 17:54:57 -07002193 return IWL_AP_ID;
2194
2195 /* If we are an AP, then find the station, or use BCAST */
Johannes Berg05c914f2008-09-11 00:01:58 +02002196 case NL80211_IFTYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002197 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002198 if (sta_id != IWL_INVALID_STATION)
2199 return sta_id;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002200 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002201
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002202 /* If this frame is going out to an IBSS network, find the station,
2203 * or create a new station table entry */
Johannes Berg05c914f2008-09-11 00:01:58 +02002204 case NL80211_IFTYPE_ADHOC: {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002205 /* Create new station table entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002206 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002207 if (sta_id != IWL_INVALID_STATION)
2208 return sta_id;
2209
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002210 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002211
2212 if (sta_id != IWL_INVALID_STATION)
2213 return sta_id;
2214
Johannes Berge1749612008-10-27 15:59:26 -07002215 IWL_DEBUG_DROP("Station %pM not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002216 "Defaulting to broadcast...\n",
Johannes Berge1749612008-10-27 15:59:26 -07002217 hdr->addr1);
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002218 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002219 return priv->hw_params.bcast_sta_id;
Joe Perches0795af52007-10-03 17:59:30 -07002220 }
Stefanik Gábor914233d2008-06-30 17:23:30 +08002221 /* If we are in monitor mode, use BCAST. This is required for
2222 * packet injection. */
Johannes Berg05c914f2008-09-11 00:01:58 +02002223 case NL80211_IFTYPE_MONITOR:
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002224 return priv->hw_params.bcast_sta_id;
Stefanik Gábor914233d2008-06-30 17:23:30 +08002225
Zhu Yib481de92007-09-25 17:54:57 -07002226 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002227 IWL_WARN(priv, "Unknown mode of operation: %d\n",
2228 priv->iw_mode);
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002229 return priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07002230 }
2231}
2232
2233/*
2234 * start REPLY_TX command process
2235 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002236static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07002237{
2238 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Johannes Berge039fa42008-05-15 12:55:29 +02002239 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002240 struct iwl3945_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002241 u32 *control_flags;
Johannes Berge2530082008-05-17 00:57:14 +02002242 int txq_id = skb_get_queue_mapping(skb);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002243 struct iwl3945_tx_queue *txq = NULL;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002244 struct iwl_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002245 dma_addr_t phys_addr;
2246 dma_addr_t txcmd_phys;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002247 struct iwl_cmd *out_cmd = NULL;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002248 u16 len, idx, len_org, hdr_len;
2249 u8 id;
2250 u8 unicast;
Zhu Yib481de92007-09-25 17:54:57 -07002251 u8 sta_id;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002252 u8 tid = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002253 u16 seq_number = 0;
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002254 __le16 fc;
Zhu Yib481de92007-09-25 17:54:57 -07002255 u8 wait_write_ptr = 0;
Tomas Winkler54dbb522008-05-15 13:54:06 +08002256 u8 *qc = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002257 unsigned long flags;
2258 int rc;
2259
2260 spin_lock_irqsave(&priv->lock, flags);
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08002261 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002262 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2263 goto drop_unlock;
2264 }
2265
Johannes Berge039fa42008-05-15 12:55:29 +02002266 if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002267 IWL_ERR(priv, "ERROR: No TX rate available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002268 goto drop_unlock;
2269 }
2270
2271 unicast = !is_multicast_ether_addr(hdr->addr1);
2272 id = 0;
2273
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002274 fc = hdr->frame_control;
Zhu Yib481de92007-09-25 17:54:57 -07002275
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002276#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002277 if (ieee80211_is_auth(fc))
2278 IWL_DEBUG_TX("Sending AUTH frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002279 else if (ieee80211_is_assoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002280 IWL_DEBUG_TX("Sending ASSOC frame\n");
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002281 else if (ieee80211_is_reassoc_req(fc))
Zhu Yib481de92007-09-25 17:54:57 -07002282 IWL_DEBUG_TX("Sending REASSOC frame\n");
2283#endif
2284
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002285 /* drop all data frame if we are not associated */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002286 if (ieee80211_is_data(fc) &&
Johannes Berg05c914f2008-09-11 00:01:58 +02002287 (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
Stefanik Gábor914233d2008-06-30 17:23:30 +08002288 (!iwl3945_is_associated(priv) ||
Johannes Berg05c914f2008-09-11 00:01:58 +02002289 ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002290 IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002291 goto drop_unlock;
2292 }
2293
2294 spin_unlock_irqrestore(&priv->lock, flags);
2295
Harvey Harrison7294ec92008-07-15 18:43:59 -07002296 hdr_len = ieee80211_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002297
2298 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002299 sta_id = iwl3945_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002300 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07002301 IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
2302 hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002303 goto drop;
2304 }
2305
2306 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2307
Harvey Harrisonfd7c8a42008-06-11 14:21:56 -07002308 if (ieee80211_is_data_qos(fc)) {
2309 qc = ieee80211_get_qos_ctl(hdr);
Harvey Harrison7294ec92008-07-15 18:43:59 -07002310 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002311 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
Zhu Yib481de92007-09-25 17:54:57 -07002312 IEEE80211_SCTL_SEQ;
2313 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2314 (hdr->seq_ctrl &
2315 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2316 seq_number += 0x10;
2317 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002318
2319 /* Descriptor for chosen Tx queue */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002320 txq = &priv->txq39[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07002321 q = &txq->q;
2322
2323 spin_lock_irqsave(&priv->lock, flags);
2324
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002325 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002326 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002327 memset(tfd, 0, sizeof(*tfd));
2328 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002329 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002330
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002331 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002332 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl3945_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002333 txq->txb[q->write_ptr].skb[0] = skb;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002334
2335 /* Init first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002336 out_cmd = &txq->cmd[idx];
2337 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2338 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002339
2340 /*
2341 * Set up the Tx-command (not MAC!) header.
2342 * Store the chosen Tx queue and TFD index within the sequence field;
2343 * after Tx, uCode's Tx response will return this value so driver can
2344 * locate the frame within the tx queue and do post-tx processing.
2345 */
Zhu Yib481de92007-09-25 17:54:57 -07002346 out_cmd->hdr.cmd = REPLY_TX;
2347 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002348 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002349
2350 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002351 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2352
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002353 /*
2354 * Use the first empty entry in this queue's command buffer array
2355 * to contain the Tx command and MAC header concatenated together
2356 * (payload data will be in another buffer).
2357 * Size of this varies, due to varying MAC header length.
2358 * If end is not dword aligned, we'll have 2 extra bytes at the end
2359 * of the MAC header (device reads on dword boundaries).
2360 * We'll tell device about this padding later.
2361 */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08002362 len = sizeof(struct iwl3945_tx_cmd) +
Tomas Winkler4c897252008-12-19 10:37:05 +08002363 sizeof(struct iwl_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002364
2365 len_org = len;
2366 len = (len + 3) & ~3;
2367
2368 if (len_org != len)
2369 len_org = 1;
2370 else
2371 len_org = 0;
2372
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002373 /* Physical address of this Tx command's header (not MAC header!),
2374 * within command buffer array. */
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002375 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
2376 offsetof(struct iwl_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002377
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002378 /* Add buffer containing Tx command and MAC(!) header to TFD's
2379 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002380 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002381
Johannes Bergd0f09802008-07-29 11:32:07 +02002382 if (info->control.hw_key)
Johannes Berge039fa42008-05-15 12:55:29 +02002383 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002384
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002385 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2386 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002387 len = skb->len - hdr_len;
2388 if (len) {
2389 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2390 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002391 iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002392 }
2393
Zhu Yib481de92007-09-25 17:54:57 -07002394 if (!len)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002395 /* If there is no payload, then we use only one Tx buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002396 *control_flags = TFD_CTL_COUNT_SET(1);
2397 else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002398 /* Else use 2 buffers.
2399 * Tell 3945 about any padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002400 *control_flags = TFD_CTL_COUNT_SET(2) |
2401 TFD_CTL_PAD_SET(U32_PAD(len));
2402
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002403 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002404 len = (u16)skb->len;
2405 out_cmd->cmd.tx.len = cpu_to_le16(len);
2406
2407 /* TODO need this for burst mode later on */
Johannes Berge039fa42008-05-15 12:55:29 +02002408 iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002409
2410 /* set is_hcca to 0; it probably will never be implemented */
Johannes Berge039fa42008-05-15 12:55:29 +02002411 iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002412
2413 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
2414 out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
2415
Harvey Harrison8b7b1e02008-06-11 14:21:56 -07002416 if (!ieee80211_has_morefrags(hdr->frame_control)) {
Zhu Yib481de92007-09-25 17:54:57 -07002417 txq->need_update = 1;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03002418 if (qc)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002419 priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
Zhu Yib481de92007-09-25 17:54:57 -07002420 } else {
2421 wait_write_ptr = 1;
2422 txq->need_update = 0;
2423 }
2424
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002425 iwl_print_hex_dump(priv, IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002426 sizeof(out_cmd->cmd.tx));
2427
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002428 iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Harvey Harrison7294ec92008-07-15 18:43:59 -07002429 ieee80211_hdrlen(fc));
Zhu Yib481de92007-09-25 17:54:57 -07002430
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002431 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002432 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002433 rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002434 spin_unlock_irqrestore(&priv->lock, flags);
2435
2436 if (rc)
2437 return rc;
2438
Samuel Ortizd20b3c62008-12-19 10:37:15 +08002439 if ((iwl_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002440 && priv->mac80211_registered) {
2441 if (wait_write_ptr) {
2442 spin_lock_irqsave(&priv->lock, flags);
2443 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002444 iwl3945_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002445 spin_unlock_irqrestore(&priv->lock, flags);
2446 }
2447
Johannes Berge2530082008-05-17 00:57:14 +02002448 ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
Zhu Yib481de92007-09-25 17:54:57 -07002449 }
2450
2451 return 0;
2452
2453drop_unlock:
2454 spin_unlock_irqrestore(&priv->lock, flags);
2455drop:
2456 return -1;
2457}
2458
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002459static void iwl3945_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002460{
Johannes Berg8318d782008-01-24 19:38:38 +01002461 const struct ieee80211_supported_band *sband = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002462 struct ieee80211_rate *rate;
2463 int i;
2464
Johannes Berg8318d782008-01-24 19:38:38 +01002465 sband = iwl3945_get_band(priv, priv->band);
2466 if (!sband) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002467 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002468 return;
2469 }
Zhu Yib481de92007-09-25 17:54:57 -07002470
2471 priv->active_rate = 0;
2472 priv->active_rate_basic = 0;
2473
Johannes Berg8318d782008-01-24 19:38:38 +01002474 IWL_DEBUG_RATE("Setting rates for %s GHz\n",
2475 sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
Zhu Yib481de92007-09-25 17:54:57 -07002476
Johannes Berg8318d782008-01-24 19:38:38 +01002477 for (i = 0; i < sband->n_bitrates; i++) {
2478 rate = &sband->bitrates[i];
2479 if ((rate->hw_value < IWL_RATE_COUNT) &&
2480 !(rate->flags & IEEE80211_CHAN_DISABLED)) {
2481 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
2482 rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
2483 priv->active_rate |= (1 << rate->hw_value);
2484 }
Zhu Yib481de92007-09-25 17:54:57 -07002485 }
2486
2487 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2488 priv->active_rate, priv->active_rate_basic);
2489
2490 /*
2491 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2492 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2493 * OFDM
2494 */
2495 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002496 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002497 ((priv->active_rate_basic &
2498 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2499 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002500 priv->staging39_rxon.cck_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002501 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2502
2503 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002504 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002505 ((priv->active_rate_basic &
2506 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2507 IWL_FIRST_OFDM_RATE) & 0xFF;
2508 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002509 priv->staging39_rxon.ofdm_basic_rates =
Zhu Yib481de92007-09-25 17:54:57 -07002510 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2511}
2512
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002513static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002514{
2515 unsigned long flags;
2516
2517 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2518 return;
2519
2520 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2521 disable_radio ? "OFF" : "ON");
2522
2523 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002524 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002525 /* FIXME: This is a workaround for AP */
Johannes Berg05c914f2008-09-11 00:01:58 +02002526 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07002527 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002528 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002529 CSR_UCODE_SW_BIT_RFKILL);
2530 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002531 iwl3945_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002532 set_bit(STATUS_RF_KILL_SW, &priv->status);
2533 }
2534 return;
2535 }
2536
2537 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002538 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002539
2540 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2541 spin_unlock_irqrestore(&priv->lock, flags);
2542
2543 /* wake up ucode */
2544 msleep(10);
2545
2546 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08002547 iwl_read32(priv, CSR_UCODE_DRV_GP1);
2548 if (!iwl_grab_nic_access(priv))
2549 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002550 spin_unlock_irqrestore(&priv->lock, flags);
2551
2552 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2553 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2554 "disabled by HW switch\n");
2555 return;
2556 }
2557
Zhu Yi808e72a2008-06-12 09:47:17 +08002558 if (priv->is_open)
2559 queue_work(priv->workqueue, &priv->restart);
Zhu Yib481de92007-09-25 17:54:57 -07002560 return;
2561}
2562
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002563void iwl3945_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002564 u32 decrypt_res, struct ieee80211_rx_status *stats)
2565{
2566 u16 fc =
2567 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2568
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002569 if (priv->active39_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002570 return;
2571
2572 if (!(fc & IEEE80211_FCTL_PROTECTED))
2573 return;
2574
2575 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2576 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2577 case RX_RES_STATUS_SEC_TYPE_TKIP:
2578 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2579 RX_RES_STATUS_BAD_ICV_MIC)
2580 stats->flag |= RX_FLAG_MMIC_ERROR;
2581 case RX_RES_STATUS_SEC_TYPE_WEP:
2582 case RX_RES_STATUS_SEC_TYPE_CCMP:
2583 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2584 RX_RES_STATUS_DECRYPT_OK) {
2585 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2586 stats->flag |= RX_FLAG_DECRYPTED;
2587 }
2588 break;
2589
2590 default:
2591 break;
2592 }
2593}
2594
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002595#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07002596
2597#include "iwl-spectrum.h"
2598
2599#define BEACON_TIME_MASK_LOW 0x00FFFFFF
2600#define BEACON_TIME_MASK_HIGH 0xFF000000
2601#define TIME_UNIT 1024
2602
2603/*
2604 * extended beacon time format
2605 * time in usec will be changed into a 32-bit value in 8:24 format
2606 * the high 1 byte is the beacon counts
2607 * the lower 3 bytes is the time in usec within one beacon interval
2608 */
2609
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002610static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002611{
2612 u32 quot;
2613 u32 rem;
2614 u32 interval = beacon_interval * 1024;
2615
2616 if (!interval || !usec)
2617 return 0;
2618
2619 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
2620 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
2621
2622 return (quot << 24) + rem;
2623}
2624
2625/* base is usually what we get from ucode with each received frame,
2626 * the same as HW timer counter counting down
2627 */
2628
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002629static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07002630{
2631 u32 base_low = base & BEACON_TIME_MASK_LOW;
2632 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
2633 u32 interval = beacon_interval * TIME_UNIT;
2634 u32 res = (base & BEACON_TIME_MASK_HIGH) +
2635 (addon & BEACON_TIME_MASK_HIGH);
2636
2637 if (base_low > addon_low)
2638 res += base_low - addon_low;
2639 else if (base_low < addon_low) {
2640 res += interval + base_low - addon_low;
2641 res += (1 << 24);
2642 } else
2643 res += (1 << 24);
2644
2645 return cpu_to_le32(res);
2646}
2647
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002648static int iwl3945_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002649 struct ieee80211_measurement_params *params,
2650 u8 type)
2651{
Tomas Winkler600c0e12008-12-19 10:37:04 +08002652 struct iwl_spectrum_cmd spectrum;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002653 struct iwl_rx_packet *res;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08002654 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07002655 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
2656 .data = (void *)&spectrum,
2657 .meta.flags = CMD_WANT_SKB,
2658 };
2659 u32 add_time = le64_to_cpu(params->start_time);
2660 int rc;
2661 int spectrum_resp_status;
2662 int duration = le16_to_cpu(params->duration);
2663
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002664 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002665 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002666 iwl3945_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07002667 le64_to_cpu(params->start_time) - priv->last_tsf,
2668 le16_to_cpu(priv->rxon_timing.beacon_interval));
2669
2670 memset(&spectrum, 0, sizeof(spectrum));
2671
2672 spectrum.channel_count = cpu_to_le16(1);
2673 spectrum.flags =
2674 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
2675 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
2676 cmd.len = sizeof(spectrum);
2677 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
2678
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002679 if (iwl3945_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002680 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002681 iwl3945_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07002682 add_time,
2683 le16_to_cpu(priv->rxon_timing.beacon_interval));
2684 else
2685 spectrum.start_time = 0;
2686
2687 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
2688 spectrum.channels[0].channel = params->channel;
2689 spectrum.channels[0].type = type;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002690 if (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK)
Zhu Yib481de92007-09-25 17:54:57 -07002691 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
2692 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
2693
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002694 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002695 if (rc)
2696 return rc;
2697
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002698 res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002699 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002700 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
Zhu Yib481de92007-09-25 17:54:57 -07002701 rc = -EIO;
2702 }
2703
2704 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
2705 switch (spectrum_resp_status) {
2706 case 0: /* Command will be handled */
2707 if (res->u.spectrum.id != 0xff) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002708 IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
2709 res->u.spectrum.id);
Zhu Yib481de92007-09-25 17:54:57 -07002710 priv->measurement_status &= ~MEASUREMENT_READY;
2711 }
2712 priv->measurement_status |= MEASUREMENT_ACTIVE;
2713 rc = 0;
2714 break;
2715
2716 case 1: /* Command will not be handled */
2717 rc = -EAGAIN;
2718 break;
2719 }
2720
2721 dev_kfree_skb_any(cmd.meta.u.skb);
2722
2723 return rc;
2724}
2725#endif
2726
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002727static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002728 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002729{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002730 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
2731 struct iwl_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07002732 struct delayed_work *pwork;
2733
2734 palive = &pkt->u.alive_frame;
2735
2736 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
2737 "0x%01X 0x%01X\n",
2738 palive->is_valid, palive->ver_type,
2739 palive->ver_subtype);
2740
2741 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
2742 IWL_DEBUG_INFO("Initialization Alive received.\n");
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002743 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
2744 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002745 pwork = &priv->init_alive_start;
2746 } else {
2747 IWL_DEBUG_INFO("Runtime Alive received.\n");
2748 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002749 sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07002750 pwork = &priv->alive_start;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002751 iwl3945_disable_events(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002752 }
2753
2754 /* We delay the ALIVE response by 5ms to
2755 * give the HW RF Kill time to activate... */
2756 if (palive->is_valid == UCODE_VALID_OK)
2757 queue_delayed_work(priv->workqueue, pwork,
2758 msecs_to_jiffies(5));
2759 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08002760 IWL_WARN(priv, "uCode did not respond OK.\n");
Zhu Yib481de92007-09-25 17:54:57 -07002761}
2762
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002763static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002764 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002765{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002766 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002767
2768 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
2769 return;
2770}
2771
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002772static void iwl3945_rx_reply_error(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002773 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002774{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002775 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002776
Winkler, Tomas15b16872008-12-19 10:37:33 +08002777 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
Zhu Yib481de92007-09-25 17:54:57 -07002778 "seq 0x%04X ser 0x%08X\n",
2779 le32_to_cpu(pkt->u.err_resp.error_type),
2780 get_cmd_string(pkt->u.err_resp.cmd_id),
2781 pkt->u.err_resp.cmd_id,
2782 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
2783 le32_to_cpu(pkt->u.err_resp.error_info));
2784}
2785
2786#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2787
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002788static void iwl3945_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002789{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002790 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002791 struct iwl3945_rxon_cmd *rxon = (void *)&priv->active39_rxon;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002792 struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002793 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
2794 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
2795 rxon->channel = csa->channel;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08002796 priv->staging39_rxon.channel = csa->channel;
Zhu Yib481de92007-09-25 17:54:57 -07002797}
2798
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002799static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002800 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002801{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002802#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002803 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002804 struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002805
2806 if (!report->state) {
2807 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
2808 "Spectrum Measure Notification: Start\n");
2809 return;
2810 }
2811
2812 memcpy(&priv->measure_report, report, sizeof(*report));
2813 priv->measurement_status |= MEASUREMENT_READY;
2814#endif
2815}
2816
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002817static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002818 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002819{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002820#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002821 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler600c0e12008-12-19 10:37:04 +08002822 struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07002823 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
2824 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
2825#endif
2826}
2827
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002828static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002829 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002830{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002831 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07002832 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
2833 "notification for %s:\n",
2834 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08002835 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
2836 le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07002837}
2838
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002839static void iwl3945_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07002840{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002841 struct iwl_priv *priv =
2842 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07002843 struct sk_buff *beacon;
2844
2845 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berge039fa42008-05-15 12:55:29 +02002846 beacon = ieee80211_beacon_get(priv->hw, priv->vif);
Zhu Yib481de92007-09-25 17:54:57 -07002847
2848 if (!beacon) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08002849 IWL_ERR(priv, "update beacon failed\n");
Zhu Yib481de92007-09-25 17:54:57 -07002850 return;
2851 }
2852
2853 mutex_lock(&priv->mutex);
2854 /* new beacon skb is allocated every time; dispose previous.*/
2855 if (priv->ibss_beacon)
2856 dev_kfree_skb(priv->ibss_beacon);
2857
2858 priv->ibss_beacon = beacon;
2859 mutex_unlock(&priv->mutex);
2860
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002861 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002862}
2863
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002864static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002865 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002866{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002867#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002868 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002869 struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
Zhu Yib481de92007-09-25 17:54:57 -07002870 u8 rate = beacon->beacon_notify_hdr.rate;
2871
2872 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2873 "tsf %d %d rate %d\n",
2874 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2875 beacon->beacon_notify_hdr.failure_frame,
2876 le32_to_cpu(beacon->ibss_mgr_status),
2877 le32_to_cpu(beacon->high_tsf),
2878 le32_to_cpu(beacon->low_tsf), rate);
2879#endif
2880
Johannes Berg05c914f2008-09-11 00:01:58 +02002881 if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
Zhu Yib481de92007-09-25 17:54:57 -07002882 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2883 queue_work(priv->workqueue, &priv->beacon_update);
2884}
2885
2886/* Service response to REPLY_SCAN_CMD (0x80) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002887static void iwl3945_rx_reply_scan(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{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002890#ifdef CONFIG_IWL3945_DEBUG
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002891 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002892 struct iwl_scanreq_notification *notif =
2893 (struct iwl_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002894
2895 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2896#endif
2897}
2898
2899/* Service SCAN_START_NOTIFICATION (0x82) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002900static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002901 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002902{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002903 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002904 struct iwl_scanstart_notification *notif =
2905 (struct iwl_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002906 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2907 IWL_DEBUG_SCAN("Scan start: "
2908 "%d [802.11%s] "
2909 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2910 notif->channel,
2911 notif->band ? "bg" : "a",
2912 notif->tsf_high,
2913 notif->tsf_low, notif->status, notif->beacon_timer);
2914}
2915
2916/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002917static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002918 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002919{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002920 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002921 struct iwl_scanresults_notification *notif =
2922 (struct iwl_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002923
2924 IWL_DEBUG_SCAN("Scan ch.res: "
2925 "%d [802.11%s] "
2926 "(TSF: 0x%08X:%08X) - %d "
2927 "elapsed=%lu usec (%dms since last)\n",
2928 notif->channel,
2929 notif->band ? "bg" : "a",
2930 le32_to_cpu(notif->tsf_high),
2931 le32_to_cpu(notif->tsf_low),
2932 le32_to_cpu(notif->statistics[0]),
2933 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2934 jiffies_to_msecs(elapsed_jiffies
2935 (priv->last_scan_jiffies, jiffies)));
2936
2937 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002938 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002939}
2940
2941/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08002942static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08002943 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07002944{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08002945 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Tomas Winkler4c897252008-12-19 10:37:05 +08002946 struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07002947
2948 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2949 scan_notif->scanned_channels,
2950 scan_notif->tsf_low,
2951 scan_notif->tsf_high, scan_notif->status);
2952
2953 /* The HW is no longer scanning */
2954 clear_bit(STATUS_SCAN_HW, &priv->status);
2955
2956 /* The scan completion notification came in, so kill that timer... */
2957 cancel_delayed_work(&priv->scan_check);
2958
2959 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002960 (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
2961 "2.4" : "5.2",
Zhu Yib481de92007-09-25 17:54:57 -07002962 jiffies_to_msecs(elapsed_jiffies
2963 (priv->scan_pass_start, jiffies)));
2964
Ron Rindjunsky66b50042008-06-25 16:46:31 +08002965 /* Remove this scanned band from the list of pending
2966 * bands to scan, band G precedes A in order of scanning
2967 * as seen in iwl3945_bg_request_scan */
2968 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
2969 priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
2970 else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
2971 priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
Zhu Yib481de92007-09-25 17:54:57 -07002972
2973 /* If a request to abort was given, or the scan did not succeed
2974 * then we reset the scan state machine and terminate,
2975 * re-queuing another scan if one has been requested */
2976 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2977 IWL_DEBUG_INFO("Aborted scan completed.\n");
2978 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2979 } else {
2980 /* If there are more bands on this scan pass reschedule */
2981 if (priv->scan_bands > 0)
2982 goto reschedule;
2983 }
2984
2985 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002986 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07002987 IWL_DEBUG_INFO("Setting scan to off\n");
2988
2989 clear_bit(STATUS_SCANNING, &priv->status);
2990
2991 IWL_DEBUG_INFO("Scan took %dms\n",
2992 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2993
2994 queue_work(priv->workqueue, &priv->scan_completed);
2995
2996 return;
2997
2998reschedule:
2999 priv->scan_pass_start = jiffies;
3000 queue_work(priv->workqueue, &priv->request_scan);
3001}
3002
3003/* Handle notification from uCode that card's power state is changing
3004 * due to software, hardware, or critical temperature RFKILL */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003005static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003006 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003007{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003008 struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003009 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3010 unsigned long status = priv->status;
3011
3012 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3013 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3014 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3015
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003016 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003017 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3018
3019 if (flags & HW_CARD_DISABLED)
3020 set_bit(STATUS_RF_KILL_HW, &priv->status);
3021 else
3022 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3023
3024
3025 if (flags & SW_CARD_DISABLED)
3026 set_bit(STATUS_RF_KILL_SW, &priv->status);
3027 else
3028 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3029
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003030 iwl3945_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003031
3032 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3033 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3034 (test_bit(STATUS_RF_KILL_SW, &status) !=
3035 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3036 queue_work(priv->workqueue, &priv->rf_kill);
3037 else
3038 wake_up_interruptible(&priv->wait_command_queue);
3039}
3040
3041/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003042 * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003043 *
3044 * Setup the RX handlers for each of the reply types sent from the uCode
3045 * to the host.
3046 *
3047 * This function chains into the hardware specific files for them to setup
3048 * any hardware specific handlers as well.
3049 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003050static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003051{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003052 priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
3053 priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
3054 priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
3055 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003056 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003057 iwl3945_rx_spectrum_measure_notif;
3058 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003059 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003060 iwl3945_rx_pm_debug_statistics_notif;
3061 priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003062
Ben Cahill9fbab512007-11-29 11:09:47 +08003063 /*
3064 * The same handler is used for both the REPLY to a discrete
3065 * statistics request from the host as well as for the periodic
3066 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003067 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003068 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
3069 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003070
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003071 priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
3072 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003073 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003074 iwl3945_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003075 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003076 iwl3945_rx_scan_complete_notif;
3077 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003078
Ben Cahill9fbab512007-11-29 11:09:47 +08003079 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003080 iwl3945_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003081}
3082
3083/**
Tomas Winkler91c066f2008-03-06 17:36:55 -08003084 * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
3085 * When FW advances 'R' index, all entries between old and new 'R' index
3086 * need to be reclaimed.
3087 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003088static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003089 int txq_id, int index)
3090{
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003091 struct iwl3945_tx_queue *txq = &priv->txq39[txq_id];
Samuel Ortizd20b3c62008-12-19 10:37:15 +08003092 struct iwl_queue *q = &txq->q;
Tomas Winkler91c066f2008-03-06 17:36:55 -08003093 int nfreed = 0;
3094
3095 if ((index >= q->n_bd) || (iwl3945_x2_queue_used(q, index) == 0)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003096 IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
Tomas Winkler91c066f2008-03-06 17:36:55 -08003097 "is out of range [0-%d] %d %d.\n", txq_id,
3098 index, q->n_bd, q->write_ptr, q->read_ptr);
3099 return;
3100 }
3101
3102 for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
3103 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
3104 if (nfreed > 1) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003105 IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
Tomas Winkler91c066f2008-03-06 17:36:55 -08003106 q->write_ptr, q->read_ptr);
3107 queue_work(priv->workqueue, &priv->restart);
3108 break;
3109 }
3110 nfreed++;
3111 }
3112}
3113
3114
3115/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003116 * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003117 * @rxb: Rx buffer to reclaim
3118 *
3119 * If an Rx buffer has an async callback associated with it the callback
3120 * will be executed. The attached skb (if present) will only be freed
3121 * if the callback returns 1
3122 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003123static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003124 struct iwl_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003125{
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003126 struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003127 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3128 int txq_id = SEQ_TO_QUEUE(sequence);
3129 int index = SEQ_TO_INDEX(sequence);
Tomas Winkler600c0e12008-12-19 10:37:04 +08003130 int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
Zhu Yib481de92007-09-25 17:54:57 -07003131 int cmd_index;
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08003132 struct iwl_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003133
Zhu Yib481de92007-09-25 17:54:57 -07003134 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3135
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003136 cmd_index = get_cmd_index(&priv->txq39[IWL_CMD_QUEUE_NUM].q, index, huge);
3137 cmd = &priv->txq39[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
Zhu Yib481de92007-09-25 17:54:57 -07003138
3139 /* Input error checking is done when commands are added to queue. */
3140 if (cmd->meta.flags & CMD_WANT_SKB) {
3141 cmd->meta.source->u.skb = rxb->skb;
3142 rxb->skb = NULL;
3143 } else if (cmd->meta.u.callback &&
3144 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3145 rxb->skb = NULL;
3146
Tomas Winkler91c066f2008-03-06 17:36:55 -08003147 iwl3945_cmd_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003148
3149 if (!(cmd->meta.flags & CMD_ASYNC)) {
3150 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3151 wake_up_interruptible(&priv->wait_command_queue);
3152 }
3153}
3154
3155/************************** RX-FUNCTIONS ****************************/
3156/*
3157 * Rx theory of operation
3158 *
3159 * The host allocates 32 DMA target addresses and passes the host address
3160 * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
3161 * 0 to 31
3162 *
3163 * Rx Queue Indexes
3164 * The host/firmware share two index registers for managing the Rx buffers.
3165 *
3166 * The READ index maps to the first position that the firmware may be writing
3167 * to -- the driver can read up to (but not including) this position and get
3168 * good data.
3169 * The READ index is managed by the firmware once the card is enabled.
3170 *
3171 * The WRITE index maps to the last position the driver has read from -- the
3172 * position preceding WRITE is the last slot the firmware can place a packet.
3173 *
3174 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3175 * WRITE = READ.
3176 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003177 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003178 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3179 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003180 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003181 * and fire the RX interrupt. The driver can then query the READ index and
3182 * process as many packets as possible, moving the WRITE index forward as it
3183 * resets the Rx queue buffers with new memory.
3184 *
3185 * The management in the driver is as follows:
3186 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3187 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003188 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003189 * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003190 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3191 * 'processed' and 'read' driver indexes as well)
3192 * + A received packet is processed and handed to the kernel network stack,
3193 * detached from the iwl->rxq. The driver 'processed' index is updated.
3194 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3195 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3196 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3197 * were enough free buffers and RX_STALLED is set it is cleared.
3198 *
3199 *
3200 * Driver sequence:
3201 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003202 * iwl3945_rx_queue_alloc() Allocates rx_free
3203 * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003204 * iwl3945_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003205 * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003206 * queue, updates firmware pointers, and updates
3207 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003208 * are available, schedules iwl3945_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003209 *
3210 * -- enable interrupts --
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003211 * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003212 * READ INDEX, detaching the SKB from the pool.
3213 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003214 * Calls iwl3945_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07003215 * slots.
3216 * ...
3217 *
3218 */
3219
3220/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003221 * iwl3945_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07003222 */
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003223static int iwl3945_rx_queue_space(const struct iwl_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003224{
3225 int s = q->read - q->write;
3226 if (s <= 0)
3227 s += RX_QUEUE_SIZE;
3228 /* keep some buffer to not confuse full and empty queue */
3229 s -= 2;
3230 if (s < 0)
3231 s = 0;
3232 return s;
3233}
3234
3235/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003236 * iwl3945_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07003237 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003238int iwl3945_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07003239{
3240 u32 reg = 0;
3241 int rc = 0;
3242 unsigned long flags;
3243
3244 spin_lock_irqsave(&q->lock, flags);
3245
3246 if (q->need_update == 0)
3247 goto exit_unlock;
3248
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003249 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07003250 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003251 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003252
3253 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003254 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003255 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3256 goto exit_unlock;
3257 }
3258
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003259 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003260 if (rc)
3261 goto exit_unlock;
3262
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003263 /* Device expects a multiple of 8 */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003264 iwl_write_direct32(priv, FH39_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07003265 q->write & ~0x7);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003266 iwl_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003267
3268 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07003269 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003270 /* Device expects a multiple of 8 */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003271 iwl_write32(priv, FH39_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07003272
3273
3274 q->need_update = 0;
3275
3276 exit_unlock:
3277 spin_unlock_irqrestore(&q->lock, flags);
3278 return rc;
3279}
3280
3281/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003282 * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07003283 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003284static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003285 dma_addr_t dma_addr)
3286{
3287 return cpu_to_le32((u32)dma_addr);
3288}
3289
3290/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003291 * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07003292 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003293 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07003294 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08003295 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07003296 *
3297 * This moves the 'write' index forward to catch up with 'processed', and
3298 * also updates the memory address in the firmware to reference the new
3299 * target buffer.
3300 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003301static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003302{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003303 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003304 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003305 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003306 unsigned long flags;
3307 int write, rc;
3308
3309 spin_lock_irqsave(&rxq->lock, flags);
3310 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003311 while ((iwl3945_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003312 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07003313 element = rxq->rx_free.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003314 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07003315 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003316
3317 /* Point to Rx buffer via next RBD in circular buffer */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003318 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003319 rxq->queue[rxq->write] = rxb;
3320 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
3321 rxq->free_count--;
3322 }
3323 spin_unlock_irqrestore(&rxq->lock, flags);
3324 /* If the pre-allocated buffer pool is dropping low, schedule to
3325 * refill it */
3326 if (rxq->free_count <= RX_LOW_WATERMARK)
3327 queue_work(priv->workqueue, &priv->rx_replenish);
3328
3329
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003330 /* If we've added more space for the firmware to place data, tell it.
3331 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07003332 if ((write != (rxq->write & ~0x7))
3333 || (abs(rxq->write - rxq->read) > 7)) {
3334 spin_lock_irqsave(&rxq->lock, flags);
3335 rxq->need_update = 1;
3336 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003337 rc = iwl3945_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07003338 if (rc)
3339 return rc;
3340 }
3341
3342 return 0;
3343}
3344
3345/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003346 * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07003347 *
3348 * When moving to rx_free an SKB is allocated for the slot.
3349 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003350 * Also restock the Rx queue via iwl3945_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08003351 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07003352 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003353static void iwl3945_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003354{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003355 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003356 struct list_head *element;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003357 struct iwl_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07003358 unsigned long flags;
3359 spin_lock_irqsave(&rxq->lock, flags);
3360 while (!list_empty(&rxq->rx_used)) {
3361 element = rxq->rx_used.next;
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003362 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003363
3364 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07003365 rxb->skb =
3366 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
3367 if (!rxb->skb) {
3368 if (net_ratelimit())
Tomas Winkler978785a2008-12-19 10:37:31 +08003369 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
Zhu Yib481de92007-09-25 17:54:57 -07003370 /* We don't reschedule replenish work here -- we will
3371 * call the restock method and if it still needs
3372 * more buffers it will schedule replenish */
3373 break;
3374 }
Zhu Yi12342c42007-12-20 11:27:32 +08003375
3376 /* If radiotap head is required, reserve some headroom here.
3377 * The physical head count is a variable rx_stats->phy_count.
3378 * We reserve 4 bytes here. Plus these extra bytes, the
3379 * headroom of the physical head should be enough for the
3380 * radiotap head that iwl3945 supported. See iwl3945_rt.
3381 */
3382 skb_reserve(rxb->skb, 4);
3383
Zhu Yib481de92007-09-25 17:54:57 -07003384 priv->alloc_rxb_skb++;
3385 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003386
3387 /* Get physical address of RB/SKB */
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003388 rxb->real_dma_addr =
Zhu Yib481de92007-09-25 17:54:57 -07003389 pci_map_single(priv->pci_dev, rxb->skb->data,
3390 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3391 list_add_tail(&rxb->list, &rxq->rx_free);
3392 rxq->free_count++;
3393 }
3394 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003395}
3396
3397/*
3398 * this should be called while priv->lock is locked
3399 */
Tomas Winkler4fd1f842007-12-05 20:59:58 +02003400static void __iwl3945_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003401{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003402 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003403
3404 iwl3945_rx_allocate(priv);
3405 iwl3945_rx_queue_restock(priv);
3406}
3407
3408
3409void iwl3945_rx_replenish(void *data)
3410{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003411 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003412 unsigned long flags;
3413
3414 iwl3945_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003415
3416 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003417 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003418 spin_unlock_irqrestore(&priv->lock, flags);
3419}
3420
3421/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08003422 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07003423 * This free routine walks the list of POOL entries and if SKB is set to
3424 * non NULL it is unmapped and freed
3425 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003426static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003427{
3428 int i;
3429 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
3430 if (rxq->pool[i].skb != NULL) {
3431 pci_unmap_single(priv->pci_dev,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003432 rxq->pool[i].real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003433 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3434 dev_kfree_skb(rxq->pool[i].skb);
3435 }
3436 }
3437
3438 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
3439 rxq->dma_addr);
3440 rxq->bd = NULL;
3441}
3442
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003443int iwl3945_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003444{
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003445 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003446 struct pci_dev *dev = priv->pci_dev;
3447 int i;
3448
3449 spin_lock_init(&rxq->lock);
3450 INIT_LIST_HEAD(&rxq->rx_free);
3451 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003452
3453 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07003454 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
3455 if (!rxq->bd)
3456 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003457
Zhu Yib481de92007-09-25 17:54:57 -07003458 /* Fill the rx_used queue with _all_ of the Rx buffers */
3459 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
3460 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003461
Zhu Yib481de92007-09-25 17:54:57 -07003462 /* Set us so that we have processed and used all buffers, but have
3463 * not restocked the Rx queue with fresh buffers */
3464 rxq->read = rxq->write = 0;
3465 rxq->free_count = 0;
3466 rxq->need_update = 0;
3467 return 0;
3468}
3469
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003470void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07003471{
3472 unsigned long flags;
3473 int i;
3474 spin_lock_irqsave(&rxq->lock, flags);
3475 INIT_LIST_HEAD(&rxq->rx_free);
3476 INIT_LIST_HEAD(&rxq->rx_used);
3477 /* Fill the rx_used queue with _all_ of the Rx buffers */
3478 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
3479 /* In the reset function, these buffers may have been allocated
3480 * to an SKB, so we need to unmap and free potential storage */
3481 if (rxq->pool[i].skb != NULL) {
3482 pci_unmap_single(priv->pci_dev,
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003483 rxq->pool[i].real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003484 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3485 priv->alloc_rxb_skb--;
3486 dev_kfree_skb(rxq->pool[i].skb);
3487 rxq->pool[i].skb = NULL;
3488 }
3489 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
3490 }
3491
3492 /* Set us so that we have processed and used all buffers, but have
3493 * not restocked the Rx queue with fresh buffers */
3494 rxq->read = rxq->write = 0;
3495 rxq->free_count = 0;
3496 spin_unlock_irqrestore(&rxq->lock, flags);
3497}
3498
3499/* Convert linear signal-to-noise ratio into dB */
3500static u8 ratio2dB[100] = {
3501/* 0 1 2 3 4 5 6 7 8 9 */
3502 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
3503 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
3504 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
3505 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
3506 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
3507 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
3508 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
3509 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
3510 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
3511 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
3512};
3513
3514/* Calculates a relative dB value from a ratio of linear
3515 * (i.e. not dB) signal levels.
3516 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003517int iwl3945_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07003518{
Adrian Bunk221c80c2008-02-02 23:19:01 +02003519 /* 1000:1 or higher just report as 60 dB */
3520 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07003521 return 60;
3522
Adrian Bunk221c80c2008-02-02 23:19:01 +02003523 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07003524 * add 20 dB to make up for divide by 10 */
Adrian Bunk221c80c2008-02-02 23:19:01 +02003525 if (sig_ratio >= 100)
Tomas Winkler3ac7f142008-07-21 02:40:14 +03003526 return 20 + (int)ratio2dB[sig_ratio/10];
Zhu Yib481de92007-09-25 17:54:57 -07003527
3528 /* We shouldn't see this */
3529 if (sig_ratio < 1)
3530 return 0;
3531
3532 /* Use table for ratios 1:1 - 99:1 */
3533 return (int)ratio2dB[sig_ratio];
3534}
3535
3536#define PERFECT_RSSI (-20) /* dBm */
3537#define WORST_RSSI (-95) /* dBm */
3538#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
3539
3540/* Calculate an indication of rx signal quality (a percentage, not dBm!).
3541 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
3542 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003543int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07003544{
3545 int sig_qual;
3546 int degradation = PERFECT_RSSI - rssi_dbm;
3547
3548 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
3549 * as indicator; formula is (signal dbm - noise dbm).
3550 * SNR at or above 40 is a great signal (100%).
3551 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
3552 * Weakest usable signal is usually 10 - 15 dB SNR. */
3553 if (noise_dbm) {
3554 if (rssi_dbm - noise_dbm >= 40)
3555 return 100;
3556 else if (rssi_dbm < noise_dbm)
3557 return 0;
3558 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
3559
3560 /* Else use just the signal level.
3561 * This formula is a least squares fit of data points collected and
3562 * compared with a reference system that had a percentage (%) display
3563 * for signal quality. */
3564 } else
3565 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
3566 (15 * RSSI_RANGE + 62 * degradation)) /
3567 (RSSI_RANGE * RSSI_RANGE);
3568
3569 if (sig_qual > 100)
3570 sig_qual = 100;
3571 else if (sig_qual < 1)
3572 sig_qual = 0;
3573
3574 return sig_qual;
3575}
3576
3577/**
Ben Cahill9fbab512007-11-29 11:09:47 +08003578 * iwl3945_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07003579 *
3580 * Uses the priv->rx_handlers callback function array to invoke
3581 * the appropriate handlers, including command responses,
3582 * frame-received notifications, and other notifications.
3583 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003584static void iwl3945_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003585{
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003586 struct iwl_rx_mem_buffer *rxb;
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003587 struct iwl_rx_packet *pkt;
Abhijeet Kolekarcc2f3622008-12-19 10:37:25 +08003588 struct iwl_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07003589 u32 r, i;
3590 int reclaim;
3591 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003592 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08003593 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07003594
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003595 /* uCode's read index (stored in shared DRAM) indicates the last Rx
3596 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003597 r = iwl3945_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003598 i = rxq->read;
3599
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003600 if (iwl3945_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
3601 fill_rx = 1;
Zhu Yib481de92007-09-25 17:54:57 -07003602 /* Rx interrupt, but nothing sent from uCode */
3603 if (i == r)
3604 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
3605
3606 while (i != r) {
3607 rxb = rxq->queue[i];
3608
Ben Cahill9fbab512007-11-29 11:09:47 +08003609 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07003610 * then a bug has been introduced in the queue refilling
3611 * routines -- catch it here */
3612 BUG_ON(rxb == NULL);
3613
3614 rxq->queue[i] = NULL;
3615
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003616 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003617 IWL_RX_BUF_SIZE,
3618 PCI_DMA_FROMDEVICE);
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08003619 pkt = (struct iwl_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003620
3621 /* Reclaim a command buffer only if this packet is a response
3622 * to a (driver-originated) command.
3623 * If the packet (e.g. Rx frame) originated from uCode,
3624 * there is no command buffer to reclaim.
3625 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
3626 * but apparently a few don't get set; catch them here. */
3627 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
3628 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
3629 (pkt->hdr.cmd != REPLY_TX);
3630
3631 /* Based on type of command response or notification,
3632 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003633 * rx_handlers table. See iwl3945_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07003634 if (priv->rx_handlers[pkt->hdr.cmd]) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003635 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003636 "r = %d, i = %d, %s, 0x%02x\n", r, i,
3637 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
3638 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
3639 } else {
3640 /* No handling needed */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003641 IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
Zhu Yib481de92007-09-25 17:54:57 -07003642 "r %d i %d No handler needed for %s, 0x%02x\n",
3643 r, i, get_cmd_string(pkt->hdr.cmd),
3644 pkt->hdr.cmd);
3645 }
3646
3647 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08003648 /* Invoke any callbacks, transfer the skb to caller, and
3649 * fire off the (possibly) blocking iwl3945_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07003650 * as we reclaim the driver command queue */
3651 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003652 iwl3945_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07003653 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003654 IWL_WARN(priv, "Claim null rxb?\n");
Zhu Yib481de92007-09-25 17:54:57 -07003655 }
3656
3657 /* For now we just don't re-use anything. We can tweak this
3658 * later to try and re-use notification packets and SKBs that
3659 * fail to Rx correctly */
3660 if (rxb->skb != NULL) {
3661 priv->alloc_rxb_skb--;
3662 dev_kfree_skb_any(rxb->skb);
3663 rxb->skb = NULL;
3664 }
3665
Abhijeet Kolekar6100b582008-12-19 10:37:24 +08003666 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
Zhu Yib481de92007-09-25 17:54:57 -07003667 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
3668 spin_lock_irqsave(&rxq->lock, flags);
3669 list_add_tail(&rxb->list, &priv->rxq.rx_used);
3670 spin_unlock_irqrestore(&rxq->lock, flags);
3671 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08003672 /* If there are a lot of unused frames,
3673 * restock the Rx queue so ucode won't assert. */
3674 if (fill_rx) {
3675 count++;
3676 if (count >= 8) {
3677 priv->rxq.read = i;
3678 __iwl3945_rx_replenish(priv);
3679 count = 0;
3680 }
3681 }
Zhu Yib481de92007-09-25 17:54:57 -07003682 }
3683
3684 /* Backtrack one entry */
3685 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003686 iwl3945_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003687}
3688
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003689/**
3690 * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
3691 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003692static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003693 struct iwl3945_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07003694{
3695 u32 reg = 0;
3696 int rc = 0;
3697 int txq_id = txq->q.id;
3698
3699 if (txq->need_update == 0)
3700 return rc;
3701
3702 /* if we're trying to save power */
3703 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
3704 /* wake up nic if it's powered down ...
3705 * uCode will wake up, and interrupt us again, so next
3706 * time we'll skip this part. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003707 reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07003708
3709 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
3710 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003711 iwl_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07003712 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3713 return rc;
3714 }
3715
3716 /* restore this queue's parameters in nic hardware. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003717 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003718 if (rc)
3719 return rc;
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003720 iwl_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003721 txq->q.write_ptr | (txq_id << 8));
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003722 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003723
3724 /* else not in power-save mode, uCode will never sleep when we're
3725 * trying to tx (during RFKILL, we're not trying to tx). */
3726 } else
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003727 iwl_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003728 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07003729
3730 txq->need_update = 0;
3731
3732 return rc;
3733}
3734
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003735#ifdef CONFIG_IWL3945_DEBUG
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003736static void iwl3945_print_rx_config_cmd(struct iwl_priv *priv,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003737 struct iwl3945_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07003738{
3739 IWL_DEBUG_RADIO("RX CONFIG:\n");
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003740 iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003741 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
3742 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
3743 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
3744 le32_to_cpu(rxon->filter_flags));
3745 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
3746 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
3747 rxon->ofdm_basic_rates);
3748 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Johannes Berge1749612008-10-27 15:59:26 -07003749 IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
3750 IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07003751 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
3752}
3753#endif
3754
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003755static void iwl3945_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003756{
3757 IWL_DEBUG_ISR("Enabling interrupts\n");
3758 set_bit(STATUS_INT_ENABLED, &priv->status);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003759 iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07003760}
3761
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003762
3763/* call this function to flush any scheduled tasklet */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003764static inline void iwl_synchronize_irq(struct iwl_priv *priv)
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003765{
Tomas Winklera96a27f2008-10-23 23:48:56 -07003766 /* wait to make sure we flush pending tasklet*/
Mohamed Abbas0359fac2008-03-28 16:21:08 -07003767 synchronize_irq(priv->pci_dev->irq);
3768 tasklet_kill(&priv->irq_tasklet);
3769}
3770
3771
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003772static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003773{
3774 clear_bit(STATUS_INT_ENABLED, &priv->status);
3775
3776 /* disable interrupts from uCode/NIC to host */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003777 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07003778
3779 /* acknowledge/clear/reset any interrupts still pending
3780 * from uCode or flow handler (Rx/Tx DMA) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003781 iwl_write32(priv, CSR_INT, 0xffffffff);
3782 iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07003783 IWL_DEBUG_ISR("Disabled interrupts\n");
3784}
3785
3786static const char *desc_lookup(int i)
3787{
3788 switch (i) {
3789 case 1:
3790 return "FAIL";
3791 case 2:
3792 return "BAD_PARAM";
3793 case 3:
3794 return "BAD_CHECKSUM";
3795 case 4:
3796 return "NMI_INTERRUPT";
3797 case 5:
3798 return "SYSASSERT";
3799 case 6:
3800 return "FATAL_ERROR";
3801 }
3802
3803 return "UNKNOWN";
3804}
3805
3806#define ERROR_START_OFFSET (1 * sizeof(u32))
3807#define ERROR_ELEM_SIZE (7 * sizeof(u32))
3808
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003809static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003810{
3811 u32 i;
3812 u32 desc, time, count, base, data1;
3813 u32 blink1, blink2, ilink1, ilink2;
3814 int rc;
3815
3816 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
3817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003818 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003819 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003820 return;
3821 }
3822
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003823 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003824 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003825 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003826 return;
3827 }
3828
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003829 count = iwl_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07003830
3831 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003832 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
3833 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
3834 priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07003835 }
3836
Winkler, Tomas15b16872008-12-19 10:37:33 +08003837 IWL_ERR(priv, "Desc Time asrtPC blink2 "
Zhu Yib481de92007-09-25 17:54:57 -07003838 "ilink1 nmiPC Line\n");
3839 for (i = ERROR_START_OFFSET;
3840 i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
3841 i += ERROR_ELEM_SIZE) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003842 desc = iwl_read_targ_mem(priv, base + i);
Zhu Yib481de92007-09-25 17:54:57 -07003843 time =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003844 iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003845 blink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003846 iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003847 blink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003848 iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003849 ilink1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003850 iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003851 ilink2 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003852 iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003853 data1 =
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003854 iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07003855
Winkler, Tomas15b16872008-12-19 10:37:33 +08003856 IWL_ERR(priv,
3857 "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
3858 desc_lookup(desc), desc, time, blink1, blink2,
3859 ilink1, ilink2, data1);
Zhu Yib481de92007-09-25 17:54:57 -07003860 }
3861
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003862 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003863
3864}
3865
Ben Cahillf58177b2007-11-29 11:09:43 +08003866#define EVENT_START_OFFSET (6 * sizeof(u32))
Zhu Yib481de92007-09-25 17:54:57 -07003867
3868/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003869 * iwl3945_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07003870 *
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003871 * NOTE: Must be called with iwl_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07003872 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003873static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07003874 u32 num_events, u32 mode)
3875{
3876 u32 i;
3877 u32 base; /* SRAM byte address of event log header */
3878 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
3879 u32 ptr; /* SRAM byte address of log data */
3880 u32 ev, time, data; /* event log data */
3881
3882 if (num_events == 0)
3883 return;
3884
3885 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
3886
3887 if (mode == 0)
3888 event_size = 2 * sizeof(u32);
3889 else
3890 event_size = 3 * sizeof(u32);
3891
3892 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
3893
3894 /* "time" is actually "data" for mode 0 (no timestamp).
3895 * place event id # at far right for easier visual parsing. */
3896 for (i = 0; i < num_events; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003897 ev = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003898 ptr += sizeof(u32);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003899 time = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003900 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003901 if (mode == 0) {
3902 /* data, ev */
3903 IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
3904 } else {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003905 data = iwl_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003906 ptr += sizeof(u32);
Winkler, Tomas15b16872008-12-19 10:37:33 +08003907 IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
Zhu Yib481de92007-09-25 17:54:57 -07003908 }
3909 }
3910}
3911
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003912static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003913{
3914 int rc;
3915 u32 base; /* SRAM byte address of event log header */
3916 u32 capacity; /* event log capacity in # entries */
3917 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
3918 u32 num_wraps; /* # times uCode wrapped to top of log */
3919 u32 next_entry; /* index of next entry to be written by uCode */
3920 u32 size; /* # entries that we'll print */
3921
3922 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003923 if (!iwl3945_hw_valid_rtc_data_addr(base)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003924 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
Zhu Yib481de92007-09-25 17:54:57 -07003925 return;
3926 }
3927
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003928 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003929 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08003930 IWL_WARN(priv, "Can not read from adapter at this time.\n");
Zhu Yib481de92007-09-25 17:54:57 -07003931 return;
3932 }
3933
3934 /* event log header */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003935 capacity = iwl_read_targ_mem(priv, base);
3936 mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
3937 num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
3938 next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07003939
3940 size = num_wraps ? capacity : next_entry;
3941
3942 /* bail out if nothing in log */
3943 if (size == 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08003944 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003945 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003946 return;
3947 }
3948
Winkler, Tomas15b16872008-12-19 10:37:33 +08003949 IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07003950 size, num_wraps);
3951
3952 /* if uCode has wrapped back to top of log, start at the oldest entry,
3953 * i.e the next one that uCode would fill. */
3954 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003955 iwl3945_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07003956 capacity - next_entry, mode);
3957
3958 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003959 iwl3945_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07003960
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08003961 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003962}
3963
3964/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003965 * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07003966 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08003967static void iwl3945_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003968{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003969 /* Set the FW error flag -- cleared on iwl3945_down */
Zhu Yib481de92007-09-25 17:54:57 -07003970 set_bit(STATUS_FW_ERROR, &priv->status);
3971
3972 /* Cancel currently queued command. */
3973 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3974
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003975#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08003976 if (priv->debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003977 iwl3945_dump_nic_error_log(priv);
3978 iwl3945_dump_nic_event_log(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003979 iwl3945_print_rx_config_cmd(priv, &priv->staging39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07003980 }
3981#endif
3982
3983 wake_up_interruptible(&priv->wait_command_queue);
3984
3985 /* Keep the restart process from trying to send host
3986 * commands by clearing the INIT status bit */
3987 clear_bit(STATUS_READY, &priv->status);
3988
3989 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3990 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
3991 "Restarting adapter due to uCode error.\n");
3992
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003993 if (iwl3945_is_associated(priv)) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08003994 memcpy(&priv->recovery39_rxon, &priv->active39_rxon,
3995 sizeof(priv->recovery39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07003996 priv->error_recovering = 1;
3997 }
3998 queue_work(priv->workqueue, &priv->restart);
3999 }
4000}
4001
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004002static void iwl3945_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004003{
4004 unsigned long flags;
4005
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004006 memcpy(&priv->staging39_rxon, &priv->recovery39_rxon,
4007 sizeof(priv->staging39_rxon));
4008 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004009 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004010
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004011 iwl3945_add_station(priv, priv->bssid, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07004012
4013 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004014 priv->assoc_id = le16_to_cpu(priv->staging39_rxon.assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07004015 priv->error_recovering = 0;
4016 spin_unlock_irqrestore(&priv->lock, flags);
4017}
4018
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004019static void iwl3945_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004020{
4021 u32 inta, handled = 0;
4022 u32 inta_fh;
4023 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004024#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004025 u32 inta_mask;
4026#endif
4027
4028 spin_lock_irqsave(&priv->lock, flags);
4029
4030 /* Ack/clear/reset pending uCode interrupts.
4031 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4032 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004033 inta = iwl_read32(priv, CSR_INT);
4034 iwl_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004035
4036 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4037 * Any new interrupts that happen after this, either while we're
4038 * in this tasklet, or later, will show up in next ISR/tasklet. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004039 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
4040 iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004041
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004042#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004043 if (priv->debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004044 /* just for debug */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004045 inta_mask = iwl_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004046 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4047 inta, inta_mask, inta_fh);
4048 }
4049#endif
4050
4051 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4052 * atomic, make sure that inta covers all the interrupts that
4053 * we've discovered, even if FH interrupt came in just after
4054 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004055 if (inta_fh & CSR39_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004056 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004057 if (inta_fh & CSR39_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004058 inta |= CSR_INT_BIT_FH_TX;
4059
4060 /* Now service all interrupt bits discovered above. */
4061 if (inta & CSR_INT_BIT_HW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004062 IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004063
4064 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004065 iwl3945_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004066
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004067 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004068
4069 handled |= CSR_INT_BIT_HW_ERR;
4070
4071 spin_unlock_irqrestore(&priv->lock, flags);
4072
4073 return;
4074 }
4075
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004076#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004077 if (priv->debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004078 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004079 if (inta & CSR_INT_BIT_SCD)
4080 IWL_DEBUG_ISR("Scheduler finished to transmit "
4081 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004082
4083 /* Alive notification via Rx interrupt will do the real work */
4084 if (inta & CSR_INT_BIT_ALIVE)
4085 IWL_DEBUG_ISR("Alive interrupt\n");
4086 }
4087#endif
4088 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004089 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004090
Zhu Yib481de92007-09-25 17:54:57 -07004091 /* Error detected by uCode */
4092 if (inta & CSR_INT_BIT_SW_ERR) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004093 IWL_ERR(priv, "Microcode SW error detected. "
4094 "Restarting 0x%X.\n", inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004095 iwl3945_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004096 handled |= CSR_INT_BIT_SW_ERR;
4097 }
4098
4099 /* uCode wakes up after power-down sleep */
4100 if (inta & CSR_INT_BIT_WAKEUP) {
4101 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004102 iwl3945_rx_queue_update_write_ptr(priv, &priv->rxq);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004103 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[0]);
4104 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[1]);
4105 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[2]);
4106 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[3]);
4107 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[4]);
4108 iwl3945_tx_queue_update_write_ptr(priv, &priv->txq39[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004109
4110 handled |= CSR_INT_BIT_WAKEUP;
4111 }
4112
4113 /* All uCode command responses, including Tx command responses,
4114 * Rx "responses" (frame-received notification), and other
4115 * notifications from uCode come through here*/
4116 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004117 iwl3945_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004118 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4119 }
4120
4121 if (inta & CSR_INT_BIT_FH_TX) {
4122 IWL_DEBUG_ISR("Tx interrupt\n");
4123
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004124 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
4125 if (!iwl_grab_nic_access(priv)) {
4126 iwl_write_direct32(priv, FH39_TCSR_CREDIT
Tomas Winklerbddadf82008-12-19 10:37:01 +08004127 (FH39_SRVC_CHNL), 0x0);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004128 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004129 }
4130 handled |= CSR_INT_BIT_FH_TX;
4131 }
4132
4133 if (inta & ~handled)
Winkler, Tomas15b16872008-12-19 10:37:33 +08004134 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
Zhu Yib481de92007-09-25 17:54:57 -07004135
4136 if (inta & ~CSR_INI_SET_MASK) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004137 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
Zhu Yib481de92007-09-25 17:54:57 -07004138 inta & ~CSR_INI_SET_MASK);
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004139 IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004140 }
4141
4142 /* Re-enable all interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004143 /* only Re-enable if disabled by irq */
4144 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4145 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004146
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004147#ifdef CONFIG_IWL3945_DEBUG
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08004148 if (priv->debug_level & (IWL_DL_ISR)) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004149 inta = iwl_read32(priv, CSR_INT);
4150 inta_mask = iwl_read32(priv, CSR_INT_MASK);
4151 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004152 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4153 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4154 }
4155#endif
4156 spin_unlock_irqrestore(&priv->lock, flags);
4157}
4158
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004159static irqreturn_t iwl3945_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004160{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004161 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004162 u32 inta, inta_mask;
4163 u32 inta_fh;
4164 if (!priv)
4165 return IRQ_NONE;
4166
4167 spin_lock(&priv->lock);
4168
4169 /* Disable (but don't clear!) interrupts here to avoid
4170 * back-to-back ISRs and sporadic interrupts from our NIC.
4171 * If we have something to service, the tasklet will re-enable ints.
4172 * If we *don't* have something, we'll re-enable before leaving here. */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004173 inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
4174 iwl_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004175
4176 /* Discover which interrupts are active/pending */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004177 inta = iwl_read32(priv, CSR_INT);
4178 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004179
4180 /* Ignore interrupt if there's nothing in NIC to service.
4181 * This may be due to IRQ shared with another device,
4182 * or due to sporadic interrupts thrown from our NIC. */
4183 if (!inta && !inta_fh) {
4184 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4185 goto none;
4186 }
4187
4188 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
4189 /* Hardware disappeared */
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004190 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004191 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004192 }
4193
4194 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4195 inta, inta_mask, inta_fh);
4196
Joonwoo Park25c03d82008-01-23 10:15:20 -08004197 inta &= ~CSR_INT_BIT_SCD;
4198
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004199 /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004200 if (likely(inta || inta_fh))
4201 tasklet_schedule(&priv->irq_tasklet);
Oliver Neukumcb4da1a2007-11-13 21:10:32 -08004202unplugged:
Zhu Yib481de92007-09-25 17:54:57 -07004203 spin_unlock(&priv->lock);
4204
4205 return IRQ_HANDLED;
4206
4207 none:
4208 /* re-enable interrupts here since we don't have anything to service. */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07004209 /* only Re-enable if disabled by irq */
4210 if (test_bit(STATUS_INT_ENABLED, &priv->status))
4211 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004212 spin_unlock(&priv->lock);
4213 return IRQ_NONE;
4214}
4215
4216/************************** EEPROM BANDS ****************************
4217 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004218 * The iwl3945_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07004219 * EEPROM contents to the specific channel number supported for each
4220 * band.
4221 *
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004222 * For example, iwl3945_priv->eeprom39.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07004223 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
4224 * The specific geography and calibration information for that channel
4225 * is contained in the eeprom map itself.
4226 *
4227 * During init, we copy the eeprom information and channel map
4228 * information into priv->channel_info_24/52 and priv->channel_map_24/52
4229 *
4230 * channel_map_24/52 provides the index in the channel_info array for a
4231 * given channel. We have to have two separate maps as there is channel
4232 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
4233 * band_2
4234 *
4235 * A value of 0xff stored in the channel_map indicates that the channel
4236 * is not supported by the hardware at all.
4237 *
4238 * A value of 0xfe in the channel_map indicates that the channel is not
4239 * valid for Tx with the current hardware. This means that
4240 * while the system can tune and receive on a given channel, it may not
4241 * be able to associate or transmit any frames on that
4242 * channel. There is no corresponding channel information for that
4243 * entry.
4244 *
4245 *********************************************************************/
4246
4247/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004248static const u8 iwl3945_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07004249 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
4250};
4251
4252/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08004253static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004254 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
4255};
4256
Ben Cahill9fbab512007-11-29 11:09:47 +08004257static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004258 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
4259};
4260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004261static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004262 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
4263};
4264
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004265static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07004266 145, 149, 153, 157, 161, 165
4267};
4268
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004269static void iwl3945_init_band_reference(const struct iwl_priv *priv, int band,
Zhu Yib481de92007-09-25 17:54:57 -07004270 int *eeprom_ch_count,
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004271 const struct iwl_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07004272 **eeprom_ch_info,
4273 const u8 **eeprom_ch_index)
4274{
4275 switch (band) {
4276 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004277 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004278 *eeprom_ch_info = priv->eeprom39.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004279 *eeprom_ch_index = iwl3945_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07004280 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004281 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004282 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004283 *eeprom_ch_info = priv->eeprom39.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004284 *eeprom_ch_index = iwl3945_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07004285 break;
4286 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004287 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004288 *eeprom_ch_info = priv->eeprom39.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004289 *eeprom_ch_index = iwl3945_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07004290 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004291 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004292 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004293 *eeprom_ch_info = priv->eeprom39.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004294 *eeprom_ch_index = iwl3945_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07004295 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08004296 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004297 *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004298 *eeprom_ch_info = priv->eeprom39.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004299 *eeprom_ch_index = iwl3945_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07004300 break;
4301 default:
4302 BUG();
4303 return;
4304 }
4305}
4306
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004307/**
4308 * iwl3945_get_channel_info - Find driver's private channel info
4309 *
4310 * Based on band and channel number.
4311 */
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004312const struct iwl_channel_info *
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004313iwl3945_get_channel_info(const struct iwl_priv *priv,
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004314 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07004315{
4316 int i;
4317
Johannes Berg8318d782008-01-24 19:38:38 +01004318 switch (band) {
4319 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004320 for (i = 14; i < priv->channel_count; i++) {
4321 if (priv->channel_info[i].channel == channel)
4322 return &priv->channel_info[i];
4323 }
4324 break;
4325
Johannes Berg8318d782008-01-24 19:38:38 +01004326 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07004327 if (channel >= 1 && channel <= 14)
4328 return &priv->channel_info[channel - 1];
4329 break;
Johannes Berg8318d782008-01-24 19:38:38 +01004330 case IEEE80211_NUM_BANDS:
4331 WARN_ON(1);
Zhu Yib481de92007-09-25 17:54:57 -07004332 }
4333
4334 return NULL;
4335}
4336
4337#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
4338 ? # x " " : "")
4339
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004340/**
4341 * iwl3945_init_channel_map - Set up driver's info for all possible channels
4342 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004343static int iwl3945_init_channel_map(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004344{
4345 int eeprom_ch_count = 0;
4346 const u8 *eeprom_ch_index = NULL;
Samuel Ortiz0f741d92008-12-19 10:37:10 +08004347 const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07004348 int band, ch;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004349 struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004350
4351 if (priv->channel_count) {
4352 IWL_DEBUG_INFO("Channel map already initialized.\n");
4353 return 0;
4354 }
4355
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004356 if (priv->eeprom39.version < 0x2f) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08004357 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08004358 priv->eeprom39.version);
Zhu Yib481de92007-09-25 17:54:57 -07004359 return -EINVAL;
4360 }
4361
4362 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
4363
4364 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004365 ARRAY_SIZE(iwl3945_eeprom_band_1) +
4366 ARRAY_SIZE(iwl3945_eeprom_band_2) +
4367 ARRAY_SIZE(iwl3945_eeprom_band_3) +
4368 ARRAY_SIZE(iwl3945_eeprom_band_4) +
4369 ARRAY_SIZE(iwl3945_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07004370
4371 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
4372
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004373 priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07004374 priv->channel_count, GFP_KERNEL);
4375 if (!priv->channel_info) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004376 IWL_ERR(priv, "Could not allocate channel_info\n");
Zhu Yib481de92007-09-25 17:54:57 -07004377 priv->channel_count = 0;
4378 return -ENOMEM;
4379 }
4380
4381 ch_info = priv->channel_info;
4382
4383 /* Loop through the 5 EEPROM bands adding them in order to the
4384 * channel map we maintain (that contains additional information than
4385 * what just in the EEPROM) */
4386 for (band = 1; band <= 5; band++) {
4387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004388 iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07004389 &eeprom_ch_info, &eeprom_ch_index);
4390
4391 /* Loop through each band adding each of the channels */
4392 for (ch = 0; ch < eeprom_ch_count; ch++) {
4393 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01004394 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
4395 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07004396
4397 /* permanently store EEPROM's channel regulatory flags
4398 * and max power in channel info database. */
4399 ch_info->eeprom = eeprom_ch_info[ch];
4400
4401 /* Copy the run-time flags so they are there even on
4402 * invalid channels */
4403 ch_info->flags = eeprom_ch_info[ch].flags;
4404
4405 if (!(is_channel_valid(ch_info))) {
4406 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
4407 "No traffic\n",
4408 ch_info->channel,
4409 ch_info->flags,
4410 is_channel_a_band(ch_info) ?
4411 "5.2" : "2.4");
4412 ch_info++;
4413 continue;
4414 }
4415
4416 /* Initialize regulatory-based run-time data */
4417 ch_info->max_power_avg = ch_info->curr_txpow =
4418 eeprom_ch_info[ch].max_power_avg;
4419 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
4420 ch_info->min_power = 0;
4421
Guy Cohenfe7c4042008-04-21 15:41:56 -07004422 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07004423 " %ddBm): Ad-Hoc %ssupported\n",
4424 ch_info->channel,
4425 is_channel_a_band(ch_info) ?
4426 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02004427 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07004428 CHECK_AND_PRINT(IBSS),
4429 CHECK_AND_PRINT(ACTIVE),
4430 CHECK_AND_PRINT(RADAR),
4431 CHECK_AND_PRINT(WIDE),
Zhu Yib481de92007-09-25 17:54:57 -07004432 CHECK_AND_PRINT(DFS),
4433 eeprom_ch_info[ch].flags,
4434 eeprom_ch_info[ch].max_power_avg,
4435 ((eeprom_ch_info[ch].
4436 flags & EEPROM_CHANNEL_IBSS)
4437 && !(eeprom_ch_info[ch].
4438 flags & EEPROM_CHANNEL_RADAR))
4439 ? "" : "not ");
4440
4441 /* Set the user_txpower_limit to the highest power
4442 * supported by any channel */
4443 if (eeprom_ch_info[ch].max_power_avg >
4444 priv->user_txpower_limit)
4445 priv->user_txpower_limit =
4446 eeprom_ch_info[ch].max_power_avg;
4447
4448 ch_info++;
4449 }
4450 }
4451
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004452 /* Set up txpower settings in driver for all channels */
Zhu Yib481de92007-09-25 17:54:57 -07004453 if (iwl3945_txpower_set_from_eeprom(priv))
4454 return -EIO;
4455
4456 return 0;
4457}
4458
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004459/*
4460 * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
4461 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004462static void iwl3945_free_channel_map(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004463{
4464 kfree(priv->channel_info);
4465 priv->channel_count = 0;
4466}
4467
Zhu Yib481de92007-09-25 17:54:57 -07004468/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
4469 * sending probe req. This should be set long enough to hear probe responses
4470 * from more than one AP. */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004471#define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
4472#define IWL_ACTIVE_DWELL_TIME_52 (20)
4473
4474#define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
4475#define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
Zhu Yib481de92007-09-25 17:54:57 -07004476
4477/* For faster active scanning, scan will move to the next channel if fewer than
4478 * PLCP_QUIET_THRESH packets are heard on this channel within
4479 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
4480 * time if it's a quiet channel (nothing responded to our probe, and there's
4481 * no other traffic).
4482 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
4483#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004484#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
Zhu Yib481de92007-09-25 17:54:57 -07004485
4486/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
4487 * Must be set longer than active dwell time.
4488 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
4489#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
4490#define IWL_PASSIVE_DWELL_TIME_52 (10)
4491#define IWL_PASSIVE_DWELL_BASE (100)
4492#define IWL_CHANNEL_TUNE_TIME 5
4493
Kolekar, Abhijeete720ce92008-11-07 09:58:42 -08004494#define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004495
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004496static inline u16 iwl3945_get_active_dwell_time(struct iwl_priv *priv,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004497 enum ieee80211_band band,
4498 u8 n_probes)
Zhu Yib481de92007-09-25 17:54:57 -07004499{
Johannes Berg8318d782008-01-24 19:38:38 +01004500 if (band == IEEE80211_BAND_5GHZ)
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004501 return IWL_ACTIVE_DWELL_TIME_52 +
4502 IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004503 else
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004504 return IWL_ACTIVE_DWELL_TIME_24 +
4505 IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
Zhu Yib481de92007-09-25 17:54:57 -07004506}
4507
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004508static u16 iwl3945_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004509 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07004510{
Johannes Berg8318d782008-01-24 19:38:38 +01004511 u16 passive = (band == IEEE80211_BAND_2GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07004512 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
4513 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
4514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004515 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004516 /* If we're associated, we clamp the maximum passive
4517 * dwell time to be 98% of the beacon interval (minus
4518 * 2 * channel tune time) */
4519 passive = priv->beacon_int;
4520 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
4521 passive = IWL_PASSIVE_DWELL_BASE;
4522 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
4523 }
4524
Zhu Yib481de92007-09-25 17:54:57 -07004525 return passive;
4526}
4527
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004528static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01004529 enum ieee80211_band band,
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004530 u8 is_active, u8 n_probes,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004531 struct iwl3945_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07004532{
4533 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01004534 const struct ieee80211_supported_band *sband;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004535 const struct iwl_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07004536 u16 passive_dwell = 0;
4537 u16 active_dwell = 0;
4538 int added, i;
4539
Johannes Berg8318d782008-01-24 19:38:38 +01004540 sband = iwl3945_get_band(priv, band);
4541 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07004542 return 0;
4543
Johannes Berg8318d782008-01-24 19:38:38 +01004544 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07004545
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08004546 active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
Johannes Berg8318d782008-01-24 19:38:38 +01004547 passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07004548
Abhijeet Kolekar8f4807a2008-09-03 11:26:31 +08004549 if (passive_dwell <= active_dwell)
4550 passive_dwell = active_dwell + 1;
4551
Johannes Berg8318d782008-01-24 19:38:38 +01004552 for (i = 0, added = 0; i < sband->n_channels; i++) {
Johannes Berg182e2e62008-04-04 10:41:56 +02004553 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4554 continue;
4555
Johannes Berg8318d782008-01-24 19:38:38 +01004556 scan_ch->channel = channels[i].hw_value;
Zhu Yib481de92007-09-25 17:54:57 -07004557
Johannes Berg8318d782008-01-24 19:38:38 +01004558 ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07004559 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08004560 IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
Zhu Yib481de92007-09-25 17:54:57 -07004561 scan_ch->channel);
4562 continue;
4563 }
4564
Zhu Yib481de92007-09-25 17:54:57 -07004565 scan_ch->active_dwell = cpu_to_le16(active_dwell);
4566 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
Abhijeet Kolekar011a0332008-12-02 12:14:07 -08004567 /* If passive , set up for auto-switch
4568 * and use long active_dwell time.
4569 */
4570 if (!is_active || is_channel_passive(ch_info) ||
4571 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
4572 scan_ch->type = 0; /* passive */
4573 if (IWL_UCODE_API(priv->ucode_ver) == 1)
4574 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
4575 } else {
4576 scan_ch->type = 1; /* active */
4577 }
4578
4579 /* Set direct probe bits. These may be used both for active
4580 * scan channels (probes gets sent right away),
4581 * or for passive channels (probes get se sent only after
4582 * hearing clear Rx packet).*/
4583 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
4584 if (n_probes)
4585 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4586 } else {
4587 /* uCode v1 does not allow setting direct probe bits on
4588 * passive channel. */
4589 if ((scan_ch->type & 1) && n_probes)
4590 scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
4591 }
Zhu Yib481de92007-09-25 17:54:57 -07004592
Ben Cahill9fbab512007-11-29 11:09:47 +08004593 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07004594 scan_ch->tpc.dsp_atten = 110;
4595 /* scan_pwr_info->tpc.dsp_atten; */
4596
4597 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01004598 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07004599 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
4600 else {
4601 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
4602 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08004603 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08004604 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07004605 */
4606 }
4607
4608 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
4609 scan_ch->channel,
4610 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
4611 (scan_ch->type & 1) ?
4612 active_dwell : passive_dwell);
4613
4614 scan_ch++;
4615 added++;
4616 }
4617
4618 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
4619 return added;
4620}
4621
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004622static void iwl3945_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004623 struct ieee80211_rate *rates)
4624{
4625 int i;
4626
4627 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01004628 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
4629 rates[i].hw_value = i; /* Rate scaling will work on indexes */
4630 rates[i].hw_value_short = i;
4631 rates[i].flags = 0;
Samuel Ortizd9829a62008-12-19 10:37:12 +08004632 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07004633 /*
Johannes Berg8318d782008-01-24 19:38:38 +01004634 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07004635 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004636 rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
Johannes Berg8318d782008-01-24 19:38:38 +01004637 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07004638 }
Zhu Yib481de92007-09-25 17:54:57 -07004639 }
4640}
4641
4642/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004643 * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07004644 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004645static int iwl3945_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004646{
Samuel Ortizd20b3c62008-12-19 10:37:15 +08004647 struct iwl_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004648 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07004649 struct ieee80211_channel *channels;
4650 struct ieee80211_channel *geo_ch;
4651 struct ieee80211_rate *rates;
4652 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07004653
Johannes Berg8318d782008-01-24 19:38:38 +01004654 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
4655 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07004656 IWL_DEBUG_INFO("Geography modes already initialized.\n");
4657 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4658 return 0;
4659 }
4660
Zhu Yib481de92007-09-25 17:54:57 -07004661 channels = kzalloc(sizeof(struct ieee80211_channel) *
4662 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01004663 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07004664 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07004665
Tomas Winkler8211ef72008-03-02 01:36:04 +02004666 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07004667 GFP_KERNEL);
4668 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07004669 kfree(channels);
4670 return -ENOMEM;
4671 }
4672
Zhu Yib481de92007-09-25 17:54:57 -07004673 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02004674 sband = &priv->bands[IEEE80211_BAND_5GHZ];
4675 sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
4676 /* just OFDM */
4677 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
4678 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Zhu Yib481de92007-09-25 17:54:57 -07004679
Tomas Winkler8211ef72008-03-02 01:36:04 +02004680 sband = &priv->bands[IEEE80211_BAND_2GHZ];
4681 sband->channels = channels;
4682 /* OFDM & CCK */
4683 sband->bitrates = rates;
4684 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07004685
4686 priv->ieee_channels = channels;
4687 priv->ieee_rates = rates;
4688
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004689 iwl3945_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07004690
Tomas Winkler8211ef72008-03-02 01:36:04 +02004691 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07004692 ch = &priv->channel_info[i];
4693
Tomas Winkler8211ef72008-03-02 01:36:04 +02004694 /* FIXME: might be removed if scan is OK*/
4695 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07004696 continue;
Zhu Yib481de92007-09-25 17:54:57 -07004697
4698 if (is_channel_a_band(ch))
Tomas Winkler8211ef72008-03-02 01:36:04 +02004699 sband = &priv->bands[IEEE80211_BAND_5GHZ];
Johannes Berg8318d782008-01-24 19:38:38 +01004700 else
Tomas Winkler8211ef72008-03-02 01:36:04 +02004701 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004702
Tomas Winkler8211ef72008-03-02 01:36:04 +02004703 geo_ch = &sband->channels[sband->n_channels++];
4704
4705 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01004706 geo_ch->max_power = ch->max_power_avg;
4707 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08004708 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07004709
4710 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01004711 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
4712 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07004713
Johannes Berg8318d782008-01-24 19:38:38 +01004714 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
4715 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07004716
4717 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01004718 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07004719
4720 if (ch->max_power_avg > priv->max_channel_txpower_limit)
4721 priv->max_channel_txpower_limit =
4722 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004723 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01004724 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02004725 }
4726
4727 /* Save flags for reg domain usage */
4728 geo_ch->orig_flags = geo_ch->flags;
4729
4730 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
4731 ch->channel, geo_ch->center_freq,
4732 is_channel_a_band(ch) ? "5.2" : "2.4",
4733 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
4734 "restricted" : "valid",
4735 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07004736 }
4737
Tomas Winkler82b9a122008-03-04 18:09:30 -08004738 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
4739 priv->cfg->sku & IWL_SKU_A) {
Tomas Winkler978785a2008-12-19 10:37:31 +08004740 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
4741 "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
4742 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08004743 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07004744 }
4745
Tomas Winkler978785a2008-12-19 10:37:31 +08004746 IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01004747 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
4748 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07004749
John W. Linvillee0e0a672008-03-25 15:58:40 -04004750 if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4751 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4752 &priv->bands[IEEE80211_BAND_2GHZ];
4753 if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4754 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4755 &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07004756
Zhu Yib481de92007-09-25 17:54:57 -07004757 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
4758
4759 return 0;
4760}
4761
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004762/*
4763 * iwl3945_free_geos - undo allocations in iwl3945_init_geos
4764 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004765static void iwl3945_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004766{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08004767 kfree(priv->ieee_channels);
4768 kfree(priv->ieee_rates);
4769 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
4770}
4771
Zhu Yib481de92007-09-25 17:54:57 -07004772/******************************************************************************
4773 *
4774 * uCode download functions
4775 *
4776 ******************************************************************************/
4777
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004778static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004779{
Tomas Winkler98c92212008-01-14 17:46:20 -08004780 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
4781 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
4782 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
4783 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
4784 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
4785 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07004786}
4787
4788/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004789 * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004790 * looking at all data.
4791 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004792static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004793{
4794 u32 val;
4795 u32 save_len = len;
4796 int rc = 0;
4797 u32 errcnt;
4798
4799 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4800
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004801 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004802 if (rc)
4803 return rc;
4804
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004805 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004806 IWL39_RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07004807
4808 errcnt = 0;
4809 for (; len > 0; len -= sizeof(u32), image++) {
4810 /* read data comes through single port, auto-incr addr */
4811 /* NOTE: Use the debugless read so we don't flood kernel log
4812 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004813 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004814 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004815 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004816 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4817 save_len - len, val, le32_to_cpu(*image));
4818 rc = -EIO;
4819 errcnt++;
4820 if (errcnt >= 20)
4821 break;
4822 }
4823 }
4824
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004825 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004826
4827 if (!errcnt)
Ian Schrambc434dd2007-10-25 17:15:29 +08004828 IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
Zhu Yib481de92007-09-25 17:54:57 -07004829
4830 return rc;
4831}
4832
4833
4834/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004835 * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07004836 * using sample data 100 bytes apart. If these sample points are good,
4837 * it's a pretty good bet that everything between them is good, too.
4838 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004839static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07004840{
4841 u32 val;
4842 int rc = 0;
4843 u32 errcnt = 0;
4844 u32 i;
4845
4846 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
4847
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004848 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004849 if (rc)
4850 return rc;
4851
4852 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
4853 /* read data comes through single port, auto-incr addr */
4854 /* NOTE: Use the debugless read so we don't flood kernel log
4855 * if IWL_DL_IO is set */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004856 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08004857 i + IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004858 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07004859 if (val != le32_to_cpu(*image)) {
4860#if 0 /* Enable this if you want to see details */
Winkler, Tomas15b16872008-12-19 10:37:33 +08004861 IWL_ERR(priv, "uCode INST section is invalid at "
Zhu Yib481de92007-09-25 17:54:57 -07004862 "offset 0x%x, is 0x%x, s/b 0x%x\n",
4863 i, val, *image);
4864#endif
4865 rc = -EIO;
4866 errcnt++;
4867 if (errcnt >= 3)
4868 break;
4869 }
4870 }
4871
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004872 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004873
4874 return rc;
4875}
4876
4877
4878/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004879 * iwl3945_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07004880 * and verify its contents
4881 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004882static int iwl3945_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004883{
4884 __le32 *image;
4885 u32 len;
4886 int rc = 0;
4887
4888 /* Try bootstrap */
4889 image = (__le32 *)priv->ucode_boot.v_addr;
4890 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004891 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004892 if (rc == 0) {
4893 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
4894 return 0;
4895 }
4896
4897 /* Try initialize */
4898 image = (__le32 *)priv->ucode_init.v_addr;
4899 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004900 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004901 if (rc == 0) {
4902 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
4903 return 0;
4904 }
4905
4906 /* Try runtime/protocol */
4907 image = (__le32 *)priv->ucode_code.v_addr;
4908 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004909 rc = iwl3945_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004910 if (rc == 0) {
4911 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
4912 return 0;
4913 }
4914
Winkler, Tomas15b16872008-12-19 10:37:33 +08004915 IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
Zhu Yib481de92007-09-25 17:54:57 -07004916
Ben Cahill9fbab512007-11-29 11:09:47 +08004917 /* Since nothing seems to match, show first several data entries in
4918 * instruction SRAM, so maybe visual inspection will give a clue.
4919 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07004920 image = (__le32 *)priv->ucode_boot.v_addr;
4921 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004922 rc = iwl3945_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07004923
4924 return rc;
4925}
4926
4927
4928/* check contents of special bootstrap uCode SRAM */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004929static int iwl3945_verify_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004930{
4931 __le32 *image = priv->ucode_boot.v_addr;
4932 u32 len = priv->ucode_boot.len;
4933 u32 reg;
4934 u32 val;
4935
4936 IWL_DEBUG_INFO("Begin verify bsm\n");
4937
4938 /* verify BSM SRAM contents */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004939 val = iwl_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07004940 for (reg = BSM_SRAM_LOWER_BOUND;
4941 reg < BSM_SRAM_LOWER_BOUND + len;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03004942 reg += sizeof(u32), image++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08004943 val = iwl_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07004944 if (val != le32_to_cpu(*image)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08004945 IWL_ERR(priv, "BSM uCode verification failed at "
Zhu Yib481de92007-09-25 17:54:57 -07004946 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
4947 BSM_SRAM_LOWER_BOUND,
4948 reg - BSM_SRAM_LOWER_BOUND, len,
4949 val, le32_to_cpu(*image));
4950 return -EIO;
4951 }
4952 }
4953
4954 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
4955
4956 return 0;
4957}
4958
4959/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004960 * iwl3945_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07004961 *
4962 * BSM operation:
4963 *
4964 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
4965 * in special SRAM that does not power down during RFKILL. When powering back
4966 * up after power-saving sleeps (or during initial uCode load), the BSM loads
4967 * the bootstrap program into the on-board processor, and starts it.
4968 *
4969 * The bootstrap program loads (via DMA) instructions and data for a new
4970 * program from host DRAM locations indicated by the host driver in the
4971 * BSM_DRAM_* registers. Once the new program is loaded, it starts
4972 * automatically.
4973 *
4974 * When initializing the NIC, the host driver points the BSM to the
4975 * "initialize" uCode image. This uCode sets up some internal data, then
4976 * notifies host via "initialize alive" that it is complete.
4977 *
4978 * The host then replaces the BSM_DRAM_* pointer values to point to the
4979 * normal runtime uCode instructions and a backup uCode data cache buffer
4980 * (filled initially with starting data values for the on-board processor),
4981 * then triggers the "initialize" uCode to load and launch the runtime uCode,
4982 * which begins normal operation.
4983 *
4984 * When doing a power-save shutdown, runtime uCode saves data SRAM into
4985 * the backup data cache in DRAM before SRAM is powered down.
4986 *
4987 * When powering back up, the BSM loads the bootstrap program. This reloads
4988 * the runtime uCode instructions and the backup data cache into SRAM,
4989 * and re-launches the runtime uCode from where it left off.
4990 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08004991static int iwl3945_load_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004992{
4993 __le32 *image = priv->ucode_boot.v_addr;
4994 u32 len = priv->ucode_boot.len;
4995 dma_addr_t pinst;
4996 dma_addr_t pdata;
4997 u32 inst_len;
4998 u32 data_len;
4999 int rc;
5000 int i;
5001 u32 done;
5002 u32 reg_offset;
5003
5004 IWL_DEBUG_INFO("Begin load bsm\n");
5005
5006 /* make sure bootstrap program is no larger than BSM's SRAM size */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005007 if (len > IWL39_MAX_BSM_SIZE)
Zhu Yib481de92007-09-25 17:54:57 -07005008 return -EINVAL;
5009
5010 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005011 * in host DRAM ... host DRAM physical address bits 31:0 for 3945.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005012 * NOTE: iwl3945_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005013 * after the "initialize" uCode has run, to point to
5014 * runtime/protocol instructions and backup data cache. */
5015 pinst = priv->ucode_init.p_addr;
5016 pdata = priv->ucode_init_data.p_addr;
5017 inst_len = priv->ucode_init.len;
5018 data_len = priv->ucode_init_data.len;
5019
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005020 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005021 if (rc)
5022 return rc;
5023
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005024 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5025 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5026 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5027 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005028
5029 /* Fill BSM memory with bootstrap instructions */
5030 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5031 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5032 reg_offset += sizeof(u32), image++)
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005033 _iwl_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005034 le32_to_cpu(*image));
5035
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005036 rc = iwl3945_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005037 if (rc) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005038 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005039 return rc;
5040 }
5041
5042 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005043 iwl_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5044 iwl_write_prph(priv, BSM_WR_MEM_DST_REG,
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005045 IWL39_RTC_INST_LOWER_BOUND);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005046 iwl_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005047
5048 /* Load bootstrap code into instruction SRAM now,
5049 * to prepare to load "initialize" uCode */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005050 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005051 BSM_WR_CTRL_REG_BIT_START);
5052
5053 /* Wait for load of bootstrap uCode to finish */
5054 for (i = 0; i < 100; i++) {
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005055 done = iwl_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005056 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5057 break;
5058 udelay(10);
5059 }
5060 if (i < 100)
5061 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5062 else {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005063 IWL_ERR(priv, "BSM write did not complete!\n");
Zhu Yib481de92007-09-25 17:54:57 -07005064 return -EIO;
5065 }
5066
5067 /* Enable future boot loads whenever power management unit triggers it
5068 * (e.g. when powering back up after power-save shutdown) */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005069 iwl_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005070 BSM_WR_CTRL_REG_BIT_START_EN);
5071
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005072 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005073
5074 return 0;
5075}
5076
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005077static void iwl3945_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005078{
5079 /* Remove all resets to allow NIC to operate */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005080 iwl_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005081}
5082
5083/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005084 * iwl3945_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005085 *
5086 * Copy into buffers for card to fetch via bus-mastering
5087 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005088static int iwl3945_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005089{
Kolekar, Abhijeeta78fe752008-12-19 10:37:21 +08005090 struct iwl_ucode *ucode;
Reinette Chatrea0987a82008-12-02 12:14:06 -08005091 int ret = -EINVAL, index;
Zhu Yib481de92007-09-25 17:54:57 -07005092 const struct firmware *ucode_raw;
5093 /* firmware file name contains uCode/driver compatibility version */
Reinette Chatrea0987a82008-12-02 12:14:06 -08005094 const char *name_pre = priv->cfg->fw_name_pre;
5095 const unsigned int api_max = priv->cfg->ucode_api_max;
5096 const unsigned int api_min = priv->cfg->ucode_api_min;
5097 char buf[25];
Zhu Yib481de92007-09-25 17:54:57 -07005098 u8 *src;
5099 size_t len;
Reinette Chatrea0987a82008-12-02 12:14:06 -08005100 u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
Zhu Yib481de92007-09-25 17:54:57 -07005101
5102 /* Ask kernel firmware_class module to get the boot firmware off disk.
5103 * request_firmware() is synchronous, file is in memory on return. */
Reinette Chatrea0987a82008-12-02 12:14:06 -08005104 for (index = api_max; index >= api_min; index--) {
5105 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
5106 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
5107 if (ret < 0) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005108 IWL_ERR(priv, "%s firmware file req failed: %d\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08005109 buf, ret);
5110 if (ret == -ENOENT)
5111 continue;
5112 else
5113 goto error;
5114 } else {
5115 if (index < api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08005116 IWL_ERR(priv, "Loaded firmware %s, "
5117 "which is deprecated. "
5118 " Please use API v%u instead.\n",
Reinette Chatrea0987a82008-12-02 12:14:06 -08005119 buf, api_max);
5120 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5121 buf, ucode_raw->size);
5122 break;
5123 }
Zhu Yib481de92007-09-25 17:54:57 -07005124 }
5125
Reinette Chatrea0987a82008-12-02 12:14:06 -08005126 if (ret < 0)
5127 goto error;
Zhu Yib481de92007-09-25 17:54:57 -07005128
5129 /* Make sure that we got at least our header! */
5130 if (ucode_raw->size < sizeof(*ucode)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005131 IWL_ERR(priv, "File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005132 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005133 goto err_release;
5134 }
5135
5136 /* Data from ucode file: header followed by uCode images */
5137 ucode = (void *)ucode_raw->data;
5138
Chatre, Reinettec02b3ac2008-12-02 12:14:05 -08005139 priv->ucode_ver = le32_to_cpu(ucode->ver);
Reinette Chatrea0987a82008-12-02 12:14:06 -08005140 api_ver = IWL_UCODE_API(priv->ucode_ver);
Zhu Yib481de92007-09-25 17:54:57 -07005141 inst_size = le32_to_cpu(ucode->inst_size);
5142 data_size = le32_to_cpu(ucode->data_size);
5143 init_size = le32_to_cpu(ucode->init_size);
5144 init_data_size = le32_to_cpu(ucode->init_data_size);
5145 boot_size = le32_to_cpu(ucode->boot_size);
5146
Reinette Chatrea0987a82008-12-02 12:14:06 -08005147 /* api_ver should match the api version forming part of the
5148 * firmware filename ... but we don't check for that and only rely
5149 * on the API version read from firware header from here on forward */
5150
5151 if (api_ver < api_min || api_ver > api_max) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005152 IWL_ERR(priv, "Driver unable to support your firmware API. "
Reinette Chatrea0987a82008-12-02 12:14:06 -08005153 "Driver supports v%u, firmware is v%u.\n",
5154 api_max, api_ver);
5155 priv->ucode_ver = 0;
5156 ret = -EINVAL;
5157 goto err_release;
5158 }
5159 if (api_ver != api_max)
Winkler, Tomas15b16872008-12-19 10:37:33 +08005160 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
Reinette Chatrea0987a82008-12-02 12:14:06 -08005161 "got %u. New firmware can be obtained "
5162 "from http://www.intellinuxwireless.org.\n",
5163 api_max, api_ver);
5164
Tomas Winkler978785a2008-12-19 10:37:31 +08005165 IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
5166 IWL_UCODE_MAJOR(priv->ucode_ver),
5167 IWL_UCODE_MINOR(priv->ucode_ver),
5168 IWL_UCODE_API(priv->ucode_ver),
5169 IWL_UCODE_SERIAL(priv->ucode_ver));
5170
Reinette Chatrea0987a82008-12-02 12:14:06 -08005171 IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
5172 priv->ucode_ver);
Ian Schrambc434dd2007-10-25 17:15:29 +08005173 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
5174 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
5175 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
5176 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
5177 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
Zhu Yib481de92007-09-25 17:54:57 -07005178
Reinette Chatrea0987a82008-12-02 12:14:06 -08005179
Zhu Yib481de92007-09-25 17:54:57 -07005180 /* Verify size of file vs. image size info in file's header */
5181 if (ucode_raw->size < sizeof(*ucode) +
5182 inst_size + data_size + init_size +
5183 init_data_size + boot_size) {
5184
5185 IWL_DEBUG_INFO("uCode file size %d too small\n",
5186 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005187 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005188 goto err_release;
5189 }
5190
5191 /* Verify that uCode images will fit in card's SRAM */
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005192 if (inst_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005193 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5194 inst_size);
5195 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005196 goto err_release;
5197 }
5198
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005199 if (data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005200 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5201 data_size);
5202 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005203 goto err_release;
5204 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005205 if (init_size > IWL39_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005206 IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
5207 init_size);
5208 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005209 goto err_release;
5210 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005211 if (init_data_size > IWL39_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005212 IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
5213 init_data_size);
5214 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005215 goto err_release;
5216 }
Samuel Ortiz250bdd22008-12-19 10:37:11 +08005217 if (boot_size > IWL39_MAX_BSM_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005218 IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
5219 boot_size);
5220 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005221 goto err_release;
5222 }
5223
5224 /* Allocate ucode buffers for card's bus-master loading ... */
5225
5226 /* Runtime instructions and 2 copies of data:
5227 * 1) unmodified from disk
5228 * 2) backup cache for save/restore during power-downs */
5229 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005230 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07005231
5232 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005233 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07005234
5235 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005236 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07005237
5238 if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
Tomas Winkler90e759d2007-11-29 11:09:41 +08005239 !priv->ucode_data_backup.v_addr)
Zhu Yib481de92007-09-25 17:54:57 -07005240 goto err_pci_alloc;
5241
Tomas Winkler90e759d2007-11-29 11:09:41 +08005242 /* Initialization instructions and data */
5243 if (init_size && init_data_size) {
5244 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005245 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005246
5247 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005248 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005249
5250 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
5251 goto err_pci_alloc;
5252 }
5253
5254 /* Bootstrap (instructions only, no data) */
5255 if (boot_size) {
5256 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08005257 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005258
5259 if (!priv->ucode_boot.v_addr)
5260 goto err_pci_alloc;
5261 }
5262
Zhu Yib481de92007-09-25 17:54:57 -07005263 /* Copy images into buffers for card's bus-master reads ... */
5264
5265 /* Runtime instructions (first block of data in file) */
5266 src = &ucode->data[0];
5267 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005268 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005269 memcpy(priv->ucode_code.v_addr, src, len);
5270 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
5271 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
5272
5273 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005274 * NOTE: Copy into backup buffer will be done in iwl3945_up() */
Zhu Yib481de92007-09-25 17:54:57 -07005275 src = &ucode->data[inst_size];
5276 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005277 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07005278 memcpy(priv->ucode_data.v_addr, src, len);
5279 memcpy(priv->ucode_data_backup.v_addr, src, len);
5280
5281 /* Initialization instructions (3rd block) */
5282 if (init_size) {
5283 src = &ucode->data[inst_size + data_size];
5284 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005285 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
5286 len);
Zhu Yib481de92007-09-25 17:54:57 -07005287 memcpy(priv->ucode_init.v_addr, src, len);
5288 }
5289
5290 /* Initialization data (4th block) */
5291 if (init_data_size) {
5292 src = &ucode->data[inst_size + data_size + init_size];
5293 len = priv->ucode_init_data.len;
5294 IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
5295 (int)len);
5296 memcpy(priv->ucode_init_data.v_addr, src, len);
5297 }
5298
5299 /* Bootstrap instructions (5th block) */
5300 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
5301 len = priv->ucode_boot.len;
5302 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
5303 (int)len);
5304 memcpy(priv->ucode_boot.v_addr, src, len);
5305
5306 /* We have our copies now, allow OS release its copies */
5307 release_firmware(ucode_raw);
5308 return 0;
5309
5310 err_pci_alloc:
Winkler, Tomas15b16872008-12-19 10:37:33 +08005311 IWL_ERR(priv, "failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005312 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005313 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005314
5315 err_release:
5316 release_firmware(ucode_raw);
5317
5318 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08005319 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07005320}
5321
5322
5323/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005324 * iwl3945_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07005325 *
5326 * Tell initialization uCode where to find runtime uCode.
5327 *
5328 * BSM registers initially contain pointers to initialization uCode.
5329 * We need to replace them to load runtime uCode inst and data,
5330 * and to save runtime data when powering down.
5331 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005332static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005333{
5334 dma_addr_t pinst;
5335 dma_addr_t pdata;
5336 int rc = 0;
5337 unsigned long flags;
5338
5339 /* bits 31:0 for 3945 */
5340 pinst = priv->ucode_code.p_addr;
5341 pdata = priv->ucode_data_backup.p_addr;
5342
5343 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005344 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005345 if (rc) {
5346 spin_unlock_irqrestore(&priv->lock, flags);
5347 return rc;
5348 }
5349
5350 /* Tell bootstrap uCode where to find image to load */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005351 iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5352 iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5353 iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005354 priv->ucode_data.len);
5355
Tomas Winklera96a27f2008-10-23 23:48:56 -07005356 /* Inst byte count must be last to set up, bit 31 signals uCode
Zhu Yib481de92007-09-25 17:54:57 -07005357 * that all new ptr/size info is in place */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005358 iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005359 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
5360
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005361 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005362
5363 spin_unlock_irqrestore(&priv->lock, flags);
5364
5365 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
5366
5367 return rc;
5368}
5369
5370/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005371 * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005372 *
5373 * Called after REPLY_ALIVE notification received from "initialize" uCode.
5374 *
Zhu Yib481de92007-09-25 17:54:57 -07005375 * Tell "initialize" uCode to go ahead and load the runtime uCode.
Ben Cahill9fbab512007-11-29 11:09:47 +08005376 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005377static void iwl3945_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005378{
5379 /* Check alive response for "valid" sign from uCode */
5380 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
5381 /* We had an error bringing up the hardware, so take it
5382 * all the way back down so we can try again */
5383 IWL_DEBUG_INFO("Initialize Alive failed.\n");
5384 goto restart;
5385 }
5386
5387 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
5388 * This is a paranoid check, because we would not have gotten the
5389 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005390 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005391 /* Runtime instruction load was bad;
5392 * take it all the way back down so we can try again */
5393 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
5394 goto restart;
5395 }
5396
5397 /* Send pointers to protocol/runtime uCode image ... init code will
5398 * load and launch runtime uCode, which will send us another "Alive"
5399 * notification. */
5400 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005401 if (iwl3945_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005402 /* Runtime instruction load won't happen;
5403 * take it all the way back down so we can try again */
5404 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
5405 goto restart;
5406 }
5407 return;
5408
5409 restart:
5410 queue_work(priv->workqueue, &priv->restart);
5411}
5412
5413
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005414/* temporary */
5415static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
5416 struct sk_buff *skb);
5417
Zhu Yib481de92007-09-25 17:54:57 -07005418/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005419 * iwl3945_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07005420 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005421 * Alive gets handled by iwl3945_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07005422 */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005423static void iwl3945_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005424{
5425 int rc = 0;
5426 int thermal_spin = 0;
5427 u32 rfkill;
5428
5429 IWL_DEBUG_INFO("Runtime Alive received.\n");
5430
5431 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
5432 /* We had an error bringing up the hardware, so take it
5433 * all the way back down so we can try again */
5434 IWL_DEBUG_INFO("Alive failed.\n");
5435 goto restart;
5436 }
5437
5438 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
5439 * This is a paranoid check, because we would not have gotten the
5440 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005441 if (iwl3945_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005442 /* Runtime instruction load was bad;
5443 * take it all the way back down so we can try again */
5444 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
5445 goto restart;
5446 }
5447
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005448 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005449
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005450 rc = iwl_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005451 if (rc) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005452 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
Zhu Yib481de92007-09-25 17:54:57 -07005453 return;
5454 }
5455
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005456 rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005457 IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005458 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005459
5460 if (rfkill & 0x1) {
5461 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Tomas Winklera96a27f2008-10-23 23:48:56 -07005462 /* if RFKILL is not on, then wait for thermal
Zhu Yib481de92007-09-25 17:54:57 -07005463 * sensor in adapter to kick in */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005464 while (iwl3945_hw_get_temperature(priv) == 0) {
Zhu Yib481de92007-09-25 17:54:57 -07005465 thermal_spin++;
5466 udelay(10);
5467 }
5468
5469 if (thermal_spin)
5470 IWL_DEBUG_INFO("Thermal calibration took %dus\n",
5471 thermal_spin * 10);
5472 } else
5473 set_bit(STATUS_RF_KILL_HW, &priv->status);
5474
Ben Cahill9fbab512007-11-29 11:09:47 +08005475 /* After the ALIVE response, we can send commands to 3945 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07005476 set_bit(STATUS_ALIVE, &priv->status);
5477
5478 /* Clear out the uCode error bit if it is set */
5479 clear_bit(STATUS_FW_ERROR, &priv->status);
5480
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005481 if (iwl_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07005482 return;
5483
Johannes Berg36d68252008-05-15 12:55:26 +02005484 ieee80211_wake_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07005485
5486 priv->active_rate = priv->rates_mask;
5487 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
5488
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005489 iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07005490
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005491 if (iwl3945_is_associated(priv)) {
5492 struct iwl3945_rxon_cmd *active_rxon =
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005493 (struct iwl3945_rxon_cmd *)(&priv->active39_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07005494
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005495 memcpy(&priv->staging39_rxon, &priv->active39_rxon,
5496 sizeof(priv->staging39_rxon));
Zhu Yib481de92007-09-25 17:54:57 -07005497 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
5498 } else {
5499 /* Initialize our rx_config data */
Zhu, Yi60294de2008-10-29 14:05:45 -07005500 iwl3945_connection_init_rx_config(priv, priv->iw_mode);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005501 memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07005502 }
5503
Ben Cahill9fbab512007-11-29 11:09:47 +08005504 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005505 iwl3945_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005506
5507 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005508 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005509
Zhu Yib481de92007-09-25 17:54:57 -07005510 iwl3945_reg_txpower_periodic(priv);
5511
Reinette Chatrefe00b5a2008-04-03 16:05:23 -07005512 iwl3945_led_register(priv);
5513
Zhu Yib481de92007-09-25 17:54:57 -07005514 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Rick Farringtona9f46782008-03-18 14:57:49 -07005515 set_bit(STATUS_READY, &priv->status);
Zhu Yi5a669262008-01-14 17:46:18 -08005516 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07005517
5518 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005519 iwl3945_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005520
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08005521 /* reassociate for ADHOC mode */
5522 if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
5523 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
5524 priv->vif);
5525 if (beacon)
5526 iwl3945_mac_beacon_update(priv->hw, beacon);
5527 }
5528
Zhu Yib481de92007-09-25 17:54:57 -07005529 return;
5530
5531 restart:
5532 queue_work(priv->workqueue, &priv->restart);
5533}
5534
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005535static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07005536
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005537static void __iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005538{
5539 unsigned long flags;
5540 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
5541 struct ieee80211_conf *conf = NULL;
5542
5543 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
5544
5545 conf = ieee80211_get_hw_conf(priv->hw);
5546
5547 if (!exit_pending)
5548 set_bit(STATUS_EXIT_PENDING, &priv->status);
5549
Mohamed Abbasab53d8a2008-03-25 16:33:36 -07005550 iwl3945_led_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005551 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005552
5553 /* Unblock any waiting calls */
5554 wake_up_interruptible_all(&priv->wait_command_queue);
5555
Zhu Yib481de92007-09-25 17:54:57 -07005556 /* Wipe out the EXIT_PENDING status bit if we are not actually
5557 * exiting the module */
5558 if (!exit_pending)
5559 clear_bit(STATUS_EXIT_PENDING, &priv->status);
5560
5561 /* stop and reset the on-board processor */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005562 iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07005563
5564 /* tell the device to stop sending interrupts */
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005565 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005566 iwl3945_disable_interrupts(priv);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07005567 spin_unlock_irqrestore(&priv->lock, flags);
5568 iwl_synchronize_irq(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005569
5570 if (priv->mac80211_registered)
5571 ieee80211_stop_queues(priv->hw);
5572
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005573 /* If we have not previously called iwl3945_init() then
Zhu Yib481de92007-09-25 17:54:57 -07005574 * clear all bits but the RF Kill and SUSPEND bits and return */
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005575 if (!iwl_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005576 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5577 STATUS_RF_KILL_HW |
5578 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5579 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005580 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5581 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005582 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005583 STATUS_IN_SUSPEND |
5584 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5585 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005586 goto exit;
5587 }
5588
5589 /* ...otherwise clear out all the status bits but the RF Kill and
5590 * SUSPEND bits and continue taking the NIC down. */
5591 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
5592 STATUS_RF_KILL_HW |
5593 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
5594 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08005595 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
5596 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07005597 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
5598 STATUS_IN_SUSPEND |
5599 test_bit(STATUS_FW_ERROR, &priv->status) <<
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005600 STATUS_FW_ERROR |
5601 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
5602 STATUS_EXIT_PENDING;
Zhu Yib481de92007-09-25 17:54:57 -07005603
5604 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005605 iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07005606 spin_unlock_irqrestore(&priv->lock, flags);
5607
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005608 iwl3945_hw_txq_ctx_stop(priv);
5609 iwl3945_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005610
5611 spin_lock_irqsave(&priv->lock, flags);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005612 if (!iwl_grab_nic_access(priv)) {
5613 iwl_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005614 APMG_CLK_VAL_DMA_CLK_RQT);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005615 iwl_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005616 }
5617 spin_unlock_irqrestore(&priv->lock, flags);
5618
5619 udelay(5);
5620
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005621 iwl3945_hw_nic_stop_master(priv);
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005622 iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005623 iwl3945_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005624
5625 exit:
Tomas Winkler3d24a9f2008-12-19 10:37:07 +08005626 memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07005627
5628 if (priv->ibss_beacon)
5629 dev_kfree_skb(priv->ibss_beacon);
5630 priv->ibss_beacon = NULL;
5631
5632 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005633 iwl3945_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005634}
5635
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005636static void iwl3945_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005637{
5638 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005639 __iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005640 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08005641
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005642 iwl3945_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005643}
5644
5645#define MAX_HW_RESTARTS 5
5646
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005647static int __iwl3945_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005648{
5649 int rc, i;
5650
5651 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005652 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
Zhu Yib481de92007-09-25 17:54:57 -07005653 return -EIO;
5654 }
5655
5656 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005657 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
Zhu Yib481de92007-09-25 17:54:57 -07005658 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005659 return -ENODEV;
5660 }
5661
Reinette Chatree903fbd2008-01-30 22:05:15 -08005662 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005663 IWL_ERR(priv, "ucode not available for device bring up\n");
Reinette Chatree903fbd2008-01-30 22:05:15 -08005664 return -EIO;
5665 }
5666
Zhu Yie655b9f2008-01-24 02:19:38 -08005667 /* If platform's RF_KILL switch is NOT set to KILL */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005668 if (iwl_read32(priv, CSR_GP_CNTRL) &
Zhu Yie655b9f2008-01-24 02:19:38 -08005669 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
5670 clear_bit(STATUS_RF_KILL_HW, &priv->status);
5671 else {
5672 set_bit(STATUS_RF_KILL_HW, &priv->status);
5673 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005674 IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08005675 return -ENODEV;
5676 }
Zhu Yib481de92007-09-25 17:54:57 -07005677 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005678
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005679 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07005680
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005681 rc = iwl3945_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005682 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005683 IWL_ERR(priv, "Unable to int nic\n");
Zhu Yib481de92007-09-25 17:54:57 -07005684 return rc;
5685 }
5686
5687 /* make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005688 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5689 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07005690 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
5691
5692 /* clear (again), then enable host interrupts */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005693 iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005694 iwl3945_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005695
5696 /* really make sure rfkill handshake bits are cleared */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08005697 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
5698 iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07005699
5700 /* Copy original ucode data image from disk into backup cache.
5701 * This will be used to initialize the on-board processor's
5702 * data SRAM for a clean start when the runtime program first loads. */
5703 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a669262008-01-14 17:46:18 -08005704 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07005705
Zhu Yie655b9f2008-01-24 02:19:38 -08005706 /* We return success when we resume from suspend and rf_kill is on. */
5707 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
5708 return 0;
5709
Zhu Yib481de92007-09-25 17:54:57 -07005710 for (i = 0; i < MAX_HW_RESTARTS; i++) {
5711
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005712 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005713
5714 /* load bootstrap state machine,
5715 * load bootstrap program into processor's memory,
5716 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005717 rc = iwl3945_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005718
5719 if (rc) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08005720 IWL_ERR(priv,
5721 "Unable to set up bootstrap uCode: %d\n", rc);
Zhu Yib481de92007-09-25 17:54:57 -07005722 continue;
5723 }
5724
5725 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005726 iwl3945_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005727
Zhu Yib481de92007-09-25 17:54:57 -07005728 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
5729
5730 return 0;
5731 }
5732
5733 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005734 __iwl3945_down(priv);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005735 clear_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005736
5737 /* tried to restart and config the device for as long as our
5738 * patience could withstand */
Winkler, Tomas15b16872008-12-19 10:37:33 +08005739 IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
Zhu Yib481de92007-09-25 17:54:57 -07005740 return -EIO;
5741}
5742
5743
5744/*****************************************************************************
5745 *
5746 * Workqueue callbacks
5747 *
5748 *****************************************************************************/
5749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005750static void iwl3945_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005751{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005752 struct iwl_priv *priv =
5753 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005754
5755 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5756 return;
5757
5758 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005759 iwl3945_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005760 mutex_unlock(&priv->mutex);
5761}
5762
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005763static void iwl3945_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005764{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005765 struct iwl_priv *priv =
5766 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07005767
5768 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5769 return;
5770
5771 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005772 iwl3945_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005773 mutex_unlock(&priv->mutex);
5774}
5775
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005776static void iwl3945_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07005777{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005778 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07005779
5780 wake_up_interruptible(&priv->wait_command_queue);
5781
5782 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5783 return;
5784
5785 mutex_lock(&priv->mutex);
5786
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005787 if (!iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005788 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
5789 "HW and/or SW RF Kill no longer active, restarting "
5790 "device\n");
5791 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
5792 queue_work(priv->workqueue, &priv->restart);
5793 } else {
5794
5795 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
5796 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
5797 "disabled by SW switch\n");
5798 else
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005799 IWL_WARN(priv, "Radio Frequency Kill Switch is On:\n"
Zhu Yib481de92007-09-25 17:54:57 -07005800 "Kill switch must be turned off for "
5801 "wireless networking to work.\n");
5802 }
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08005803
Zhu Yib481de92007-09-25 17:54:57 -07005804 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02005805 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005806}
5807
5808#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
5809
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005810static void iwl3945_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005811{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005812 struct iwl_priv *priv =
5813 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07005814
5815 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
5816 return;
5817
5818 mutex_lock(&priv->mutex);
5819 if (test_bit(STATUS_SCANNING, &priv->status) ||
5820 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5821 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
5822 "Scan completion watchdog resetting adapter (%dms)\n",
5823 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005824
Zhu Yib481de92007-09-25 17:54:57 -07005825 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005826 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005827 }
5828 mutex_unlock(&priv->mutex);
5829}
5830
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005831static void iwl3945_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07005832{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08005833 struct iwl_priv *priv =
5834 container_of(data, struct iwl_priv, request_scan);
Winkler, Tomasc2d79b42008-12-19 10:37:34 +08005835 struct iwl_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07005836 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005837 .len = sizeof(struct iwl3945_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07005838 .meta.flags = CMD_SIZE_HUGE,
5839 };
5840 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005841 struct iwl3945_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07005842 struct ieee80211_conf *conf = NULL;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005843 u8 n_probes = 2;
Johannes Berg8318d782008-01-24 19:38:38 +01005844 enum ieee80211_band band;
John W. Linville9387b7c2008-09-30 20:59:05 -04005845 DECLARE_SSID_BUF(ssid);
Zhu Yib481de92007-09-25 17:54:57 -07005846
5847 conf = ieee80211_get_hw_conf(priv->hw);
5848
5849 mutex_lock(&priv->mutex);
5850
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005851 if (!iwl_is_ready(priv)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005852 IWL_WARN(priv, "request scan called when driver not ready.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005853 goto done;
5854 }
5855
Tomas Winklera96a27f2008-10-23 23:48:56 -07005856 /* Make sure the scan wasn't canceled before this queued work
Zhu Yib481de92007-09-25 17:54:57 -07005857 * was given the chance to run... */
5858 if (!test_bit(STATUS_SCANNING, &priv->status))
5859 goto done;
5860
5861 /* This should never be called or scheduled if there is currently
5862 * a scan active in the hardware. */
5863 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
5864 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
5865 "Ignoring second request.\n");
5866 rc = -EIO;
5867 goto done;
5868 }
5869
5870 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
5871 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
5872 goto done;
5873 }
5874
5875 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
5876 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
5877 goto done;
5878 }
5879
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08005880 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005881 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
5882 goto done;
5883 }
5884
5885 if (!test_bit(STATUS_READY, &priv->status)) {
5886 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
5887 goto done;
5888 }
5889
5890 if (!priv->scan_bands) {
5891 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
5892 goto done;
5893 }
5894
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005895 if (!priv->scan39) {
5896 priv->scan39 = kmalloc(sizeof(struct iwl3945_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07005897 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005898 if (!priv->scan39) {
Zhu Yib481de92007-09-25 17:54:57 -07005899 rc = -ENOMEM;
5900 goto done;
5901 }
5902 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08005903 scan = priv->scan39;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005904 memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07005905
5906 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
5907 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
5908
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005909 if (iwl3945_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005910 u16 interval = 0;
5911 u32 extra;
5912 u32 suspend_time = 100;
5913 u32 scan_suspend_time = 100;
5914 unsigned long flags;
5915
5916 IWL_DEBUG_INFO("Scanning while associated...\n");
5917
5918 spin_lock_irqsave(&priv->lock, flags);
5919 interval = priv->beacon_int;
5920 spin_unlock_irqrestore(&priv->lock, flags);
5921
5922 scan->suspend_time = 0;
Mohamed Abbas15e869d2007-10-25 17:15:46 +08005923 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07005924 if (!interval)
5925 interval = suspend_time;
5926 /*
5927 * suspend time format:
5928 * 0-19: beacon interval in usec (time before exec.)
5929 * 20-23: 0
5930 * 24-31: number of beacons (suspend between channels)
5931 */
5932
5933 extra = (suspend_time / interval) << 24;
5934 scan_suspend_time = 0xFF0FFFFF &
5935 (extra | ((suspend_time % interval) * 1024));
5936
5937 scan->suspend_time = cpu_to_le32(scan_suspend_time);
5938 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
5939 scan_suspend_time, interval);
5940 }
5941
5942 /* We should add the ability for user to lock to PASSIVE ONLY */
5943 if (priv->one_direct_scan) {
5944 IWL_DEBUG_SCAN
5945 ("Kicking off one direct scan for '%s'\n",
John W. Linville9387b7c2008-09-30 20:59:05 -04005946 print_ssid(ssid, priv->direct_ssid,
5947 priv->direct_ssid_len));
Zhu Yib481de92007-09-25 17:54:57 -07005948 scan->direct_scan[0].id = WLAN_EID_SSID;
5949 scan->direct_scan[0].len = priv->direct_ssid_len;
5950 memcpy(scan->direct_scan[0].ssid,
5951 priv->direct_ssid, priv->direct_ssid_len);
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005952 n_probes++;
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005953 } else
Bill Moss786b4552008-04-17 16:03:40 -07005954 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07005955
5956 /* We don't build a direct scan probe request; the uCode will do
5957 * that based on the direct_mask added to each channel entry */
5958 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005959 iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Johannes Berg430cfe92008-10-28 18:06:02 +01005960 IWL_MAX_SCAN_SIZE - sizeof(*scan)));
Zhu Yib481de92007-09-25 17:54:57 -07005961 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08005962 scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
Zhu Yib481de92007-09-25 17:54:57 -07005963 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
5964
5965 /* flags + rate selection */
5966
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005967 if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005968 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
5969 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
5970 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01005971 band = IEEE80211_BAND_2GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005972 } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
Zhu Yib481de92007-09-25 17:54:57 -07005973 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
5974 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01005975 band = IEEE80211_BAND_5GHZ;
Ron Rindjunsky66b50042008-06-25 16:46:31 +08005976 } else {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08005977 IWL_WARN(priv, "Invalid scan band count\n");
Zhu Yib481de92007-09-25 17:54:57 -07005978 goto done;
5979 }
5980
5981 /* select Rx antennas */
5982 scan->flags |= iwl3945_get_antenna_flags(priv);
5983
Johannes Berg05c914f2008-09-11 00:01:58 +02005984 if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
Zhu Yib481de92007-09-25 17:54:57 -07005985 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
5986
Abhijeet Kolekarf9340522008-09-03 11:26:58 +08005987 scan->channel_count =
5988 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
5989 n_probes,
5990 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
Zhu Yib481de92007-09-25 17:54:57 -07005991
Reinette Chatre14b54332008-11-04 12:21:35 -08005992 if (scan->channel_count == 0) {
5993 IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
5994 goto done;
5995 }
5996
Zhu Yib481de92007-09-25 17:54:57 -07005997 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005998 scan->channel_count * sizeof(struct iwl3945_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07005999 cmd.data = scan;
6000 scan->len = cpu_to_le16(cmd.len);
6001
6002 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006003 rc = iwl3945_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006004 if (rc)
6005 goto done;
6006
6007 queue_delayed_work(priv->workqueue, &priv->scan_check,
6008 IWL_SCAN_CHECK_WATCHDOG);
6009
6010 mutex_unlock(&priv->mutex);
6011 return;
6012
6013 done:
Mohamed Abbas2420ebc2008-11-04 12:21:34 -08006014 /* can not perform scan make sure we clear scanning
6015 * bits from status so next scan request can be performed.
6016 * if we dont clear scanning status bit here all next scan
6017 * will fail
6018 */
6019 clear_bit(STATUS_SCAN_HW, &priv->status);
6020 clear_bit(STATUS_SCANNING, &priv->status);
6021
Ian Schram01ebd062007-10-25 17:15:22 +08006022 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006023 queue_work(priv->workqueue, &priv->scan_completed);
6024 mutex_unlock(&priv->mutex);
6025}
6026
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006027static void iwl3945_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006028{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006029 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006030
6031 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6032 return;
6033
6034 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006035 __iwl3945_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006036 mutex_unlock(&priv->mutex);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006037 iwl3945_rfkill_set_hw_state(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006038}
6039
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006040static void iwl3945_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006041{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006042 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006043
6044 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6045 return;
6046
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006047 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006048 queue_work(priv->workqueue, &priv->up);
6049}
6050
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006051static void iwl3945_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006052{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006053 struct iwl_priv *priv =
6054 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006055
6056 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6057 return;
6058
6059 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006060 iwl3945_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006061 mutex_unlock(&priv->mutex);
6062}
6063
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006064#define IWL_DELAY_NEXT_SCAN (HZ*2)
6065
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006066static void iwl3945_post_associate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006067{
Zhu Yib481de92007-09-25 17:54:57 -07006068 int rc = 0;
6069 struct ieee80211_conf *conf = NULL;
6070
Johannes Berg05c914f2008-09-11 00:01:58 +02006071 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006072 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
Zhu Yib481de92007-09-25 17:54:57 -07006073 return;
6074 }
6075
6076
Johannes Berge1749612008-10-27 15:59:26 -07006077 IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006078 priv->assoc_id, priv->active39_rxon.bssid_addr);
Zhu Yib481de92007-09-25 17:54:57 -07006079
6080 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6081 return;
6082
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08006083 if (!priv->vif || !priv->is_open)
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006084 return;
Abhijeet Kolekar322a9812008-09-03 11:26:27 +08006085
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006086 iwl3945_scan_cancel_timeout(priv, 200);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006087
Zhu Yib481de92007-09-25 17:54:57 -07006088 conf = ieee80211_get_hw_conf(priv->hw);
6089
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006090 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006091 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006092
Tomas Winkler28afaf92008-12-19 10:37:06 +08006093 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006094 iwl3945_setup_rxon_timing(priv);
6095 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006096 sizeof(priv->rxon_timing), &priv->rxon_timing);
6097 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006098 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006099 "Attempting to continue.\n");
6100
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006101 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006102
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006103 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006104
6105 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6106 priv->assoc_id, priv->beacon_int);
6107
6108 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006109 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006110 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006111 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006112
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006113 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006114 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006115 priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006116 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006117 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006118
Johannes Berg05c914f2008-09-11 00:01:58 +02006119 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006120 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07006121
6122 }
6123
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006124 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006125
6126 switch (priv->iw_mode) {
Johannes Berg05c914f2008-09-11 00:01:58 +02006127 case NL80211_IFTYPE_STATION:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006128 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006129 break;
6130
Johannes Berg05c914f2008-09-11 00:01:58 +02006131 case NL80211_IFTYPE_ADHOC:
Zhu Yib481de92007-09-25 17:54:57 -07006132
Abhijeet Kolekarce546fd2008-11-19 15:32:22 -08006133 priv->assoc_id = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006134 iwl3945_add_station(priv, priv->bssid, 0, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006135 iwl3945_sync_sta(priv, IWL_STA_ID,
Johannes Berg8318d782008-01-24 19:38:38 +01006136 (priv->band == IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07006137 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
6138 CMD_ASYNC);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006139 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
6140 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006141
6142 break;
6143
6144 default:
Winkler, Tomas15b16872008-12-19 10:37:33 +08006145 IWL_ERR(priv, "%s Should not be called in %d mode\n",
Tomas Winkler3ac7f142008-07-21 02:40:14 +03006146 __func__, priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07006147 break;
6148 }
6149
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006150 iwl3945_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006151
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006152 /* we have just associated, don't start scan too early */
6153 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006154}
6155
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006156static void iwl3945_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006157{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006158 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006159
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006160 if (!iwl_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006161 return;
6162
6163 mutex_lock(&priv->mutex);
6164
6165 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006166 iwl3945_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006167
6168 mutex_unlock(&priv->mutex);
6169}
6170
Johannes Berge8975582008-10-09 12:18:51 +02006171static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006172
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006173static void iwl3945_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006174{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006175 struct iwl_priv *priv =
6176 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006177
6178 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6179
6180 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6181 return;
6182
Zhu Yia0646472007-12-20 14:10:01 +08006183 if (test_bit(STATUS_CONF_PENDING, &priv->status))
Johannes Berge8975582008-10-09 12:18:51 +02006184 iwl3945_mac_config(priv->hw, 0);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006185
Zhu Yib481de92007-09-25 17:54:57 -07006186 ieee80211_scan_completed(priv->hw);
6187
6188 /* Since setting the TXPOWER may have been deferred while
6189 * performing the scan, fire one off */
6190 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006191 iwl3945_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006192 mutex_unlock(&priv->mutex);
6193}
6194
6195/*****************************************************************************
6196 *
6197 * mac80211 entry point functions
6198 *
6199 *****************************************************************************/
6200
Zhu Yi5a669262008-01-14 17:46:18 -08006201#define UCODE_READY_TIMEOUT (2 * HZ)
6202
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006203static int iwl3945_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006204{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006205 struct iwl_priv *priv = hw->priv;
Zhu Yi5a669262008-01-14 17:46:18 -08006206 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006207
6208 IWL_DEBUG_MAC80211("enter\n");
6209
Zhu Yi5a669262008-01-14 17:46:18 -08006210 if (pci_enable_device(priv->pci_dev)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006211 IWL_ERR(priv, "Fail to pci_enable_device\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006212 return -ENODEV;
6213 }
6214 pci_restore_state(priv->pci_dev);
6215 pci_enable_msi(priv->pci_dev);
6216
6217 ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
6218 DRV_NAME, priv);
6219 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006220 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
Zhu Yi5a669262008-01-14 17:46:18 -08006221 goto out_disable_msi;
6222 }
6223
Zhu Yib481de92007-09-25 17:54:57 -07006224 /* we should be verifying the device is ready to be opened */
6225 mutex_lock(&priv->mutex);
6226
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006227 memset(&priv->staging39_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
Zhu Yi5a669262008-01-14 17:46:18 -08006228 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
6229 * ucode filename and max sizes are card-specific. */
6230
6231 if (!priv->ucode_code.len) {
6232 ret = iwl3945_read_ucode(priv);
6233 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006234 IWL_ERR(priv, "Could not read microcode: %d\n", ret);
Zhu Yi5a669262008-01-14 17:46:18 -08006235 mutex_unlock(&priv->mutex);
6236 goto out_release_irq;
6237 }
6238 }
6239
Zhu Yie655b9f2008-01-24 02:19:38 -08006240 ret = __iwl3945_up(priv);
Zhu Yi5a669262008-01-14 17:46:18 -08006241
Zhu Yib481de92007-09-25 17:54:57 -07006242 mutex_unlock(&priv->mutex);
Zhu Yi5a669262008-01-14 17:46:18 -08006243
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02006244 iwl3945_rfkill_set_hw_state(priv);
6245
Zhu Yie655b9f2008-01-24 02:19:38 -08006246 if (ret)
6247 goto out_release_irq;
6248
6249 IWL_DEBUG_INFO("Start UP work.\n");
6250
6251 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
6252 return 0;
6253
Zhu Yi5a669262008-01-14 17:46:18 -08006254 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
6255 * mac80211 will not be run successfully. */
6256 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
6257 test_bit(STATUS_READY, &priv->status),
6258 UCODE_READY_TIMEOUT);
6259 if (!ret) {
6260 if (!test_bit(STATUS_READY, &priv->status)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08006261 IWL_ERR(priv,
6262 "Wait for START_ALIVE timeout after %dms.\n",
6263 jiffies_to_msecs(UCODE_READY_TIMEOUT));
Zhu Yi5a669262008-01-14 17:46:18 -08006264 ret = -ETIMEDOUT;
6265 goto out_release_irq;
6266 }
6267 }
6268
Zhu Yie655b9f2008-01-24 02:19:38 -08006269 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07006270 IWL_DEBUG_MAC80211("leave\n");
6271 return 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006272
6273out_release_irq:
6274 free_irq(priv->pci_dev->irq, priv);
6275out_disable_msi:
6276 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08006277 pci_disable_device(priv->pci_dev);
6278 priv->is_open = 0;
6279 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a669262008-01-14 17:46:18 -08006280 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006281}
6282
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006283static void iwl3945_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006284{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006285 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006286
6287 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006288
Zhu Yie655b9f2008-01-24 02:19:38 -08006289 if (!priv->is_open) {
6290 IWL_DEBUG_MAC80211("leave - skip\n");
6291 return;
6292 }
6293
Zhu Yib481de92007-09-25 17:54:57 -07006294 priv->is_open = 0;
Zhu Yi5a669262008-01-14 17:46:18 -08006295
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006296 if (iwl_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08006297 /* stop mac, cancel any scan request and clear
6298 * RXON_FILTER_ASSOC_MSK BIT
6299 */
Zhu Yi5a669262008-01-14 17:46:18 -08006300 mutex_lock(&priv->mutex);
6301 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006302 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08006303 }
6304
Zhu Yi5a669262008-01-14 17:46:18 -08006305 iwl3945_down(priv);
6306
6307 flush_workqueue(priv->workqueue);
6308 free_irq(priv->pci_dev->irq, priv);
6309 pci_disable_msi(priv->pci_dev);
6310 pci_save_state(priv->pci_dev);
6311 pci_disable_device(priv->pci_dev);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006312
Zhu Yib481de92007-09-25 17:54:57 -07006313 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006314}
6315
Johannes Berge039fa42008-05-15 12:55:29 +02006316static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006317{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006318 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006319
6320 IWL_DEBUG_MAC80211("enter\n");
6321
Zhu Yib481de92007-09-25 17:54:57 -07006322 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berge039fa42008-05-15 12:55:29 +02006323 ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07006324
Johannes Berge039fa42008-05-15 12:55:29 +02006325 if (iwl3945_tx_skb(priv, skb))
Zhu Yib481de92007-09-25 17:54:57 -07006326 dev_kfree_skb_any(skb);
6327
6328 IWL_DEBUG_MAC80211("leave\n");
Reinette Chatre637f8832009-01-19 15:30:32 -08006329 return NETDEV_TX_OK;
Zhu Yib481de92007-09-25 17:54:57 -07006330}
6331
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006332static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006333 struct ieee80211_if_init_conf *conf)
6334{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006335 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006336 unsigned long flags;
6337
Johannes Berg32bfd352007-12-19 01:31:26 +01006338 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006339
Johannes Berg32bfd352007-12-19 01:31:26 +01006340 if (priv->vif) {
6341 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Tomas Winkler864792e2007-11-27 21:00:52 +02006342 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07006343 }
6344
6345 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01006346 priv->vif = conf->vif;
Zhu, Yi60294de2008-10-29 14:05:45 -07006347 priv->iw_mode = conf->type;
Zhu Yib481de92007-09-25 17:54:57 -07006348
6349 spin_unlock_irqrestore(&priv->lock, flags);
6350
6351 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02006352
6353 if (conf->mac_addr) {
Johannes Berge1749612008-10-27 15:59:26 -07006354 IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
Tomas Winkler864792e2007-11-27 21:00:52 +02006355 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
6356 }
6357
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006358 if (iwl_is_ready(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006359 iwl3945_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07006360
Zhu Yib481de92007-09-25 17:54:57 -07006361 mutex_unlock(&priv->mutex);
6362
Zhu Yi5a669262008-01-14 17:46:18 -08006363 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006364 return 0;
6365}
6366
6367/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006368 * iwl3945_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07006369 *
6370 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
6371 * be set inappropriately and the driver currently sets the hardware up to
6372 * use it whenever needed.
6373 */
Johannes Berge8975582008-10-09 12:18:51 +02006374static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
Zhu Yib481de92007-09-25 17:54:57 -07006375{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006376 struct iwl_priv *priv = hw->priv;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006377 const struct iwl_channel_info *ch_info;
Johannes Berge8975582008-10-09 12:18:51 +02006378 struct ieee80211_conf *conf = &hw->conf;
Zhu Yib481de92007-09-25 17:54:57 -07006379 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08006380 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006381
6382 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01006383 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006384
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006385 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006386 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006387 ret = -EIO;
6388 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006389 }
6390
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006391 if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07006392 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08006393 IWL_DEBUG_MAC80211("leave - scanning\n");
6394 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006395 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08006396 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006397 }
6398
6399 spin_lock_irqsave(&priv->lock, flags);
6400
Johannes Berg8318d782008-01-24 19:38:38 +01006401 ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
6402 conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006403 if (!is_channel_valid(ch_info)) {
Ron Rindjunsky66b50042008-06-25 16:46:31 +08006404 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
Johannes Berg8318d782008-01-24 19:38:38 +01006405 conf->channel->hw_value, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006406 IWL_DEBUG_MAC80211("leave - invalid channel\n");
6407 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006408 ret = -EINVAL;
6409 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006410 }
6411
Johannes Berg8318d782008-01-24 19:38:38 +01006412 iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07006413
Johannes Berg8318d782008-01-24 19:38:38 +01006414 iwl3945_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07006415
6416 /* The list of supported rates and rate mask can be different
6417 * for each phymode; since the phymode may have changed, reset
6418 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006419 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006420
6421 spin_unlock_irqrestore(&priv->lock, flags);
6422
6423#ifdef IEEE80211_CONF_CHANNEL_SWITCH
6424 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006425 iwl3945_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006426 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006427 }
6428#endif
6429
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006430 iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07006431
6432 if (!conf->radio_enabled) {
6433 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006434 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006435 }
6436
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006437 if (iwl_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006438 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08006439 ret = -EIO;
6440 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07006441 }
6442
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006443 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006444
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006445 if (memcmp(&priv->active39_rxon,
6446 &priv->staging39_rxon, sizeof(priv->staging39_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006447 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006448 else
6449 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
6450
6451 IWL_DEBUG_MAC80211("leave\n");
6452
Zhu Yi76bb77e2007-11-22 10:53:22 +08006453out:
Zhu Yia0646472007-12-20 14:10:01 +08006454 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07006455 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08006456 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006457}
6458
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006459static void iwl3945_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006460{
6461 int rc = 0;
6462
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08006463 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006464 return;
6465
6466 /* The following should be done only at AP bring up */
Ron Rindjunsky5d1e2322008-06-30 17:23:04 +08006467 if (!(iwl3945_is_associated(priv))) {
Zhu Yib481de92007-09-25 17:54:57 -07006468
6469 /* RXON - unassoc (to set timing command) */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006470 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006471 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006472
6473 /* RXON Timing */
Tomas Winkler28afaf92008-12-19 10:37:06 +08006474 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006475 iwl3945_setup_rxon_timing(priv);
6476 rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006477 sizeof(priv->rxon_timing), &priv->rxon_timing);
6478 if (rc)
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006479 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
Zhu Yib481de92007-09-25 17:54:57 -07006480 "Attempting to continue.\n");
6481
6482 /* FIXME: what should be the assoc_id for AP? */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006483 priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
Zhu Yib481de92007-09-25 17:54:57 -07006484 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006485 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006486 RXON_FLG_SHORT_PREAMBLE_MSK;
6487 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006488 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006489 ~RXON_FLG_SHORT_PREAMBLE_MSK;
6490
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006491 if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
Zhu Yib481de92007-09-25 17:54:57 -07006492 if (priv->assoc_capability &
6493 WLAN_CAPABILITY_SHORT_SLOT_TIME)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006494 priv->staging39_rxon.flags |=
Zhu Yib481de92007-09-25 17:54:57 -07006495 RXON_FLG_SHORT_SLOT_MSK;
6496 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006497 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006498 ~RXON_FLG_SHORT_SLOT_MSK;
6499
Johannes Berg05c914f2008-09-11 00:01:58 +02006500 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006501 priv->staging39_rxon.flags &=
Zhu Yib481de92007-09-25 17:54:57 -07006502 ~RXON_FLG_SHORT_SLOT_MSK;
6503 }
6504 /* restore RXON assoc */
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006505 priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006506 iwl3945_commit_rxon(priv);
Kolekar, Abhijeetb5323d32008-12-19 10:37:22 +08006507 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
Zhu Yi556f8db2007-09-27 11:27:33 +08006508 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006509 iwl3945_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006510
6511 /* FIXME - we need to add code here to detect a totally new
6512 * configuration, reset the AP, unassoc, rxon timing, assoc,
6513 * clear sta table, add BCAST sta... */
6514}
6515
Johannes Berg32bfd352007-12-19 01:31:26 +01006516static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
6517 struct ieee80211_vif *vif,
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006518 struct ieee80211_if_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07006519{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006520 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006521 int rc;
6522
6523 if (conf == NULL)
6524 return -EIO;
6525
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006526 if (priv->vif != vif) {
6527 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08006528 return 0;
6529 }
6530
Johannes Berg9d139c82008-07-09 14:40:37 +02006531 /* handle this temporarily here */
Johannes Berg05c914f2008-09-11 00:01:58 +02006532 if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
Johannes Berg9d139c82008-07-09 14:40:37 +02006533 conf->changed & IEEE80211_IFCC_BEACON) {
6534 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
6535 if (!beacon)
6536 return -ENOMEM;
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006537 mutex_lock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006538 rc = iwl3945_mac_beacon_update(hw, beacon);
Mohamed Abbas9bdf5ec2008-11-07 09:58:35 -08006539 mutex_unlock(&priv->mutex);
Johannes Berg9d139c82008-07-09 14:40:37 +02006540 if (rc)
6541 return rc;
6542 }
6543
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006544 if (!iwl_is_alive(priv))
Zhu Yi5a669262008-01-14 17:46:18 -08006545 return -EAGAIN;
6546
Zhu Yib481de92007-09-25 17:54:57 -07006547 mutex_lock(&priv->mutex);
6548
Zhu Yib481de92007-09-25 17:54:57 -07006549 if (conf->bssid)
Johannes Berge1749612008-10-27 15:59:26 -07006550 IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006551
Johannes Berg4150c572007-09-17 01:29:23 -04006552/*
6553 * very dubious code was here; the probe filtering flag is never set:
6554 *
Zhu Yib481de92007-09-25 17:54:57 -07006555 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
6556 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04006557 */
Zhu Yib481de92007-09-25 17:54:57 -07006558
Johannes Berg05c914f2008-09-11 00:01:58 +02006559 if (priv->iw_mode == NL80211_IFTYPE_AP) {
Zhu Yib481de92007-09-25 17:54:57 -07006560 if (!conf->bssid) {
6561 conf->bssid = priv->mac_addr;
6562 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Johannes Berge1749612008-10-27 15:59:26 -07006563 IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
6564 conf->bssid);
Zhu Yib481de92007-09-25 17:54:57 -07006565 }
6566 if (priv->ibss_beacon)
6567 dev_kfree_skb(priv->ibss_beacon);
6568
Johannes Berg9d139c82008-07-09 14:40:37 +02006569 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
Zhu Yib481de92007-09-25 17:54:57 -07006570 }
6571
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006572 if (iwl_is_rfkill(priv))
Mohamed Abbasfde35712007-11-29 11:10:15 +08006573 goto done;
6574
Zhu Yib481de92007-09-25 17:54:57 -07006575 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
6576 !is_multicast_ether_addr(conf->bssid)) {
6577 /* If there is currently a HW scan going on in the background
6578 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006579 if (iwl3945_scan_cancel_timeout(priv, 100)) {
Winkler, Tomas39aadf82008-12-19 10:37:32 +08006580 IWL_WARN(priv, "Aborted scan still in progress "
Zhu Yib481de92007-09-25 17:54:57 -07006581 "after 100ms\n");
6582 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
6583 mutex_unlock(&priv->mutex);
6584 return -EAGAIN;
6585 }
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006586 memcpy(priv->staging39_rxon.bssid_addr, conf->bssid, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006587
6588 /* TODO: Audit driver for usage of these members and see
6589 * if mac80211 deprecates them (priv->bssid looks like it
6590 * shouldn't be there, but I haven't scanned the IBSS code
6591 * to verify) - jpk */
6592 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
6593
Johannes Berg05c914f2008-09-11 00:01:58 +02006594 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006595 iwl3945_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006596 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006597 rc = iwl3945_commit_rxon(priv);
Johannes Berg05c914f2008-09-11 00:01:58 +02006598 if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006599 iwl3945_add_station(priv,
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006600 priv->active39_rxon.bssid_addr, 1, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006601 }
6602
6603 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006604 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006605 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006606 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006607 }
6608
Mohamed Abbasfde35712007-11-29 11:10:15 +08006609 done:
Zhu Yib481de92007-09-25 17:54:57 -07006610 IWL_DEBUG_MAC80211("leave\n");
6611 mutex_unlock(&priv->mutex);
6612
6613 return 0;
6614}
6615
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006616static void iwl3945_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04006617 unsigned int changed_flags,
6618 unsigned int *total_flags,
6619 int mc_count, struct dev_addr_list *mc_list)
6620{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006621 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006622 __le32 *filter_flags = &priv->staging39_rxon.filter_flags;
Rick Farrington25b3f572008-06-30 17:23:28 +08006623
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006624 IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
6625 changed_flags, *total_flags);
6626
6627 if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
6628 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
6629 *filter_flags |= RXON_FILTER_PROMISC_MSK;
6630 else
6631 *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
Abhijeet Kolekar5ec03972008-05-05 10:22:48 +08006632 }
Zhu, Yi352bc8d2008-11-12 13:14:09 -08006633 if (changed_flags & FIF_ALLMULTI) {
6634 if (*total_flags & FIF_ALLMULTI)
6635 *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
6636 else
6637 *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
6638 }
6639 if (changed_flags & FIF_CONTROL) {
6640 if (*total_flags & FIF_CONTROL)
6641 *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
6642 else
6643 *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
6644 }
6645 if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
6646 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
6647 *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
6648 else
6649 *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
6650 }
6651
6652 /* We avoid iwl_commit_rxon here to commit the new filter flags
6653 * since mac80211 will call ieee80211_hw_config immediately.
6654 * (mc_list is not supported at this time). Otherwise, we need to
6655 * queue a background iwl_commit_rxon work.
6656 */
6657
6658 *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
Rick Farrington25b3f572008-06-30 17:23:28 +08006659 FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
Johannes Berg4150c572007-09-17 01:29:23 -04006660}
6661
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006662static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006663 struct ieee80211_if_init_conf *conf)
6664{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006665 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006666
6667 IWL_DEBUG_MAC80211("enter\n");
6668
6669 mutex_lock(&priv->mutex);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006670
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006671 if (iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006672 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006673 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Mohamed Abbasfde35712007-11-29 11:10:15 +08006674 iwl3945_commit_rxon(priv);
6675 }
Johannes Berg32bfd352007-12-19 01:31:26 +01006676 if (priv->vif == conf->vif) {
6677 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07006678 memset(priv->bssid, 0, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07006679 }
6680 mutex_unlock(&priv->mutex);
6681
6682 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07006683}
6684
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006685#define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
6686
6687static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
6688 struct ieee80211_vif *vif,
6689 struct ieee80211_bss_conf *bss_conf,
6690 u32 changes)
6691{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006692 struct iwl_priv *priv = hw->priv;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006693
6694 IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
6695
6696 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
6697 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
6698 bss_conf->use_short_preamble);
6699 if (bss_conf->use_short_preamble)
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006700 priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006701 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006702 priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006703 }
6704
6705 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
6706 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
6707 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006708 priv->staging39_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006709 else
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006710 priv->staging39_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006711 }
6712
6713 if (changes & BSS_CHANGED_ASSOC) {
6714 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
6715 /* This should never happen as this function should
6716 * never be called from interrupt context. */
6717 if (WARN_ON_ONCE(in_interrupt()))
6718 return;
6719 if (bss_conf->assoc) {
6720 priv->assoc_id = bss_conf->aid;
6721 priv->beacon_int = bss_conf->beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006722 priv->timestamp = bss_conf->timestamp;
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08006723 priv->assoc_capability = bss_conf->assoc_capability;
6724 priv->next_scan_jiffies = jiffies +
6725 IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
6726 mutex_lock(&priv->mutex);
6727 iwl3945_post_associate(priv);
6728 mutex_unlock(&priv->mutex);
6729 } else {
6730 priv->assoc_id = 0;
6731 IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
6732 }
6733 } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
6734 IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
6735 iwl3945_send_rxon_assoc(priv);
6736 }
6737
6738}
6739
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006740static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07006741{
6742 int rc = 0;
6743 unsigned long flags;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006744 struct iwl_priv *priv = hw->priv;
John W. Linville9387b7c2008-09-30 20:59:05 -04006745 DECLARE_SSID_BUF(ssid_buf);
Zhu Yib481de92007-09-25 17:54:57 -07006746
6747 IWL_DEBUG_MAC80211("enter\n");
6748
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006749 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006750 spin_lock_irqsave(&priv->lock, flags);
6751
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006752 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006753 rc = -EIO;
6754 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
6755 goto out_unlock;
6756 }
6757
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006758 /* we don't schedule scan within next_scan_jiffies period */
6759 if (priv->next_scan_jiffies &&
6760 time_after(priv->next_scan_jiffies, jiffies)) {
6761 rc = -EAGAIN;
6762 goto out_unlock;
6763 }
Bill Moss15dbf1b2008-05-06 11:05:15 +08006764 /* if we just finished scan ask for delay for a broadcast scan */
6765 if ((len == 0) && priv->last_scan_jiffies &&
6766 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
6767 jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07006768 rc = -EAGAIN;
6769 goto out_unlock;
6770 }
6771 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006772 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
John W. Linville9387b7c2008-09-30 20:59:05 -04006773 print_ssid(ssid_buf, ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07006774
6775 priv->one_direct_scan = 1;
6776 priv->direct_ssid_len = (u8)
6777 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
6778 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08006779 } else
6780 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07006781
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006782 rc = iwl3945_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006783
6784 IWL_DEBUG_MAC80211("leave\n");
6785
6786out_unlock:
6787 spin_unlock_irqrestore(&priv->lock, flags);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006788 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07006789
6790 return rc;
6791}
6792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006793static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07006794 const u8 *local_addr, const u8 *addr,
6795 struct ieee80211_key_conf *key)
6796{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006797 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006798 int rc = 0;
6799 u8 sta_id;
6800
6801 IWL_DEBUG_MAC80211("enter\n");
6802
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08006803 if (iwl3945_mod_params.sw_crypto) {
Zhu Yib481de92007-09-25 17:54:57 -07006804 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
6805 return -EOPNOTSUPP;
6806 }
6807
6808 if (is_zero_ether_addr(addr))
6809 /* only support pairwise keys */
6810 return -EOPNOTSUPP;
6811
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006812 sta_id = iwl3945_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07006813 if (sta_id == IWL_INVALID_STATION) {
Johannes Berge1749612008-10-27 15:59:26 -07006814 IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
6815 addr);
Zhu Yib481de92007-09-25 17:54:57 -07006816 return -EINVAL;
6817 }
6818
6819 mutex_lock(&priv->mutex);
6820
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006821 iwl3945_scan_cancel_timeout(priv, 100);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006822
Zhu Yib481de92007-09-25 17:54:57 -07006823 switch (cmd) {
6824 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006825 rc = iwl3945_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006826 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006827 iwl3945_set_rxon_hwcrypto(priv, 1);
6828 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006829 key->hw_key_idx = sta_id;
6830 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
6831 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
6832 }
6833 break;
6834 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006835 rc = iwl3945_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07006836 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006837 iwl3945_set_rxon_hwcrypto(priv, 0);
6838 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006839 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
6840 }
6841 break;
6842 default:
6843 rc = -EINVAL;
6844 }
6845
6846 IWL_DEBUG_MAC80211("leave\n");
6847 mutex_unlock(&priv->mutex);
6848
6849 return rc;
6850}
6851
Johannes Berge100bb62008-04-30 18:51:21 +02006852static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
Zhu Yib481de92007-09-25 17:54:57 -07006853 const struct ieee80211_tx_queue_params *params)
6854{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006855 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006856 unsigned long flags;
6857 int q;
Zhu Yib481de92007-09-25 17:54:57 -07006858
6859 IWL_DEBUG_MAC80211("enter\n");
6860
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006861 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006862 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6863 return -EIO;
6864 }
6865
6866 if (queue >= AC_NUM) {
6867 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
6868 return 0;
6869 }
6870
Zhu Yib481de92007-09-25 17:54:57 -07006871 q = AC_NUM - 1 - queue;
6872
6873 spin_lock_irqsave(&priv->lock, flags);
6874
6875 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
6876 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
6877 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
6878 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7be2008-02-10 16:49:38 +01006879 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07006880
6881 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
6882 priv->qos_data.qos_active = 1;
6883
6884 spin_unlock_irqrestore(&priv->lock, flags);
6885
6886 mutex_lock(&priv->mutex);
Johannes Berg05c914f2008-09-11 00:01:58 +02006887 if (priv->iw_mode == NL80211_IFTYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006888 iwl3945_activate_qos(priv, 1);
6889 else if (priv->assoc_id && iwl3945_is_associated(priv))
6890 iwl3945_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006891
6892 mutex_unlock(&priv->mutex);
6893
Zhu Yib481de92007-09-25 17:54:57 -07006894 IWL_DEBUG_MAC80211("leave\n");
6895 return 0;
6896}
6897
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006898static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07006899 struct ieee80211_tx_queue_stats *stats)
6900{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006901 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006902 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006903 struct iwl3945_tx_queue *txq;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006904 struct iwl_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07006905 unsigned long flags;
6906
6907 IWL_DEBUG_MAC80211("enter\n");
6908
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006909 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006910 IWL_DEBUG_MAC80211("leave - RF not ready\n");
6911 return -EIO;
6912 }
6913
6914 spin_lock_irqsave(&priv->lock, flags);
6915
6916 for (i = 0; i < AC_NUM; i++) {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006917 txq = &priv->txq39[i];
Zhu Yib481de92007-09-25 17:54:57 -07006918 q = &txq->q;
Samuel Ortizd20b3c62008-12-19 10:37:15 +08006919 avail = iwl_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07006920
Johannes Berg57ffc582008-04-29 17:18:59 +02006921 stats[i].len = q->n_window - avail;
6922 stats[i].limit = q->n_window - q->high_mark;
6923 stats[i].count = q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -07006924
6925 }
6926 spin_unlock_irqrestore(&priv->lock, flags);
6927
6928 IWL_DEBUG_MAC80211("leave\n");
6929
6930 return 0;
6931}
6932
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006933static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006934{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006935 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006936 unsigned long flags;
6937
6938 mutex_lock(&priv->mutex);
6939 IWL_DEBUG_MAC80211("enter\n");
6940
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006941 iwl_reset_qos(priv);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006942
Zhu Yib481de92007-09-25 17:54:57 -07006943 spin_lock_irqsave(&priv->lock, flags);
6944 priv->assoc_id = 0;
6945 priv->assoc_capability = 0;
6946 priv->call_post_assoc_from_beacon = 0;
6947
6948 /* new association get rid of ibss beacon skb */
6949 if (priv->ibss_beacon)
6950 dev_kfree_skb(priv->ibss_beacon);
6951
6952 priv->ibss_beacon = NULL;
6953
6954 priv->beacon_int = priv->hw->conf.beacon_int;
Tomas Winkler28afaf92008-12-19 10:37:06 +08006955 priv->timestamp = 0;
Johannes Berg05c914f2008-09-11 00:01:58 +02006956 if ((priv->iw_mode == NL80211_IFTYPE_STATION))
Zhu Yib481de92007-09-25 17:54:57 -07006957 priv->beacon_int = 0;
6958
6959 spin_unlock_irqrestore(&priv->lock, flags);
6960
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006961 if (!iwl_is_ready_rf(priv)) {
Mohamed Abbasfde35712007-11-29 11:10:15 +08006962 IWL_DEBUG_MAC80211("leave - not ready\n");
6963 mutex_unlock(&priv->mutex);
6964 return;
6965 }
6966
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006967 /* we are restarting association process
6968 * clear RXON_FILTER_ASSOC_MSK bit
6969 */
Johannes Berg05c914f2008-09-11 00:01:58 +02006970 if (priv->iw_mode != NL80211_IFTYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006971 iwl3945_scan_cancel_timeout(priv, 100);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08006972 priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006973 iwl3945_commit_rxon(priv);
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006974 }
6975
Zhu Yib481de92007-09-25 17:54:57 -07006976 /* Per mac80211.h: This is only used in IBSS mode... */
Johannes Berg05c914f2008-09-11 00:01:58 +02006977 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Mohamed Abbas15e869d2007-10-25 17:15:46 +08006978
Zhu Yib481de92007-09-25 17:54:57 -07006979 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
6980 mutex_unlock(&priv->mutex);
6981 return;
6982 }
6983
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006984 iwl3945_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006985
6986 mutex_unlock(&priv->mutex);
6987
6988 IWL_DEBUG_MAC80211("leave\n");
6989
6990}
6991
Johannes Berge039fa42008-05-15 12:55:29 +02006992static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07006993{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08006994 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07006995 unsigned long flags;
6996
Zhu Yib481de92007-09-25 17:54:57 -07006997 IWL_DEBUG_MAC80211("enter\n");
6998
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08006999 if (!iwl_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007000 IWL_DEBUG_MAC80211("leave - RF not ready\n");
Zhu Yib481de92007-09-25 17:54:57 -07007001 return -EIO;
7002 }
7003
Johannes Berg05c914f2008-09-11 00:01:58 +02007004 if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
Zhu Yib481de92007-09-25 17:54:57 -07007005 IWL_DEBUG_MAC80211("leave - not IBSS\n");
Zhu Yib481de92007-09-25 17:54:57 -07007006 return -EIO;
7007 }
7008
7009 spin_lock_irqsave(&priv->lock, flags);
7010
7011 if (priv->ibss_beacon)
7012 dev_kfree_skb(priv->ibss_beacon);
7013
7014 priv->ibss_beacon = skb;
7015
7016 priv->assoc_id = 0;
7017
7018 IWL_DEBUG_MAC80211("leave\n");
7019 spin_unlock_irqrestore(&priv->lock, flags);
7020
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007021 iwl_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007022
Abhijeet Kolekardc4b1e72008-09-03 11:26:30 +08007023 iwl3945_post_associate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007024
Zhu Yib481de92007-09-25 17:54:57 -07007025
7026 return 0;
7027}
7028
7029/*****************************************************************************
7030 *
7031 * sysfs attributes
7032 *
7033 *****************************************************************************/
7034
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007035#ifdef CONFIG_IWL3945_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007036
7037/*
7038 * The following adds a new attribute to the sysfs representation
7039 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7040 * used for controlling the debug level.
7041 *
7042 * See the level definitions in iwl for details.
7043 */
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007044static ssize_t show_debug_level(struct device *d,
7045 struct device_attribute *attr, char *buf)
Zhu Yib481de92007-09-25 17:54:57 -07007046{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007047 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007048
7049 return sprintf(buf, "0x%08X\n", priv->debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007050}
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007051static ssize_t store_debug_level(struct device *d,
7052 struct device_attribute *attr,
Zhu Yib481de92007-09-25 17:54:57 -07007053 const char *buf, size_t count)
7054{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007055 struct iwl_priv *priv = d->driver_data;
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007056 unsigned long val;
7057 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07007058
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007059 ret = strict_strtoul(buf, 0, &val);
7060 if (ret)
Tomas Winkler978785a2008-12-19 10:37:31 +08007061 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07007062 else
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007063 priv->debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007064
7065 return strnlen(buf, count);
7066}
7067
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007068static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
7069 show_debug_level, store_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007070
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007071#endif /* CONFIG_IWL3945_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007072
Zhu Yib481de92007-09-25 17:54:57 -07007073static ssize_t show_temperature(struct device *d,
7074 struct device_attribute *attr, char *buf)
7075{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007076 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007077
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007078 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007079 return -EAGAIN;
7080
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007081 return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07007082}
7083
7084static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
7085
Zhu Yib481de92007-09-25 17:54:57 -07007086static ssize_t show_tx_power(struct device *d,
7087 struct device_attribute *attr, char *buf)
7088{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007089 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007090 return sprintf(buf, "%d\n", priv->user_txpower_limit);
7091}
7092
7093static ssize_t store_tx_power(struct device *d,
7094 struct device_attribute *attr,
7095 const char *buf, size_t count)
7096{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007097 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007098 char *p = (char *)buf;
7099 u32 val;
7100
7101 val = simple_strtoul(p, &p, 10);
7102 if (p == buf)
Tomas Winkler978785a2008-12-19 10:37:31 +08007103 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
Zhu Yib481de92007-09-25 17:54:57 -07007104 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007105 iwl3945_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07007106
7107 return count;
7108}
7109
7110static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
7111
7112static ssize_t show_flags(struct device *d,
7113 struct device_attribute *attr, char *buf)
7114{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007115 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007116
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007117 return sprintf(buf, "0x%04X\n", priv->active39_rxon.flags);
Zhu Yib481de92007-09-25 17:54:57 -07007118}
7119
7120static ssize_t store_flags(struct device *d,
7121 struct device_attribute *attr,
7122 const char *buf, size_t count)
7123{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007124 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007125 u32 flags = simple_strtoul(buf, NULL, 0);
7126
7127 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007128 if (le32_to_cpu(priv->staging39_rxon.flags) != flags) {
Zhu Yib481de92007-09-25 17:54:57 -07007129 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007130 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007131 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007132 else {
7133 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
7134 flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007135 priv->staging39_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007136 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007137 }
7138 }
7139 mutex_unlock(&priv->mutex);
7140
7141 return count;
7142}
7143
7144static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
7145
7146static ssize_t show_filter_flags(struct device *d,
7147 struct device_attribute *attr, char *buf)
7148{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007149 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007150
7151 return sprintf(buf, "0x%04X\n",
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007152 le32_to_cpu(priv->active39_rxon.filter_flags));
Zhu Yib481de92007-09-25 17:54:57 -07007153}
7154
7155static ssize_t store_filter_flags(struct device *d,
7156 struct device_attribute *attr,
7157 const char *buf, size_t count)
7158{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007159 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007160 u32 filter_flags = simple_strtoul(buf, NULL, 0);
7161
7162 mutex_lock(&priv->mutex);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007163 if (le32_to_cpu(priv->staging39_rxon.filter_flags) != filter_flags) {
Zhu Yib481de92007-09-25 17:54:57 -07007164 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007165 if (iwl3945_scan_cancel_timeout(priv, 100))
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007166 IWL_WARN(priv, "Could not cancel scan.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007167 else {
7168 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
7169 "0x%04X\n", filter_flags);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007170 priv->staging39_rxon.filter_flags =
Zhu Yib481de92007-09-25 17:54:57 -07007171 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007172 iwl3945_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007173 }
7174 }
7175 mutex_unlock(&priv->mutex);
7176
7177 return count;
7178}
7179
7180static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
7181 store_filter_flags);
7182
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007183#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007184
7185static ssize_t show_measurement(struct device *d,
7186 struct device_attribute *attr, char *buf)
7187{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007188 struct iwl_priv *priv = dev_get_drvdata(d);
Tomas Winkler600c0e12008-12-19 10:37:04 +08007189 struct iwl_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007190 u32 size = sizeof(measure_report), len = 0, ofs = 0;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007191 u8 *data = (u8 *)&measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07007192 unsigned long flags;
7193
7194 spin_lock_irqsave(&priv->lock, flags);
7195 if (!(priv->measurement_status & MEASUREMENT_READY)) {
7196 spin_unlock_irqrestore(&priv->lock, flags);
7197 return 0;
7198 }
7199 memcpy(&measure_report, &priv->measure_report, size);
7200 priv->measurement_status = 0;
7201 spin_unlock_irqrestore(&priv->lock, flags);
7202
7203 while (size && (PAGE_SIZE - len)) {
7204 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7205 PAGE_SIZE - len, 1);
7206 len = strlen(buf);
7207 if (PAGE_SIZE - len)
7208 buf[len++] = '\n';
7209
7210 ofs += 16;
7211 size -= min(size, 16U);
7212 }
7213
7214 return len;
7215}
7216
7217static ssize_t store_measurement(struct device *d,
7218 struct device_attribute *attr,
7219 const char *buf, size_t count)
7220{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007221 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007222 struct ieee80211_measurement_params params = {
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007223 .channel = le16_to_cpu(priv->active39_rxon.channel),
Zhu Yib481de92007-09-25 17:54:57 -07007224 .start_time = cpu_to_le64(priv->last_tsf),
7225 .duration = cpu_to_le16(1),
7226 };
7227 u8 type = IWL_MEASURE_BASIC;
7228 u8 buffer[32];
7229 u8 channel;
7230
7231 if (count) {
7232 char *p = buffer;
7233 strncpy(buffer, buf, min(sizeof(buffer), count));
7234 channel = simple_strtoul(p, NULL, 0);
7235 if (channel)
7236 params.channel = channel;
7237
7238 p = buffer;
7239 while (*p && *p != ' ')
7240 p++;
7241 if (*p)
7242 type = simple_strtoul(p + 1, NULL, 0);
7243 }
7244
7245 IWL_DEBUG_INFO("Invoking measurement of type %d on "
7246 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007247 iwl3945_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07007248
7249 return count;
7250}
7251
7252static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
7253 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007254#endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07007255
Zhu Yib481de92007-09-25 17:54:57 -07007256static ssize_t store_retry_rate(struct device *d,
7257 struct device_attribute *attr,
7258 const char *buf, size_t count)
7259{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007260 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007261
7262 priv->retry_rate = simple_strtoul(buf, NULL, 0);
7263 if (priv->retry_rate <= 0)
7264 priv->retry_rate = 1;
7265
7266 return count;
7267}
7268
7269static ssize_t show_retry_rate(struct device *d,
7270 struct device_attribute *attr, char *buf)
7271{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007272 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007273 return sprintf(buf, "%d", priv->retry_rate);
7274}
7275
7276static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
7277 store_retry_rate);
7278
7279static ssize_t store_power_level(struct device *d,
7280 struct device_attribute *attr,
7281 const char *buf, size_t count)
7282{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007283 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007284 int rc;
7285 int mode;
7286
7287 mode = simple_strtoul(buf, NULL, 0);
7288 mutex_lock(&priv->mutex);
7289
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007290 if (!iwl_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007291 rc = -EAGAIN;
7292 goto out;
7293 }
7294
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007295 if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
7296 (mode == IWL39_POWER_AC))
7297 mode = IWL39_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07007298 else
7299 mode |= IWL_POWER_ENABLED;
7300
7301 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007302 rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07007303 if (rc) {
7304 IWL_DEBUG_MAC80211("failed setting power mode.\n");
7305 goto out;
7306 }
7307 priv->power_mode = mode;
7308 }
7309
7310 rc = count;
7311
7312 out:
7313 mutex_unlock(&priv->mutex);
7314 return rc;
7315}
7316
7317#define MAX_WX_STRING 80
7318
7319/* Values are in microsecond */
7320static const s32 timeout_duration[] = {
7321 350000,
7322 250000,
7323 75000,
7324 37000,
7325 25000,
7326};
7327static const s32 period_duration[] = {
7328 400000,
7329 700000,
7330 1000000,
7331 1000000,
7332 1000000
7333};
7334
7335static ssize_t show_power_level(struct device *d,
7336 struct device_attribute *attr, char *buf)
7337{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007338 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007339 int level = IWL_POWER_LEVEL(priv->power_mode);
7340 char *p = buf;
7341
7342 p += sprintf(p, "%d ", level);
7343 switch (level) {
7344 case IWL_POWER_MODE_CAM:
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007345 case IWL39_POWER_AC:
Zhu Yib481de92007-09-25 17:54:57 -07007346 p += sprintf(p, "(AC)");
7347 break;
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007348 case IWL39_POWER_BATTERY:
Zhu Yib481de92007-09-25 17:54:57 -07007349 p += sprintf(p, "(BATTERY)");
7350 break;
7351 default:
7352 p += sprintf(p,
7353 "(Timeout %dms, Period %dms)",
7354 timeout_duration[level - 1] / 1000,
7355 period_duration[level - 1] / 1000);
7356 }
7357
7358 if (!(priv->power_mode & IWL_POWER_ENABLED))
7359 p += sprintf(p, " OFF\n");
7360 else
7361 p += sprintf(p, " \n");
7362
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007363 return p - buf + 1;
Zhu Yib481de92007-09-25 17:54:57 -07007364
7365}
7366
7367static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
7368 store_power_level);
7369
7370static ssize_t show_channels(struct device *d,
7371 struct device_attribute *attr, char *buf)
7372{
Johannes Berg8318d782008-01-24 19:38:38 +01007373 /* all this shit doesn't belong into sysfs anyway */
7374 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007375}
7376
7377static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
7378
7379static ssize_t show_statistics(struct device *d,
7380 struct device_attribute *attr, char *buf)
7381{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007382 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007383 u32 size = sizeof(struct iwl3945_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07007384 u32 len = 0, ofs = 0;
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007385 u8 *data = (u8 *)&priv->statistics_39;
Zhu Yib481de92007-09-25 17:54:57 -07007386 int rc = 0;
7387
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007388 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007389 return -EAGAIN;
7390
7391 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007392 rc = iwl3945_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007393 mutex_unlock(&priv->mutex);
7394
7395 if (rc) {
7396 len = sprintf(buf,
7397 "Error sending statistics request: 0x%08X\n", rc);
7398 return len;
7399 }
7400
7401 while (size && (PAGE_SIZE - len)) {
7402 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
7403 PAGE_SIZE - len, 1);
7404 len = strlen(buf);
7405 if (PAGE_SIZE - len)
7406 buf[len++] = '\n';
7407
7408 ofs += 16;
7409 size -= min(size, 16U);
7410 }
7411
7412 return len;
7413}
7414
7415static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
7416
7417static ssize_t show_antenna(struct device *d,
7418 struct device_attribute *attr, char *buf)
7419{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007420 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007421
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007422 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007423 return -EAGAIN;
7424
7425 return sprintf(buf, "%d\n", priv->antenna);
7426}
7427
7428static ssize_t store_antenna(struct device *d,
7429 struct device_attribute *attr,
7430 const char *buf, size_t count)
7431{
7432 int ant;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007433 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07007434
7435 if (count == 0)
7436 return 0;
7437
7438 if (sscanf(buf, "%1i", &ant) != 1) {
7439 IWL_DEBUG_INFO("not in hex or decimal form.\n");
7440 return count;
7441 }
7442
7443 if ((ant >= 0) && (ant <= 2)) {
7444 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007445 priv->antenna = (enum iwl3945_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07007446 } else
7447 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
7448
7449
7450 return count;
7451}
7452
7453static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
7454
7455static ssize_t show_status(struct device *d,
7456 struct device_attribute *attr, char *buf)
7457{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007458 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007459 if (!iwl_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007460 return -EAGAIN;
7461 return sprintf(buf, "0x%08x\n", (int)priv->status);
7462}
7463
7464static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
7465
7466static ssize_t dump_error_log(struct device *d,
7467 struct device_attribute *attr,
7468 const char *buf, size_t count)
7469{
7470 char *p = (char *)buf;
7471
7472 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007473 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007474
7475 return strnlen(buf, count);
7476}
7477
7478static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
7479
7480static ssize_t dump_event_log(struct device *d,
7481 struct device_attribute *attr,
7482 const char *buf, size_t count)
7483{
7484 char *p = (char *)buf;
7485
7486 if (p[0] == '1')
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007487 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07007488
7489 return strnlen(buf, count);
7490}
7491
7492static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
7493
7494/*****************************************************************************
7495 *
Tomas Winklera96a27f2008-10-23 23:48:56 -07007496 * driver setup and tear down
Zhu Yib481de92007-09-25 17:54:57 -07007497 *
7498 *****************************************************************************/
7499
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007500static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007501{
7502 priv->workqueue = create_workqueue(DRV_NAME);
7503
7504 init_waitqueue_head(&priv->wait_command_queue);
7505
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007506 INIT_WORK(&priv->up, iwl3945_bg_up);
7507 INIT_WORK(&priv->restart, iwl3945_bg_restart);
7508 INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
7509 INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
7510 INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
7511 INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
7512 INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
7513 INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007514 INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
7515 INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
7516 INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07007517
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007518 iwl3945_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007519
7520 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007521 iwl3945_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07007522}
7523
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007524static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007525{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007526 iwl3945_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007527
Joonwoo Parke47eb6a2007-11-29 10:42:49 +09007528 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007529 cancel_delayed_work(&priv->scan_check);
7530 cancel_delayed_work(&priv->alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07007531 cancel_work_sync(&priv->beacon_update);
7532}
7533
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007534static struct attribute *iwl3945_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07007535 &dev_attr_antenna.attr,
7536 &dev_attr_channels.attr,
7537 &dev_attr_dump_errors.attr,
7538 &dev_attr_dump_events.attr,
7539 &dev_attr_flags.attr,
7540 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007541#ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07007542 &dev_attr_measurement.attr,
7543#endif
7544 &dev_attr_power_level.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007545 &dev_attr_retry_rate.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007546 &dev_attr_statistics.attr,
7547 &dev_attr_status.attr,
7548 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07007549 &dev_attr_tx_power.attr,
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007550#ifdef CONFIG_IWL3945_DEBUG
7551 &dev_attr_debug_level.attr,
7552#endif
Zhu Yib481de92007-09-25 17:54:57 -07007553 NULL
7554};
7555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007556static struct attribute_group iwl3945_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07007557 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007558 .attrs = iwl3945_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07007559};
7560
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007561static struct ieee80211_ops iwl3945_hw_ops = {
7562 .tx = iwl3945_mac_tx,
7563 .start = iwl3945_mac_start,
7564 .stop = iwl3945_mac_stop,
7565 .add_interface = iwl3945_mac_add_interface,
7566 .remove_interface = iwl3945_mac_remove_interface,
7567 .config = iwl3945_mac_config,
7568 .config_interface = iwl3945_mac_config_interface,
7569 .configure_filter = iwl3945_configure_filter,
7570 .set_key = iwl3945_mac_set_key,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007571 .get_tx_stats = iwl3945_mac_get_tx_stats,
7572 .conf_tx = iwl3945_mac_conf_tx,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007573 .reset_tsf = iwl3945_mac_reset_tsf,
Abhijeet Kolekarcd56d332008-09-03 11:26:21 +08007574 .bss_info_changed = iwl3945_bss_info_changed,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007575 .hw_scan = iwl3945_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07007576};
7577
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007578static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07007579{
7580 int err = 0;
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007581 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07007582 struct ieee80211_hw *hw;
Kolekar, Abhijeetc0f20d92008-12-19 10:37:19 +08007583 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007584 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007585
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007586 /***********************
7587 * 1. Allocating HW data
7588 * ********************/
7589
Zhu Yib481de92007-09-25 17:54:57 -07007590 /* mac80211 allocates memory for this device instance, including
7591 * space for this driver's private structure */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007592 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl3945_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07007593 if (hw == NULL) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08007594 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
Zhu Yib481de92007-09-25 17:54:57 -07007595 err = -ENOMEM;
7596 goto out;
7597 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007598
Zhu Yib481de92007-09-25 17:54:57 -07007599 SET_IEEE80211_DEV(hw, &pdev->dev);
7600
Zhu Yib481de92007-09-25 17:54:57 -07007601 priv = hw->priv;
7602 priv->hw = hw;
Zhu Yib481de92007-09-25 17:54:57 -07007603 priv->pci_dev = pdev;
Tomas Winkler82b9a122008-03-04 18:09:30 -08007604 priv->cfg = cfg;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007605
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007606 if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
7607 (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007608 IWL_ERR(priv,
7609 "invalid queues_num, should be between %d and %d\n",
7610 IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
Samuel Ortiza3139c52008-12-19 10:37:09 +08007611 err = -EINVAL;
7612 goto out;
7613 }
7614
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007615 /* Disabling hardware scan means that mac80211 will perform scans
7616 * "the hard way", rather than using device's scan. */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007617 if (iwl3945_mod_params.disable_hw_scan) {
Samuel Ortiz40b8ec02008-12-19 10:37:08 +08007618 IWL_DEBUG_INFO("Disabling hw_scan\n");
7619 iwl3945_hw_ops.hw_scan = NULL;
7620 }
7621
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007622 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
7623 hw->rate_control_algorithm = "iwl-3945-rs";
7624 hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
7625
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007626 /* Select antenna (may be helpful if only one antenna is connected) */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007627 priv->antenna = (enum iwl3945_antenna)iwl3945_mod_params.antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007628#ifdef CONFIG_IWL3945_DEBUG
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007629 priv->debug_level = iwl3945_mod_params.debug;
Zhu Yib481de92007-09-25 17:54:57 -07007630 atomic_set(&priv->restrict_refcnt, 0);
7631#endif
Zhu Yib481de92007-09-25 17:54:57 -07007632
Bruno Randolf566bfe52008-05-08 19:15:40 +02007633 /* Tell mac80211 our characteristics */
Johannes Berg605a0bd2008-07-15 10:10:01 +02007634 hw->flags = IEEE80211_HW_SIGNAL_DBM |
Bruno Randolf566bfe52008-05-08 19:15:40 +02007635 IEEE80211_HW_NOISE_DBM;
Zhu Yib481de92007-09-25 17:54:57 -07007636
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007637 hw->wiphy->interface_modes =
Luis R. Rodriguezf59ac042008-08-29 16:26:43 -07007638 BIT(NL80211_IFTYPE_STATION) |
7639 BIT(NL80211_IFTYPE_ADHOC);
7640
Luis R. Rodriguezea4a82dc2008-11-12 14:22:04 -08007641 hw->wiphy->fw_handles_regulatory = true;
7642
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007643 /* 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07007644 hw->queues = 4;
7645
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007646 /***************************
7647 * 2. Initializing PCI bus
7648 * *************************/
Zhu Yib481de92007-09-25 17:54:57 -07007649 if (pci_enable_device(pdev)) {
7650 err = -ENODEV;
7651 goto out_ieee80211_free_hw;
7652 }
7653
7654 pci_set_master(pdev);
7655
Zhu Yib481de92007-09-25 17:54:57 -07007656 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
7657 if (!err)
7658 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
7659 if (err) {
Tomas Winkler978785a2008-12-19 10:37:31 +08007660 IWL_WARN(priv, "No suitable DMA available.\n");
Zhu Yib481de92007-09-25 17:54:57 -07007661 goto out_pci_disable_device;
7662 }
7663
7664 pci_set_drvdata(pdev, priv);
7665 err = pci_request_regions(pdev, DRV_NAME);
7666 if (err)
7667 goto out_pci_disable_device;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08007668
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007669 /***********************
7670 * 3. Read REV Register
7671 * ********************/
Zhu Yib481de92007-09-25 17:54:57 -07007672 priv->hw_base = pci_iomap(pdev, 0, 0);
7673 if (!priv->hw_base) {
7674 err = -ENODEV;
7675 goto out_pci_release_regions;
7676 }
7677
7678 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
7679 (unsigned long long) pci_resource_len(pdev, 0));
7680 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
7681
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007682 /* We disable the RETRY_TIMEOUT register (0x41) to keep
7683 * PCI Tx retries from interfering with C3 CPU state */
7684 pci_write_config_byte(pdev, 0x41, 0x00);
Zhu Yib481de92007-09-25 17:54:57 -07007685
Zhu Yi5a669262008-01-14 17:46:18 -08007686 /* nic init */
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08007687 iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007688 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
Zhu Yi5a669262008-01-14 17:46:18 -08007689
Abhijeet Kolekar5d49f492008-12-19 10:37:29 +08007690 iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
7691 err = iwl_poll_direct_bit(priv, CSR_GP_CNTRL,
Zhu, Yi73d7b5a2008-12-05 07:58:40 -08007692 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007693 if (err < 0) {
7694 IWL_DEBUG_INFO("Failed to init the card\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007695 goto out_remove_sysfs;
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007696 }
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007697
7698 /***********************
7699 * 4. Read EEPROM
7700 * ********************/
Zhu Yi5a669262008-01-14 17:46:18 -08007701 /* Read the EEPROM */
7702 err = iwl3945_eeprom_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007703 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007704 IWL_ERR(priv, "Unable to init EEPROM\n");
Zhu Yi5a669262008-01-14 17:46:18 -08007705 goto out_remove_sysfs;
7706 }
7707 /* MAC Address location in EEPROM same for 3945/4965 */
7708 get_eeprom_mac(priv, priv->mac_addr);
Johannes Berge1749612008-10-27 15:59:26 -07007709 IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
Zhu Yi5a669262008-01-14 17:46:18 -08007710 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
7711
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007712 /***********************
7713 * 5. Setup HW Constants
7714 * ********************/
7715 /* Device-specific setup */
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007716 if (iwl3945_hw_set_hw_params(priv)) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007717 IWL_ERR(priv, "failed to set hw settings\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007718 goto out_iounmap;
7719 }
7720
7721 /***********************
7722 * 6. Setup priv
7723 * ********************/
7724 priv->retry_rate = 1;
7725 priv->ibss_beacon = NULL;
7726
7727 spin_lock_init(&priv->lock);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007728 spin_lock_init(&priv->power_data_39.lock);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007729 spin_lock_init(&priv->sta_lock);
7730 spin_lock_init(&priv->hcmd_lock);
7731
7732 INIT_LIST_HEAD(&priv->free_frames);
7733 mutex_init(&priv->mutex);
7734
7735 /* Clear the driver's (not device's) station table */
7736 iwl3945_clear_stations_table(priv);
7737
7738 priv->data_retry_limit = -1;
7739 priv->ieee_channels = NULL;
7740 priv->ieee_rates = NULL;
7741 priv->band = IEEE80211_BAND_2GHZ;
7742
7743 priv->iw_mode = NL80211_IFTYPE_STATION;
7744
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007745 iwl_reset_qos(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007746
7747 priv->qos_data.qos_active = 0;
7748 priv->qos_data.qos_cap.val = 0;
7749
7750
7751 priv->rates_mask = IWL_RATES_MASK;
7752 /* If power management is turned on, default to AC mode */
Samuel Ortiz1125eff2008-12-19 10:37:14 +08007753 priv->power_mode = IWL39_POWER_AC;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007754 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
7755
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007756 err = iwl3945_init_channel_map(priv);
7757 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007758 IWL_ERR(priv, "initializing regulatory failed: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007759 goto out_release_irq;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007760 }
7761
7762 err = iwl3945_init_geos(priv);
7763 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007764 IWL_ERR(priv, "initializing geos failed: %d\n", err);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007765 goto out_free_channel_map;
7766 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007767
Tomas Winkler978785a2008-12-19 10:37:31 +08007768 IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
7769 priv->cfg->name);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007770
7771 /***********************************
7772 * 7. Initialize Module Parameters
7773 * **********************************/
7774
7775 /* Initialize module parameter values here */
7776 /* Disable radio (SW RF KILL) via parameter when loading driver */
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08007777 if (iwl3945_mod_params.disable) {
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007778 set_bit(STATUS_RF_KILL_SW, &priv->status);
7779 IWL_DEBUG_INFO("Radio disabled.\n");
7780 }
7781
7782
7783 /***********************
7784 * 8. Setup Services
7785 * ********************/
7786
7787 spin_lock_irqsave(&priv->lock, flags);
7788 iwl3945_disable_interrupts(priv);
7789 spin_unlock_irqrestore(&priv->lock, flags);
7790
7791 err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
7792 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007793 IWL_ERR(priv, "failed to create sysfs device attributes\n");
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007794 goto out_free_geos;
7795 }
7796
7797 iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
7798 iwl3945_setup_deferred_work(priv);
7799 iwl3945_setup_rx_handlers(priv);
7800
7801 /***********************
7802 * 9. Conclude
7803 * ********************/
7804 pci_save_state(pdev);
7805 pci_disable_device(pdev);
7806
7807 /*********************************
7808 * 10. Setup and Register mac80211
7809 * *******************************/
7810
Zhu Yi5a669262008-01-14 17:46:18 -08007811 err = ieee80211_register_hw(priv->hw);
7812 if (err) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007813 IWL_ERR(priv, "Failed to register network device: %d\n", err);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007814 goto out_remove_sysfs;
Zhu Yib481de92007-09-25 17:54:57 -07007815 }
7816
Zhu Yi5a669262008-01-14 17:46:18 -08007817 priv->hw->conf.beacon_int = 100;
7818 priv->mac80211_registered = 1;
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007819
Zhu Yib481de92007-09-25 17:54:57 -07007820
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007821 err = iwl3945_rfkill_init(priv);
7822 if (err)
Winkler, Tomas15b16872008-12-19 10:37:33 +08007823 IWL_ERR(priv, "Unable to initialize RFKILL system. "
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007824 "Ignoring error: %d\n", err);
7825
Zhu Yib481de92007-09-25 17:54:57 -07007826 return 0;
7827
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007828 out_remove_sysfs:
7829 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007830 out_free_geos:
7831 iwl3945_free_geos(priv);
7832 out_free_channel_map:
7833 iwl3945_free_channel_map(priv);
Kolekar, Abhijeetcee53dd2008-11-12 13:14:04 -08007834
Zhu Yib481de92007-09-25 17:54:57 -07007835
7836 out_release_irq:
Zhu Yib481de92007-09-25 17:54:57 -07007837 destroy_workqueue(priv->workqueue);
7838 priv->workqueue = NULL;
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007839 iwl3945_unset_hw_params(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007840
7841 out_iounmap:
7842 pci_iounmap(pdev, priv->hw_base);
7843 out_pci_release_regions:
7844 pci_release_regions(pdev);
7845 out_pci_disable_device:
7846 pci_disable_device(pdev);
7847 pci_set_drvdata(pdev, NULL);
7848 out_ieee80211_free_hw:
7849 ieee80211_free_hw(priv->hw);
7850 out:
7851 return err;
7852}
7853
Reinette Chatrec83dbf62008-03-21 13:53:41 -07007854static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007855{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007856 struct iwl_priv *priv = pci_get_drvdata(pdev);
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007857 unsigned long flags;
Zhu Yib481de92007-09-25 17:54:57 -07007858
7859 if (!priv)
7860 return;
7861
7862 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
7863
Zhu Yib481de92007-09-25 17:54:57 -07007864 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08007865
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007866 iwl3945_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007867
Mohamed Abbas0359fac2008-03-28 16:21:08 -07007868 /* make sure we flush any pending irq or
7869 * tasklet for the driver
7870 */
7871 spin_lock_irqsave(&priv->lock, flags);
7872 iwl3945_disable_interrupts(priv);
7873 spin_unlock_irqrestore(&priv->lock, flags);
7874
7875 iwl_synchronize_irq(priv);
7876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007877 sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07007878
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007879 iwl3945_rfkill_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007880 iwl3945_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007881
7882 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007883 iwl3945_rx_queue_free(priv, &priv->rxq);
7884 iwl3945_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007885
Abhijeet Kolekar3832ec92008-12-19 10:37:26 +08007886 iwl3945_unset_hw_params(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007887 iwl3945_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007888
Tomas Winkler3ac7f142008-07-21 02:40:14 +03007889 if (priv->mac80211_registered)
Zhu Yib481de92007-09-25 17:54:57 -07007890 ieee80211_unregister_hw(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07007891
Mohamed Abbas6ef89d02007-10-25 17:15:47 +08007892 /*netif_stop_queue(dev); */
7893 flush_workqueue(priv->workqueue);
7894
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007895 /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07007896 * priv->workqueue... so we can't take down the workqueue
7897 * until now... */
7898 destroy_workqueue(priv->workqueue);
7899 priv->workqueue = NULL;
7900
Zhu Yib481de92007-09-25 17:54:57 -07007901 pci_iounmap(pdev, priv->hw_base);
7902 pci_release_regions(pdev);
7903 pci_disable_device(pdev);
7904 pci_set_drvdata(pdev, NULL);
7905
Reinette Chatre849e0dc2008-01-23 10:15:18 -08007906 iwl3945_free_channel_map(priv);
7907 iwl3945_free_geos(priv);
Abhijeet Kolekarf2c7e522008-12-19 10:37:27 +08007908 kfree(priv->scan39);
Zhu Yib481de92007-09-25 17:54:57 -07007909 if (priv->ibss_beacon)
7910 dev_kfree_skb(priv->ibss_beacon);
7911
7912 ieee80211_free_hw(priv->hw);
7913}
7914
7915#ifdef CONFIG_PM
7916
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007917static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07007918{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007919 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007920
Zhu Yie655b9f2008-01-24 02:19:38 -08007921 if (priv->is_open) {
7922 set_bit(STATUS_IN_SUSPEND, &priv->status);
7923 iwl3945_mac_stop(priv->hw);
7924 priv->is_open = 1;
7925 }
Zhu Yib481de92007-09-25 17:54:57 -07007926
Zhu Yib481de92007-09-25 17:54:57 -07007927 pci_set_power_state(pdev, PCI_D3hot);
7928
Zhu Yib481de92007-09-25 17:54:57 -07007929 return 0;
7930}
7931
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007932static int iwl3945_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07007933{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007934 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07007935
Zhu Yib481de92007-09-25 17:54:57 -07007936 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07007937
Zhu Yie655b9f2008-01-24 02:19:38 -08007938 if (priv->is_open)
7939 iwl3945_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07007940
Zhu Yie655b9f2008-01-24 02:19:38 -08007941 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007942 return 0;
7943}
7944
7945#endif /* CONFIG_PM */
7946
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007947/*************** RFKILL FUNCTIONS **********/
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007948#ifdef CONFIG_IWL3945_RFKILL
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007949/* software rf-kill from user */
7950static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
7951{
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007952 struct iwl_priv *priv = data;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007953 int err = 0;
7954
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007955 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007956 return 0;
7957
7958 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7959 return 0;
7960
Tomas Winklera96a27f2008-10-23 23:48:56 -07007961 IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007962 mutex_lock(&priv->mutex);
7963
7964 switch (state) {
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007965 case RFKILL_STATE_UNBLOCKED:
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08007966 if (iwl_is_rfkill_hw(priv)) {
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007967 err = -EBUSY;
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007968 goto out_unlock;
7969 }
7970 iwl3945_radio_kill_sw(priv, 0);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007971 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007972 case RFKILL_STATE_SOFT_BLOCKED:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007973 iwl3945_radio_kill_sw(priv, 1);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007974 break;
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007975 default:
Winkler, Tomas39aadf82008-12-19 10:37:32 +08007976 IWL_WARN(priv, "received unexpected RFKILL state %d\n", state);
Zhu Yiacdfe9b2008-06-30 17:23:32 +08007977 break;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007978 }
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007979out_unlock:
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007980 mutex_unlock(&priv->mutex);
7981
7982 return err;
7983}
7984
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08007985int iwl3945_rfkill_init(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007986{
7987 struct device *device = wiphy_dev(priv->hw->wiphy);
7988 int ret = 0;
7989
7990 BUG_ON(device == NULL);
7991
7992 IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02007993 priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
7994 if (!priv->rfkill) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08007995 IWL_ERR(priv, "Unable to allocate rfkill device.\n");
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08007996 ret = -ENOMEM;
7997 goto error;
7998 }
7999
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008000 priv->rfkill->name = priv->cfg->name;
8001 priv->rfkill->data = priv;
8002 priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
8003 priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
8004 priv->rfkill->user_claim_unsupported = 1;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008005
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008006 priv->rfkill->dev.class->suspend = NULL;
8007 priv->rfkill->dev.class->resume = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008008
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008009 ret = rfkill_register(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008010 if (ret) {
Winkler, Tomas15b16872008-12-19 10:37:33 +08008011 IWL_ERR(priv, "Unable to register rfkill: %d\n", ret);
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008012 goto freed_rfkill;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008013 }
8014
8015 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
8016 return ret;
8017
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008018freed_rfkill:
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008019 if (priv->rfkill != NULL)
8020 rfkill_free(priv->rfkill);
8021 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008022
8023error:
8024 IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
8025 return ret;
8026}
8027
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008028void iwl3945_rfkill_unregister(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008029{
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008030 if (priv->rfkill)
8031 rfkill_unregister(priv->rfkill);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008032
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008033 priv->rfkill = NULL;
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008034}
8035
8036/* set rf-kill to the right state. */
Abhijeet Kolekar4a8a4322008-12-19 10:37:28 +08008037void iwl3945_rfkill_set_hw_state(struct iwl_priv *priv)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008038{
8039
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008040 if (!priv->rfkill)
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008041 return;
8042
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08008043 if (iwl_is_rfkill_hw(priv)) {
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008044 rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
8045 return;
8046 }
8047
Kolekar, Abhijeet775a6e22008-12-19 10:37:36 +08008048 if (!iwl_is_rfkill_sw(priv))
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008049 rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008050 else
Adel Gadllah80fcc9e2008-07-01 17:49:50 +02008051 rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
Abhijeet Kolekarebef2002008-06-30 17:23:18 +08008052}
8053#endif
8054
Zhu Yib481de92007-09-25 17:54:57 -07008055/*****************************************************************************
8056 *
8057 * driver and module entry point
8058 *
8059 *****************************************************************************/
8060
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008061static struct pci_driver iwl3945_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008062 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008063 .id_table = iwl3945_hw_card_ids,
8064 .probe = iwl3945_pci_probe,
8065 .remove = __devexit_p(iwl3945_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008066#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008067 .suspend = iwl3945_pci_suspend,
8068 .resume = iwl3945_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008069#endif
8070};
8071
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008072static int __init iwl3945_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008073{
8074
8075 int ret;
8076 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8077 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008078
8079 ret = iwl3945_rate_control_register();
8080 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08008081 printk(KERN_ERR DRV_NAME
8082 "Unable to register rate control algorithm: %d\n", ret);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008083 return ret;
8084 }
8085
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008086 ret = pci_register_driver(&iwl3945_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008087 if (ret) {
Samuel Ortiza3139c52008-12-19 10:37:09 +08008088 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008089 goto error_register;
Zhu Yib481de92007-09-25 17:54:57 -07008090 }
Zhu Yib481de92007-09-25 17:54:57 -07008091
8092 return ret;
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008093
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008094error_register:
8095 iwl3945_rate_control_unregister();
8096 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07008097}
8098
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008099static void __exit iwl3945_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008100{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008101 pci_unregister_driver(&iwl3945_driver);
Reinette Chatre897e1cf2008-03-28 16:21:09 -07008102 iwl3945_rate_control_unregister();
Zhu Yib481de92007-09-25 17:54:57 -07008103}
8104
Reinette Chatrea0987a82008-12-02 12:14:06 -08008105MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
Zhu Yi25cb6ca2008-09-11 11:45:22 +08008106
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08008107module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008108MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08008109module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008110MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08008111module_param_named(hwcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008112MODULE_PARM_DESC(hwcrypto,
8113 "using hardware crypto engine (default 0 [software])\n");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08008114module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008115MODULE_PARM_DESC(debug, "debug output mask");
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08008116module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008117MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8118
Kolekar, Abhijeetdf878d82008-12-19 10:37:35 +08008119module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008120MODULE_PARM_DESC(queues_num, "number of hw queues.");
8121
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008122module_exit(iwl3945_exit);
8123module_init(iwl3945_init);