blob: a2ee34e40b20085c1d77c39466ae09def619a93f [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:
25 * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26 * 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>
32#include <linux/version.h>
33#include <linux/init.h>
34#include <linux/pci.h>
35#include <linux/dma-mapping.h>
36#include <linux/delay.h>
37#include <linux/skbuff.h>
38#include <linux/netdevice.h>
39#include <linux/wireless.h>
40#include <linux/firmware.h>
Zhu Yib481de92007-09-25 17:54:57 -070041#include <linux/etherdevice.h>
42#include <linux/if_arp.h>
43
Zhu Yib481de92007-09-25 17:54:57 -070044#include <net/mac80211.h>
45
46#include <asm/div64.h>
47
Assaf Krauss6bc913b2008-03-11 16:17:18 -070048#include "iwl-eeprom.h"
Tomas Winkler82b9a122008-03-04 18:09:30 -080049#include "iwl-core.h"
Zhu Yib481de92007-09-25 17:54:57 -070050#include "iwl-4965.h"
51#include "iwl-helpers.h"
52
Tomas Winklerc79dd5b2008-03-12 16:58:50 -070053static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080054 struct iwl4965_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080055
Zhu Yib481de92007-09-25 17:54:57 -070056/******************************************************************************
57 *
58 * module boiler plate
59 *
60 ******************************************************************************/
61
62/* module parameters */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080063static int iwl4965_param_disable_hw_scan; /* def: 0 = use 4965's h/w scan */
64static int iwl4965_param_debug; /* def: 0 = minimal debug log messages */
Ben Cahill9fbab512007-11-29 11:09:47 +080065static int iwl4965_param_disable; /* def: enable radio */
66static int iwl4965_param_antenna; /* def: 0 = both antennas (use diversity) */
67int iwl4965_param_hwcrypto; /* def: using software encryption */
Cahill, Ben M6440adb2007-11-29 11:09:55 +080068static int iwl4965_param_qos_enable = 1; /* def: 1 = use quality of service */
69int iwl4965_param_queues_num = IWL_MAX_NUM_QUEUES; /* def: 16 Tx queues */
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +020070int iwl4965_param_amsdu_size_8K; /* def: enable 8K amsdu size */
Zhu Yib481de92007-09-25 17:54:57 -070071
72/*
73 * module name, copyright, version, etc.
74 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
75 */
76
77#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
78
Tomas Winkler0a6857e2008-03-12 16:58:49 -070079#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070080#define VD "d"
81#else
82#define VD
83#endif
84
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080085#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070086#define VS "s"
87#else
88#define VS
89#endif
90
Tomas Winklerdf48c322008-03-06 10:40:19 -080091#define DRV_VERSION IWLWIFI_VERSION VD VS
Zhu Yib481de92007-09-25 17:54:57 -070092
Zhu Yib481de92007-09-25 17:54:57 -070093
94MODULE_DESCRIPTION(DRV_DESCRIPTION);
95MODULE_VERSION(DRV_VERSION);
96MODULE_AUTHOR(DRV_COPYRIGHT);
97MODULE_LICENSE("GPL");
98
99__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
100{
101 u16 fc = le16_to_cpu(hdr->frame_control);
102 int hdr_len = ieee80211_get_hdrlen(fc);
103
104 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
105 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
106 return NULL;
107}
108
Johannes Berg8318d782008-01-24 19:38:38 +0100109static const struct ieee80211_supported_band *iwl4965_get_hw_mode(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700110 struct iwl_priv *priv, enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -0700111{
Johannes Berg8318d782008-01-24 19:38:38 +0100112 return priv->hw->wiphy->bands[band];
Zhu Yib481de92007-09-25 17:54:57 -0700113}
114
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800115static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700116{
117 /* Single white space is for Linksys APs */
118 if (essid_len == 1 && essid[0] == ' ')
119 return 1;
120
121 /* Otherwise, if the entire essid is 0, we assume it is hidden */
122 while (essid_len) {
123 essid_len--;
124 if (essid[essid_len] != '\0')
125 return 0;
126 }
127
128 return 1;
129}
130
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800131static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700132{
133 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
134 const char *s = essid;
135 char *d = escaped;
136
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800137 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700138 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
139 return escaped;
140 }
141
142 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
143 while (essid_len--) {
144 if (*s == '\0') {
145 *d++ = '\\';
146 *d++ = '0';
147 s++;
148 } else
149 *d++ = *s++;
150 }
151 *d = '\0';
152 return escaped;
153}
154
Zhu Yib481de92007-09-25 17:54:57 -0700155/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
156 * DMA services
157 *
158 * Theory of operation
159 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800160 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
161 * of buffer descriptors, each of which points to one or more data buffers for
162 * the device to read from or fill. Driver and device exchange status of each
163 * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
164 * entries in each circular buffer, to protect against confusing empty and full
165 * queue states.
166 *
167 * The device reads or writes the data in the queues via the device's several
168 * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
Zhu Yib481de92007-09-25 17:54:57 -0700169 *
170 * For Tx queue, there are low mark and high mark limits. If, after queuing
171 * the packet for Tx, free space become < low mark, Tx queue stopped. When
172 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
173 * Tx queue resumed.
174 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800175 * The 4965 operates with up to 17 queues: One receive queue, one transmit
176 * queue (#4) for sending commands to the device firmware, and 15 other
177 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
Ben Cahille3851442007-11-29 11:10:07 +0800178 *
179 * See more detailed info in iwl-4965-hw.h.
Zhu Yib481de92007-09-25 17:54:57 -0700180 ***************************************************/
181
Ron Rindjunskyfe01b472008-01-28 14:07:24 +0200182int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700183{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800184 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700185
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800186 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700187 s -= q->n_bd;
188
189 if (s <= 0)
190 s += q->n_window;
191 /* keep some reserve to not confuse empty and full situations */
192 s -= 2;
193 if (s < 0)
194 s = 0;
195 return s;
196}
197
Zhu Yib481de92007-09-25 17:54:57 -0700198
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800199static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700200{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800201 return q->write_ptr > q->read_ptr ?
202 (i >= q->read_ptr && i < q->write_ptr) :
203 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700204}
205
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800206static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700207{
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800208 /* This is for scan command, the big buffer at end of command array */
Zhu Yib481de92007-09-25 17:54:57 -0700209 if (is_huge)
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800210 return q->n_window; /* must be power of 2 */
Zhu Yib481de92007-09-25 17:54:57 -0700211
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800212 /* Otherwise, use normal size buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700213 return index & (q->n_window - 1);
214}
215
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800216/**
217 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
218 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700219static int iwl4965_queue_init(struct iwl_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700220 int count, int slots_num, u32 id)
221{
222 q->n_bd = count;
223 q->n_window = slots_num;
224 q->id = id;
225
Tomas Winklerc54b6792008-03-06 17:36:53 -0800226 /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
227 * and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700228 BUG_ON(!is_power_of_2(count));
229
230 /* slots_num must be power-of-two size, otherwise
231 * get_cmd_index is broken. */
232 BUG_ON(!is_power_of_2(slots_num));
233
234 q->low_mark = q->n_window / 4;
235 if (q->low_mark < 4)
236 q->low_mark = 4;
237
238 q->high_mark = q->n_window / 8;
239 if (q->high_mark < 2)
240 q->high_mark = 2;
241
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800242 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700243
244 return 0;
245}
246
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800247/**
248 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
249 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700250static int iwl4965_tx_queue_alloc(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800251 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700252{
253 struct pci_dev *dev = priv->pci_dev;
254
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800255 /* Driver private data, only for Tx (not command) queues,
256 * not shared with device. */
Zhu Yib481de92007-09-25 17:54:57 -0700257 if (id != IWL_CMD_QUEUE_NUM) {
258 txq->txb = kmalloc(sizeof(txq->txb[0]) *
259 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
260 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800261 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700262 "structures failed\n");
263 goto error;
264 }
265 } else
266 txq->txb = NULL;
267
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800268 /* Circular buffer of transmit frame descriptors (TFDs),
269 * shared with device */
Zhu Yib481de92007-09-25 17:54:57 -0700270 txq->bd = pci_alloc_consistent(dev,
271 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
272 &txq->q.dma_addr);
273
274 if (!txq->bd) {
275 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
276 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
277 goto error;
278 }
279 txq->q.id = id;
280
281 return 0;
282
283 error:
284 if (txq->txb) {
285 kfree(txq->txb);
286 txq->txb = NULL;
287 }
288
289 return -ENOMEM;
290}
291
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800292/**
293 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
294 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700295int iwl4965_tx_queue_init(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800296 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700297{
298 struct pci_dev *dev = priv->pci_dev;
299 int len;
300 int rc = 0;
301
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800302 /*
303 * Alloc buffer array for commands (Tx or other types of commands).
304 * For the command queue (#4), allocate command space + one big
305 * command for scan, since scan command is very huge; the system will
306 * not have two scans at the same time, so only one is needed.
Tomas Winklerbb542442007-12-05 20:59:59 +0200307 * For normal Tx queues (all other queues), no super-size command
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800308 * space is needed.
309 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800310 len = sizeof(struct iwl4965_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700311 if (txq_id == IWL_CMD_QUEUE_NUM)
312 len += IWL_MAX_SCAN_SIZE;
313 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
314 if (!txq->cmd)
315 return -ENOMEM;
316
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800317 /* Alloc driver data array and TFD circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800318 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700319 if (rc) {
320 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
321
322 return -ENOMEM;
323 }
324 txq->need_update = 0;
325
326 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Tomas Winklerc54b6792008-03-06 17:36:53 -0800327 * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700328 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800329
330 /* Initialize queue's high/low-water marks, and head/tail indexes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800331 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700332
Cahill, Ben M8b6eaea2007-11-29 11:09:54 +0800333 /* Tell device where to find queue */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800334 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700335
336 return 0;
337}
338
339/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800340 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700341 * @txq: Transmit queue to deallocate.
342 *
343 * Empty queue by removing and destroying all BD's.
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800344 * Free all buffers.
345 * 0-fill, but do not free "txq" descriptor structure.
Zhu Yib481de92007-09-25 17:54:57 -0700346 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700347void iwl4965_tx_queue_free(struct iwl_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700348{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800349 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700350 struct pci_dev *dev = priv->pci_dev;
351 int len;
352
353 if (q->n_bd == 0)
354 return;
355
356 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800357 for (; q->write_ptr != q->read_ptr;
Tomas Winklerc54b6792008-03-06 17:36:53 -0800358 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800359 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700360
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800361 len = sizeof(struct iwl4965_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700362 if (q->id == IWL_CMD_QUEUE_NUM)
363 len += IWL_MAX_SCAN_SIZE;
364
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800365 /* De-alloc array of command/tx buffers */
Zhu Yib481de92007-09-25 17:54:57 -0700366 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
367
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800368 /* De-alloc circular buffer of TFDs */
Zhu Yib481de92007-09-25 17:54:57 -0700369 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800370 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700371 txq->q.n_bd, txq->bd, txq->q.dma_addr);
372
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800373 /* De-alloc array of per-TFD driver data */
Zhu Yib481de92007-09-25 17:54:57 -0700374 if (txq->txb) {
375 kfree(txq->txb);
376 txq->txb = NULL;
377 }
378
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800379 /* 0-fill queue descriptor structure */
Zhu Yib481de92007-09-25 17:54:57 -0700380 memset(txq, 0, sizeof(*txq));
381}
382
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800383const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700384
385/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800386 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700387 * the functionality provided here
388 */
389
390/**************************************************************/
391
Ian Schram01ebd062007-10-25 17:15:22 +0800392#if 0 /* temporary disable till we add real remove station */
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800393/**
394 * iwl4965_remove_station - Remove driver's knowledge of station.
395 *
396 * NOTE: This does not remove station from device's station table.
397 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700398static u8 iwl4965_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700399{
400 int index = IWL_INVALID_STATION;
401 int i;
402 unsigned long flags;
403
404 spin_lock_irqsave(&priv->sta_lock, flags);
405
406 if (is_ap)
407 index = IWL_AP_ID;
408 else if (is_broadcast_ether_addr(addr))
409 index = priv->hw_setting.bcast_sta_id;
410 else
411 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
412 if (priv->stations[i].used &&
413 !compare_ether_addr(priv->stations[i].sta.sta.addr,
414 addr)) {
415 index = i;
416 break;
417 }
418
419 if (unlikely(index == IWL_INVALID_STATION))
420 goto out;
421
422 if (priv->stations[index].used) {
423 priv->stations[index].used = 0;
424 priv->num_stations--;
425 }
426
427 BUG_ON(priv->num_stations < 0);
428
429out:
430 spin_unlock_irqrestore(&priv->sta_lock, flags);
431 return 0;
432}
Zhu Yi556f8db2007-09-27 11:27:33 +0800433#endif
Zhu Yib481de92007-09-25 17:54:57 -0700434
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800435/**
436 * iwl4965_clear_stations_table - Clear the driver's station table
437 *
438 * NOTE: This does not clear or otherwise alter the device's station table.
439 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700440static void iwl4965_clear_stations_table(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700441{
442 unsigned long flags;
443
444 spin_lock_irqsave(&priv->sta_lock, flags);
445
446 priv->num_stations = 0;
447 memset(priv->stations, 0, sizeof(priv->stations));
448
449 spin_unlock_irqrestore(&priv->sta_lock, flags);
450}
451
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800452/**
453 * iwl4965_add_station_flags - Add station to tables in driver and device
454 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700455u8 iwl4965_add_station_flags(struct iwl_priv *priv, const u8 *addr,
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200456 int is_ap, u8 flags, void *ht_data)
Zhu Yib481de92007-09-25 17:54:57 -0700457{
458 int i;
459 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800460 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700461 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700462 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700463
464 spin_lock_irqsave(&priv->sta_lock, flags_spin);
465 if (is_ap)
466 index = IWL_AP_ID;
467 else if (is_broadcast_ether_addr(addr))
468 index = priv->hw_setting.bcast_sta_id;
469 else
470 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
471 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
472 addr)) {
473 index = i;
474 break;
475 }
476
477 if (!priv->stations[i].used &&
478 index == IWL_INVALID_STATION)
479 index = i;
480 }
481
482
Ben Cahill9fbab512007-11-29 11:09:47 +0800483 /* These two conditions have the same outcome, but keep them separate
484 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700485 if (unlikely(index == IWL_INVALID_STATION)) {
486 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
487 return index;
488 }
489
490 if (priv->stations[index].used &&
491 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
492 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
493 return index;
494 }
495
496
Joe Perches0795af52007-10-03 17:59:30 -0700497 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700498 station = &priv->stations[index];
499 station->used = 1;
500 priv->num_stations++;
501
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800502 /* Set up the REPLY_ADD_STA command to send to device */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800503 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700504 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
505 station->sta.mode = 0;
506 station->sta.sta.sta_id = index;
507 station->sta.station_flags = 0;
508
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800509#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700510 /* BCAST station and IBSS stations do not work in HT mode */
511 if (index != priv->hw_setting.bcast_sta_id &&
512 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200513 iwl4965_set_ht_add_station(priv, index,
514 (struct ieee80211_ht_info *) ht_data);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800515#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700516
517 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800518
519 /* Add station to device's station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800520 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700521 return index;
522
523}
524
525/*************** DRIVER STATUS FUNCTIONS *****/
526
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700527static inline int iwl4965_is_ready(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700528{
529 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
530 * set but EXIT_PENDING is not */
531 return test_bit(STATUS_READY, &priv->status) &&
532 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
533 !test_bit(STATUS_EXIT_PENDING, &priv->status);
534}
535
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700536static inline int iwl4965_is_alive(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700537{
538 return test_bit(STATUS_ALIVE, &priv->status);
539}
540
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700541static inline int iwl4965_is_init(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700542{
543 return test_bit(STATUS_INIT, &priv->status);
544}
545
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700546static inline int iwl4965_is_rfkill(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700547{
548 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
549 test_bit(STATUS_RF_KILL_SW, &priv->status);
550}
551
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700552static inline int iwl4965_is_ready_rf(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700553{
554
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800555 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700556 return 0;
557
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800558 return iwl4965_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700559}
560
561/*************** HOST COMMAND QUEUE FUNCTIONS *****/
562
563#define IWL_CMD(x) case x : return #x
564
565static const char *get_cmd_string(u8 cmd)
566{
567 switch (cmd) {
568 IWL_CMD(REPLY_ALIVE);
569 IWL_CMD(REPLY_ERROR);
570 IWL_CMD(REPLY_RXON);
571 IWL_CMD(REPLY_RXON_ASSOC);
572 IWL_CMD(REPLY_QOS_PARAM);
573 IWL_CMD(REPLY_RXON_TIMING);
574 IWL_CMD(REPLY_ADD_STA);
575 IWL_CMD(REPLY_REMOVE_STA);
576 IWL_CMD(REPLY_REMOVE_ALL_STA);
577 IWL_CMD(REPLY_TX);
578 IWL_CMD(REPLY_RATE_SCALE);
579 IWL_CMD(REPLY_LEDS_CMD);
580 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
581 IWL_CMD(RADAR_NOTIFICATION);
582 IWL_CMD(REPLY_QUIET_CMD);
583 IWL_CMD(REPLY_CHANNEL_SWITCH);
584 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
585 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
586 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
587 IWL_CMD(POWER_TABLE_CMD);
588 IWL_CMD(PM_SLEEP_NOTIFICATION);
589 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
590 IWL_CMD(REPLY_SCAN_CMD);
591 IWL_CMD(REPLY_SCAN_ABORT_CMD);
592 IWL_CMD(SCAN_START_NOTIFICATION);
593 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
594 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
595 IWL_CMD(BEACON_NOTIFICATION);
596 IWL_CMD(REPLY_TX_BEACON);
597 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
598 IWL_CMD(QUIET_NOTIFICATION);
599 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
600 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
601 IWL_CMD(REPLY_BT_CONFIG);
602 IWL_CMD(REPLY_STATISTICS_CMD);
603 IWL_CMD(STATISTICS_NOTIFICATION);
604 IWL_CMD(REPLY_CARD_STATE_CMD);
605 IWL_CMD(CARD_STATE_NOTIFICATION);
606 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
607 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
608 IWL_CMD(SENSITIVITY_CMD);
609 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
610 IWL_CMD(REPLY_RX_PHY_CMD);
611 IWL_CMD(REPLY_RX_MPDU_CMD);
612 IWL_CMD(REPLY_4965_RX);
613 IWL_CMD(REPLY_COMPRESSED_BA);
614 default:
615 return "UNKNOWN";
616
617 }
618}
619
620#define HOST_COMPLETE_TIMEOUT (HZ / 2)
621
622/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800623 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700624 * @priv: device private data point
625 * @cmd: a point to the ucode command structure
626 *
627 * The function returns < 0 values to indicate the operation is
628 * failed. On success, it turns the index (> 0) of command in the
629 * command queue.
630 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700631static int iwl4965_enqueue_hcmd(struct iwl_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700632{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800633 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
634 struct iwl4965_queue *q = &txq->q;
635 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700636 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800637 struct iwl4965_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700638 u32 idx;
639 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
640 dma_addr_t phys_addr;
641 int ret;
642 unsigned long flags;
643
644 /* If any of the command structures end up being larger than
645 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
646 * we will need to increase the size of the TFD entries */
647 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
648 !(cmd->meta.flags & CMD_SIZE_HUGE));
649
Gregory Greenmanc342a1b2008-02-06 11:20:40 -0800650 if (iwl4965_is_rfkill(priv)) {
651 IWL_DEBUG_INFO("Not sending command - RF KILL");
652 return -EIO;
653 }
654
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800655 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700656 IWL_ERROR("No space for Tx\n");
657 return -ENOSPC;
658 }
659
660 spin_lock_irqsave(&priv->hcmd_lock, flags);
661
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800662 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700663 memset(tfd, 0, sizeof(*tfd));
664
665 control_flags = (u32 *) tfd;
666
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800667 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700668 out_cmd = &txq->cmd[idx];
669
670 out_cmd->hdr.cmd = cmd->id;
671 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
672 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
673
674 /* At this point, the out_cmd now has all of the incoming cmd
675 * information */
676
677 out_cmd->hdr.flags = 0;
678 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800679 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700680 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
681 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
682
683 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800684 offsetof(struct iwl4965_cmd, hdr);
685 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700686
687 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
688 "%d bytes at %d[%d]:%d\n",
689 get_cmd_string(out_cmd->hdr.cmd),
690 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800691 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700692
693 txq->need_update = 1;
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800694
695 /* Set up entry in queue's byte count circular buffer */
Zhu Yib481de92007-09-25 17:54:57 -0700696 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800697
698 /* Increment and update queue's write index */
Tomas Winklerc54b6792008-03-06 17:36:53 -0800699 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800700 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700701
702 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
703 return ret ? ret : idx;
704}
705
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700706static int iwl4965_send_cmd_async(struct iwl_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700707{
708 int ret;
709
710 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
711
712 /* An asynchronous command can not expect an SKB to be set. */
713 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
714
715 /* An asynchronous command MUST have a callback. */
716 BUG_ON(!cmd->meta.u.callback);
717
718 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
719 return -EBUSY;
720
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800721 ret = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700722 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800723 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700724 get_cmd_string(cmd->id), ret);
725 return ret;
726 }
727 return 0;
728}
729
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700730static int iwl4965_send_cmd_sync(struct iwl_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700731{
732 int cmd_idx;
733 int ret;
734 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
735
736 BUG_ON(cmd->meta.flags & CMD_ASYNC);
737
738 /* A synchronous command can not have a callback set. */
739 BUG_ON(cmd->meta.u.callback != NULL);
740
741 if (atomic_xchg(&entry, 1)) {
742 IWL_ERROR("Error sending %s: Already sending a host command\n",
743 get_cmd_string(cmd->id));
744 return -EBUSY;
745 }
746
747 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
748
749 if (cmd->meta.flags & CMD_WANT_SKB)
750 cmd->meta.source = &cmd->meta;
751
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800752 cmd_idx = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700753 if (cmd_idx < 0) {
754 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800755 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700756 get_cmd_string(cmd->id), ret);
757 goto out;
758 }
759
760 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
761 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
762 HOST_COMPLETE_TIMEOUT);
763 if (!ret) {
764 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
765 IWL_ERROR("Error sending %s: time out after %dms.\n",
766 get_cmd_string(cmd->id),
767 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
768
769 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
770 ret = -ETIMEDOUT;
771 goto cancel;
772 }
773 }
774
775 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
776 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
777 get_cmd_string(cmd->id));
778 ret = -ECANCELED;
779 goto fail;
780 }
781 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
782 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
783 get_cmd_string(cmd->id));
784 ret = -EIO;
785 goto fail;
786 }
787 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
788 IWL_ERROR("Error: Response NULL in '%s'\n",
789 get_cmd_string(cmd->id));
790 ret = -EIO;
791 goto out;
792 }
793
794 ret = 0;
795 goto out;
796
797cancel:
798 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800799 struct iwl4965_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700800
801 /* Cancel the CMD_WANT_SKB flag for the cmd in the
802 * TX cmd queue. Otherwise in case the cmd comes
803 * in later, it will possibly set an invalid
804 * address (cmd->meta.source). */
805 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
806 qcmd->meta.flags &= ~CMD_WANT_SKB;
807 }
808fail:
809 if (cmd->meta.u.skb) {
810 dev_kfree_skb_any(cmd->meta.u.skb);
811 cmd->meta.u.skb = NULL;
812 }
813out:
814 atomic_set(&entry, 0);
815 return ret;
816}
817
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700818int iwl4965_send_cmd(struct iwl_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700819{
Zhu Yib481de92007-09-25 17:54:57 -0700820 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800821 return iwl4965_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700822
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800823 return iwl4965_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700824}
825
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700826int iwl4965_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700827{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800828 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700829 .id = id,
830 .len = len,
831 .data = data,
832 };
833
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800834 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700835}
836
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700837static int __must_check iwl4965_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700838{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800839 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700840 .id = id,
841 .len = sizeof(val),
842 .data = &val,
843 };
844
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800845 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700846}
847
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700848int iwl4965_send_statistics_request(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700849{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800850 return iwl4965_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700851}
852
853/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800854 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700855 *
856 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800857 * NOTE: mutex must be held before calling this fnction
858 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700859static int iwl4965_rxon_add_station(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700860 const u8 *addr, int is_ap)
861{
Zhu Yi556f8db2007-09-27 11:27:33 +0800862 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700863
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800864 /* Add station to device's station table */
Ron Rindjunsky67d62032007-11-26 16:14:40 +0200865#ifdef CONFIG_IWL4965_HT
866 struct ieee80211_conf *conf = &priv->hw->conf;
867 struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
868
869 if ((is_ap) &&
870 (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
871 (priv->iw_mode == IEEE80211_IF_TYPE_STA))
872 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
873 0, cur_ht_config);
874 else
875#endif /* CONFIG_IWL4965_HT */
876 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
877 0, NULL);
Cahill, Ben M6440adb2007-11-29 11:09:55 +0800878
879 /* Set up default rate scaling table in device's station table */
Zhu Yib481de92007-09-25 17:54:57 -0700880 iwl4965_add_station(priv, addr, is_ap);
881
Zhu Yi556f8db2007-09-27 11:27:33 +0800882 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700883}
884
885/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800886 * iwl4965_set_rxon_channel - Set the phymode and channel values in staging RXON
Zhu Yib481de92007-09-25 17:54:57 -0700887 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
888 * @channel: Any channel valid for the requested phymode
889
890 * In addition to setting the staging RXON, priv->phymode is also set.
891 *
892 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
893 * in the staging RXON flag structure based on the phymode
894 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -0700895static int iwl4965_set_rxon_channel(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +0100896 enum ieee80211_band band,
Ben Cahill9fbab512007-11-29 11:09:47 +0800897 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700898{
Johannes Berg8318d782008-01-24 19:38:38 +0100899 if (!iwl4965_get_channel_info(priv, band, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700900 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
Johannes Berg8318d782008-01-24 19:38:38 +0100901 channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700902 return -EINVAL;
903 }
904
905 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
Johannes Berg8318d782008-01-24 19:38:38 +0100906 (priv->band == band))
Zhu Yib481de92007-09-25 17:54:57 -0700907 return 0;
908
909 priv->staging_rxon.channel = cpu_to_le16(channel);
Johannes Berg8318d782008-01-24 19:38:38 +0100910 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -0700911 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
912 else
913 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
914
Johannes Berg8318d782008-01-24 19:38:38 +0100915 priv->band = band;
Zhu Yib481de92007-09-25 17:54:57 -0700916
Johannes Berg8318d782008-01-24 19:38:38 +0100917 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
Zhu Yib481de92007-09-25 17:54:57 -0700918
919 return 0;
920}
921
922/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800923 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700924 *
925 * NOTE: This is really only useful during development and can eventually
926 * be #ifdef'd out once the driver is stable and folks aren't actively
927 * making changes
928 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800929static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700930{
931 int error = 0;
932 int counter = 1;
933
934 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
935 error |= le32_to_cpu(rxon->flags &
936 (RXON_FLG_TGJ_NARROW_BAND_MSK |
937 RXON_FLG_RADAR_DETECT_MSK));
938 if (error)
939 IWL_WARNING("check 24G fields %d | %d\n",
940 counter++, error);
941 } else {
942 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
943 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
944 if (error)
945 IWL_WARNING("check 52 fields %d | %d\n",
946 counter++, error);
947 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
948 if (error)
949 IWL_WARNING("check 52 CCK %d | %d\n",
950 counter++, error);
951 }
952 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
953 if (error)
954 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
955
956 /* make sure basic rates 6Mbps and 1Mbps are supported */
957 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
958 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
959 if (error)
960 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
961
962 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
963 if (error)
964 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
965
966 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
967 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
968 if (error)
969 IWL_WARNING("check CCK and short slot %d | %d\n",
970 counter++, error);
971
972 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
973 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
974 if (error)
975 IWL_WARNING("check CCK & auto detect %d | %d\n",
976 counter++, error);
977
978 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
979 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
980 if (error)
981 IWL_WARNING("check TGG and auto detect %d | %d\n",
982 counter++, error);
983
984 if (error)
985 IWL_WARNING("Tuning to channel %d\n",
986 le16_to_cpu(rxon->channel));
987
988 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800989 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700990 return -1;
991 }
992 return 0;
993}
994
995/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800996 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800997 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700998 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800999 * If the RXON structure is changing enough to require a new tune,
1000 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
1001 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -07001002 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001003static int iwl4965_full_rxon_required(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001004{
1005
1006 /* These items are only settable from the full RXON command */
1007 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
1008 compare_ether_addr(priv->staging_rxon.bssid_addr,
1009 priv->active_rxon.bssid_addr) ||
1010 compare_ether_addr(priv->staging_rxon.node_addr,
1011 priv->active_rxon.node_addr) ||
1012 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
1013 priv->active_rxon.wlap_bssid_addr) ||
1014 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
1015 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
1016 (priv->staging_rxon.air_propagation !=
1017 priv->active_rxon.air_propagation) ||
1018 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
1019 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
1020 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
1021 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
1022 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
1023 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
1024 return 1;
1025
1026 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
1027 * be updated with the RXON_ASSOC command -- however only some
1028 * flag transitions are allowed using RXON_ASSOC */
1029
1030 /* Check if we are not switching bands */
1031 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1032 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1033 return 1;
1034
1035 /* Check if we are switching association toggle */
1036 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1037 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1038 return 1;
1039
1040 return 0;
1041}
1042
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001043static int iwl4965_send_rxon_assoc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001044{
1045 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001046 struct iwl4965_rx_packet *res = NULL;
1047 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1048 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001049 .id = REPLY_RXON_ASSOC,
1050 .len = sizeof(rxon_assoc),
1051 .meta.flags = CMD_WANT_SKB,
1052 .data = &rxon_assoc,
1053 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001054 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
1055 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001056
1057 if ((rxon1->flags == rxon2->flags) &&
1058 (rxon1->filter_flags == rxon2->filter_flags) &&
1059 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1060 (rxon1->ofdm_ht_single_stream_basic_rates ==
1061 rxon2->ofdm_ht_single_stream_basic_rates) &&
1062 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1063 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1064 (rxon1->rx_chain == rxon2->rx_chain) &&
1065 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1066 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1067 return 0;
1068 }
1069
1070 rxon_assoc.flags = priv->staging_rxon.flags;
1071 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1072 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1073 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1074 rxon_assoc.reserved = 0;
1075 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1076 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1077 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1078 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1079 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1080
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001081 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001082 if (rc)
1083 return rc;
1084
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001085 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001086 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1087 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1088 rc = -EIO;
1089 }
1090
1091 priv->alloc_rxb_skb--;
1092 dev_kfree_skb_any(cmd.meta.u.skb);
1093
1094 return rc;
1095}
1096
1097/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001098 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -07001099 *
Ian Schram01ebd062007-10-25 17:15:22 +08001100 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -07001101 * the active_rxon structure is updated with the new data. This
1102 * function correctly transitions out of the RXON_ASSOC_MSK state if
1103 * a HW tune is required based on the RXON structure changes.
1104 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001105static int iwl4965_commit_rxon(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001106{
1107 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001108 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001109 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001110 int rc = 0;
1111
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001112 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001113 return -1;
1114
1115 /* always get timestamp with Rx frame */
1116 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1117
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001118 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001119 if (rc) {
1120 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1121 return -EINVAL;
1122 }
1123
1124 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001125 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -07001126 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001127 if (!iwl4965_full_rxon_required(priv)) {
1128 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001129 if (rc) {
1130 IWL_ERROR("Error setting RXON_ASSOC "
1131 "configuration (%d).\n", rc);
1132 return rc;
1133 }
1134
1135 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1136
1137 return 0;
1138 }
1139
1140 /* station table will be cleared */
1141 priv->assoc_station_added = 0;
1142
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001143#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001144 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1145 if (!priv->error_recovering)
1146 priv->start_calib = 0;
1147
1148 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001149#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001150
1151 /* If we are currently associated and the new config requires
1152 * an RXON_ASSOC and the new config wants the associated mask enabled,
1153 * we must clear the associated from the active configuration
1154 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001155 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001156 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1157 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1158 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1159
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001160 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1161 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001162 &priv->active_rxon);
1163
1164 /* If the mask clearing failed then we set
1165 * active_rxon back to what it was previously */
1166 if (rc) {
1167 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1168 IWL_ERROR("Error clearing ASSOC_MSK on current "
1169 "configuration (%d).\n", rc);
1170 return rc;
1171 }
Zhu Yib481de92007-09-25 17:54:57 -07001172 }
1173
1174 IWL_DEBUG_INFO("Sending RXON\n"
1175 "* with%s RXON_FILTER_ASSOC_MSK\n"
1176 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -07001177 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001178 ((priv->staging_rxon.filter_flags &
1179 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1180 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -07001181 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001182
1183 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001184 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1185 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001186 if (rc) {
1187 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1188 return rc;
1189 }
1190
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001191 iwl4965_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001192
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001193#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001194 if (!priv->error_recovering)
1195 priv->start_calib = 0;
1196
1197 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1198 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001199#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001200
1201 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1202
1203 /* If we issue a new RXON command which required a tune then we must
1204 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001205 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001206 if (rc) {
1207 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1208 return rc;
1209 }
1210
1211 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001212 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001213 IWL_INVALID_STATION) {
1214 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1215 return -EIO;
1216 }
1217
1218 /* If we have set the ASSOC_MSK and we are in BSS mode then
1219 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001220 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001221 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001222 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -07001223 == IWL_INVALID_STATION) {
1224 IWL_ERROR("Error adding AP address for transmit.\n");
1225 return -EIO;
1226 }
1227 priv->assoc_station_added = 1;
1228 }
1229
1230 return 0;
1231}
1232
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001233static int iwl4965_send_bt_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001234{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001235 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001236 .flags = 3,
1237 .lead_time = 0xAA,
1238 .max_kill = 1,
1239 .kill_ack_mask = 0,
1240 .kill_cts_mask = 0,
1241 };
1242
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001243 return iwl4965_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1244 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001245}
1246
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001247static int iwl4965_send_scan_abort(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001248{
1249 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001250 struct iwl4965_rx_packet *res;
1251 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001252 .id = REPLY_SCAN_ABORT_CMD,
1253 .meta.flags = CMD_WANT_SKB,
1254 };
1255
1256 /* If there isn't a scan actively going on in the hardware
1257 * then we are in between scan bands and not actually
1258 * actively scanning, so don't send the abort command */
1259 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1260 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1261 return 0;
1262 }
1263
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001264 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001265 if (rc) {
1266 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1267 return rc;
1268 }
1269
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001270 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001271 if (res->u.status != CAN_ABORT_STATUS) {
1272 /* The scan abort will return 1 for success or
1273 * 2 for "failure". A failure condition can be
1274 * due to simply not being in an active scan which
1275 * can occur if we send the scan abort before we
1276 * the microcode has notified us that a scan is
1277 * completed. */
1278 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1279 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1280 clear_bit(STATUS_SCAN_HW, &priv->status);
1281 }
1282
1283 dev_kfree_skb_any(cmd.meta.u.skb);
1284
1285 return rc;
1286}
1287
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001288static int iwl4965_card_state_sync_callback(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001289 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001290 struct sk_buff *skb)
1291{
1292 return 1;
1293}
1294
1295/*
1296 * CARD_STATE_CMD
1297 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001298 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001299 *
1300 * When in the 'enable' state the card operates as normal.
1301 * When in the 'disable' state, the card enters into a low power mode.
1302 * When in the 'halt' state, the card is shut down and must be fully
1303 * restarted to come back on.
1304 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001305static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001306{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001307 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001308 .id = REPLY_CARD_STATE_CMD,
1309 .len = sizeof(u32),
1310 .data = &flags,
1311 .meta.flags = meta_flag,
1312 };
1313
1314 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001315 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001316
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001317 return iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001318}
1319
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001320static int iwl4965_add_sta_sync_callback(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001321 struct iwl4965_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001322{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001323 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001324
1325 if (!skb) {
1326 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1327 return 1;
1328 }
1329
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001330 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001331 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1332 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1333 res->hdr.flags);
1334 return 1;
1335 }
1336
1337 switch (res->u.add_sta.status) {
1338 case ADD_STA_SUCCESS_MSK:
1339 break;
1340 default:
1341 break;
1342 }
1343
1344 /* We didn't cache the SKB; let the caller free it */
1345 return 1;
1346}
1347
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001348int iwl4965_send_add_station(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001349 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001350{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001351 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001352 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001353 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001354 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001355 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001356 .meta.flags = flags,
1357 .data = sta,
1358 };
1359
1360 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001361 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001362 else
1363 cmd.meta.flags |= CMD_WANT_SKB;
1364
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001365 rc = iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001366
1367 if (rc || (flags & CMD_ASYNC))
1368 return rc;
1369
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001370 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001371 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1372 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1373 res->hdr.flags);
1374 rc = -EIO;
1375 }
1376
1377 if (rc == 0) {
1378 switch (res->u.add_sta.status) {
1379 case ADD_STA_SUCCESS_MSK:
1380 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1381 break;
1382 default:
1383 rc = -EIO;
1384 IWL_WARNING("REPLY_ADD_STA failed\n");
1385 break;
1386 }
1387 }
1388
1389 priv->alloc_rxb_skb--;
1390 dev_kfree_skb_any(cmd.meta.u.skb);
1391
1392 return rc;
1393}
1394
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001395static int iwl4965_update_sta_key_info(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001396 struct ieee80211_key_conf *keyconf,
1397 u8 sta_id)
1398{
1399 unsigned long flags;
1400 __le16 key_flags = 0;
1401
1402 switch (keyconf->alg) {
1403 case ALG_CCMP:
1404 key_flags |= STA_KEY_FLG_CCMP;
1405 key_flags |= cpu_to_le16(
1406 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1407 key_flags &= ~STA_KEY_FLG_INVALID;
1408 break;
1409 case ALG_TKIP:
1410 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001411 default:
1412 return -EINVAL;
1413 }
1414 spin_lock_irqsave(&priv->sta_lock, flags);
1415 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1416 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1417 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1418 keyconf->keylen);
1419
1420 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1421 keyconf->keylen);
1422 priv->stations[sta_id].sta.key.key_flags = key_flags;
1423 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1424 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1425
1426 spin_unlock_irqrestore(&priv->sta_lock, flags);
1427
1428 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001429 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001430 return 0;
1431}
1432
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001433static int iwl4965_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001434{
1435 unsigned long flags;
1436
1437 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001438 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1439 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001440 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1441 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1442 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1443 spin_unlock_irqrestore(&priv->sta_lock, flags);
1444
1445 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001446 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001447 return 0;
1448}
1449
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001450static void iwl4965_clear_free_frames(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001451{
1452 struct list_head *element;
1453
1454 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1455 priv->frames_count);
1456
1457 while (!list_empty(&priv->free_frames)) {
1458 element = priv->free_frames.next;
1459 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001460 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001461 priv->frames_count--;
1462 }
1463
1464 if (priv->frames_count) {
1465 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1466 priv->frames_count);
1467 priv->frames_count = 0;
1468 }
1469}
1470
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001471static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001472{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001473 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001474 struct list_head *element;
1475 if (list_empty(&priv->free_frames)) {
1476 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1477 if (!frame) {
1478 IWL_ERROR("Could not allocate frame!\n");
1479 return NULL;
1480 }
1481
1482 priv->frames_count++;
1483 return frame;
1484 }
1485
1486 element = priv->free_frames.next;
1487 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001488 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001489}
1490
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001491static void iwl4965_free_frame(struct iwl_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001492{
1493 memset(frame, 0, sizeof(*frame));
1494 list_add(&frame->list, &priv->free_frames);
1495}
1496
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001497unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001498 struct ieee80211_hdr *hdr,
1499 const u8 *dest, int left)
1500{
1501
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001502 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001503 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1504 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1505 return 0;
1506
1507 if (priv->ibss_beacon->len > left)
1508 return 0;
1509
1510 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1511
1512 return priv->ibss_beacon->len;
1513}
1514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001515static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001516{
1517 u8 i;
1518
1519 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001520 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001521 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001522 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001523 }
1524
1525 return IWL_RATE_INVALID;
1526}
1527
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001528static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001529{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001530 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001531 unsigned int frame_size;
1532 int rc;
1533 u8 rate;
1534
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001535 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001536
1537 if (!frame) {
1538 IWL_ERROR("Could not obtain free frame buffer for beacon "
1539 "command.\n");
1540 return -ENOMEM;
1541 }
1542
1543 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001544 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001545 0xFF0);
1546 if (rate == IWL_INVALID_RATE)
1547 rate = IWL_RATE_6M_PLCP;
1548 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001549 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001550 if (rate == IWL_INVALID_RATE)
1551 rate = IWL_RATE_1M_PLCP;
1552 }
1553
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001554 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001556 rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001557 &frame->u.cmd[0]);
1558
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001559 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001560
1561 return rc;
1562}
1563
1564/******************************************************************************
1565 *
Zhu Yib481de92007-09-25 17:54:57 -07001566 * Misc. internal state and helper functions
1567 *
1568 ******************************************************************************/
Zhu Yib481de92007-09-25 17:54:57 -07001569
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001570static void iwl4965_unset_hw_setting(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001571{
1572 if (priv->hw_setting.shared_virt)
1573 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001574 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001575 priv->hw_setting.shared_virt,
1576 priv->hw_setting.shared_phys);
1577}
1578
1579/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001580 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001581 *
1582 * return : set the bit for each supported rate insert in ie
1583 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001584static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001585 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001586{
1587 u16 ret_rates = 0, bit;
1588 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001589 u8 *cnt = ie;
1590 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001591
1592 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1593 if (bit & supported_rate) {
1594 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001595 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001596 ((bit & basic_rate) ? 0x80 : 0x00);
1597 (*cnt)++;
1598 (*left)--;
1599 if ((*left <= 0) ||
1600 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001601 break;
1602 }
1603 }
1604
1605 return ret_rates;
1606}
1607
Zhu Yib481de92007-09-25 17:54:57 -07001608/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001609 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001610 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001611static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
Tomas Winkler78330fd2008-02-06 02:37:18 +02001612 enum ieee80211_band band,
1613 struct ieee80211_mgmt *frame,
1614 int left, int is_direct)
Zhu Yib481de92007-09-25 17:54:57 -07001615{
1616 int len = 0;
1617 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001618 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001619#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001620 const struct ieee80211_supported_band *sband =
1621 iwl4965_get_hw_mode(priv, band);
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001622#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001623
1624 /* Make sure there is enough space for the probe request,
1625 * two mandatory IEs and the data */
1626 left -= 24;
1627 if (left < 0)
1628 return 0;
1629 len += 24;
1630
1631 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001632 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001633 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001634 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001635 frame->seq_ctrl = 0;
1636
1637 /* fill in our indirect SSID IE */
1638 /* ...next IE... */
1639
1640 left -= 2;
1641 if (left < 0)
1642 return 0;
1643 len += 2;
1644 pos = &(frame->u.probe_req.variable[0]);
1645 *pos++ = WLAN_EID_SSID;
1646 *pos++ = 0;
1647
1648 /* fill in our direct SSID IE... */
1649 if (is_direct) {
1650 /* ...next IE... */
1651 left -= 2 + priv->essid_len;
1652 if (left < 0)
1653 return 0;
1654 /* ... fill it in... */
1655 *pos++ = WLAN_EID_SSID;
1656 *pos++ = priv->essid_len;
1657 memcpy(pos, priv->essid, priv->essid_len);
1658 pos += priv->essid_len;
1659 len += 2 + priv->essid_len;
1660 }
1661
1662 /* fill in supported rate */
1663 /* ...next IE... */
1664 left -= 2;
1665 if (left < 0)
1666 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001667
Zhu Yib481de92007-09-25 17:54:57 -07001668 /* ... fill it in... */
1669 *pos++ = WLAN_EID_SUPP_RATES;
1670 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001671
mabbasbee488d2007-10-25 17:15:42 +08001672 /* exclude 60M rate */
1673 active_rates = priv->rates_mask;
1674 active_rates &= ~IWL_RATE_60M_MASK;
1675
1676 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001677
Tomas Winklerc7c46672007-10-18 02:04:15 +02001678 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001679 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001680 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001681 active_rates &= ~ret_rates;
1682
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001683 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001684 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001685 active_rates &= ~ret_rates;
1686
Zhu Yib481de92007-09-25 17:54:57 -07001687 len += 2 + *pos;
1688 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001689 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001690 goto fill_end;
1691
1692 /* fill in supported extended rate */
1693 /* ...next IE... */
1694 left -= 2;
1695 if (left < 0)
1696 return 0;
1697 /* ... fill it in... */
1698 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1699 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001700 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001701 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001702 if (*pos > 0)
1703 len += 2 + *pos;
1704
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001705#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02001706 if (sband && sband->ht_info.ht_supported) {
1707 struct ieee80211_ht_cap *ht_cap;
Zhu Yib481de92007-09-25 17:54:57 -07001708 pos += (*pos) + 1;
1709 *pos++ = WLAN_EID_HT_CAPABILITY;
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001710 *pos++ = sizeof(struct ieee80211_ht_cap);
Tomas Winkler78330fd2008-02-06 02:37:18 +02001711 ht_cap = (struct ieee80211_ht_cap *)pos;
1712 ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
1713 memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
1714 ht_cap->ampdu_params_info =(sband->ht_info.ampdu_factor &
1715 IEEE80211_HT_CAP_AMPDU_FACTOR) |
1716 ((sband->ht_info.ampdu_density << 2) &
1717 IEEE80211_HT_CAP_AMPDU_DENSITY);
Ron Rindjunsky8fb880322007-11-26 16:14:38 +02001718 len += 2 + sizeof(struct ieee80211_ht_cap);
Zhu Yib481de92007-09-25 17:54:57 -07001719 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001720#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001721
1722 fill_end:
1723 return (u16)len;
1724}
1725
1726/*
1727 * QoS support
1728*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001729static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001730 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001731{
1732
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001733 return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1734 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001735}
1736
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001737static void iwl4965_reset_qos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001738{
1739 u16 cw_min = 15;
1740 u16 cw_max = 1023;
1741 u8 aifs = 2;
1742 u8 is_legacy = 0;
1743 unsigned long flags;
1744 int i;
1745
1746 spin_lock_irqsave(&priv->lock, flags);
1747 priv->qos_data.qos_active = 0;
1748
1749 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1750 if (priv->qos_data.qos_enable)
1751 priv->qos_data.qos_active = 1;
1752 if (!(priv->active_rate & 0xfff0)) {
1753 cw_min = 31;
1754 is_legacy = 1;
1755 }
1756 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1757 if (priv->qos_data.qos_enable)
1758 priv->qos_data.qos_active = 1;
1759 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1760 cw_min = 31;
1761 is_legacy = 1;
1762 }
1763
1764 if (priv->qos_data.qos_active)
1765 aifs = 3;
1766
1767 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1768 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1769 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1770 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1771 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1772
1773 if (priv->qos_data.qos_active) {
1774 i = 1;
1775 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1776 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1777 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1778 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1779 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1780
1781 i = 2;
1782 priv->qos_data.def_qos_parm.ac[i].cw_min =
1783 cpu_to_le16((cw_min + 1) / 2 - 1);
1784 priv->qos_data.def_qos_parm.ac[i].cw_max =
1785 cpu_to_le16(cw_max);
1786 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1787 if (is_legacy)
1788 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1789 cpu_to_le16(6016);
1790 else
1791 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1792 cpu_to_le16(3008);
1793 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1794
1795 i = 3;
1796 priv->qos_data.def_qos_parm.ac[i].cw_min =
1797 cpu_to_le16((cw_min + 1) / 4 - 1);
1798 priv->qos_data.def_qos_parm.ac[i].cw_max =
1799 cpu_to_le16((cw_max + 1) / 2 - 1);
1800 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1801 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1802 if (is_legacy)
1803 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1804 cpu_to_le16(3264);
1805 else
1806 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1807 cpu_to_le16(1504);
1808 } else {
1809 for (i = 1; i < 4; i++) {
1810 priv->qos_data.def_qos_parm.ac[i].cw_min =
1811 cpu_to_le16(cw_min);
1812 priv->qos_data.def_qos_parm.ac[i].cw_max =
1813 cpu_to_le16(cw_max);
1814 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
1815 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1816 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1817 }
1818 }
1819 IWL_DEBUG_QOS("set QoS to default \n");
1820
1821 spin_unlock_irqrestore(&priv->lock, flags);
1822}
1823
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001824static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07001825{
1826 unsigned long flags;
1827
Zhu Yib481de92007-09-25 17:54:57 -07001828 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1829 return;
1830
1831 if (!priv->qos_data.qos_enable)
1832 return;
1833
1834 spin_lock_irqsave(&priv->lock, flags);
1835 priv->qos_data.def_qos_parm.qos_flags = 0;
1836
1837 if (priv->qos_data.qos_cap.q_AP.queue_request &&
1838 !priv->qos_data.qos_cap.q_AP.txop_request)
1839 priv->qos_data.def_qos_parm.qos_flags |=
1840 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07001841 if (priv->qos_data.qos_active)
1842 priv->qos_data.def_qos_parm.qos_flags |=
1843 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
1844
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001845#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02001846 if (priv->current_ht_config.is_ht)
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001847 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001848#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001849
Zhu Yib481de92007-09-25 17:54:57 -07001850 spin_unlock_irqrestore(&priv->lock, flags);
1851
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001852 if (force || iwl4965_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08001853 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
1854 priv->qos_data.qos_active,
1855 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07001856
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001857 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07001858 &(priv->qos_data.def_qos_parm));
1859 }
1860}
1861
Zhu Yib481de92007-09-25 17:54:57 -07001862/*
1863 * Power management (not Tx power!) functions
1864 */
1865#define MSEC_TO_USEC 1024
1866
1867#define NOSLP __constant_cpu_to_le16(0), 0, 0
1868#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
1869#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
1870#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
1871 __constant_cpu_to_le32(X1), \
1872 __constant_cpu_to_le32(X2), \
1873 __constant_cpu_to_le32(X3), \
1874 __constant_cpu_to_le32(X4)}
1875
1876
1877/* default power management (not Tx power) table values */
1878/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001879static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001880 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1881 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
1882 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
1883 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
1884 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
1885 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
1886};
1887
1888/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001889static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07001890 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
1891 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
1892 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
1893 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
1894 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
1895 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
1896 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
1897 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
1898 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
1899 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
1900};
1901
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001902int iwl4965_power_init_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001903{
1904 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001905 struct iwl4965_power_mgr *pow_data;
1906 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07001907 u16 pci_pm;
1908
1909 IWL_DEBUG_POWER("Initialize power \n");
1910
1911 pow_data = &(priv->power_data);
1912
1913 memset(pow_data, 0, sizeof(*pow_data));
1914
1915 pow_data->active_index = IWL_POWER_RANGE_0;
1916 pow_data->dtim_val = 0xffff;
1917
1918 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
1919 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
1920
1921 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
1922 if (rc != 0)
1923 return 0;
1924 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001925 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07001926
1927 IWL_DEBUG_POWER("adjust power command flags\n");
1928
1929 for (i = 0; i < IWL_POWER_AC; i++) {
1930 cmd = &pow_data->pwr_range_0[i].cmd;
1931
1932 if (pci_pm & 0x1)
1933 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
1934 else
1935 cmd->flags |= IWL_POWER_PCI_PM_MSK;
1936 }
1937 }
1938 return rc;
1939}
1940
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07001941static int iwl4965_update_power_cmd(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001942 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07001943{
1944 int rc = 0, i;
1945 u8 skip;
1946 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001947 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07001948 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001949 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07001950
1951 if (mode > IWL_POWER_INDEX_5) {
1952 IWL_DEBUG_POWER("Error invalid power mode \n");
1953 return -1;
1954 }
1955 pow_data = &(priv->power_data);
1956
1957 if (pow_data->active_index == IWL_POWER_RANGE_0)
1958 range = &pow_data->pwr_range_0[0];
1959 else
1960 range = &pow_data->pwr_range_1[1];
1961
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001962 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07001963
1964#ifdef IWL_MAC80211_DISABLE
1965 if (priv->assoc_network != NULL) {
1966 unsigned long flags;
1967
1968 period = priv->assoc_network->tim.tim_period;
1969 }
1970#endif /*IWL_MAC80211_DISABLE */
1971 skip = range[mode].no_dtim;
1972
1973 if (period == 0) {
1974 period = 1;
1975 skip = 0;
1976 }
1977
1978 if (skip == 0) {
1979 max_sleep = period;
1980 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
1981 } else {
1982 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
1983 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
1984 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
1985 }
1986
1987 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
1988 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
1989 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
1990 }
1991
1992 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
1993 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
1994 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
1995 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
1996 le32_to_cpu(cmd->sleep_interval[0]),
1997 le32_to_cpu(cmd->sleep_interval[1]),
1998 le32_to_cpu(cmd->sleep_interval[2]),
1999 le32_to_cpu(cmd->sleep_interval[3]),
2000 le32_to_cpu(cmd->sleep_interval[4]));
2001
2002 return rc;
2003}
2004
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002005static int iwl4965_send_power_mode(struct iwl_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002006{
John W. Linville9a62f732007-11-15 16:27:36 -05002007 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002008 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002009 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002010
2011 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08002012 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07002013 * else user level */
2014 switch (mode) {
2015 case IWL_POWER_BATTERY:
2016 final_mode = IWL_POWER_INDEX_3;
2017 break;
2018 case IWL_POWER_AC:
2019 final_mode = IWL_POWER_MODE_CAM;
2020 break;
2021 default:
2022 final_mode = mode;
2023 break;
2024 }
2025
2026 cmd.keep_alive_beacons = 0;
2027
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002028 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002029
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002030 rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002031
2032 if (final_mode == IWL_POWER_MODE_CAM)
2033 clear_bit(STATUS_POWER_PMI, &priv->status);
2034 else
2035 set_bit(STATUS_POWER_PMI, &priv->status);
2036
2037 return rc;
2038}
2039
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002040int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002041{
2042 /* Filter incoming packets to determine if they are targeted toward
2043 * this network, discarding packets coming from ourselves */
2044 switch (priv->iw_mode) {
2045 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2046 /* packets from our adapter are dropped (echo) */
2047 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2048 return 0;
2049 /* {broad,multi}cast packets to our IBSS go through */
2050 if (is_multicast_ether_addr(header->addr1))
2051 return !compare_ether_addr(header->addr3, priv->bssid);
2052 /* packets to our adapter go through */
2053 return !compare_ether_addr(header->addr1, priv->mac_addr);
2054 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2055 /* packets from our adapter are dropped (echo) */
2056 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2057 return 0;
2058 /* {broad,multi}cast packets to our BSS go through */
2059 if (is_multicast_ether_addr(header->addr1))
2060 return !compare_ether_addr(header->addr2, priv->bssid);
2061 /* packets to our adapter go through */
2062 return !compare_ether_addr(header->addr1, priv->mac_addr);
2063 }
2064
2065 return 1;
2066}
2067
2068#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2069
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002070static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002071{
2072 switch (status & TX_STATUS_MSK) {
2073 case TX_STATUS_SUCCESS:
2074 return "SUCCESS";
2075 TX_STATUS_ENTRY(SHORT_LIMIT);
2076 TX_STATUS_ENTRY(LONG_LIMIT);
2077 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2078 TX_STATUS_ENTRY(MGMNT_ABORT);
2079 TX_STATUS_ENTRY(NEXT_FRAG);
2080 TX_STATUS_ENTRY(LIFE_EXPIRE);
2081 TX_STATUS_ENTRY(DEST_PS);
2082 TX_STATUS_ENTRY(ABORTED);
2083 TX_STATUS_ENTRY(BT_RETRY);
2084 TX_STATUS_ENTRY(STA_INVALID);
2085 TX_STATUS_ENTRY(FRAG_DROPPED);
2086 TX_STATUS_ENTRY(TID_DISABLE);
2087 TX_STATUS_ENTRY(FRAME_FLUSHED);
2088 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2089 TX_STATUS_ENTRY(TX_LOCKED);
2090 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2091 }
2092
2093 return "UNKNOWN";
2094}
2095
2096/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002097 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002098 *
2099 * NOTE: priv->mutex is not required before calling this function
2100 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002101static int iwl4965_scan_cancel(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002102{
2103 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2104 clear_bit(STATUS_SCANNING, &priv->status);
2105 return 0;
2106 }
2107
2108 if (test_bit(STATUS_SCANNING, &priv->status)) {
2109 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2110 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2111 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2112 queue_work(priv->workqueue, &priv->abort_scan);
2113
2114 } else
2115 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2116
2117 return test_bit(STATUS_SCANNING, &priv->status);
2118 }
2119
2120 return 0;
2121}
2122
2123/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002124 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002125 * @ms: amount of time to wait (in milliseconds) for scan to abort
2126 *
2127 * NOTE: priv->mutex must be held before calling this function
2128 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002129static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07002130{
2131 unsigned long now = jiffies;
2132 int ret;
2133
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002134 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002135 if (ret && ms) {
2136 mutex_unlock(&priv->mutex);
2137 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2138 test_bit(STATUS_SCANNING, &priv->status))
2139 msleep(1);
2140 mutex_lock(&priv->mutex);
2141
2142 return test_bit(STATUS_SCANNING, &priv->status);
2143 }
2144
2145 return ret;
2146}
2147
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002148static void iwl4965_sequence_reset(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002149{
2150 /* Reset ieee stats */
2151
2152 /* We don't reset the net_device_stats (ieee->stats) on
2153 * re-association */
2154
2155 priv->last_seq_num = -1;
2156 priv->last_frag_num = -1;
2157 priv->last_packet_time = 0;
2158
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002159 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002160}
2161
2162#define MAX_UCODE_BEACON_INTERVAL 4096
2163#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2164
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002165static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002166{
2167 u16 new_val = 0;
2168 u16 beacon_factor = 0;
2169
2170 beacon_factor =
2171 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2172 / MAX_UCODE_BEACON_INTERVAL;
2173 new_val = beacon_val / beacon_factor;
2174
2175 return cpu_to_le16(new_val);
2176}
2177
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002178static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002179{
2180 u64 interval_tm_unit;
2181 u64 tsf, result;
2182 unsigned long flags;
2183 struct ieee80211_conf *conf = NULL;
2184 u16 beacon_int = 0;
2185
2186 conf = ieee80211_get_hw_conf(priv->hw);
2187
2188 spin_lock_irqsave(&priv->lock, flags);
2189 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2190 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2191
2192 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2193
2194 tsf = priv->timestamp1;
2195 tsf = ((tsf << 32) | priv->timestamp0);
2196
2197 beacon_int = priv->beacon_int;
2198 spin_unlock_irqrestore(&priv->lock, flags);
2199
2200 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2201 if (beacon_int == 0) {
2202 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2203 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2204 } else {
2205 priv->rxon_timing.beacon_interval =
2206 cpu_to_le16(beacon_int);
2207 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002208 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002209 le16_to_cpu(priv->rxon_timing.beacon_interval));
2210 }
2211
2212 priv->rxon_timing.atim_window = 0;
2213 } else {
2214 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002215 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002216 /* TODO: we need to get atim_window from upper stack
2217 * for now we set to 0 */
2218 priv->rxon_timing.atim_window = 0;
2219 }
2220
2221 interval_tm_unit =
2222 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2223 result = do_div(tsf, interval_tm_unit);
2224 priv->rxon_timing.beacon_init_val =
2225 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2226
2227 IWL_DEBUG_ASSOC
2228 ("beacon interval %d beacon timer %d beacon tim %d\n",
2229 le16_to_cpu(priv->rxon_timing.beacon_interval),
2230 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2231 le16_to_cpu(priv->rxon_timing.atim_window));
2232}
2233
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002234static int iwl4965_scan_initiate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002235{
2236 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2237 IWL_ERROR("APs don't scan.\n");
2238 return 0;
2239 }
2240
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002241 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002242 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2243 return -EIO;
2244 }
2245
2246 if (test_bit(STATUS_SCANNING, &priv->status)) {
2247 IWL_DEBUG_SCAN("Scan already in progress.\n");
2248 return -EAGAIN;
2249 }
2250
2251 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2252 IWL_DEBUG_SCAN("Scan request while abort pending. "
2253 "Queuing.\n");
2254 return -EAGAIN;
2255 }
2256
2257 IWL_DEBUG_INFO("Starting scan...\n");
2258 priv->scan_bands = 2;
2259 set_bit(STATUS_SCANNING, &priv->status);
2260 priv->scan_start = jiffies;
2261 priv->scan_pass_start = priv->scan_start;
2262
2263 queue_work(priv->workqueue, &priv->request_scan);
2264
2265 return 0;
2266}
2267
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002268static int iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002269{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002270 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002271
2272 if (hw_decrypt)
2273 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2274 else
2275 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2276
2277 return 0;
2278}
2279
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002280static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002281 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07002282{
Johannes Berg8318d782008-01-24 19:38:38 +01002283 if (band == IEEE80211_BAND_5GHZ) {
Zhu Yib481de92007-09-25 17:54:57 -07002284 priv->staging_rxon.flags &=
2285 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2286 | RXON_FLG_CCK_MSK);
2287 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2288 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002289 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002290 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2291 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2292 else
2293 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2294
2295 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2296 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2297
2298 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2299 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2300 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2301 }
2302}
2303
2304/*
Ian Schram01ebd062007-10-25 17:15:22 +08002305 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002306 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002307static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002308{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002309 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002310
2311 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2312
2313 switch (priv->iw_mode) {
2314 case IEEE80211_IF_TYPE_AP:
2315 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2316 break;
2317
2318 case IEEE80211_IF_TYPE_STA:
2319 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2320 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2321 break;
2322
2323 case IEEE80211_IF_TYPE_IBSS:
2324 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2325 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2326 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2327 RXON_FILTER_ACCEPT_GRP_MSK;
2328 break;
2329
2330 case IEEE80211_IF_TYPE_MNTR:
2331 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2332 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2333 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2334 break;
2335 }
2336
2337#if 0
2338 /* TODO: Figure out when short_preamble would be set and cache from
2339 * that */
2340 if (!hw_to_local(priv->hw)->short_preamble)
2341 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2342 else
2343 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2344#endif
2345
Johannes Berg8318d782008-01-24 19:38:38 +01002346 ch_info = iwl4965_get_channel_info(priv, priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002347 le16_to_cpu(priv->staging_rxon.channel));
2348
2349 if (!ch_info)
2350 ch_info = &priv->channel_info[0];
2351
2352 /*
2353 * in some case A channels are all non IBSS
2354 * in this case force B/G channel
2355 */
2356 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2357 !(is_channel_ibss(ch_info)))
2358 ch_info = &priv->channel_info[0];
2359
2360 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01002361 priv->band = ch_info->band;
Zhu Yib481de92007-09-25 17:54:57 -07002362
Johannes Berg8318d782008-01-24 19:38:38 +01002363 iwl4965_set_flags_for_phymode(priv, priv->band);
Zhu Yib481de92007-09-25 17:54:57 -07002364
2365 priv->staging_rxon.ofdm_basic_rates =
2366 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2367 priv->staging_rxon.cck_basic_rates =
2368 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2369
2370 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2371 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2372 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2373 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2374 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2375 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2376 iwl4965_set_rxon_chain(priv);
2377}
2378
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002379static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002380{
Zhu Yib481de92007-09-25 17:54:57 -07002381 if (mode == IEEE80211_IF_TYPE_IBSS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002382 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002383
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002384 ch_info = iwl4965_get_channel_info(priv,
Johannes Berg8318d782008-01-24 19:38:38 +01002385 priv->band,
Zhu Yib481de92007-09-25 17:54:57 -07002386 le16_to_cpu(priv->staging_rxon.channel));
2387
2388 if (!ch_info || !is_channel_ibss(ch_info)) {
2389 IWL_ERROR("channel %d not IBSS channel\n",
2390 le16_to_cpu(priv->staging_rxon.channel));
2391 return -EINVAL;
2392 }
2393 }
2394
Zhu Yib481de92007-09-25 17:54:57 -07002395 priv->iw_mode = mode;
2396
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002397 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002398 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2399
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002400 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002401
Mohamed Abbasfde35712007-11-29 11:10:15 +08002402 /* dont commit rxon if rf-kill is on*/
2403 if (!iwl4965_is_ready_rf(priv))
2404 return -EAGAIN;
2405
2406 cancel_delayed_work(&priv->scan_check);
2407 if (iwl4965_scan_cancel_timeout(priv, 100)) {
2408 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2409 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2410 return -EAGAIN;
2411 }
2412
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002413 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002414
2415 return 0;
2416}
2417
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002418static void iwl4965_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002419 struct ieee80211_tx_control *ctl,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002420 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002421 struct sk_buff *skb_frag,
2422 int last_frag)
2423{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002424 struct iwl4965_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002425
2426 switch (keyinfo->alg) {
2427 case ALG_CCMP:
2428 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2429 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
Max Stepanov8236e182008-03-12 16:58:48 -07002430 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
2431 cmd->cmd.tx.tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002432 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2433 break;
2434
2435 case ALG_TKIP:
2436#if 0
2437 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2438
2439 if (last_frag)
2440 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2441 8);
2442 else
2443 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2444#endif
2445 break;
2446
2447 case ALG_WEP:
2448 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2449 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2450
2451 if (keyinfo->keylen == 13)
2452 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2453
2454 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2455
2456 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2457 "with key %d\n", ctl->key_idx);
2458 break;
2459
Zhu Yib481de92007-09-25 17:54:57 -07002460 default:
2461 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2462 break;
2463 }
2464}
2465
2466/*
2467 * handle build REPLY_TX command notification.
2468 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002469static void iwl4965_build_tx_cmd_basic(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002470 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002471 struct ieee80211_tx_control *ctrl,
2472 struct ieee80211_hdr *hdr,
2473 int is_unicast, u8 std_id)
2474{
2475 __le16 *qc;
2476 u16 fc = le16_to_cpu(hdr->frame_control);
2477 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2478
2479 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2480 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2481 tx_flags |= TX_CMD_FLG_ACK_MSK;
2482 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2483 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2484 if (ieee80211_is_probe_response(fc) &&
2485 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2486 tx_flags |= TX_CMD_FLG_TSF_MSK;
2487 } else {
2488 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2489 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2490 }
2491
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002492 if (ieee80211_is_back_request(fc))
2493 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2494
2495
Zhu Yib481de92007-09-25 17:54:57 -07002496 cmd->cmd.tx.sta_id = std_id;
2497 if (ieee80211_get_morefrag(hdr))
2498 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2499
2500 qc = ieee80211_get_qos_ctrl(hdr);
2501 if (qc) {
2502 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2503 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2504 } else
2505 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2506
2507 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2508 tx_flags |= TX_CMD_FLG_RTS_MSK;
2509 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2510 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2511 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2512 tx_flags |= TX_CMD_FLG_CTS_MSK;
2513 }
2514
2515 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2516 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2517
2518 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2519 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2520 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2521 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002522 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002523 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002524 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Zhu Yib481de92007-09-25 17:54:57 -07002525 } else
2526 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2527
2528 cmd->cmd.tx.driver_txop = 0;
2529 cmd->cmd.tx.tx_flags = tx_flags;
2530 cmd->cmd.tx.next_frame_len = 0;
2531}
Tomas Winkler19758be2008-03-12 16:58:51 -07002532static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
2533{
2534 /* 0 - mgmt, 1 - cnt, 2 - data */
2535 int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
2536 priv->tx_stats[idx].cnt++;
2537 priv->tx_stats[idx].bytes += len;
2538}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002539/**
2540 * iwl4965_get_sta_id - Find station's index within station table
2541 *
2542 * If new IBSS station, create new entry in station table
2543 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002544static int iwl4965_get_sta_id(struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08002545 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002546{
2547 int sta_id;
2548 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002549 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002550
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002551 /* If this frame is broadcast or management, use broadcast station id */
Zhu Yib481de92007-09-25 17:54:57 -07002552 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2553 is_multicast_ether_addr(hdr->addr1))
2554 return priv->hw_setting.bcast_sta_id;
2555
2556 switch (priv->iw_mode) {
2557
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002558 /* If we are a client station in a BSS network, use the special
2559 * AP station entry (that's the only station we communicate with) */
Zhu Yib481de92007-09-25 17:54:57 -07002560 case IEEE80211_IF_TYPE_STA:
2561 return IWL_AP_ID;
2562
2563 /* If we are an AP, then find the station, or use BCAST */
2564 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002565 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002566 if (sta_id != IWL_INVALID_STATION)
2567 return sta_id;
2568 return priv->hw_setting.bcast_sta_id;
2569
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002570 /* If this frame is going out to an IBSS network, find the station,
2571 * or create a new station table entry */
Zhu Yib481de92007-09-25 17:54:57 -07002572 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002573 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002574 if (sta_id != IWL_INVALID_STATION)
2575 return sta_id;
2576
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002577 /* Create new station table entry */
Ron Rindjunsky67d62032007-11-26 16:14:40 +02002578 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2579 0, CMD_ASYNC, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07002580
2581 if (sta_id != IWL_INVALID_STATION)
2582 return sta_id;
2583
Joe Perches0795af52007-10-03 17:59:30 -07002584 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002585 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002586 print_mac(mac, hdr->addr1));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002587 iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002588 return priv->hw_setting.bcast_sta_id;
2589
2590 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002591 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002592 return priv->hw_setting.bcast_sta_id;
2593 }
2594}
2595
2596/*
2597 * start REPLY_TX command process
2598 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002599static int iwl4965_tx_skb(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002600 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2601{
2602 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002603 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002604 u32 *control_flags;
2605 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002606 struct iwl4965_tx_queue *txq = NULL;
2607 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002608 dma_addr_t phys_addr;
2609 dma_addr_t txcmd_phys;
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002610 dma_addr_t scratch_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002611 struct iwl4965_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002612 u16 len, idx, len_org;
2613 u8 id, hdr_len, unicast;
2614 u8 sta_id;
2615 u16 seq_number = 0;
2616 u16 fc;
2617 __le16 *qc;
2618 u8 wait_write_ptr = 0;
2619 unsigned long flags;
2620 int rc;
2621
2622 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002623 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002624 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2625 goto drop_unlock;
2626 }
2627
Johannes Berg32bfd352007-12-19 01:31:26 +01002628 if (!priv->vif) {
2629 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
Zhu Yib481de92007-09-25 17:54:57 -07002630 goto drop_unlock;
2631 }
2632
Johannes Berg8318d782008-01-24 19:38:38 +01002633 if ((ctl->tx_rate->hw_value & 0xFF) == IWL_INVALID_RATE) {
Zhu Yib481de92007-09-25 17:54:57 -07002634 IWL_ERROR("ERROR: No TX rate available.\n");
2635 goto drop_unlock;
2636 }
2637
2638 unicast = !is_multicast_ether_addr(hdr->addr1);
2639 id = 0;
2640
2641 fc = le16_to_cpu(hdr->frame_control);
2642
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002643#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002644 if (ieee80211_is_auth(fc))
2645 IWL_DEBUG_TX("Sending AUTH frame\n");
2646 else if (ieee80211_is_assoc_request(fc))
2647 IWL_DEBUG_TX("Sending ASSOC frame\n");
2648 else if (ieee80211_is_reassoc_request(fc))
2649 IWL_DEBUG_TX("Sending REASSOC frame\n");
2650#endif
2651
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08002652 /* drop all data frame if we are not associated */
Gregory Greenman76f39152008-01-23 10:15:21 -08002653 if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2654 (!iwl4965_is_associated(priv) ||
Reinette Chatrea6477242008-02-14 10:40:28 -08002655 ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
Gregory Greenman76f39152008-01-23 10:15:21 -08002656 !priv->assoc_station_added)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002657 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002658 goto drop_unlock;
2659 }
2660
2661 spin_unlock_irqrestore(&priv->lock, flags);
2662
2663 hdr_len = ieee80211_get_hdrlen(fc);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002664
2665 /* Find (or create) index into station table for destination station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002666 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002667 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002668 DECLARE_MAC_BUF(mac);
2669
2670 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2671 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002672 goto drop;
2673 }
2674
2675 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2676
2677 qc = ieee80211_get_qos_ctrl(hdr);
2678 if (qc) {
2679 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2680 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2681 IEEE80211_SCTL_SEQ;
2682 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2683 (hdr->seq_ctrl &
2684 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2685 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002686#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07002687 /* aggregation is on for this <sta,tid> */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002688 if (ctl->flags & IEEE80211_TXCTL_AMPDU)
Zhu Yib481de92007-09-25 17:54:57 -07002689 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02002690 priv->stations[sta_id].tid[tid].tfds_in_queue++;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002691#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002692 }
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002693
2694 /* Descriptor for chosen Tx queue */
Zhu Yib481de92007-09-25 17:54:57 -07002695 txq = &priv->txq[txq_id];
2696 q = &txq->q;
2697
2698 spin_lock_irqsave(&priv->lock, flags);
2699
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002700 /* Set up first empty TFD within this queue's circular TFD buffer */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002701 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002702 memset(tfd, 0, sizeof(*tfd));
2703 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002704 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002705
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002706 /* Set up driver data for this TFD */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002707 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002708 txq->txb[q->write_ptr].skb[0] = skb;
2709 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002710 ctl, sizeof(struct ieee80211_tx_control));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002711
2712 /* Set up first empty entry in queue's array of Tx/cmd buffers */
Zhu Yib481de92007-09-25 17:54:57 -07002713 out_cmd = &txq->cmd[idx];
2714 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2715 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002716
2717 /*
2718 * Set up the Tx-command (not MAC!) header.
2719 * Store the chosen Tx queue and TFD index within the sequence field;
2720 * after Tx, uCode's Tx response will return this value so driver can
2721 * locate the frame within the tx queue and do post-tx processing.
2722 */
Zhu Yib481de92007-09-25 17:54:57 -07002723 out_cmd->hdr.cmd = REPLY_TX;
2724 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002725 INDEX_TO_SEQ(q->write_ptr)));
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002726
2727 /* Copy MAC header from skb into command buffer */
Zhu Yib481de92007-09-25 17:54:57 -07002728 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2729
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002730 /*
2731 * Use the first empty entry in this queue's command buffer array
2732 * to contain the Tx command and MAC header concatenated together
2733 * (payload data will be in another buffer).
2734 * Size of this varies, due to varying MAC header length.
2735 * If end is not dword aligned, we'll have 2 extra bytes at the end
2736 * of the MAC header (device reads on dword boundaries).
2737 * We'll tell device about this padding later.
2738 */
Zhu Yib481de92007-09-25 17:54:57 -07002739 len = priv->hw_setting.tx_cmd_len +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002740 sizeof(struct iwl4965_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002741
2742 len_org = len;
2743 len = (len + 3) & ~3;
2744
2745 if (len_org != len)
2746 len_org = 1;
2747 else
2748 len_org = 0;
2749
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002750 /* Physical address of this Tx command's header (not MAC header!),
2751 * within command buffer array. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002752 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
2753 offsetof(struct iwl4965_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002754
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002755 /* Add buffer containing Tx command and MAC(!) header to TFD's
2756 * first entry */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002757 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002758
2759 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002760 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002761
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002762 /* Set up TFD's 2nd entry to point directly to remainder of skb,
2763 * if any (802.11 null frames have no payload). */
Zhu Yib481de92007-09-25 17:54:57 -07002764 len = skb->len - hdr_len;
2765 if (len) {
2766 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2767 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002768 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002769 }
2770
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002771 /* Tell 4965 about any 2-byte padding after MAC header */
Zhu Yib481de92007-09-25 17:54:57 -07002772 if (len_org)
2773 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2774
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002775 /* Total # bytes to be transmitted */
Zhu Yib481de92007-09-25 17:54:57 -07002776 len = (u16)skb->len;
2777 out_cmd->cmd.tx.len = cpu_to_le16(len);
2778
2779 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002780 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002781
2782 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002783 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002784
Tomas Winkler19758be2008-03-12 16:58:51 -07002785 iwl_update_tx_stats(priv, fc, len);
2786
Tomas Winkler87e4f7d2008-01-14 17:46:16 -08002787 scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
2788 offsetof(struct iwl4965_tx_cmd, scratch);
2789 out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
2790 out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
2791
Zhu Yib481de92007-09-25 17:54:57 -07002792 if (!ieee80211_get_morefrag(hdr)) {
2793 txq->need_update = 1;
2794 if (qc) {
2795 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2796 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2797 }
2798 } else {
2799 wait_write_ptr = 1;
2800 txq->need_update = 0;
2801 }
2802
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002803 iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002804 sizeof(out_cmd->cmd.tx));
2805
Tomas Winkler0a6857e2008-03-12 16:58:49 -07002806 iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002807 ieee80211_get_hdrlen(fc));
2808
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002809 /* Set up entry for this TFD in Tx byte-count array */
Zhu Yib481de92007-09-25 17:54:57 -07002810 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2811
Cahill, Ben M6440adb2007-11-29 11:09:55 +08002812 /* Tell device the write index *just past* this latest filled TFD */
Tomas Winklerc54b6792008-03-06 17:36:53 -08002813 q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002814 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002815 spin_unlock_irqrestore(&priv->lock, flags);
2816
2817 if (rc)
2818 return rc;
2819
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002820 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002821 && priv->mac80211_registered) {
2822 if (wait_write_ptr) {
2823 spin_lock_irqsave(&priv->lock, flags);
2824 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002825 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002826 spin_unlock_irqrestore(&priv->lock, flags);
2827 }
2828
2829 ieee80211_stop_queue(priv->hw, ctl->queue);
2830 }
2831
2832 return 0;
2833
2834drop_unlock:
2835 spin_unlock_irqrestore(&priv->lock, flags);
2836drop:
2837 return -1;
2838}
2839
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002840static void iwl4965_set_rate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002841{
Johannes Berg8318d782008-01-24 19:38:38 +01002842 const struct ieee80211_supported_band *hw = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002843 struct ieee80211_rate *rate;
2844 int i;
2845
Johannes Berg8318d782008-01-24 19:38:38 +01002846 hw = iwl4965_get_hw_mode(priv, priv->band);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002847 if (!hw) {
2848 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2849 return;
2850 }
Zhu Yib481de92007-09-25 17:54:57 -07002851
2852 priv->active_rate = 0;
2853 priv->active_rate_basic = 0;
2854
Johannes Berg8318d782008-01-24 19:38:38 +01002855 for (i = 0; i < hw->n_bitrates; i++) {
2856 rate = &(hw->bitrates[i]);
2857 if (rate->hw_value < IWL_RATE_COUNT)
2858 priv->active_rate |= (1 << rate->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07002859 }
2860
2861 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
2862 priv->active_rate, priv->active_rate_basic);
2863
2864 /*
2865 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
2866 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
2867 * OFDM
2868 */
2869 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
2870 priv->staging_rxon.cck_basic_rates =
2871 ((priv->active_rate_basic &
2872 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
2873 else
2874 priv->staging_rxon.cck_basic_rates =
2875 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2876
2877 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
2878 priv->staging_rxon.ofdm_basic_rates =
2879 ((priv->active_rate_basic &
2880 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
2881 IWL_FIRST_OFDM_RATE) & 0xFF;
2882 else
2883 priv->staging_rxon.ofdm_basic_rates =
2884 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2885}
2886
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002887static void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07002888{
2889 unsigned long flags;
2890
2891 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
2892 return;
2893
2894 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
2895 disable_radio ? "OFF" : "ON");
2896
2897 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002898 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002899 /* FIXME: This is a workaround for AP */
2900 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
2901 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002902 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07002903 CSR_UCODE_SW_BIT_RFKILL);
2904 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002905 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002906 set_bit(STATUS_RF_KILL_SW, &priv->status);
2907 }
2908 return;
2909 }
2910
2911 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002912 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07002913
2914 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2915 spin_unlock_irqrestore(&priv->lock, flags);
2916
2917 /* wake up ucode */
2918 msleep(10);
2919
2920 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002921 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
2922 if (!iwl4965_grab_nic_access(priv))
2923 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002924 spin_unlock_irqrestore(&priv->lock, flags);
2925
2926 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
2927 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2928 "disabled by HW switch\n");
2929 return;
2930 }
2931
2932 queue_work(priv->workqueue, &priv->restart);
2933 return;
2934}
2935
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002936void iwl4965_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07002937 u32 decrypt_res, struct ieee80211_rx_status *stats)
2938{
2939 u16 fc =
2940 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
2941
2942 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
2943 return;
2944
2945 if (!(fc & IEEE80211_FCTL_PROTECTED))
2946 return;
2947
2948 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
2949 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
2950 case RX_RES_STATUS_SEC_TYPE_TKIP:
2951 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2952 RX_RES_STATUS_BAD_ICV_MIC)
2953 stats->flag |= RX_FLAG_MMIC_ERROR;
2954 case RX_RES_STATUS_SEC_TYPE_WEP:
2955 case RX_RES_STATUS_SEC_TYPE_CCMP:
2956 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
2957 RX_RES_STATUS_DECRYPT_OK) {
2958 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
2959 stats->flag |= RX_FLAG_DECRYPTED;
2960 }
2961 break;
2962
2963 default:
2964 break;
2965 }
2966}
2967
Zhu Yib481de92007-09-25 17:54:57 -07002968
2969#define IWL_PACKET_RETRY_TIME HZ
2970
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07002971int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002972{
2973 u16 sc = le16_to_cpu(header->seq_ctrl);
2974 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
2975 u16 frag = sc & IEEE80211_SCTL_FRAG;
2976 u16 *last_seq, *last_frag;
2977 unsigned long *last_time;
2978
2979 switch (priv->iw_mode) {
2980 case IEEE80211_IF_TYPE_IBSS:{
2981 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002982 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002983 u8 *mac = header->addr2;
2984 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
2985
2986 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002987 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07002988 if (!compare_ether_addr(entry->mac, mac))
2989 break;
2990 }
2991 if (p == &priv->ibss_mac_hash[index]) {
2992 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
2993 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08002994 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07002995 return 0;
2996 }
2997 memcpy(entry->mac, mac, ETH_ALEN);
2998 entry->seq_num = seq;
2999 entry->frag_num = frag;
3000 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08003001 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07003002 return 0;
3003 }
3004 last_seq = &entry->seq_num;
3005 last_frag = &entry->frag_num;
3006 last_time = &entry->packet_time;
3007 break;
3008 }
3009 case IEEE80211_IF_TYPE_STA:
3010 last_seq = &priv->last_seq_num;
3011 last_frag = &priv->last_frag_num;
3012 last_time = &priv->last_packet_time;
3013 break;
3014 default:
3015 return 0;
3016 }
3017 if ((*last_seq == seq) &&
3018 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3019 if (*last_frag == frag)
3020 goto drop;
3021 if (*last_frag + 1 != frag)
3022 /* out-of-order fragment */
3023 goto drop;
3024 } else
3025 *last_seq = seq;
3026
3027 *last_frag = frag;
3028 *last_time = jiffies;
3029 return 0;
3030
3031 drop:
3032 return 1;
3033}
3034
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003035#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003036
3037#include "iwl-spectrum.h"
3038
3039#define BEACON_TIME_MASK_LOW 0x00FFFFFF
3040#define BEACON_TIME_MASK_HIGH 0xFF000000
3041#define TIME_UNIT 1024
3042
3043/*
3044 * extended beacon time format
3045 * time in usec will be changed into a 32-bit value in 8:24 format
3046 * the high 1 byte is the beacon counts
3047 * the lower 3 bytes is the time in usec within one beacon interval
3048 */
3049
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003050static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003051{
3052 u32 quot;
3053 u32 rem;
3054 u32 interval = beacon_interval * 1024;
3055
3056 if (!interval || !usec)
3057 return 0;
3058
3059 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3060 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3061
3062 return (quot << 24) + rem;
3063}
3064
3065/* base is usually what we get from ucode with each received frame,
3066 * the same as HW timer counter counting down
3067 */
3068
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003069static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003070{
3071 u32 base_low = base & BEACON_TIME_MASK_LOW;
3072 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3073 u32 interval = beacon_interval * TIME_UNIT;
3074 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3075 (addon & BEACON_TIME_MASK_HIGH);
3076
3077 if (base_low > addon_low)
3078 res += base_low - addon_low;
3079 else if (base_low < addon_low) {
3080 res += interval + base_low - addon_low;
3081 res += (1 << 24);
3082 } else
3083 res += (1 << 24);
3084
3085 return cpu_to_le32(res);
3086}
3087
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003088static int iwl4965_get_measurement(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003089 struct ieee80211_measurement_params *params,
3090 u8 type)
3091{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003092 struct iwl4965_spectrum_cmd spectrum;
3093 struct iwl4965_rx_packet *res;
3094 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003095 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3096 .data = (void *)&spectrum,
3097 .meta.flags = CMD_WANT_SKB,
3098 };
3099 u32 add_time = le64_to_cpu(params->start_time);
3100 int rc;
3101 int spectrum_resp_status;
3102 int duration = le16_to_cpu(params->duration);
3103
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003104 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003105 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003106 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07003107 le64_to_cpu(params->start_time) - priv->last_tsf,
3108 le16_to_cpu(priv->rxon_timing.beacon_interval));
3109
3110 memset(&spectrum, 0, sizeof(spectrum));
3111
3112 spectrum.channel_count = cpu_to_le16(1);
3113 spectrum.flags =
3114 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3115 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3116 cmd.len = sizeof(spectrum);
3117 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003119 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003120 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003121 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07003122 add_time,
3123 le16_to_cpu(priv->rxon_timing.beacon_interval));
3124 else
3125 spectrum.start_time = 0;
3126
3127 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3128 spectrum.channels[0].channel = params->channel;
3129 spectrum.channels[0].type = type;
3130 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3131 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3132 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3133
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003134 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003135 if (rc)
3136 return rc;
3137
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003138 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003139 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3140 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3141 rc = -EIO;
3142 }
3143
3144 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3145 switch (spectrum_resp_status) {
3146 case 0: /* Command will be handled */
3147 if (res->u.spectrum.id != 0xff) {
3148 IWL_DEBUG_INFO
3149 ("Replaced existing measurement: %d\n",
3150 res->u.spectrum.id);
3151 priv->measurement_status &= ~MEASUREMENT_READY;
3152 }
3153 priv->measurement_status |= MEASUREMENT_ACTIVE;
3154 rc = 0;
3155 break;
3156
3157 case 1: /* Command will not be handled */
3158 rc = -EAGAIN;
3159 break;
3160 }
3161
3162 dev_kfree_skb_any(cmd.meta.u.skb);
3163
3164 return rc;
3165}
3166#endif
3167
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003168static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003169 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003170{
3171
3172 tx_sta->status.ack_signal = 0;
3173 tx_sta->status.excessive_retries = 0;
3174 tx_sta->status.queue_length = 0;
3175 tx_sta->status.queue_number = 0;
3176
3177 if (in_interrupt())
3178 ieee80211_tx_status_irqsafe(priv->hw,
3179 tx_sta->skb[0], &(tx_sta->status));
3180 else
3181 ieee80211_tx_status(priv->hw,
3182 tx_sta->skb[0], &(tx_sta->status));
3183
3184 tx_sta->skb[0] = NULL;
3185}
3186
3187/**
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003188 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
Zhu Yib481de92007-09-25 17:54:57 -07003189 *
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003190 * When FW advances 'R' index, all entries between old and new 'R' index
3191 * need to be reclaimed. As result, some free space forms. If there is
3192 * enough free space (> low mark), wake the stack that feeds us.
Zhu Yib481de92007-09-25 17:54:57 -07003193 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003194int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07003195{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003196 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
3197 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07003198 int nfreed = 0;
3199
3200 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3201 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3202 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003203 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003204 return 0;
3205 }
3206
Tomas Winklerc54b6792008-03-06 17:36:53 -08003207 for (index = iwl_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003208 q->read_ptr != index;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003209 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07003210 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003211 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003212 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003213 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003214 } else if (nfreed > 1) {
3215 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003216 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003217 queue_work(priv->workqueue, &priv->restart);
3218 }
3219 nfreed++;
3220 }
3221
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003222/* if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07003223 (txq_id != IWL_CMD_QUEUE_NUM) &&
3224 priv->mac80211_registered)
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003225 ieee80211_wake_queue(priv->hw, txq_id); */
Zhu Yib481de92007-09-25 17:54:57 -07003226
3227
3228 return nfreed;
3229}
3230
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003231static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07003232{
3233 status &= TX_STATUS_MSK;
3234 return (status == TX_STATUS_SUCCESS)
3235 || (status == TX_STATUS_DIRECT_DONE);
3236}
3237
3238/******************************************************************************
3239 *
3240 * Generic RX handler implementations
3241 *
3242 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003243#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07003244
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003245static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003246 struct ieee80211_hdr *hdr)
3247{
3248 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3249 return IWL_AP_ID;
3250 else {
3251 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003252 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07003253 }
3254}
3255
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003256static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003257 struct iwl_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07003258{
3259 if (priv->txq[txq_id].txb[idx].skb[0])
3260 return (struct ieee80211_hdr *)priv->txq[txq_id].
3261 txb[idx].skb[0]->data;
3262 return NULL;
3263}
3264
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003265static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07003266{
3267 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3268 tx_resp->frame_count);
3269 return le32_to_cpu(*scd_ssn) & MAX_SN;
3270
3271}
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003272
3273/**
3274 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
3275 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003276static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003277 struct iwl4965_ht_agg *agg,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003278 struct iwl4965_tx_resp_agg *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003279 u16 start_idx)
3280{
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003281 u16 status;
3282 struct agg_tx_status *frame_status = &tx_resp->status;
Zhu Yib481de92007-09-25 17:54:57 -07003283 struct ieee80211_tx_status *tx_status = NULL;
3284 struct ieee80211_hdr *hdr = NULL;
3285 int i, sh;
3286 int txq_id, idx;
3287 u16 seq;
3288
3289 if (agg->wait_for_ba)
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003290 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
Zhu Yib481de92007-09-25 17:54:57 -07003291
3292 agg->frame_count = tx_resp->frame_count;
3293 agg->start_idx = start_idx;
3294 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003295 agg->bitmap = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003296
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003297 /* # frames attempted by Tx command */
Zhu Yib481de92007-09-25 17:54:57 -07003298 if (agg->frame_count == 1) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003299 /* Only one frame was attempted; no block-ack will arrive */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003300 status = le16_to_cpu(frame_status[0].status);
3301 seq = le16_to_cpu(frame_status[0].sequence);
3302 idx = SEQ_TO_INDEX(seq);
3303 txq_id = SEQ_TO_QUEUE(seq);
Zhu Yib481de92007-09-25 17:54:57 -07003304
Zhu Yib481de92007-09-25 17:54:57 -07003305 /* FIXME: code repetition */
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003306 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
3307 agg->frame_count, agg->start_idx, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003308
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003309 tx_status = &(priv->txq[txq_id].txb[idx].status);
Zhu Yib481de92007-09-25 17:54:57 -07003310 tx_status->retry_count = tx_resp->failure_frame;
3311 tx_status->queue_number = status & 0xff;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003312 tx_status->queue_length = tx_resp->failure_rts;
3313 tx_status->control.flags &= ~IEEE80211_TXCTL_AMPDU;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003314 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003315 IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003316 iwl4965_hwrate_to_tx_control(priv,
3317 le32_to_cpu(tx_resp->rate_n_flags),
3318 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003319 /* FIXME: code repetition end */
3320
3321 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3322 status & 0xff, tx_resp->failure_frame);
3323 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003324 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003325
3326 agg->wait_for_ba = 0;
3327 } else {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003328 /* Two or more frames were attempted; expect block-ack */
Zhu Yib481de92007-09-25 17:54:57 -07003329 u64 bitmap = 0;
3330 int start = agg->start_idx;
3331
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003332 /* Construct bit-map of pending frames within Tx window */
Zhu Yib481de92007-09-25 17:54:57 -07003333 for (i = 0; i < agg->frame_count; i++) {
3334 u16 sc;
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003335 status = le16_to_cpu(frame_status[i].status);
3336 seq = le16_to_cpu(frame_status[i].sequence);
Zhu Yib481de92007-09-25 17:54:57 -07003337 idx = SEQ_TO_INDEX(seq);
3338 txq_id = SEQ_TO_QUEUE(seq);
3339
3340 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3341 AGG_TX_STATE_ABORT_MSK))
3342 continue;
3343
3344 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3345 agg->frame_count, txq_id, idx);
3346
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003347 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003348
3349 sc = le16_to_cpu(hdr->seq_ctrl);
3350 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3351 IWL_ERROR("BUG_ON idx doesn't match seq control"
3352 " idx=%d, seq_idx=%d, seq=%d\n",
3353 idx, SEQ_TO_SN(sc),
3354 hdr->seq_ctrl);
3355 return -1;
3356 }
3357
3358 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3359 i, idx, SEQ_TO_SN(sc));
3360
3361 sh = idx - start;
3362 if (sh > 64) {
3363 sh = (start - idx) + 0xff;
3364 bitmap = bitmap << sh;
3365 sh = 0;
3366 start = idx;
3367 } else if (sh < -64)
3368 sh = 0xff - (start - idx);
3369 else if (sh < 0) {
3370 sh = start - idx;
3371 start = idx;
3372 bitmap = bitmap << sh;
3373 sh = 0;
3374 }
3375 bitmap |= (1 << sh);
3376 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3377 start, (u32)(bitmap & 0xFFFFFFFF));
3378 }
3379
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003380 agg->bitmap = bitmap;
Zhu Yib481de92007-09-25 17:54:57 -07003381 agg->start_idx = start;
3382 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003383 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
Zhu Yib481de92007-09-25 17:54:57 -07003384 agg->frame_count, agg->start_idx,
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003385 agg->bitmap);
Zhu Yib481de92007-09-25 17:54:57 -07003386
3387 if (bitmap)
3388 agg->wait_for_ba = 1;
3389 }
3390 return 0;
3391}
3392#endif
Zhu Yib481de92007-09-25 17:54:57 -07003393
Cahill, Ben M6440adb2007-11-29 11:09:55 +08003394/**
3395 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
3396 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003397static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003398 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003399{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003400 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003401 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3402 int txq_id = SEQ_TO_QUEUE(sequence);
3403 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003404 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003405 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003406 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003407 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003408#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003409 int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
3410 struct ieee80211_hdr *hdr;
3411 __le16 *qc;
Zhu Yib481de92007-09-25 17:54:57 -07003412#endif
3413
3414 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3415 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3416 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003417 index, txq->q.n_bd, txq->q.write_ptr,
3418 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003419 return;
3420 }
3421
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003422#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003423 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3424 qc = ieee80211_get_qos_ctrl(hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003425
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003426 if (qc)
Zhu Yib481de92007-09-25 17:54:57 -07003427 tid = le16_to_cpu(*qc) & 0xf;
3428
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003429 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
3430 if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
3431 IWL_ERROR("Station not known\n");
3432 return;
3433 }
3434
3435 if (txq->sched_retry) {
3436 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
3437 struct iwl4965_ht_agg *agg = NULL;
3438
3439 if (!qc)
Zhu Yib481de92007-09-25 17:54:57 -07003440 return;
Zhu Yib481de92007-09-25 17:54:57 -07003441
3442 agg = &priv->stations[sta_id].tid[tid].agg;
3443
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003444 iwl4965_tx_status_reply_tx(priv, agg,
3445 (struct iwl4965_tx_resp_agg *)tx_resp, index);
Zhu Yib481de92007-09-25 17:54:57 -07003446
3447 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003448 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003449 /* TODO: send BAR */
3450 }
3451
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003452 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
3453 int freed;
Tomas Winklerc54b6792008-03-06 17:36:53 -08003454 index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003455 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3456 "%d index %d\n", scd_ssn , index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003457 freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
3458 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3459
3460 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3461 txq_id >= 0 && priv->mac80211_registered &&
3462 agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
3463 ieee80211_wake_queue(priv->hw, txq_id);
3464
3465 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -07003466 }
3467 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003468#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003469 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003470
3471 tx_status->retry_count = tx_resp->failure_frame;
3472 tx_status->queue_number = status;
3473 tx_status->queue_length = tx_resp->bt_kill_count;
3474 tx_status->queue_length |= tx_resp->failure_rts;
Zhu Yib481de92007-09-25 17:54:57 -07003475 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003476 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Ron Rindjunsky4c424e42008-03-04 18:09:27 -08003477 iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
3478 &tx_status->control);
Zhu Yib481de92007-09-25 17:54:57 -07003479
Zhu Yib481de92007-09-25 17:54:57 -07003480 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003481 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003482 status, le32_to_cpu(tx_resp->rate_n_flags),
3483 tx_resp->failure_frame);
3484
3485 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003486 if (index != -1) {
3487 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003488#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003489 if (tid != MAX_TID_COUNT)
3490 priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
3491 if (iwl4965_queue_space(&txq->q) > txq->q.low_mark &&
3492 (txq_id >= 0) &&
3493 priv->mac80211_registered)
3494 ieee80211_wake_queue(priv->hw, txq_id);
3495 if (tid != MAX_TID_COUNT)
3496 iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
3497#endif
Zhu Yib481de92007-09-25 17:54:57 -07003498 }
Ron Rindjunskyfe01b472008-01-28 14:07:24 +02003499#ifdef CONFIG_IWL4965_HT
3500 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003501#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003502
3503 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3504 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3505}
3506
3507
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003508static void iwl4965_rx_reply_alive(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003509 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003510{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003511 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3512 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003513 struct delayed_work *pwork;
3514
3515 palive = &pkt->u.alive_frame;
3516
3517 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3518 "0x%01X 0x%01X\n",
3519 palive->is_valid, palive->ver_type,
3520 palive->ver_subtype);
3521
3522 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3523 IWL_DEBUG_INFO("Initialization Alive received.\n");
3524 memcpy(&priv->card_alive_init,
3525 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003526 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003527 pwork = &priv->init_alive_start;
3528 } else {
3529 IWL_DEBUG_INFO("Runtime Alive received.\n");
3530 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003531 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003532 pwork = &priv->alive_start;
3533 }
3534
3535 /* We delay the ALIVE response by 5ms to
3536 * give the HW RF Kill time to activate... */
3537 if (palive->is_valid == UCODE_VALID_OK)
3538 queue_delayed_work(priv->workqueue, pwork,
3539 msecs_to_jiffies(5));
3540 else
3541 IWL_WARNING("uCode did not respond OK.\n");
3542}
3543
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003544static void iwl4965_rx_reply_add_sta(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003545 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003546{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003547 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003548
3549 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3550 return;
3551}
3552
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003553static void iwl4965_rx_reply_error(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003554 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003555{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003556 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003557
3558 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3559 "seq 0x%04X ser 0x%08X\n",
3560 le32_to_cpu(pkt->u.err_resp.error_type),
3561 get_cmd_string(pkt->u.err_resp.cmd_id),
3562 pkt->u.err_resp.cmd_id,
3563 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3564 le32_to_cpu(pkt->u.err_resp.error_info));
3565}
3566
3567#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3568
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003569static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003570{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003571 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3572 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3573 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003574 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3575 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3576 rxon->channel = csa->channel;
3577 priv->staging_rxon.channel = csa->channel;
3578}
3579
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003580static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003581 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003582{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003583#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003584 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3585 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003586
3587 if (!report->state) {
3588 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3589 "Spectrum Measure Notification: Start\n");
3590 return;
3591 }
3592
3593 memcpy(&priv->measure_report, report, sizeof(*report));
3594 priv->measurement_status |= MEASUREMENT_READY;
3595#endif
3596}
3597
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003598static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003599 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003600{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003601#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003602 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3603 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003604 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3605 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3606#endif
3607}
3608
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003609static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003610 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003611{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003612 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003613 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3614 "notification for %s:\n",
3615 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003616 iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003617}
3618
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003619static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003620{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003621 struct iwl_priv *priv =
3622 container_of(work, struct iwl_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003623 struct sk_buff *beacon;
3624
3625 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
Johannes Berg32bfd352007-12-19 01:31:26 +01003626 beacon = ieee80211_beacon_get(priv->hw, priv->vif, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07003627
3628 if (!beacon) {
3629 IWL_ERROR("update beacon failed\n");
3630 return;
3631 }
3632
3633 mutex_lock(&priv->mutex);
3634 /* new beacon skb is allocated every time; dispose previous.*/
3635 if (priv->ibss_beacon)
3636 dev_kfree_skb(priv->ibss_beacon);
3637
3638 priv->ibss_beacon = beacon;
3639 mutex_unlock(&priv->mutex);
3640
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003641 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003642}
3643
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003644static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003645 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003646{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003647#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003648 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3649 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3650 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003651
3652 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3653 "tsf %d %d rate %d\n",
3654 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3655 beacon->beacon_notify_hdr.failure_frame,
3656 le32_to_cpu(beacon->ibss_mgr_status),
3657 le32_to_cpu(beacon->high_tsf),
3658 le32_to_cpu(beacon->low_tsf), rate);
3659#endif
3660
3661 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3662 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3663 queue_work(priv->workqueue, &priv->beacon_update);
3664}
3665
3666/* Service response to REPLY_SCAN_CMD (0x80) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003667static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003668 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003669{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07003670#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003671 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3672 struct iwl4965_scanreq_notification *notif =
3673 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003674
3675 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3676#endif
3677}
3678
3679/* Service SCAN_START_NOTIFICATION (0x82) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003680static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003681 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003682{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003683 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3684 struct iwl4965_scanstart_notification *notif =
3685 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003686 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3687 IWL_DEBUG_SCAN("Scan start: "
3688 "%d [802.11%s] "
3689 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3690 notif->channel,
3691 notif->band ? "bg" : "a",
3692 notif->tsf_high,
3693 notif->tsf_low, notif->status, notif->beacon_timer);
3694}
3695
3696/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003697static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003698 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003699{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003700 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3701 struct iwl4965_scanresults_notification *notif =
3702 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003703
3704 IWL_DEBUG_SCAN("Scan ch.res: "
3705 "%d [802.11%s] "
3706 "(TSF: 0x%08X:%08X) - %d "
3707 "elapsed=%lu usec (%dms since last)\n",
3708 notif->channel,
3709 notif->band ? "bg" : "a",
3710 le32_to_cpu(notif->tsf_high),
3711 le32_to_cpu(notif->tsf_low),
3712 le32_to_cpu(notif->statistics[0]),
3713 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3714 jiffies_to_msecs(elapsed_jiffies
3715 (priv->last_scan_jiffies, jiffies)));
3716
3717 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003718 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003719}
3720
3721/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003722static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003723 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003724{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003725 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3726 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003727
3728 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3729 scan_notif->scanned_channels,
3730 scan_notif->tsf_low,
3731 scan_notif->tsf_high, scan_notif->status);
3732
3733 /* The HW is no longer scanning */
3734 clear_bit(STATUS_SCAN_HW, &priv->status);
3735
3736 /* The scan completion notification came in, so kill that timer... */
3737 cancel_delayed_work(&priv->scan_check);
3738
3739 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3740 (priv->scan_bands == 2) ? "2.4" : "5.2",
3741 jiffies_to_msecs(elapsed_jiffies
3742 (priv->scan_pass_start, jiffies)));
3743
3744 /* Remove this scanned band from the list
3745 * of pending bands to scan */
3746 priv->scan_bands--;
3747
3748 /* If a request to abort was given, or the scan did not succeed
3749 * then we reset the scan state machine and terminate,
3750 * re-queuing another scan if one has been requested */
3751 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3752 IWL_DEBUG_INFO("Aborted scan completed.\n");
3753 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3754 } else {
3755 /* If there are more bands on this scan pass reschedule */
3756 if (priv->scan_bands > 0)
3757 goto reschedule;
3758 }
3759
3760 priv->last_scan_jiffies = jiffies;
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08003761 priv->next_scan_jiffies = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003762 IWL_DEBUG_INFO("Setting scan to off\n");
3763
3764 clear_bit(STATUS_SCANNING, &priv->status);
3765
3766 IWL_DEBUG_INFO("Scan took %dms\n",
3767 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3768
3769 queue_work(priv->workqueue, &priv->scan_completed);
3770
3771 return;
3772
3773reschedule:
3774 priv->scan_pass_start = jiffies;
3775 queue_work(priv->workqueue, &priv->request_scan);
3776}
3777
3778/* Handle notification from uCode that card's power state is changing
3779 * due to software, hardware, or critical temperature RFKILL */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003780static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003781 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003782{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003783 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003784 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
3785 unsigned long status = priv->status;
3786
3787 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
3788 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
3789 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
3790
3791 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
3792 RF_CARD_DISABLED)) {
3793
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003794 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003795 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3796
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003797 if (!iwl4965_grab_nic_access(priv)) {
3798 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003799 priv, HBUS_TARG_MBX_C,
3800 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3801
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003802 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003803 }
3804
3805 if (!(flags & RXON_CARD_DISABLED)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003806 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07003807 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003808 if (!iwl4965_grab_nic_access(priv)) {
3809 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07003810 priv, HBUS_TARG_MBX_C,
3811 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
3812
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003813 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003814 }
3815 }
3816
3817 if (flags & RF_CARD_DISABLED) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003818 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003819 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003820 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3821 if (!iwl4965_grab_nic_access(priv))
3822 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003823 }
3824 }
3825
3826 if (flags & HW_CARD_DISABLED)
3827 set_bit(STATUS_RF_KILL_HW, &priv->status);
3828 else
3829 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3830
3831
3832 if (flags & SW_CARD_DISABLED)
3833 set_bit(STATUS_RF_KILL_SW, &priv->status);
3834 else
3835 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3836
3837 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003838 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003839
3840 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
3841 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
3842 (test_bit(STATUS_RF_KILL_SW, &status) !=
3843 test_bit(STATUS_RF_KILL_SW, &priv->status)))
3844 queue_work(priv->workqueue, &priv->rf_kill);
3845 else
3846 wake_up_interruptible(&priv->wait_command_queue);
3847}
3848
3849/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003850 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07003851 *
3852 * Setup the RX handlers for each of the reply types sent from the uCode
3853 * to the host.
3854 *
3855 * This function chains into the hardware specific files for them to setup
3856 * any hardware specific handlers as well.
3857 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003858static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07003859{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003860 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
3861 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
3862 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
3863 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07003864 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003865 iwl4965_rx_spectrum_measure_notif;
3866 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003867 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003868 iwl4965_rx_pm_debug_statistics_notif;
3869 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003870
Ben Cahill9fbab512007-11-29 11:09:47 +08003871 /*
3872 * The same handler is used for both the REPLY to a discrete
3873 * statistics request from the host as well as for the periodic
3874 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07003875 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003876 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
3877 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07003878
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003879 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
3880 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003881 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003882 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07003883 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003884 iwl4965_rx_scan_complete_notif;
3885 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
3886 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07003887
Ben Cahill9fbab512007-11-29 11:09:47 +08003888 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003889 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003890}
3891
3892/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003893 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07003894 * @rxb: Rx buffer to reclaim
3895 *
3896 * If an Rx buffer has an async callback associated with it the callback
3897 * will be executed. The attached skb (if present) will only be freed
3898 * if the callback returns 1
3899 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07003900static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003901 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003902{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003903 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003904 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3905 int txq_id = SEQ_TO_QUEUE(sequence);
3906 int index = SEQ_TO_INDEX(sequence);
3907 int huge = sequence & SEQ_HUGE_FRAME;
3908 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003909 struct iwl4965_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07003910
3911 /* If a Tx command is being handled and it isn't in the actual
3912 * command queue then there a command routing bug has been introduced
3913 * in the queue management code. */
3914 if (txq_id != IWL_CMD_QUEUE_NUM)
3915 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
3916 txq_id, pkt->hdr.cmd);
3917 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
3918
3919 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
3920 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
3921
3922 /* Input error checking is done when commands are added to queue. */
3923 if (cmd->meta.flags & CMD_WANT_SKB) {
3924 cmd->meta.source->u.skb = rxb->skb;
3925 rxb->skb = NULL;
3926 } else if (cmd->meta.u.callback &&
3927 !cmd->meta.u.callback(priv, cmd, rxb->skb))
3928 rxb->skb = NULL;
3929
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003930 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003931
3932 if (!(cmd->meta.flags & CMD_ASYNC)) {
3933 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
3934 wake_up_interruptible(&priv->wait_command_queue);
3935 }
3936}
3937
3938/************************** RX-FUNCTIONS ****************************/
3939/*
3940 * Rx theory of operation
3941 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003942 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
3943 * each of which point to Receive Buffers to be filled by 4965. These get
3944 * used not only for Rx frames, but for any command response or notification
3945 * from the 4965. The driver and 4965 manage the Rx buffers by means
3946 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07003947 *
3948 * Rx Queue Indexes
3949 * The host/firmware share two index registers for managing the Rx buffers.
3950 *
3951 * The READ index maps to the first position that the firmware may be writing
3952 * to -- the driver can read up to (but not including) this position and get
3953 * good data.
3954 * The READ index is managed by the firmware once the card is enabled.
3955 *
3956 * The WRITE index maps to the last position the driver has read from -- the
3957 * position preceding WRITE is the last slot the firmware can place a packet.
3958 *
3959 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
3960 * WRITE = READ.
3961 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003962 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07003963 * INDEX position, and WRITE to the last (READ - 1 wrapped)
3964 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003965 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07003966 * and fire the RX interrupt. The driver can then query the READ index and
3967 * process as many packets as possible, moving the WRITE index forward as it
3968 * resets the Rx queue buffers with new memory.
3969 *
3970 * The management in the driver is as follows:
3971 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
3972 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08003973 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003974 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07003975 * iwl->rxq is replenished and the READ INDEX is updated (updating the
3976 * 'processed' and 'read' driver indexes as well)
3977 * + A received packet is processed and handed to the kernel network stack,
3978 * detached from the iwl->rxq. The driver 'processed' index is updated.
3979 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
3980 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
3981 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
3982 * were enough free buffers and RX_STALLED is set it is cleared.
3983 *
3984 *
3985 * Driver sequence:
3986 *
Ben Cahill9fbab512007-11-29 11:09:47 +08003987 * iwl4965_rx_queue_alloc() Allocates rx_free
3988 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003989 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08003990 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07003991 * queue, updates firmware pointers, and updates
3992 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003993 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07003994 *
3995 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08003996 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07003997 * READ INDEX, detaching the SKB from the pool.
3998 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003999 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07004000 * slots.
4001 * ...
4002 *
4003 */
4004
4005/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004006 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07004007 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004008static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004009{
4010 int s = q->read - q->write;
4011 if (s <= 0)
4012 s += RX_QUEUE_SIZE;
4013 /* keep some buffer to not confuse full and empty queue */
4014 s -= 2;
4015 if (s < 0)
4016 s = 0;
4017 return s;
4018}
4019
4020/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004021 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07004022 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004023int iwl4965_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004024{
4025 u32 reg = 0;
4026 int rc = 0;
4027 unsigned long flags;
4028
4029 spin_lock_irqsave(&q->lock, flags);
4030
4031 if (q->need_update == 0)
4032 goto exit_unlock;
4033
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004034 /* If power-saving is in use, make sure device is awake */
Zhu Yib481de92007-09-25 17:54:57 -07004035 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004036 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004037
4038 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004039 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004040 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4041 goto exit_unlock;
4042 }
4043
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004044 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004045 if (rc)
4046 goto exit_unlock;
4047
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004048 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004049 iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07004050 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004051 iwl4965_release_nic_access(priv);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004052
4053 /* Else device is assumed to be awake */
Zhu Yib481de92007-09-25 17:54:57 -07004054 } else
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004055 /* Device expects a multiple of 8 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004056 iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07004057
4058
4059 q->need_update = 0;
4060
4061 exit_unlock:
4062 spin_unlock_irqrestore(&q->lock, flags);
4063 return rc;
4064}
4065
4066/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004067 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07004068 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004069static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004070 dma_addr_t dma_addr)
4071{
4072 return cpu_to_le32((u32)(dma_addr >> 8));
4073}
4074
4075
4076/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004077 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07004078 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004079 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07004080 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08004081 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07004082 *
4083 * This moves the 'write' index forward to catch up with 'processed', and
4084 * also updates the memory address in the firmware to reference the new
4085 * target buffer.
4086 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004087static int iwl4965_rx_queue_restock(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004088{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004089 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004090 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004091 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004092 unsigned long flags;
4093 int write, rc;
4094
4095 spin_lock_irqsave(&rxq->lock, flags);
4096 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004097 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004098 /* Get next free Rx buffer, remove from free list */
Zhu Yib481de92007-09-25 17:54:57 -07004099 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004100 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004101 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004102
4103 /* Point to Rx buffer via next RBD in circular buffer */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004104 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07004105 rxq->queue[rxq->write] = rxb;
4106 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4107 rxq->free_count--;
4108 }
4109 spin_unlock_irqrestore(&rxq->lock, flags);
4110 /* If the pre-allocated buffer pool is dropping low, schedule to
4111 * refill it */
4112 if (rxq->free_count <= RX_LOW_WATERMARK)
4113 queue_work(priv->workqueue, &priv->rx_replenish);
4114
4115
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004116 /* If we've added more space for the firmware to place data, tell it.
4117 * Increment device's write pointer in multiples of 8. */
Zhu Yib481de92007-09-25 17:54:57 -07004118 if ((write != (rxq->write & ~0x7))
4119 || (abs(rxq->write - rxq->read) > 7)) {
4120 spin_lock_irqsave(&rxq->lock, flags);
4121 rxq->need_update = 1;
4122 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004123 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07004124 if (rc)
4125 return rc;
4126 }
4127
4128 return 0;
4129}
4130
4131/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004132 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07004133 *
4134 * When moving to rx_free an SKB is allocated for the slot.
4135 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004136 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08004137 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07004138 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004139static void iwl4965_rx_allocate(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004140{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004141 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004142 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004143 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004144 unsigned long flags;
4145 spin_lock_irqsave(&rxq->lock, flags);
4146 while (!list_empty(&rxq->rx_used)) {
4147 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004148 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004149
4150 /* Alloc a new receive buffer */
Zhu Yib481de92007-09-25 17:54:57 -07004151 rxb->skb =
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004152 alloc_skb(priv->hw_setting.rx_buf_size,
4153 __GFP_NOWARN | GFP_ATOMIC);
Zhu Yib481de92007-09-25 17:54:57 -07004154 if (!rxb->skb) {
4155 if (net_ratelimit())
4156 printk(KERN_CRIT DRV_NAME
4157 ": Can not allocate SKB buffers\n");
4158 /* We don't reschedule replenish work here -- we will
4159 * call the restock method and if it still needs
4160 * more buffers it will schedule replenish */
4161 break;
4162 }
4163 priv->alloc_rxb_skb++;
4164 list_del(element);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004165
4166 /* Get physical address of RB/SKB */
Zhu Yib481de92007-09-25 17:54:57 -07004167 rxb->dma_addr =
4168 pci_map_single(priv->pci_dev, rxb->skb->data,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004169 priv->hw_setting.rx_buf_size, PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004170 list_add_tail(&rxb->list, &rxq->rx_free);
4171 rxq->free_count++;
4172 }
4173 spin_unlock_irqrestore(&rxq->lock, flags);
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004174}
4175
4176/*
4177 * this should be called while priv->lock is locked
4178*/
Tomas Winkler4fd1f842007-12-05 20:59:58 +02004179static void __iwl4965_rx_replenish(void *data)
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004180{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004181 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004182
4183 iwl4965_rx_allocate(priv);
4184 iwl4965_rx_queue_restock(priv);
4185}
4186
4187
4188void iwl4965_rx_replenish(void *data)
4189{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004190 struct iwl_priv *priv = data;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004191 unsigned long flags;
4192
4193 iwl4965_rx_allocate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004194
4195 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004196 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004197 spin_unlock_irqrestore(&priv->lock, flags);
4198}
4199
4200/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08004201 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07004202 * This free routine walks the list of POOL entries and if SKB is set to
4203 * non NULL it is unmapped and freed
4204 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004205static void iwl4965_rx_queue_free(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004206{
4207 int i;
4208 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4209 if (rxq->pool[i].skb != NULL) {
4210 pci_unmap_single(priv->pci_dev,
4211 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004212 priv->hw_setting.rx_buf_size,
4213 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004214 dev_kfree_skb(rxq->pool[i].skb);
4215 }
4216 }
4217
4218 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4219 rxq->dma_addr);
4220 rxq->bd = NULL;
4221}
4222
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004223int iwl4965_rx_queue_alloc(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004224{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004225 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004226 struct pci_dev *dev = priv->pci_dev;
4227 int i;
4228
4229 spin_lock_init(&rxq->lock);
4230 INIT_LIST_HEAD(&rxq->rx_free);
4231 INIT_LIST_HEAD(&rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004232
4233 /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
Zhu Yib481de92007-09-25 17:54:57 -07004234 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4235 if (!rxq->bd)
4236 return -ENOMEM;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004237
Zhu Yib481de92007-09-25 17:54:57 -07004238 /* Fill the rx_used queue with _all_ of the Rx buffers */
4239 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4240 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004241
Zhu Yib481de92007-09-25 17:54:57 -07004242 /* Set us so that we have processed and used all buffers, but have
4243 * not restocked the Rx queue with fresh buffers */
4244 rxq->read = rxq->write = 0;
4245 rxq->free_count = 0;
4246 rxq->need_update = 0;
4247 return 0;
4248}
4249
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004250void iwl4965_rx_queue_reset(struct iwl_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004251{
4252 unsigned long flags;
4253 int i;
4254 spin_lock_irqsave(&rxq->lock, flags);
4255 INIT_LIST_HEAD(&rxq->rx_free);
4256 INIT_LIST_HEAD(&rxq->rx_used);
4257 /* Fill the rx_used queue with _all_ of the Rx buffers */
4258 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4259 /* In the reset function, these buffers may have been allocated
4260 * to an SKB, so we need to unmap and free potential storage */
4261 if (rxq->pool[i].skb != NULL) {
4262 pci_unmap_single(priv->pci_dev,
4263 rxq->pool[i].dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004264 priv->hw_setting.rx_buf_size,
4265 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004266 priv->alloc_rxb_skb--;
4267 dev_kfree_skb(rxq->pool[i].skb);
4268 rxq->pool[i].skb = NULL;
4269 }
4270 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4271 }
4272
4273 /* Set us so that we have processed and used all buffers, but have
4274 * not restocked the Rx queue with fresh buffers */
4275 rxq->read = rxq->write = 0;
4276 rxq->free_count = 0;
4277 spin_unlock_irqrestore(&rxq->lock, flags);
4278}
4279
4280/* Convert linear signal-to-noise ratio into dB */
4281static u8 ratio2dB[100] = {
4282/* 0 1 2 3 4 5 6 7 8 9 */
4283 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4284 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4285 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4286 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4287 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4288 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4289 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4290 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4291 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4292 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4293};
4294
4295/* Calculates a relative dB value from a ratio of linear
4296 * (i.e. not dB) signal levels.
4297 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004298int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004299{
Adrian Bunkc899a572007-10-14 19:51:15 +02004300 /* 1000:1 or higher just report as 60 dB */
4301 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004302 return 60;
4303
Adrian Bunkc899a572007-10-14 19:51:15 +02004304 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004305 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004306 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004307 return (20 + (int)ratio2dB[sig_ratio/10]);
4308
4309 /* We shouldn't see this */
4310 if (sig_ratio < 1)
4311 return 0;
4312
4313 /* Use table for ratios 1:1 - 99:1 */
4314 return (int)ratio2dB[sig_ratio];
4315}
4316
4317#define PERFECT_RSSI (-20) /* dBm */
4318#define WORST_RSSI (-95) /* dBm */
4319#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4320
4321/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4322 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4323 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004324int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004325{
4326 int sig_qual;
4327 int degradation = PERFECT_RSSI - rssi_dbm;
4328
4329 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4330 * as indicator; formula is (signal dbm - noise dbm).
4331 * SNR at or above 40 is a great signal (100%).
4332 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4333 * Weakest usable signal is usually 10 - 15 dB SNR. */
4334 if (noise_dbm) {
4335 if (rssi_dbm - noise_dbm >= 40)
4336 return 100;
4337 else if (rssi_dbm < noise_dbm)
4338 return 0;
4339 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4340
4341 /* Else use just the signal level.
4342 * This formula is a least squares fit of data points collected and
4343 * compared with a reference system that had a percentage (%) display
4344 * for signal quality. */
4345 } else
4346 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4347 (15 * RSSI_RANGE + 62 * degradation)) /
4348 (RSSI_RANGE * RSSI_RANGE);
4349
4350 if (sig_qual > 100)
4351 sig_qual = 100;
4352 else if (sig_qual < 1)
4353 sig_qual = 0;
4354
4355 return sig_qual;
4356}
4357
4358/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004359 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004360 *
4361 * Uses the priv->rx_handlers callback function array to invoke
4362 * the appropriate handlers, including command responses,
4363 * frame-received notifications, and other notifications.
4364 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004365static void iwl4965_rx_handle(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004366{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004367 struct iwl4965_rx_mem_buffer *rxb;
4368 struct iwl4965_rx_packet *pkt;
4369 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004370 u32 r, i;
4371 int reclaim;
4372 unsigned long flags;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004373 u8 fill_rx = 0;
Mohamed Abbasd68ab682008-02-07 13:16:33 -08004374 u32 count = 8;
Zhu Yib481de92007-09-25 17:54:57 -07004375
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004376 /* uCode's read index (stored in shared DRAM) indicates the last Rx
4377 * buffer that the driver may process (last buffer filled by ucode). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004378 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004379 i = rxq->read;
4380
4381 /* Rx interrupt, but nothing sent from uCode */
4382 if (i == r)
4383 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4384
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004385 if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
4386 fill_rx = 1;
4387
Zhu Yib481de92007-09-25 17:54:57 -07004388 while (i != r) {
4389 rxb = rxq->queue[i];
4390
Ben Cahill9fbab512007-11-29 11:09:47 +08004391 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004392 * then a bug has been introduced in the queue refilling
4393 * routines -- catch it here */
4394 BUG_ON(rxb == NULL);
4395
4396 rxq->queue[i] = NULL;
4397
4398 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004399 priv->hw_setting.rx_buf_size,
Zhu Yib481de92007-09-25 17:54:57 -07004400 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004401 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004402
4403 /* Reclaim a command buffer only if this packet is a response
4404 * to a (driver-originated) command.
4405 * If the packet (e.g. Rx frame) originated from uCode,
4406 * there is no command buffer to reclaim.
4407 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4408 * but apparently a few don't get set; catch them here. */
4409 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4410 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4411 (pkt->hdr.cmd != REPLY_4965_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004412 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004413 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4414 (pkt->hdr.cmd != REPLY_TX);
4415
4416 /* Based on type of command response or notification,
4417 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004418 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004419 if (priv->rx_handlers[pkt->hdr.cmd]) {
4420 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4421 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4422 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4423 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4424 } else {
4425 /* No handling needed */
4426 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4427 "r %d i %d No handler needed for %s, 0x%02x\n",
4428 r, i, get_cmd_string(pkt->hdr.cmd),
4429 pkt->hdr.cmd);
4430 }
4431
4432 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004433 /* Invoke any callbacks, transfer the skb to caller, and
4434 * fire off the (possibly) blocking iwl4965_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004435 * as we reclaim the driver command queue */
4436 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004437 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004438 else
4439 IWL_WARNING("Claim null rxb?\n");
4440 }
4441
4442 /* For now we just don't re-use anything. We can tweak this
4443 * later to try and re-use notification packets and SKBs that
4444 * fail to Rx correctly */
4445 if (rxb->skb != NULL) {
4446 priv->alloc_rxb_skb--;
4447 dev_kfree_skb_any(rxb->skb);
4448 rxb->skb = NULL;
4449 }
4450
4451 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02004452 priv->hw_setting.rx_buf_size,
4453 PCI_DMA_FROMDEVICE);
Zhu Yib481de92007-09-25 17:54:57 -07004454 spin_lock_irqsave(&rxq->lock, flags);
4455 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4456 spin_unlock_irqrestore(&rxq->lock, flags);
4457 i = (i + 1) & RX_QUEUE_MASK;
Mohamed Abbas5c0eef92007-11-29 11:10:14 +08004458 /* If there are a lot of unused frames,
4459 * restock the Rx queue so ucode wont assert. */
4460 if (fill_rx) {
4461 count++;
4462 if (count >= 8) {
4463 priv->rxq.read = i;
4464 __iwl4965_rx_replenish(priv);
4465 count = 0;
4466 }
4467 }
Zhu Yib481de92007-09-25 17:54:57 -07004468 }
4469
4470 /* Backtrack one entry */
4471 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004472 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004473}
4474
Cahill, Ben M6440adb2007-11-29 11:09:55 +08004475/**
4476 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
4477 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004478static int iwl4965_tx_queue_update_write_ptr(struct iwl_priv *priv,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004479 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004480{
4481 u32 reg = 0;
4482 int rc = 0;
4483 int txq_id = txq->q.id;
4484
4485 if (txq->need_update == 0)
4486 return rc;
4487
4488 /* if we're trying to save power */
4489 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4490 /* wake up nic if it's powered down ...
4491 * uCode will wake up, and interrupt us again, so next
4492 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004493 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004494
4495 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4496 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004497 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004498 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4499 return rc;
4500 }
4501
4502 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004503 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004504 if (rc)
4505 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004506 iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004507 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004508 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004509
4510 /* else not in power-save mode, uCode will never sleep when we're
4511 * trying to tx (during RFKILL, we're not trying to tx). */
4512 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004513 iwl4965_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004514 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004515
4516 txq->need_update = 0;
4517
4518 return rc;
4519}
4520
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004521#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004522static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004523{
Joe Perches0795af52007-10-03 17:59:30 -07004524 DECLARE_MAC_BUF(mac);
4525
Zhu Yib481de92007-09-25 17:54:57 -07004526 IWL_DEBUG_RADIO("RX CONFIG:\n");
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004527 iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004528 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4529 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4530 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4531 le32_to_cpu(rxon->filter_flags));
4532 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4533 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4534 rxon->ofdm_basic_rates);
4535 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004536 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4537 print_mac(mac, rxon->node_addr));
4538 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4539 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004540 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4541}
4542#endif
4543
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004544static void iwl4965_enable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004545{
4546 IWL_DEBUG_ISR("Enabling interrupts\n");
4547 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004548 iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004549}
4550
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004551static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004552{
4553 clear_bit(STATUS_INT_ENABLED, &priv->status);
4554
4555 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004556 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004557
4558 /* acknowledge/clear/reset any interrupts still pending
4559 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004560 iwl4965_write32(priv, CSR_INT, 0xffffffff);
4561 iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004562 IWL_DEBUG_ISR("Disabled interrupts\n");
4563}
4564
4565static const char *desc_lookup(int i)
4566{
4567 switch (i) {
4568 case 1:
4569 return "FAIL";
4570 case 2:
4571 return "BAD_PARAM";
4572 case 3:
4573 return "BAD_CHECKSUM";
4574 case 4:
4575 return "NMI_INTERRUPT";
4576 case 5:
4577 return "SYSASSERT";
4578 case 6:
4579 return "FATAL_ERROR";
4580 }
4581
4582 return "UNKNOWN";
4583}
4584
4585#define ERROR_START_OFFSET (1 * sizeof(u32))
4586#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4587
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004588static void iwl4965_dump_nic_error_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004589{
4590 u32 data2, line;
4591 u32 desc, time, count, base, data1;
4592 u32 blink1, blink2, ilink1, ilink2;
4593 int rc;
4594
4595 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4596
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004597 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004598 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4599 return;
4600 }
4601
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004602 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004603 if (rc) {
4604 IWL_WARNING("Can not read from adapter at this time.\n");
4605 return;
4606 }
4607
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004608 count = iwl4965_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004609
4610 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4611 IWL_ERROR("Start IWL Error Log Dump:\n");
Tomas Winkler2acae162008-03-02 01:25:59 +02004612 IWL_ERROR("Status: 0x%08lX, count: %d\n", priv->status, count);
Zhu Yib481de92007-09-25 17:54:57 -07004613 }
4614
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004615 desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
4616 blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
4617 blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
4618 ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
4619 ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
4620 data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
4621 data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
4622 line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
4623 time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004624
4625 IWL_ERROR("Desc Time "
4626 "data1 data2 line\n");
4627 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4628 desc_lookup(desc), desc, time, data1, data2, line);
4629 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4630 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4631 ilink1, ilink2);
4632
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004633 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004634}
4635
4636#define EVENT_START_OFFSET (4 * sizeof(u32))
4637
4638/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004639 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004640 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004641 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004642 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004643static void iwl4965_print_event_log(struct iwl_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004644 u32 num_events, u32 mode)
4645{
4646 u32 i;
4647 u32 base; /* SRAM byte address of event log header */
4648 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4649 u32 ptr; /* SRAM byte address of log data */
4650 u32 ev, time, data; /* event log data */
4651
4652 if (num_events == 0)
4653 return;
4654
4655 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4656
4657 if (mode == 0)
4658 event_size = 2 * sizeof(u32);
4659 else
4660 event_size = 3 * sizeof(u32);
4661
4662 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4663
4664 /* "time" is actually "data" for mode 0 (no timestamp).
4665 * place event id # at far right for easier visual parsing. */
4666 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004667 ev = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004668 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004669 time = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004670 ptr += sizeof(u32);
4671 if (mode == 0)
4672 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4673 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004674 data = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004675 ptr += sizeof(u32);
4676 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4677 }
4678 }
4679}
4680
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004681static void iwl4965_dump_nic_event_log(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004682{
4683 int rc;
4684 u32 base; /* SRAM byte address of event log header */
4685 u32 capacity; /* event log capacity in # entries */
4686 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4687 u32 num_wraps; /* # times uCode wrapped to top of log */
4688 u32 next_entry; /* index of next entry to be written by uCode */
4689 u32 size; /* # entries that we'll print */
4690
4691 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004692 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004693 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4694 return;
4695 }
4696
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004697 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004698 if (rc) {
4699 IWL_WARNING("Can not read from adapter at this time.\n");
4700 return;
4701 }
4702
4703 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004704 capacity = iwl4965_read_targ_mem(priv, base);
4705 mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
4706 num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
4707 next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004708
4709 size = num_wraps ? capacity : next_entry;
4710
4711 /* bail out if nothing in log */
4712 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004713 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004714 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004715 return;
4716 }
4717
Zhu Yi583fab32007-09-27 11:27:30 +08004718 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004719 size, num_wraps);
4720
4721 /* if uCode has wrapped back to top of log, start at the oldest entry,
4722 * i.e the next one that uCode would fill. */
4723 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004724 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004725 capacity - next_entry, mode);
4726
4727 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004728 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004729
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004730 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004731}
4732
4733/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004734 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004735 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004736static void iwl4965_irq_handle_error(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004737{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004738 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004739 set_bit(STATUS_FW_ERROR, &priv->status);
4740
4741 /* Cancel currently queued command. */
4742 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4743
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004744#ifdef CONFIG_IWLWIFI_DEBUG
4745 if (iwl_debug_level & IWL_DL_FW_ERRORS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004746 iwl4965_dump_nic_error_log(priv);
4747 iwl4965_dump_nic_event_log(priv);
4748 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004749 }
4750#endif
4751
4752 wake_up_interruptible(&priv->wait_command_queue);
4753
4754 /* Keep the restart process from trying to send host
4755 * commands by clearing the INIT status bit */
4756 clear_bit(STATUS_READY, &priv->status);
4757
4758 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4759 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4760 "Restarting adapter due to uCode error.\n");
4761
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004762 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004763 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4764 sizeof(priv->recovery_rxon));
4765 priv->error_recovering = 1;
4766 }
4767 queue_work(priv->workqueue, &priv->restart);
4768 }
4769}
4770
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004771static void iwl4965_error_recovery(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004772{
4773 unsigned long flags;
4774
4775 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4776 sizeof(priv->staging_rxon));
4777 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004778 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004779
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004780 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004781
4782 spin_lock_irqsave(&priv->lock, flags);
4783 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4784 priv->error_recovering = 0;
4785 spin_unlock_irqrestore(&priv->lock, flags);
4786}
4787
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004788static void iwl4965_irq_tasklet(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004789{
4790 u32 inta, handled = 0;
4791 u32 inta_fh;
4792 unsigned long flags;
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004793#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004794 u32 inta_mask;
4795#endif
4796
4797 spin_lock_irqsave(&priv->lock, flags);
4798
4799 /* Ack/clear/reset pending uCode interrupts.
4800 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4801 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004802 inta = iwl4965_read32(priv, CSR_INT);
4803 iwl4965_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004804
4805 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4806 * Any new interrupts that happen after this, either while we're
4807 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004808 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
4809 iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004810
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004811#ifdef CONFIG_IWLWIFI_DEBUG
4812 if (iwl_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004813 /* just for debug */
4814 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004815 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4816 inta, inta_mask, inta_fh);
4817 }
4818#endif
4819
4820 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4821 * atomic, make sure that inta covers all the interrupts that
4822 * we've discovered, even if FH interrupt came in just after
4823 * reading CSR_INT. */
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004824 if (inta_fh & CSR49_FH_INT_RX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004825 inta |= CSR_INT_BIT_FH_RX;
Tomas Winkler6f83eaa2008-03-04 18:09:28 -08004826 if (inta_fh & CSR49_FH_INT_TX_MASK)
Zhu Yib481de92007-09-25 17:54:57 -07004827 inta |= CSR_INT_BIT_FH_TX;
4828
4829 /* Now service all interrupt bits discovered above. */
4830 if (inta & CSR_INT_BIT_HW_ERR) {
4831 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4832
4833 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004834 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004835
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004836 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004837
4838 handled |= CSR_INT_BIT_HW_ERR;
4839
4840 spin_unlock_irqrestore(&priv->lock, flags);
4841
4842 return;
4843 }
4844
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004845#ifdef CONFIG_IWLWIFI_DEBUG
4846 if (iwl_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07004847 /* NIC fires this, but we don't use it, redundant with WAKEUP */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004848 if (inta & CSR_INT_BIT_SCD)
4849 IWL_DEBUG_ISR("Scheduler finished to transmit "
4850 "the frame/frames.\n");
Zhu Yib481de92007-09-25 17:54:57 -07004851
4852 /* Alive notification via Rx interrupt will do the real work */
4853 if (inta & CSR_INT_BIT_ALIVE)
4854 IWL_DEBUG_ISR("Alive interrupt\n");
4855 }
4856#endif
4857 /* Safely ignore these bits for debug checks below */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004858 inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
Zhu Yib481de92007-09-25 17:54:57 -07004859
Ben Cahill9fbab512007-11-29 11:09:47 +08004860 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07004861 if (inta & CSR_INT_BIT_RF_KILL) {
4862 int hw_rf_kill = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004863 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07004864 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
4865 hw_rf_kill = 1;
4866
4867 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
4868 "RF_KILL bit toggled to %s.\n",
4869 hw_rf_kill ? "disable radio":"enable radio");
4870
4871 /* Queue restart only if RF_KILL switch was set to "kill"
4872 * when we loaded driver, and is now set to "enable".
4873 * After we're Alive, RF_KILL gets handled by
Reinette Chatre32304552008-02-15 14:34:37 -08004874 * iwl4965_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08004875 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
4876 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07004877 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08004878 }
Zhu Yib481de92007-09-25 17:54:57 -07004879
4880 handled |= CSR_INT_BIT_RF_KILL;
4881 }
4882
Ben Cahill9fbab512007-11-29 11:09:47 +08004883 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07004884 if (inta & CSR_INT_BIT_CT_KILL) {
4885 IWL_ERROR("Microcode CT kill error detected.\n");
4886 handled |= CSR_INT_BIT_CT_KILL;
4887 }
4888
4889 /* Error detected by uCode */
4890 if (inta & CSR_INT_BIT_SW_ERR) {
4891 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
4892 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004893 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004894 handled |= CSR_INT_BIT_SW_ERR;
4895 }
4896
4897 /* uCode wakes up after power-down sleep */
4898 if (inta & CSR_INT_BIT_WAKEUP) {
4899 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004900 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
4901 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
4902 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
4903 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
4904 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
4905 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
4906 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07004907
4908 handled |= CSR_INT_BIT_WAKEUP;
4909 }
4910
4911 /* All uCode command responses, including Tx command responses,
4912 * Rx "responses" (frame-received notification), and other
4913 * notifications from uCode come through here*/
4914 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004915 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004916 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
4917 }
4918
4919 if (inta & CSR_INT_BIT_FH_TX) {
4920 IWL_DEBUG_ISR("Tx interrupt\n");
4921 handled |= CSR_INT_BIT_FH_TX;
4922 }
4923
4924 if (inta & ~handled)
4925 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
4926
4927 if (inta & ~CSR_INI_SET_MASK) {
4928 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
4929 inta & ~CSR_INI_SET_MASK);
4930 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
4931 }
4932
4933 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004934 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004935
Tomas Winkler0a6857e2008-03-12 16:58:49 -07004936#ifdef CONFIG_IWLWIFI_DEBUG
4937 if (iwl_debug_level & (IWL_DL_ISR)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004938 inta = iwl4965_read32(priv, CSR_INT);
4939 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
4940 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004941 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
4942 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
4943 }
4944#endif
4945 spin_unlock_irqrestore(&priv->lock, flags);
4946}
4947
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004948static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004949{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07004950 struct iwl_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07004951 u32 inta, inta_mask;
4952 u32 inta_fh;
4953 if (!priv)
4954 return IRQ_NONE;
4955
4956 spin_lock(&priv->lock);
4957
4958 /* Disable (but don't clear!) interrupts here to avoid
4959 * back-to-back ISRs and sporadic interrupts from our NIC.
4960 * If we have something to service, the tasklet will re-enable ints.
4961 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004962 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
4963 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004964
4965 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004966 inta = iwl4965_read32(priv, CSR_INT);
4967 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07004968
4969 /* Ignore interrupt if there's nothing in NIC to service.
4970 * This may be due to IRQ shared with another device,
4971 * or due to sporadic interrupts thrown from our NIC. */
4972 if (!inta && !inta_fh) {
4973 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
4974 goto none;
4975 }
4976
4977 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08004978 /* Hardware disappeared. It might have already raised
4979 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07004980 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08004981 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07004982 }
4983
4984 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4985 inta, inta_mask, inta_fh);
4986
Joonwoo Park25c03d82008-01-23 10:15:20 -08004987 inta &= ~CSR_INT_BIT_SCD;
4988
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004989 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Joonwoo Park25c03d82008-01-23 10:15:20 -08004990 if (likely(inta || inta_fh))
4991 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07004992
Oliver Neukum66fbb542007-11-15 09:31:10 +08004993 unplugged:
4994 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07004995 return IRQ_HANDLED;
4996
4997 none:
4998 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004999 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005000 spin_unlock(&priv->lock);
5001 return IRQ_NONE;
5002}
5003
5004/************************** EEPROM BANDS ****************************
5005 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005006 * The iwl4965_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07005007 * EEPROM contents to the specific channel number supported for each
5008 * band.
5009 *
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005010 * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07005011 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5012 * The specific geography and calibration information for that channel
5013 * is contained in the eeprom map itself.
5014 *
5015 * During init, we copy the eeprom information and channel map
5016 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5017 *
5018 * channel_map_24/52 provides the index in the channel_info array for a
5019 * given channel. We have to have two separate maps as there is channel
5020 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5021 * band_2
5022 *
5023 * A value of 0xff stored in the channel_map indicates that the channel
5024 * is not supported by the hardware at all.
5025 *
5026 * A value of 0xfe in the channel_map indicates that the channel is not
5027 * valid for Tx with the current hardware. This means that
5028 * while the system can tune and receive on a given channel, it may not
5029 * be able to associate or transmit any frames on that
5030 * channel. There is no corresponding channel information for that
5031 * entry.
5032 *
5033 *********************************************************************/
5034
5035/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005036static const u8 iwl4965_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07005037 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5038};
5039
5040/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08005041static const u8 iwl4965_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005042 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5043};
5044
Ben Cahill9fbab512007-11-29 11:09:47 +08005045static const u8 iwl4965_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005046 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5047};
5048
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005049static const u8 iwl4965_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005050 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5051};
5052
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005053static const u8 iwl4965_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005054 145, 149, 153, 157, 161, 165
5055};
5056
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005057static u8 iwl4965_eeprom_band_6[] = { /* 2.4 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005058 1, 2, 3, 4, 5, 6, 7
5059};
5060
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005061static u8 iwl4965_eeprom_band_7[] = { /* 5.2 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005062 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5063};
5064
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005065static void iwl4965_init_band_reference(const struct iwl_priv *priv,
Ben Cahill9fbab512007-11-29 11:09:47 +08005066 int band,
Zhu Yib481de92007-09-25 17:54:57 -07005067 int *eeprom_ch_count,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005068 const struct iwl4965_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07005069 **eeprom_ch_info,
5070 const u8 **eeprom_ch_index)
5071{
5072 switch (band) {
5073 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005074 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_1);
Zhu Yib481de92007-09-25 17:54:57 -07005075 *eeprom_ch_info = priv->eeprom.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005076 *eeprom_ch_index = iwl4965_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07005077 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005078 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005079 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_2);
Zhu Yib481de92007-09-25 17:54:57 -07005080 *eeprom_ch_info = priv->eeprom.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005081 *eeprom_ch_index = iwl4965_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07005082 break;
5083 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005084 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_3);
Zhu Yib481de92007-09-25 17:54:57 -07005085 *eeprom_ch_info = priv->eeprom.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005086 *eeprom_ch_index = iwl4965_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07005087 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005088 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005089 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_4);
Zhu Yib481de92007-09-25 17:54:57 -07005090 *eeprom_ch_info = priv->eeprom.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005091 *eeprom_ch_index = iwl4965_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07005092 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005093 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005094 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005095 *eeprom_ch_info = priv->eeprom.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005096 *eeprom_ch_index = iwl4965_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07005097 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005098 case 6: /* 2.4GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005099 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_6);
Zhu Yib481de92007-09-25 17:54:57 -07005100 *eeprom_ch_info = priv->eeprom.band_24_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005101 *eeprom_ch_index = iwl4965_eeprom_band_6;
Zhu Yib481de92007-09-25 17:54:57 -07005102 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005103 case 7: /* 5 GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005104 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_7);
Zhu Yib481de92007-09-25 17:54:57 -07005105 *eeprom_ch_info = priv->eeprom.band_52_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005106 *eeprom_ch_index = iwl4965_eeprom_band_7;
Zhu Yib481de92007-09-25 17:54:57 -07005107 break;
5108 default:
5109 BUG();
5110 return;
5111 }
5112}
5113
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005114/**
5115 * iwl4965_get_channel_info - Find driver's private channel info
5116 *
5117 * Based on band and channel number.
5118 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005119const struct iwl4965_channel_info *iwl4965_get_channel_info(const struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01005120 enum ieee80211_band band, u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -07005121{
5122 int i;
5123
Johannes Berg8318d782008-01-24 19:38:38 +01005124 switch (band) {
5125 case IEEE80211_BAND_5GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07005126 for (i = 14; i < priv->channel_count; i++) {
5127 if (priv->channel_info[i].channel == channel)
5128 return &priv->channel_info[i];
5129 }
5130 break;
Johannes Berg8318d782008-01-24 19:38:38 +01005131 case IEEE80211_BAND_2GHZ:
Zhu Yib481de92007-09-25 17:54:57 -07005132 if (channel >= 1 && channel <= 14)
5133 return &priv->channel_info[channel - 1];
5134 break;
Johannes Berg8318d782008-01-24 19:38:38 +01005135 default:
5136 BUG();
Zhu Yib481de92007-09-25 17:54:57 -07005137 }
5138
5139 return NULL;
5140}
5141
5142#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5143 ? # x " " : "")
5144
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005145/**
5146 * iwl4965_init_channel_map - Set up driver's info for all possible channels
5147 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005148static int iwl4965_init_channel_map(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005149{
5150 int eeprom_ch_count = 0;
5151 const u8 *eeprom_ch_index = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005152 const struct iwl4965_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005153 int band, ch;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005154 struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005155
5156 if (priv->channel_count) {
5157 IWL_DEBUG_INFO("Channel map already initialized.\n");
5158 return 0;
5159 }
5160
5161 if (priv->eeprom.version < 0x2f) {
5162 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5163 priv->eeprom.version);
5164 return -EINVAL;
5165 }
5166
5167 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5168
5169 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005170 ARRAY_SIZE(iwl4965_eeprom_band_1) +
5171 ARRAY_SIZE(iwl4965_eeprom_band_2) +
5172 ARRAY_SIZE(iwl4965_eeprom_band_3) +
5173 ARRAY_SIZE(iwl4965_eeprom_band_4) +
5174 ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005175
5176 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5177
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005178 priv->channel_info = kzalloc(sizeof(struct iwl4965_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07005179 priv->channel_count, GFP_KERNEL);
5180 if (!priv->channel_info) {
5181 IWL_ERROR("Could not allocate channel_info\n");
5182 priv->channel_count = 0;
5183 return -ENOMEM;
5184 }
5185
5186 ch_info = priv->channel_info;
5187
5188 /* Loop through the 5 EEPROM bands adding them in order to the
5189 * channel map we maintain (that contains additional information than
5190 * what just in the EEPROM) */
5191 for (band = 1; band <= 5; band++) {
5192
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005193 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005194 &eeprom_ch_info, &eeprom_ch_index);
5195
5196 /* Loop through each band adding each of the channels */
5197 for (ch = 0; ch < eeprom_ch_count; ch++) {
5198 ch_info->channel = eeprom_ch_index[ch];
Johannes Berg8318d782008-01-24 19:38:38 +01005199 ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
5200 IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07005201
5202 /* permanently store EEPROM's channel regulatory flags
5203 * and max power in channel info database. */
5204 ch_info->eeprom = eeprom_ch_info[ch];
5205
5206 /* Copy the run-time flags so they are there even on
5207 * invalid channels */
5208 ch_info->flags = eeprom_ch_info[ch].flags;
5209
5210 if (!(is_channel_valid(ch_info))) {
5211 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5212 "No traffic\n",
5213 ch_info->channel,
5214 ch_info->flags,
5215 is_channel_a_band(ch_info) ?
5216 "5.2" : "2.4");
5217 ch_info++;
5218 continue;
5219 }
5220
5221 /* Initialize regulatory-based run-time data */
5222 ch_info->max_power_avg = ch_info->curr_txpow =
5223 eeprom_ch_info[ch].max_power_avg;
5224 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5225 ch_info->min_power = 0;
5226
Tomas Winkler8211ef72008-03-02 01:36:04 +02005227 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s%s(0x%02x"
Zhu Yib481de92007-09-25 17:54:57 -07005228 " %ddBm): Ad-Hoc %ssupported\n",
5229 ch_info->channel,
5230 is_channel_a_band(ch_info) ?
5231 "5.2" : "2.4",
Tomas Winkler8211ef72008-03-02 01:36:04 +02005232 CHECK_AND_PRINT(VALID),
Zhu Yib481de92007-09-25 17:54:57 -07005233 CHECK_AND_PRINT(IBSS),
5234 CHECK_AND_PRINT(ACTIVE),
5235 CHECK_AND_PRINT(RADAR),
5236 CHECK_AND_PRINT(WIDE),
5237 CHECK_AND_PRINT(NARROW),
5238 CHECK_AND_PRINT(DFS),
5239 eeprom_ch_info[ch].flags,
5240 eeprom_ch_info[ch].max_power_avg,
5241 ((eeprom_ch_info[ch].
5242 flags & EEPROM_CHANNEL_IBSS)
5243 && !(eeprom_ch_info[ch].
5244 flags & EEPROM_CHANNEL_RADAR))
5245 ? "" : "not ");
5246
5247 /* Set the user_txpower_limit to the highest power
5248 * supported by any channel */
5249 if (eeprom_ch_info[ch].max_power_avg >
5250 priv->user_txpower_limit)
5251 priv->user_txpower_limit =
5252 eeprom_ch_info[ch].max_power_avg;
5253
5254 ch_info++;
5255 }
5256 }
5257
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005258 /* Two additional EEPROM bands for 2.4 and 5 GHz FAT channels */
Zhu Yib481de92007-09-25 17:54:57 -07005259 for (band = 6; band <= 7; band++) {
Johannes Berg8318d782008-01-24 19:38:38 +01005260 enum ieee80211_band ieeeband;
Zhu Yib481de92007-09-25 17:54:57 -07005261 u8 fat_extension_chan;
5262
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005263 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005264 &eeprom_ch_info, &eeprom_ch_index);
5265
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005266 /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
Johannes Berg8318d782008-01-24 19:38:38 +01005267 ieeeband = (band == 6) ? IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005268
Zhu Yib481de92007-09-25 17:54:57 -07005269 /* Loop through each band adding each of the channels */
5270 for (ch = 0; ch < eeprom_ch_count; ch++) {
5271
5272 if ((band == 6) &&
5273 ((eeprom_ch_index[ch] == 5) ||
5274 (eeprom_ch_index[ch] == 6) ||
5275 (eeprom_ch_index[ch] == 7)))
5276 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5277 else
5278 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5279
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005280 /* Set up driver's info for lower half */
Johannes Berg8318d782008-01-24 19:38:38 +01005281 iwl4965_set_fat_chan_info(priv, ieeeband,
Zhu Yib481de92007-09-25 17:54:57 -07005282 eeprom_ch_index[ch],
5283 &(eeprom_ch_info[ch]),
5284 fat_extension_chan);
5285
Cahill, Ben M6440adb2007-11-29 11:09:55 +08005286 /* Set up driver's info for upper half */
Johannes Berg8318d782008-01-24 19:38:38 +01005287 iwl4965_set_fat_chan_info(priv, ieeeband,
Zhu Yib481de92007-09-25 17:54:57 -07005288 (eeprom_ch_index[ch] + 4),
5289 &(eeprom_ch_info[ch]),
5290 HT_IE_EXT_CHANNEL_BELOW);
5291 }
5292 }
5293
5294 return 0;
5295}
5296
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005297/*
5298 * iwl4965_free_channel_map - undo allocations in iwl4965_init_channel_map
5299 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005300static void iwl4965_free_channel_map(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005301{
5302 kfree(priv->channel_info);
5303 priv->channel_count = 0;
5304}
5305
Zhu Yib481de92007-09-25 17:54:57 -07005306/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5307 * sending probe req. This should be set long enough to hear probe responses
5308 * from more than one AP. */
5309#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5310#define IWL_ACTIVE_DWELL_TIME_52 (10)
5311
5312/* For faster active scanning, scan will move to the next channel if fewer than
5313 * PLCP_QUIET_THRESH packets are heard on this channel within
5314 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5315 * time if it's a quiet channel (nothing responded to our probe, and there's
5316 * no other traffic).
5317 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5318#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5319#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5320
5321/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5322 * Must be set longer than active dwell time.
5323 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5324#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5325#define IWL_PASSIVE_DWELL_TIME_52 (10)
5326#define IWL_PASSIVE_DWELL_BASE (100)
5327#define IWL_CHANNEL_TUNE_TIME 5
5328
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005329static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01005330 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07005331{
Johannes Berg8318d782008-01-24 19:38:38 +01005332 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07005333 return IWL_ACTIVE_DWELL_TIME_52;
5334 else
5335 return IWL_ACTIVE_DWELL_TIME_24;
5336}
5337
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005338static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01005339 enum ieee80211_band band)
Zhu Yib481de92007-09-25 17:54:57 -07005340{
Johannes Berg8318d782008-01-24 19:38:38 +01005341 u16 active = iwl4965_get_active_dwell_time(priv, band);
5342 u16 passive = (band != IEEE80211_BAND_5GHZ) ?
Zhu Yib481de92007-09-25 17:54:57 -07005343 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5344 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5345
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005346 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005347 /* If we're associated, we clamp the maximum passive
5348 * dwell time to be 98% of the beacon interval (minus
5349 * 2 * channel tune time) */
5350 passive = priv->beacon_int;
5351 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5352 passive = IWL_PASSIVE_DWELL_BASE;
5353 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5354 }
5355
5356 if (passive <= active)
5357 passive = active + 1;
5358
5359 return passive;
5360}
5361
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005362static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
Johannes Berg8318d782008-01-24 19:38:38 +01005363 enum ieee80211_band band,
Zhu Yib481de92007-09-25 17:54:57 -07005364 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005365 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07005366{
5367 const struct ieee80211_channel *channels = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01005368 const struct ieee80211_supported_band *sband;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005369 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005370 u16 passive_dwell = 0;
5371 u16 active_dwell = 0;
5372 int added, i;
5373
Johannes Berg8318d782008-01-24 19:38:38 +01005374 sband = iwl4965_get_hw_mode(priv, band);
5375 if (!sband)
Zhu Yib481de92007-09-25 17:54:57 -07005376 return 0;
5377
Johannes Berg8318d782008-01-24 19:38:38 +01005378 channels = sband->channels;
Zhu Yib481de92007-09-25 17:54:57 -07005379
Johannes Berg8318d782008-01-24 19:38:38 +01005380 active_dwell = iwl4965_get_active_dwell_time(priv, band);
5381 passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
Zhu Yib481de92007-09-25 17:54:57 -07005382
Johannes Berg8318d782008-01-24 19:38:38 +01005383 for (i = 0, added = 0; i < sband->n_channels; i++) {
5384 if (ieee80211_frequency_to_channel(channels[i].center_freq) ==
Zhu Yib481de92007-09-25 17:54:57 -07005385 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005386 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005387 IWL_DEBUG_SCAN
5388 ("Skipping current channel %d\n",
5389 le16_to_cpu(priv->active_rxon.channel));
5390 continue;
5391 }
5392 } else if (priv->only_active_channel)
5393 continue;
5394
Johannes Berg8318d782008-01-24 19:38:38 +01005395 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
Zhu Yib481de92007-09-25 17:54:57 -07005396
Johannes Berg8318d782008-01-24 19:38:38 +01005397 ch_info = iwl4965_get_channel_info(priv, band,
Ben Cahill9fbab512007-11-29 11:09:47 +08005398 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07005399 if (!is_channel_valid(ch_info)) {
5400 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5401 scan_ch->channel);
5402 continue;
5403 }
5404
5405 if (!is_active || is_channel_passive(ch_info) ||
Johannes Berg8318d782008-01-24 19:38:38 +01005406 (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
Zhu Yib481de92007-09-25 17:54:57 -07005407 scan_ch->type = 0; /* passive */
5408 else
5409 scan_ch->type = 1; /* active */
5410
5411 if (scan_ch->type & 1)
5412 scan_ch->type |= (direct_mask << 1);
5413
5414 if (is_channel_narrow(ch_info))
5415 scan_ch->type |= (1 << 7);
5416
5417 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5418 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5419
Ben Cahill9fbab512007-11-29 11:09:47 +08005420 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07005421 scan_ch->tpc.dsp_atten = 110;
5422 /* scan_pwr_info->tpc.dsp_atten; */
5423
5424 /*scan_pwr_info->tpc.tx_gain; */
Johannes Berg8318d782008-01-24 19:38:38 +01005425 if (band == IEEE80211_BAND_5GHZ)
Zhu Yib481de92007-09-25 17:54:57 -07005426 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5427 else {
5428 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5429 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08005430 * power level:
Reinette Chatre8a1b0242008-01-14 17:46:25 -08005431 * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07005432 */
5433 }
5434
5435 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5436 scan_ch->channel,
5437 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5438 (scan_ch->type & 1) ?
5439 active_dwell : passive_dwell);
5440
5441 scan_ch++;
5442 added++;
5443 }
5444
5445 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5446 return added;
5447}
5448
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005449static void iwl4965_init_hw_rates(struct iwl_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005450 struct ieee80211_rate *rates)
5451{
5452 int i;
5453
5454 for (i = 0; i < IWL_RATE_COUNT; i++) {
Johannes Berg8318d782008-01-24 19:38:38 +01005455 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
5456 rates[i].hw_value = i; /* Rate scaling will work on indexes */
5457 rates[i].hw_value_short = i;
5458 rates[i].flags = 0;
5459 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
Zhu Yib481de92007-09-25 17:54:57 -07005460 /*
Johannes Berg8318d782008-01-24 19:38:38 +01005461 * If CCK != 1M then set short preamble rate flag.
Zhu Yib481de92007-09-25 17:54:57 -07005462 */
Tomas Winkler35cdeaf2008-03-11 16:17:20 -07005463 rates[i].flags |=
5464 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
5465 0 : IEEE80211_RATE_SHORT_PREAMBLE;
Zhu Yib481de92007-09-25 17:54:57 -07005466 }
Zhu Yib481de92007-09-25 17:54:57 -07005467 }
Zhu Yib481de92007-09-25 17:54:57 -07005468}
5469
5470/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005471 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07005472 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005473static int iwl4965_init_geos(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005474{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005475 struct iwl4965_channel_info *ch;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005476 struct ieee80211_supported_band *sband;
Zhu Yib481de92007-09-25 17:54:57 -07005477 struct ieee80211_channel *channels;
5478 struct ieee80211_channel *geo_ch;
5479 struct ieee80211_rate *rates;
5480 int i = 0;
Zhu Yib481de92007-09-25 17:54:57 -07005481
Johannes Berg8318d782008-01-24 19:38:38 +01005482 if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
5483 priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
Zhu Yib481de92007-09-25 17:54:57 -07005484 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5485 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5486 return 0;
5487 }
5488
Zhu Yib481de92007-09-25 17:54:57 -07005489 channels = kzalloc(sizeof(struct ieee80211_channel) *
5490 priv->channel_count, GFP_KERNEL);
Johannes Berg8318d782008-01-24 19:38:38 +01005491 if (!channels)
Zhu Yib481de92007-09-25 17:54:57 -07005492 return -ENOMEM;
Zhu Yib481de92007-09-25 17:54:57 -07005493
Tomas Winkler8211ef72008-03-02 01:36:04 +02005494 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
Zhu Yib481de92007-09-25 17:54:57 -07005495 GFP_KERNEL);
5496 if (!rates) {
Zhu Yib481de92007-09-25 17:54:57 -07005497 kfree(channels);
5498 return -ENOMEM;
5499 }
5500
Zhu Yib481de92007-09-25 17:54:57 -07005501 /* 5.2GHz channels start after the 2.4GHz channels */
Tomas Winkler8211ef72008-03-02 01:36:04 +02005502 sband = &priv->bands[IEEE80211_BAND_5GHZ];
5503 sband->channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
5504 /* just OFDM */
5505 sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
5506 sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
Johannes Berg8318d782008-01-24 19:38:38 +01005507
Tomas Winkler8211ef72008-03-02 01:36:04 +02005508 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_5GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005509
Tomas Winkler8211ef72008-03-02 01:36:04 +02005510 sband = &priv->bands[IEEE80211_BAND_2GHZ];
5511 sband->channels = channels;
5512 /* OFDM & CCK */
5513 sband->bitrates = rates;
5514 sband->n_bitrates = IWL_RATE_COUNT;
Zhu Yib481de92007-09-25 17:54:57 -07005515
Tomas Winkler8211ef72008-03-02 01:36:04 +02005516 iwl4965_init_ht_hw_capab(&sband->ht_info, IEEE80211_BAND_2GHZ);
Tomas Winkler78330fd2008-02-06 02:37:18 +02005517
Zhu Yib481de92007-09-25 17:54:57 -07005518 priv->ieee_channels = channels;
5519 priv->ieee_rates = rates;
5520
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005521 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07005522
Tomas Winkler8211ef72008-03-02 01:36:04 +02005523 for (i = 0; i < priv->channel_count; i++) {
Zhu Yib481de92007-09-25 17:54:57 -07005524 ch = &priv->channel_info[i];
5525
Tomas Winkler8211ef72008-03-02 01:36:04 +02005526 /* FIXME: might be removed if scan is OK */
5527 if (!is_channel_valid(ch))
Zhu Yib481de92007-09-25 17:54:57 -07005528 continue;
Zhu Yib481de92007-09-25 17:54:57 -07005529
Tomas Winkler8211ef72008-03-02 01:36:04 +02005530 if (is_channel_a_band(ch))
5531 sband = &priv->bands[IEEE80211_BAND_5GHZ];
5532 else
5533 sband = &priv->bands[IEEE80211_BAND_2GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005534
Tomas Winkler8211ef72008-03-02 01:36:04 +02005535 geo_ch = &sband->channels[sband->n_channels++];
5536
5537 geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
Johannes Berg8318d782008-01-24 19:38:38 +01005538 geo_ch->max_power = ch->max_power_avg;
5539 geo_ch->max_antenna_gain = 0xff;
Mohamed Abbas7b723042008-01-31 21:46:40 -08005540 geo_ch->hw_value = ch->channel;
Zhu Yib481de92007-09-25 17:54:57 -07005541
5542 if (is_channel_valid(ch)) {
Johannes Berg8318d782008-01-24 19:38:38 +01005543 if (!(ch->flags & EEPROM_CHANNEL_IBSS))
5544 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
Zhu Yib481de92007-09-25 17:54:57 -07005545
Johannes Berg8318d782008-01-24 19:38:38 +01005546 if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
5547 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07005548
5549 if (ch->flags & EEPROM_CHANNEL_RADAR)
Johannes Berg8318d782008-01-24 19:38:38 +01005550 geo_ch->flags |= IEEE80211_CHAN_RADAR;
Zhu Yib481de92007-09-25 17:54:57 -07005551
5552 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5553 priv->max_channel_txpower_limit =
5554 ch->max_power_avg;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005555 } else {
Johannes Berg8318d782008-01-24 19:38:38 +01005556 geo_ch->flags |= IEEE80211_CHAN_DISABLED;
Tomas Winkler8211ef72008-03-02 01:36:04 +02005557 }
5558
5559 /* Save flags for reg domain usage */
5560 geo_ch->orig_flags = geo_ch->flags;
5561
5562 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
5563 ch->channel, geo_ch->center_freq,
5564 is_channel_a_band(ch) ? "5.2" : "2.4",
5565 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
5566 "restricted" : "valid",
5567 geo_ch->flags);
Zhu Yib481de92007-09-25 17:54:57 -07005568 }
5569
Tomas Winkler82b9a122008-03-04 18:09:30 -08005570 if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
5571 priv->cfg->sku & IWL_SKU_A) {
Zhu Yib481de92007-09-25 17:54:57 -07005572 printk(KERN_INFO DRV_NAME
5573 ": Incorrectly detected BG card as ABG. Please send "
5574 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5575 priv->pci_dev->device, priv->pci_dev->subsystem_device);
Tomas Winkler82b9a122008-03-04 18:09:30 -08005576 priv->cfg->sku &= ~IWL_SKU_A;
Zhu Yib481de92007-09-25 17:54:57 -07005577 }
5578
5579 printk(KERN_INFO DRV_NAME
5580 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
Johannes Berg8318d782008-01-24 19:38:38 +01005581 priv->bands[IEEE80211_BAND_2GHZ].n_channels,
5582 priv->bands[IEEE80211_BAND_5GHZ].n_channels);
Zhu Yib481de92007-09-25 17:54:57 -07005583
Johannes Berg8318d782008-01-24 19:38:38 +01005584 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] = &priv->bands[IEEE80211_BAND_2GHZ];
5585 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] = &priv->bands[IEEE80211_BAND_5GHZ];
Zhu Yib481de92007-09-25 17:54:57 -07005586
Zhu Yib481de92007-09-25 17:54:57 -07005587 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5588
5589 return 0;
5590}
5591
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005592/*
5593 * iwl4965_free_geos - undo allocations in iwl4965_init_geos
5594 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005595static void iwl4965_free_geos(struct iwl_priv *priv)
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005596{
Reinette Chatre849e0dc2008-01-23 10:15:18 -08005597 kfree(priv->ieee_channels);
5598 kfree(priv->ieee_rates);
5599 clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
5600}
5601
Zhu Yib481de92007-09-25 17:54:57 -07005602/******************************************************************************
5603 *
5604 * uCode download functions
5605 *
5606 ******************************************************************************/
5607
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005608static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005609{
Tomas Winkler98c92212008-01-14 17:46:20 -08005610 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
5611 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
5612 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
5613 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
5614 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
5615 iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07005616}
5617
5618/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005619 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005620 * looking at all data.
5621 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005622static int iwl4965_verify_inst_full(struct iwl_priv *priv, __le32 *image,
Ben Cahill9fbab512007-11-29 11:09:47 +08005623 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005624{
5625 u32 val;
5626 u32 save_len = len;
5627 int rc = 0;
5628 u32 errcnt;
5629
5630 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5631
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005632 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005633 if (rc)
5634 return rc;
5635
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005636 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005637
5638 errcnt = 0;
5639 for (; len > 0; len -= sizeof(u32), image++) {
5640 /* read data comes through single port, auto-incr addr */
5641 /* NOTE: Use the debugless read so we don't flood kernel log
5642 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005643 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005644 if (val != le32_to_cpu(*image)) {
5645 IWL_ERROR("uCode INST section is invalid at "
5646 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5647 save_len - len, val, le32_to_cpu(*image));
5648 rc = -EIO;
5649 errcnt++;
5650 if (errcnt >= 20)
5651 break;
5652 }
5653 }
5654
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005655 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005656
5657 if (!errcnt)
5658 IWL_DEBUG_INFO
5659 ("ucode image in INSTRUCTION memory is good\n");
5660
5661 return rc;
5662}
5663
5664
5665/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005666 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005667 * using sample data 100 bytes apart. If these sample points are good,
5668 * it's a pretty good bet that everything between them is good, too.
5669 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005670static int iwl4965_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005671{
5672 u32 val;
5673 int rc = 0;
5674 u32 errcnt = 0;
5675 u32 i;
5676
5677 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5678
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005679 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005680 if (rc)
5681 return rc;
5682
5683 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5684 /* read data comes through single port, auto-incr addr */
5685 /* NOTE: Use the debugless read so we don't flood kernel log
5686 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005687 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005688 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005689 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005690 if (val != le32_to_cpu(*image)) {
5691#if 0 /* Enable this if you want to see details */
5692 IWL_ERROR("uCode INST section is invalid at "
5693 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5694 i, val, *image);
5695#endif
5696 rc = -EIO;
5697 errcnt++;
5698 if (errcnt >= 3)
5699 break;
5700 }
5701 }
5702
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005703 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005704
5705 return rc;
5706}
5707
5708
5709/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005710 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005711 * and verify its contents
5712 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005713static int iwl4965_verify_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005714{
5715 __le32 *image;
5716 u32 len;
5717 int rc = 0;
5718
5719 /* Try bootstrap */
5720 image = (__le32 *)priv->ucode_boot.v_addr;
5721 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005722 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005723 if (rc == 0) {
5724 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5725 return 0;
5726 }
5727
5728 /* Try initialize */
5729 image = (__le32 *)priv->ucode_init.v_addr;
5730 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005731 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005732 if (rc == 0) {
5733 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5734 return 0;
5735 }
5736
5737 /* Try runtime/protocol */
5738 image = (__le32 *)priv->ucode_code.v_addr;
5739 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005740 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005741 if (rc == 0) {
5742 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5743 return 0;
5744 }
5745
5746 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5747
Ben Cahill9fbab512007-11-29 11:09:47 +08005748 /* Since nothing seems to match, show first several data entries in
5749 * instruction SRAM, so maybe visual inspection will give a clue.
5750 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005751 image = (__le32 *)priv->ucode_boot.v_addr;
5752 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005753 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005754
5755 return rc;
5756}
5757
5758
5759/* check contents of special bootstrap uCode SRAM */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005760static int iwl4965_verify_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005761{
5762 __le32 *image = priv->ucode_boot.v_addr;
5763 u32 len = priv->ucode_boot.len;
5764 u32 reg;
5765 u32 val;
5766
5767 IWL_DEBUG_INFO("Begin verify bsm\n");
5768
5769 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005770 val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005771 for (reg = BSM_SRAM_LOWER_BOUND;
5772 reg < BSM_SRAM_LOWER_BOUND + len;
5773 reg += sizeof(u32), image ++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005774 val = iwl4965_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005775 if (val != le32_to_cpu(*image)) {
5776 IWL_ERROR("BSM uCode verification failed at "
5777 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5778 BSM_SRAM_LOWER_BOUND,
5779 reg - BSM_SRAM_LOWER_BOUND, len,
5780 val, le32_to_cpu(*image));
5781 return -EIO;
5782 }
5783 }
5784
5785 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
5786
5787 return 0;
5788}
5789
5790/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005791 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07005792 *
5793 * BSM operation:
5794 *
5795 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
5796 * in special SRAM that does not power down during RFKILL. When powering back
5797 * up after power-saving sleeps (or during initial uCode load), the BSM loads
5798 * the bootstrap program into the on-board processor, and starts it.
5799 *
5800 * The bootstrap program loads (via DMA) instructions and data for a new
5801 * program from host DRAM locations indicated by the host driver in the
5802 * BSM_DRAM_* registers. Once the new program is loaded, it starts
5803 * automatically.
5804 *
5805 * When initializing the NIC, the host driver points the BSM to the
5806 * "initialize" uCode image. This uCode sets up some internal data, then
5807 * notifies host via "initialize alive" that it is complete.
5808 *
5809 * The host then replaces the BSM_DRAM_* pointer values to point to the
5810 * normal runtime uCode instructions and a backup uCode data cache buffer
5811 * (filled initially with starting data values for the on-board processor),
5812 * then triggers the "initialize" uCode to load and launch the runtime uCode,
5813 * which begins normal operation.
5814 *
5815 * When doing a power-save shutdown, runtime uCode saves data SRAM into
5816 * the backup data cache in DRAM before SRAM is powered down.
5817 *
5818 * When powering back up, the BSM loads the bootstrap program. This reloads
5819 * the runtime uCode instructions and the backup data cache into SRAM,
5820 * and re-launches the runtime uCode from where it left off.
5821 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005822static int iwl4965_load_bsm(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005823{
5824 __le32 *image = priv->ucode_boot.v_addr;
5825 u32 len = priv->ucode_boot.len;
5826 dma_addr_t pinst;
5827 dma_addr_t pdata;
5828 u32 inst_len;
5829 u32 data_len;
5830 int rc;
5831 int i;
5832 u32 done;
5833 u32 reg_offset;
5834
5835 IWL_DEBUG_INFO("Begin load bsm\n");
5836
5837 /* make sure bootstrap program is no larger than BSM's SRAM size */
5838 if (len > IWL_MAX_BSM_SIZE)
5839 return -EINVAL;
5840
5841 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08005842 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005843 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07005844 * after the "initialize" uCode has run, to point to
5845 * runtime/protocol instructions and backup data cache. */
5846 pinst = priv->ucode_init.p_addr >> 4;
5847 pdata = priv->ucode_init_data.p_addr >> 4;
5848 inst_len = priv->ucode_init.len;
5849 data_len = priv->ucode_init_data.len;
5850
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005851 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005852 if (rc)
5853 return rc;
5854
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005855 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
5856 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
5857 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
5858 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07005859
5860 /* Fill BSM memory with bootstrap instructions */
5861 for (reg_offset = BSM_SRAM_LOWER_BOUND;
5862 reg_offset < BSM_SRAM_LOWER_BOUND + len;
5863 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005864 _iwl4965_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07005865 le32_to_cpu(*image));
5866
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005867 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005868 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005869 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005870 return rc;
5871 }
5872
5873 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005874 iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
5875 iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005876 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005877 iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07005878
5879 /* Load bootstrap code into instruction SRAM now,
5880 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005881 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005882 BSM_WR_CTRL_REG_BIT_START);
5883
5884 /* Wait for load of bootstrap uCode to finish */
5885 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005886 done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005887 if (!(done & BSM_WR_CTRL_REG_BIT_START))
5888 break;
5889 udelay(10);
5890 }
5891 if (i < 100)
5892 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
5893 else {
5894 IWL_ERROR("BSM write did not complete!\n");
5895 return -EIO;
5896 }
5897
5898 /* Enable future boot loads whenever power management unit triggers it
5899 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005900 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07005901 BSM_WR_CTRL_REG_BIT_START_EN);
5902
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005903 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005904
5905 return 0;
5906}
5907
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005908static void iwl4965_nic_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005909{
5910 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005911 iwl4965_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07005912}
5913
Tomas Winkler90e759d2007-11-29 11:09:41 +08005914
Zhu Yib481de92007-09-25 17:54:57 -07005915/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005916 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07005917 *
5918 * Copy into buffers for card to fetch via bus-mastering
5919 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07005920static int iwl4965_read_ucode(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005921{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005922 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08005923 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07005924 const struct firmware *ucode_raw;
Tomas Winkler4bf775c2008-03-04 18:09:31 -08005925 const char *name = priv->cfg->fw_name;
Zhu Yib481de92007-09-25 17:54:57 -07005926 u8 *src;
5927 size_t len;
5928 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
5929
5930 /* Ask kernel firmware_class module to get the boot firmware off disk.
5931 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08005932 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
5933 if (ret < 0) {
5934 IWL_ERROR("%s firmware file req failed: Reason %d\n",
5935 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07005936 goto error;
5937 }
5938
5939 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
5940 name, ucode_raw->size);
5941
5942 /* Make sure that we got at least our header! */
5943 if (ucode_raw->size < sizeof(*ucode)) {
5944 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08005945 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005946 goto err_release;
5947 }
5948
5949 /* Data from ucode file: header followed by uCode images */
5950 ucode = (void *)ucode_raw->data;
5951
5952 ver = le32_to_cpu(ucode->ver);
5953 inst_size = le32_to_cpu(ucode->inst_size);
5954 data_size = le32_to_cpu(ucode->data_size);
5955 init_size = le32_to_cpu(ucode->init_size);
5956 init_data_size = le32_to_cpu(ucode->init_data_size);
5957 boot_size = le32_to_cpu(ucode->boot_size);
5958
5959 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
5960 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
5961 inst_size);
5962 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
5963 data_size);
5964 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
5965 init_size);
5966 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
5967 init_data_size);
5968 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
5969 boot_size);
5970
5971 /* Verify size of file vs. image size info in file's header */
5972 if (ucode_raw->size < sizeof(*ucode) +
5973 inst_size + data_size + init_size +
5974 init_data_size + boot_size) {
5975
5976 IWL_DEBUG_INFO("uCode file size %d too small\n",
5977 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08005978 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005979 goto err_release;
5980 }
5981
5982 /* Verify that uCode images will fit in card's SRAM */
5983 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005984 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
5985 inst_size);
5986 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005987 goto err_release;
5988 }
5989
5990 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08005991 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
5992 data_size);
5993 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07005994 goto err_release;
5995 }
5996 if (init_size > IWL_MAX_INST_SIZE) {
5997 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08005998 ("uCode init instr len %d too large to fit in\n",
5999 init_size);
6000 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006001 goto err_release;
6002 }
6003 if (init_data_size > IWL_MAX_DATA_SIZE) {
6004 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006005 ("uCode init data len %d too large to fit in\n",
6006 init_data_size);
6007 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006008 goto err_release;
6009 }
6010 if (boot_size > IWL_MAX_BSM_SIZE) {
6011 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006012 ("uCode boot instr len %d too large to fit in\n",
6013 boot_size);
6014 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006015 goto err_release;
6016 }
6017
6018 /* Allocate ucode buffers for card's bus-master loading ... */
6019
6020 /* Runtime instructions and 2 copies of data:
6021 * 1) unmodified from disk
6022 * 2) backup cache for save/restore during power-downs */
6023 priv->ucode_code.len = inst_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006024 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07006025
6026 priv->ucode_data.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006027 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07006028
6029 priv->ucode_data_backup.len = data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006030 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07006031
6032 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006033 if (init_size && init_data_size) {
6034 priv->ucode_init.len = init_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006035 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07006036
Tomas Winkler90e759d2007-11-29 11:09:41 +08006037 priv->ucode_init_data.len = init_data_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006038 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
Tomas Winkler90e759d2007-11-29 11:09:41 +08006039
6040 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
6041 goto err_pci_alloc;
6042 }
Zhu Yib481de92007-09-25 17:54:57 -07006043
6044 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006045 if (boot_size) {
6046 priv->ucode_boot.len = boot_size;
Tomas Winkler98c92212008-01-14 17:46:20 -08006047 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07006048
Tomas Winkler90e759d2007-11-29 11:09:41 +08006049 if (!priv->ucode_boot.v_addr)
6050 goto err_pci_alloc;
6051 }
Zhu Yib481de92007-09-25 17:54:57 -07006052
6053 /* Copy images into buffers for card's bus-master reads ... */
6054
6055 /* Runtime instructions (first block of data in file) */
6056 src = &ucode->data[0];
6057 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006058 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006059 memcpy(priv->ucode_code.v_addr, src, len);
6060 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6061 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6062
6063 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006064 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07006065 src = &ucode->data[inst_size];
6066 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006067 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006068 memcpy(priv->ucode_data.v_addr, src, len);
6069 memcpy(priv->ucode_data_backup.v_addr, src, len);
6070
6071 /* Initialization instructions (3rd block) */
6072 if (init_size) {
6073 src = &ucode->data[inst_size + data_size];
6074 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006075 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
6076 len);
Zhu Yib481de92007-09-25 17:54:57 -07006077 memcpy(priv->ucode_init.v_addr, src, len);
6078 }
6079
6080 /* Initialization data (4th block) */
6081 if (init_data_size) {
6082 src = &ucode->data[inst_size + data_size + init_size];
6083 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006084 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
6085 len);
Zhu Yib481de92007-09-25 17:54:57 -07006086 memcpy(priv->ucode_init_data.v_addr, src, len);
6087 }
6088
6089 /* Bootstrap instructions (5th block) */
6090 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6091 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006092 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006093 memcpy(priv->ucode_boot.v_addr, src, len);
6094
6095 /* We have our copies now, allow OS release its copies */
6096 release_firmware(ucode_raw);
6097 return 0;
6098
6099 err_pci_alloc:
6100 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006101 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006102 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006103
6104 err_release:
6105 release_firmware(ucode_raw);
6106
6107 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08006108 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006109}
6110
6111
6112/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006113 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07006114 *
6115 * Tell initialization uCode where to find runtime uCode.
6116 *
6117 * BSM registers initially contain pointers to initialization uCode.
6118 * We need to replace them to load runtime uCode inst and data,
6119 * and to save runtime data when powering down.
6120 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006121static int iwl4965_set_ucode_ptrs(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006122{
6123 dma_addr_t pinst;
6124 dma_addr_t pdata;
6125 int rc = 0;
6126 unsigned long flags;
6127
6128 /* bits 35:4 for 4965 */
6129 pinst = priv->ucode_code.p_addr >> 4;
6130 pdata = priv->ucode_data_backup.p_addr >> 4;
6131
6132 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006133 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006134 if (rc) {
6135 spin_unlock_irqrestore(&priv->lock, flags);
6136 return rc;
6137 }
6138
6139 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006140 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6141 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6142 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006143 priv->ucode_data.len);
6144
6145 /* Inst bytecount must be last to set up, bit 31 signals uCode
6146 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006147 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006148 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6149
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006150 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006151
6152 spin_unlock_irqrestore(&priv->lock, flags);
6153
6154 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6155
6156 return rc;
6157}
6158
6159/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006160 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006161 *
6162 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6163 *
6164 * The 4965 "initialize" ALIVE reply contains calibration data for:
6165 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6166 * (3945 does not contain this data).
6167 *
6168 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6169*/
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006170static void iwl4965_init_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006171{
6172 /* Check alive response for "valid" sign from uCode */
6173 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6174 /* We had an error bringing up the hardware, so take it
6175 * all the way back down so we can try again */
6176 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6177 goto restart;
6178 }
6179
6180 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6181 * This is a paranoid check, because we would not have gotten the
6182 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006183 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006184 /* Runtime instruction load was bad;
6185 * take it all the way back down so we can try again */
6186 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6187 goto restart;
6188 }
6189
6190 /* Calculate temperature */
6191 priv->temperature = iwl4965_get_temperature(priv);
6192
6193 /* Send pointers to protocol/runtime uCode image ... init code will
6194 * load and launch runtime uCode, which will send us another "Alive"
6195 * notification. */
6196 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006197 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006198 /* Runtime instruction load won't happen;
6199 * take it all the way back down so we can try again */
6200 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6201 goto restart;
6202 }
6203 return;
6204
6205 restart:
6206 queue_work(priv->workqueue, &priv->restart);
6207}
6208
6209
6210/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006211 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006212 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006213 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07006214 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006215static void iwl4965_alive_start(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006216{
6217 int rc = 0;
6218
6219 IWL_DEBUG_INFO("Runtime Alive received.\n");
6220
6221 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6222 /* We had an error bringing up the hardware, so take it
6223 * all the way back down so we can try again */
6224 IWL_DEBUG_INFO("Alive failed.\n");
6225 goto restart;
6226 }
6227
6228 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6229 * This is a paranoid check, because we would not have gotten the
6230 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006231 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006232 /* Runtime instruction load was bad;
6233 * take it all the way back down so we can try again */
6234 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6235 goto restart;
6236 }
6237
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006238 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006239
6240 rc = iwl4965_alive_notify(priv);
6241 if (rc) {
6242 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6243 rc);
6244 goto restart;
6245 }
6246
Ben Cahill9fbab512007-11-29 11:09:47 +08006247 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07006248 set_bit(STATUS_ALIVE, &priv->status);
6249
6250 /* Clear out the uCode error bit if it is set */
6251 clear_bit(STATUS_FW_ERROR, &priv->status);
6252
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006253 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006254 return;
6255
Zhu Yi5a66926a2008-01-14 17:46:18 -08006256 ieee80211_start_queues(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006257
6258 priv->active_rate = priv->rates_mask;
6259 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6260
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006261 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07006262
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006263 if (iwl4965_is_associated(priv)) {
6264 struct iwl4965_rxon_cmd *active_rxon =
6265 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07006266
6267 memcpy(&priv->staging_rxon, &priv->active_rxon,
6268 sizeof(priv->staging_rxon));
6269 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6270 } else {
6271 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006272 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006273 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6274 }
6275
Ben Cahill9fbab512007-11-29 11:09:47 +08006276 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006277 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006278
6279 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006280 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006281
6282 /* At this point, the NIC is initialized and operational */
6283 priv->notif_missed_beacons = 0;
6284 set_bit(STATUS_READY, &priv->status);
6285
6286 iwl4965_rf_kill_ct_config(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08006287
Zhu Yib481de92007-09-25 17:54:57 -07006288 IWL_DEBUG_INFO("ALIVE processing complete.\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08006289 wake_up_interruptible(&priv->wait_command_queue);
Zhu Yib481de92007-09-25 17:54:57 -07006290
6291 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006292 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006293
6294 return;
6295
6296 restart:
6297 queue_work(priv->workqueue, &priv->restart);
6298}
6299
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006300static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07006301
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006302static void __iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006303{
6304 unsigned long flags;
6305 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6306 struct ieee80211_conf *conf = NULL;
6307
6308 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6309
6310 conf = ieee80211_get_hw_conf(priv->hw);
6311
6312 if (!exit_pending)
6313 set_bit(STATUS_EXIT_PENDING, &priv->status);
6314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006315 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006316
6317 /* Unblock any waiting calls */
6318 wake_up_interruptible_all(&priv->wait_command_queue);
6319
Zhu Yib481de92007-09-25 17:54:57 -07006320 /* Wipe out the EXIT_PENDING status bit if we are not actually
6321 * exiting the module */
6322 if (!exit_pending)
6323 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6324
6325 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006326 iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07006327
6328 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006329 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006330
6331 if (priv->mac80211_registered)
6332 ieee80211_stop_queues(priv->hw);
6333
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006334 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07006335 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006336 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006337 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6338 STATUS_RF_KILL_HW |
6339 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6340 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08006341 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
6342 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07006343 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6344 STATUS_IN_SUSPEND;
6345 goto exit;
6346 }
6347
6348 /* ...otherwise clear out all the status bits but the RF Kill and
6349 * SUSPEND bits and continue taking the NIC down. */
6350 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6351 STATUS_RF_KILL_HW |
6352 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6353 STATUS_RF_KILL_SW |
Reinette Chatre97888642008-02-06 11:20:38 -08006354 test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
6355 STATUS_GEO_CONFIGURED |
Zhu Yib481de92007-09-25 17:54:57 -07006356 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6357 STATUS_IN_SUSPEND |
6358 test_bit(STATUS_FW_ERROR, &priv->status) <<
6359 STATUS_FW_ERROR;
6360
6361 spin_lock_irqsave(&priv->lock, flags);
Ben Cahill9fbab512007-11-29 11:09:47 +08006362 iwl4965_clear_bit(priv, CSR_GP_CNTRL,
6363 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07006364 spin_unlock_irqrestore(&priv->lock, flags);
6365
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006366 iwl4965_hw_txq_ctx_stop(priv);
6367 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006368
6369 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006370 if (!iwl4965_grab_nic_access(priv)) {
6371 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006372 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006373 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006374 }
6375 spin_unlock_irqrestore(&priv->lock, flags);
6376
6377 udelay(5);
6378
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006379 iwl4965_hw_nic_stop_master(priv);
6380 iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6381 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006382
6383 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006384 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07006385
6386 if (priv->ibss_beacon)
6387 dev_kfree_skb(priv->ibss_beacon);
6388 priv->ibss_beacon = NULL;
6389
6390 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006391 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006392}
6393
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006394static void iwl4965_down(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006395{
6396 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006397 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006398 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08006399
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006400 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006401}
6402
6403#define MAX_HW_RESTARTS 5
6404
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006405static int __iwl4965_up(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006406{
6407 int rc, i;
Zhu Yib481de92007-09-25 17:54:57 -07006408
6409 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6410 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6411 return -EIO;
6412 }
6413
6414 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6415 IWL_WARNING("Radio disabled by SW RF kill (module "
6416 "parameter)\n");
Zhu Yie655b9f2008-01-24 02:19:38 -08006417 return -ENODEV;
6418 }
6419
Reinette Chatree903fbd2008-01-30 22:05:15 -08006420 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6421 IWL_ERROR("ucode not available for device bringup\n");
6422 return -EIO;
6423 }
6424
Zhu Yie655b9f2008-01-24 02:19:38 -08006425 /* If platform's RF_KILL switch is NOT set to KILL */
6426 if (iwl4965_read32(priv, CSR_GP_CNTRL) &
6427 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
6428 clear_bit(STATUS_RF_KILL_HW, &priv->status);
6429 else {
6430 set_bit(STATUS_RF_KILL_HW, &priv->status);
6431 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
6432 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6433 return -ENODEV;
6434 }
Zhu Yib481de92007-09-25 17:54:57 -07006435 }
6436
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006437 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07006438
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006439 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006440 if (rc) {
6441 IWL_ERROR("Unable to int nic\n");
6442 return rc;
6443 }
6444
6445 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006446 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6447 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07006448 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6449
6450 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006451 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
6452 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006453
6454 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006455 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6456 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07006457
6458 /* Copy original ucode data image from disk into backup cache.
6459 * This will be used to initialize the on-board processor's
6460 * data SRAM for a clean start when the runtime program first loads. */
6461 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
Zhu Yi5a66926a2008-01-14 17:46:18 -08006462 priv->ucode_data.len);
Zhu Yib481de92007-09-25 17:54:57 -07006463
Zhu Yie655b9f2008-01-24 02:19:38 -08006464 /* We return success when we resume from suspend and rf_kill is on. */
6465 if (test_bit(STATUS_RF_KILL_HW, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07006466 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07006467
6468 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6469
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006470 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006471
6472 /* load bootstrap state machine,
6473 * load bootstrap program into processor's memory,
6474 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006475 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006476
6477 if (rc) {
6478 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6479 continue;
6480 }
6481
6482 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006483 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006484
Zhu Yib481de92007-09-25 17:54:57 -07006485 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6486
6487 return 0;
6488 }
6489
6490 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006491 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006492
6493 /* tried to restart and config the device for as long as our
6494 * patience could withstand */
6495 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6496 return -EIO;
6497}
6498
6499
6500/*****************************************************************************
6501 *
6502 * Workqueue callbacks
6503 *
6504 *****************************************************************************/
6505
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006506static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006507{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006508 struct iwl_priv *priv =
6509 container_of(data, struct iwl_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006510
6511 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6512 return;
6513
6514 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006515 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006516 mutex_unlock(&priv->mutex);
6517}
6518
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006519static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006520{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006521 struct iwl_priv *priv =
6522 container_of(data, struct iwl_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006523
6524 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6525 return;
6526
6527 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006528 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006529 mutex_unlock(&priv->mutex);
6530}
6531
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006532static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006533{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006534 struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07006535
6536 wake_up_interruptible(&priv->wait_command_queue);
6537
6538 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6539 return;
6540
6541 mutex_lock(&priv->mutex);
6542
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006543 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006544 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6545 "HW and/or SW RF Kill no longer active, restarting "
6546 "device\n");
6547 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6548 queue_work(priv->workqueue, &priv->restart);
6549 } else {
6550
6551 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6552 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6553 "disabled by SW switch\n");
6554 else
6555 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6556 "Kill switch must be turned off for "
6557 "wireless networking to work.\n");
6558 }
6559 mutex_unlock(&priv->mutex);
6560}
6561
6562#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6563
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006564static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006565{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006566 struct iwl_priv *priv =
6567 container_of(data, struct iwl_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006568
6569 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6570 return;
6571
6572 mutex_lock(&priv->mutex);
6573 if (test_bit(STATUS_SCANNING, &priv->status) ||
6574 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6575 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6576 "Scan completion watchdog resetting adapter (%dms)\n",
6577 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006578
Zhu Yib481de92007-09-25 17:54:57 -07006579 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006580 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006581 }
6582 mutex_unlock(&priv->mutex);
6583}
6584
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006585static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006586{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006587 struct iwl_priv *priv =
6588 container_of(data, struct iwl_priv, request_scan);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006589 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006590 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006591 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006592 .meta.flags = CMD_SIZE_HUGE,
6593 };
6594 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006595 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006596 struct ieee80211_conf *conf = NULL;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006597 u16 cmd_len;
Johannes Berg8318d782008-01-24 19:38:38 +01006598 enum ieee80211_band band;
Tomas Winkler78330fd2008-02-06 02:37:18 +02006599 u8 direct_mask;
Zhu Yib481de92007-09-25 17:54:57 -07006600
6601 conf = ieee80211_get_hw_conf(priv->hw);
6602
6603 mutex_lock(&priv->mutex);
6604
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006605 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006606 IWL_WARNING("request scan called when driver not ready.\n");
6607 goto done;
6608 }
6609
6610 /* Make sure the scan wasn't cancelled before this queued work
6611 * was given the chance to run... */
6612 if (!test_bit(STATUS_SCANNING, &priv->status))
6613 goto done;
6614
6615 /* This should never be called or scheduled if there is currently
6616 * a scan active in the hardware. */
6617 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6618 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6619 "Ignoring second request.\n");
6620 rc = -EIO;
6621 goto done;
6622 }
6623
6624 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6625 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6626 goto done;
6627 }
6628
6629 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6630 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6631 goto done;
6632 }
6633
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006634 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006635 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6636 goto done;
6637 }
6638
6639 if (!test_bit(STATUS_READY, &priv->status)) {
6640 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6641 goto done;
6642 }
6643
6644 if (!priv->scan_bands) {
6645 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6646 goto done;
6647 }
6648
6649 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006650 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006651 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6652 if (!priv->scan) {
6653 rc = -ENOMEM;
6654 goto done;
6655 }
6656 }
6657 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006658 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006659
6660 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6661 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6662
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006663 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006664 u16 interval = 0;
6665 u32 extra;
6666 u32 suspend_time = 100;
6667 u32 scan_suspend_time = 100;
6668 unsigned long flags;
6669
6670 IWL_DEBUG_INFO("Scanning while associated...\n");
6671
6672 spin_lock_irqsave(&priv->lock, flags);
6673 interval = priv->beacon_int;
6674 spin_unlock_irqrestore(&priv->lock, flags);
6675
6676 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006677 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006678 if (!interval)
6679 interval = suspend_time;
6680
6681 extra = (suspend_time / interval) << 22;
6682 scan_suspend_time = (extra |
6683 ((suspend_time % interval) * 1024));
6684 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6685 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6686 scan_suspend_time, interval);
6687 }
6688
6689 /* We should add the ability for user to lock to PASSIVE ONLY */
6690 if (priv->one_direct_scan) {
6691 IWL_DEBUG_SCAN
6692 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006693 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006694 priv->direct_ssid_len));
6695 scan->direct_scan[0].id = WLAN_EID_SSID;
6696 scan->direct_scan[0].len = priv->direct_ssid_len;
6697 memcpy(scan->direct_scan[0].ssid,
6698 priv->direct_ssid, priv->direct_ssid_len);
6699 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006700 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006701 scan->direct_scan[0].id = WLAN_EID_SSID;
6702 scan->direct_scan[0].len = priv->essid_len;
6703 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6704 direct_mask = 1;
6705 } else
6706 direct_mask = 0;
6707
Zhu Yib481de92007-09-25 17:54:57 -07006708 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6709 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6710 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6711
Zhu Yib481de92007-09-25 17:54:57 -07006712
6713 switch (priv->scan_bands) {
6714 case 2:
6715 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6716 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006717 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006718 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6719
6720 scan->good_CRC_th = 0;
Johannes Berg8318d782008-01-24 19:38:38 +01006721 band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006722 break;
6723
6724 case 1:
6725 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006726 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006727 RATE_MCS_ANT_B_MSK);
6728 scan->good_CRC_th = IWL_GOOD_CRC_TH;
Johannes Berg8318d782008-01-24 19:38:38 +01006729 band = IEEE80211_BAND_5GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07006730 break;
6731
6732 default:
6733 IWL_WARNING("Invalid scan band count\n");
6734 goto done;
6735 }
6736
Tomas Winkler78330fd2008-02-06 02:37:18 +02006737 /* We don't build a direct scan probe request; the uCode will do
6738 * that based on the direct_mask added to each channel entry */
6739 cmd_len = iwl4965_fill_probe_req(priv, band,
6740 (struct ieee80211_mgmt *)scan->data,
6741 IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
6742
6743 scan->tx_cmd.len = cpu_to_le16(cmd_len);
Zhu Yib481de92007-09-25 17:54:57 -07006744 /* select Rx chains */
6745
6746 /* Force use of chains B and C (0x6) for scan Rx.
6747 * Avoid A (0x1) because of its off-channel reception on A-band.
6748 * MIMO is not used here, but value is required to make uCode happy. */
6749 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
6750 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
6751 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
6752 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
6753
6754 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
6755 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
6756
Reinette Chatre26c0f032008-03-11 16:17:15 -07006757 if (direct_mask) {
Zhu Yib481de92007-09-25 17:54:57 -07006758 IWL_DEBUG_SCAN
6759 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006760 iwl4965_escape_essid(priv->essid, priv->essid_len));
Reinette Chatre26c0f032008-03-11 16:17:15 -07006761 scan->channel_count =
6762 iwl4965_get_channels_for_scan(
6763 priv, band, 1, /* active */
6764 direct_mask,
6765 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6766 } else {
Zhu Yib481de92007-09-25 17:54:57 -07006767 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
Reinette Chatre26c0f032008-03-11 16:17:15 -07006768 scan->channel_count =
6769 iwl4965_get_channels_for_scan(
6770 priv, band, 0, /* passive */
6771 direct_mask,
6772 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
6773 }
Zhu Yib481de92007-09-25 17:54:57 -07006774
6775 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006776 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07006777 cmd.data = scan;
6778 scan->len = cpu_to_le16(cmd.len);
6779
6780 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006781 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07006782 if (rc)
6783 goto done;
6784
6785 queue_delayed_work(priv->workqueue, &priv->scan_check,
6786 IWL_SCAN_CHECK_WATCHDOG);
6787
6788 mutex_unlock(&priv->mutex);
6789 return;
6790
6791 done:
Ian Schram01ebd062007-10-25 17:15:22 +08006792 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07006793 queue_work(priv->workqueue, &priv->scan_completed);
6794 mutex_unlock(&priv->mutex);
6795}
6796
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006797static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006798{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006799 struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07006800
6801 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6802 return;
6803
6804 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006805 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006806 mutex_unlock(&priv->mutex);
6807}
6808
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006809static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006810{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006811 struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07006812
6813 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6814 return;
6815
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006816 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006817 queue_work(priv->workqueue, &priv->up);
6818}
6819
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006820static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006821{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006822 struct iwl_priv *priv =
6823 container_of(data, struct iwl_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07006824
6825 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6826 return;
6827
6828 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006829 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006830 mutex_unlock(&priv->mutex);
6831}
6832
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006833#define IWL_DELAY_NEXT_SCAN (HZ*2)
6834
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006835static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006836{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006837 struct iwl_priv *priv = container_of(data, struct iwl_priv,
Zhu Yib481de92007-09-25 17:54:57 -07006838 post_associate.work);
6839
6840 int rc = 0;
6841 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07006842 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006843
6844 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
6845 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
6846 return;
6847 }
6848
Joe Perches0795af52007-10-03 17:59:30 -07006849 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
6850 priv->assoc_id,
6851 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006852
6853
6854 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6855 return;
6856
6857 mutex_lock(&priv->mutex);
6858
Johannes Berg32bfd352007-12-19 01:31:26 +01006859 if (!priv->vif || !priv->is_open) {
Mohamed Abbas948c1712007-10-25 17:15:45 +08006860 mutex_unlock(&priv->mutex);
6861 return;
6862 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006863 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08006864
Zhu Yib481de92007-09-25 17:54:57 -07006865 conf = ieee80211_get_hw_conf(priv->hw);
6866
6867 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006868 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006869
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006870 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
6871 iwl4965_setup_rxon_timing(priv);
6872 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07006873 sizeof(priv->rxon_timing), &priv->rxon_timing);
6874 if (rc)
6875 IWL_WARNING("REPLY_RXON_TIMING failed - "
6876 "Attempting to continue.\n");
6877
6878 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
6879
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006880#ifdef CONFIG_IWL4965_HT
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02006881 if (priv->current_ht_config.is_ht)
6882 iwl4965_set_rxon_ht(priv, &priv->current_ht_config);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006883#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07006884 iwl4965_set_rxon_chain(priv);
6885 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
6886
6887 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
6888 priv->assoc_id, priv->beacon_int);
6889
6890 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
6891 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
6892 else
6893 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
6894
6895 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
6896 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
6897 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
6898 else
6899 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6900
6901 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6902 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
6903
6904 }
6905
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006906 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006907
6908 switch (priv->iw_mode) {
6909 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006910 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07006911 break;
6912
6913 case IEEE80211_IF_TYPE_IBSS:
6914
6915 /* clear out the station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006916 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006917
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006918 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
6919 iwl4965_rxon_add_station(priv, priv->bssid, 0);
6920 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
6921 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006922
6923 break;
6924
6925 default:
6926 IWL_ERROR("%s Should not be called in %d mode\n",
6927 __FUNCTION__, priv->iw_mode);
6928 break;
6929 }
6930
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006931 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006932
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006933#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07006934 /* Enable Rx differential gain and sensitivity calibrations */
6935 iwl4965_chain_noise_reset(priv);
6936 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08006937#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07006938
6939 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
6940 priv->assoc_station_added = 1;
6941
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006942 iwl4965_activate_qos(priv, 0);
Ron Rindjunsky292ae172008-02-06 11:20:39 -08006943
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08006944 /* we have just associated, don't start scan too early */
6945 priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
Zhu Yib481de92007-09-25 17:54:57 -07006946 mutex_unlock(&priv->mutex);
6947}
6948
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006949static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006950{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006951 struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07006952
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006953 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006954 return;
6955
6956 mutex_lock(&priv->mutex);
6957
6958 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006959 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006960
6961 mutex_unlock(&priv->mutex);
6962}
6963
Zhu Yi76bb77e2007-11-22 10:53:22 +08006964static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
6965
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006966static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006967{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006968 struct iwl_priv *priv =
6969 container_of(work, struct iwl_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07006970
6971 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
6972
6973 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6974 return;
6975
Zhu Yia0646472007-12-20 14:10:01 +08006976 if (test_bit(STATUS_CONF_PENDING, &priv->status))
6977 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
Zhu Yi76bb77e2007-11-22 10:53:22 +08006978
Zhu Yib481de92007-09-25 17:54:57 -07006979 ieee80211_scan_completed(priv->hw);
6980
6981 /* Since setting the TXPOWER may have been deferred while
6982 * performing the scan, fire one off */
6983 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006984 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006985 mutex_unlock(&priv->mutex);
6986}
6987
6988/*****************************************************************************
6989 *
6990 * mac80211 entry point functions
6991 *
6992 *****************************************************************************/
6993
Zhu Yi5a66926a2008-01-14 17:46:18 -08006994#define UCODE_READY_TIMEOUT (2 * HZ)
6995
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006996static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07006997{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07006998 struct iwl_priv *priv = hw->priv;
Zhu Yi5a66926a2008-01-14 17:46:18 -08006999 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07007000
7001 IWL_DEBUG_MAC80211("enter\n");
7002
Zhu Yi5a66926a2008-01-14 17:46:18 -08007003 if (pci_enable_device(priv->pci_dev)) {
7004 IWL_ERROR("Fail to pci_enable_device\n");
7005 return -ENODEV;
7006 }
7007 pci_restore_state(priv->pci_dev);
7008 pci_enable_msi(priv->pci_dev);
7009
7010 ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
7011 DRV_NAME, priv);
7012 if (ret) {
7013 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
7014 goto out_disable_msi;
7015 }
7016
Zhu Yib481de92007-09-25 17:54:57 -07007017 /* we should be verifying the device is ready to be opened */
7018 mutex_lock(&priv->mutex);
7019
Zhu Yi5a66926a2008-01-14 17:46:18 -08007020 memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
7021 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
7022 * ucode filename and max sizes are card-specific. */
7023
7024 if (!priv->ucode_code.len) {
7025 ret = iwl4965_read_ucode(priv);
7026 if (ret) {
7027 IWL_ERROR("Could not read microcode: %d\n", ret);
7028 mutex_unlock(&priv->mutex);
7029 goto out_release_irq;
7030 }
7031 }
7032
Zhu Yie655b9f2008-01-24 02:19:38 -08007033 ret = __iwl4965_up(priv);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007034
Zhu Yib481de92007-09-25 17:54:57 -07007035 mutex_unlock(&priv->mutex);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007036
Zhu Yie655b9f2008-01-24 02:19:38 -08007037 if (ret)
7038 goto out_release_irq;
7039
7040 IWL_DEBUG_INFO("Start UP work done.\n");
7041
7042 if (test_bit(STATUS_IN_SUSPEND, &priv->status))
7043 return 0;
7044
Zhu Yi5a66926a2008-01-14 17:46:18 -08007045 /* Wait for START_ALIVE from ucode. Otherwise callbacks from
7046 * mac80211 will not be run successfully. */
7047 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
7048 test_bit(STATUS_READY, &priv->status),
7049 UCODE_READY_TIMEOUT);
7050 if (!ret) {
7051 if (!test_bit(STATUS_READY, &priv->status)) {
7052 IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
7053 jiffies_to_msecs(UCODE_READY_TIMEOUT));
7054 ret = -ETIMEDOUT;
7055 goto out_release_irq;
7056 }
7057 }
7058
Zhu Yie655b9f2008-01-24 02:19:38 -08007059 priv->is_open = 1;
Zhu Yib481de92007-09-25 17:54:57 -07007060 IWL_DEBUG_MAC80211("leave\n");
7061 return 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007062
7063out_release_irq:
7064 free_irq(priv->pci_dev->irq, priv);
7065out_disable_msi:
7066 pci_disable_msi(priv->pci_dev);
Zhu Yie655b9f2008-01-24 02:19:38 -08007067 pci_disable_device(priv->pci_dev);
7068 priv->is_open = 0;
7069 IWL_DEBUG_MAC80211("leave - failed\n");
Zhu Yi5a66926a2008-01-14 17:46:18 -08007070 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007071}
7072
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007073static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007074{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007075 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007076
7077 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08007078
Zhu Yie655b9f2008-01-24 02:19:38 -08007079 if (!priv->is_open) {
7080 IWL_DEBUG_MAC80211("leave - skip\n");
7081 return;
7082 }
7083
Zhu Yib481de92007-09-25 17:54:57 -07007084 priv->is_open = 0;
Zhu Yi5a66926a2008-01-14 17:46:18 -08007085
7086 if (iwl4965_is_ready_rf(priv)) {
Zhu Yie655b9f2008-01-24 02:19:38 -08007087 /* stop mac, cancel any scan request and clear
7088 * RXON_FILTER_ASSOC_MSK BIT
7089 */
Zhu Yi5a66926a2008-01-14 17:46:18 -08007090 mutex_lock(&priv->mutex);
7091 iwl4965_scan_cancel_timeout(priv, 100);
7092 cancel_delayed_work(&priv->post_associate);
Mohamed Abbasfde35712007-11-29 11:10:15 +08007093 mutex_unlock(&priv->mutex);
Mohamed Abbasfde35712007-11-29 11:10:15 +08007094 }
7095
Zhu Yi5a66926a2008-01-14 17:46:18 -08007096 iwl4965_down(priv);
7097
7098 flush_workqueue(priv->workqueue);
7099 free_irq(priv->pci_dev->irq, priv);
7100 pci_disable_msi(priv->pci_dev);
7101 pci_save_state(priv->pci_dev);
7102 pci_disable_device(priv->pci_dev);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007103
Zhu Yib481de92007-09-25 17:54:57 -07007104 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007105}
7106
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007107static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007108 struct ieee80211_tx_control *ctl)
7109{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007110 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007111
7112 IWL_DEBUG_MAC80211("enter\n");
7113
7114 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7115 IWL_DEBUG_MAC80211("leave - monitor\n");
7116 return -1;
7117 }
7118
7119 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
Johannes Berg8318d782008-01-24 19:38:38 +01007120 ctl->tx_rate->bitrate);
Zhu Yib481de92007-09-25 17:54:57 -07007121
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007122 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07007123 dev_kfree_skb_any(skb);
7124
7125 IWL_DEBUG_MAC80211("leave\n");
7126 return 0;
7127}
7128
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007129static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007130 struct ieee80211_if_init_conf *conf)
7131{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007132 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007133 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07007134 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007135
Johannes Berg32bfd352007-12-19 01:31:26 +01007136 IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007137
Johannes Berg32bfd352007-12-19 01:31:26 +01007138 if (priv->vif) {
7139 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
Reinette Chatre75849d22008-01-23 10:15:17 -08007140 return -EOPNOTSUPP;
Zhu Yib481de92007-09-25 17:54:57 -07007141 }
7142
7143 spin_lock_irqsave(&priv->lock, flags);
Johannes Berg32bfd352007-12-19 01:31:26 +01007144 priv->vif = conf->vif;
Zhu Yib481de92007-09-25 17:54:57 -07007145
7146 spin_unlock_irqrestore(&priv->lock, flags);
7147
7148 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02007149
7150 if (conf->mac_addr) {
7151 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
7152 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
7153 }
Zhu Yib481de92007-09-25 17:54:57 -07007154
Zhu Yi5a66926a2008-01-14 17:46:18 -08007155 if (iwl4965_is_ready(priv))
7156 iwl4965_set_mode(priv, conf->type);
7157
Zhu Yib481de92007-09-25 17:54:57 -07007158 mutex_unlock(&priv->mutex);
7159
Zhu Yi5a66926a2008-01-14 17:46:18 -08007160 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007161 return 0;
7162}
7163
7164/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007165 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07007166 *
7167 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7168 * be set inappropriately and the driver currently sets the hardware up to
7169 * use it whenever needed.
7170 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007171static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007172{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007173 struct iwl_priv *priv = hw->priv;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007174 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07007175 unsigned long flags;
Zhu Yi76bb77e2007-11-22 10:53:22 +08007176 int ret = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007177
7178 mutex_lock(&priv->mutex);
Johannes Berg8318d782008-01-24 19:38:38 +01007179 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
Zhu Yib481de92007-09-25 17:54:57 -07007180
Zhu Yi12342c42007-12-20 11:27:32 +08007181 priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
7182
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007183 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007184 IWL_DEBUG_MAC80211("leave - not ready\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007185 ret = -EIO;
7186 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007187 }
7188
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007189 if (unlikely(!iwl4965_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07007190 test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yia0646472007-12-20 14:10:01 +08007191 IWL_DEBUG_MAC80211("leave - scanning\n");
7192 set_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07007193 mutex_unlock(&priv->mutex);
Zhu Yia0646472007-12-20 14:10:01 +08007194 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007195 }
7196
7197 spin_lock_irqsave(&priv->lock, flags);
7198
Johannes Berg8318d782008-01-24 19:38:38 +01007199 ch_info = iwl4965_get_channel_info(priv, conf->channel->band,
7200 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07007201 if (!is_channel_valid(ch_info)) {
Zhu Yib481de92007-09-25 17:54:57 -07007202 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7203 spin_unlock_irqrestore(&priv->lock, flags);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007204 ret = -EINVAL;
7205 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007206 }
7207
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007208#ifdef CONFIG_IWL4965_HT
Tomas Winkler78330fd2008-02-06 02:37:18 +02007209 /* if we are switching from ht to 2.4 clear flags
Zhu Yib481de92007-09-25 17:54:57 -07007210 * from any ht related info since 2.4 does not
7211 * support ht */
Tomas Winkler78330fd2008-02-06 02:37:18 +02007212 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
Zhu Yib481de92007-09-25 17:54:57 -07007213#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7214 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7215#endif
7216 )
7217 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007218#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007219
Johannes Berg8318d782008-01-24 19:38:38 +01007220 iwl4965_set_rxon_channel(priv, conf->channel->band,
7221 ieee80211_frequency_to_channel(conf->channel->center_freq));
Zhu Yib481de92007-09-25 17:54:57 -07007222
Johannes Berg8318d782008-01-24 19:38:38 +01007223 iwl4965_set_flags_for_phymode(priv, conf->channel->band);
Zhu Yib481de92007-09-25 17:54:57 -07007224
7225 /* The list of supported rates and rate mask can be different
Johannes Berg8318d782008-01-24 19:38:38 +01007226 * for each band; since the band may have changed, reset
Zhu Yib481de92007-09-25 17:54:57 -07007227 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007228 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007229
7230 spin_unlock_irqrestore(&priv->lock, flags);
7231
7232#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7233 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007234 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007235 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007236 }
7237#endif
7238
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007239 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07007240
7241 if (!conf->radio_enabled) {
7242 IWL_DEBUG_MAC80211("leave - radio disabled\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007243 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007244 }
7245
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007246 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007247 IWL_DEBUG_MAC80211("leave - RF kill\n");
Zhu Yi76bb77e2007-11-22 10:53:22 +08007248 ret = -EIO;
7249 goto out;
Zhu Yib481de92007-09-25 17:54:57 -07007250 }
7251
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007252 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007253
7254 if (memcmp(&priv->active_rxon,
7255 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007256 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007257 else
7258 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7259
7260 IWL_DEBUG_MAC80211("leave\n");
7261
Zhu Yia0646472007-12-20 14:10:01 +08007262out:
7263 clear_bit(STATUS_CONF_PENDING, &priv->status);
Zhu Yi5a66926a2008-01-14 17:46:18 -08007264 mutex_unlock(&priv->mutex);
Zhu Yi76bb77e2007-11-22 10:53:22 +08007265 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07007266}
7267
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007268static void iwl4965_config_ap(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007269{
7270 int rc = 0;
7271
Maarten Lankhorstd986bcd2008-01-23 10:15:16 -08007272 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Zhu Yib481de92007-09-25 17:54:57 -07007273 return;
7274
7275 /* The following should be done only at AP bring up */
7276 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7277
7278 /* RXON - unassoc (to set timing command) */
7279 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007280 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007281
7282 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007283 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7284 iwl4965_setup_rxon_timing(priv);
7285 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07007286 sizeof(priv->rxon_timing), &priv->rxon_timing);
7287 if (rc)
7288 IWL_WARNING("REPLY_RXON_TIMING failed - "
7289 "Attempting to continue.\n");
7290
7291 iwl4965_set_rxon_chain(priv);
7292
7293 /* FIXME: what should be the assoc_id for AP? */
7294 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7295 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7296 priv->staging_rxon.flags |=
7297 RXON_FLG_SHORT_PREAMBLE_MSK;
7298 else
7299 priv->staging_rxon.flags &=
7300 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7301
7302 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7303 if (priv->assoc_capability &
7304 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7305 priv->staging_rxon.flags |=
7306 RXON_FLG_SHORT_SLOT_MSK;
7307 else
7308 priv->staging_rxon.flags &=
7309 ~RXON_FLG_SHORT_SLOT_MSK;
7310
7311 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7312 priv->staging_rxon.flags &=
7313 ~RXON_FLG_SHORT_SLOT_MSK;
7314 }
7315 /* restore RXON assoc */
7316 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007317 iwl4965_commit_rxon(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007318 iwl4965_activate_qos(priv, 1);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007319 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08007320 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007321 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007322
7323 /* FIXME - we need to add code here to detect a totally new
7324 * configuration, reset the AP, unassoc, rxon timing, assoc,
7325 * clear sta table, add BCAST sta... */
7326}
7327
Johannes Berg32bfd352007-12-19 01:31:26 +01007328static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
7329 struct ieee80211_vif *vif,
Zhu Yib481de92007-09-25 17:54:57 -07007330 struct ieee80211_if_conf *conf)
7331{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007332 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007333 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007334 unsigned long flags;
7335 int rc;
7336
7337 if (conf == NULL)
7338 return -EIO;
7339
Emmanuel Grumbachb716bb92008-03-04 18:09:32 -08007340 if (priv->vif != vif) {
7341 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
7342 mutex_unlock(&priv->mutex);
7343 return 0;
7344 }
7345
Zhu Yib481de92007-09-25 17:54:57 -07007346 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7347 (!conf->beacon || !conf->ssid_len)) {
7348 IWL_DEBUG_MAC80211
7349 ("Leaving in AP mode because HostAPD is not ready.\n");
7350 return 0;
7351 }
7352
Zhu Yi5a66926a2008-01-14 17:46:18 -08007353 if (!iwl4965_is_alive(priv))
7354 return -EAGAIN;
7355
Zhu Yib481de92007-09-25 17:54:57 -07007356 mutex_lock(&priv->mutex);
7357
Zhu Yib481de92007-09-25 17:54:57 -07007358 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07007359 IWL_DEBUG_MAC80211("bssid: %s\n",
7360 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007361
Johannes Berg4150c572007-09-17 01:29:23 -04007362/*
7363 * very dubious code was here; the probe filtering flag is never set:
7364 *
Zhu Yib481de92007-09-25 17:54:57 -07007365 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7366 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04007367 */
Zhu Yib481de92007-09-25 17:54:57 -07007368
7369 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7370 if (!conf->bssid) {
7371 conf->bssid = priv->mac_addr;
7372 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07007373 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7374 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007375 }
7376 if (priv->ibss_beacon)
7377 dev_kfree_skb(priv->ibss_beacon);
7378
7379 priv->ibss_beacon = conf->beacon;
7380 }
7381
Mohamed Abbasfde35712007-11-29 11:10:15 +08007382 if (iwl4965_is_rfkill(priv))
7383 goto done;
7384
Zhu Yib481de92007-09-25 17:54:57 -07007385 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7386 !is_multicast_ether_addr(conf->bssid)) {
7387 /* If there is currently a HW scan going on in the background
7388 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007389 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07007390 IWL_WARNING("Aborted scan still in progress "
7391 "after 100ms\n");
7392 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7393 mutex_unlock(&priv->mutex);
7394 return -EAGAIN;
7395 }
7396 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7397
7398 /* TODO: Audit driver for usage of these members and see
7399 * if mac80211 deprecates them (priv->bssid looks like it
7400 * shouldn't be there, but I haven't scanned the IBSS code
7401 * to verify) - jpk */
7402 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7403
7404 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007405 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007406 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007407 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007408 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007409 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07007410 priv, priv->active_rxon.bssid_addr, 1);
7411 }
7412
7413 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007414 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07007415 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007416 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007417 }
7418
Mohamed Abbasfde35712007-11-29 11:10:15 +08007419 done:
Zhu Yib481de92007-09-25 17:54:57 -07007420 spin_lock_irqsave(&priv->lock, flags);
7421 if (!conf->ssid_len)
7422 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7423 else
7424 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7425
7426 priv->essid_len = conf->ssid_len;
7427 spin_unlock_irqrestore(&priv->lock, flags);
7428
7429 IWL_DEBUG_MAC80211("leave\n");
7430 mutex_unlock(&priv->mutex);
7431
7432 return 0;
7433}
7434
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007435static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04007436 unsigned int changed_flags,
7437 unsigned int *total_flags,
7438 int mc_count, struct dev_addr_list *mc_list)
7439{
7440 /*
7441 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007442 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04007443 */
7444 *total_flags = 0;
7445}
7446
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007447static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007448 struct ieee80211_if_init_conf *conf)
7449{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007450 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007451
7452 IWL_DEBUG_MAC80211("enter\n");
7453
7454 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007455
Mohamed Abbasfde35712007-11-29 11:10:15 +08007456 if (iwl4965_is_ready_rf(priv)) {
7457 iwl4965_scan_cancel_timeout(priv, 100);
7458 cancel_delayed_work(&priv->post_associate);
7459 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
7460 iwl4965_commit_rxon(priv);
7461 }
Johannes Berg32bfd352007-12-19 01:31:26 +01007462 if (priv->vif == conf->vif) {
7463 priv->vif = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07007464 memset(priv->bssid, 0, ETH_ALEN);
7465 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7466 priv->essid_len = 0;
7467 }
7468 mutex_unlock(&priv->mutex);
7469
7470 IWL_DEBUG_MAC80211("leave\n");
7471
7472}
Johannes Berg471b3ef2007-12-28 14:32:58 +01007473
7474static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
7475 struct ieee80211_vif *vif,
7476 struct ieee80211_bss_conf *bss_conf,
7477 u32 changes)
Tomas Winkler220173b2007-10-16 00:50:25 +02007478{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007479 struct iwl_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02007480
Johannes Berg471b3ef2007-12-28 14:32:58 +01007481 if (changes & BSS_CHANGED_ERP_PREAMBLE) {
7482 if (bss_conf->use_short_preamble)
Tomas Winkler220173b2007-10-16 00:50:25 +02007483 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7484 else
7485 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7486 }
7487
Johannes Berg471b3ef2007-12-28 14:32:58 +01007488 if (changes & BSS_CHANGED_ERP_CTS_PROT) {
Johannes Berg8318d782008-01-24 19:38:38 +01007489 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
Tomas Winkler220173b2007-10-16 00:50:25 +02007490 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
7491 else
7492 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
7493 }
7494
Johannes Berg471b3ef2007-12-28 14:32:58 +01007495 if (changes & BSS_CHANGED_ASSOC) {
7496 /*
7497 * TODO:
7498 * do stuff instead of sniffing assoc resp
7499 */
7500 }
7501
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007502 if (iwl4965_is_associated(priv))
7503 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02007504}
Zhu Yib481de92007-09-25 17:54:57 -07007505
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007506static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07007507{
7508 int rc = 0;
7509 unsigned long flags;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007510 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007511
7512 IWL_DEBUG_MAC80211("enter\n");
7513
mabbas052c4b92007-10-25 17:15:43 +08007514 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007515 spin_lock_irqsave(&priv->lock, flags);
7516
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007517 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007518 rc = -EIO;
7519 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7520 goto out_unlock;
7521 }
7522
7523 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7524 rc = -EIO;
7525 IWL_ERROR("ERROR: APs don't scan\n");
7526 goto out_unlock;
7527 }
7528
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007529 /* we don't schedule scan within next_scan_jiffies period */
7530 if (priv->next_scan_jiffies &&
7531 time_after(priv->next_scan_jiffies, jiffies)) {
7532 rc = -EAGAIN;
7533 goto out_unlock;
7534 }
Zhu Yib481de92007-09-25 17:54:57 -07007535 /* if we just finished scan ask for delay */
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007536 if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
7537 IWL_DELAY_NEXT_SCAN, jiffies)) {
Zhu Yib481de92007-09-25 17:54:57 -07007538 rc = -EAGAIN;
7539 goto out_unlock;
7540 }
7541 if (len) {
Mohamed Abbas7878a5a2007-11-29 11:10:13 +08007542 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007543 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007544
7545 priv->one_direct_scan = 1;
7546 priv->direct_ssid_len = (u8)
7547 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7548 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007549 } else
7550 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007551
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007552 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007553
7554 IWL_DEBUG_MAC80211("leave\n");
7555
7556out_unlock:
7557 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08007558 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007559
7560 return rc;
7561}
7562
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007563static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007564 const u8 *local_addr, const u8 *addr,
7565 struct ieee80211_key_conf *key)
7566{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007567 struct iwl_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007568 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007569 int rc = 0;
7570 u8 sta_id;
7571
7572 IWL_DEBUG_MAC80211("enter\n");
7573
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007574 if (!iwl4965_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007575 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7576 return -EOPNOTSUPP;
7577 }
7578
7579 if (is_zero_ether_addr(addr))
7580 /* only support pairwise keys */
7581 return -EOPNOTSUPP;
7582
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007583 sta_id = iwl4965_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07007584 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07007585 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7586 print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -07007587 return -EINVAL;
7588 }
7589
7590 mutex_lock(&priv->mutex);
7591
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007592 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007593
Zhu Yib481de92007-09-25 17:54:57 -07007594 switch (cmd) {
7595 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007596 rc = iwl4965_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007597 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007598 iwl4965_set_rxon_hwcrypto(priv, 1);
7599 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007600 key->hw_key_idx = sta_id;
7601 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7602 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7603 }
7604 break;
7605 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007606 rc = iwl4965_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007607 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007608 iwl4965_set_rxon_hwcrypto(priv, 0);
7609 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007610 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7611 }
7612 break;
7613 default:
7614 rc = -EINVAL;
7615 }
7616
7617 IWL_DEBUG_MAC80211("leave\n");
7618 mutex_unlock(&priv->mutex);
7619
7620 return rc;
7621}
7622
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007623static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007624 const struct ieee80211_tx_queue_params *params)
7625{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007626 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007627 unsigned long flags;
7628 int q;
Zhu Yib481de92007-09-25 17:54:57 -07007629
7630 IWL_DEBUG_MAC80211("enter\n");
7631
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007632 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007633 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7634 return -EIO;
7635 }
7636
7637 if (queue >= AC_NUM) {
7638 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7639 return 0;
7640 }
7641
Zhu Yib481de92007-09-25 17:54:57 -07007642 if (!priv->qos_data.qos_enable) {
7643 priv->qos_data.qos_active = 0;
7644 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7645 return 0;
7646 }
7647 q = AC_NUM - 1 - queue;
7648
7649 spin_lock_irqsave(&priv->lock, flags);
7650
7651 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7652 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7653 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7654 priv->qos_data.def_qos_parm.ac[q].edca_txop =
Johannes Berg3330d7b2008-02-10 16:49:38 +01007655 cpu_to_le16((params->txop * 32));
Zhu Yib481de92007-09-25 17:54:57 -07007656
7657 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7658 priv->qos_data.qos_active = 1;
7659
7660 spin_unlock_irqrestore(&priv->lock, flags);
7661
7662 mutex_lock(&priv->mutex);
7663 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007664 iwl4965_activate_qos(priv, 1);
7665 else if (priv->assoc_id && iwl4965_is_associated(priv))
7666 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007667
7668 mutex_unlock(&priv->mutex);
7669
Zhu Yib481de92007-09-25 17:54:57 -07007670 IWL_DEBUG_MAC80211("leave\n");
7671 return 0;
7672}
7673
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007674static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007675 struct ieee80211_tx_queue_stats *stats)
7676{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007677 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007678 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007679 struct iwl4965_tx_queue *txq;
7680 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007681 unsigned long flags;
7682
7683 IWL_DEBUG_MAC80211("enter\n");
7684
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007685 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007686 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7687 return -EIO;
7688 }
7689
7690 spin_lock_irqsave(&priv->lock, flags);
7691
7692 for (i = 0; i < AC_NUM; i++) {
7693 txq = &priv->txq[i];
7694 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007695 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007696
7697 stats->data[i].len = q->n_window - avail;
7698 stats->data[i].limit = q->n_window - q->high_mark;
7699 stats->data[i].count = q->n_window;
7700
7701 }
7702 spin_unlock_irqrestore(&priv->lock, flags);
7703
7704 IWL_DEBUG_MAC80211("leave\n");
7705
7706 return 0;
7707}
7708
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007709static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007710 struct ieee80211_low_level_stats *stats)
7711{
7712 IWL_DEBUG_MAC80211("enter\n");
7713 IWL_DEBUG_MAC80211("leave\n");
7714
7715 return 0;
7716}
7717
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007718static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007719{
7720 IWL_DEBUG_MAC80211("enter\n");
7721 IWL_DEBUG_MAC80211("leave\n");
7722
7723 return 0;
7724}
7725
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007726static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007727{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007728 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007729 unsigned long flags;
7730
7731 mutex_lock(&priv->mutex);
7732 IWL_DEBUG_MAC80211("enter\n");
7733
7734 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007735#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007736 spin_lock_irqsave(&priv->lock, flags);
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007737 memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
Zhu Yib481de92007-09-25 17:54:57 -07007738 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007739#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007740
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007741 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007742
7743 cancel_delayed_work(&priv->post_associate);
7744
7745 spin_lock_irqsave(&priv->lock, flags);
7746 priv->assoc_id = 0;
7747 priv->assoc_capability = 0;
7748 priv->call_post_assoc_from_beacon = 0;
7749 priv->assoc_station_added = 0;
7750
7751 /* new association get rid of ibss beacon skb */
7752 if (priv->ibss_beacon)
7753 dev_kfree_skb(priv->ibss_beacon);
7754
7755 priv->ibss_beacon = NULL;
7756
7757 priv->beacon_int = priv->hw->conf.beacon_int;
7758 priv->timestamp1 = 0;
7759 priv->timestamp0 = 0;
7760 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7761 priv->beacon_int = 0;
7762
7763 spin_unlock_irqrestore(&priv->lock, flags);
7764
Mohamed Abbasfde35712007-11-29 11:10:15 +08007765 if (!iwl4965_is_ready_rf(priv)) {
7766 IWL_DEBUG_MAC80211("leave - not ready\n");
7767 mutex_unlock(&priv->mutex);
7768 return;
7769 }
7770
mabbas052c4b92007-10-25 17:15:43 +08007771 /* we are restarting association process
7772 * clear RXON_FILTER_ASSOC_MSK bit
7773 */
7774 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007775 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007776 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007777 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007778 }
7779
Zhu Yib481de92007-09-25 17:54:57 -07007780 /* Per mac80211.h: This is only used in IBSS mode... */
7781 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007782
Zhu Yib481de92007-09-25 17:54:57 -07007783 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7784 mutex_unlock(&priv->mutex);
7785 return;
7786 }
7787
Zhu Yib481de92007-09-25 17:54:57 -07007788 priv->only_active_channel = 0;
7789
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007790 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007791
7792 mutex_unlock(&priv->mutex);
7793
7794 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007795}
7796
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007797static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007798 struct ieee80211_tx_control *control)
7799{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007800 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007801 unsigned long flags;
7802
7803 mutex_lock(&priv->mutex);
7804 IWL_DEBUG_MAC80211("enter\n");
7805
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007806 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007807 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7808 mutex_unlock(&priv->mutex);
7809 return -EIO;
7810 }
7811
7812 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7813 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7814 mutex_unlock(&priv->mutex);
7815 return -EIO;
7816 }
7817
7818 spin_lock_irqsave(&priv->lock, flags);
7819
7820 if (priv->ibss_beacon)
7821 dev_kfree_skb(priv->ibss_beacon);
7822
7823 priv->ibss_beacon = skb;
7824
7825 priv->assoc_id = 0;
7826
7827 IWL_DEBUG_MAC80211("leave\n");
7828 spin_unlock_irqrestore(&priv->lock, flags);
7829
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007830 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007831
7832 queue_work(priv->workqueue, &priv->post_associate.work);
7833
7834 mutex_unlock(&priv->mutex);
7835
7836 return 0;
7837}
7838
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007839#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007840
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007841static void iwl4965_ht_info_fill(struct ieee80211_conf *conf,
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007842 struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007843{
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007844 struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
7845 struct ieee80211_ht_info *ht_conf = &conf->ht_conf;
7846 struct ieee80211_ht_bss_info *ht_bss_conf = &conf->ht_bss_conf;
Zhu Yib481de92007-09-25 17:54:57 -07007847
7848 IWL_DEBUG_MAC80211("enter: \n");
7849
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007850 if (!(conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE)) {
7851 iwl_conf->is_ht = 0;
7852 return;
Zhu Yib481de92007-09-25 17:54:57 -07007853 }
7854
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007855 iwl_conf->is_ht = 1;
7856 priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
Zhu Yib481de92007-09-25 17:54:57 -07007857
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007858 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
7859 iwl_conf->sgf |= 0x1;
7860 if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
7861 iwl_conf->sgf |= 0x2;
Zhu Yib481de92007-09-25 17:54:57 -07007862
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007863 iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
7864 iwl_conf->max_amsdu_size =
7865 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007866
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007867 iwl_conf->supported_chan_width =
7868 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
Guy Cohen134eb5d2008-03-04 18:09:25 -08007869 iwl_conf->extension_chan_offset =
7870 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
7871 /* If no above or below channel supplied disable FAT channel */
7872 if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
7873 iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
7874 iwl_conf->supported_chan_width = 0;
7875
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007876 iwl_conf->tx_mimo_ps_mode =
7877 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
7878 memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
Zhu Yib481de92007-09-25 17:54:57 -07007879
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007880 iwl_conf->control_channel = ht_bss_conf->primary_channel;
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007881 iwl_conf->tx_chan_width =
7882 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
7883 iwl_conf->ht_protection =
7884 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
7885 iwl_conf->non_GF_STA_present =
7886 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
Zhu Yib481de92007-09-25 17:54:57 -07007887
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007888 IWL_DEBUG_MAC80211("control channel %d\n",
7889 iwl_conf->control_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007890 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007891}
7892
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007893static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007894 struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007895{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007896 struct iwl_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007897
7898 IWL_DEBUG_MAC80211("enter: \n");
7899
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007900 iwl4965_ht_info_fill(conf, priv);
Zhu Yib481de92007-09-25 17:54:57 -07007901 iwl4965_set_rxon_chain(priv);
7902
7903 if (priv && priv->assoc_id &&
7904 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
7905 unsigned long flags;
7906
7907 spin_lock_irqsave(&priv->lock, flags);
7908 if (priv->beacon_int)
7909 queue_work(priv->workqueue, &priv->post_associate.work);
7910 else
7911 priv->call_post_assoc_from_beacon = 1;
7912 spin_unlock_irqrestore(&priv->lock, flags);
7913 }
7914
Ron Rindjunskyfd105e72007-11-26 16:14:39 +02007915 IWL_DEBUG_MAC80211("leave:\n");
7916 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07007917}
7918
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007919#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07007920
7921/*****************************************************************************
7922 *
7923 * sysfs attributes
7924 *
7925 *****************************************************************************/
7926
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007927#ifdef CONFIG_IWLWIFI_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07007928
7929/*
7930 * The following adds a new attribute to the sysfs representation
7931 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
7932 * used for controlling the debug level.
7933 *
7934 * See the level definitions in iwl for details.
7935 */
7936
7937static ssize_t show_debug_level(struct device_driver *d, char *buf)
7938{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007939 return sprintf(buf, "0x%08X\n", iwl_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07007940}
7941static ssize_t store_debug_level(struct device_driver *d,
7942 const char *buf, size_t count)
7943{
7944 char *p = (char *)buf;
7945 u32 val;
7946
7947 val = simple_strtoul(p, &p, 0);
7948 if (p == buf)
7949 printk(KERN_INFO DRV_NAME
7950 ": %s is not in hex or decimal form.\n", buf);
7951 else
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007952 iwl_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07007953
7954 return strnlen(buf, count);
7955}
7956
7957static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
7958 show_debug_level, store_debug_level);
7959
Tomas Winkler0a6857e2008-03-12 16:58:49 -07007960#endif /* CONFIG_IWLWIFI_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07007961
7962static ssize_t show_rf_kill(struct device *d,
7963 struct device_attribute *attr, char *buf)
7964{
7965 /*
7966 * 0 - RF kill not enabled
7967 * 1 - SW based RF kill active (sysfs)
7968 * 2 - HW based RF kill active
7969 * 3 - Both HW and SW based RF kill active
7970 */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007971 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007972 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
7973 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
7974
7975 return sprintf(buf, "%i\n", val);
7976}
7977
7978static ssize_t store_rf_kill(struct device *d,
7979 struct device_attribute *attr,
7980 const char *buf, size_t count)
7981{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007982 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007983
7984 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007985 iwl4965_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07007986 mutex_unlock(&priv->mutex);
7987
7988 return count;
7989}
7990
7991static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
7992
7993static ssize_t show_temperature(struct device *d,
7994 struct device_attribute *attr, char *buf)
7995{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07007996 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07007997
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007998 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007999 return -EAGAIN;
8000
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008001 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07008002}
8003
8004static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
8005
8006static ssize_t show_rs_window(struct device *d,
8007 struct device_attribute *attr,
8008 char *buf)
8009{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008010 struct iwl_priv *priv = d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008011 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07008012}
8013static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
8014
8015static ssize_t show_tx_power(struct device *d,
8016 struct device_attribute *attr, char *buf)
8017{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008018 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008019 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8020}
8021
8022static ssize_t store_tx_power(struct device *d,
8023 struct device_attribute *attr,
8024 const char *buf, size_t count)
8025{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008026 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008027 char *p = (char *)buf;
8028 u32 val;
8029
8030 val = simple_strtoul(p, &p, 10);
8031 if (p == buf)
8032 printk(KERN_INFO DRV_NAME
8033 ": %s is not in decimal form.\n", buf);
8034 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008035 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07008036
8037 return count;
8038}
8039
8040static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8041
8042static ssize_t show_flags(struct device *d,
8043 struct device_attribute *attr, char *buf)
8044{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008045 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008046
8047 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8048}
8049
8050static ssize_t store_flags(struct device *d,
8051 struct device_attribute *attr,
8052 const char *buf, size_t count)
8053{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008054 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008055 u32 flags = simple_strtoul(buf, NULL, 0);
8056
8057 mutex_lock(&priv->mutex);
8058 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8059 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008060 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008061 IWL_WARNING("Could not cancel scan.\n");
8062 else {
8063 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8064 flags);
8065 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008066 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008067 }
8068 }
8069 mutex_unlock(&priv->mutex);
8070
8071 return count;
8072}
8073
8074static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8075
8076static ssize_t show_filter_flags(struct device *d,
8077 struct device_attribute *attr, char *buf)
8078{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008079 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008080
8081 return sprintf(buf, "0x%04X\n",
8082 le32_to_cpu(priv->active_rxon.filter_flags));
8083}
8084
8085static ssize_t store_filter_flags(struct device *d,
8086 struct device_attribute *attr,
8087 const char *buf, size_t count)
8088{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008089 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008090 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8091
8092 mutex_lock(&priv->mutex);
8093 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8094 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008095 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008096 IWL_WARNING("Could not cancel scan.\n");
8097 else {
8098 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8099 "0x%04X\n", filter_flags);
8100 priv->staging_rxon.filter_flags =
8101 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008102 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008103 }
8104 }
8105 mutex_unlock(&priv->mutex);
8106
8107 return count;
8108}
8109
8110static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8111 store_filter_flags);
8112
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008113#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008114
8115static ssize_t show_measurement(struct device *d,
8116 struct device_attribute *attr, char *buf)
8117{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008118 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008119 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07008120 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8121 u8 *data = (u8 *) & measure_report;
8122 unsigned long flags;
8123
8124 spin_lock_irqsave(&priv->lock, flags);
8125 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8126 spin_unlock_irqrestore(&priv->lock, flags);
8127 return 0;
8128 }
8129 memcpy(&measure_report, &priv->measure_report, size);
8130 priv->measurement_status = 0;
8131 spin_unlock_irqrestore(&priv->lock, flags);
8132
8133 while (size && (PAGE_SIZE - len)) {
8134 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8135 PAGE_SIZE - len, 1);
8136 len = strlen(buf);
8137 if (PAGE_SIZE - len)
8138 buf[len++] = '\n';
8139
8140 ofs += 16;
8141 size -= min(size, 16U);
8142 }
8143
8144 return len;
8145}
8146
8147static ssize_t store_measurement(struct device *d,
8148 struct device_attribute *attr,
8149 const char *buf, size_t count)
8150{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008151 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008152 struct ieee80211_measurement_params params = {
8153 .channel = le16_to_cpu(priv->active_rxon.channel),
8154 .start_time = cpu_to_le64(priv->last_tsf),
8155 .duration = cpu_to_le16(1),
8156 };
8157 u8 type = IWL_MEASURE_BASIC;
8158 u8 buffer[32];
8159 u8 channel;
8160
8161 if (count) {
8162 char *p = buffer;
8163 strncpy(buffer, buf, min(sizeof(buffer), count));
8164 channel = simple_strtoul(p, NULL, 0);
8165 if (channel)
8166 params.channel = channel;
8167
8168 p = buffer;
8169 while (*p && *p != ' ')
8170 p++;
8171 if (*p)
8172 type = simple_strtoul(p + 1, NULL, 0);
8173 }
8174
8175 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8176 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008177 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07008178
8179 return count;
8180}
8181
8182static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8183 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008184#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07008185
8186static ssize_t store_retry_rate(struct device *d,
8187 struct device_attribute *attr,
8188 const char *buf, size_t count)
8189{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008190 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008191
8192 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8193 if (priv->retry_rate <= 0)
8194 priv->retry_rate = 1;
8195
8196 return count;
8197}
8198
8199static ssize_t show_retry_rate(struct device *d,
8200 struct device_attribute *attr, char *buf)
8201{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008202 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008203 return sprintf(buf, "%d", priv->retry_rate);
8204}
8205
8206static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8207 store_retry_rate);
8208
8209static ssize_t store_power_level(struct device *d,
8210 struct device_attribute *attr,
8211 const char *buf, size_t count)
8212{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008213 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008214 int rc;
8215 int mode;
8216
8217 mode = simple_strtoul(buf, NULL, 0);
8218 mutex_lock(&priv->mutex);
8219
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008220 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008221 rc = -EAGAIN;
8222 goto out;
8223 }
8224
8225 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8226 mode = IWL_POWER_AC;
8227 else
8228 mode |= IWL_POWER_ENABLED;
8229
8230 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008231 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07008232 if (rc) {
8233 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8234 goto out;
8235 }
8236 priv->power_mode = mode;
8237 }
8238
8239 rc = count;
8240
8241 out:
8242 mutex_unlock(&priv->mutex);
8243 return rc;
8244}
8245
8246#define MAX_WX_STRING 80
8247
8248/* Values are in microsecond */
8249static const s32 timeout_duration[] = {
8250 350000,
8251 250000,
8252 75000,
8253 37000,
8254 25000,
8255};
8256static const s32 period_duration[] = {
8257 400000,
8258 700000,
8259 1000000,
8260 1000000,
8261 1000000
8262};
8263
8264static ssize_t show_power_level(struct device *d,
8265 struct device_attribute *attr, char *buf)
8266{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008267 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008268 int level = IWL_POWER_LEVEL(priv->power_mode);
8269 char *p = buf;
8270
8271 p += sprintf(p, "%d ", level);
8272 switch (level) {
8273 case IWL_POWER_MODE_CAM:
8274 case IWL_POWER_AC:
8275 p += sprintf(p, "(AC)");
8276 break;
8277 case IWL_POWER_BATTERY:
8278 p += sprintf(p, "(BATTERY)");
8279 break;
8280 default:
8281 p += sprintf(p,
8282 "(Timeout %dms, Period %dms)",
8283 timeout_duration[level - 1] / 1000,
8284 period_duration[level - 1] / 1000);
8285 }
8286
8287 if (!(priv->power_mode & IWL_POWER_ENABLED))
8288 p += sprintf(p, " OFF\n");
8289 else
8290 p += sprintf(p, " \n");
8291
8292 return (p - buf + 1);
8293
8294}
8295
8296static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8297 store_power_level);
8298
8299static ssize_t show_channels(struct device *d,
8300 struct device_attribute *attr, char *buf)
8301{
Johannes Berg8318d782008-01-24 19:38:38 +01008302 /* all this shit doesn't belong into sysfs anyway */
8303 return 0;
Zhu Yib481de92007-09-25 17:54:57 -07008304}
8305
8306static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8307
8308static ssize_t show_statistics(struct device *d,
8309 struct device_attribute *attr, char *buf)
8310{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008311 struct iwl_priv *priv = dev_get_drvdata(d);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008312 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07008313 u32 len = 0, ofs = 0;
8314 u8 *data = (u8 *) & priv->statistics;
8315 int rc = 0;
8316
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008317 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008318 return -EAGAIN;
8319
8320 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008321 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008322 mutex_unlock(&priv->mutex);
8323
8324 if (rc) {
8325 len = sprintf(buf,
8326 "Error sending statistics request: 0x%08X\n", rc);
8327 return len;
8328 }
8329
8330 while (size && (PAGE_SIZE - len)) {
8331 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8332 PAGE_SIZE - len, 1);
8333 len = strlen(buf);
8334 if (PAGE_SIZE - len)
8335 buf[len++] = '\n';
8336
8337 ofs += 16;
8338 size -= min(size, 16U);
8339 }
8340
8341 return len;
8342}
8343
8344static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8345
8346static ssize_t show_antenna(struct device *d,
8347 struct device_attribute *attr, char *buf)
8348{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008349 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008350
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008351 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008352 return -EAGAIN;
8353
8354 return sprintf(buf, "%d\n", priv->antenna);
8355}
8356
8357static ssize_t store_antenna(struct device *d,
8358 struct device_attribute *attr,
8359 const char *buf, size_t count)
8360{
8361 int ant;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008362 struct iwl_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008363
8364 if (count == 0)
8365 return 0;
8366
8367 if (sscanf(buf, "%1i", &ant) != 1) {
8368 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8369 return count;
8370 }
8371
8372 if ((ant >= 0) && (ant <= 2)) {
8373 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008374 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07008375 } else
8376 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8377
8378
8379 return count;
8380}
8381
8382static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8383
8384static ssize_t show_status(struct device *d,
8385 struct device_attribute *attr, char *buf)
8386{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008387 struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008388 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008389 return -EAGAIN;
8390 return sprintf(buf, "0x%08x\n", (int)priv->status);
8391}
8392
8393static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8394
8395static ssize_t dump_error_log(struct device *d,
8396 struct device_attribute *attr,
8397 const char *buf, size_t count)
8398{
8399 char *p = (char *)buf;
8400
8401 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008402 iwl4965_dump_nic_error_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008403
8404 return strnlen(buf, count);
8405}
8406
8407static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8408
8409static ssize_t dump_event_log(struct device *d,
8410 struct device_attribute *attr,
8411 const char *buf, size_t count)
8412{
8413 char *p = (char *)buf;
8414
8415 if (p[0] == '1')
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008416 iwl4965_dump_nic_event_log((struct iwl_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008417
8418 return strnlen(buf, count);
8419}
8420
8421static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8422
8423/*****************************************************************************
8424 *
8425 * driver setup and teardown
8426 *
8427 *****************************************************************************/
8428
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008429static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008430{
8431 priv->workqueue = create_workqueue(DRV_NAME);
8432
8433 init_waitqueue_head(&priv->wait_command_queue);
8434
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008435 INIT_WORK(&priv->up, iwl4965_bg_up);
8436 INIT_WORK(&priv->restart, iwl4965_bg_restart);
8437 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
8438 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
8439 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
8440 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
8441 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
8442 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
8443 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
8444 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
8445 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
8446 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07008447
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008448 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008449
8450 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008451 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07008452}
8453
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008454static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008455{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008456 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008457
Joonwoo Park3ae6a052007-11-29 10:43:16 +09008458 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07008459 cancel_delayed_work(&priv->scan_check);
8460 cancel_delayed_work(&priv->alive_start);
8461 cancel_delayed_work(&priv->post_associate);
8462 cancel_work_sync(&priv->beacon_update);
8463}
8464
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008465static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07008466 &dev_attr_antenna.attr,
8467 &dev_attr_channels.attr,
8468 &dev_attr_dump_errors.attr,
8469 &dev_attr_dump_events.attr,
8470 &dev_attr_flags.attr,
8471 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008472#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008473 &dev_attr_measurement.attr,
8474#endif
8475 &dev_attr_power_level.attr,
8476 &dev_attr_retry_rate.attr,
8477 &dev_attr_rf_kill.attr,
8478 &dev_attr_rs_window.attr,
8479 &dev_attr_statistics.attr,
8480 &dev_attr_status.attr,
8481 &dev_attr_temperature.attr,
Zhu Yib481de92007-09-25 17:54:57 -07008482 &dev_attr_tx_power.attr,
8483
8484 NULL
8485};
8486
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008487static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07008488 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008489 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07008490};
8491
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008492static struct ieee80211_ops iwl4965_hw_ops = {
8493 .tx = iwl4965_mac_tx,
8494 .start = iwl4965_mac_start,
8495 .stop = iwl4965_mac_stop,
8496 .add_interface = iwl4965_mac_add_interface,
8497 .remove_interface = iwl4965_mac_remove_interface,
8498 .config = iwl4965_mac_config,
8499 .config_interface = iwl4965_mac_config_interface,
8500 .configure_filter = iwl4965_configure_filter,
8501 .set_key = iwl4965_mac_set_key,
8502 .get_stats = iwl4965_mac_get_stats,
8503 .get_tx_stats = iwl4965_mac_get_tx_stats,
8504 .conf_tx = iwl4965_mac_conf_tx,
8505 .get_tsf = iwl4965_mac_get_tsf,
8506 .reset_tsf = iwl4965_mac_reset_tsf,
8507 .beacon_update = iwl4965_mac_beacon_update,
Johannes Berg471b3ef2007-12-28 14:32:58 +01008508 .bss_info_changed = iwl4965_bss_info_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008509#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008510 .conf_ht = iwl4965_mac_conf_ht,
Ron Rindjunsky9ab46172007-12-25 17:00:38 +02008511 .ampdu_action = iwl4965_mac_ampdu_action,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008512#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008513 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07008514};
8515
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008516static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008517{
8518 int err = 0;
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008519 struct iwl_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008520 struct ieee80211_hw *hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008521 struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008522 int i;
Zhu Yi5a66926a2008-01-14 17:46:18 -08008523 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07008524
Assaf Krauss316c30d2008-03-14 10:38:46 -07008525 /************************
8526 * 1. Allocating HW data
8527 ************************/
8528
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008529 /* Disabling hardware scan means that mac80211 will perform scans
8530 * "the hard way", rather than using device's scan. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008531 if (iwl4965_param_disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008532 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008533 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008534 }
8535
Zhu Yib481de92007-09-25 17:54:57 -07008536 /* mac80211 allocates memory for this device instance, including
8537 * space for this driver's private structure */
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008538 hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl4965_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07008539 if (hw == NULL) {
8540 IWL_ERROR("Can not allocate network device\n");
8541 err = -ENOMEM;
8542 goto out;
8543 }
8544 SET_IEEE80211_DEV(hw, &pdev->dev);
8545
8546 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8547 priv = hw->priv;
8548 priv->hw = hw;
Tomas Winkler82b9a122008-03-04 18:09:30 -08008549 priv->cfg = cfg;
Zhu Yib481de92007-09-25 17:54:57 -07008550
8551 priv->pci_dev = pdev;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008552
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008553#ifdef CONFIG_IWLWIFI_DEBUG
8554 iwl_debug_level = iwl4965_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008555 atomic_set(&priv->restrict_refcnt, 0);
8556#endif
Zhu Yib481de92007-09-25 17:54:57 -07008557
Assaf Krauss316c30d2008-03-14 10:38:46 -07008558 /**************************
8559 * 2. Initializing PCI bus
8560 **************************/
8561 if (pci_enable_device(pdev)) {
8562 err = -ENODEV;
8563 goto out_ieee80211_free_hw;
8564 }
8565
8566 pci_set_master(pdev);
8567
8568 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
8569 if (!err)
8570 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
8571 if (err) {
8572 printk(KERN_WARNING DRV_NAME
8573 ": No suitable DMA available.\n");
8574 goto out_pci_disable_device;
8575 }
8576
8577 err = pci_request_regions(pdev, DRV_NAME);
8578 if (err)
8579 goto out_pci_disable_device;
8580
8581 pci_set_drvdata(pdev, priv);
8582
8583 /* We disable the RETRY_TIMEOUT register (0x41) to keep
8584 * PCI Tx retries from interfering with C3 CPU state */
8585 pci_write_config_byte(pdev, 0x41, 0x00);
8586
8587 /***********************
8588 * 3. Read REV register
8589 ***********************/
8590 priv->hw_base = pci_iomap(pdev, 0, 0);
8591 if (!priv->hw_base) {
8592 err = -ENODEV;
8593 goto out_pci_release_regions;
8594 }
8595
8596 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
8597 (unsigned long long) pci_resource_len(pdev, 0));
8598 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
8599
8600 printk(KERN_INFO DRV_NAME
8601 ": Detected Intel Wireless WiFi Link %s\n", priv->cfg->name);
8602
8603 /*****************
8604 * 4. Read EEPROM
8605 *****************/
8606 /* nic init */
8607 iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
8608 CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
8609
8610 iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
8611 err = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
8612 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
8613 CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
8614 if (err < 0) {
8615 IWL_DEBUG_INFO("Failed to init the card\n");
8616 goto out_iounmap;
8617 }
8618 /* Read the EEPROM */
8619 err = iwl_eeprom_init(priv);
8620 if (err) {
8621 IWL_ERROR("Unable to init EEPROM\n");
8622 goto out_iounmap;
8623 }
8624 /* MAC Address location in EEPROM same for 3945/4965 */
8625 iwl_eeprom_get_mac(priv, priv->mac_addr);
8626 IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
8627 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
8628
8629 /************************
8630 * 5. Setup HW constants
8631 ************************/
8632 /* Device-specific setup */
8633 if (iwl4965_hw_set_hw_setting(priv)) {
8634 IWL_ERROR("failed to set hw settings\n");
8635 goto out_iounmap;
8636 }
8637
8638 /*******************
8639 * 6. Setup hw/priv
8640 *******************/
Zhu Yib481de92007-09-25 17:54:57 -07008641
8642 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8643 * the range of signal quality values that we'll provide.
8644 * Negative values for level/noise indicate that we'll provide dBm.
8645 * For WE, at least, non-0 values here *enable* display of values
8646 * in app (iwconfig). */
8647 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8648 hw->max_noise = -20; /* noise level, negative indicates dBm */
8649 hw->max_signal = 100; /* link quality indication (%) */
8650
8651 /* Tell mac80211 our Tx characteristics */
8652 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8653
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008654 /* Default value; 4 EDCA QOS priorities */
Zhu Yib481de92007-09-25 17:54:57 -07008655 hw->queues = 4;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008656#ifdef CONFIG_IWL4965_HT
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008657 /* Enhanced value; more queues, to support 11n aggregation */
Zhu Yib481de92007-09-25 17:54:57 -07008658 hw->queues = 16;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008659#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07008660
Assaf Krauss316c30d2008-03-14 10:38:46 -07008661 hw->rate_control_algorithm = "iwl-4965-rs";
8662 priv->antenna = (enum iwl4965_antenna)iwl4965_param_antenna;
8663 priv->retry_rate = 1;
8664 priv->ibss_beacon = NULL;
8665
Zhu Yib481de92007-09-25 17:54:57 -07008666 spin_lock_init(&priv->lock);
8667 spin_lock_init(&priv->power_data.lock);
8668 spin_lock_init(&priv->sta_lock);
8669 spin_lock_init(&priv->hcmd_lock);
8670 spin_lock_init(&priv->lq_mngr.lock);
8671
8672 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
8673 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
8674
8675 INIT_LIST_HEAD(&priv->free_frames);
8676
8677 mutex_init(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07008678
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008679 /* Clear the driver's (not device's) station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008680 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008681
8682 priv->data_retry_limit = -1;
8683 priv->ieee_channels = NULL;
8684 priv->ieee_rates = NULL;
Johannes Berg8318d782008-01-24 19:38:38 +01008685 priv->band = IEEE80211_BAND_2GHZ;
Zhu Yib481de92007-09-25 17:54:57 -07008686
Zhu Yib481de92007-09-25 17:54:57 -07008687 priv->iw_mode = IEEE80211_IF_TYPE_STA;
8688
Zhu Yib481de92007-09-25 17:54:57 -07008689 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
Zhu Yib481de92007-09-25 17:54:57 -07008690 priv->valid_antenna = 0x7; /* assume all 3 connected */
8691 priv->ps_mode = IWL_MIMO_PS_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07008692
Cahill, Ben M6440adb2007-11-29 11:09:55 +08008693 /* Choose which receivers/antennas to use */
Zhu Yib481de92007-09-25 17:54:57 -07008694 iwl4965_set_rxon_chain(priv);
8695
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008696 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008697
8698 priv->qos_data.qos_active = 0;
8699 priv->qos_data.qos_cap.val = 0;
Zhu Yib481de92007-09-25 17:54:57 -07008700
Johannes Berg8318d782008-01-24 19:38:38 +01008701 iwl4965_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
Zhu Yib481de92007-09-25 17:54:57 -07008702
8703 priv->rates_mask = IWL_RATES_MASK;
8704 /* If power management is turned on, default to AC mode */
8705 priv->power_mode = IWL_POWER_AC;
8706 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
8707
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008708 err = iwl4965_init_channel_map(priv);
8709 if (err) {
8710 IWL_ERROR("initializing regulatory failed: %d\n", err);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008711 goto out_unset_hw_settings;
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008712 }
8713
8714 err = iwl4965_init_geos(priv);
8715 if (err) {
8716 IWL_ERROR("initializing geos failed: %d\n", err);
8717 goto out_free_channel_map;
8718 }
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008719
Zhu Yi5a66926a2008-01-14 17:46:18 -08008720 iwl4965_rate_control_register(priv->hw);
8721 err = ieee80211_register_hw(priv->hw);
8722 if (err) {
8723 IWL_ERROR("Failed to register network device (error %d)\n", err);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008724 goto out_free_geos;
Zhu Yib481de92007-09-25 17:54:57 -07008725 }
8726
Zhu Yi5a66926a2008-01-14 17:46:18 -08008727 priv->hw->conf.beacon_int = 100;
8728 priv->mac80211_registered = 1;
Assaf Krauss316c30d2008-03-14 10:38:46 -07008729
8730 /**********************************
8731 * 7. Initialize module parameters
8732 **********************************/
8733
8734 /* Disable radio (SW RF KILL) via parameter when loading driver */
8735 if (iwl4965_param_disable) {
8736 set_bit(STATUS_RF_KILL_SW, &priv->status);
8737 IWL_DEBUG_INFO("Radio disabled.\n");
8738 }
8739
8740 if (iwl4965_param_qos_enable)
8741 priv->qos_data.qos_enable = 1;
8742
8743 /********************
8744 * 8. Setup services
8745 ********************/
8746 iwl4965_disable_interrupts(priv);
8747
8748 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
8749 if (err) {
8750 IWL_ERROR("failed to create sysfs device attributes\n");
8751 goto out_free_geos;
8752 }
8753
8754 err = iwl_dbgfs_register(priv, DRV_NAME);
8755 if (err) {
8756 IWL_ERROR("failed to create debugfs files\n");
8757 goto out_remove_sysfs;
8758 }
8759
8760 iwl4965_setup_deferred_work(priv);
8761 iwl4965_setup_rx_handlers(priv);
8762
8763 /********************
8764 * 9. Conclude
8765 ********************/
Zhu Yi5a66926a2008-01-14 17:46:18 -08008766 pci_save_state(pdev);
8767 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008768
8769 return 0;
8770
Assaf Krauss316c30d2008-03-14 10:38:46 -07008771 out_remove_sysfs:
8772 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008773 out_free_geos:
8774 iwl4965_free_geos(priv);
8775 out_free_channel_map:
8776 iwl4965_free_channel_map(priv);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008777 out_unset_hw_settings:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008778 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008779 out_iounmap:
8780 pci_iounmap(pdev, priv->hw_base);
8781 out_pci_release_regions:
8782 pci_release_regions(pdev);
Assaf Krauss316c30d2008-03-14 10:38:46 -07008783 pci_set_drvdata(pdev, NULL);
Zhu Yib481de92007-09-25 17:54:57 -07008784 out_pci_disable_device:
8785 pci_disable_device(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008786 out_ieee80211_free_hw:
8787 ieee80211_free_hw(priv->hw);
8788 out:
8789 return err;
8790}
8791
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008792static void iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008793{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008794 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008795 struct list_head *p, *q;
8796 int i;
8797
8798 if (!priv)
8799 return;
8800
8801 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
8802
Zhu Yib481de92007-09-25 17:54:57 -07008803 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08008804
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008805 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008806
8807 /* Free MAC hash list for ADHOC */
8808 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
8809 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
8810 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008811 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07008812 }
8813 }
8814
Tomas Winkler712b6cf2008-03-12 16:58:52 -07008815 iwl_dbgfs_unregister(priv);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008816 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07008817
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008818 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008819
8820 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008821 iwl4965_rx_queue_free(priv, &priv->rxq);
8822 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008823
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008824 iwl4965_unset_hw_setting(priv);
8825 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008826
8827 if (priv->mac80211_registered) {
8828 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008829 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008830 }
8831
Mohamed Abbas948c1712007-10-25 17:15:45 +08008832 /*netif_stop_queue(dev); */
8833 flush_workqueue(priv->workqueue);
8834
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008835 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07008836 * priv->workqueue... so we can't take down the workqueue
8837 * until now... */
8838 destroy_workqueue(priv->workqueue);
8839 priv->workqueue = NULL;
8840
Zhu Yib481de92007-09-25 17:54:57 -07008841 pci_iounmap(pdev, priv->hw_base);
8842 pci_release_regions(pdev);
8843 pci_disable_device(pdev);
8844 pci_set_drvdata(pdev, NULL);
8845
Reinette Chatre849e0dc2008-01-23 10:15:18 -08008846 iwl4965_free_channel_map(priv);
8847 iwl4965_free_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008848
8849 if (priv->ibss_beacon)
8850 dev_kfree_skb(priv->ibss_beacon);
8851
8852 ieee80211_free_hw(priv->hw);
8853}
8854
8855#ifdef CONFIG_PM
8856
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008857static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07008858{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008859 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008860
Zhu Yie655b9f2008-01-24 02:19:38 -08008861 if (priv->is_open) {
8862 set_bit(STATUS_IN_SUSPEND, &priv->status);
8863 iwl4965_mac_stop(priv->hw);
8864 priv->is_open = 1;
8865 }
Zhu Yib481de92007-09-25 17:54:57 -07008866
Zhu Yib481de92007-09-25 17:54:57 -07008867 pci_set_power_state(pdev, PCI_D3hot);
8868
Zhu Yib481de92007-09-25 17:54:57 -07008869 return 0;
8870}
8871
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008872static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07008873{
Tomas Winklerc79dd5b2008-03-12 16:58:50 -07008874 struct iwl_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07008875
Zhu Yib481de92007-09-25 17:54:57 -07008876 pci_set_power_state(pdev, PCI_D0);
Zhu Yib481de92007-09-25 17:54:57 -07008877
Zhu Yie655b9f2008-01-24 02:19:38 -08008878 if (priv->is_open)
8879 iwl4965_mac_start(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07008880
Zhu Yie655b9f2008-01-24 02:19:38 -08008881 clear_bit(STATUS_IN_SUSPEND, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07008882 return 0;
8883}
8884
8885#endif /* CONFIG_PM */
8886
8887/*****************************************************************************
8888 *
8889 * driver and module entry point
8890 *
8891 *****************************************************************************/
8892
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008893static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07008894 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008895 .id_table = iwl4965_hw_card_ids,
8896 .probe = iwl4965_pci_probe,
8897 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07008898#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008899 .suspend = iwl4965_pci_suspend,
8900 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07008901#endif
8902};
8903
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008904static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07008905{
8906
8907 int ret;
8908 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
8909 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008910 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008911 if (ret) {
8912 IWL_ERROR("Unable to initialize PCI module\n");
8913 return ret;
8914 }
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008915#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008916 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008917 if (ret) {
8918 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008919 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008920 return ret;
8921 }
8922#endif
8923
8924 return ret;
8925}
8926
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008927static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07008928{
Tomas Winkler0a6857e2008-03-12 16:58:49 -07008929#ifdef CONFIG_IWLWIFI_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008930 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008931#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008932 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07008933}
8934
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008935module_param_named(antenna, iwl4965_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008936MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008937module_param_named(disable, iwl4965_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008938MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008939module_param_named(hwcrypto, iwl4965_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008940MODULE_PARM_DESC(hwcrypto,
8941 "using hardware crypto engine (default 0 [software])\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008942module_param_named(debug, iwl4965_param_debug, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008943MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008944module_param_named(disable_hw_scan, iwl4965_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008945MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
8946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008947module_param_named(queues_num, iwl4965_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008948MODULE_PARM_DESC(queues_num, "number of hw queues.");
8949
8950/* QoS */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008951module_param_named(qos_enable, iwl4965_param_qos_enable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07008952MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
Ron Rindjunsky9ee1ba42007-11-26 16:14:42 +02008953module_param_named(amsdu_size_8K, iwl4965_param_amsdu_size_8K, int, 0444);
8954MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
Zhu Yib481de92007-09-25 17:54:57 -07008955
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008956module_exit(iwl4965_exit);
8957module_init(iwl4965_init);