blob: 74c3d3917fe1ea8a5531be520551855a4a342d27 [file] [log] [blame]
Zhu Yib481de92007-09-25 17:54:57 -07001/******************************************************************************
2 *
3 * Copyright(c) 2003 - 2007 Intel Corporation. All rights reserved.
4 *
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
44#include <net/ieee80211_radiotap.h>
45#include <net/mac80211.h>
46
47#include <asm/div64.h>
48
Zhu Yib481de92007-09-25 17:54:57 -070049#include "iwl-4965.h"
50#include "iwl-helpers.h"
51
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080052#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080053u32 iwl4965_debug_level;
Zhu Yib481de92007-09-25 17:54:57 -070054#endif
55
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080056static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
57 struct iwl4965_tx_queue *txq);
Christoph Hellwig416e1432007-10-25 17:15:49 +080058
Zhu Yib481de92007-09-25 17:54:57 -070059/******************************************************************************
60 *
61 * module boiler plate
62 *
63 ******************************************************************************/
64
65/* module parameters */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080066static int iwl4965_param_disable_hw_scan;
67static int iwl4965_param_debug;
Ben Cahill9fbab512007-11-29 11:09:47 +080068static int iwl4965_param_disable; /* def: enable radio */
69static int iwl4965_param_antenna; /* def: 0 = both antennas (use diversity) */
70int iwl4965_param_hwcrypto; /* def: using software encryption */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -080071static int iwl4965_param_qos_enable = 1;
72int iwl4965_param_queues_num = IWL_MAX_NUM_QUEUES;
Zhu Yib481de92007-09-25 17:54:57 -070073
74/*
75 * module name, copyright, version, etc.
76 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
77 */
78
79#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
80
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080081#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -070082#define VD "d"
83#else
84#define VD
85#endif
86
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +080087#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -070088#define VS "s"
89#else
90#define VS
91#endif
92
Zhu Yi80f3e022007-10-25 17:15:48 +080093#define IWLWIFI_VERSION "1.1.19k" VD VS
Zhu Yib481de92007-09-25 17:54:57 -070094#define DRV_COPYRIGHT "Copyright(c) 2003-2007 Intel Corporation"
95#define DRV_VERSION IWLWIFI_VERSION
96
97/* Change firmware file name, using "-" and incrementing number,
98 * *only* when uCode interface or architecture changes so that it
99 * is not compatible with earlier drivers.
100 * This number will also appear in << 8 position of 1st dword of uCode file */
101#define IWL4965_UCODE_API "-1"
102
103MODULE_DESCRIPTION(DRV_DESCRIPTION);
104MODULE_VERSION(DRV_VERSION);
105MODULE_AUTHOR(DRV_COPYRIGHT);
106MODULE_LICENSE("GPL");
107
108__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
109{
110 u16 fc = le16_to_cpu(hdr->frame_control);
111 int hdr_len = ieee80211_get_hdrlen(fc);
112
113 if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
114 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
115 return NULL;
116}
117
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800118static const struct ieee80211_hw_mode *iwl4965_get_hw_mode(
119 struct iwl4965_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -0700120{
121 int i;
122
123 for (i = 0; i < 3; i++)
124 if (priv->modes[i].mode == mode)
125 return &priv->modes[i];
126
127 return NULL;
128}
129
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800130static int iwl4965_is_empty_essid(const char *essid, int essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700131{
132 /* Single white space is for Linksys APs */
133 if (essid_len == 1 && essid[0] == ' ')
134 return 1;
135
136 /* Otherwise, if the entire essid is 0, we assume it is hidden */
137 while (essid_len) {
138 essid_len--;
139 if (essid[essid_len] != '\0')
140 return 0;
141 }
142
143 return 1;
144}
145
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800146static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
Zhu Yib481de92007-09-25 17:54:57 -0700147{
148 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
149 const char *s = essid;
150 char *d = escaped;
151
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800152 if (iwl4965_is_empty_essid(essid, essid_len)) {
Zhu Yib481de92007-09-25 17:54:57 -0700153 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
154 return escaped;
155 }
156
157 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
158 while (essid_len--) {
159 if (*s == '\0') {
160 *d++ = '\\';
161 *d++ = '0';
162 s++;
163 } else
164 *d++ = *s++;
165 }
166 *d = '\0';
167 return escaped;
168}
169
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800170static void iwl4965_print_hex_dump(int level, void *p, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -0700171{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800172#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800173 if (!(iwl4965_debug_level & level))
Zhu Yib481de92007-09-25 17:54:57 -0700174 return;
175
176 print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
177 p, len, 1);
178#endif
179}
180
181/*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
182 * DMA services
183 *
184 * Theory of operation
185 *
186 * A queue is a circular buffers with 'Read' and 'Write' pointers.
187 * 2 empty entries always kept in the buffer to protect from overflow.
188 *
189 * For Tx queue, there are low mark and high mark limits. If, after queuing
190 * the packet for Tx, free space become < low mark, Tx queue stopped. When
191 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
192 * Tx queue resumed.
193 *
Zhu Yi583fab32007-09-27 11:27:30 +0800194 * The IWL operates with six queues, one receive queue in the device's
Zhu Yib481de92007-09-25 17:54:57 -0700195 * sram, one transmit queue for sending commands to the device firmware,
196 * and four transmit queues for data.
197 ***************************************************/
198
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800199static int iwl4965_queue_space(const struct iwl4965_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -0700200{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800201 int s = q->read_ptr - q->write_ptr;
Zhu Yib481de92007-09-25 17:54:57 -0700202
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800203 if (q->read_ptr > q->write_ptr)
Zhu Yib481de92007-09-25 17:54:57 -0700204 s -= q->n_bd;
205
206 if (s <= 0)
207 s += q->n_window;
208 /* keep some reserve to not confuse empty and full situations */
209 s -= 2;
210 if (s < 0)
211 s = 0;
212 return s;
213}
214
215/* XXX: n_bd must be power-of-two size */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800216static inline int iwl4965_queue_inc_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -0700217{
218 return ++index & (n_bd - 1);
219}
220
221/* XXX: n_bd must be power-of-two size */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800222static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
Zhu Yib481de92007-09-25 17:54:57 -0700223{
224 return --index & (n_bd - 1);
225}
226
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800227static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
Zhu Yib481de92007-09-25 17:54:57 -0700228{
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800229 return q->write_ptr > q->read_ptr ?
230 (i >= q->read_ptr && i < q->write_ptr) :
231 !(i < q->read_ptr && i >= q->write_ptr);
Zhu Yib481de92007-09-25 17:54:57 -0700232}
233
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800234static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
Zhu Yib481de92007-09-25 17:54:57 -0700235{
236 if (is_huge)
237 return q->n_window;
238
239 return index & (q->n_window - 1);
240}
241
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800242static int iwl4965_queue_init(struct iwl4965_priv *priv, struct iwl4965_queue *q,
Zhu Yib481de92007-09-25 17:54:57 -0700243 int count, int slots_num, u32 id)
244{
245 q->n_bd = count;
246 q->n_window = slots_num;
247 q->id = id;
248
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800249 /* count must be power-of-two size, otherwise iwl4965_queue_inc_wrap
250 * and iwl4965_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700251 BUG_ON(!is_power_of_2(count));
252
253 /* slots_num must be power-of-two size, otherwise
254 * get_cmd_index is broken. */
255 BUG_ON(!is_power_of_2(slots_num));
256
257 q->low_mark = q->n_window / 4;
258 if (q->low_mark < 4)
259 q->low_mark = 4;
260
261 q->high_mark = q->n_window / 8;
262 if (q->high_mark < 2)
263 q->high_mark = 2;
264
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800265 q->write_ptr = q->read_ptr = 0;
Zhu Yib481de92007-09-25 17:54:57 -0700266
267 return 0;
268}
269
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800270static int iwl4965_tx_queue_alloc(struct iwl4965_priv *priv,
271 struct iwl4965_tx_queue *txq, u32 id)
Zhu Yib481de92007-09-25 17:54:57 -0700272{
273 struct pci_dev *dev = priv->pci_dev;
274
275 if (id != IWL_CMD_QUEUE_NUM) {
276 txq->txb = kmalloc(sizeof(txq->txb[0]) *
277 TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
278 if (!txq->txb) {
Ian Schram01ebd062007-10-25 17:15:22 +0800279 IWL_ERROR("kmalloc for auxiliary BD "
Zhu Yib481de92007-09-25 17:54:57 -0700280 "structures failed\n");
281 goto error;
282 }
283 } else
284 txq->txb = NULL;
285
286 txq->bd = pci_alloc_consistent(dev,
287 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
288 &txq->q.dma_addr);
289
290 if (!txq->bd) {
291 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
292 sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
293 goto error;
294 }
295 txq->q.id = id;
296
297 return 0;
298
299 error:
300 if (txq->txb) {
301 kfree(txq->txb);
302 txq->txb = NULL;
303 }
304
305 return -ENOMEM;
306}
307
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800308int iwl4965_tx_queue_init(struct iwl4965_priv *priv,
309 struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
Zhu Yib481de92007-09-25 17:54:57 -0700310{
311 struct pci_dev *dev = priv->pci_dev;
312 int len;
313 int rc = 0;
314
Ian Schram01ebd062007-10-25 17:15:22 +0800315 /* allocate command space + one big command for scan since scan
Zhu Yib481de92007-09-25 17:54:57 -0700316 * command is very huge the system will not have two scan at the
317 * same time */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800318 len = sizeof(struct iwl4965_cmd) * slots_num;
Zhu Yib481de92007-09-25 17:54:57 -0700319 if (txq_id == IWL_CMD_QUEUE_NUM)
320 len += IWL_MAX_SCAN_SIZE;
321 txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
322 if (!txq->cmd)
323 return -ENOMEM;
324
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800325 rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700326 if (rc) {
327 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
328
329 return -ENOMEM;
330 }
331 txq->need_update = 0;
332
333 /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800334 * iwl4965_queue_inc_wrap and iwl4965_queue_dec_wrap are broken. */
Zhu Yib481de92007-09-25 17:54:57 -0700335 BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800336 iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
Zhu Yib481de92007-09-25 17:54:57 -0700337
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800338 iwl4965_hw_tx_queue_init(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700339
340 return 0;
341}
342
343/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800344 * iwl4965_tx_queue_free - Deallocate DMA queue.
Zhu Yib481de92007-09-25 17:54:57 -0700345 * @txq: Transmit queue to deallocate.
346 *
347 * Empty queue by removing and destroying all BD's.
348 * Free all buffers. txq itself is not freed.
349 *
350 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800351void iwl4965_tx_queue_free(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -0700352{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800353 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -0700354 struct pci_dev *dev = priv->pci_dev;
355 int len;
356
357 if (q->n_bd == 0)
358 return;
359
360 /* first, empty all BD's */
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800361 for (; q->write_ptr != q->read_ptr;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800362 q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd))
363 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700364
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800365 len = sizeof(struct iwl4965_cmd) * q->n_window;
Zhu Yib481de92007-09-25 17:54:57 -0700366 if (q->id == IWL_CMD_QUEUE_NUM)
367 len += IWL_MAX_SCAN_SIZE;
368
369 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
370
371 /* free buffers belonging to queue itself */
372 if (txq->q.n_bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800373 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
Zhu Yib481de92007-09-25 17:54:57 -0700374 txq->q.n_bd, txq->bd, txq->q.dma_addr);
375
376 if (txq->txb) {
377 kfree(txq->txb);
378 txq->txb = NULL;
379 }
380
381 /* 0 fill whole structure */
382 memset(txq, 0, sizeof(*txq));
383}
384
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800385const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
Zhu Yib481de92007-09-25 17:54:57 -0700386
387/*************** STATION TABLE MANAGEMENT ****
Ben Cahill9fbab512007-11-29 11:09:47 +0800388 * mac80211 should be examined to determine if sta_info is duplicating
Zhu Yib481de92007-09-25 17:54:57 -0700389 * the functionality provided here
390 */
391
392/**************************************************************/
393
Ian Schram01ebd062007-10-25 17:15:22 +0800394#if 0 /* temporary disable till we add real remove station */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800395static u8 iwl4965_remove_station(struct iwl4965_priv *priv, const u8 *addr, int is_ap)
Zhu Yib481de92007-09-25 17:54:57 -0700396{
397 int index = IWL_INVALID_STATION;
398 int i;
399 unsigned long flags;
400
401 spin_lock_irqsave(&priv->sta_lock, flags);
402
403 if (is_ap)
404 index = IWL_AP_ID;
405 else if (is_broadcast_ether_addr(addr))
406 index = priv->hw_setting.bcast_sta_id;
407 else
408 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
409 if (priv->stations[i].used &&
410 !compare_ether_addr(priv->stations[i].sta.sta.addr,
411 addr)) {
412 index = i;
413 break;
414 }
415
416 if (unlikely(index == IWL_INVALID_STATION))
417 goto out;
418
419 if (priv->stations[index].used) {
420 priv->stations[index].used = 0;
421 priv->num_stations--;
422 }
423
424 BUG_ON(priv->num_stations < 0);
425
426out:
427 spin_unlock_irqrestore(&priv->sta_lock, flags);
428 return 0;
429}
Zhu Yi556f8db2007-09-27 11:27:33 +0800430#endif
Zhu Yib481de92007-09-25 17:54:57 -0700431
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800432static void iwl4965_clear_stations_table(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700433{
434 unsigned long flags;
435
436 spin_lock_irqsave(&priv->sta_lock, flags);
437
438 priv->num_stations = 0;
439 memset(priv->stations, 0, sizeof(priv->stations));
440
441 spin_unlock_irqrestore(&priv->sta_lock, flags);
442}
443
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800444u8 iwl4965_add_station_flags(struct iwl4965_priv *priv, const u8 *addr, int is_ap, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -0700445{
446 int i;
447 int index = IWL_INVALID_STATION;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800448 struct iwl4965_station_entry *station;
Zhu Yib481de92007-09-25 17:54:57 -0700449 unsigned long flags_spin;
Joe Perches0795af52007-10-03 17:59:30 -0700450 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -0700451
452 spin_lock_irqsave(&priv->sta_lock, flags_spin);
453 if (is_ap)
454 index = IWL_AP_ID;
455 else if (is_broadcast_ether_addr(addr))
456 index = priv->hw_setting.bcast_sta_id;
457 else
458 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
459 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
460 addr)) {
461 index = i;
462 break;
463 }
464
465 if (!priv->stations[i].used &&
466 index == IWL_INVALID_STATION)
467 index = i;
468 }
469
470
Ben Cahill9fbab512007-11-29 11:09:47 +0800471 /* These two conditions have the same outcome, but keep them separate
472 since they have different meanings */
Zhu Yib481de92007-09-25 17:54:57 -0700473 if (unlikely(index == IWL_INVALID_STATION)) {
474 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
475 return index;
476 }
477
478 if (priv->stations[index].used &&
479 !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
480 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
481 return index;
482 }
483
484
Joe Perches0795af52007-10-03 17:59:30 -0700485 IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -0700486 station = &priv->stations[index];
487 station->used = 1;
488 priv->num_stations++;
489
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800490 memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
Zhu Yib481de92007-09-25 17:54:57 -0700491 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
492 station->sta.mode = 0;
493 station->sta.sta.sta_id = index;
494 station->sta.station_flags = 0;
495
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800496#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -0700497 /* BCAST station and IBSS stations do not work in HT mode */
498 if (index != priv->hw_setting.bcast_sta_id &&
499 priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
500 iwl4965_set_ht_add_station(priv, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +0800501#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -0700502
503 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800504 iwl4965_send_add_station(priv, &station->sta, flags);
Zhu Yib481de92007-09-25 17:54:57 -0700505 return index;
506
507}
508
509/*************** DRIVER STATUS FUNCTIONS *****/
510
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800511static inline int iwl4965_is_ready(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700512{
513 /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
514 * set but EXIT_PENDING is not */
515 return test_bit(STATUS_READY, &priv->status) &&
516 test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
517 !test_bit(STATUS_EXIT_PENDING, &priv->status);
518}
519
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800520static inline int iwl4965_is_alive(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700521{
522 return test_bit(STATUS_ALIVE, &priv->status);
523}
524
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800525static inline int iwl4965_is_init(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700526{
527 return test_bit(STATUS_INIT, &priv->status);
528}
529
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800530static inline int iwl4965_is_rfkill(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700531{
532 return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
533 test_bit(STATUS_RF_KILL_SW, &priv->status);
534}
535
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800536static inline int iwl4965_is_ready_rf(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700537{
538
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800539 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -0700540 return 0;
541
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800542 return iwl4965_is_ready(priv);
Zhu Yib481de92007-09-25 17:54:57 -0700543}
544
545/*************** HOST COMMAND QUEUE FUNCTIONS *****/
546
547#define IWL_CMD(x) case x : return #x
548
549static const char *get_cmd_string(u8 cmd)
550{
551 switch (cmd) {
552 IWL_CMD(REPLY_ALIVE);
553 IWL_CMD(REPLY_ERROR);
554 IWL_CMD(REPLY_RXON);
555 IWL_CMD(REPLY_RXON_ASSOC);
556 IWL_CMD(REPLY_QOS_PARAM);
557 IWL_CMD(REPLY_RXON_TIMING);
558 IWL_CMD(REPLY_ADD_STA);
559 IWL_CMD(REPLY_REMOVE_STA);
560 IWL_CMD(REPLY_REMOVE_ALL_STA);
561 IWL_CMD(REPLY_TX);
562 IWL_CMD(REPLY_RATE_SCALE);
563 IWL_CMD(REPLY_LEDS_CMD);
564 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
565 IWL_CMD(RADAR_NOTIFICATION);
566 IWL_CMD(REPLY_QUIET_CMD);
567 IWL_CMD(REPLY_CHANNEL_SWITCH);
568 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
569 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
570 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
571 IWL_CMD(POWER_TABLE_CMD);
572 IWL_CMD(PM_SLEEP_NOTIFICATION);
573 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
574 IWL_CMD(REPLY_SCAN_CMD);
575 IWL_CMD(REPLY_SCAN_ABORT_CMD);
576 IWL_CMD(SCAN_START_NOTIFICATION);
577 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
578 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
579 IWL_CMD(BEACON_NOTIFICATION);
580 IWL_CMD(REPLY_TX_BEACON);
581 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
582 IWL_CMD(QUIET_NOTIFICATION);
583 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
584 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
585 IWL_CMD(REPLY_BT_CONFIG);
586 IWL_CMD(REPLY_STATISTICS_CMD);
587 IWL_CMD(STATISTICS_NOTIFICATION);
588 IWL_CMD(REPLY_CARD_STATE_CMD);
589 IWL_CMD(CARD_STATE_NOTIFICATION);
590 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
591 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
592 IWL_CMD(SENSITIVITY_CMD);
593 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
594 IWL_CMD(REPLY_RX_PHY_CMD);
595 IWL_CMD(REPLY_RX_MPDU_CMD);
596 IWL_CMD(REPLY_4965_RX);
597 IWL_CMD(REPLY_COMPRESSED_BA);
598 default:
599 return "UNKNOWN";
600
601 }
602}
603
604#define HOST_COMPLETE_TIMEOUT (HZ / 2)
605
606/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800607 * iwl4965_enqueue_hcmd - enqueue a uCode command
Zhu Yib481de92007-09-25 17:54:57 -0700608 * @priv: device private data point
609 * @cmd: a point to the ucode command structure
610 *
611 * The function returns < 0 values to indicate the operation is
612 * failed. On success, it turns the index (> 0) of command in the
613 * command queue.
614 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800615static int iwl4965_enqueue_hcmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700616{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800617 struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
618 struct iwl4965_queue *q = &txq->q;
619 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -0700620 u32 *control_flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800621 struct iwl4965_cmd *out_cmd;
Zhu Yib481de92007-09-25 17:54:57 -0700622 u32 idx;
623 u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
624 dma_addr_t phys_addr;
625 int ret;
626 unsigned long flags;
627
628 /* If any of the command structures end up being larger than
629 * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
630 * we will need to increase the size of the TFD entries */
631 BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
632 !(cmd->meta.flags & CMD_SIZE_HUGE));
633
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800634 if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
Zhu Yib481de92007-09-25 17:54:57 -0700635 IWL_ERROR("No space for Tx\n");
636 return -ENOSPC;
637 }
638
639 spin_lock_irqsave(&priv->hcmd_lock, flags);
640
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800641 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -0700642 memset(tfd, 0, sizeof(*tfd));
643
644 control_flags = (u32 *) tfd;
645
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800646 idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
Zhu Yib481de92007-09-25 17:54:57 -0700647 out_cmd = &txq->cmd[idx];
648
649 out_cmd->hdr.cmd = cmd->id;
650 memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
651 memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
652
653 /* At this point, the out_cmd now has all of the incoming cmd
654 * information */
655
656 out_cmd->hdr.flags = 0;
657 out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800658 INDEX_TO_SEQ(q->write_ptr));
Zhu Yib481de92007-09-25 17:54:57 -0700659 if (out_cmd->meta.flags & CMD_SIZE_HUGE)
660 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
661
662 phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800663 offsetof(struct iwl4965_cmd, hdr);
664 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
Zhu Yib481de92007-09-25 17:54:57 -0700665
666 IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
667 "%d bytes at %d[%d]:%d\n",
668 get_cmd_string(out_cmd->hdr.cmd),
669 out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
Tomas Winklerfc4b6852007-10-25 17:15:24 +0800670 fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
Zhu Yib481de92007-09-25 17:54:57 -0700671
672 txq->need_update = 1;
673 ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800674 q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
675 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -0700676
677 spin_unlock_irqrestore(&priv->hcmd_lock, flags);
678 return ret ? ret : idx;
679}
680
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800681static int iwl4965_send_cmd_async(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700682{
683 int ret;
684
685 BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
686
687 /* An asynchronous command can not expect an SKB to be set. */
688 BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
689
690 /* An asynchronous command MUST have a callback. */
691 BUG_ON(!cmd->meta.u.callback);
692
693 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
694 return -EBUSY;
695
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800696 ret = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700697 if (ret < 0) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800698 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700699 get_cmd_string(cmd->id), ret);
700 return ret;
701 }
702 return 0;
703}
704
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800705static int iwl4965_send_cmd_sync(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700706{
707 int cmd_idx;
708 int ret;
709 static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
710
711 BUG_ON(cmd->meta.flags & CMD_ASYNC);
712
713 /* A synchronous command can not have a callback set. */
714 BUG_ON(cmd->meta.u.callback != NULL);
715
716 if (atomic_xchg(&entry, 1)) {
717 IWL_ERROR("Error sending %s: Already sending a host command\n",
718 get_cmd_string(cmd->id));
719 return -EBUSY;
720 }
721
722 set_bit(STATUS_HCMD_ACTIVE, &priv->status);
723
724 if (cmd->meta.flags & CMD_WANT_SKB)
725 cmd->meta.source = &cmd->meta;
726
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800727 cmd_idx = iwl4965_enqueue_hcmd(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700728 if (cmd_idx < 0) {
729 ret = cmd_idx;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800730 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
Zhu Yib481de92007-09-25 17:54:57 -0700731 get_cmd_string(cmd->id), ret);
732 goto out;
733 }
734
735 ret = wait_event_interruptible_timeout(priv->wait_command_queue,
736 !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
737 HOST_COMPLETE_TIMEOUT);
738 if (!ret) {
739 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
740 IWL_ERROR("Error sending %s: time out after %dms.\n",
741 get_cmd_string(cmd->id),
742 jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
743
744 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
745 ret = -ETIMEDOUT;
746 goto cancel;
747 }
748 }
749
750 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
751 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
752 get_cmd_string(cmd->id));
753 ret = -ECANCELED;
754 goto fail;
755 }
756 if (test_bit(STATUS_FW_ERROR, &priv->status)) {
757 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
758 get_cmd_string(cmd->id));
759 ret = -EIO;
760 goto fail;
761 }
762 if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
763 IWL_ERROR("Error: Response NULL in '%s'\n",
764 get_cmd_string(cmd->id));
765 ret = -EIO;
766 goto out;
767 }
768
769 ret = 0;
770 goto out;
771
772cancel:
773 if (cmd->meta.flags & CMD_WANT_SKB) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800774 struct iwl4965_cmd *qcmd;
Zhu Yib481de92007-09-25 17:54:57 -0700775
776 /* Cancel the CMD_WANT_SKB flag for the cmd in the
777 * TX cmd queue. Otherwise in case the cmd comes
778 * in later, it will possibly set an invalid
779 * address (cmd->meta.source). */
780 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
781 qcmd->meta.flags &= ~CMD_WANT_SKB;
782 }
783fail:
784 if (cmd->meta.u.skb) {
785 dev_kfree_skb_any(cmd->meta.u.skb);
786 cmd->meta.u.skb = NULL;
787 }
788out:
789 atomic_set(&entry, 0);
790 return ret;
791}
792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800793int iwl4965_send_cmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
Zhu Yib481de92007-09-25 17:54:57 -0700794{
Zhu Yib481de92007-09-25 17:54:57 -0700795 if (cmd->meta.flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800796 return iwl4965_send_cmd_async(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700797
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800798 return iwl4965_send_cmd_sync(priv, cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700799}
800
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800801int iwl4965_send_cmd_pdu(struct iwl4965_priv *priv, u8 id, u16 len, const void *data)
Zhu Yib481de92007-09-25 17:54:57 -0700802{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800803 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700804 .id = id,
805 .len = len,
806 .data = data,
807 };
808
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800809 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700810}
811
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800812static int __must_check iwl4965_send_cmd_u32(struct iwl4965_priv *priv, u8 id, u32 val)
Zhu Yib481de92007-09-25 17:54:57 -0700813{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800814 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -0700815 .id = id,
816 .len = sizeof(val),
817 .data = &val,
818 };
819
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800820 return iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -0700821}
822
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800823int iwl4965_send_statistics_request(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700824{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800825 return iwl4965_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700826}
827
828/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800829 * iwl4965_rxon_add_station - add station into station table.
Zhu Yib481de92007-09-25 17:54:57 -0700830 *
831 * there is only one AP station with id= IWL_AP_ID
Ben Cahill9fbab512007-11-29 11:09:47 +0800832 * NOTE: mutex must be held before calling this fnction
833 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800834static int iwl4965_rxon_add_station(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -0700835 const u8 *addr, int is_ap)
836{
Zhu Yi556f8db2007-09-27 11:27:33 +0800837 u8 sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700838
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800839 sta_id = iwl4965_add_station_flags(priv, addr, is_ap, 0);
Zhu Yib481de92007-09-25 17:54:57 -0700840 iwl4965_add_station(priv, addr, is_ap);
841
Zhu Yi556f8db2007-09-27 11:27:33 +0800842 return sta_id;
Zhu Yib481de92007-09-25 17:54:57 -0700843}
844
845/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800846 * iwl4965_set_rxon_channel - Set the phymode and channel values in staging RXON
Zhu Yib481de92007-09-25 17:54:57 -0700847 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
848 * @channel: Any channel valid for the requested phymode
849
850 * In addition to setting the staging RXON, priv->phymode is also set.
851 *
852 * NOTE: Does not commit to the hardware; it sets appropriate bit fields
853 * in the staging RXON flag structure based on the phymode
854 */
Ben Cahill9fbab512007-11-29 11:09:47 +0800855static int iwl4965_set_rxon_channel(struct iwl4965_priv *priv, u8 phymode,
856 u16 channel)
Zhu Yib481de92007-09-25 17:54:57 -0700857{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800858 if (!iwl4965_get_channel_info(priv, phymode, channel)) {
Zhu Yib481de92007-09-25 17:54:57 -0700859 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
860 channel, phymode);
861 return -EINVAL;
862 }
863
864 if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
865 (priv->phymode == phymode))
866 return 0;
867
868 priv->staging_rxon.channel = cpu_to_le16(channel);
869 if (phymode == MODE_IEEE80211A)
870 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
871 else
872 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
873
874 priv->phymode = phymode;
875
876 IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
877
878 return 0;
879}
880
881/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800882 * iwl4965_check_rxon_cmd - validate RXON structure is valid
Zhu Yib481de92007-09-25 17:54:57 -0700883 *
884 * NOTE: This is really only useful during development and can eventually
885 * be #ifdef'd out once the driver is stable and folks aren't actively
886 * making changes
887 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800888static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -0700889{
890 int error = 0;
891 int counter = 1;
892
893 if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
894 error |= le32_to_cpu(rxon->flags &
895 (RXON_FLG_TGJ_NARROW_BAND_MSK |
896 RXON_FLG_RADAR_DETECT_MSK));
897 if (error)
898 IWL_WARNING("check 24G fields %d | %d\n",
899 counter++, error);
900 } else {
901 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
902 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
903 if (error)
904 IWL_WARNING("check 52 fields %d | %d\n",
905 counter++, error);
906 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
907 if (error)
908 IWL_WARNING("check 52 CCK %d | %d\n",
909 counter++, error);
910 }
911 error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
912 if (error)
913 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
914
915 /* make sure basic rates 6Mbps and 1Mbps are supported */
916 error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
917 ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
918 if (error)
919 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
920
921 error |= (le16_to_cpu(rxon->assoc_id) > 2007);
922 if (error)
923 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
924
925 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
926 == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
927 if (error)
928 IWL_WARNING("check CCK and short slot %d | %d\n",
929 counter++, error);
930
931 error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
932 == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
933 if (error)
934 IWL_WARNING("check CCK & auto detect %d | %d\n",
935 counter++, error);
936
937 error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
938 RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
939 if (error)
940 IWL_WARNING("check TGG and auto detect %d | %d\n",
941 counter++, error);
942
943 if (error)
944 IWL_WARNING("Tuning to channel %d\n",
945 le16_to_cpu(rxon->channel));
946
947 if (error) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800948 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
Zhu Yib481de92007-09-25 17:54:57 -0700949 return -1;
950 }
951 return 0;
952}
953
954/**
Ben Cahill9fbab512007-11-29 11:09:47 +0800955 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
Ian Schram01ebd062007-10-25 17:15:22 +0800956 * @priv: staging_rxon is compared to active_rxon
Zhu Yib481de92007-09-25 17:54:57 -0700957 *
Ben Cahill9fbab512007-11-29 11:09:47 +0800958 * If the RXON structure is changing enough to require a new tune,
959 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
960 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
Zhu Yib481de92007-09-25 17:54:57 -0700961 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -0800962static int iwl4965_full_rxon_required(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -0700963{
964
965 /* These items are only settable from the full RXON command */
966 if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
967 compare_ether_addr(priv->staging_rxon.bssid_addr,
968 priv->active_rxon.bssid_addr) ||
969 compare_ether_addr(priv->staging_rxon.node_addr,
970 priv->active_rxon.node_addr) ||
971 compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
972 priv->active_rxon.wlap_bssid_addr) ||
973 (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
974 (priv->staging_rxon.channel != priv->active_rxon.channel) ||
975 (priv->staging_rxon.air_propagation !=
976 priv->active_rxon.air_propagation) ||
977 (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
978 priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
979 (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
980 priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
981 (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
982 (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
983 return 1;
984
985 /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
986 * be updated with the RXON_ASSOC command -- however only some
987 * flag transitions are allowed using RXON_ASSOC */
988
989 /* Check if we are not switching bands */
990 if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
991 (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
992 return 1;
993
994 /* Check if we are switching association toggle */
995 if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
996 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
997 return 1;
998
999 return 0;
1000}
1001
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001002static int iwl4965_send_rxon_assoc(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001003{
1004 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001005 struct iwl4965_rx_packet *res = NULL;
1006 struct iwl4965_rxon_assoc_cmd rxon_assoc;
1007 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001008 .id = REPLY_RXON_ASSOC,
1009 .len = sizeof(rxon_assoc),
1010 .meta.flags = CMD_WANT_SKB,
1011 .data = &rxon_assoc,
1012 };
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001013 const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
1014 const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07001015
1016 if ((rxon1->flags == rxon2->flags) &&
1017 (rxon1->filter_flags == rxon2->filter_flags) &&
1018 (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1019 (rxon1->ofdm_ht_single_stream_basic_rates ==
1020 rxon2->ofdm_ht_single_stream_basic_rates) &&
1021 (rxon1->ofdm_ht_dual_stream_basic_rates ==
1022 rxon2->ofdm_ht_dual_stream_basic_rates) &&
1023 (rxon1->rx_chain == rxon2->rx_chain) &&
1024 (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1025 IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
1026 return 0;
1027 }
1028
1029 rxon_assoc.flags = priv->staging_rxon.flags;
1030 rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1031 rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1032 rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1033 rxon_assoc.reserved = 0;
1034 rxon_assoc.ofdm_ht_single_stream_basic_rates =
1035 priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1036 rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1037 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1038 rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1039
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001040 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001041 if (rc)
1042 return rc;
1043
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001044 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001045 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1046 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1047 rc = -EIO;
1048 }
1049
1050 priv->alloc_rxb_skb--;
1051 dev_kfree_skb_any(cmd.meta.u.skb);
1052
1053 return rc;
1054}
1055
1056/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001057 * iwl4965_commit_rxon - commit staging_rxon to hardware
Zhu Yib481de92007-09-25 17:54:57 -07001058 *
Ian Schram01ebd062007-10-25 17:15:22 +08001059 * The RXON command in staging_rxon is committed to the hardware and
Zhu Yib481de92007-09-25 17:54:57 -07001060 * the active_rxon structure is updated with the new data. This
1061 * function correctly transitions out of the RXON_ASSOC_MSK state if
1062 * a HW tune is required based on the RXON structure changes.
1063 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001064static int iwl4965_commit_rxon(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001065{
1066 /* cast away the const for active_rxon in this function */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001067 struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
Joe Perches0795af52007-10-03 17:59:30 -07001068 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07001069 int rc = 0;
1070
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001071 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07001072 return -1;
1073
1074 /* always get timestamp with Rx frame */
1075 priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1076
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001077 rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001078 if (rc) {
1079 IWL_ERROR("Invalid RXON configuration. Not committing.\n");
1080 return -EINVAL;
1081 }
1082
1083 /* If we don't need to send a full RXON, we can use
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001084 * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
Zhu Yib481de92007-09-25 17:54:57 -07001085 * and other flags for the current radio configuration. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001086 if (!iwl4965_full_rxon_required(priv)) {
1087 rc = iwl4965_send_rxon_assoc(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001088 if (rc) {
1089 IWL_ERROR("Error setting RXON_ASSOC "
1090 "configuration (%d).\n", rc);
1091 return rc;
1092 }
1093
1094 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1095
1096 return 0;
1097 }
1098
1099 /* station table will be cleared */
1100 priv->assoc_station_added = 0;
1101
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001102#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001103 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1104 if (!priv->error_recovering)
1105 priv->start_calib = 0;
1106
1107 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001108#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001109
1110 /* If we are currently associated and the new config requires
1111 * an RXON_ASSOC and the new config wants the associated mask enabled,
1112 * we must clear the associated from the active configuration
1113 * before we apply the new config */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001114 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001115 (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1116 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1117 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1118
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001119 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1120 sizeof(struct iwl4965_rxon_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001121 &priv->active_rxon);
1122
1123 /* If the mask clearing failed then we set
1124 * active_rxon back to what it was previously */
1125 if (rc) {
1126 active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1127 IWL_ERROR("Error clearing ASSOC_MSK on current "
1128 "configuration (%d).\n", rc);
1129 return rc;
1130 }
Zhu Yib481de92007-09-25 17:54:57 -07001131 }
1132
1133 IWL_DEBUG_INFO("Sending RXON\n"
1134 "* with%s RXON_FILTER_ASSOC_MSK\n"
1135 "* channel = %d\n"
Joe Perches0795af52007-10-03 17:59:30 -07001136 "* bssid = %s\n",
Zhu Yib481de92007-09-25 17:54:57 -07001137 ((priv->staging_rxon.filter_flags &
1138 RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1139 le16_to_cpu(priv->staging_rxon.channel),
Joe Perches0795af52007-10-03 17:59:30 -07001140 print_mac(mac, priv->staging_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07001141
1142 /* Apply the new configuration */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001143 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1144 sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07001145 if (rc) {
1146 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1147 return rc;
1148 }
1149
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001150 iwl4965_clear_stations_table(priv);
Zhu Yi556f8db2007-09-27 11:27:33 +08001151
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001152#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07001153 if (!priv->error_recovering)
1154 priv->start_calib = 0;
1155
1156 priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1157 iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001158#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07001159
1160 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1161
1162 /* If we issue a new RXON command which required a tune then we must
1163 * send a new TXPOWER command or we won't be able to Tx any frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001164 rc = iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001165 if (rc) {
1166 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1167 return rc;
1168 }
1169
1170 /* Add the broadcast address so we can send broadcast frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001171 if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
Zhu Yib481de92007-09-25 17:54:57 -07001172 IWL_INVALID_STATION) {
1173 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1174 return -EIO;
1175 }
1176
1177 /* If we have set the ASSOC_MSK and we are in BSS mode then
1178 * add the IWL_AP_ID to the station rate table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001179 if (iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07001180 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001181 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
Zhu Yib481de92007-09-25 17:54:57 -07001182 == IWL_INVALID_STATION) {
1183 IWL_ERROR("Error adding AP address for transmit.\n");
1184 return -EIO;
1185 }
1186 priv->assoc_station_added = 1;
1187 }
1188
1189 return 0;
1190}
1191
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001192static int iwl4965_send_bt_config(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001193{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001194 struct iwl4965_bt_cmd bt_cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001195 .flags = 3,
1196 .lead_time = 0xAA,
1197 .max_kill = 1,
1198 .kill_ack_mask = 0,
1199 .kill_cts_mask = 0,
1200 };
1201
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001202 return iwl4965_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1203 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001204}
1205
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001206static int iwl4965_send_scan_abort(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001207{
1208 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001209 struct iwl4965_rx_packet *res;
1210 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001211 .id = REPLY_SCAN_ABORT_CMD,
1212 .meta.flags = CMD_WANT_SKB,
1213 };
1214
1215 /* If there isn't a scan actively going on in the hardware
1216 * then we are in between scan bands and not actually
1217 * actively scanning, so don't send the abort command */
1218 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1219 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1220 return 0;
1221 }
1222
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001223 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001224 if (rc) {
1225 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1226 return rc;
1227 }
1228
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001229 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001230 if (res->u.status != CAN_ABORT_STATUS) {
1231 /* The scan abort will return 1 for success or
1232 * 2 for "failure". A failure condition can be
1233 * due to simply not being in an active scan which
1234 * can occur if we send the scan abort before we
1235 * the microcode has notified us that a scan is
1236 * completed. */
1237 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1238 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1239 clear_bit(STATUS_SCAN_HW, &priv->status);
1240 }
1241
1242 dev_kfree_skb_any(cmd.meta.u.skb);
1243
1244 return rc;
1245}
1246
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001247static int iwl4965_card_state_sync_callback(struct iwl4965_priv *priv,
1248 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07001249 struct sk_buff *skb)
1250{
1251 return 1;
1252}
1253
1254/*
1255 * CARD_STATE_CMD
1256 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001257 * Use: Sets the device's internal card state to enable, disable, or halt
Zhu Yib481de92007-09-25 17:54:57 -07001258 *
1259 * When in the 'enable' state the card operates as normal.
1260 * When in the 'disable' state, the card enters into a low power mode.
1261 * When in the 'halt' state, the card is shut down and must be fully
1262 * restarted to come back on.
1263 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001264static int iwl4965_send_card_state(struct iwl4965_priv *priv, u32 flags, u8 meta_flag)
Zhu Yib481de92007-09-25 17:54:57 -07001265{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001266 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001267 .id = REPLY_CARD_STATE_CMD,
1268 .len = sizeof(u32),
1269 .data = &flags,
1270 .meta.flags = meta_flag,
1271 };
1272
1273 if (meta_flag & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001274 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001275
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001276 return iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001277}
1278
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001279static int iwl4965_add_sta_sync_callback(struct iwl4965_priv *priv,
1280 struct iwl4965_cmd *cmd, struct sk_buff *skb)
Zhu Yib481de92007-09-25 17:54:57 -07001281{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001282 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001283
1284 if (!skb) {
1285 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1286 return 1;
1287 }
1288
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001289 res = (struct iwl4965_rx_packet *)skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001290 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1291 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1292 res->hdr.flags);
1293 return 1;
1294 }
1295
1296 switch (res->u.add_sta.status) {
1297 case ADD_STA_SUCCESS_MSK:
1298 break;
1299 default:
1300 break;
1301 }
1302
1303 /* We didn't cache the SKB; let the caller free it */
1304 return 1;
1305}
1306
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001307int iwl4965_send_add_station(struct iwl4965_priv *priv,
1308 struct iwl4965_addsta_cmd *sta, u8 flags)
Zhu Yib481de92007-09-25 17:54:57 -07001309{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001310 struct iwl4965_rx_packet *res = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07001311 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001312 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07001313 .id = REPLY_ADD_STA,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001314 .len = sizeof(struct iwl4965_addsta_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07001315 .meta.flags = flags,
1316 .data = sta,
1317 };
1318
1319 if (flags & CMD_ASYNC)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001320 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
Zhu Yib481de92007-09-25 17:54:57 -07001321 else
1322 cmd.meta.flags |= CMD_WANT_SKB;
1323
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001324 rc = iwl4965_send_cmd(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07001325
1326 if (rc || (flags & CMD_ASYNC))
1327 return rc;
1328
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001329 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07001330 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1331 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1332 res->hdr.flags);
1333 rc = -EIO;
1334 }
1335
1336 if (rc == 0) {
1337 switch (res->u.add_sta.status) {
1338 case ADD_STA_SUCCESS_MSK:
1339 IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1340 break;
1341 default:
1342 rc = -EIO;
1343 IWL_WARNING("REPLY_ADD_STA failed\n");
1344 break;
1345 }
1346 }
1347
1348 priv->alloc_rxb_skb--;
1349 dev_kfree_skb_any(cmd.meta.u.skb);
1350
1351 return rc;
1352}
1353
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001354static int iwl4965_update_sta_key_info(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001355 struct ieee80211_key_conf *keyconf,
1356 u8 sta_id)
1357{
1358 unsigned long flags;
1359 __le16 key_flags = 0;
1360
1361 switch (keyconf->alg) {
1362 case ALG_CCMP:
1363 key_flags |= STA_KEY_FLG_CCMP;
1364 key_flags |= cpu_to_le16(
1365 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1366 key_flags &= ~STA_KEY_FLG_INVALID;
1367 break;
1368 case ALG_TKIP:
1369 case ALG_WEP:
Zhu Yib481de92007-09-25 17:54:57 -07001370 default:
1371 return -EINVAL;
1372 }
1373 spin_lock_irqsave(&priv->sta_lock, flags);
1374 priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1375 priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1376 memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1377 keyconf->keylen);
1378
1379 memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1380 keyconf->keylen);
1381 priv->stations[sta_id].sta.key.key_flags = key_flags;
1382 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1383 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1384
1385 spin_unlock_irqrestore(&priv->sta_lock, flags);
1386
1387 IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001388 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001389 return 0;
1390}
1391
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001392static int iwl4965_clear_sta_key_info(struct iwl4965_priv *priv, u8 sta_id)
Zhu Yib481de92007-09-25 17:54:57 -07001393{
1394 unsigned long flags;
1395
1396 spin_lock_irqsave(&priv->sta_lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001397 memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1398 memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
Zhu Yib481de92007-09-25 17:54:57 -07001399 priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1400 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1401 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1402 spin_unlock_irqrestore(&priv->sta_lock, flags);
1403
1404 IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001405 iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
Zhu Yib481de92007-09-25 17:54:57 -07001406 return 0;
1407}
1408
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001409static void iwl4965_clear_free_frames(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001410{
1411 struct list_head *element;
1412
1413 IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1414 priv->frames_count);
1415
1416 while (!list_empty(&priv->free_frames)) {
1417 element = priv->free_frames.next;
1418 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001419 kfree(list_entry(element, struct iwl4965_frame, list));
Zhu Yib481de92007-09-25 17:54:57 -07001420 priv->frames_count--;
1421 }
1422
1423 if (priv->frames_count) {
1424 IWL_WARNING("%d frames still in use. Did we lose one?\n",
1425 priv->frames_count);
1426 priv->frames_count = 0;
1427 }
1428}
1429
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001430static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001431{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001432 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001433 struct list_head *element;
1434 if (list_empty(&priv->free_frames)) {
1435 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1436 if (!frame) {
1437 IWL_ERROR("Could not allocate frame!\n");
1438 return NULL;
1439 }
1440
1441 priv->frames_count++;
1442 return frame;
1443 }
1444
1445 element = priv->free_frames.next;
1446 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001447 return list_entry(element, struct iwl4965_frame, list);
Zhu Yib481de92007-09-25 17:54:57 -07001448}
1449
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001450static void iwl4965_free_frame(struct iwl4965_priv *priv, struct iwl4965_frame *frame)
Zhu Yib481de92007-09-25 17:54:57 -07001451{
1452 memset(frame, 0, sizeof(*frame));
1453 list_add(&frame->list, &priv->free_frames);
1454}
1455
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001456unsigned int iwl4965_fill_beacon_frame(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001457 struct ieee80211_hdr *hdr,
1458 const u8 *dest, int left)
1459{
1460
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001461 if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
Zhu Yib481de92007-09-25 17:54:57 -07001462 ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1463 (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1464 return 0;
1465
1466 if (priv->ibss_beacon->len > left)
1467 return 0;
1468
1469 memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1470
1471 return priv->ibss_beacon->len;
1472}
1473
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001474int iwl4965_rate_index_from_plcp(int plcp)
Zhu Yib481de92007-09-25 17:54:57 -07001475{
1476 int i = 0;
1477
Ben Cahill77626352007-11-29 11:09:44 +08001478 /* 4965 HT rate format */
Zhu Yib481de92007-09-25 17:54:57 -07001479 if (plcp & RATE_MCS_HT_MSK) {
1480 i = (plcp & 0xff);
1481
1482 if (i >= IWL_RATE_MIMO_6M_PLCP)
1483 i = i - IWL_RATE_MIMO_6M_PLCP;
1484
1485 i += IWL_FIRST_OFDM_RATE;
1486 /* skip 9M not supported in ht*/
1487 if (i >= IWL_RATE_9M_INDEX)
1488 i += 1;
1489 if ((i >= IWL_FIRST_OFDM_RATE) &&
1490 (i <= IWL_LAST_OFDM_RATE))
1491 return i;
Ben Cahill77626352007-11-29 11:09:44 +08001492
1493 /* 4965 legacy rate format, search for match in table */
Zhu Yib481de92007-09-25 17:54:57 -07001494 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001495 for (i = 0; i < ARRAY_SIZE(iwl4965_rates); i++)
1496 if (iwl4965_rates[i].plcp == (plcp &0xFF))
Zhu Yib481de92007-09-25 17:54:57 -07001497 return i;
1498 }
1499 return -1;
1500}
1501
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001502static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
Zhu Yib481de92007-09-25 17:54:57 -07001503{
1504 u8 i;
1505
1506 for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001507 i = iwl4965_rates[i].next_ieee) {
Zhu Yib481de92007-09-25 17:54:57 -07001508 if (rate_mask & (1 << i))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001509 return iwl4965_rates[i].plcp;
Zhu Yib481de92007-09-25 17:54:57 -07001510 }
1511
1512 return IWL_RATE_INVALID;
1513}
1514
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001515static int iwl4965_send_beacon_cmd(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001516{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001517 struct iwl4965_frame *frame;
Zhu Yib481de92007-09-25 17:54:57 -07001518 unsigned int frame_size;
1519 int rc;
1520 u8 rate;
1521
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001522 frame = iwl4965_get_free_frame(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001523
1524 if (!frame) {
1525 IWL_ERROR("Could not obtain free frame buffer for beacon "
1526 "command.\n");
1527 return -ENOMEM;
1528 }
1529
1530 if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001531 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
Zhu Yib481de92007-09-25 17:54:57 -07001532 0xFF0);
1533 if (rate == IWL_INVALID_RATE)
1534 rate = IWL_RATE_6M_PLCP;
1535 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001536 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
Zhu Yib481de92007-09-25 17:54:57 -07001537 if (rate == IWL_INVALID_RATE)
1538 rate = IWL_RATE_1M_PLCP;
1539 }
1540
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001541 frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
Zhu Yib481de92007-09-25 17:54:57 -07001542
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001543 rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
Zhu Yib481de92007-09-25 17:54:57 -07001544 &frame->u.cmd[0]);
1545
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001546 iwl4965_free_frame(priv, frame);
Zhu Yib481de92007-09-25 17:54:57 -07001547
1548 return rc;
1549}
1550
1551/******************************************************************************
1552 *
1553 * EEPROM related functions
1554 *
1555 ******************************************************************************/
1556
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001557static void get_eeprom_mac(struct iwl4965_priv *priv, u8 *mac)
Zhu Yib481de92007-09-25 17:54:57 -07001558{
1559 memcpy(mac, priv->eeprom.mac_address, 6);
1560}
1561
1562/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001563 * iwl4965_eeprom_init - read EEPROM contents
Zhu Yib481de92007-09-25 17:54:57 -07001564 *
1565 * Load the EEPROM from adapter into priv->eeprom
1566 *
1567 * NOTE: This routine uses the non-debug IO access functions.
1568 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001569int iwl4965_eeprom_init(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001570{
1571 u16 *e = (u16 *)&priv->eeprom;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001572 u32 gp = iwl4965_read32(priv, CSR_EEPROM_GP);
Zhu Yib481de92007-09-25 17:54:57 -07001573 u32 r;
1574 int sz = sizeof(priv->eeprom);
1575 int rc;
1576 int i;
1577 u16 addr;
1578
1579 /* The EEPROM structure has several padding buffers within it
1580 * and when adding new EEPROM maps is subject to programmer errors
1581 * which may be very difficult to identify without explicitly
1582 * checking the resulting size of the eeprom map. */
1583 BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1584
1585 if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1586 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1587 return -ENOENT;
1588 }
1589
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001590 rc = iwl4965_eeprom_acquire_semaphore(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001591 if (rc < 0) {
Ian Schram91e17472007-10-25 17:15:23 +08001592 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
Zhu Yib481de92007-09-25 17:54:57 -07001593 return -ENOENT;
1594 }
1595
1596 /* eeprom is an array of 16bit values */
1597 for (addr = 0; addr < sz; addr += sizeof(u16)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001598 _iwl4965_write32(priv, CSR_EEPROM_REG, addr << 1);
1599 _iwl4965_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
Zhu Yib481de92007-09-25 17:54:57 -07001600
1601 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1602 i += IWL_EEPROM_ACCESS_DELAY) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001603 r = _iwl4965_read_direct32(priv, CSR_EEPROM_REG);
Zhu Yib481de92007-09-25 17:54:57 -07001604 if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1605 break;
1606 udelay(IWL_EEPROM_ACCESS_DELAY);
1607 }
1608
1609 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1610 IWL_ERROR("Time out reading EEPROM[%d]", addr);
1611 rc = -ETIMEDOUT;
1612 goto done;
1613 }
1614 e[addr / 2] = le16_to_cpu(r >> 16);
1615 }
1616 rc = 0;
1617
1618done:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001619 iwl4965_eeprom_release_semaphore(priv);
Zhu Yib481de92007-09-25 17:54:57 -07001620 return rc;
1621}
1622
1623/******************************************************************************
1624 *
1625 * Misc. internal state and helper functions
1626 *
1627 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001628#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07001629
1630/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001631 * iwl4965_report_frame - dump frame to syslog during debug sessions
Zhu Yib481de92007-09-25 17:54:57 -07001632 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001633 * You may hack this function to show different aspects of received frames,
Zhu Yib481de92007-09-25 17:54:57 -07001634 * including selective frame dumps.
1635 * group100 parameter selects whether to show 1 out of 100 good frames.
1636 *
Ben Cahill9fbab512007-11-29 11:09:47 +08001637 * TODO: This was originally written for 3945, need to audit for
1638 * proper operation with 4965.
Zhu Yib481de92007-09-25 17:54:57 -07001639 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001640void iwl4965_report_frame(struct iwl4965_priv *priv,
1641 struct iwl4965_rx_packet *pkt,
Zhu Yib481de92007-09-25 17:54:57 -07001642 struct ieee80211_hdr *header, int group100)
1643{
1644 u32 to_us;
1645 u32 print_summary = 0;
1646 u32 print_dump = 0; /* set to 1 to dump all frames' contents */
1647 u32 hundred = 0;
1648 u32 dataframe = 0;
1649 u16 fc;
1650 u16 seq_ctl;
1651 u16 channel;
1652 u16 phy_flags;
1653 int rate_sym;
1654 u16 length;
1655 u16 status;
1656 u16 bcn_tmr;
1657 u32 tsf_low;
1658 u64 tsf;
1659 u8 rssi;
1660 u8 agc;
1661 u16 sig_avg;
1662 u16 noise_diff;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001663 struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1664 struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1665 struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
Zhu Yib481de92007-09-25 17:54:57 -07001666 u8 *data = IWL_RX_DATA(pkt);
1667
1668 /* MAC header */
1669 fc = le16_to_cpu(header->frame_control);
1670 seq_ctl = le16_to_cpu(header->seq_ctrl);
1671
1672 /* metadata */
1673 channel = le16_to_cpu(rx_hdr->channel);
1674 phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1675 rate_sym = rx_hdr->rate;
1676 length = le16_to_cpu(rx_hdr->len);
1677
1678 /* end-of-frame status and timestamp */
1679 status = le32_to_cpu(rx_end->status);
1680 bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1681 tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1682 tsf = le64_to_cpu(rx_end->timestamp);
1683
1684 /* signal statistics */
1685 rssi = rx_stats->rssi;
1686 agc = rx_stats->agc;
1687 sig_avg = le16_to_cpu(rx_stats->sig_avg);
1688 noise_diff = le16_to_cpu(rx_stats->noise_diff);
1689
1690 to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1691
1692 /* if data frame is to us and all is good,
1693 * (optionally) print summary for only 1 out of every 100 */
1694 if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1695 (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1696 dataframe = 1;
1697 if (!group100)
1698 print_summary = 1; /* print each frame */
1699 else if (priv->framecnt_to_us < 100) {
1700 priv->framecnt_to_us++;
1701 print_summary = 0;
1702 } else {
1703 priv->framecnt_to_us = 0;
1704 print_summary = 1;
1705 hundred = 1;
1706 }
1707 } else {
1708 /* print summary for all other frames */
1709 print_summary = 1;
1710 }
1711
1712 if (print_summary) {
1713 char *title;
1714 u32 rate;
1715
1716 if (hundred)
1717 title = "100Frames";
1718 else if (fc & IEEE80211_FCTL_RETRY)
1719 title = "Retry";
1720 else if (ieee80211_is_assoc_response(fc))
1721 title = "AscRsp";
1722 else if (ieee80211_is_reassoc_response(fc))
1723 title = "RasRsp";
1724 else if (ieee80211_is_probe_response(fc)) {
1725 title = "PrbRsp";
1726 print_dump = 1; /* dump frame contents */
1727 } else if (ieee80211_is_beacon(fc)) {
1728 title = "Beacon";
1729 print_dump = 1; /* dump frame contents */
1730 } else if (ieee80211_is_atim(fc))
1731 title = "ATIM";
1732 else if (ieee80211_is_auth(fc))
1733 title = "Auth";
1734 else if (ieee80211_is_deauth(fc))
1735 title = "DeAuth";
1736 else if (ieee80211_is_disassoc(fc))
1737 title = "DisAssoc";
1738 else
1739 title = "Frame";
1740
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001741 rate = iwl4965_rate_index_from_plcp(rate_sym);
Zhu Yib481de92007-09-25 17:54:57 -07001742 if (rate == -1)
1743 rate = 0;
1744 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001745 rate = iwl4965_rates[rate].ieee / 2;
Zhu Yib481de92007-09-25 17:54:57 -07001746
1747 /* print frame summary.
1748 * MAC addresses show just the last byte (for brevity),
1749 * but you can hack it to show more, if you'd like to. */
1750 if (dataframe)
1751 IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1752 "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1753 title, fc, header->addr1[5],
1754 length, rssi, channel, rate);
1755 else {
1756 /* src/dst addresses assume managed mode */
1757 IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1758 "src=0x%02x, rssi=%u, tim=%lu usec, "
1759 "phy=0x%02x, chnl=%d\n",
1760 title, fc, header->addr1[5],
1761 header->addr3[5], rssi,
1762 tsf_low - priv->scan_start_tsf,
1763 phy_flags, channel);
1764 }
1765 }
1766 if (print_dump)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001767 iwl4965_print_hex_dump(IWL_DL_RX, data, length);
Zhu Yib481de92007-09-25 17:54:57 -07001768}
1769#endif
1770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001771static void iwl4965_unset_hw_setting(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001772{
1773 if (priv->hw_setting.shared_virt)
1774 pci_free_consistent(priv->pci_dev,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001775 sizeof(struct iwl4965_shared),
Zhu Yib481de92007-09-25 17:54:57 -07001776 priv->hw_setting.shared_virt,
1777 priv->hw_setting.shared_phys);
1778}
1779
1780/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001781 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
Zhu Yib481de92007-09-25 17:54:57 -07001782 *
1783 * return : set the bit for each supported rate insert in ie
1784 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001785static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
Tomas Winklerc7c46672007-10-18 02:04:15 +02001786 u16 basic_rate, int *left)
Zhu Yib481de92007-09-25 17:54:57 -07001787{
1788 u16 ret_rates = 0, bit;
1789 int i;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001790 u8 *cnt = ie;
1791 u8 *rates = ie + 1;
Zhu Yib481de92007-09-25 17:54:57 -07001792
1793 for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1794 if (bit & supported_rate) {
1795 ret_rates |= bit;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001796 rates[*cnt] = iwl4965_rates[i].ieee |
Tomas Winklerc7c46672007-10-18 02:04:15 +02001797 ((bit & basic_rate) ? 0x80 : 0x00);
1798 (*cnt)++;
1799 (*left)--;
1800 if ((*left <= 0) ||
1801 (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
Zhu Yib481de92007-09-25 17:54:57 -07001802 break;
1803 }
1804 }
1805
1806 return ret_rates;
1807}
1808
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001809#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001810void static iwl4965_set_ht_capab(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07001811 struct ieee80211_ht_capability *ht_cap,
1812 u8 use_wide_chan);
1813#endif
1814
1815/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001816 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
Zhu Yib481de92007-09-25 17:54:57 -07001817 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001818static u16 iwl4965_fill_probe_req(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07001819 struct ieee80211_mgmt *frame,
1820 int left, int is_direct)
1821{
1822 int len = 0;
1823 u8 *pos = NULL;
mabbasbee488d2007-10-25 17:15:42 +08001824 u16 active_rates, ret_rates, cck_rates, active_rate_basic;
Zhu Yib481de92007-09-25 17:54:57 -07001825
1826 /* Make sure there is enough space for the probe request,
1827 * two mandatory IEs and the data */
1828 left -= 24;
1829 if (left < 0)
1830 return 0;
1831 len += 24;
1832
1833 frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001834 memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001835 memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001836 memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
Zhu Yib481de92007-09-25 17:54:57 -07001837 frame->seq_ctrl = 0;
1838
1839 /* fill in our indirect SSID IE */
1840 /* ...next IE... */
1841
1842 left -= 2;
1843 if (left < 0)
1844 return 0;
1845 len += 2;
1846 pos = &(frame->u.probe_req.variable[0]);
1847 *pos++ = WLAN_EID_SSID;
1848 *pos++ = 0;
1849
1850 /* fill in our direct SSID IE... */
1851 if (is_direct) {
1852 /* ...next IE... */
1853 left -= 2 + priv->essid_len;
1854 if (left < 0)
1855 return 0;
1856 /* ... fill it in... */
1857 *pos++ = WLAN_EID_SSID;
1858 *pos++ = priv->essid_len;
1859 memcpy(pos, priv->essid, priv->essid_len);
1860 pos += priv->essid_len;
1861 len += 2 + priv->essid_len;
1862 }
1863
1864 /* fill in supported rate */
1865 /* ...next IE... */
1866 left -= 2;
1867 if (left < 0)
1868 return 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001869
Zhu Yib481de92007-09-25 17:54:57 -07001870 /* ... fill it in... */
1871 *pos++ = WLAN_EID_SUPP_RATES;
1872 *pos = 0;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001873
mabbasbee488d2007-10-25 17:15:42 +08001874 /* exclude 60M rate */
1875 active_rates = priv->rates_mask;
1876 active_rates &= ~IWL_RATE_60M_MASK;
1877
1878 active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
Zhu Yib481de92007-09-25 17:54:57 -07001879
Tomas Winklerc7c46672007-10-18 02:04:15 +02001880 cck_rates = IWL_CCK_RATES_MASK & active_rates;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001881 ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
mabbasbee488d2007-10-25 17:15:42 +08001882 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001883 active_rates &= ~ret_rates;
1884
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001885 ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001886 active_rate_basic, &left);
Tomas Winklerc7c46672007-10-18 02:04:15 +02001887 active_rates &= ~ret_rates;
1888
Zhu Yib481de92007-09-25 17:54:57 -07001889 len += 2 + *pos;
1890 pos += (*pos) + 1;
Tomas Winklerc7c46672007-10-18 02:04:15 +02001891 if (active_rates == 0)
Zhu Yib481de92007-09-25 17:54:57 -07001892 goto fill_end;
1893
1894 /* fill in supported extended rate */
1895 /* ...next IE... */
1896 left -= 2;
1897 if (left < 0)
1898 return 0;
1899 /* ... fill it in... */
1900 *pos++ = WLAN_EID_EXT_SUPP_RATES;
1901 *pos = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001902 iwl4965_supported_rate_to_ie(pos, active_rates,
mabbasbee488d2007-10-25 17:15:42 +08001903 active_rate_basic, &left);
Zhu Yib481de92007-09-25 17:54:57 -07001904 if (*pos > 0)
1905 len += 2 + *pos;
1906
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001907#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07001908 if (is_direct && priv->is_ht_enabled) {
1909 u8 use_wide_chan = 1;
1910
1911 if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
1912 use_wide_chan = 0;
1913 pos += (*pos) + 1;
1914 *pos++ = WLAN_EID_HT_CAPABILITY;
1915 *pos++ = sizeof(struct ieee80211_ht_capability);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001916 iwl4965_set_ht_capab(NULL, (struct ieee80211_ht_capability *)pos,
Zhu Yib481de92007-09-25 17:54:57 -07001917 use_wide_chan);
1918 len += 2 + sizeof(struct ieee80211_ht_capability);
1919 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001920#endif /*CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07001921
1922 fill_end:
1923 return (u16)len;
1924}
1925
1926/*
1927 * QoS support
1928*/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08001929#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001930static int iwl4965_send_qos_params_command(struct iwl4965_priv *priv,
1931 struct iwl4965_qosparam_cmd *qos)
Zhu Yib481de92007-09-25 17:54:57 -07001932{
1933
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001934 return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
1935 sizeof(struct iwl4965_qosparam_cmd), qos);
Zhu Yib481de92007-09-25 17:54:57 -07001936}
1937
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08001938static void iwl4965_reset_qos(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07001939{
1940 u16 cw_min = 15;
1941 u16 cw_max = 1023;
1942 u8 aifs = 2;
1943 u8 is_legacy = 0;
1944 unsigned long flags;
1945 int i;
1946
1947 spin_lock_irqsave(&priv->lock, flags);
1948 priv->qos_data.qos_active = 0;
1949
1950 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
1951 if (priv->qos_data.qos_enable)
1952 priv->qos_data.qos_active = 1;
1953 if (!(priv->active_rate & 0xfff0)) {
1954 cw_min = 31;
1955 is_legacy = 1;
1956 }
1957 } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1958 if (priv->qos_data.qos_enable)
1959 priv->qos_data.qos_active = 1;
1960 } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
1961 cw_min = 31;
1962 is_legacy = 1;
1963 }
1964
1965 if (priv->qos_data.qos_active)
1966 aifs = 3;
1967
1968 priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
1969 priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
1970 priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
1971 priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
1972 priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
1973
1974 if (priv->qos_data.qos_active) {
1975 i = 1;
1976 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
1977 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
1978 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
1979 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
1980 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1981
1982 i = 2;
1983 priv->qos_data.def_qos_parm.ac[i].cw_min =
1984 cpu_to_le16((cw_min + 1) / 2 - 1);
1985 priv->qos_data.def_qos_parm.ac[i].cw_max =
1986 cpu_to_le16(cw_max);
1987 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
1988 if (is_legacy)
1989 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1990 cpu_to_le16(6016);
1991 else
1992 priv->qos_data.def_qos_parm.ac[i].edca_txop =
1993 cpu_to_le16(3008);
1994 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
1995
1996 i = 3;
1997 priv->qos_data.def_qos_parm.ac[i].cw_min =
1998 cpu_to_le16((cw_min + 1) / 4 - 1);
1999 priv->qos_data.def_qos_parm.ac[i].cw_max =
2000 cpu_to_le16((cw_max + 1) / 2 - 1);
2001 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2002 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2003 if (is_legacy)
2004 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2005 cpu_to_le16(3264);
2006 else
2007 priv->qos_data.def_qos_parm.ac[i].edca_txop =
2008 cpu_to_le16(1504);
2009 } else {
2010 for (i = 1; i < 4; i++) {
2011 priv->qos_data.def_qos_parm.ac[i].cw_min =
2012 cpu_to_le16(cw_min);
2013 priv->qos_data.def_qos_parm.ac[i].cw_max =
2014 cpu_to_le16(cw_max);
2015 priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
2016 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2017 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2018 }
2019 }
2020 IWL_DEBUG_QOS("set QoS to default \n");
2021
2022 spin_unlock_irqrestore(&priv->lock, flags);
2023}
2024
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002025static void iwl4965_activate_qos(struct iwl4965_priv *priv, u8 force)
Zhu Yib481de92007-09-25 17:54:57 -07002026{
2027 unsigned long flags;
2028
Zhu Yib481de92007-09-25 17:54:57 -07002029 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2030 return;
2031
2032 if (!priv->qos_data.qos_enable)
2033 return;
2034
2035 spin_lock_irqsave(&priv->lock, flags);
2036 priv->qos_data.def_qos_parm.qos_flags = 0;
2037
2038 if (priv->qos_data.qos_cap.q_AP.queue_request &&
2039 !priv->qos_data.qos_cap.q_AP.txop_request)
2040 priv->qos_data.def_qos_parm.qos_flags |=
2041 QOS_PARAM_FLG_TXOP_TYPE_MSK;
Zhu Yib481de92007-09-25 17:54:57 -07002042 if (priv->qos_data.qos_active)
2043 priv->qos_data.def_qos_parm.qos_flags |=
2044 QOS_PARAM_FLG_UPDATE_EDCA_MSK;
2045
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002046#ifdef CONFIG_IWL4965_HT
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08002047 if (priv->is_ht_enabled && priv->current_assoc_ht.is_ht)
2048 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002049#endif /* CONFIG_IWL4965_HT */
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08002050
Zhu Yib481de92007-09-25 17:54:57 -07002051 spin_unlock_irqrestore(&priv->lock, flags);
2052
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002053 if (force || iwl4965_is_associated(priv)) {
Tomas Winklerf1f1f5c2007-10-25 17:15:26 +08002054 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
2055 priv->qos_data.qos_active,
2056 priv->qos_data.def_qos_parm.qos_flags);
Zhu Yib481de92007-09-25 17:54:57 -07002057
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002058 iwl4965_send_qos_params_command(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002059 &(priv->qos_data.def_qos_parm));
2060 }
2061}
2062
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002063#endif /* CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07002064/*
2065 * Power management (not Tx power!) functions
2066 */
2067#define MSEC_TO_USEC 1024
2068
2069#define NOSLP __constant_cpu_to_le16(0), 0, 0
2070#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
2071#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2072#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2073 __constant_cpu_to_le32(X1), \
2074 __constant_cpu_to_le32(X2), \
2075 __constant_cpu_to_le32(X3), \
2076 __constant_cpu_to_le32(X4)}
2077
2078
2079/* default power management (not Tx power) table values */
2080/* for tim 0-10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002081static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07002082 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2083 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2084 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2085 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2086 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2087 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2088};
2089
2090/* for tim > 10 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002091static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
Zhu Yib481de92007-09-25 17:54:57 -07002092 {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2093 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2094 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2095 {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2096 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2097 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2098 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2099 {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2100 {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2101 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2102};
2103
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002104int iwl4965_power_init_handle(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002105{
2106 int rc = 0, i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002107 struct iwl4965_power_mgr *pow_data;
2108 int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
Zhu Yib481de92007-09-25 17:54:57 -07002109 u16 pci_pm;
2110
2111 IWL_DEBUG_POWER("Initialize power \n");
2112
2113 pow_data = &(priv->power_data);
2114
2115 memset(pow_data, 0, sizeof(*pow_data));
2116
2117 pow_data->active_index = IWL_POWER_RANGE_0;
2118 pow_data->dtim_val = 0xffff;
2119
2120 memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2121 memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2122
2123 rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2124 if (rc != 0)
2125 return 0;
2126 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002127 struct iwl4965_powertable_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002128
2129 IWL_DEBUG_POWER("adjust power command flags\n");
2130
2131 for (i = 0; i < IWL_POWER_AC; i++) {
2132 cmd = &pow_data->pwr_range_0[i].cmd;
2133
2134 if (pci_pm & 0x1)
2135 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2136 else
2137 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2138 }
2139 }
2140 return rc;
2141}
2142
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002143static int iwl4965_update_power_cmd(struct iwl4965_priv *priv,
2144 struct iwl4965_powertable_cmd *cmd, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002145{
2146 int rc = 0, i;
2147 u8 skip;
2148 u32 max_sleep = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002149 struct iwl4965_power_vec_entry *range;
Zhu Yib481de92007-09-25 17:54:57 -07002150 u8 period = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002151 struct iwl4965_power_mgr *pow_data;
Zhu Yib481de92007-09-25 17:54:57 -07002152
2153 if (mode > IWL_POWER_INDEX_5) {
2154 IWL_DEBUG_POWER("Error invalid power mode \n");
2155 return -1;
2156 }
2157 pow_data = &(priv->power_data);
2158
2159 if (pow_data->active_index == IWL_POWER_RANGE_0)
2160 range = &pow_data->pwr_range_0[0];
2161 else
2162 range = &pow_data->pwr_range_1[1];
2163
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002164 memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
Zhu Yib481de92007-09-25 17:54:57 -07002165
2166#ifdef IWL_MAC80211_DISABLE
2167 if (priv->assoc_network != NULL) {
2168 unsigned long flags;
2169
2170 period = priv->assoc_network->tim.tim_period;
2171 }
2172#endif /*IWL_MAC80211_DISABLE */
2173 skip = range[mode].no_dtim;
2174
2175 if (period == 0) {
2176 period = 1;
2177 skip = 0;
2178 }
2179
2180 if (skip == 0) {
2181 max_sleep = period;
2182 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2183 } else {
2184 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2185 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2186 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2187 }
2188
2189 for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2190 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2191 cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2192 }
2193
2194 IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2195 IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2196 IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2197 IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2198 le32_to_cpu(cmd->sleep_interval[0]),
2199 le32_to_cpu(cmd->sleep_interval[1]),
2200 le32_to_cpu(cmd->sleep_interval[2]),
2201 le32_to_cpu(cmd->sleep_interval[3]),
2202 le32_to_cpu(cmd->sleep_interval[4]));
2203
2204 return rc;
2205}
2206
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002207static int iwl4965_send_power_mode(struct iwl4965_priv *priv, u32 mode)
Zhu Yib481de92007-09-25 17:54:57 -07002208{
John W. Linville9a62f732007-11-15 16:27:36 -05002209 u32 uninitialized_var(final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002210 int rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002211 struct iwl4965_powertable_cmd cmd;
Zhu Yib481de92007-09-25 17:54:57 -07002212
2213 /* If on battery, set to 3,
Ian Schram01ebd062007-10-25 17:15:22 +08002214 * if plugged into AC power, set to CAM ("continuously aware mode"),
Zhu Yib481de92007-09-25 17:54:57 -07002215 * else user level */
2216 switch (mode) {
2217 case IWL_POWER_BATTERY:
2218 final_mode = IWL_POWER_INDEX_3;
2219 break;
2220 case IWL_POWER_AC:
2221 final_mode = IWL_POWER_MODE_CAM;
2222 break;
2223 default:
2224 final_mode = mode;
2225 break;
2226 }
2227
2228 cmd.keep_alive_beacons = 0;
2229
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002230 iwl4965_update_power_cmd(priv, &cmd, final_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002231
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002232 rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07002233
2234 if (final_mode == IWL_POWER_MODE_CAM)
2235 clear_bit(STATUS_POWER_PMI, &priv->status);
2236 else
2237 set_bit(STATUS_POWER_PMI, &priv->status);
2238
2239 return rc;
2240}
2241
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002242int iwl4965_is_network_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07002243{
2244 /* Filter incoming packets to determine if they are targeted toward
2245 * this network, discarding packets coming from ourselves */
2246 switch (priv->iw_mode) {
2247 case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
2248 /* packets from our adapter are dropped (echo) */
2249 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2250 return 0;
2251 /* {broad,multi}cast packets to our IBSS go through */
2252 if (is_multicast_ether_addr(header->addr1))
2253 return !compare_ether_addr(header->addr3, priv->bssid);
2254 /* packets to our adapter go through */
2255 return !compare_ether_addr(header->addr1, priv->mac_addr);
2256 case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2257 /* packets from our adapter are dropped (echo) */
2258 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2259 return 0;
2260 /* {broad,multi}cast packets to our BSS go through */
2261 if (is_multicast_ether_addr(header->addr1))
2262 return !compare_ether_addr(header->addr2, priv->bssid);
2263 /* packets to our adapter go through */
2264 return !compare_ether_addr(header->addr1, priv->mac_addr);
2265 }
2266
2267 return 1;
2268}
2269
2270#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2271
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002272static const char *iwl4965_get_tx_fail_reason(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07002273{
2274 switch (status & TX_STATUS_MSK) {
2275 case TX_STATUS_SUCCESS:
2276 return "SUCCESS";
2277 TX_STATUS_ENTRY(SHORT_LIMIT);
2278 TX_STATUS_ENTRY(LONG_LIMIT);
2279 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2280 TX_STATUS_ENTRY(MGMNT_ABORT);
2281 TX_STATUS_ENTRY(NEXT_FRAG);
2282 TX_STATUS_ENTRY(LIFE_EXPIRE);
2283 TX_STATUS_ENTRY(DEST_PS);
2284 TX_STATUS_ENTRY(ABORTED);
2285 TX_STATUS_ENTRY(BT_RETRY);
2286 TX_STATUS_ENTRY(STA_INVALID);
2287 TX_STATUS_ENTRY(FRAG_DROPPED);
2288 TX_STATUS_ENTRY(TID_DISABLE);
2289 TX_STATUS_ENTRY(FRAME_FLUSHED);
2290 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2291 TX_STATUS_ENTRY(TX_LOCKED);
2292 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2293 }
2294
2295 return "UNKNOWN";
2296}
2297
2298/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002299 * iwl4965_scan_cancel - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002300 *
2301 * NOTE: priv->mutex is not required before calling this function
2302 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002303static int iwl4965_scan_cancel(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002304{
2305 if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2306 clear_bit(STATUS_SCANNING, &priv->status);
2307 return 0;
2308 }
2309
2310 if (test_bit(STATUS_SCANNING, &priv->status)) {
2311 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2312 IWL_DEBUG_SCAN("Queuing scan abort.\n");
2313 set_bit(STATUS_SCAN_ABORTING, &priv->status);
2314 queue_work(priv->workqueue, &priv->abort_scan);
2315
2316 } else
2317 IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2318
2319 return test_bit(STATUS_SCANNING, &priv->status);
2320 }
2321
2322 return 0;
2323}
2324
2325/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002326 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
Zhu Yib481de92007-09-25 17:54:57 -07002327 * @ms: amount of time to wait (in milliseconds) for scan to abort
2328 *
2329 * NOTE: priv->mutex must be held before calling this function
2330 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002331static int iwl4965_scan_cancel_timeout(struct iwl4965_priv *priv, unsigned long ms)
Zhu Yib481de92007-09-25 17:54:57 -07002332{
2333 unsigned long now = jiffies;
2334 int ret;
2335
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002336 ret = iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002337 if (ret && ms) {
2338 mutex_unlock(&priv->mutex);
2339 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2340 test_bit(STATUS_SCANNING, &priv->status))
2341 msleep(1);
2342 mutex_lock(&priv->mutex);
2343
2344 return test_bit(STATUS_SCANNING, &priv->status);
2345 }
2346
2347 return ret;
2348}
2349
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002350static void iwl4965_sequence_reset(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002351{
2352 /* Reset ieee stats */
2353
2354 /* We don't reset the net_device_stats (ieee->stats) on
2355 * re-association */
2356
2357 priv->last_seq_num = -1;
2358 priv->last_frag_num = -1;
2359 priv->last_packet_time = 0;
2360
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002361 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002362}
2363
2364#define MAX_UCODE_BEACON_INTERVAL 4096
2365#define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
2366
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002367static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
Zhu Yib481de92007-09-25 17:54:57 -07002368{
2369 u16 new_val = 0;
2370 u16 beacon_factor = 0;
2371
2372 beacon_factor =
2373 (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2374 / MAX_UCODE_BEACON_INTERVAL;
2375 new_val = beacon_val / beacon_factor;
2376
2377 return cpu_to_le16(new_val);
2378}
2379
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002380static void iwl4965_setup_rxon_timing(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002381{
2382 u64 interval_tm_unit;
2383 u64 tsf, result;
2384 unsigned long flags;
2385 struct ieee80211_conf *conf = NULL;
2386 u16 beacon_int = 0;
2387
2388 conf = ieee80211_get_hw_conf(priv->hw);
2389
2390 spin_lock_irqsave(&priv->lock, flags);
2391 priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2392 priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2393
2394 priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2395
2396 tsf = priv->timestamp1;
2397 tsf = ((tsf << 32) | priv->timestamp0);
2398
2399 beacon_int = priv->beacon_int;
2400 spin_unlock_irqrestore(&priv->lock, flags);
2401
2402 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2403 if (beacon_int == 0) {
2404 priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2405 priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2406 } else {
2407 priv->rxon_timing.beacon_interval =
2408 cpu_to_le16(beacon_int);
2409 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002410 iwl4965_adjust_beacon_interval(
Zhu Yib481de92007-09-25 17:54:57 -07002411 le16_to_cpu(priv->rxon_timing.beacon_interval));
2412 }
2413
2414 priv->rxon_timing.atim_window = 0;
2415 } else {
2416 priv->rxon_timing.beacon_interval =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002417 iwl4965_adjust_beacon_interval(conf->beacon_int);
Zhu Yib481de92007-09-25 17:54:57 -07002418 /* TODO: we need to get atim_window from upper stack
2419 * for now we set to 0 */
2420 priv->rxon_timing.atim_window = 0;
2421 }
2422
2423 interval_tm_unit =
2424 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2425 result = do_div(tsf, interval_tm_unit);
2426 priv->rxon_timing.beacon_init_val =
2427 cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2428
2429 IWL_DEBUG_ASSOC
2430 ("beacon interval %d beacon timer %d beacon tim %d\n",
2431 le16_to_cpu(priv->rxon_timing.beacon_interval),
2432 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2433 le16_to_cpu(priv->rxon_timing.atim_window));
2434}
2435
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002436static int iwl4965_scan_initiate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002437{
2438 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2439 IWL_ERROR("APs don't scan.\n");
2440 return 0;
2441 }
2442
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002443 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002444 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2445 return -EIO;
2446 }
2447
2448 if (test_bit(STATUS_SCANNING, &priv->status)) {
2449 IWL_DEBUG_SCAN("Scan already in progress.\n");
2450 return -EAGAIN;
2451 }
2452
2453 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2454 IWL_DEBUG_SCAN("Scan request while abort pending. "
2455 "Queuing.\n");
2456 return -EAGAIN;
2457 }
2458
2459 IWL_DEBUG_INFO("Starting scan...\n");
2460 priv->scan_bands = 2;
2461 set_bit(STATUS_SCANNING, &priv->status);
2462 priv->scan_start = jiffies;
2463 priv->scan_pass_start = priv->scan_start;
2464
2465 queue_work(priv->workqueue, &priv->request_scan);
2466
2467 return 0;
2468}
2469
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002470static int iwl4965_set_rxon_hwcrypto(struct iwl4965_priv *priv, int hw_decrypt)
Zhu Yib481de92007-09-25 17:54:57 -07002471{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002472 struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
Zhu Yib481de92007-09-25 17:54:57 -07002473
2474 if (hw_decrypt)
2475 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2476 else
2477 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2478
2479 return 0;
2480}
2481
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002482static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv, u8 phymode)
Zhu Yib481de92007-09-25 17:54:57 -07002483{
2484 if (phymode == MODE_IEEE80211A) {
2485 priv->staging_rxon.flags &=
2486 ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2487 | RXON_FLG_CCK_MSK);
2488 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2489 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002490 /* Copied from iwl4965_bg_post_associate() */
Zhu Yib481de92007-09-25 17:54:57 -07002491 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2492 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2493 else
2494 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2495
2496 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2497 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2498
2499 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2500 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2501 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2502 }
2503}
2504
2505/*
Ian Schram01ebd062007-10-25 17:15:22 +08002506 * initialize rxon structure with default values from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07002507 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002508static void iwl4965_connection_init_rx_config(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002509{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002510 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002511
2512 memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2513
2514 switch (priv->iw_mode) {
2515 case IEEE80211_IF_TYPE_AP:
2516 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2517 break;
2518
2519 case IEEE80211_IF_TYPE_STA:
2520 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2521 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2522 break;
2523
2524 case IEEE80211_IF_TYPE_IBSS:
2525 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2526 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2527 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2528 RXON_FILTER_ACCEPT_GRP_MSK;
2529 break;
2530
2531 case IEEE80211_IF_TYPE_MNTR:
2532 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2533 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2534 RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2535 break;
2536 }
2537
2538#if 0
2539 /* TODO: Figure out when short_preamble would be set and cache from
2540 * that */
2541 if (!hw_to_local(priv->hw)->short_preamble)
2542 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2543 else
2544 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2545#endif
2546
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002547 ch_info = iwl4965_get_channel_info(priv, priv->phymode,
Zhu Yib481de92007-09-25 17:54:57 -07002548 le16_to_cpu(priv->staging_rxon.channel));
2549
2550 if (!ch_info)
2551 ch_info = &priv->channel_info[0];
2552
2553 /*
2554 * in some case A channels are all non IBSS
2555 * in this case force B/G channel
2556 */
2557 if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2558 !(is_channel_ibss(ch_info)))
2559 ch_info = &priv->channel_info[0];
2560
2561 priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2562 if (is_channel_a_band(ch_info))
2563 priv->phymode = MODE_IEEE80211A;
2564 else
2565 priv->phymode = MODE_IEEE80211G;
2566
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002567 iwl4965_set_flags_for_phymode(priv, priv->phymode);
Zhu Yib481de92007-09-25 17:54:57 -07002568
2569 priv->staging_rxon.ofdm_basic_rates =
2570 (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2571 priv->staging_rxon.cck_basic_rates =
2572 (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2573
2574 priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2575 RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2576 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2577 memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2578 priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2579 priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2580 iwl4965_set_rxon_chain(priv);
2581}
2582
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002583static int iwl4965_set_mode(struct iwl4965_priv *priv, int mode)
Zhu Yib481de92007-09-25 17:54:57 -07002584{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002585 if (!iwl4965_is_ready_rf(priv))
Zhu Yib481de92007-09-25 17:54:57 -07002586 return -EAGAIN;
2587
2588 if (mode == IEEE80211_IF_TYPE_IBSS) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002589 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07002590
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002591 ch_info = iwl4965_get_channel_info(priv,
Zhu Yib481de92007-09-25 17:54:57 -07002592 priv->phymode,
2593 le16_to_cpu(priv->staging_rxon.channel));
2594
2595 if (!ch_info || !is_channel_ibss(ch_info)) {
2596 IWL_ERROR("channel %d not IBSS channel\n",
2597 le16_to_cpu(priv->staging_rxon.channel));
2598 return -EINVAL;
2599 }
2600 }
2601
2602 cancel_delayed_work(&priv->scan_check);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002603 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07002604 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2605 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2606 return -EAGAIN;
2607 }
2608
2609 priv->iw_mode = mode;
2610
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002611 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002612 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2613
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002614 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002615
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002616 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07002617
2618 return 0;
2619}
2620
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002621static void iwl4965_build_tx_cmd_hwcrypto(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002622 struct ieee80211_tx_control *ctl,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002623 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002624 struct sk_buff *skb_frag,
2625 int last_frag)
2626{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002627 struct iwl4965_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
Zhu Yib481de92007-09-25 17:54:57 -07002628
2629 switch (keyinfo->alg) {
2630 case ALG_CCMP:
2631 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2632 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2633 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2634 break;
2635
2636 case ALG_TKIP:
2637#if 0
2638 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2639
2640 if (last_frag)
2641 memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2642 8);
2643 else
2644 memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2645#endif
2646 break;
2647
2648 case ALG_WEP:
2649 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2650 (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2651
2652 if (keyinfo->keylen == 13)
2653 cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2654
2655 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2656
2657 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2658 "with key %d\n", ctl->key_idx);
2659 break;
2660
Zhu Yib481de92007-09-25 17:54:57 -07002661 default:
2662 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2663 break;
2664 }
2665}
2666
2667/*
2668 * handle build REPLY_TX command notification.
2669 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002670static void iwl4965_build_tx_cmd_basic(struct iwl4965_priv *priv,
2671 struct iwl4965_cmd *cmd,
Zhu Yib481de92007-09-25 17:54:57 -07002672 struct ieee80211_tx_control *ctrl,
2673 struct ieee80211_hdr *hdr,
2674 int is_unicast, u8 std_id)
2675{
2676 __le16 *qc;
2677 u16 fc = le16_to_cpu(hdr->frame_control);
2678 __le32 tx_flags = cmd->cmd.tx.tx_flags;
2679
2680 cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2681 if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2682 tx_flags |= TX_CMD_FLG_ACK_MSK;
2683 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2684 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2685 if (ieee80211_is_probe_response(fc) &&
2686 !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2687 tx_flags |= TX_CMD_FLG_TSF_MSK;
2688 } else {
2689 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2690 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2691 }
2692
2693 cmd->cmd.tx.sta_id = std_id;
2694 if (ieee80211_get_morefrag(hdr))
2695 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2696
2697 qc = ieee80211_get_qos_ctrl(hdr);
2698 if (qc) {
2699 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2700 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2701 } else
2702 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2703
2704 if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2705 tx_flags |= TX_CMD_FLG_RTS_MSK;
2706 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2707 } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2708 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2709 tx_flags |= TX_CMD_FLG_CTS_MSK;
2710 }
2711
2712 if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2713 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2714
2715 tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2716 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2717 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2718 (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
Ian Schrambc434dd2007-10-25 17:15:29 +08002719 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
Zhu Yib481de92007-09-25 17:54:57 -07002720 else
Ian Schrambc434dd2007-10-25 17:15:29 +08002721 cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
Zhu Yib481de92007-09-25 17:54:57 -07002722 } else
2723 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2724
2725 cmd->cmd.tx.driver_txop = 0;
2726 cmd->cmd.tx.tx_flags = tx_flags;
2727 cmd->cmd.tx.next_frame_len = 0;
2728}
2729
Ben Cahill9fbab512007-11-29 11:09:47 +08002730static int iwl4965_get_sta_id(struct iwl4965_priv *priv,
2731 struct ieee80211_hdr *hdr)
Zhu Yib481de92007-09-25 17:54:57 -07002732{
2733 int sta_id;
2734 u16 fc = le16_to_cpu(hdr->frame_control);
Joe Perches0795af52007-10-03 17:59:30 -07002735 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07002736
2737 /* If this frame is broadcast or not data then use the broadcast
2738 * station id */
2739 if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2740 is_multicast_ether_addr(hdr->addr1))
2741 return priv->hw_setting.bcast_sta_id;
2742
2743 switch (priv->iw_mode) {
2744
2745 /* If this frame is part of a BSS network (we're a station), then
2746 * we use the AP's station id */
2747 case IEEE80211_IF_TYPE_STA:
2748 return IWL_AP_ID;
2749
2750 /* If we are an AP, then find the station, or use BCAST */
2751 case IEEE80211_IF_TYPE_AP:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002752 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002753 if (sta_id != IWL_INVALID_STATION)
2754 return sta_id;
2755 return priv->hw_setting.bcast_sta_id;
2756
2757 /* If this frame is part of a IBSS network, then we use the
2758 * target specific station id */
2759 case IEEE80211_IF_TYPE_IBSS:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002760 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
Zhu Yib481de92007-09-25 17:54:57 -07002761 if (sta_id != IWL_INVALID_STATION)
2762 return sta_id;
2763
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002764 sta_id = iwl4965_add_station_flags(priv, hdr->addr1, 0, CMD_ASYNC);
Zhu Yib481de92007-09-25 17:54:57 -07002765
2766 if (sta_id != IWL_INVALID_STATION)
2767 return sta_id;
2768
Joe Perches0795af52007-10-03 17:59:30 -07002769 IWL_DEBUG_DROP("Station %s not in station map. "
Zhu Yib481de92007-09-25 17:54:57 -07002770 "Defaulting to broadcast...\n",
Joe Perches0795af52007-10-03 17:59:30 -07002771 print_mac(mac, hdr->addr1));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002772 iwl4965_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
Zhu Yib481de92007-09-25 17:54:57 -07002773 return priv->hw_setting.bcast_sta_id;
2774
2775 default:
Ian Schram01ebd062007-10-25 17:15:22 +08002776 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
Zhu Yib481de92007-09-25 17:54:57 -07002777 return priv->hw_setting.bcast_sta_id;
2778 }
2779}
2780
2781/*
2782 * start REPLY_TX command process
2783 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002784static int iwl4965_tx_skb(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07002785 struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2786{
2787 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002788 struct iwl4965_tfd_frame *tfd;
Zhu Yib481de92007-09-25 17:54:57 -07002789 u32 *control_flags;
2790 int txq_id = ctl->queue;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002791 struct iwl4965_tx_queue *txq = NULL;
2792 struct iwl4965_queue *q = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002793 dma_addr_t phys_addr;
2794 dma_addr_t txcmd_phys;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002795 struct iwl4965_cmd *out_cmd = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07002796 u16 len, idx, len_org;
2797 u8 id, hdr_len, unicast;
2798 u8 sta_id;
2799 u16 seq_number = 0;
2800 u16 fc;
2801 __le16 *qc;
2802 u8 wait_write_ptr = 0;
2803 unsigned long flags;
2804 int rc;
2805
2806 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002807 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07002808 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2809 goto drop_unlock;
2810 }
2811
2812 if (!priv->interface_id) {
2813 IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
2814 goto drop_unlock;
2815 }
2816
2817 if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2818 IWL_ERROR("ERROR: No TX rate available.\n");
2819 goto drop_unlock;
2820 }
2821
2822 unicast = !is_multicast_ether_addr(hdr->addr1);
2823 id = 0;
2824
2825 fc = le16_to_cpu(hdr->frame_control);
2826
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002827#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07002828 if (ieee80211_is_auth(fc))
2829 IWL_DEBUG_TX("Sending AUTH frame\n");
2830 else if (ieee80211_is_assoc_request(fc))
2831 IWL_DEBUG_TX("Sending ASSOC frame\n");
2832 else if (ieee80211_is_reassoc_request(fc))
2833 IWL_DEBUG_TX("Sending REASSOC frame\n");
2834#endif
2835
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002836 if (!iwl4965_is_associated(priv) &&
Zhu Yib481de92007-09-25 17:54:57 -07002837 ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002838 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
Zhu Yib481de92007-09-25 17:54:57 -07002839 goto drop_unlock;
2840 }
2841
2842 spin_unlock_irqrestore(&priv->lock, flags);
2843
2844 hdr_len = ieee80211_get_hdrlen(fc);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002845 sta_id = iwl4965_get_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002846 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07002847 DECLARE_MAC_BUF(mac);
2848
2849 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2850 print_mac(mac, hdr->addr1));
Zhu Yib481de92007-09-25 17:54:57 -07002851 goto drop;
2852 }
2853
2854 IWL_DEBUG_RATE("station Id %d\n", sta_id);
2855
2856 qc = ieee80211_get_qos_ctrl(hdr);
2857 if (qc) {
2858 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2859 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2860 IEEE80211_SCTL_SEQ;
2861 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2862 (hdr->seq_ctrl &
2863 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2864 seq_number += 0x10;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002865#ifdef CONFIG_IWL4965_HT
2866#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07002867 /* aggregation is on for this <sta,tid> */
2868 if (ctl->flags & IEEE80211_TXCTL_HT_MPDU_AGG)
2869 txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08002870#endif /* CONFIG_IWL4965_HT_AGG */
2871#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07002872 }
2873 txq = &priv->txq[txq_id];
2874 q = &txq->q;
2875
2876 spin_lock_irqsave(&priv->lock, flags);
2877
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002878 tfd = &txq->bd[q->write_ptr];
Zhu Yib481de92007-09-25 17:54:57 -07002879 memset(tfd, 0, sizeof(*tfd));
2880 control_flags = (u32 *) tfd;
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002881 idx = get_cmd_index(q, q->write_ptr, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002882
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002883 memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002884 txq->txb[q->write_ptr].skb[0] = skb;
2885 memcpy(&(txq->txb[q->write_ptr].status.control),
Zhu Yib481de92007-09-25 17:54:57 -07002886 ctl, sizeof(struct ieee80211_tx_control));
2887 out_cmd = &txq->cmd[idx];
2888 memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2889 memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
2890 out_cmd->hdr.cmd = REPLY_TX;
2891 out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
Tomas Winklerfc4b6852007-10-25 17:15:24 +08002892 INDEX_TO_SEQ(q->write_ptr)));
Zhu Yib481de92007-09-25 17:54:57 -07002893 /* copy frags header */
2894 memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
2895
2896 /* hdr = (struct ieee80211_hdr *)out_cmd->cmd.tx.hdr; */
2897 len = priv->hw_setting.tx_cmd_len +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002898 sizeof(struct iwl4965_cmd_header) + hdr_len;
Zhu Yib481de92007-09-25 17:54:57 -07002899
2900 len_org = len;
2901 len = (len + 3) & ~3;
2902
2903 if (len_org != len)
2904 len_org = 1;
2905 else
2906 len_org = 0;
2907
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002908 txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
2909 offsetof(struct iwl4965_cmd, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07002910
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002911 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
Zhu Yib481de92007-09-25 17:54:57 -07002912
2913 if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002914 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002915
2916 /* 802.11 null functions have no payload... */
2917 len = skb->len - hdr_len;
2918 if (len) {
2919 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
2920 len, PCI_DMA_TODEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002921 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
Zhu Yib481de92007-09-25 17:54:57 -07002922 }
2923
2924 if (len_org)
2925 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
2926
2927 len = (u16)skb->len;
2928 out_cmd->cmd.tx.len = cpu_to_le16(len);
2929
2930 /* TODO need this for burst mode later on */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002931 iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07002932
2933 /* set is_hcca to 0; it probably will never be implemented */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002934 iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
Zhu Yib481de92007-09-25 17:54:57 -07002935
2936 iwl4965_tx_cmd(priv, out_cmd, sta_id, txcmd_phys,
2937 hdr, hdr_len, ctl, NULL);
2938
2939 if (!ieee80211_get_morefrag(hdr)) {
2940 txq->need_update = 1;
2941 if (qc) {
2942 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2943 priv->stations[sta_id].tid[tid].seq_number = seq_number;
2944 }
2945 } else {
2946 wait_write_ptr = 1;
2947 txq->need_update = 0;
2948 }
2949
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002950 iwl4965_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
Zhu Yib481de92007-09-25 17:54:57 -07002951 sizeof(out_cmd->cmd.tx));
2952
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002953 iwl4965_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
Zhu Yib481de92007-09-25 17:54:57 -07002954 ieee80211_get_hdrlen(fc));
2955
2956 iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
2957
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002958 q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
2959 rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002960 spin_unlock_irqrestore(&priv->lock, flags);
2961
2962 if (rc)
2963 return rc;
2964
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002965 if ((iwl4965_queue_space(q) < q->high_mark)
Zhu Yib481de92007-09-25 17:54:57 -07002966 && priv->mac80211_registered) {
2967 if (wait_write_ptr) {
2968 spin_lock_irqsave(&priv->lock, flags);
2969 txq->need_update = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002970 iwl4965_tx_queue_update_write_ptr(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07002971 spin_unlock_irqrestore(&priv->lock, flags);
2972 }
2973
2974 ieee80211_stop_queue(priv->hw, ctl->queue);
2975 }
2976
2977 return 0;
2978
2979drop_unlock:
2980 spin_unlock_irqrestore(&priv->lock, flags);
2981drop:
2982 return -1;
2983}
2984
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002985static void iwl4965_set_rate(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07002986{
2987 const struct ieee80211_hw_mode *hw = NULL;
2988 struct ieee80211_rate *rate;
2989 int i;
2990
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08002991 hw = iwl4965_get_hw_mode(priv, priv->phymode);
Saleem Abdulrasoolc4ba9622007-11-18 23:59:08 -08002992 if (!hw) {
2993 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
2994 return;
2995 }
Zhu Yib481de92007-09-25 17:54:57 -07002996
2997 priv->active_rate = 0;
2998 priv->active_rate_basic = 0;
2999
3000 IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
3001 hw->mode == MODE_IEEE80211A ?
3002 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
3003
3004 for (i = 0; i < hw->num_rates; i++) {
3005 rate = &(hw->rates[i]);
3006 if ((rate->val < IWL_RATE_COUNT) &&
3007 (rate->flags & IEEE80211_RATE_SUPPORTED)) {
3008 IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003009 rate->val, iwl4965_rates[rate->val].plcp,
Zhu Yib481de92007-09-25 17:54:57 -07003010 (rate->flags & IEEE80211_RATE_BASIC) ?
3011 "*" : "");
3012 priv->active_rate |= (1 << rate->val);
3013 if (rate->flags & IEEE80211_RATE_BASIC)
3014 priv->active_rate_basic |= (1 << rate->val);
3015 } else
3016 IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003017 rate->val, iwl4965_rates[rate->val].plcp);
Zhu Yib481de92007-09-25 17:54:57 -07003018 }
3019
3020 IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
3021 priv->active_rate, priv->active_rate_basic);
3022
3023 /*
3024 * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
3025 * otherwise set it to the default of all CCK rates and 6, 12, 24 for
3026 * OFDM
3027 */
3028 if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
3029 priv->staging_rxon.cck_basic_rates =
3030 ((priv->active_rate_basic &
3031 IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
3032 else
3033 priv->staging_rxon.cck_basic_rates =
3034 (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
3035
3036 if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
3037 priv->staging_rxon.ofdm_basic_rates =
3038 ((priv->active_rate_basic &
3039 (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
3040 IWL_FIRST_OFDM_RATE) & 0xFF;
3041 else
3042 priv->staging_rxon.ofdm_basic_rates =
3043 (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
3044}
3045
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003046static void iwl4965_radio_kill_sw(struct iwl4965_priv *priv, int disable_radio)
Zhu Yib481de92007-09-25 17:54:57 -07003047{
3048 unsigned long flags;
3049
3050 if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
3051 return;
3052
3053 IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
3054 disable_radio ? "OFF" : "ON");
3055
3056 if (disable_radio) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003057 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003058 /* FIXME: This is a workaround for AP */
3059 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3060 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003061 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07003062 CSR_UCODE_SW_BIT_RFKILL);
3063 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003064 iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
Zhu Yib481de92007-09-25 17:54:57 -07003065 set_bit(STATUS_RF_KILL_SW, &priv->status);
3066 }
3067 return;
3068 }
3069
3070 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003071 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07003072
3073 clear_bit(STATUS_RF_KILL_SW, &priv->status);
3074 spin_unlock_irqrestore(&priv->lock, flags);
3075
3076 /* wake up ucode */
3077 msleep(10);
3078
3079 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003080 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
3081 if (!iwl4965_grab_nic_access(priv))
3082 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003083 spin_unlock_irqrestore(&priv->lock, flags);
3084
3085 if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
3086 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3087 "disabled by HW switch\n");
3088 return;
3089 }
3090
3091 queue_work(priv->workqueue, &priv->restart);
3092 return;
3093}
3094
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003095void iwl4965_set_decrypted_flag(struct iwl4965_priv *priv, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07003096 u32 decrypt_res, struct ieee80211_rx_status *stats)
3097{
3098 u16 fc =
3099 le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
3100
3101 if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
3102 return;
3103
3104 if (!(fc & IEEE80211_FCTL_PROTECTED))
3105 return;
3106
3107 IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
3108 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
3109 case RX_RES_STATUS_SEC_TYPE_TKIP:
3110 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3111 RX_RES_STATUS_BAD_ICV_MIC)
3112 stats->flag |= RX_FLAG_MMIC_ERROR;
3113 case RX_RES_STATUS_SEC_TYPE_WEP:
3114 case RX_RES_STATUS_SEC_TYPE_CCMP:
3115 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
3116 RX_RES_STATUS_DECRYPT_OK) {
3117 IWL_DEBUG_RX("hw decrypt successfully!!!\n");
3118 stats->flag |= RX_FLAG_DECRYPTED;
3119 }
3120 break;
3121
3122 default:
3123 break;
3124 }
3125}
3126
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003127void iwl4965_handle_data_packet_monitor(struct iwl4965_priv *priv,
3128 struct iwl4965_rx_mem_buffer *rxb,
Zhu Yib481de92007-09-25 17:54:57 -07003129 void *data, short len,
3130 struct ieee80211_rx_status *stats,
3131 u16 phy_flags)
3132{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003133 struct iwl4965_rt_rx_hdr *iwl4965_rt;
Zhu Yib481de92007-09-25 17:54:57 -07003134
3135 /* First cache any information we need before we overwrite
3136 * the information provided in the skb from the hardware */
3137 s8 signal = stats->ssi;
3138 s8 noise = 0;
3139 int rate = stats->rate;
3140 u64 tsf = stats->mactime;
3141 __le16 phy_flags_hw = cpu_to_le16(phy_flags);
3142
3143 /* We received data from the HW, so stop the watchdog */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003144 if (len > IWL_RX_BUF_SIZE - sizeof(*iwl4965_rt)) {
Zhu Yib481de92007-09-25 17:54:57 -07003145 IWL_DEBUG_DROP("Dropping too large packet in monitor\n");
3146 return;
3147 }
3148
3149 /* copy the frame data to write after where the radiotap header goes */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003150 iwl4965_rt = (void *)rxb->skb->data;
3151 memmove(iwl4965_rt->payload, data, len);
Zhu Yib481de92007-09-25 17:54:57 -07003152
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003153 iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
3154 iwl4965_rt->rt_hdr.it_pad = 0; /* always good to zero */
Zhu Yib481de92007-09-25 17:54:57 -07003155
3156 /* total header + data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003157 iwl4965_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*iwl4965_rt));
Zhu Yib481de92007-09-25 17:54:57 -07003158
3159 /* Set the size of the skb to the size of the frame */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003160 skb_put(rxb->skb, sizeof(*iwl4965_rt) + len);
Zhu Yib481de92007-09-25 17:54:57 -07003161
3162 /* Big bitfield of all the fields we provide in radiotap */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003163 iwl4965_rt->rt_hdr.it_present =
Zhu Yib481de92007-09-25 17:54:57 -07003164 cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
3165 (1 << IEEE80211_RADIOTAP_FLAGS) |
3166 (1 << IEEE80211_RADIOTAP_RATE) |
3167 (1 << IEEE80211_RADIOTAP_CHANNEL) |
3168 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
3169 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
3170 (1 << IEEE80211_RADIOTAP_ANTENNA));
3171
3172 /* Zero the flags, we'll add to them as we go */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003173 iwl4965_rt->rt_flags = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003174
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003175 iwl4965_rt->rt_tsf = cpu_to_le64(tsf);
Zhu Yib481de92007-09-25 17:54:57 -07003176
3177 /* Convert to dBm */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003178 iwl4965_rt->rt_dbmsignal = signal;
3179 iwl4965_rt->rt_dbmnoise = noise;
Zhu Yib481de92007-09-25 17:54:57 -07003180
3181 /* Convert the channel frequency and set the flags */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003182 iwl4965_rt->rt_channelMHz = cpu_to_le16(stats->freq);
Zhu Yib481de92007-09-25 17:54:57 -07003183 if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003184 iwl4965_rt->rt_chbitmask =
Zhu Yib481de92007-09-25 17:54:57 -07003185 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
3186 else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003187 iwl4965_rt->rt_chbitmask =
Zhu Yib481de92007-09-25 17:54:57 -07003188 cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
3189 else /* 802.11g */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003190 iwl4965_rt->rt_chbitmask =
Zhu Yib481de92007-09-25 17:54:57 -07003191 cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ));
3192
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003193 rate = iwl4965_rate_index_from_plcp(rate);
Zhu Yib481de92007-09-25 17:54:57 -07003194 if (rate == -1)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003195 iwl4965_rt->rt_rate = 0;
Zhu Yib481de92007-09-25 17:54:57 -07003196 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003197 iwl4965_rt->rt_rate = iwl4965_rates[rate].ieee;
Zhu Yib481de92007-09-25 17:54:57 -07003198
3199 /* antenna number */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003200 iwl4965_rt->rt_antenna =
Zhu Yib481de92007-09-25 17:54:57 -07003201 le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
3202
3203 /* set the preamble flag if we have it */
3204 if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003205 iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
Zhu Yib481de92007-09-25 17:54:57 -07003206
3207 IWL_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
3208
3209 stats->flag |= RX_FLAG_RADIOTAP;
3210 ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
3211 rxb->skb = NULL;
3212}
3213
3214
3215#define IWL_PACKET_RETRY_TIME HZ
3216
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003217int iwl4965_is_duplicate_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
Zhu Yib481de92007-09-25 17:54:57 -07003218{
3219 u16 sc = le16_to_cpu(header->seq_ctrl);
3220 u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
3221 u16 frag = sc & IEEE80211_SCTL_FRAG;
3222 u16 *last_seq, *last_frag;
3223 unsigned long *last_time;
3224
3225 switch (priv->iw_mode) {
3226 case IEEE80211_IF_TYPE_IBSS:{
3227 struct list_head *p;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003228 struct iwl4965_ibss_seq *entry = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003229 u8 *mac = header->addr2;
3230 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
3231
3232 __list_for_each(p, &priv->ibss_mac_hash[index]) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003233 entry = list_entry(p, struct iwl4965_ibss_seq, list);
Zhu Yib481de92007-09-25 17:54:57 -07003234 if (!compare_ether_addr(entry->mac, mac))
3235 break;
3236 }
3237 if (p == &priv->ibss_mac_hash[index]) {
3238 entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
3239 if (!entry) {
Ian Schrambc434dd2007-10-25 17:15:29 +08003240 IWL_ERROR("Cannot malloc new mac entry\n");
Zhu Yib481de92007-09-25 17:54:57 -07003241 return 0;
3242 }
3243 memcpy(entry->mac, mac, ETH_ALEN);
3244 entry->seq_num = seq;
3245 entry->frag_num = frag;
3246 entry->packet_time = jiffies;
Ian Schrambc434dd2007-10-25 17:15:29 +08003247 list_add(&entry->list, &priv->ibss_mac_hash[index]);
Zhu Yib481de92007-09-25 17:54:57 -07003248 return 0;
3249 }
3250 last_seq = &entry->seq_num;
3251 last_frag = &entry->frag_num;
3252 last_time = &entry->packet_time;
3253 break;
3254 }
3255 case IEEE80211_IF_TYPE_STA:
3256 last_seq = &priv->last_seq_num;
3257 last_frag = &priv->last_frag_num;
3258 last_time = &priv->last_packet_time;
3259 break;
3260 default:
3261 return 0;
3262 }
3263 if ((*last_seq == seq) &&
3264 time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
3265 if (*last_frag == frag)
3266 goto drop;
3267 if (*last_frag + 1 != frag)
3268 /* out-of-order fragment */
3269 goto drop;
3270 } else
3271 *last_seq = seq;
3272
3273 *last_frag = frag;
3274 *last_time = jiffies;
3275 return 0;
3276
3277 drop:
3278 return 1;
3279}
3280
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003281#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07003282
3283#include "iwl-spectrum.h"
3284
3285#define BEACON_TIME_MASK_LOW 0x00FFFFFF
3286#define BEACON_TIME_MASK_HIGH 0xFF000000
3287#define TIME_UNIT 1024
3288
3289/*
3290 * extended beacon time format
3291 * time in usec will be changed into a 32-bit value in 8:24 format
3292 * the high 1 byte is the beacon counts
3293 * the lower 3 bytes is the time in usec within one beacon interval
3294 */
3295
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003296static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003297{
3298 u32 quot;
3299 u32 rem;
3300 u32 interval = beacon_interval * 1024;
3301
3302 if (!interval || !usec)
3303 return 0;
3304
3305 quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
3306 rem = (usec % interval) & BEACON_TIME_MASK_LOW;
3307
3308 return (quot << 24) + rem;
3309}
3310
3311/* base is usually what we get from ucode with each received frame,
3312 * the same as HW timer counter counting down
3313 */
3314
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003315static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
Zhu Yib481de92007-09-25 17:54:57 -07003316{
3317 u32 base_low = base & BEACON_TIME_MASK_LOW;
3318 u32 addon_low = addon & BEACON_TIME_MASK_LOW;
3319 u32 interval = beacon_interval * TIME_UNIT;
3320 u32 res = (base & BEACON_TIME_MASK_HIGH) +
3321 (addon & BEACON_TIME_MASK_HIGH);
3322
3323 if (base_low > addon_low)
3324 res += base_low - addon_low;
3325 else if (base_low < addon_low) {
3326 res += interval + base_low - addon_low;
3327 res += (1 << 24);
3328 } else
3329 res += (1 << 24);
3330
3331 return cpu_to_le32(res);
3332}
3333
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003334static int iwl4965_get_measurement(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003335 struct ieee80211_measurement_params *params,
3336 u8 type)
3337{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003338 struct iwl4965_spectrum_cmd spectrum;
3339 struct iwl4965_rx_packet *res;
3340 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07003341 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
3342 .data = (void *)&spectrum,
3343 .meta.flags = CMD_WANT_SKB,
3344 };
3345 u32 add_time = le64_to_cpu(params->start_time);
3346 int rc;
3347 int spectrum_resp_status;
3348 int duration = le16_to_cpu(params->duration);
3349
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003350 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003351 add_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003352 iwl4965_usecs_to_beacons(
Zhu Yib481de92007-09-25 17:54:57 -07003353 le64_to_cpu(params->start_time) - priv->last_tsf,
3354 le16_to_cpu(priv->rxon_timing.beacon_interval));
3355
3356 memset(&spectrum, 0, sizeof(spectrum));
3357
3358 spectrum.channel_count = cpu_to_le16(1);
3359 spectrum.flags =
3360 RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
3361 spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
3362 cmd.len = sizeof(spectrum);
3363 spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
3364
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003365 if (iwl4965_is_associated(priv))
Zhu Yib481de92007-09-25 17:54:57 -07003366 spectrum.start_time =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003367 iwl4965_add_beacon_time(priv->last_beacon_time,
Zhu Yib481de92007-09-25 17:54:57 -07003368 add_time,
3369 le16_to_cpu(priv->rxon_timing.beacon_interval));
3370 else
3371 spectrum.start_time = 0;
3372
3373 spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
3374 spectrum.channels[0].channel = params->channel;
3375 spectrum.channels[0].type = type;
3376 if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
3377 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
3378 RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
3379
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003380 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07003381 if (rc)
3382 return rc;
3383
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003384 res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003385 if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
3386 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
3387 rc = -EIO;
3388 }
3389
3390 spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
3391 switch (spectrum_resp_status) {
3392 case 0: /* Command will be handled */
3393 if (res->u.spectrum.id != 0xff) {
3394 IWL_DEBUG_INFO
3395 ("Replaced existing measurement: %d\n",
3396 res->u.spectrum.id);
3397 priv->measurement_status &= ~MEASUREMENT_READY;
3398 }
3399 priv->measurement_status |= MEASUREMENT_ACTIVE;
3400 rc = 0;
3401 break;
3402
3403 case 1: /* Command will not be handled */
3404 rc = -EAGAIN;
3405 break;
3406 }
3407
3408 dev_kfree_skb_any(cmd.meta.u.skb);
3409
3410 return rc;
3411}
3412#endif
3413
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003414static void iwl4965_txstatus_to_ieee(struct iwl4965_priv *priv,
3415 struct iwl4965_tx_info *tx_sta)
Zhu Yib481de92007-09-25 17:54:57 -07003416{
3417
3418 tx_sta->status.ack_signal = 0;
3419 tx_sta->status.excessive_retries = 0;
3420 tx_sta->status.queue_length = 0;
3421 tx_sta->status.queue_number = 0;
3422
3423 if (in_interrupt())
3424 ieee80211_tx_status_irqsafe(priv->hw,
3425 tx_sta->skb[0], &(tx_sta->status));
3426 else
3427 ieee80211_tx_status(priv->hw,
3428 tx_sta->skb[0], &(tx_sta->status));
3429
3430 tx_sta->skb[0] = NULL;
3431}
3432
3433/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003434 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries no more used by NIC.
Zhu Yib481de92007-09-25 17:54:57 -07003435 *
3436 * When FW advances 'R' index, all entries between old and
3437 * new 'R' index need to be reclaimed. As result, some free space
3438 * forms. If there is enough free space (> low mark), wake Tx queue.
3439 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003440int iwl4965_tx_queue_reclaim(struct iwl4965_priv *priv, int txq_id, int index)
Zhu Yib481de92007-09-25 17:54:57 -07003441{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003442 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
3443 struct iwl4965_queue *q = &txq->q;
Zhu Yib481de92007-09-25 17:54:57 -07003444 int nfreed = 0;
3445
3446 if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
3447 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
3448 "is out of range [0-%d] %d %d.\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003449 index, q->n_bd, q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003450 return 0;
3451 }
3452
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003453 for (index = iwl4965_queue_inc_wrap(index, q->n_bd);
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003454 q->read_ptr != index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003455 q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd)) {
Zhu Yib481de92007-09-25 17:54:57 -07003456 if (txq_id != IWL_CMD_QUEUE_NUM) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003457 iwl4965_txstatus_to_ieee(priv,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003458 &(txq->txb[txq->q.read_ptr]));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003459 iwl4965_hw_txq_free_tfd(priv, txq);
Zhu Yib481de92007-09-25 17:54:57 -07003460 } else if (nfreed > 1) {
3461 IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003462 q->write_ptr, q->read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003463 queue_work(priv->workqueue, &priv->restart);
3464 }
3465 nfreed++;
3466 }
3467
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003468 if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
Zhu Yib481de92007-09-25 17:54:57 -07003469 (txq_id != IWL_CMD_QUEUE_NUM) &&
3470 priv->mac80211_registered)
3471 ieee80211_wake_queue(priv->hw, txq_id);
3472
3473
3474 return nfreed;
3475}
3476
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003477static int iwl4965_is_tx_success(u32 status)
Zhu Yib481de92007-09-25 17:54:57 -07003478{
3479 status &= TX_STATUS_MSK;
3480 return (status == TX_STATUS_SUCCESS)
3481 || (status == TX_STATUS_DIRECT_DONE);
3482}
3483
3484/******************************************************************************
3485 *
3486 * Generic RX handler implementations
3487 *
3488 ******************************************************************************/
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003489#ifdef CONFIG_IWL4965_HT
3490#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07003491
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003492static inline int iwl4965_get_ra_sta_id(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07003493 struct ieee80211_hdr *hdr)
3494{
3495 if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
3496 return IWL_AP_ID;
3497 else {
3498 u8 *da = ieee80211_get_DA(hdr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003499 return iwl4965_hw_find_station(priv, da);
Zhu Yib481de92007-09-25 17:54:57 -07003500 }
3501}
3502
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003503static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
3504 struct iwl4965_priv *priv, int txq_id, int idx)
Zhu Yib481de92007-09-25 17:54:57 -07003505{
3506 if (priv->txq[txq_id].txb[idx].skb[0])
3507 return (struct ieee80211_hdr *)priv->txq[txq_id].
3508 txb[idx].skb[0]->data;
3509 return NULL;
3510}
3511
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003512static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
Zhu Yib481de92007-09-25 17:54:57 -07003513{
3514 __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
3515 tx_resp->frame_count);
3516 return le32_to_cpu(*scd_ssn) & MAX_SN;
3517
3518}
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003519static int iwl4965_tx_status_reply_tx(struct iwl4965_priv *priv,
3520 struct iwl4965_ht_agg *agg,
3521 struct iwl4965_tx_resp *tx_resp,
Zhu Yib481de92007-09-25 17:54:57 -07003522 u16 start_idx)
3523{
3524 u32 status;
3525 __le32 *frame_status = &tx_resp->status;
3526 struct ieee80211_tx_status *tx_status = NULL;
3527 struct ieee80211_hdr *hdr = NULL;
3528 int i, sh;
3529 int txq_id, idx;
3530 u16 seq;
3531
3532 if (agg->wait_for_ba)
3533 IWL_DEBUG_TX_REPLY("got tx repsons w/o back\n");
3534
3535 agg->frame_count = tx_resp->frame_count;
3536 agg->start_idx = start_idx;
3537 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3538 agg->bitmap0 = agg->bitmap1 = 0;
3539
3540 if (agg->frame_count == 1) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003541 struct iwl4965_tx_queue *txq ;
Zhu Yib481de92007-09-25 17:54:57 -07003542 status = le32_to_cpu(frame_status[0]);
3543
3544 txq_id = agg->txq_id;
3545 txq = &priv->txq[txq_id];
3546 /* FIXME: code repetition */
3547 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d \n",
3548 agg->frame_count, agg->start_idx);
3549
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003550 tx_status = &(priv->txq[txq_id].txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003551 tx_status->retry_count = tx_resp->failure_frame;
3552 tx_status->queue_number = status & 0xff;
3553 tx_status->queue_length = tx_resp->bt_kill_count;
3554 tx_status->queue_length |= tx_resp->failure_rts;
3555
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003556 tx_status->flags = iwl4965_is_tx_success(status)?
Zhu Yib481de92007-09-25 17:54:57 -07003557 IEEE80211_TX_STATUS_ACK : 0;
3558 tx_status->control.tx_rate =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003559 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003560 /* FIXME: code repetition end */
3561
3562 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
3563 status & 0xff, tx_resp->failure_frame);
3564 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003565 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
Zhu Yib481de92007-09-25 17:54:57 -07003566
3567 agg->wait_for_ba = 0;
3568 } else {
3569 u64 bitmap = 0;
3570 int start = agg->start_idx;
3571
3572 for (i = 0; i < agg->frame_count; i++) {
3573 u16 sc;
3574 status = le32_to_cpu(frame_status[i]);
3575 seq = status >> 16;
3576 idx = SEQ_TO_INDEX(seq);
3577 txq_id = SEQ_TO_QUEUE(seq);
3578
3579 if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
3580 AGG_TX_STATE_ABORT_MSK))
3581 continue;
3582
3583 IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
3584 agg->frame_count, txq_id, idx);
3585
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003586 hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
Zhu Yib481de92007-09-25 17:54:57 -07003587
3588 sc = le16_to_cpu(hdr->seq_ctrl);
3589 if (idx != (SEQ_TO_SN(sc) & 0xff)) {
3590 IWL_ERROR("BUG_ON idx doesn't match seq control"
3591 " idx=%d, seq_idx=%d, seq=%d\n",
3592 idx, SEQ_TO_SN(sc),
3593 hdr->seq_ctrl);
3594 return -1;
3595 }
3596
3597 IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
3598 i, idx, SEQ_TO_SN(sc));
3599
3600 sh = idx - start;
3601 if (sh > 64) {
3602 sh = (start - idx) + 0xff;
3603 bitmap = bitmap << sh;
3604 sh = 0;
3605 start = idx;
3606 } else if (sh < -64)
3607 sh = 0xff - (start - idx);
3608 else if (sh < 0) {
3609 sh = start - idx;
3610 start = idx;
3611 bitmap = bitmap << sh;
3612 sh = 0;
3613 }
3614 bitmap |= (1 << sh);
3615 IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
3616 start, (u32)(bitmap & 0xFFFFFFFF));
3617 }
3618
3619 agg->bitmap0 = bitmap & 0xFFFFFFFF;
3620 agg->bitmap1 = bitmap >> 32;
3621 agg->start_idx = start;
3622 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
3623 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%x\n",
3624 agg->frame_count, agg->start_idx,
3625 agg->bitmap0);
3626
3627 if (bitmap)
3628 agg->wait_for_ba = 1;
3629 }
3630 return 0;
3631}
3632#endif
3633#endif
3634
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003635static void iwl4965_rx_reply_tx(struct iwl4965_priv *priv,
3636 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003637{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003638 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003639 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
3640 int txq_id = SEQ_TO_QUEUE(sequence);
3641 int index = SEQ_TO_INDEX(sequence);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003642 struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
Zhu Yib481de92007-09-25 17:54:57 -07003643 struct ieee80211_tx_status *tx_status;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003644 struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
Zhu Yib481de92007-09-25 17:54:57 -07003645 u32 status = le32_to_cpu(tx_resp->status);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003646#ifdef CONFIG_IWL4965_HT
3647#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07003648 int tid, sta_id;
3649#endif
3650#endif
3651
3652 if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
3653 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
3654 "is out of range [0-%d] %d %d\n", txq_id,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003655 index, txq->q.n_bd, txq->q.write_ptr,
3656 txq->q.read_ptr);
Zhu Yib481de92007-09-25 17:54:57 -07003657 return;
3658 }
3659
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003660#ifdef CONFIG_IWL4965_HT
3661#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07003662 if (txq->sched_retry) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003663 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
Zhu Yib481de92007-09-25 17:54:57 -07003664 struct ieee80211_hdr *hdr =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003665 iwl4965_tx_queue_get_hdr(priv, txq_id, index);
3666 struct iwl4965_ht_agg *agg = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07003667 __le16 *qc = ieee80211_get_qos_ctrl(hdr);
3668
3669 if (qc == NULL) {
3670 IWL_ERROR("BUG_ON qc is null!!!!\n");
3671 return;
3672 }
3673
3674 tid = le16_to_cpu(*qc) & 0xf;
3675
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003676 sta_id = iwl4965_get_ra_sta_id(priv, hdr);
Zhu Yib481de92007-09-25 17:54:57 -07003677 if (unlikely(sta_id == IWL_INVALID_STATION)) {
3678 IWL_ERROR("Station not known for\n");
3679 return;
3680 }
3681
3682 agg = &priv->stations[sta_id].tid[tid].agg;
3683
3684 iwl4965_tx_status_reply_tx(priv, agg, tx_resp, index);
3685
3686 if ((tx_resp->frame_count == 1) &&
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003687 !iwl4965_is_tx_success(status)) {
Zhu Yib481de92007-09-25 17:54:57 -07003688 /* TODO: send BAR */
3689 }
3690
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003691 if ((txq->q.read_ptr != (scd_ssn & 0xff))) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003692 index = iwl4965_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
Zhu Yib481de92007-09-25 17:54:57 -07003693 IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
3694 "%d index %d\n", scd_ssn , index);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003695 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07003696 }
3697 } else {
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003698#endif /* CONFIG_IWL4965_HT_AGG */
3699#endif /* CONFIG_IWL4965_HT */
Tomas Winklerfc4b6852007-10-25 17:15:24 +08003700 tx_status = &(txq->txb[txq->q.read_ptr].status);
Zhu Yib481de92007-09-25 17:54:57 -07003701
3702 tx_status->retry_count = tx_resp->failure_frame;
3703 tx_status->queue_number = status;
3704 tx_status->queue_length = tx_resp->bt_kill_count;
3705 tx_status->queue_length |= tx_resp->failure_rts;
3706
3707 tx_status->flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003708 iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
Zhu Yib481de92007-09-25 17:54:57 -07003709
3710 tx_status->control.tx_rate =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003711 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003712
3713 IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003714 "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
Zhu Yib481de92007-09-25 17:54:57 -07003715 status, le32_to_cpu(tx_resp->rate_n_flags),
3716 tx_resp->failure_frame);
3717
3718 IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
3719 if (index != -1)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003720 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003721#ifdef CONFIG_IWL4965_HT
3722#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07003723 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003724#endif /* CONFIG_IWL4965_HT_AGG */
3725#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07003726
3727 if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
3728 IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
3729}
3730
3731
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003732static void iwl4965_rx_reply_alive(struct iwl4965_priv *priv,
3733 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003734{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003735 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3736 struct iwl4965_alive_resp *palive;
Zhu Yib481de92007-09-25 17:54:57 -07003737 struct delayed_work *pwork;
3738
3739 palive = &pkt->u.alive_frame;
3740
3741 IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
3742 "0x%01X 0x%01X\n",
3743 palive->is_valid, palive->ver_type,
3744 palive->ver_subtype);
3745
3746 if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
3747 IWL_DEBUG_INFO("Initialization Alive received.\n");
3748 memcpy(&priv->card_alive_init,
3749 &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003750 sizeof(struct iwl4965_init_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003751 pwork = &priv->init_alive_start;
3752 } else {
3753 IWL_DEBUG_INFO("Runtime Alive received.\n");
3754 memcpy(&priv->card_alive, &pkt->u.alive_frame,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003755 sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07003756 pwork = &priv->alive_start;
3757 }
3758
3759 /* We delay the ALIVE response by 5ms to
3760 * give the HW RF Kill time to activate... */
3761 if (palive->is_valid == UCODE_VALID_OK)
3762 queue_delayed_work(priv->workqueue, pwork,
3763 msecs_to_jiffies(5));
3764 else
3765 IWL_WARNING("uCode did not respond OK.\n");
3766}
3767
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003768static void iwl4965_rx_reply_add_sta(struct iwl4965_priv *priv,
3769 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003770{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003771 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003772
3773 IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
3774 return;
3775}
3776
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003777static void iwl4965_rx_reply_error(struct iwl4965_priv *priv,
3778 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003779{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003780 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003781
3782 IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
3783 "seq 0x%04X ser 0x%08X\n",
3784 le32_to_cpu(pkt->u.err_resp.error_type),
3785 get_cmd_string(pkt->u.err_resp.cmd_id),
3786 pkt->u.err_resp.cmd_id,
3787 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
3788 le32_to_cpu(pkt->u.err_resp.error_info));
3789}
3790
3791#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
3792
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003793static void iwl4965_rx_csa(struct iwl4965_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003794{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003795 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3796 struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
3797 struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003798 IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
3799 le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
3800 rxon->channel = csa->channel;
3801 priv->staging_rxon.channel = csa->channel;
3802}
3803
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003804static void iwl4965_rx_spectrum_measure_notif(struct iwl4965_priv *priv,
3805 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003806{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003807#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003808 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3809 struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003810
3811 if (!report->state) {
3812 IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
3813 "Spectrum Measure Notification: Start\n");
3814 return;
3815 }
3816
3817 memcpy(&priv->measure_report, report, sizeof(*report));
3818 priv->measurement_status |= MEASUREMENT_READY;
3819#endif
3820}
3821
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003822static void iwl4965_rx_pm_sleep_notif(struct iwl4965_priv *priv,
3823 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003824{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003825#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003826 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3827 struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
Zhu Yib481de92007-09-25 17:54:57 -07003828 IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
3829 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
3830#endif
3831}
3832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003833static void iwl4965_rx_pm_debug_statistics_notif(struct iwl4965_priv *priv,
3834 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003835{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003836 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07003837 IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
3838 "notification for %s:\n",
3839 le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003840 iwl4965_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
Zhu Yib481de92007-09-25 17:54:57 -07003841}
3842
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003843static void iwl4965_bg_beacon_update(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07003844{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003845 struct iwl4965_priv *priv =
3846 container_of(work, struct iwl4965_priv, beacon_update);
Zhu Yib481de92007-09-25 17:54:57 -07003847 struct sk_buff *beacon;
3848
3849 /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
3850 beacon = ieee80211_beacon_get(priv->hw, priv->interface_id, NULL);
3851
3852 if (!beacon) {
3853 IWL_ERROR("update beacon failed\n");
3854 return;
3855 }
3856
3857 mutex_lock(&priv->mutex);
3858 /* new beacon skb is allocated every time; dispose previous.*/
3859 if (priv->ibss_beacon)
3860 dev_kfree_skb(priv->ibss_beacon);
3861
3862 priv->ibss_beacon = beacon;
3863 mutex_unlock(&priv->mutex);
3864
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003865 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07003866}
3867
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003868static void iwl4965_rx_beacon_notif(struct iwl4965_priv *priv,
3869 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003870{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003871#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003872 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3873 struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
3874 u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
Zhu Yib481de92007-09-25 17:54:57 -07003875
3876 IWL_DEBUG_RX("beacon status %x retries %d iss %d "
3877 "tsf %d %d rate %d\n",
3878 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
3879 beacon->beacon_notify_hdr.failure_frame,
3880 le32_to_cpu(beacon->ibss_mgr_status),
3881 le32_to_cpu(beacon->high_tsf),
3882 le32_to_cpu(beacon->low_tsf), rate);
3883#endif
3884
3885 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
3886 (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
3887 queue_work(priv->workqueue, &priv->beacon_update);
3888}
3889
3890/* Service response to REPLY_SCAN_CMD (0x80) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003891static void iwl4965_rx_reply_scan(struct iwl4965_priv *priv,
3892 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003893{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08003894#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003895 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3896 struct iwl4965_scanreq_notification *notif =
3897 (struct iwl4965_scanreq_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003898
3899 IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
3900#endif
3901}
3902
3903/* Service SCAN_START_NOTIFICATION (0x82) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003904static void iwl4965_rx_scan_start_notif(struct iwl4965_priv *priv,
3905 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003906{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003907 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3908 struct iwl4965_scanstart_notification *notif =
3909 (struct iwl4965_scanstart_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003910 priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
3911 IWL_DEBUG_SCAN("Scan start: "
3912 "%d [802.11%s] "
3913 "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
3914 notif->channel,
3915 notif->band ? "bg" : "a",
3916 notif->tsf_high,
3917 notif->tsf_low, notif->status, notif->beacon_timer);
3918}
3919
3920/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003921static void iwl4965_rx_scan_results_notif(struct iwl4965_priv *priv,
3922 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003923{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003924 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3925 struct iwl4965_scanresults_notification *notif =
3926 (struct iwl4965_scanresults_notification *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003927
3928 IWL_DEBUG_SCAN("Scan ch.res: "
3929 "%d [802.11%s] "
3930 "(TSF: 0x%08X:%08X) - %d "
3931 "elapsed=%lu usec (%dms since last)\n",
3932 notif->channel,
3933 notif->band ? "bg" : "a",
3934 le32_to_cpu(notif->tsf_high),
3935 le32_to_cpu(notif->tsf_low),
3936 le32_to_cpu(notif->statistics[0]),
3937 le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
3938 jiffies_to_msecs(elapsed_jiffies
3939 (priv->last_scan_jiffies, jiffies)));
3940
3941 priv->last_scan_jiffies = jiffies;
3942}
3943
3944/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003945static void iwl4965_rx_scan_complete_notif(struct iwl4965_priv *priv,
3946 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07003947{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08003948 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
3949 struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
Zhu Yib481de92007-09-25 17:54:57 -07003950
3951 IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
3952 scan_notif->scanned_channels,
3953 scan_notif->tsf_low,
3954 scan_notif->tsf_high, scan_notif->status);
3955
3956 /* The HW is no longer scanning */
3957 clear_bit(STATUS_SCAN_HW, &priv->status);
3958
3959 /* The scan completion notification came in, so kill that timer... */
3960 cancel_delayed_work(&priv->scan_check);
3961
3962 IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
3963 (priv->scan_bands == 2) ? "2.4" : "5.2",
3964 jiffies_to_msecs(elapsed_jiffies
3965 (priv->scan_pass_start, jiffies)));
3966
3967 /* Remove this scanned band from the list
3968 * of pending bands to scan */
3969 priv->scan_bands--;
3970
3971 /* If a request to abort was given, or the scan did not succeed
3972 * then we reset the scan state machine and terminate,
3973 * re-queuing another scan if one has been requested */
3974 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3975 IWL_DEBUG_INFO("Aborted scan completed.\n");
3976 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
3977 } else {
3978 /* If there are more bands on this scan pass reschedule */
3979 if (priv->scan_bands > 0)
3980 goto reschedule;
3981 }
3982
3983 priv->last_scan_jiffies = jiffies;
3984 IWL_DEBUG_INFO("Setting scan to off\n");
3985
3986 clear_bit(STATUS_SCANNING, &priv->status);
3987
3988 IWL_DEBUG_INFO("Scan took %dms\n",
3989 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
3990
3991 queue_work(priv->workqueue, &priv->scan_completed);
3992
3993 return;
3994
3995reschedule:
3996 priv->scan_pass_start = jiffies;
3997 queue_work(priv->workqueue, &priv->request_scan);
3998}
3999
4000/* Handle notification from uCode that card's power state is changing
4001 * due to software, hardware, or critical temperature RFKILL */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004002static void iwl4965_rx_card_state_notif(struct iwl4965_priv *priv,
4003 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07004004{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004005 struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004006 u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
4007 unsigned long status = priv->status;
4008
4009 IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
4010 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
4011 (flags & SW_CARD_DISABLED) ? "Kill" : "On");
4012
4013 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
4014 RF_CARD_DISABLED)) {
4015
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004016 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07004017 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
4018
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004019 if (!iwl4965_grab_nic_access(priv)) {
4020 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07004021 priv, HBUS_TARG_MBX_C,
4022 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4023
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004024 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004025 }
4026
4027 if (!(flags & RXON_CARD_DISABLED)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004028 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07004029 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004030 if (!iwl4965_grab_nic_access(priv)) {
4031 iwl4965_write_direct32(
Zhu Yib481de92007-09-25 17:54:57 -07004032 priv, HBUS_TARG_MBX_C,
4033 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
4034
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004035 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004036 }
4037 }
4038
4039 if (flags & RF_CARD_DISABLED) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004040 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
Zhu Yib481de92007-09-25 17:54:57 -07004041 CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004042 iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
4043 if (!iwl4965_grab_nic_access(priv))
4044 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004045 }
4046 }
4047
4048 if (flags & HW_CARD_DISABLED)
4049 set_bit(STATUS_RF_KILL_HW, &priv->status);
4050 else
4051 clear_bit(STATUS_RF_KILL_HW, &priv->status);
4052
4053
4054 if (flags & SW_CARD_DISABLED)
4055 set_bit(STATUS_RF_KILL_SW, &priv->status);
4056 else
4057 clear_bit(STATUS_RF_KILL_SW, &priv->status);
4058
4059 if (!(flags & RXON_CARD_DISABLED))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004060 iwl4965_scan_cancel(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004061
4062 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
4063 test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
4064 (test_bit(STATUS_RF_KILL_SW, &status) !=
4065 test_bit(STATUS_RF_KILL_SW, &priv->status)))
4066 queue_work(priv->workqueue, &priv->rf_kill);
4067 else
4068 wake_up_interruptible(&priv->wait_command_queue);
4069}
4070
4071/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004072 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
Zhu Yib481de92007-09-25 17:54:57 -07004073 *
4074 * Setup the RX handlers for each of the reply types sent from the uCode
4075 * to the host.
4076 *
4077 * This function chains into the hardware specific files for them to setup
4078 * any hardware specific handlers as well.
4079 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004080static void iwl4965_setup_rx_handlers(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004081{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004082 priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
4083 priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
4084 priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
4085 priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
Zhu Yib481de92007-09-25 17:54:57 -07004086 priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004087 iwl4965_rx_spectrum_measure_notif;
4088 priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
Zhu Yib481de92007-09-25 17:54:57 -07004089 priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004090 iwl4965_rx_pm_debug_statistics_notif;
4091 priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
Zhu Yib481de92007-09-25 17:54:57 -07004092
Ben Cahill9fbab512007-11-29 11:09:47 +08004093 /*
4094 * The same handler is used for both the REPLY to a discrete
4095 * statistics request from the host as well as for the periodic
4096 * statistics notifications (after received beacons) from the uCode.
Zhu Yib481de92007-09-25 17:54:57 -07004097 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004098 priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
4099 priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
Zhu Yib481de92007-09-25 17:54:57 -07004100
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004101 priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
4102 priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
Zhu Yib481de92007-09-25 17:54:57 -07004103 priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004104 iwl4965_rx_scan_results_notif;
Zhu Yib481de92007-09-25 17:54:57 -07004105 priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004106 iwl4965_rx_scan_complete_notif;
4107 priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
4108 priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
Zhu Yib481de92007-09-25 17:54:57 -07004109
Ben Cahill9fbab512007-11-29 11:09:47 +08004110 /* Set up hardware specific Rx handlers */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004111 iwl4965_hw_rx_handler_setup(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004112}
4113
4114/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004115 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
Zhu Yib481de92007-09-25 17:54:57 -07004116 * @rxb: Rx buffer to reclaim
4117 *
4118 * If an Rx buffer has an async callback associated with it the callback
4119 * will be executed. The attached skb (if present) will only be freed
4120 * if the callback returns 1
4121 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004122static void iwl4965_tx_cmd_complete(struct iwl4965_priv *priv,
4123 struct iwl4965_rx_mem_buffer *rxb)
Zhu Yib481de92007-09-25 17:54:57 -07004124{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004125 struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004126 u16 sequence = le16_to_cpu(pkt->hdr.sequence);
4127 int txq_id = SEQ_TO_QUEUE(sequence);
4128 int index = SEQ_TO_INDEX(sequence);
4129 int huge = sequence & SEQ_HUGE_FRAME;
4130 int cmd_index;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004131 struct iwl4965_cmd *cmd;
Zhu Yib481de92007-09-25 17:54:57 -07004132
4133 /* If a Tx command is being handled and it isn't in the actual
4134 * command queue then there a command routing bug has been introduced
4135 * in the queue management code. */
4136 if (txq_id != IWL_CMD_QUEUE_NUM)
4137 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
4138 txq_id, pkt->hdr.cmd);
4139 BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
4140
4141 cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
4142 cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
4143
4144 /* Input error checking is done when commands are added to queue. */
4145 if (cmd->meta.flags & CMD_WANT_SKB) {
4146 cmd->meta.source->u.skb = rxb->skb;
4147 rxb->skb = NULL;
4148 } else if (cmd->meta.u.callback &&
4149 !cmd->meta.u.callback(priv, cmd, rxb->skb))
4150 rxb->skb = NULL;
4151
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004152 iwl4965_tx_queue_reclaim(priv, txq_id, index);
Zhu Yib481de92007-09-25 17:54:57 -07004153
4154 if (!(cmd->meta.flags & CMD_ASYNC)) {
4155 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4156 wake_up_interruptible(&priv->wait_command_queue);
4157 }
4158}
4159
4160/************************** RX-FUNCTIONS ****************************/
4161/*
4162 * Rx theory of operation
4163 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004164 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
4165 * each of which point to Receive Buffers to be filled by 4965. These get
4166 * used not only for Rx frames, but for any command response or notification
4167 * from the 4965. The driver and 4965 manage the Rx buffers by means
4168 * of indexes into the circular buffer.
Zhu Yib481de92007-09-25 17:54:57 -07004169 *
4170 * Rx Queue Indexes
4171 * The host/firmware share two index registers for managing the Rx buffers.
4172 *
4173 * The READ index maps to the first position that the firmware may be writing
4174 * to -- the driver can read up to (but not including) this position and get
4175 * good data.
4176 * The READ index is managed by the firmware once the card is enabled.
4177 *
4178 * The WRITE index maps to the last position the driver has read from -- the
4179 * position preceding WRITE is the last slot the firmware can place a packet.
4180 *
4181 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
4182 * WRITE = READ.
4183 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004184 * During initialization, the host sets up the READ queue position to the first
Zhu Yib481de92007-09-25 17:54:57 -07004185 * INDEX position, and WRITE to the last (READ - 1 wrapped)
4186 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004187 * When the firmware places a packet in a buffer, it will advance the READ index
Zhu Yib481de92007-09-25 17:54:57 -07004188 * and fire the RX interrupt. The driver can then query the READ index and
4189 * process as many packets as possible, moving the WRITE index forward as it
4190 * resets the Rx queue buffers with new memory.
4191 *
4192 * The management in the driver is as follows:
4193 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
4194 * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
Ian Schram01ebd062007-10-25 17:15:22 +08004195 * to replenish the iwl->rxq->rx_free.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004196 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
Zhu Yib481de92007-09-25 17:54:57 -07004197 * iwl->rxq is replenished and the READ INDEX is updated (updating the
4198 * 'processed' and 'read' driver indexes as well)
4199 * + A received packet is processed and handed to the kernel network stack,
4200 * detached from the iwl->rxq. The driver 'processed' index is updated.
4201 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
4202 * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
4203 * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
4204 * were enough free buffers and RX_STALLED is set it is cleared.
4205 *
4206 *
4207 * Driver sequence:
4208 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004209 * iwl4965_rx_queue_alloc() Allocates rx_free
4210 * iwl4965_rx_replenish() Replenishes rx_free list from rx_used, and calls
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004211 * iwl4965_rx_queue_restock
Ben Cahill9fbab512007-11-29 11:09:47 +08004212 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
Zhu Yib481de92007-09-25 17:54:57 -07004213 * queue, updates firmware pointers, and updates
4214 * the WRITE index. If insufficient rx_free buffers
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004215 * are available, schedules iwl4965_rx_replenish
Zhu Yib481de92007-09-25 17:54:57 -07004216 *
4217 * -- enable interrupts --
Ben Cahill9fbab512007-11-29 11:09:47 +08004218 * ISR - iwl4965_rx() Detach iwl4965_rx_mem_buffers from pool up to the
Zhu Yib481de92007-09-25 17:54:57 -07004219 * READ INDEX, detaching the SKB from the pool.
4220 * Moves the packet buffer from queue to rx_used.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004221 * Calls iwl4965_rx_queue_restock to refill any empty
Zhu Yib481de92007-09-25 17:54:57 -07004222 * slots.
4223 * ...
4224 *
4225 */
4226
4227/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004228 * iwl4965_rx_queue_space - Return number of free slots available in queue.
Zhu Yib481de92007-09-25 17:54:57 -07004229 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004230static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004231{
4232 int s = q->read - q->write;
4233 if (s <= 0)
4234 s += RX_QUEUE_SIZE;
4235 /* keep some buffer to not confuse full and empty queue */
4236 s -= 2;
4237 if (s < 0)
4238 s = 0;
4239 return s;
4240}
4241
4242/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004243 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
Zhu Yib481de92007-09-25 17:54:57 -07004244 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004245int iwl4965_rx_queue_update_write_ptr(struct iwl4965_priv *priv, struct iwl4965_rx_queue *q)
Zhu Yib481de92007-09-25 17:54:57 -07004246{
4247 u32 reg = 0;
4248 int rc = 0;
4249 unsigned long flags;
4250
4251 spin_lock_irqsave(&q->lock, flags);
4252
4253 if (q->need_update == 0)
4254 goto exit_unlock;
4255
4256 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004257 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004258
4259 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004260 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004261 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4262 goto exit_unlock;
4263 }
4264
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004265 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004266 if (rc)
4267 goto exit_unlock;
4268
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004269 iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
Zhu Yib481de92007-09-25 17:54:57 -07004270 q->write & ~0x7);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004271 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004272 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004273 iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
Zhu Yib481de92007-09-25 17:54:57 -07004274
4275
4276 q->need_update = 0;
4277
4278 exit_unlock:
4279 spin_unlock_irqrestore(&q->lock, flags);
4280 return rc;
4281}
4282
4283/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004284 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
Zhu Yib481de92007-09-25 17:54:57 -07004285 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004286static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07004287 dma_addr_t dma_addr)
4288{
4289 return cpu_to_le32((u32)(dma_addr >> 8));
4290}
4291
4292
4293/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004294 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
Zhu Yib481de92007-09-25 17:54:57 -07004295 *
Ben Cahill9fbab512007-11-29 11:09:47 +08004296 * If there are slots in the RX queue that need to be restocked,
Zhu Yib481de92007-09-25 17:54:57 -07004297 * and we have free pre-allocated buffers, fill the ranks as much
Ben Cahill9fbab512007-11-29 11:09:47 +08004298 * as we can, pulling from rx_free.
Zhu Yib481de92007-09-25 17:54:57 -07004299 *
4300 * This moves the 'write' index forward to catch up with 'processed', and
4301 * also updates the memory address in the firmware to reference the new
4302 * target buffer.
4303 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004304static int iwl4965_rx_queue_restock(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004305{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004306 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004307 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004308 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004309 unsigned long flags;
4310 int write, rc;
4311
4312 spin_lock_irqsave(&rxq->lock, flags);
4313 write = rxq->write & ~0x7;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004314 while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
Zhu Yib481de92007-09-25 17:54:57 -07004315 element = rxq->rx_free.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004316 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004317 list_del(element);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004318 rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
Zhu Yib481de92007-09-25 17:54:57 -07004319 rxq->queue[rxq->write] = rxb;
4320 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
4321 rxq->free_count--;
4322 }
4323 spin_unlock_irqrestore(&rxq->lock, flags);
4324 /* If the pre-allocated buffer pool is dropping low, schedule to
4325 * refill it */
4326 if (rxq->free_count <= RX_LOW_WATERMARK)
4327 queue_work(priv->workqueue, &priv->rx_replenish);
4328
4329
4330 /* If we've added more space for the firmware to place data, tell it */
4331 if ((write != (rxq->write & ~0x7))
4332 || (abs(rxq->write - rxq->read) > 7)) {
4333 spin_lock_irqsave(&rxq->lock, flags);
4334 rxq->need_update = 1;
4335 spin_unlock_irqrestore(&rxq->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004336 rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
Zhu Yib481de92007-09-25 17:54:57 -07004337 if (rc)
4338 return rc;
4339 }
4340
4341 return 0;
4342}
4343
4344/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004345 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
Zhu Yib481de92007-09-25 17:54:57 -07004346 *
4347 * When moving to rx_free an SKB is allocated for the slot.
4348 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004349 * Also restock the Rx queue via iwl4965_rx_queue_restock.
Ian Schram01ebd062007-10-25 17:15:22 +08004350 * This is called as a scheduled work item (except for during initialization)
Zhu Yib481de92007-09-25 17:54:57 -07004351 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004352void iwl4965_rx_replenish(void *data)
Zhu Yib481de92007-09-25 17:54:57 -07004353{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004354 struct iwl4965_priv *priv = data;
4355 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004356 struct list_head *element;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004357 struct iwl4965_rx_mem_buffer *rxb;
Zhu Yib481de92007-09-25 17:54:57 -07004358 unsigned long flags;
4359 spin_lock_irqsave(&rxq->lock, flags);
4360 while (!list_empty(&rxq->rx_used)) {
4361 element = rxq->rx_used.next;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004362 rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
Zhu Yib481de92007-09-25 17:54:57 -07004363 rxb->skb =
4364 alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
4365 if (!rxb->skb) {
4366 if (net_ratelimit())
4367 printk(KERN_CRIT DRV_NAME
4368 ": Can not allocate SKB buffers\n");
4369 /* We don't reschedule replenish work here -- we will
4370 * call the restock method and if it still needs
4371 * more buffers it will schedule replenish */
4372 break;
4373 }
4374 priv->alloc_rxb_skb++;
4375 list_del(element);
4376 rxb->dma_addr =
4377 pci_map_single(priv->pci_dev, rxb->skb->data,
4378 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4379 list_add_tail(&rxb->list, &rxq->rx_free);
4380 rxq->free_count++;
4381 }
4382 spin_unlock_irqrestore(&rxq->lock, flags);
4383
4384 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004385 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004386 spin_unlock_irqrestore(&priv->lock, flags);
4387}
4388
4389/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
Ben Cahill9fbab512007-11-29 11:09:47 +08004390 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
Zhu Yib481de92007-09-25 17:54:57 -07004391 * This free routine walks the list of POOL entries and if SKB is set to
4392 * non NULL it is unmapped and freed
4393 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004394static void iwl4965_rx_queue_free(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004395{
4396 int i;
4397 for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
4398 if (rxq->pool[i].skb != NULL) {
4399 pci_unmap_single(priv->pci_dev,
4400 rxq->pool[i].dma_addr,
4401 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4402 dev_kfree_skb(rxq->pool[i].skb);
4403 }
4404 }
4405
4406 pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
4407 rxq->dma_addr);
4408 rxq->bd = NULL;
4409}
4410
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004411int iwl4965_rx_queue_alloc(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004412{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004413 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004414 struct pci_dev *dev = priv->pci_dev;
4415 int i;
4416
4417 spin_lock_init(&rxq->lock);
4418 INIT_LIST_HEAD(&rxq->rx_free);
4419 INIT_LIST_HEAD(&rxq->rx_used);
4420 rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
4421 if (!rxq->bd)
4422 return -ENOMEM;
4423 /* Fill the rx_used queue with _all_ of the Rx buffers */
4424 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
4425 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4426 /* Set us so that we have processed and used all buffers, but have
4427 * not restocked the Rx queue with fresh buffers */
4428 rxq->read = rxq->write = 0;
4429 rxq->free_count = 0;
4430 rxq->need_update = 0;
4431 return 0;
4432}
4433
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004434void iwl4965_rx_queue_reset(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
Zhu Yib481de92007-09-25 17:54:57 -07004435{
4436 unsigned long flags;
4437 int i;
4438 spin_lock_irqsave(&rxq->lock, flags);
4439 INIT_LIST_HEAD(&rxq->rx_free);
4440 INIT_LIST_HEAD(&rxq->rx_used);
4441 /* Fill the rx_used queue with _all_ of the Rx buffers */
4442 for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
4443 /* In the reset function, these buffers may have been allocated
4444 * to an SKB, so we need to unmap and free potential storage */
4445 if (rxq->pool[i].skb != NULL) {
4446 pci_unmap_single(priv->pci_dev,
4447 rxq->pool[i].dma_addr,
4448 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4449 priv->alloc_rxb_skb--;
4450 dev_kfree_skb(rxq->pool[i].skb);
4451 rxq->pool[i].skb = NULL;
4452 }
4453 list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
4454 }
4455
4456 /* Set us so that we have processed and used all buffers, but have
4457 * not restocked the Rx queue with fresh buffers */
4458 rxq->read = rxq->write = 0;
4459 rxq->free_count = 0;
4460 spin_unlock_irqrestore(&rxq->lock, flags);
4461}
4462
4463/* Convert linear signal-to-noise ratio into dB */
4464static u8 ratio2dB[100] = {
4465/* 0 1 2 3 4 5 6 7 8 9 */
4466 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
4467 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
4468 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
4469 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
4470 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
4471 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
4472 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
4473 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
4474 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
4475 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
4476};
4477
4478/* Calculates a relative dB value from a ratio of linear
4479 * (i.e. not dB) signal levels.
4480 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004481int iwl4965_calc_db_from_ratio(int sig_ratio)
Zhu Yib481de92007-09-25 17:54:57 -07004482{
Adrian Bunkc899a572007-10-14 19:51:15 +02004483 /* 1000:1 or higher just report as 60 dB */
4484 if (sig_ratio >= 1000)
Zhu Yib481de92007-09-25 17:54:57 -07004485 return 60;
4486
Adrian Bunkc899a572007-10-14 19:51:15 +02004487 /* 100:1 or higher, divide by 10 and use table,
Zhu Yib481de92007-09-25 17:54:57 -07004488 * add 20 dB to make up for divide by 10 */
Adrian Bunkc899a572007-10-14 19:51:15 +02004489 if (sig_ratio >= 100)
Zhu Yib481de92007-09-25 17:54:57 -07004490 return (20 + (int)ratio2dB[sig_ratio/10]);
4491
4492 /* We shouldn't see this */
4493 if (sig_ratio < 1)
4494 return 0;
4495
4496 /* Use table for ratios 1:1 - 99:1 */
4497 return (int)ratio2dB[sig_ratio];
4498}
4499
4500#define PERFECT_RSSI (-20) /* dBm */
4501#define WORST_RSSI (-95) /* dBm */
4502#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
4503
4504/* Calculate an indication of rx signal quality (a percentage, not dBm!).
4505 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
4506 * about formulas used below. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004507int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
Zhu Yib481de92007-09-25 17:54:57 -07004508{
4509 int sig_qual;
4510 int degradation = PERFECT_RSSI - rssi_dbm;
4511
4512 /* If we get a noise measurement, use signal-to-noise ratio (SNR)
4513 * as indicator; formula is (signal dbm - noise dbm).
4514 * SNR at or above 40 is a great signal (100%).
4515 * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
4516 * Weakest usable signal is usually 10 - 15 dB SNR. */
4517 if (noise_dbm) {
4518 if (rssi_dbm - noise_dbm >= 40)
4519 return 100;
4520 else if (rssi_dbm < noise_dbm)
4521 return 0;
4522 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
4523
4524 /* Else use just the signal level.
4525 * This formula is a least squares fit of data points collected and
4526 * compared with a reference system that had a percentage (%) display
4527 * for signal quality. */
4528 } else
4529 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
4530 (15 * RSSI_RANGE + 62 * degradation)) /
4531 (RSSI_RANGE * RSSI_RANGE);
4532
4533 if (sig_qual > 100)
4534 sig_qual = 100;
4535 else if (sig_qual < 1)
4536 sig_qual = 0;
4537
4538 return sig_qual;
4539}
4540
4541/**
Ben Cahill9fbab512007-11-29 11:09:47 +08004542 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
Zhu Yib481de92007-09-25 17:54:57 -07004543 *
4544 * Uses the priv->rx_handlers callback function array to invoke
4545 * the appropriate handlers, including command responses,
4546 * frame-received notifications, and other notifications.
4547 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004548static void iwl4965_rx_handle(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004549{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004550 struct iwl4965_rx_mem_buffer *rxb;
4551 struct iwl4965_rx_packet *pkt;
4552 struct iwl4965_rx_queue *rxq = &priv->rxq;
Zhu Yib481de92007-09-25 17:54:57 -07004553 u32 r, i;
4554 int reclaim;
4555 unsigned long flags;
4556
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004557 r = iwl4965_hw_get_rx_read(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004558 i = rxq->read;
4559
4560 /* Rx interrupt, but nothing sent from uCode */
4561 if (i == r)
4562 IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
4563
4564 while (i != r) {
4565 rxb = rxq->queue[i];
4566
Ben Cahill9fbab512007-11-29 11:09:47 +08004567 /* If an RXB doesn't have a Rx queue slot associated with it,
Zhu Yib481de92007-09-25 17:54:57 -07004568 * then a bug has been introduced in the queue refilling
4569 * routines -- catch it here */
4570 BUG_ON(rxb == NULL);
4571
4572 rxq->queue[i] = NULL;
4573
4574 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
4575 IWL_RX_BUF_SIZE,
4576 PCI_DMA_FROMDEVICE);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004577 pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
Zhu Yib481de92007-09-25 17:54:57 -07004578
4579 /* Reclaim a command buffer only if this packet is a response
4580 * to a (driver-originated) command.
4581 * If the packet (e.g. Rx frame) originated from uCode,
4582 * there is no command buffer to reclaim.
4583 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
4584 * but apparently a few don't get set; catch them here. */
4585 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
4586 (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
4587 (pkt->hdr.cmd != REPLY_4965_RX) &&
Zhu Yicfe01702007-09-27 11:27:31 +08004588 (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
Zhu Yib481de92007-09-25 17:54:57 -07004589 (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
4590 (pkt->hdr.cmd != REPLY_TX);
4591
4592 /* Based on type of command response or notification,
4593 * handle those that need handling via function in
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004594 * rx_handlers table. See iwl4965_setup_rx_handlers() */
Zhu Yib481de92007-09-25 17:54:57 -07004595 if (priv->rx_handlers[pkt->hdr.cmd]) {
4596 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4597 "r = %d, i = %d, %s, 0x%02x\n", r, i,
4598 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
4599 priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
4600 } else {
4601 /* No handling needed */
4602 IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
4603 "r %d i %d No handler needed for %s, 0x%02x\n",
4604 r, i, get_cmd_string(pkt->hdr.cmd),
4605 pkt->hdr.cmd);
4606 }
4607
4608 if (reclaim) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004609 /* Invoke any callbacks, transfer the skb to caller, and
4610 * fire off the (possibly) blocking iwl4965_send_cmd()
Zhu Yib481de92007-09-25 17:54:57 -07004611 * as we reclaim the driver command queue */
4612 if (rxb && rxb->skb)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004613 iwl4965_tx_cmd_complete(priv, rxb);
Zhu Yib481de92007-09-25 17:54:57 -07004614 else
4615 IWL_WARNING("Claim null rxb?\n");
4616 }
4617
4618 /* For now we just don't re-use anything. We can tweak this
4619 * later to try and re-use notification packets and SKBs that
4620 * fail to Rx correctly */
4621 if (rxb->skb != NULL) {
4622 priv->alloc_rxb_skb--;
4623 dev_kfree_skb_any(rxb->skb);
4624 rxb->skb = NULL;
4625 }
4626
4627 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
4628 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
4629 spin_lock_irqsave(&rxq->lock, flags);
4630 list_add_tail(&rxb->list, &priv->rxq.rx_used);
4631 spin_unlock_irqrestore(&rxq->lock, flags);
4632 i = (i + 1) & RX_QUEUE_MASK;
4633 }
4634
4635 /* Backtrack one entry */
4636 priv->rxq.read = i;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004637 iwl4965_rx_queue_restock(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004638}
4639
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004640static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
4641 struct iwl4965_tx_queue *txq)
Zhu Yib481de92007-09-25 17:54:57 -07004642{
4643 u32 reg = 0;
4644 int rc = 0;
4645 int txq_id = txq->q.id;
4646
4647 if (txq->need_update == 0)
4648 return rc;
4649
4650 /* if we're trying to save power */
4651 if (test_bit(STATUS_POWER_PMI, &priv->status)) {
4652 /* wake up nic if it's powered down ...
4653 * uCode will wake up, and interrupt us again, so next
4654 * time we'll skip this part. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004655 reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
Zhu Yib481de92007-09-25 17:54:57 -07004656
4657 if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
4658 IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004659 iwl4965_set_bit(priv, CSR_GP_CNTRL,
Zhu Yib481de92007-09-25 17:54:57 -07004660 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
4661 return rc;
4662 }
4663
4664 /* restore this queue's parameters in nic hardware. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004665 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004666 if (rc)
4667 return rc;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004668 iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004669 txq->q.write_ptr | (txq_id << 8));
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004670 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004671
4672 /* else not in power-save mode, uCode will never sleep when we're
4673 * trying to tx (during RFKILL, we're not trying to tx). */
4674 } else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004675 iwl4965_write32(priv, HBUS_TARG_WRPTR,
Tomas Winklerfc4b6852007-10-25 17:15:24 +08004676 txq->q.write_ptr | (txq_id << 8));
Zhu Yib481de92007-09-25 17:54:57 -07004677
4678 txq->need_update = 0;
4679
4680 return rc;
4681}
4682
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004683#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004684static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
Zhu Yib481de92007-09-25 17:54:57 -07004685{
Joe Perches0795af52007-10-03 17:59:30 -07004686 DECLARE_MAC_BUF(mac);
4687
Zhu Yib481de92007-09-25 17:54:57 -07004688 IWL_DEBUG_RADIO("RX CONFIG:\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004689 iwl4965_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
Zhu Yib481de92007-09-25 17:54:57 -07004690 IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
4691 IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
4692 IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
4693 le32_to_cpu(rxon->filter_flags));
4694 IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
4695 IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
4696 rxon->ofdm_basic_rates);
4697 IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
Joe Perches0795af52007-10-03 17:59:30 -07004698 IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
4699 print_mac(mac, rxon->node_addr));
4700 IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
4701 print_mac(mac, rxon->bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07004702 IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
4703}
4704#endif
4705
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004706static void iwl4965_enable_interrupts(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004707{
4708 IWL_DEBUG_ISR("Enabling interrupts\n");
4709 set_bit(STATUS_INT_ENABLED, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004710 iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004711}
4712
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004713static inline void iwl4965_disable_interrupts(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004714{
4715 clear_bit(STATUS_INT_ENABLED, &priv->status);
4716
4717 /* disable interrupts from uCode/NIC to host */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004718 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07004719
4720 /* acknowledge/clear/reset any interrupts still pending
4721 * from uCode or flow handler (Rx/Tx DMA) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004722 iwl4965_write32(priv, CSR_INT, 0xffffffff);
4723 iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
Zhu Yib481de92007-09-25 17:54:57 -07004724 IWL_DEBUG_ISR("Disabled interrupts\n");
4725}
4726
4727static const char *desc_lookup(int i)
4728{
4729 switch (i) {
4730 case 1:
4731 return "FAIL";
4732 case 2:
4733 return "BAD_PARAM";
4734 case 3:
4735 return "BAD_CHECKSUM";
4736 case 4:
4737 return "NMI_INTERRUPT";
4738 case 5:
4739 return "SYSASSERT";
4740 case 6:
4741 return "FATAL_ERROR";
4742 }
4743
4744 return "UNKNOWN";
4745}
4746
4747#define ERROR_START_OFFSET (1 * sizeof(u32))
4748#define ERROR_ELEM_SIZE (7 * sizeof(u32))
4749
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004750static void iwl4965_dump_nic_error_log(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004751{
4752 u32 data2, line;
4753 u32 desc, time, count, base, data1;
4754 u32 blink1, blink2, ilink1, ilink2;
4755 int rc;
4756
4757 base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
4758
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004759 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004760 IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
4761 return;
4762 }
4763
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004764 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004765 if (rc) {
4766 IWL_WARNING("Can not read from adapter at this time.\n");
4767 return;
4768 }
4769
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004770 count = iwl4965_read_targ_mem(priv, base);
Zhu Yib481de92007-09-25 17:54:57 -07004771
4772 if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
4773 IWL_ERROR("Start IWL Error Log Dump:\n");
4774 IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
4775 priv->status, priv->config, count);
4776 }
4777
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004778 desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
4779 blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
4780 blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
4781 ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
4782 ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
4783 data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
4784 data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
4785 line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
4786 time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07004787
4788 IWL_ERROR("Desc Time "
4789 "data1 data2 line\n");
4790 IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
4791 desc_lookup(desc), desc, time, data1, data2, line);
4792 IWL_ERROR("blink1 blink2 ilink1 ilink2\n");
4793 IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
4794 ilink1, ilink2);
4795
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004796 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004797}
4798
4799#define EVENT_START_OFFSET (4 * sizeof(u32))
4800
4801/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004802 * iwl4965_print_event_log - Dump error event log to syslog
Zhu Yib481de92007-09-25 17:54:57 -07004803 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004804 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
Zhu Yib481de92007-09-25 17:54:57 -07004805 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004806static void iwl4965_print_event_log(struct iwl4965_priv *priv, u32 start_idx,
Zhu Yib481de92007-09-25 17:54:57 -07004807 u32 num_events, u32 mode)
4808{
4809 u32 i;
4810 u32 base; /* SRAM byte address of event log header */
4811 u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
4812 u32 ptr; /* SRAM byte address of log data */
4813 u32 ev, time, data; /* event log data */
4814
4815 if (num_events == 0)
4816 return;
4817
4818 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
4819
4820 if (mode == 0)
4821 event_size = 2 * sizeof(u32);
4822 else
4823 event_size = 3 * sizeof(u32);
4824
4825 ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
4826
4827 /* "time" is actually "data" for mode 0 (no timestamp).
4828 * place event id # at far right for easier visual parsing. */
4829 for (i = 0; i < num_events; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004830 ev = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004831 ptr += sizeof(u32);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004832 time = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004833 ptr += sizeof(u32);
4834 if (mode == 0)
4835 IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
4836 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004837 data = iwl4965_read_targ_mem(priv, ptr);
Zhu Yib481de92007-09-25 17:54:57 -07004838 ptr += sizeof(u32);
4839 IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
4840 }
4841 }
4842}
4843
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004844static void iwl4965_dump_nic_event_log(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004845{
4846 int rc;
4847 u32 base; /* SRAM byte address of event log header */
4848 u32 capacity; /* event log capacity in # entries */
4849 u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
4850 u32 num_wraps; /* # times uCode wrapped to top of log */
4851 u32 next_entry; /* index of next entry to be written by uCode */
4852 u32 size; /* # entries that we'll print */
4853
4854 base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004855 if (!iwl4965_hw_valid_rtc_data_addr(base)) {
Zhu Yib481de92007-09-25 17:54:57 -07004856 IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
4857 return;
4858 }
4859
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004860 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004861 if (rc) {
4862 IWL_WARNING("Can not read from adapter at this time.\n");
4863 return;
4864 }
4865
4866 /* event log header */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004867 capacity = iwl4965_read_targ_mem(priv, base);
4868 mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
4869 num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
4870 next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
Zhu Yib481de92007-09-25 17:54:57 -07004871
4872 size = num_wraps ? capacity : next_entry;
4873
4874 /* bail out if nothing in log */
4875 if (size == 0) {
Zhu Yi583fab32007-09-27 11:27:30 +08004876 IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004877 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004878 return;
4879 }
4880
Zhu Yi583fab32007-09-27 11:27:30 +08004881 IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
Zhu Yib481de92007-09-25 17:54:57 -07004882 size, num_wraps);
4883
4884 /* if uCode has wrapped back to top of log, start at the oldest entry,
4885 * i.e the next one that uCode would fill. */
4886 if (num_wraps)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004887 iwl4965_print_event_log(priv, next_entry,
Zhu Yib481de92007-09-25 17:54:57 -07004888 capacity - next_entry, mode);
4889
4890 /* (then/else) start at top of log */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004891 iwl4965_print_event_log(priv, 0, next_entry, mode);
Zhu Yib481de92007-09-25 17:54:57 -07004892
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004893 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004894}
4895
4896/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004897 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
Zhu Yib481de92007-09-25 17:54:57 -07004898 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004899static void iwl4965_irq_handle_error(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004900{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004901 /* Set the FW error flag -- cleared on iwl4965_down */
Zhu Yib481de92007-09-25 17:54:57 -07004902 set_bit(STATUS_FW_ERROR, &priv->status);
4903
4904 /* Cancel currently queued command. */
4905 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
4906
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004907#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004908 if (iwl4965_debug_level & IWL_DL_FW_ERRORS) {
4909 iwl4965_dump_nic_error_log(priv);
4910 iwl4965_dump_nic_event_log(priv);
4911 iwl4965_print_rx_config_cmd(&priv->staging_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07004912 }
4913#endif
4914
4915 wake_up_interruptible(&priv->wait_command_queue);
4916
4917 /* Keep the restart process from trying to send host
4918 * commands by clearing the INIT status bit */
4919 clear_bit(STATUS_READY, &priv->status);
4920
4921 if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
4922 IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
4923 "Restarting adapter due to uCode error.\n");
4924
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004925 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07004926 memcpy(&priv->recovery_rxon, &priv->active_rxon,
4927 sizeof(priv->recovery_rxon));
4928 priv->error_recovering = 1;
4929 }
4930 queue_work(priv->workqueue, &priv->restart);
4931 }
4932}
4933
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004934static void iwl4965_error_recovery(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004935{
4936 unsigned long flags;
4937
4938 memcpy(&priv->staging_rxon, &priv->recovery_rxon,
4939 sizeof(priv->staging_rxon));
4940 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004941 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004942
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004943 iwl4965_rxon_add_station(priv, priv->bssid, 1);
Zhu Yib481de92007-09-25 17:54:57 -07004944
4945 spin_lock_irqsave(&priv->lock, flags);
4946 priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
4947 priv->error_recovering = 0;
4948 spin_unlock_irqrestore(&priv->lock, flags);
4949}
4950
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004951static void iwl4965_irq_tasklet(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07004952{
4953 u32 inta, handled = 0;
4954 u32 inta_fh;
4955 unsigned long flags;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004956#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07004957 u32 inta_mask;
4958#endif
4959
4960 spin_lock_irqsave(&priv->lock, flags);
4961
4962 /* Ack/clear/reset pending uCode interrupts.
4963 * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
4964 * and will clear only when CSR_FH_INT_STATUS gets cleared. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004965 inta = iwl4965_read32(priv, CSR_INT);
4966 iwl4965_write32(priv, CSR_INT, inta);
Zhu Yib481de92007-09-25 17:54:57 -07004967
4968 /* Ack/clear/reset pending flow-handler (DMA) interrupts.
4969 * Any new interrupts that happen after this, either while we're
4970 * in this tasklet, or later, will show up in next ISR/tasklet. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004971 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
4972 iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
Zhu Yib481de92007-09-25 17:54:57 -07004973
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08004974#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004975 if (iwl4965_debug_level & IWL_DL_ISR) {
Ben Cahill9fbab512007-11-29 11:09:47 +08004976 /* just for debug */
4977 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
Zhu Yib481de92007-09-25 17:54:57 -07004978 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
4979 inta, inta_mask, inta_fh);
4980 }
4981#endif
4982
4983 /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
4984 * atomic, make sure that inta covers all the interrupts that
4985 * we've discovered, even if FH interrupt came in just after
4986 * reading CSR_INT. */
4987 if (inta_fh & CSR_FH_INT_RX_MASK)
4988 inta |= CSR_INT_BIT_FH_RX;
4989 if (inta_fh & CSR_FH_INT_TX_MASK)
4990 inta |= CSR_INT_BIT_FH_TX;
4991
4992 /* Now service all interrupt bits discovered above. */
4993 if (inta & CSR_INT_BIT_HW_ERR) {
4994 IWL_ERROR("Microcode HW error detected. Restarting.\n");
4995
4996 /* Tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004997 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07004998
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08004999 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005000
5001 handled |= CSR_INT_BIT_HW_ERR;
5002
5003 spin_unlock_irqrestore(&priv->lock, flags);
5004
5005 return;
5006 }
5007
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005008#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005009 if (iwl4965_debug_level & (IWL_DL_ISR)) {
Zhu Yib481de92007-09-25 17:54:57 -07005010 /* NIC fires this, but we don't use it, redundant with WAKEUP */
5011 if (inta & CSR_INT_BIT_MAC_CLK_ACTV)
5012 IWL_DEBUG_ISR("Microcode started or stopped.\n");
5013
5014 /* Alive notification via Rx interrupt will do the real work */
5015 if (inta & CSR_INT_BIT_ALIVE)
5016 IWL_DEBUG_ISR("Alive interrupt\n");
5017 }
5018#endif
5019 /* Safely ignore these bits for debug checks below */
5020 inta &= ~(CSR_INT_BIT_MAC_CLK_ACTV | CSR_INT_BIT_ALIVE);
5021
Ben Cahill9fbab512007-11-29 11:09:47 +08005022 /* HW RF KILL switch toggled */
Zhu Yib481de92007-09-25 17:54:57 -07005023 if (inta & CSR_INT_BIT_RF_KILL) {
5024 int hw_rf_kill = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005025 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07005026 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
5027 hw_rf_kill = 1;
5028
5029 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
5030 "RF_KILL bit toggled to %s.\n",
5031 hw_rf_kill ? "disable radio":"enable radio");
5032
5033 /* Queue restart only if RF_KILL switch was set to "kill"
5034 * when we loaded driver, and is now set to "enable".
5035 * After we're Alive, RF_KILL gets handled by
5036 * iwl_rx_card_state_notif() */
Zhu Yi53e49092007-12-06 16:08:44 +08005037 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
5038 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Zhu Yib481de92007-09-25 17:54:57 -07005039 queue_work(priv->workqueue, &priv->restart);
Zhu Yi53e49092007-12-06 16:08:44 +08005040 }
Zhu Yib481de92007-09-25 17:54:57 -07005041
5042 handled |= CSR_INT_BIT_RF_KILL;
5043 }
5044
Ben Cahill9fbab512007-11-29 11:09:47 +08005045 /* Chip got too hot and stopped itself */
Zhu Yib481de92007-09-25 17:54:57 -07005046 if (inta & CSR_INT_BIT_CT_KILL) {
5047 IWL_ERROR("Microcode CT kill error detected.\n");
5048 handled |= CSR_INT_BIT_CT_KILL;
5049 }
5050
5051 /* Error detected by uCode */
5052 if (inta & CSR_INT_BIT_SW_ERR) {
5053 IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
5054 inta);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005055 iwl4965_irq_handle_error(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005056 handled |= CSR_INT_BIT_SW_ERR;
5057 }
5058
5059 /* uCode wakes up after power-down sleep */
5060 if (inta & CSR_INT_BIT_WAKEUP) {
5061 IWL_DEBUG_ISR("Wakeup interrupt\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005062 iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
5063 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
5064 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
5065 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
5066 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
5067 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
5068 iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
Zhu Yib481de92007-09-25 17:54:57 -07005069
5070 handled |= CSR_INT_BIT_WAKEUP;
5071 }
5072
5073 /* All uCode command responses, including Tx command responses,
5074 * Rx "responses" (frame-received notification), and other
5075 * notifications from uCode come through here*/
5076 if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005077 iwl4965_rx_handle(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005078 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
5079 }
5080
5081 if (inta & CSR_INT_BIT_FH_TX) {
5082 IWL_DEBUG_ISR("Tx interrupt\n");
5083 handled |= CSR_INT_BIT_FH_TX;
5084 }
5085
5086 if (inta & ~handled)
5087 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
5088
5089 if (inta & ~CSR_INI_SET_MASK) {
5090 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
5091 inta & ~CSR_INI_SET_MASK);
5092 IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
5093 }
5094
5095 /* Re-enable all interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005096 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005097
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08005098#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005099 if (iwl4965_debug_level & (IWL_DL_ISR)) {
5100 inta = iwl4965_read32(priv, CSR_INT);
5101 inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
5102 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07005103 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
5104 "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
5105 }
5106#endif
5107 spin_unlock_irqrestore(&priv->lock, flags);
5108}
5109
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005110static irqreturn_t iwl4965_isr(int irq, void *data)
Zhu Yib481de92007-09-25 17:54:57 -07005111{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005112 struct iwl4965_priv *priv = data;
Zhu Yib481de92007-09-25 17:54:57 -07005113 u32 inta, inta_mask;
5114 u32 inta_fh;
5115 if (!priv)
5116 return IRQ_NONE;
5117
5118 spin_lock(&priv->lock);
5119
5120 /* Disable (but don't clear!) interrupts here to avoid
5121 * back-to-back ISRs and sporadic interrupts from our NIC.
5122 * If we have something to service, the tasklet will re-enable ints.
5123 * If we *don't* have something, we'll re-enable before leaving here. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005124 inta_mask = iwl4965_read32(priv, CSR_INT_MASK); /* just for debug */
5125 iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
Zhu Yib481de92007-09-25 17:54:57 -07005126
5127 /* Discover which interrupts are active/pending */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005128 inta = iwl4965_read32(priv, CSR_INT);
5129 inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
Zhu Yib481de92007-09-25 17:54:57 -07005130
5131 /* Ignore interrupt if there's nothing in NIC to service.
5132 * This may be due to IRQ shared with another device,
5133 * or due to sporadic interrupts thrown from our NIC. */
5134 if (!inta && !inta_fh) {
5135 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
5136 goto none;
5137 }
5138
5139 if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
Oliver Neukum66fbb542007-11-15 09:31:10 +08005140 /* Hardware disappeared. It might have already raised
5141 * an interrupt */
Zhu Yib481de92007-09-25 17:54:57 -07005142 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
Oliver Neukum66fbb542007-11-15 09:31:10 +08005143 goto unplugged;
Zhu Yib481de92007-09-25 17:54:57 -07005144 }
5145
5146 IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
5147 inta, inta_mask, inta_fh);
5148
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005149 /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
Zhu Yib481de92007-09-25 17:54:57 -07005150 tasklet_schedule(&priv->irq_tasklet);
Zhu Yib481de92007-09-25 17:54:57 -07005151
Oliver Neukum66fbb542007-11-15 09:31:10 +08005152 unplugged:
5153 spin_unlock(&priv->lock);
Zhu Yib481de92007-09-25 17:54:57 -07005154 return IRQ_HANDLED;
5155
5156 none:
5157 /* re-enable interrupts here since we don't have anything to service. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005158 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005159 spin_unlock(&priv->lock);
5160 return IRQ_NONE;
5161}
5162
5163/************************** EEPROM BANDS ****************************
5164 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005165 * The iwl4965_eeprom_band definitions below provide the mapping from the
Zhu Yib481de92007-09-25 17:54:57 -07005166 * EEPROM contents to the specific channel number supported for each
5167 * band.
5168 *
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005169 * For example, iwl4965_priv->eeprom.band_3_channels[4] from the band_3
Zhu Yib481de92007-09-25 17:54:57 -07005170 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
5171 * The specific geography and calibration information for that channel
5172 * is contained in the eeprom map itself.
5173 *
5174 * During init, we copy the eeprom information and channel map
5175 * information into priv->channel_info_24/52 and priv->channel_map_24/52
5176 *
5177 * channel_map_24/52 provides the index in the channel_info array for a
5178 * given channel. We have to have two separate maps as there is channel
5179 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
5180 * band_2
5181 *
5182 * A value of 0xff stored in the channel_map indicates that the channel
5183 * is not supported by the hardware at all.
5184 *
5185 * A value of 0xfe in the channel_map indicates that the channel is not
5186 * valid for Tx with the current hardware. This means that
5187 * while the system can tune and receive on a given channel, it may not
5188 * be able to associate or transmit any frames on that
5189 * channel. There is no corresponding channel information for that
5190 * entry.
5191 *
5192 *********************************************************************/
5193
5194/* 2.4 GHz */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005195static const u8 iwl4965_eeprom_band_1[14] = {
Zhu Yib481de92007-09-25 17:54:57 -07005196 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
5197};
5198
5199/* 5.2 GHz bands */
Ben Cahill9fbab512007-11-29 11:09:47 +08005200static const u8 iwl4965_eeprom_band_2[] = { /* 4915-5080MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005201 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
5202};
5203
Ben Cahill9fbab512007-11-29 11:09:47 +08005204static const u8 iwl4965_eeprom_band_3[] = { /* 5170-5320MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005205 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
5206};
5207
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005208static const u8 iwl4965_eeprom_band_4[] = { /* 5500-5700MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005209 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
5210};
5211
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005212static const u8 iwl4965_eeprom_band_5[] = { /* 5725-5825MHz */
Zhu Yib481de92007-09-25 17:54:57 -07005213 145, 149, 153, 157, 161, 165
5214};
5215
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005216static u8 iwl4965_eeprom_band_6[] = { /* 2.4 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005217 1, 2, 3, 4, 5, 6, 7
5218};
5219
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005220static u8 iwl4965_eeprom_band_7[] = { /* 5.2 FAT channel */
Zhu Yib481de92007-09-25 17:54:57 -07005221 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
5222};
5223
Ben Cahill9fbab512007-11-29 11:09:47 +08005224static void iwl4965_init_band_reference(const struct iwl4965_priv *priv,
5225 int band,
Zhu Yib481de92007-09-25 17:54:57 -07005226 int *eeprom_ch_count,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005227 const struct iwl4965_eeprom_channel
Zhu Yib481de92007-09-25 17:54:57 -07005228 **eeprom_ch_info,
5229 const u8 **eeprom_ch_index)
5230{
5231 switch (band) {
5232 case 1: /* 2.4GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005233 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_1);
Zhu Yib481de92007-09-25 17:54:57 -07005234 *eeprom_ch_info = priv->eeprom.band_1_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005235 *eeprom_ch_index = iwl4965_eeprom_band_1;
Zhu Yib481de92007-09-25 17:54:57 -07005236 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005237 case 2: /* 4.9GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005238 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_2);
Zhu Yib481de92007-09-25 17:54:57 -07005239 *eeprom_ch_info = priv->eeprom.band_2_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005240 *eeprom_ch_index = iwl4965_eeprom_band_2;
Zhu Yib481de92007-09-25 17:54:57 -07005241 break;
5242 case 3: /* 5.2GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005243 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_3);
Zhu Yib481de92007-09-25 17:54:57 -07005244 *eeprom_ch_info = priv->eeprom.band_3_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005245 *eeprom_ch_index = iwl4965_eeprom_band_3;
Zhu Yib481de92007-09-25 17:54:57 -07005246 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005247 case 4: /* 5.5GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005248 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_4);
Zhu Yib481de92007-09-25 17:54:57 -07005249 *eeprom_ch_info = priv->eeprom.band_4_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005250 *eeprom_ch_index = iwl4965_eeprom_band_4;
Zhu Yib481de92007-09-25 17:54:57 -07005251 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005252 case 5: /* 5.7GHz band */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005253 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005254 *eeprom_ch_info = priv->eeprom.band_5_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005255 *eeprom_ch_index = iwl4965_eeprom_band_5;
Zhu Yib481de92007-09-25 17:54:57 -07005256 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005257 case 6: /* 2.4GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005258 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_6);
Zhu Yib481de92007-09-25 17:54:57 -07005259 *eeprom_ch_info = priv->eeprom.band_24_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005260 *eeprom_ch_index = iwl4965_eeprom_band_6;
Zhu Yib481de92007-09-25 17:54:57 -07005261 break;
Ben Cahill9fbab512007-11-29 11:09:47 +08005262 case 7: /* 5 GHz FAT channels */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005263 *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_7);
Zhu Yib481de92007-09-25 17:54:57 -07005264 *eeprom_ch_info = priv->eeprom.band_52_channels;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005265 *eeprom_ch_index = iwl4965_eeprom_band_7;
Zhu Yib481de92007-09-25 17:54:57 -07005266 break;
5267 default:
5268 BUG();
5269 return;
5270 }
5271}
5272
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005273const struct iwl4965_channel_info *iwl4965_get_channel_info(const struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005274 int phymode, u16 channel)
5275{
5276 int i;
5277
5278 switch (phymode) {
5279 case MODE_IEEE80211A:
5280 for (i = 14; i < priv->channel_count; i++) {
5281 if (priv->channel_info[i].channel == channel)
5282 return &priv->channel_info[i];
5283 }
5284 break;
5285
5286 case MODE_IEEE80211B:
5287 case MODE_IEEE80211G:
5288 if (channel >= 1 && channel <= 14)
5289 return &priv->channel_info[channel - 1];
5290 break;
5291
5292 }
5293
5294 return NULL;
5295}
5296
5297#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
5298 ? # x " " : "")
5299
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005300static int iwl4965_init_channel_map(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005301{
5302 int eeprom_ch_count = 0;
5303 const u8 *eeprom_ch_index = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005304 const struct iwl4965_eeprom_channel *eeprom_ch_info = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07005305 int band, ch;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005306 struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005307
5308 if (priv->channel_count) {
5309 IWL_DEBUG_INFO("Channel map already initialized.\n");
5310 return 0;
5311 }
5312
5313 if (priv->eeprom.version < 0x2f) {
5314 IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
5315 priv->eeprom.version);
5316 return -EINVAL;
5317 }
5318
5319 IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
5320
5321 priv->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005322 ARRAY_SIZE(iwl4965_eeprom_band_1) +
5323 ARRAY_SIZE(iwl4965_eeprom_band_2) +
5324 ARRAY_SIZE(iwl4965_eeprom_band_3) +
5325 ARRAY_SIZE(iwl4965_eeprom_band_4) +
5326 ARRAY_SIZE(iwl4965_eeprom_band_5);
Zhu Yib481de92007-09-25 17:54:57 -07005327
5328 IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
5329
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005330 priv->channel_info = kzalloc(sizeof(struct iwl4965_channel_info) *
Zhu Yib481de92007-09-25 17:54:57 -07005331 priv->channel_count, GFP_KERNEL);
5332 if (!priv->channel_info) {
5333 IWL_ERROR("Could not allocate channel_info\n");
5334 priv->channel_count = 0;
5335 return -ENOMEM;
5336 }
5337
5338 ch_info = priv->channel_info;
5339
5340 /* Loop through the 5 EEPROM bands adding them in order to the
5341 * channel map we maintain (that contains additional information than
5342 * what just in the EEPROM) */
5343 for (band = 1; band <= 5; band++) {
5344
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005345 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005346 &eeprom_ch_info, &eeprom_ch_index);
5347
5348 /* Loop through each band adding each of the channels */
5349 for (ch = 0; ch < eeprom_ch_count; ch++) {
5350 ch_info->channel = eeprom_ch_index[ch];
5351 ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
5352 MODE_IEEE80211A;
5353
5354 /* permanently store EEPROM's channel regulatory flags
5355 * and max power in channel info database. */
5356 ch_info->eeprom = eeprom_ch_info[ch];
5357
5358 /* Copy the run-time flags so they are there even on
5359 * invalid channels */
5360 ch_info->flags = eeprom_ch_info[ch].flags;
5361
5362 if (!(is_channel_valid(ch_info))) {
5363 IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
5364 "No traffic\n",
5365 ch_info->channel,
5366 ch_info->flags,
5367 is_channel_a_band(ch_info) ?
5368 "5.2" : "2.4");
5369 ch_info++;
5370 continue;
5371 }
5372
5373 /* Initialize regulatory-based run-time data */
5374 ch_info->max_power_avg = ch_info->curr_txpow =
5375 eeprom_ch_info[ch].max_power_avg;
5376 ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
5377 ch_info->min_power = 0;
5378
5379 IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
5380 " %ddBm): Ad-Hoc %ssupported\n",
5381 ch_info->channel,
5382 is_channel_a_band(ch_info) ?
5383 "5.2" : "2.4",
5384 CHECK_AND_PRINT(IBSS),
5385 CHECK_AND_PRINT(ACTIVE),
5386 CHECK_AND_PRINT(RADAR),
5387 CHECK_AND_PRINT(WIDE),
5388 CHECK_AND_PRINT(NARROW),
5389 CHECK_AND_PRINT(DFS),
5390 eeprom_ch_info[ch].flags,
5391 eeprom_ch_info[ch].max_power_avg,
5392 ((eeprom_ch_info[ch].
5393 flags & EEPROM_CHANNEL_IBSS)
5394 && !(eeprom_ch_info[ch].
5395 flags & EEPROM_CHANNEL_RADAR))
5396 ? "" : "not ");
5397
5398 /* Set the user_txpower_limit to the highest power
5399 * supported by any channel */
5400 if (eeprom_ch_info[ch].max_power_avg >
5401 priv->user_txpower_limit)
5402 priv->user_txpower_limit =
5403 eeprom_ch_info[ch].max_power_avg;
5404
5405 ch_info++;
5406 }
5407 }
5408
5409 for (band = 6; band <= 7; band++) {
5410 int phymode;
5411 u8 fat_extension_chan;
5412
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005413 iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
Zhu Yib481de92007-09-25 17:54:57 -07005414 &eeprom_ch_info, &eeprom_ch_index);
5415
5416 phymode = (band == 6) ? MODE_IEEE80211B : MODE_IEEE80211A;
5417 /* Loop through each band adding each of the channels */
5418 for (ch = 0; ch < eeprom_ch_count; ch++) {
5419
5420 if ((band == 6) &&
5421 ((eeprom_ch_index[ch] == 5) ||
5422 (eeprom_ch_index[ch] == 6) ||
5423 (eeprom_ch_index[ch] == 7)))
5424 fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
5425 else
5426 fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
5427
5428 iwl4965_set_fat_chan_info(priv, phymode,
5429 eeprom_ch_index[ch],
5430 &(eeprom_ch_info[ch]),
5431 fat_extension_chan);
5432
5433 iwl4965_set_fat_chan_info(priv, phymode,
5434 (eeprom_ch_index[ch] + 4),
5435 &(eeprom_ch_info[ch]),
5436 HT_IE_EXT_CHANNEL_BELOW);
5437 }
5438 }
5439
5440 return 0;
5441}
5442
5443/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
5444 * sending probe req. This should be set long enough to hear probe responses
5445 * from more than one AP. */
5446#define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
5447#define IWL_ACTIVE_DWELL_TIME_52 (10)
5448
5449/* For faster active scanning, scan will move to the next channel if fewer than
5450 * PLCP_QUIET_THRESH packets are heard on this channel within
5451 * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
5452 * time if it's a quiet channel (nothing responded to our probe, and there's
5453 * no other traffic).
5454 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
5455#define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
5456#define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
5457
5458/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
5459 * Must be set longer than active dwell time.
5460 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
5461#define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
5462#define IWL_PASSIVE_DWELL_TIME_52 (10)
5463#define IWL_PASSIVE_DWELL_BASE (100)
5464#define IWL_CHANNEL_TUNE_TIME 5
5465
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005466static inline u16 iwl4965_get_active_dwell_time(struct iwl4965_priv *priv, int phymode)
Zhu Yib481de92007-09-25 17:54:57 -07005467{
5468 if (phymode == MODE_IEEE80211A)
5469 return IWL_ACTIVE_DWELL_TIME_52;
5470 else
5471 return IWL_ACTIVE_DWELL_TIME_24;
5472}
5473
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005474static u16 iwl4965_get_passive_dwell_time(struct iwl4965_priv *priv, int phymode)
Zhu Yib481de92007-09-25 17:54:57 -07005475{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005476 u16 active = iwl4965_get_active_dwell_time(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07005477 u16 passive = (phymode != MODE_IEEE80211A) ?
5478 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
5479 IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
5480
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005481 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005482 /* If we're associated, we clamp the maximum passive
5483 * dwell time to be 98% of the beacon interval (minus
5484 * 2 * channel tune time) */
5485 passive = priv->beacon_int;
5486 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
5487 passive = IWL_PASSIVE_DWELL_BASE;
5488 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
5489 }
5490
5491 if (passive <= active)
5492 passive = active + 1;
5493
5494 return passive;
5495}
5496
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005497static int iwl4965_get_channels_for_scan(struct iwl4965_priv *priv, int phymode,
Zhu Yib481de92007-09-25 17:54:57 -07005498 u8 is_active, u8 direct_mask,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005499 struct iwl4965_scan_channel *scan_ch)
Zhu Yib481de92007-09-25 17:54:57 -07005500{
5501 const struct ieee80211_channel *channels = NULL;
5502 const struct ieee80211_hw_mode *hw_mode;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005503 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07005504 u16 passive_dwell = 0;
5505 u16 active_dwell = 0;
5506 int added, i;
5507
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005508 hw_mode = iwl4965_get_hw_mode(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07005509 if (!hw_mode)
5510 return 0;
5511
5512 channels = hw_mode->channels;
5513
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005514 active_dwell = iwl4965_get_active_dwell_time(priv, phymode);
5515 passive_dwell = iwl4965_get_passive_dwell_time(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07005516
5517 for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
5518 if (channels[i].chan ==
5519 le16_to_cpu(priv->active_rxon.channel)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005520 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07005521 IWL_DEBUG_SCAN
5522 ("Skipping current channel %d\n",
5523 le16_to_cpu(priv->active_rxon.channel));
5524 continue;
5525 }
5526 } else if (priv->only_active_channel)
5527 continue;
5528
5529 scan_ch->channel = channels[i].chan;
5530
Ben Cahill9fbab512007-11-29 11:09:47 +08005531 ch_info = iwl4965_get_channel_info(priv, phymode,
5532 scan_ch->channel);
Zhu Yib481de92007-09-25 17:54:57 -07005533 if (!is_channel_valid(ch_info)) {
5534 IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
5535 scan_ch->channel);
5536 continue;
5537 }
5538
5539 if (!is_active || is_channel_passive(ch_info) ||
5540 !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
5541 scan_ch->type = 0; /* passive */
5542 else
5543 scan_ch->type = 1; /* active */
5544
5545 if (scan_ch->type & 1)
5546 scan_ch->type |= (direct_mask << 1);
5547
5548 if (is_channel_narrow(ch_info))
5549 scan_ch->type |= (1 << 7);
5550
5551 scan_ch->active_dwell = cpu_to_le16(active_dwell);
5552 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
5553
Ben Cahill9fbab512007-11-29 11:09:47 +08005554 /* Set txpower levels to defaults */
Zhu Yib481de92007-09-25 17:54:57 -07005555 scan_ch->tpc.dsp_atten = 110;
5556 /* scan_pwr_info->tpc.dsp_atten; */
5557
5558 /*scan_pwr_info->tpc.tx_gain; */
5559 if (phymode == MODE_IEEE80211A)
5560 scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
5561 else {
5562 scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
5563 /* NOTE: if we were doing 6Mb OFDM for scans we'd use
Ben Cahill9fbab512007-11-29 11:09:47 +08005564 * power level:
5565 * scan_ch->tpc.tx_gain = ((1<<5) | (2 << 3)) | 3;
Zhu Yib481de92007-09-25 17:54:57 -07005566 */
5567 }
5568
5569 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
5570 scan_ch->channel,
5571 (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
5572 (scan_ch->type & 1) ?
5573 active_dwell : passive_dwell);
5574
5575 scan_ch++;
5576 added++;
5577 }
5578
5579 IWL_DEBUG_SCAN("total channels to scan %d \n", added);
5580 return added;
5581}
5582
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005583static void iwl4965_reset_channel_flag(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005584{
5585 int i, j;
5586 for (i = 0; i < 3; i++) {
5587 struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
5588 for (j = 0; j < hw_mode->num_channels; j++)
5589 hw_mode->channels[j].flag = hw_mode->channels[j].val;
5590 }
5591}
5592
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005593static void iwl4965_init_hw_rates(struct iwl4965_priv *priv,
Zhu Yib481de92007-09-25 17:54:57 -07005594 struct ieee80211_rate *rates)
5595{
5596 int i;
5597
5598 for (i = 0; i < IWL_RATE_COUNT; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005599 rates[i].rate = iwl4965_rates[i].ieee * 5;
Zhu Yib481de92007-09-25 17:54:57 -07005600 rates[i].val = i; /* Rate scaling will work on indexes */
5601 rates[i].val2 = i;
5602 rates[i].flags = IEEE80211_RATE_SUPPORTED;
5603 /* Only OFDM have the bits-per-symbol set */
5604 if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
5605 rates[i].flags |= IEEE80211_RATE_OFDM;
5606 else {
5607 /*
5608 * If CCK 1M then set rate flag to CCK else CCK_2
5609 * which is CCK | PREAMBLE2
5610 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005611 rates[i].flags |= (iwl4965_rates[i].plcp == 10) ?
Zhu Yib481de92007-09-25 17:54:57 -07005612 IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
5613 }
5614
5615 /* Set up which ones are basic rates... */
5616 if (IWL_BASIC_RATES_MASK & (1 << i))
5617 rates[i].flags |= IEEE80211_RATE_BASIC;
5618 }
Zhu Yib481de92007-09-25 17:54:57 -07005619}
5620
5621/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005622 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
Zhu Yib481de92007-09-25 17:54:57 -07005623 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005624static int iwl4965_init_geos(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005625{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005626 struct iwl4965_channel_info *ch;
Zhu Yib481de92007-09-25 17:54:57 -07005627 struct ieee80211_hw_mode *modes;
5628 struct ieee80211_channel *channels;
5629 struct ieee80211_channel *geo_ch;
5630 struct ieee80211_rate *rates;
5631 int i = 0;
5632 enum {
5633 A = 0,
5634 B = 1,
5635 G = 2,
5636 A_11N = 3,
5637 G_11N = 4,
5638 };
5639 int mode_count = 5;
5640
5641 if (priv->modes) {
5642 IWL_DEBUG_INFO("Geography modes already initialized.\n");
5643 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5644 return 0;
5645 }
5646
5647 modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
5648 GFP_KERNEL);
5649 if (!modes)
5650 return -ENOMEM;
5651
5652 channels = kzalloc(sizeof(struct ieee80211_channel) *
5653 priv->channel_count, GFP_KERNEL);
5654 if (!channels) {
5655 kfree(modes);
5656 return -ENOMEM;
5657 }
5658
5659 rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
5660 GFP_KERNEL);
5661 if (!rates) {
5662 kfree(modes);
5663 kfree(channels);
5664 return -ENOMEM;
5665 }
5666
5667 /* 0 = 802.11a
5668 * 1 = 802.11b
5669 * 2 = 802.11g
5670 */
5671
5672 /* 5.2GHz channels start after the 2.4GHz channels */
5673 modes[A].mode = MODE_IEEE80211A;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005674 modes[A].channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
Zhu Yib481de92007-09-25 17:54:57 -07005675 modes[A].rates = rates;
5676 modes[A].num_rates = 8; /* just OFDM */
5677 modes[A].rates = &rates[4];
5678 modes[A].num_channels = 0;
5679
5680 modes[B].mode = MODE_IEEE80211B;
5681 modes[B].channels = channels;
5682 modes[B].rates = rates;
5683 modes[B].num_rates = 4; /* just CCK */
5684 modes[B].num_channels = 0;
5685
5686 modes[G].mode = MODE_IEEE80211G;
5687 modes[G].channels = channels;
5688 modes[G].rates = rates;
5689 modes[G].num_rates = 12; /* OFDM & CCK */
5690 modes[G].num_channels = 0;
5691
5692 modes[G_11N].mode = MODE_IEEE80211G;
5693 modes[G_11N].channels = channels;
5694 modes[G_11N].num_rates = 13; /* OFDM & CCK */
5695 modes[G_11N].rates = rates;
5696 modes[G_11N].num_channels = 0;
5697
5698 modes[A_11N].mode = MODE_IEEE80211A;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005699 modes[A_11N].channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
Zhu Yib481de92007-09-25 17:54:57 -07005700 modes[A_11N].rates = &rates[4];
5701 modes[A_11N].num_rates = 9; /* just OFDM */
5702 modes[A_11N].num_channels = 0;
5703
5704 priv->ieee_channels = channels;
5705 priv->ieee_rates = rates;
5706
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005707 iwl4965_init_hw_rates(priv, rates);
Zhu Yib481de92007-09-25 17:54:57 -07005708
5709 for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
5710 ch = &priv->channel_info[i];
5711
5712 if (!is_channel_valid(ch)) {
5713 IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
5714 "skipping.\n",
5715 ch->channel, is_channel_a_band(ch) ?
5716 "5.2" : "2.4");
5717 continue;
5718 }
5719
5720 if (is_channel_a_band(ch)) {
5721 geo_ch = &modes[A].channels[modes[A].num_channels++];
5722 modes[A_11N].num_channels++;
5723 } else {
5724 geo_ch = &modes[B].channels[modes[B].num_channels++];
5725 modes[G].num_channels++;
5726 modes[G_11N].num_channels++;
5727 }
5728
5729 geo_ch->freq = ieee80211chan2mhz(ch->channel);
5730 geo_ch->chan = ch->channel;
5731 geo_ch->power_level = ch->max_power_avg;
5732 geo_ch->antenna_max = 0xff;
5733
5734 if (is_channel_valid(ch)) {
5735 geo_ch->flag = IEEE80211_CHAN_W_SCAN;
5736 if (ch->flags & EEPROM_CHANNEL_IBSS)
5737 geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
5738
5739 if (ch->flags & EEPROM_CHANNEL_ACTIVE)
5740 geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
5741
5742 if (ch->flags & EEPROM_CHANNEL_RADAR)
5743 geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
5744
5745 if (ch->max_power_avg > priv->max_channel_txpower_limit)
5746 priv->max_channel_txpower_limit =
5747 ch->max_power_avg;
5748 }
5749
5750 geo_ch->val = geo_ch->flag;
5751 }
5752
5753 if ((modes[A].num_channels == 0) && priv->is_abg) {
5754 printk(KERN_INFO DRV_NAME
5755 ": Incorrectly detected BG card as ABG. Please send "
5756 "your PCI ID 0x%04X:0x%04X to maintainer.\n",
5757 priv->pci_dev->device, priv->pci_dev->subsystem_device);
5758 priv->is_abg = 0;
5759 }
5760
5761 printk(KERN_INFO DRV_NAME
5762 ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
5763 modes[G].num_channels, modes[A].num_channels);
5764
5765 /*
5766 * NOTE: We register these in preference of order -- the
5767 * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
5768 * a phymode based on rates or AP capabilities but seems to
5769 * configure it purely on if the channel being configured
5770 * is supported by a mode -- and the first match is taken
5771 */
5772
5773 if (modes[G].num_channels)
5774 ieee80211_register_hwmode(priv->hw, &modes[G]);
5775 if (modes[B].num_channels)
5776 ieee80211_register_hwmode(priv->hw, &modes[B]);
5777 if (modes[A].num_channels)
5778 ieee80211_register_hwmode(priv->hw, &modes[A]);
5779
5780 priv->modes = modes;
5781 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
5782
5783 return 0;
5784}
5785
5786/******************************************************************************
5787 *
5788 * uCode download functions
5789 *
5790 ******************************************************************************/
5791
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005792static void iwl4965_dealloc_ucode_pci(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005793{
5794 if (priv->ucode_code.v_addr != NULL) {
5795 pci_free_consistent(priv->pci_dev,
5796 priv->ucode_code.len,
5797 priv->ucode_code.v_addr,
5798 priv->ucode_code.p_addr);
5799 priv->ucode_code.v_addr = NULL;
5800 }
5801 if (priv->ucode_data.v_addr != NULL) {
5802 pci_free_consistent(priv->pci_dev,
5803 priv->ucode_data.len,
5804 priv->ucode_data.v_addr,
5805 priv->ucode_data.p_addr);
5806 priv->ucode_data.v_addr = NULL;
5807 }
5808 if (priv->ucode_data_backup.v_addr != NULL) {
5809 pci_free_consistent(priv->pci_dev,
5810 priv->ucode_data_backup.len,
5811 priv->ucode_data_backup.v_addr,
5812 priv->ucode_data_backup.p_addr);
5813 priv->ucode_data_backup.v_addr = NULL;
5814 }
5815 if (priv->ucode_init.v_addr != NULL) {
5816 pci_free_consistent(priv->pci_dev,
5817 priv->ucode_init.len,
5818 priv->ucode_init.v_addr,
5819 priv->ucode_init.p_addr);
5820 priv->ucode_init.v_addr = NULL;
5821 }
5822 if (priv->ucode_init_data.v_addr != NULL) {
5823 pci_free_consistent(priv->pci_dev,
5824 priv->ucode_init_data.len,
5825 priv->ucode_init_data.v_addr,
5826 priv->ucode_init_data.p_addr);
5827 priv->ucode_init_data.v_addr = NULL;
5828 }
5829 if (priv->ucode_boot.v_addr != NULL) {
5830 pci_free_consistent(priv->pci_dev,
5831 priv->ucode_boot.len,
5832 priv->ucode_boot.v_addr,
5833 priv->ucode_boot.p_addr);
5834 priv->ucode_boot.v_addr = NULL;
5835 }
5836}
5837
5838/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005839 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005840 * looking at all data.
5841 */
Ben Cahill9fbab512007-11-29 11:09:47 +08005842static int iwl4965_verify_inst_full(struct iwl4965_priv *priv, __le32 * image,
5843 u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005844{
5845 u32 val;
5846 u32 save_len = len;
5847 int rc = 0;
5848 u32 errcnt;
5849
5850 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5851
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005852 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005853 if (rc)
5854 return rc;
5855
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005856 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
Zhu Yib481de92007-09-25 17:54:57 -07005857
5858 errcnt = 0;
5859 for (; len > 0; len -= sizeof(u32), image++) {
5860 /* read data comes through single port, auto-incr addr */
5861 /* NOTE: Use the debugless read so we don't flood kernel log
5862 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005863 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005864 if (val != le32_to_cpu(*image)) {
5865 IWL_ERROR("uCode INST section is invalid at "
5866 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5867 save_len - len, val, le32_to_cpu(*image));
5868 rc = -EIO;
5869 errcnt++;
5870 if (errcnt >= 20)
5871 break;
5872 }
5873 }
5874
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005875 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005876
5877 if (!errcnt)
5878 IWL_DEBUG_INFO
5879 ("ucode image in INSTRUCTION memory is good\n");
5880
5881 return rc;
5882}
5883
5884
5885/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005886 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
Zhu Yib481de92007-09-25 17:54:57 -07005887 * using sample data 100 bytes apart. If these sample points are good,
5888 * it's a pretty good bet that everything between them is good, too.
5889 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005890static int iwl4965_verify_inst_sparse(struct iwl4965_priv *priv, __le32 *image, u32 len)
Zhu Yib481de92007-09-25 17:54:57 -07005891{
5892 u32 val;
5893 int rc = 0;
5894 u32 errcnt = 0;
5895 u32 i;
5896
5897 IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
5898
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005899 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005900 if (rc)
5901 return rc;
5902
5903 for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
5904 /* read data comes through single port, auto-incr addr */
5905 /* NOTE: Use the debugless read so we don't flood kernel log
5906 * if IWL_DL_IO is set */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005907 iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
Zhu Yib481de92007-09-25 17:54:57 -07005908 i + RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005909 val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
Zhu Yib481de92007-09-25 17:54:57 -07005910 if (val != le32_to_cpu(*image)) {
5911#if 0 /* Enable this if you want to see details */
5912 IWL_ERROR("uCode INST section is invalid at "
5913 "offset 0x%x, is 0x%x, s/b 0x%x\n",
5914 i, val, *image);
5915#endif
5916 rc = -EIO;
5917 errcnt++;
5918 if (errcnt >= 3)
5919 break;
5920 }
5921 }
5922
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005923 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07005924
5925 return rc;
5926}
5927
5928
5929/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005930 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
Zhu Yib481de92007-09-25 17:54:57 -07005931 * and verify its contents
5932 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005933static int iwl4965_verify_ucode(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005934{
5935 __le32 *image;
5936 u32 len;
5937 int rc = 0;
5938
5939 /* Try bootstrap */
5940 image = (__le32 *)priv->ucode_boot.v_addr;
5941 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005942 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005943 if (rc == 0) {
5944 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
5945 return 0;
5946 }
5947
5948 /* Try initialize */
5949 image = (__le32 *)priv->ucode_init.v_addr;
5950 len = priv->ucode_init.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005951 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005952 if (rc == 0) {
5953 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
5954 return 0;
5955 }
5956
5957 /* Try runtime/protocol */
5958 image = (__le32 *)priv->ucode_code.v_addr;
5959 len = priv->ucode_code.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005960 rc = iwl4965_verify_inst_sparse(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005961 if (rc == 0) {
5962 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
5963 return 0;
5964 }
5965
5966 IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
5967
Ben Cahill9fbab512007-11-29 11:09:47 +08005968 /* Since nothing seems to match, show first several data entries in
5969 * instruction SRAM, so maybe visual inspection will give a clue.
5970 * Selection of bootstrap image (vs. other images) is arbitrary. */
Zhu Yib481de92007-09-25 17:54:57 -07005971 image = (__le32 *)priv->ucode_boot.v_addr;
5972 len = priv->ucode_boot.len;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005973 rc = iwl4965_verify_inst_full(priv, image, len);
Zhu Yib481de92007-09-25 17:54:57 -07005974
5975 return rc;
5976}
5977
5978
5979/* check contents of special bootstrap uCode SRAM */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005980static int iwl4965_verify_bsm(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07005981{
5982 __le32 *image = priv->ucode_boot.v_addr;
5983 u32 len = priv->ucode_boot.len;
5984 u32 reg;
5985 u32 val;
5986
5987 IWL_DEBUG_INFO("Begin verify bsm\n");
5988
5989 /* verify BSM SRAM contents */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005990 val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
Zhu Yib481de92007-09-25 17:54:57 -07005991 for (reg = BSM_SRAM_LOWER_BOUND;
5992 reg < BSM_SRAM_LOWER_BOUND + len;
5993 reg += sizeof(u32), image ++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08005994 val = iwl4965_read_prph(priv, reg);
Zhu Yib481de92007-09-25 17:54:57 -07005995 if (val != le32_to_cpu(*image)) {
5996 IWL_ERROR("BSM uCode verification failed at "
5997 "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
5998 BSM_SRAM_LOWER_BOUND,
5999 reg - BSM_SRAM_LOWER_BOUND, len,
6000 val, le32_to_cpu(*image));
6001 return -EIO;
6002 }
6003 }
6004
6005 IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
6006
6007 return 0;
6008}
6009
6010/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006011 * iwl4965_load_bsm - Load bootstrap instructions
Zhu Yib481de92007-09-25 17:54:57 -07006012 *
6013 * BSM operation:
6014 *
6015 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
6016 * in special SRAM that does not power down during RFKILL. When powering back
6017 * up after power-saving sleeps (or during initial uCode load), the BSM loads
6018 * the bootstrap program into the on-board processor, and starts it.
6019 *
6020 * The bootstrap program loads (via DMA) instructions and data for a new
6021 * program from host DRAM locations indicated by the host driver in the
6022 * BSM_DRAM_* registers. Once the new program is loaded, it starts
6023 * automatically.
6024 *
6025 * When initializing the NIC, the host driver points the BSM to the
6026 * "initialize" uCode image. This uCode sets up some internal data, then
6027 * notifies host via "initialize alive" that it is complete.
6028 *
6029 * The host then replaces the BSM_DRAM_* pointer values to point to the
6030 * normal runtime uCode instructions and a backup uCode data cache buffer
6031 * (filled initially with starting data values for the on-board processor),
6032 * then triggers the "initialize" uCode to load and launch the runtime uCode,
6033 * which begins normal operation.
6034 *
6035 * When doing a power-save shutdown, runtime uCode saves data SRAM into
6036 * the backup data cache in DRAM before SRAM is powered down.
6037 *
6038 * When powering back up, the BSM loads the bootstrap program. This reloads
6039 * the runtime uCode instructions and the backup data cache into SRAM,
6040 * and re-launches the runtime uCode from where it left off.
6041 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006042static int iwl4965_load_bsm(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006043{
6044 __le32 *image = priv->ucode_boot.v_addr;
6045 u32 len = priv->ucode_boot.len;
6046 dma_addr_t pinst;
6047 dma_addr_t pdata;
6048 u32 inst_len;
6049 u32 data_len;
6050 int rc;
6051 int i;
6052 u32 done;
6053 u32 reg_offset;
6054
6055 IWL_DEBUG_INFO("Begin load bsm\n");
6056
6057 /* make sure bootstrap program is no larger than BSM's SRAM size */
6058 if (len > IWL_MAX_BSM_SIZE)
6059 return -EINVAL;
6060
6061 /* Tell bootstrap uCode where to find the "Initialize" uCode
Ben Cahill9fbab512007-11-29 11:09:47 +08006062 * in host DRAM ... host DRAM physical address bits 35:4 for 4965.
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006063 * NOTE: iwl4965_initialize_alive_start() will replace these values,
Zhu Yib481de92007-09-25 17:54:57 -07006064 * after the "initialize" uCode has run, to point to
6065 * runtime/protocol instructions and backup data cache. */
6066 pinst = priv->ucode_init.p_addr >> 4;
6067 pdata = priv->ucode_init_data.p_addr >> 4;
6068 inst_len = priv->ucode_init.len;
6069 data_len = priv->ucode_init_data.len;
6070
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006071 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006072 if (rc)
6073 return rc;
6074
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006075 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6076 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6077 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
6078 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
Zhu Yib481de92007-09-25 17:54:57 -07006079
6080 /* Fill BSM memory with bootstrap instructions */
6081 for (reg_offset = BSM_SRAM_LOWER_BOUND;
6082 reg_offset < BSM_SRAM_LOWER_BOUND + len;
6083 reg_offset += sizeof(u32), image++)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006084 _iwl4965_write_prph(priv, reg_offset,
Zhu Yib481de92007-09-25 17:54:57 -07006085 le32_to_cpu(*image));
6086
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006087 rc = iwl4965_verify_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006088 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006089 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006090 return rc;
6091 }
6092
6093 /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006094 iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
6095 iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006096 RTC_INST_LOWER_BOUND);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006097 iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
Zhu Yib481de92007-09-25 17:54:57 -07006098
6099 /* Load bootstrap code into instruction SRAM now,
6100 * to prepare to load "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006101 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006102 BSM_WR_CTRL_REG_BIT_START);
6103
6104 /* Wait for load of bootstrap uCode to finish */
6105 for (i = 0; i < 100; i++) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006106 done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
Zhu Yib481de92007-09-25 17:54:57 -07006107 if (!(done & BSM_WR_CTRL_REG_BIT_START))
6108 break;
6109 udelay(10);
6110 }
6111 if (i < 100)
6112 IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
6113 else {
6114 IWL_ERROR("BSM write did not complete!\n");
6115 return -EIO;
6116 }
6117
6118 /* Enable future boot loads whenever power management unit triggers it
6119 * (e.g. when powering back up after power-save shutdown) */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006120 iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006121 BSM_WR_CTRL_REG_BIT_START_EN);
6122
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006123 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006124
6125 return 0;
6126}
6127
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006128static void iwl4965_nic_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006129{
6130 /* Remove all resets to allow NIC to operate */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006131 iwl4965_write32(priv, CSR_RESET, 0);
Zhu Yib481de92007-09-25 17:54:57 -07006132}
6133
Tomas Winkler90e759d2007-11-29 11:09:41 +08006134static int iwl4965_alloc_fw_desc(struct pci_dev *pci_dev, struct fw_desc *desc)
6135{
6136 desc->v_addr = pci_alloc_consistent(pci_dev, desc->len, &desc->p_addr);
6137 return (desc->v_addr != NULL) ? 0 : -ENOMEM;
6138}
6139
Zhu Yib481de92007-09-25 17:54:57 -07006140/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006141 * iwl4965_read_ucode - Read uCode images from disk file.
Zhu Yib481de92007-09-25 17:54:57 -07006142 *
6143 * Copy into buffers for card to fetch via bus-mastering
6144 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006145static int iwl4965_read_ucode(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006146{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006147 struct iwl4965_ucode *ucode;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006148 int ret;
Zhu Yib481de92007-09-25 17:54:57 -07006149 const struct firmware *ucode_raw;
6150 const char *name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode";
6151 u8 *src;
6152 size_t len;
6153 u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
6154
6155 /* Ask kernel firmware_class module to get the boot firmware off disk.
6156 * request_firmware() is synchronous, file is in memory on return. */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006157 ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
6158 if (ret < 0) {
6159 IWL_ERROR("%s firmware file req failed: Reason %d\n",
6160 name, ret);
Zhu Yib481de92007-09-25 17:54:57 -07006161 goto error;
6162 }
6163
6164 IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
6165 name, ucode_raw->size);
6166
6167 /* Make sure that we got at least our header! */
6168 if (ucode_raw->size < sizeof(*ucode)) {
6169 IWL_ERROR("File size way too small!\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006170 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006171 goto err_release;
6172 }
6173
6174 /* Data from ucode file: header followed by uCode images */
6175 ucode = (void *)ucode_raw->data;
6176
6177 ver = le32_to_cpu(ucode->ver);
6178 inst_size = le32_to_cpu(ucode->inst_size);
6179 data_size = le32_to_cpu(ucode->data_size);
6180 init_size = le32_to_cpu(ucode->init_size);
6181 init_data_size = le32_to_cpu(ucode->init_data_size);
6182 boot_size = le32_to_cpu(ucode->boot_size);
6183
6184 IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
6185 IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
6186 inst_size);
6187 IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
6188 data_size);
6189 IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
6190 init_size);
6191 IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
6192 init_data_size);
6193 IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
6194 boot_size);
6195
6196 /* Verify size of file vs. image size info in file's header */
6197 if (ucode_raw->size < sizeof(*ucode) +
6198 inst_size + data_size + init_size +
6199 init_data_size + boot_size) {
6200
6201 IWL_DEBUG_INFO("uCode file size %d too small\n",
6202 (int)ucode_raw->size);
Tomas Winkler90e759d2007-11-29 11:09:41 +08006203 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006204 goto err_release;
6205 }
6206
6207 /* Verify that uCode images will fit in card's SRAM */
6208 if (inst_size > IWL_MAX_INST_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08006209 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
6210 inst_size);
6211 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006212 goto err_release;
6213 }
6214
6215 if (data_size > IWL_MAX_DATA_SIZE) {
Tomas Winkler90e759d2007-11-29 11:09:41 +08006216 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
6217 data_size);
6218 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006219 goto err_release;
6220 }
6221 if (init_size > IWL_MAX_INST_SIZE) {
6222 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006223 ("uCode init instr len %d too large to fit in\n",
6224 init_size);
6225 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006226 goto err_release;
6227 }
6228 if (init_data_size > IWL_MAX_DATA_SIZE) {
6229 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006230 ("uCode init data len %d too large to fit in\n",
6231 init_data_size);
6232 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006233 goto err_release;
6234 }
6235 if (boot_size > IWL_MAX_BSM_SIZE) {
6236 IWL_DEBUG_INFO
Tomas Winkler90e759d2007-11-29 11:09:41 +08006237 ("uCode boot instr len %d too large to fit in\n",
6238 boot_size);
6239 ret = -EINVAL;
Zhu Yib481de92007-09-25 17:54:57 -07006240 goto err_release;
6241 }
6242
6243 /* Allocate ucode buffers for card's bus-master loading ... */
6244
6245 /* Runtime instructions and 2 copies of data:
6246 * 1) unmodified from disk
6247 * 2) backup cache for save/restore during power-downs */
6248 priv->ucode_code.len = inst_size;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006249 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
Zhu Yib481de92007-09-25 17:54:57 -07006250
6251 priv->ucode_data.len = data_size;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006252 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
Zhu Yib481de92007-09-25 17:54:57 -07006253
6254 priv->ucode_data_backup.len = data_size;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006255 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
Zhu Yib481de92007-09-25 17:54:57 -07006256
6257 /* Initialization instructions and data */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006258 if (init_size && init_data_size) {
6259 priv->ucode_init.len = init_size;
6260 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
Zhu Yib481de92007-09-25 17:54:57 -07006261
Tomas Winkler90e759d2007-11-29 11:09:41 +08006262 priv->ucode_init_data.len = init_data_size;
6263 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
6264
6265 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
6266 goto err_pci_alloc;
6267 }
Zhu Yib481de92007-09-25 17:54:57 -07006268
6269 /* Bootstrap (instructions only, no data) */
Tomas Winkler90e759d2007-11-29 11:09:41 +08006270 if (boot_size) {
6271 priv->ucode_boot.len = boot_size;
6272 iwl4965_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
Zhu Yib481de92007-09-25 17:54:57 -07006273
Tomas Winkler90e759d2007-11-29 11:09:41 +08006274 if (!priv->ucode_boot.v_addr)
6275 goto err_pci_alloc;
6276 }
Zhu Yib481de92007-09-25 17:54:57 -07006277
6278 /* Copy images into buffers for card's bus-master reads ... */
6279
6280 /* Runtime instructions (first block of data in file) */
6281 src = &ucode->data[0];
6282 len = priv->ucode_code.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006283 IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006284 memcpy(priv->ucode_code.v_addr, src, len);
6285 IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
6286 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
6287
6288 /* Runtime data (2nd block)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006289 * NOTE: Copy into backup buffer will be done in iwl4965_up() */
Zhu Yib481de92007-09-25 17:54:57 -07006290 src = &ucode->data[inst_size];
6291 len = priv->ucode_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006292 IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006293 memcpy(priv->ucode_data.v_addr, src, len);
6294 memcpy(priv->ucode_data_backup.v_addr, src, len);
6295
6296 /* Initialization instructions (3rd block) */
6297 if (init_size) {
6298 src = &ucode->data[inst_size + data_size];
6299 len = priv->ucode_init.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006300 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
6301 len);
Zhu Yib481de92007-09-25 17:54:57 -07006302 memcpy(priv->ucode_init.v_addr, src, len);
6303 }
6304
6305 /* Initialization data (4th block) */
6306 if (init_data_size) {
6307 src = &ucode->data[inst_size + data_size + init_size];
6308 len = priv->ucode_init_data.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006309 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
6310 len);
Zhu Yib481de92007-09-25 17:54:57 -07006311 memcpy(priv->ucode_init_data.v_addr, src, len);
6312 }
6313
6314 /* Bootstrap instructions (5th block) */
6315 src = &ucode->data[inst_size + data_size + init_size + init_data_size];
6316 len = priv->ucode_boot.len;
Tomas Winkler90e759d2007-11-29 11:09:41 +08006317 IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
Zhu Yib481de92007-09-25 17:54:57 -07006318 memcpy(priv->ucode_boot.v_addr, src, len);
6319
6320 /* We have our copies now, allow OS release its copies */
6321 release_firmware(ucode_raw);
6322 return 0;
6323
6324 err_pci_alloc:
6325 IWL_ERROR("failed to allocate pci memory\n");
Tomas Winkler90e759d2007-11-29 11:09:41 +08006326 ret = -ENOMEM;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006327 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006328
6329 err_release:
6330 release_firmware(ucode_raw);
6331
6332 error:
Tomas Winkler90e759d2007-11-29 11:09:41 +08006333 return ret;
Zhu Yib481de92007-09-25 17:54:57 -07006334}
6335
6336
6337/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006338 * iwl4965_set_ucode_ptrs - Set uCode address location
Zhu Yib481de92007-09-25 17:54:57 -07006339 *
6340 * Tell initialization uCode where to find runtime uCode.
6341 *
6342 * BSM registers initially contain pointers to initialization uCode.
6343 * We need to replace them to load runtime uCode inst and data,
6344 * and to save runtime data when powering down.
6345 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006346static int iwl4965_set_ucode_ptrs(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006347{
6348 dma_addr_t pinst;
6349 dma_addr_t pdata;
6350 int rc = 0;
6351 unsigned long flags;
6352
6353 /* bits 35:4 for 4965 */
6354 pinst = priv->ucode_code.p_addr >> 4;
6355 pdata = priv->ucode_data_backup.p_addr >> 4;
6356
6357 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006358 rc = iwl4965_grab_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006359 if (rc) {
6360 spin_unlock_irqrestore(&priv->lock, flags);
6361 return rc;
6362 }
6363
6364 /* Tell bootstrap uCode where to find image to load */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006365 iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
6366 iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
6367 iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006368 priv->ucode_data.len);
6369
6370 /* Inst bytecount must be last to set up, bit 31 signals uCode
6371 * that all new ptr/size info is in place */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006372 iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006373 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
6374
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006375 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006376
6377 spin_unlock_irqrestore(&priv->lock, flags);
6378
6379 IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
6380
6381 return rc;
6382}
6383
6384/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006385 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006386 *
6387 * Called after REPLY_ALIVE notification received from "initialize" uCode.
6388 *
6389 * The 4965 "initialize" ALIVE reply contains calibration data for:
6390 * Voltage, temperature, and MIMO tx gain correction, now stored in priv
6391 * (3945 does not contain this data).
6392 *
6393 * Tell "initialize" uCode to go ahead and load the runtime uCode.
6394*/
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006395static void iwl4965_init_alive_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006396{
6397 /* Check alive response for "valid" sign from uCode */
6398 if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
6399 /* We had an error bringing up the hardware, so take it
6400 * all the way back down so we can try again */
6401 IWL_DEBUG_INFO("Initialize Alive failed.\n");
6402 goto restart;
6403 }
6404
6405 /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
6406 * This is a paranoid check, because we would not have gotten the
6407 * "initialize" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006408 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006409 /* Runtime instruction load was bad;
6410 * take it all the way back down so we can try again */
6411 IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
6412 goto restart;
6413 }
6414
6415 /* Calculate temperature */
6416 priv->temperature = iwl4965_get_temperature(priv);
6417
6418 /* Send pointers to protocol/runtime uCode image ... init code will
6419 * load and launch runtime uCode, which will send us another "Alive"
6420 * notification. */
6421 IWL_DEBUG_INFO("Initialization Alive received.\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006422 if (iwl4965_set_ucode_ptrs(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006423 /* Runtime instruction load won't happen;
6424 * take it all the way back down so we can try again */
6425 IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
6426 goto restart;
6427 }
6428 return;
6429
6430 restart:
6431 queue_work(priv->workqueue, &priv->restart);
6432}
6433
6434
6435/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006436 * iwl4965_alive_start - called after REPLY_ALIVE notification received
Zhu Yib481de92007-09-25 17:54:57 -07006437 * from protocol/runtime uCode (initialization uCode's
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006438 * Alive gets handled by iwl4965_init_alive_start()).
Zhu Yib481de92007-09-25 17:54:57 -07006439 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006440static void iwl4965_alive_start(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006441{
6442 int rc = 0;
6443
6444 IWL_DEBUG_INFO("Runtime Alive received.\n");
6445
6446 if (priv->card_alive.is_valid != UCODE_VALID_OK) {
6447 /* We had an error bringing up the hardware, so take it
6448 * all the way back down so we can try again */
6449 IWL_DEBUG_INFO("Alive failed.\n");
6450 goto restart;
6451 }
6452
6453 /* Initialize uCode has loaded Runtime uCode ... verify inst image.
6454 * This is a paranoid check, because we would not have gotten the
6455 * "runtime" alive if code weren't properly loaded. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006456 if (iwl4965_verify_ucode(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006457 /* Runtime instruction load was bad;
6458 * take it all the way back down so we can try again */
6459 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
6460 goto restart;
6461 }
6462
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006463 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006464
6465 rc = iwl4965_alive_notify(priv);
6466 if (rc) {
6467 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
6468 rc);
6469 goto restart;
6470 }
6471
Ben Cahill9fbab512007-11-29 11:09:47 +08006472 /* After the ALIVE response, we can send host commands to 4965 uCode */
Zhu Yib481de92007-09-25 17:54:57 -07006473 set_bit(STATUS_ALIVE, &priv->status);
6474
6475 /* Clear out the uCode error bit if it is set */
6476 clear_bit(STATUS_FW_ERROR, &priv->status);
6477
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006478 rc = iwl4965_init_channel_map(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006479 if (rc) {
6480 IWL_ERROR("initializing regulatory failed: %d\n", rc);
6481 return;
6482 }
6483
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006484 iwl4965_init_geos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006485
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006486 if (iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07006487 return;
6488
6489 if (!priv->mac80211_registered) {
6490 /* Unlock so any user space entry points can call back into
6491 * the driver without a deadlock... */
6492 mutex_unlock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006493 iwl4965_rate_control_register(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006494 rc = ieee80211_register_hw(priv->hw);
6495 priv->hw->conf.beacon_int = 100;
6496 mutex_lock(&priv->mutex);
6497
6498 if (rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006499 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07006500 IWL_ERROR("Failed to register network "
6501 "device (error %d)\n", rc);
6502 return;
6503 }
6504
6505 priv->mac80211_registered = 1;
6506
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006507 iwl4965_reset_channel_flag(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006508 } else
6509 ieee80211_start_queues(priv->hw);
6510
6511 priv->active_rate = priv->rates_mask;
6512 priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
6513
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006514 iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
Zhu Yib481de92007-09-25 17:54:57 -07006515
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006516 if (iwl4965_is_associated(priv)) {
6517 struct iwl4965_rxon_cmd *active_rxon =
6518 (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
Zhu Yib481de92007-09-25 17:54:57 -07006519
6520 memcpy(&priv->staging_rxon, &priv->active_rxon,
6521 sizeof(priv->staging_rxon));
6522 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
6523 } else {
6524 /* Initialize our rx_config data */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006525 iwl4965_connection_init_rx_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006526 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
6527 }
6528
Ben Cahill9fbab512007-11-29 11:09:47 +08006529 /* Configure Bluetooth device coexistence support */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006530 iwl4965_send_bt_config(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006531
6532 /* Configure the adapter for unassociated operation */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006533 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006534
6535 /* At this point, the NIC is initialized and operational */
6536 priv->notif_missed_beacons = 0;
6537 set_bit(STATUS_READY, &priv->status);
6538
6539 iwl4965_rf_kill_ct_config(priv);
6540 IWL_DEBUG_INFO("ALIVE processing complete.\n");
6541
6542 if (priv->error_recovering)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006543 iwl4965_error_recovery(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006544
6545 return;
6546
6547 restart:
6548 queue_work(priv->workqueue, &priv->restart);
6549}
6550
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006551static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv);
Zhu Yib481de92007-09-25 17:54:57 -07006552
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006553static void __iwl4965_down(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006554{
6555 unsigned long flags;
6556 int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
6557 struct ieee80211_conf *conf = NULL;
6558
6559 IWL_DEBUG_INFO(DRV_NAME " is going down\n");
6560
6561 conf = ieee80211_get_hw_conf(priv->hw);
6562
6563 if (!exit_pending)
6564 set_bit(STATUS_EXIT_PENDING, &priv->status);
6565
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006566 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006567
6568 /* Unblock any waiting calls */
6569 wake_up_interruptible_all(&priv->wait_command_queue);
6570
Zhu Yib481de92007-09-25 17:54:57 -07006571 /* Wipe out the EXIT_PENDING status bit if we are not actually
6572 * exiting the module */
6573 if (!exit_pending)
6574 clear_bit(STATUS_EXIT_PENDING, &priv->status);
6575
6576 /* stop and reset the on-board processor */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006577 iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
Zhu Yib481de92007-09-25 17:54:57 -07006578
6579 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006580 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006581
6582 if (priv->mac80211_registered)
6583 ieee80211_stop_queues(priv->hw);
6584
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006585 /* If we have not previously called iwl4965_init() then
Zhu Yib481de92007-09-25 17:54:57 -07006586 * clear all bits but the RF Kill and SUSPEND bits and return */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006587 if (!iwl4965_is_init(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006588 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6589 STATUS_RF_KILL_HW |
6590 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6591 STATUS_RF_KILL_SW |
6592 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6593 STATUS_IN_SUSPEND;
6594 goto exit;
6595 }
6596
6597 /* ...otherwise clear out all the status bits but the RF Kill and
6598 * SUSPEND bits and continue taking the NIC down. */
6599 priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
6600 STATUS_RF_KILL_HW |
6601 test_bit(STATUS_RF_KILL_SW, &priv->status) <<
6602 STATUS_RF_KILL_SW |
6603 test_bit(STATUS_IN_SUSPEND, &priv->status) <<
6604 STATUS_IN_SUSPEND |
6605 test_bit(STATUS_FW_ERROR, &priv->status) <<
6606 STATUS_FW_ERROR;
6607
6608 spin_lock_irqsave(&priv->lock, flags);
Ben Cahill9fbab512007-11-29 11:09:47 +08006609 iwl4965_clear_bit(priv, CSR_GP_CNTRL,
6610 CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07006611 spin_unlock_irqrestore(&priv->lock, flags);
6612
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006613 iwl4965_hw_txq_ctx_stop(priv);
6614 iwl4965_hw_rxq_stop(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006615
6616 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006617 if (!iwl4965_grab_nic_access(priv)) {
6618 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Zhu Yib481de92007-09-25 17:54:57 -07006619 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006620 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006621 }
6622 spin_unlock_irqrestore(&priv->lock, flags);
6623
6624 udelay(5);
6625
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006626 iwl4965_hw_nic_stop_master(priv);
6627 iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
6628 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006629
6630 exit:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006631 memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
Zhu Yib481de92007-09-25 17:54:57 -07006632
6633 if (priv->ibss_beacon)
6634 dev_kfree_skb(priv->ibss_beacon);
6635 priv->ibss_beacon = NULL;
6636
6637 /* clear out any free frames */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006638 iwl4965_clear_free_frames(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006639}
6640
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006641static void iwl4965_down(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006642{
6643 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006644 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006645 mutex_unlock(&priv->mutex);
Zhu Yib24d22b2007-12-19 13:59:52 +08006646
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006647 iwl4965_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006648}
6649
6650#define MAX_HW_RESTARTS 5
6651
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006652static int __iwl4965_up(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07006653{
Joe Perches0795af52007-10-03 17:59:30 -07006654 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07006655 int rc, i;
6656 u32 hw_rf_kill = 0;
6657
6658 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6659 IWL_WARNING("Exit pending; will not bring the NIC up\n");
6660 return -EIO;
6661 }
6662
6663 if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
6664 IWL_WARNING("Radio disabled by SW RF kill (module "
6665 "parameter)\n");
6666 return 0;
6667 }
6668
Reinette Chatrea781cf92008-01-21 10:08:31 -08006669 if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
6670 IWL_ERROR("ucode not available for device bringup\n");
6671 return -EIO;
6672 }
6673
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006674 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07006675
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006676 rc = iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006677 if (rc) {
6678 IWL_ERROR("Unable to int nic\n");
6679 return rc;
6680 }
6681
6682 /* make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006683 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6684 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07006685 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
6686
6687 /* clear (again), then enable host interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006688 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
6689 iwl4965_enable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006690
6691 /* really make sure rfkill handshake bits are cleared */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006692 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
6693 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07006694
6695 /* Copy original ucode data image from disk into backup cache.
6696 * This will be used to initialize the on-board processor's
6697 * data SRAM for a clean start when the runtime program first loads. */
6698 memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
6699 priv->ucode_data.len);
6700
6701 /* If platform's RF_KILL switch is set to KILL,
6702 * wait for BIT_INT_RF_KILL interrupt before loading uCode
6703 * and getting things started */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006704 if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
Zhu Yib481de92007-09-25 17:54:57 -07006705 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
6706 hw_rf_kill = 1;
6707
6708 if (test_bit(STATUS_RF_KILL_HW, &priv->status) || hw_rf_kill) {
6709 IWL_WARNING("Radio disabled by HW RF Kill switch\n");
6710 return 0;
6711 }
6712
6713 for (i = 0; i < MAX_HW_RESTARTS; i++) {
6714
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006715 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006716
6717 /* load bootstrap state machine,
6718 * load bootstrap program into processor's memory,
6719 * prepare to load the "initialize" uCode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006720 rc = iwl4965_load_bsm(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006721
6722 if (rc) {
6723 IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
6724 continue;
6725 }
6726
6727 /* start card; "initialize" will load runtime ucode */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006728 iwl4965_nic_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006729
Ben Cahill9fbab512007-11-29 11:09:47 +08006730 /* MAC Address location in EEPROM is same for 3945/4965 */
Zhu Yib481de92007-09-25 17:54:57 -07006731 get_eeprom_mac(priv, priv->mac_addr);
Joe Perches0795af52007-10-03 17:59:30 -07006732 IWL_DEBUG_INFO("MAC address: %s\n",
6733 print_mac(mac, priv->mac_addr));
Zhu Yib481de92007-09-25 17:54:57 -07006734
6735 SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
6736
6737 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
6738
6739 return 0;
6740 }
6741
6742 set_bit(STATUS_EXIT_PENDING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006743 __iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006744
6745 /* tried to restart and config the device for as long as our
6746 * patience could withstand */
6747 IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
6748 return -EIO;
6749}
6750
6751
6752/*****************************************************************************
6753 *
6754 * Workqueue callbacks
6755 *
6756 *****************************************************************************/
6757
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006758static void iwl4965_bg_init_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006759{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006760 struct iwl4965_priv *priv =
6761 container_of(data, struct iwl4965_priv, init_alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006762
6763 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6764 return;
6765
6766 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006767 iwl4965_init_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006768 mutex_unlock(&priv->mutex);
6769}
6770
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006771static void iwl4965_bg_alive_start(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006772{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006773 struct iwl4965_priv *priv =
6774 container_of(data, struct iwl4965_priv, alive_start.work);
Zhu Yib481de92007-09-25 17:54:57 -07006775
6776 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6777 return;
6778
6779 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006780 iwl4965_alive_start(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006781 mutex_unlock(&priv->mutex);
6782}
6783
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006784static void iwl4965_bg_rf_kill(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07006785{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006786 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, rf_kill);
Zhu Yib481de92007-09-25 17:54:57 -07006787
6788 wake_up_interruptible(&priv->wait_command_queue);
6789
6790 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6791 return;
6792
6793 mutex_lock(&priv->mutex);
6794
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006795 if (!iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006796 IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
6797 "HW and/or SW RF Kill no longer active, restarting "
6798 "device\n");
6799 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
6800 queue_work(priv->workqueue, &priv->restart);
6801 } else {
6802
6803 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
6804 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
6805 "disabled by SW switch\n");
6806 else
6807 IWL_WARNING("Radio Frequency Kill Switch is On:\n"
6808 "Kill switch must be turned off for "
6809 "wireless networking to work.\n");
6810 }
6811 mutex_unlock(&priv->mutex);
6812}
6813
6814#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
6815
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006816static void iwl4965_bg_scan_check(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006817{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006818 struct iwl4965_priv *priv =
6819 container_of(data, struct iwl4965_priv, scan_check.work);
Zhu Yib481de92007-09-25 17:54:57 -07006820
6821 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
6822 return;
6823
6824 mutex_lock(&priv->mutex);
6825 if (test_bit(STATUS_SCANNING, &priv->status) ||
6826 test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6827 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
6828 "Scan completion watchdog resetting adapter (%dms)\n",
6829 jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
mabbas052c4b92007-10-25 17:15:43 +08006830
Zhu Yib481de92007-09-25 17:54:57 -07006831 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006832 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07006833 }
6834 mutex_unlock(&priv->mutex);
6835}
6836
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006837static void iwl4965_bg_request_scan(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07006838{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006839 struct iwl4965_priv *priv =
6840 container_of(data, struct iwl4965_priv, request_scan);
6841 struct iwl4965_host_cmd cmd = {
Zhu Yib481de92007-09-25 17:54:57 -07006842 .id = REPLY_SCAN_CMD,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006843 .len = sizeof(struct iwl4965_scan_cmd),
Zhu Yib481de92007-09-25 17:54:57 -07006844 .meta.flags = CMD_SIZE_HUGE,
6845 };
6846 int rc = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006847 struct iwl4965_scan_cmd *scan;
Zhu Yib481de92007-09-25 17:54:57 -07006848 struct ieee80211_conf *conf = NULL;
6849 u8 direct_mask;
6850 int phymode;
6851
6852 conf = ieee80211_get_hw_conf(priv->hw);
6853
6854 mutex_lock(&priv->mutex);
6855
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006856 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006857 IWL_WARNING("request scan called when driver not ready.\n");
6858 goto done;
6859 }
6860
6861 /* Make sure the scan wasn't cancelled before this queued work
6862 * was given the chance to run... */
6863 if (!test_bit(STATUS_SCANNING, &priv->status))
6864 goto done;
6865
6866 /* This should never be called or scheduled if there is currently
6867 * a scan active in the hardware. */
6868 if (test_bit(STATUS_SCAN_HW, &priv->status)) {
6869 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
6870 "Ignoring second request.\n");
6871 rc = -EIO;
6872 goto done;
6873 }
6874
6875 if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
6876 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
6877 goto done;
6878 }
6879
6880 if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
6881 IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
6882 goto done;
6883 }
6884
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006885 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006886 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
6887 goto done;
6888 }
6889
6890 if (!test_bit(STATUS_READY, &priv->status)) {
6891 IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
6892 goto done;
6893 }
6894
6895 if (!priv->scan_bands) {
6896 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
6897 goto done;
6898 }
6899
6900 if (!priv->scan) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006901 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
Zhu Yib481de92007-09-25 17:54:57 -07006902 IWL_MAX_SCAN_SIZE, GFP_KERNEL);
6903 if (!priv->scan) {
6904 rc = -ENOMEM;
6905 goto done;
6906 }
6907 }
6908 scan = priv->scan;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006909 memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
Zhu Yib481de92007-09-25 17:54:57 -07006910
6911 scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
6912 scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
6913
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006914 if (iwl4965_is_associated(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07006915 u16 interval = 0;
6916 u32 extra;
6917 u32 suspend_time = 100;
6918 u32 scan_suspend_time = 100;
6919 unsigned long flags;
6920
6921 IWL_DEBUG_INFO("Scanning while associated...\n");
6922
6923 spin_lock_irqsave(&priv->lock, flags);
6924 interval = priv->beacon_int;
6925 spin_unlock_irqrestore(&priv->lock, flags);
6926
6927 scan->suspend_time = 0;
mabbas052c4b92007-10-25 17:15:43 +08006928 scan->max_out_time = cpu_to_le32(200 * 1024);
Zhu Yib481de92007-09-25 17:54:57 -07006929 if (!interval)
6930 interval = suspend_time;
6931
6932 extra = (suspend_time / interval) << 22;
6933 scan_suspend_time = (extra |
6934 ((suspend_time % interval) * 1024));
6935 scan->suspend_time = cpu_to_le32(scan_suspend_time);
6936 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
6937 scan_suspend_time, interval);
6938 }
6939
6940 /* We should add the ability for user to lock to PASSIVE ONLY */
6941 if (priv->one_direct_scan) {
6942 IWL_DEBUG_SCAN
6943 ("Kicking off one direct scan for '%s'\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006944 iwl4965_escape_essid(priv->direct_ssid,
Zhu Yib481de92007-09-25 17:54:57 -07006945 priv->direct_ssid_len));
6946 scan->direct_scan[0].id = WLAN_EID_SSID;
6947 scan->direct_scan[0].len = priv->direct_ssid_len;
6948 memcpy(scan->direct_scan[0].ssid,
6949 priv->direct_ssid, priv->direct_ssid_len);
6950 direct_mask = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006951 } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
Zhu Yib481de92007-09-25 17:54:57 -07006952 scan->direct_scan[0].id = WLAN_EID_SSID;
6953 scan->direct_scan[0].len = priv->essid_len;
6954 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
6955 direct_mask = 1;
6956 } else
6957 direct_mask = 0;
6958
6959 /* We don't build a direct scan probe request; the uCode will do
6960 * that based on the direct_mask added to each channel entry */
6961 scan->tx_cmd.len = cpu_to_le16(
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006962 iwl4965_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
Zhu Yib481de92007-09-25 17:54:57 -07006963 IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
6964 scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
6965 scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
6966 scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
6967
6968 /* flags + rate selection */
6969
6970 scan->tx_cmd.tx_flags |= cpu_to_le32(0x200);
6971
6972 switch (priv->scan_bands) {
6973 case 2:
6974 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
6975 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006976 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006977 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
6978
6979 scan->good_CRC_th = 0;
6980 phymode = MODE_IEEE80211G;
6981 break;
6982
6983 case 1:
6984 scan->tx_cmd.rate_n_flags =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08006985 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
Zhu Yib481de92007-09-25 17:54:57 -07006986 RATE_MCS_ANT_B_MSK);
6987 scan->good_CRC_th = IWL_GOOD_CRC_TH;
6988 phymode = MODE_IEEE80211A;
6989 break;
6990
6991 default:
6992 IWL_WARNING("Invalid scan band count\n");
6993 goto done;
6994 }
6995
6996 /* select Rx chains */
6997
6998 /* Force use of chains B and C (0x6) for scan Rx.
6999 * Avoid A (0x1) because of its off-channel reception on A-band.
7000 * MIMO is not used here, but value is required to make uCode happy. */
7001 scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
7002 cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
7003 (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
7004 (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
7005
7006 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
7007 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
7008
7009 if (direct_mask)
7010 IWL_DEBUG_SCAN
7011 ("Initiating direct scan for %s.\n",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007012 iwl4965_escape_essid(priv->essid, priv->essid_len));
Zhu Yib481de92007-09-25 17:54:57 -07007013 else
7014 IWL_DEBUG_SCAN("Initiating indirect scan.\n");
7015
7016 scan->channel_count =
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007017 iwl4965_get_channels_for_scan(
Zhu Yib481de92007-09-25 17:54:57 -07007018 priv, phymode, 1, /* active */
7019 direct_mask,
7020 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
7021
7022 cmd.len += le16_to_cpu(scan->tx_cmd.len) +
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007023 scan->channel_count * sizeof(struct iwl4965_scan_channel);
Zhu Yib481de92007-09-25 17:54:57 -07007024 cmd.data = scan;
7025 scan->len = cpu_to_le16(cmd.len);
7026
7027 set_bit(STATUS_SCAN_HW, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007028 rc = iwl4965_send_cmd_sync(priv, &cmd);
Zhu Yib481de92007-09-25 17:54:57 -07007029 if (rc)
7030 goto done;
7031
7032 queue_delayed_work(priv->workqueue, &priv->scan_check,
7033 IWL_SCAN_CHECK_WATCHDOG);
7034
7035 mutex_unlock(&priv->mutex);
7036 return;
7037
7038 done:
Ian Schram01ebd062007-10-25 17:15:22 +08007039 /* inform mac80211 scan aborted */
Zhu Yib481de92007-09-25 17:54:57 -07007040 queue_work(priv->workqueue, &priv->scan_completed);
7041 mutex_unlock(&priv->mutex);
7042}
7043
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007044static void iwl4965_bg_up(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07007045{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007046 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, up);
Zhu Yib481de92007-09-25 17:54:57 -07007047
7048 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7049 return;
7050
7051 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007052 __iwl4965_up(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007053 mutex_unlock(&priv->mutex);
7054}
7055
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007056static void iwl4965_bg_restart(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07007057{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007058 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, restart);
Zhu Yib481de92007-09-25 17:54:57 -07007059
7060 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7061 return;
7062
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007063 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007064 queue_work(priv->workqueue, &priv->up);
7065}
7066
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007067static void iwl4965_bg_rx_replenish(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07007068{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007069 struct iwl4965_priv *priv =
7070 container_of(data, struct iwl4965_priv, rx_replenish);
Zhu Yib481de92007-09-25 17:54:57 -07007071
7072 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7073 return;
7074
7075 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007076 iwl4965_rx_replenish(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007077 mutex_unlock(&priv->mutex);
7078}
7079
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007080static void iwl4965_bg_post_associate(struct work_struct *data)
Zhu Yib481de92007-09-25 17:54:57 -07007081{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007082 struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv,
Zhu Yib481de92007-09-25 17:54:57 -07007083 post_associate.work);
7084
7085 int rc = 0;
7086 struct ieee80211_conf *conf = NULL;
Joe Perches0795af52007-10-03 17:59:30 -07007087 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007088
7089 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7090 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
7091 return;
7092 }
7093
Joe Perches0795af52007-10-03 17:59:30 -07007094 IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
7095 priv->assoc_id,
7096 print_mac(mac, priv->active_rxon.bssid_addr));
Zhu Yib481de92007-09-25 17:54:57 -07007097
7098
7099 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7100 return;
7101
7102 mutex_lock(&priv->mutex);
7103
Mohamed Abbas948c1712007-10-25 17:15:45 +08007104 if (!priv->interface_id || !priv->is_open) {
7105 mutex_unlock(&priv->mutex);
7106 return;
7107 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007108 iwl4965_scan_cancel_timeout(priv, 200);
mabbas052c4b92007-10-25 17:15:43 +08007109
Zhu Yib481de92007-09-25 17:54:57 -07007110 conf = ieee80211_get_hw_conf(priv->hw);
7111
7112 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007113 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007114
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007115 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7116 iwl4965_setup_rxon_timing(priv);
7117 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07007118 sizeof(priv->rxon_timing), &priv->rxon_timing);
7119 if (rc)
7120 IWL_WARNING("REPLY_RXON_TIMING failed - "
7121 "Attempting to continue.\n");
7122
7123 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
7124
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007125#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007126 if (priv->is_ht_enabled && priv->current_assoc_ht.is_ht)
7127 iwl4965_set_rxon_ht(priv, &priv->current_assoc_ht);
7128 else {
7129 priv->active_rate_ht[0] = 0;
7130 priv->active_rate_ht[1] = 0;
7131 priv->current_channel_width = IWL_CHANNEL_WIDTH_20MHZ;
7132 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007133#endif /* CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07007134 iwl4965_set_rxon_chain(priv);
7135 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7136
7137 IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
7138 priv->assoc_id, priv->beacon_int);
7139
7140 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7141 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7142 else
7143 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7144
7145 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7146 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
7147 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
7148 else
7149 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7150
7151 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7152 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
7153
7154 }
7155
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007156 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007157
7158 switch (priv->iw_mode) {
7159 case IEEE80211_IF_TYPE_STA:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007160 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07007161 break;
7162
7163 case IEEE80211_IF_TYPE_IBSS:
7164
7165 /* clear out the station table */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007166 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007167
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007168 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
7169 iwl4965_rxon_add_station(priv, priv->bssid, 0);
7170 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
7171 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007172
7173 break;
7174
7175 default:
7176 IWL_ERROR("%s Should not be called in %d mode\n",
7177 __FUNCTION__, priv->iw_mode);
7178 break;
7179 }
7180
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007181 iwl4965_sequence_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007182
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007183#ifdef CONFIG_IWL4965_SENSITIVITY
Zhu Yib481de92007-09-25 17:54:57 -07007184 /* Enable Rx differential gain and sensitivity calibrations */
7185 iwl4965_chain_noise_reset(priv);
7186 priv->start_calib = 1;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007187#endif /* CONFIG_IWL4965_SENSITIVITY */
Zhu Yib481de92007-09-25 17:54:57 -07007188
7189 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7190 priv->assoc_station_added = 1;
7191
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007192#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007193 iwl4965_activate_qos(priv, 0);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007194#endif /* CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07007195 mutex_unlock(&priv->mutex);
7196}
7197
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007198static void iwl4965_bg_abort_scan(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07007199{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007200 struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, abort_scan);
Zhu Yib481de92007-09-25 17:54:57 -07007201
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007202 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007203 return;
7204
7205 mutex_lock(&priv->mutex);
7206
7207 set_bit(STATUS_SCAN_ABORTING, &priv->status);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007208 iwl4965_send_scan_abort(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007209
7210 mutex_unlock(&priv->mutex);
7211}
7212
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007213static void iwl4965_bg_scan_completed(struct work_struct *work)
Zhu Yib481de92007-09-25 17:54:57 -07007214{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007215 struct iwl4965_priv *priv =
7216 container_of(work, struct iwl4965_priv, scan_completed);
Zhu Yib481de92007-09-25 17:54:57 -07007217
7218 IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
7219
7220 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
7221 return;
7222
7223 ieee80211_scan_completed(priv->hw);
7224
7225 /* Since setting the TXPOWER may have been deferred while
7226 * performing the scan, fire one off */
7227 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007228 iwl4965_hw_reg_send_txpower(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007229 mutex_unlock(&priv->mutex);
7230}
7231
7232/*****************************************************************************
7233 *
7234 * mac80211 entry point functions
7235 *
7236 *****************************************************************************/
7237
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007238static int iwl4965_mac_start(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007239{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007240 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007241
7242 IWL_DEBUG_MAC80211("enter\n");
7243
7244 /* we should be verifying the device is ready to be opened */
7245 mutex_lock(&priv->mutex);
7246
7247 priv->is_open = 1;
7248
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007249 if (!iwl4965_is_rfkill(priv))
Zhu Yib481de92007-09-25 17:54:57 -07007250 ieee80211_start_queues(priv->hw);
7251
7252 mutex_unlock(&priv->mutex);
7253 IWL_DEBUG_MAC80211("leave\n");
7254 return 0;
7255}
7256
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007257static void iwl4965_mac_stop(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007258{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007259 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007260
7261 IWL_DEBUG_MAC80211("enter\n");
Mohamed Abbas948c1712007-10-25 17:15:45 +08007262
7263
7264 mutex_lock(&priv->mutex);
7265 /* stop mac, cancel any scan request and clear
7266 * RXON_FILTER_ASSOC_MSK BIT
7267 */
Zhu Yib481de92007-09-25 17:54:57 -07007268 priv->is_open = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007269 iwl4965_scan_cancel_timeout(priv, 100);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007270 cancel_delayed_work(&priv->post_associate);
7271 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007272 iwl4965_commit_rxon(priv);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007273 mutex_unlock(&priv->mutex);
7274
Zhu Yib481de92007-09-25 17:54:57 -07007275 IWL_DEBUG_MAC80211("leave\n");
Zhu Yib481de92007-09-25 17:54:57 -07007276}
7277
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007278static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007279 struct ieee80211_tx_control *ctl)
7280{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007281 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007282
7283 IWL_DEBUG_MAC80211("enter\n");
7284
7285 if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
7286 IWL_DEBUG_MAC80211("leave - monitor\n");
7287 return -1;
7288 }
7289
7290 IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
7291 ctl->tx_rate);
7292
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007293 if (iwl4965_tx_skb(priv, skb, ctl))
Zhu Yib481de92007-09-25 17:54:57 -07007294 dev_kfree_skb_any(skb);
7295
7296 IWL_DEBUG_MAC80211("leave\n");
7297 return 0;
7298}
7299
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007300static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007301 struct ieee80211_if_init_conf *conf)
7302{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007303 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007304 unsigned long flags;
Joe Perches0795af52007-10-03 17:59:30 -07007305 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007306
7307 IWL_DEBUG_MAC80211("enter: id %d, type %d\n", conf->if_id, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007308
7309 if (priv->interface_id) {
7310 IWL_DEBUG_MAC80211("leave - interface_id != 0\n");
7311 return 0;
7312 }
7313
7314 spin_lock_irqsave(&priv->lock, flags);
7315 priv->interface_id = conf->if_id;
7316
7317 spin_unlock_irqrestore(&priv->lock, flags);
7318
7319 mutex_lock(&priv->mutex);
Tomas Winkler864792e2007-11-27 21:00:52 +02007320
7321 if (conf->mac_addr) {
7322 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
7323 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
7324 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007325 iwl4965_set_mode(priv, conf->type);
Zhu Yib481de92007-09-25 17:54:57 -07007326
7327 IWL_DEBUG_MAC80211("leave\n");
7328 mutex_unlock(&priv->mutex);
7329
7330 return 0;
7331}
7332
7333/**
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007334 * iwl4965_mac_config - mac80211 config callback
Zhu Yib481de92007-09-25 17:54:57 -07007335 *
7336 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
7337 * be set inappropriately and the driver currently sets the hardware up to
7338 * use it whenever needed.
7339 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007340static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
Zhu Yib481de92007-09-25 17:54:57 -07007341{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007342 struct iwl4965_priv *priv = hw->priv;
7343 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07007344 unsigned long flags;
7345
7346 mutex_lock(&priv->mutex);
7347 IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
7348
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007349 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007350 IWL_DEBUG_MAC80211("leave - not ready\n");
7351 mutex_unlock(&priv->mutex);
7352 return -EIO;
7353 }
7354
7355 /* TODO: Figure out how to get ieee80211_local->sta_scanning w/ only
Ian Schram01ebd062007-10-25 17:15:22 +08007356 * what is exposed through include/ declarations */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007357 if (unlikely(!iwl4965_param_disable_hw_scan &&
Zhu Yib481de92007-09-25 17:54:57 -07007358 test_bit(STATUS_SCANNING, &priv->status))) {
7359 IWL_DEBUG_MAC80211("leave - scanning\n");
7360 mutex_unlock(&priv->mutex);
7361 return 0;
7362 }
7363
7364 spin_lock_irqsave(&priv->lock, flags);
7365
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007366 ch_info = iwl4965_get_channel_info(priv, conf->phymode, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07007367 if (!is_channel_valid(ch_info)) {
7368 IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
7369 conf->channel, conf->phymode);
7370 IWL_DEBUG_MAC80211("leave - invalid channel\n");
7371 spin_unlock_irqrestore(&priv->lock, flags);
7372 mutex_unlock(&priv->mutex);
7373 return -EINVAL;
7374 }
7375
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007376#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007377 /* if we are switching fron ht to 2.4 clear flags
7378 * from any ht related info since 2.4 does not
7379 * support ht */
7380 if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel)
7381#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7382 && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
7383#endif
7384 )
7385 priv->staging_rxon.flags = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007386#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007388 iwl4965_set_rxon_channel(priv, conf->phymode, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07007389
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007390 iwl4965_set_flags_for_phymode(priv, conf->phymode);
Zhu Yib481de92007-09-25 17:54:57 -07007391
7392 /* The list of supported rates and rate mask can be different
7393 * for each phymode; since the phymode may have changed, reset
7394 * the rate mask to what mac80211 lists */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007395 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007396
7397 spin_unlock_irqrestore(&priv->lock, flags);
7398
7399#ifdef IEEE80211_CONF_CHANNEL_SWITCH
7400 if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007401 iwl4965_hw_channel_switch(priv, conf->channel);
Zhu Yib481de92007-09-25 17:54:57 -07007402 mutex_unlock(&priv->mutex);
7403 return 0;
7404 }
7405#endif
7406
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007407 iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
Zhu Yib481de92007-09-25 17:54:57 -07007408
7409 if (!conf->radio_enabled) {
7410 IWL_DEBUG_MAC80211("leave - radio disabled\n");
7411 mutex_unlock(&priv->mutex);
7412 return 0;
7413 }
7414
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007415 if (iwl4965_is_rfkill(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007416 IWL_DEBUG_MAC80211("leave - RF kill\n");
7417 mutex_unlock(&priv->mutex);
7418 return -EIO;
7419 }
7420
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007421 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007422
7423 if (memcmp(&priv->active_rxon,
7424 &priv->staging_rxon, sizeof(priv->staging_rxon)))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007425 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007426 else
7427 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
7428
7429 IWL_DEBUG_MAC80211("leave\n");
7430
7431 mutex_unlock(&priv->mutex);
7432
7433 return 0;
7434}
7435
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007436static void iwl4965_config_ap(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07007437{
7438 int rc = 0;
7439
7440 if (priv->status & STATUS_EXIT_PENDING)
7441 return;
7442
7443 /* The following should be done only at AP bring up */
7444 if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
7445
7446 /* RXON - unassoc (to set timing command) */
7447 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007448 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007449
7450 /* RXON Timing */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007451 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
7452 iwl4965_setup_rxon_timing(priv);
7453 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
Zhu Yib481de92007-09-25 17:54:57 -07007454 sizeof(priv->rxon_timing), &priv->rxon_timing);
7455 if (rc)
7456 IWL_WARNING("REPLY_RXON_TIMING failed - "
7457 "Attempting to continue.\n");
7458
7459 iwl4965_set_rxon_chain(priv);
7460
7461 /* FIXME: what should be the assoc_id for AP? */
7462 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
7463 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
7464 priv->staging_rxon.flags |=
7465 RXON_FLG_SHORT_PREAMBLE_MSK;
7466 else
7467 priv->staging_rxon.flags &=
7468 ~RXON_FLG_SHORT_PREAMBLE_MSK;
7469
7470 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
7471 if (priv->assoc_capability &
7472 WLAN_CAPABILITY_SHORT_SLOT_TIME)
7473 priv->staging_rxon.flags |=
7474 RXON_FLG_SHORT_SLOT_MSK;
7475 else
7476 priv->staging_rxon.flags &=
7477 ~RXON_FLG_SHORT_SLOT_MSK;
7478
7479 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
7480 priv->staging_rxon.flags &=
7481 ~RXON_FLG_SHORT_SLOT_MSK;
7482 }
7483 /* restore RXON assoc */
7484 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007485 iwl4965_commit_rxon(priv);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007486#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007487 iwl4965_activate_qos(priv, 1);
Zhu Yib481de92007-09-25 17:54:57 -07007488#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007489 iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
Zhu Yie1493de2007-09-27 11:27:32 +08007490 }
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007491 iwl4965_send_beacon_cmd(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007492
7493 /* FIXME - we need to add code here to detect a totally new
7494 * configuration, reset the AP, unassoc, rxon timing, assoc,
7495 * clear sta table, add BCAST sta... */
7496}
7497
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007498static int iwl4965_mac_config_interface(struct ieee80211_hw *hw, int if_id,
Zhu Yib481de92007-09-25 17:54:57 -07007499 struct ieee80211_if_conf *conf)
7500{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007501 struct iwl4965_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007502 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007503 unsigned long flags;
7504 int rc;
7505
7506 if (conf == NULL)
7507 return -EIO;
7508
7509 if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
7510 (!conf->beacon || !conf->ssid_len)) {
7511 IWL_DEBUG_MAC80211
7512 ("Leaving in AP mode because HostAPD is not ready.\n");
7513 return 0;
7514 }
7515
7516 mutex_lock(&priv->mutex);
7517
7518 IWL_DEBUG_MAC80211("enter: interface id %d\n", if_id);
7519 if (conf->bssid)
Joe Perches0795af52007-10-03 17:59:30 -07007520 IWL_DEBUG_MAC80211("bssid: %s\n",
7521 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007522
Johannes Berg4150c572007-09-17 01:29:23 -04007523/*
7524 * very dubious code was here; the probe filtering flag is never set:
7525 *
Zhu Yib481de92007-09-25 17:54:57 -07007526 if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
7527 !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
Johannes Berg4150c572007-09-17 01:29:23 -04007528 */
7529 if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
Zhu Yib481de92007-09-25 17:54:57 -07007530 IWL_DEBUG_MAC80211("leave - scanning\n");
7531 mutex_unlock(&priv->mutex);
7532 return 0;
7533 }
7534
7535 if (priv->interface_id != if_id) {
7536 IWL_DEBUG_MAC80211("leave - interface_id != if_id\n");
7537 mutex_unlock(&priv->mutex);
7538 return 0;
7539 }
7540
7541 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
7542 if (!conf->bssid) {
7543 conf->bssid = priv->mac_addr;
7544 memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
Joe Perches0795af52007-10-03 17:59:30 -07007545 IWL_DEBUG_MAC80211("bssid was set to: %s\n",
7546 print_mac(mac, conf->bssid));
Zhu Yib481de92007-09-25 17:54:57 -07007547 }
7548 if (priv->ibss_beacon)
7549 dev_kfree_skb(priv->ibss_beacon);
7550
7551 priv->ibss_beacon = conf->beacon;
7552 }
7553
7554 if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
7555 !is_multicast_ether_addr(conf->bssid)) {
7556 /* If there is currently a HW scan going on in the background
7557 * then we need to cancel it else the RXON below will fail. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007558 if (iwl4965_scan_cancel_timeout(priv, 100)) {
Zhu Yib481de92007-09-25 17:54:57 -07007559 IWL_WARNING("Aborted scan still in progress "
7560 "after 100ms\n");
7561 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
7562 mutex_unlock(&priv->mutex);
7563 return -EAGAIN;
7564 }
7565 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
7566
7567 /* TODO: Audit driver for usage of these members and see
7568 * if mac80211 deprecates them (priv->bssid looks like it
7569 * shouldn't be there, but I haven't scanned the IBSS code
7570 * to verify) - jpk */
7571 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
7572
7573 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007574 iwl4965_config_ap(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007575 else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007576 rc = iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007577 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007578 iwl4965_rxon_add_station(
Zhu Yib481de92007-09-25 17:54:57 -07007579 priv, priv->active_rxon.bssid_addr, 1);
7580 }
7581
7582 } else {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007583 iwl4965_scan_cancel_timeout(priv, 100);
Zhu Yib481de92007-09-25 17:54:57 -07007584 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007585 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007586 }
7587
7588 spin_lock_irqsave(&priv->lock, flags);
7589 if (!conf->ssid_len)
7590 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7591 else
7592 memcpy(priv->essid, conf->ssid, conf->ssid_len);
7593
7594 priv->essid_len = conf->ssid_len;
7595 spin_unlock_irqrestore(&priv->lock, flags);
7596
7597 IWL_DEBUG_MAC80211("leave\n");
7598 mutex_unlock(&priv->mutex);
7599
7600 return 0;
7601}
7602
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007603static void iwl4965_configure_filter(struct ieee80211_hw *hw,
Johannes Berg4150c572007-09-17 01:29:23 -04007604 unsigned int changed_flags,
7605 unsigned int *total_flags,
7606 int mc_count, struct dev_addr_list *mc_list)
7607{
7608 /*
7609 * XXX: dummy
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007610 * see also iwl4965_connection_init_rx_config
Johannes Berg4150c572007-09-17 01:29:23 -04007611 */
7612 *total_flags = 0;
7613}
7614
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007615static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007616 struct ieee80211_if_init_conf *conf)
7617{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007618 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007619
7620 IWL_DEBUG_MAC80211("enter\n");
7621
7622 mutex_lock(&priv->mutex);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007623
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007624 iwl4965_scan_cancel_timeout(priv, 100);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007625 cancel_delayed_work(&priv->post_associate);
7626 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007627 iwl4965_commit_rxon(priv);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007628
Zhu Yib481de92007-09-25 17:54:57 -07007629 if (priv->interface_id == conf->if_id) {
7630 priv->interface_id = 0;
7631 memset(priv->bssid, 0, ETH_ALEN);
7632 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
7633 priv->essid_len = 0;
7634 }
7635 mutex_unlock(&priv->mutex);
7636
7637 IWL_DEBUG_MAC80211("leave\n");
7638
7639}
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007640static void iwl4965_mac_erp_ie_changed(struct ieee80211_hw *hw,
Tomas Winkler220173b2007-10-16 00:50:25 +02007641 u8 changes, int cts_protection, int preamble)
7642{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007643 struct iwl4965_priv *priv = hw->priv;
Tomas Winkler220173b2007-10-16 00:50:25 +02007644
7645 if (changes & IEEE80211_ERP_CHANGE_PREAMBLE) {
7646 if (preamble == WLAN_ERP_PREAMBLE_SHORT)
7647 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
7648 else
7649 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
7650 }
7651
7652 if (changes & IEEE80211_ERP_CHANGE_PROTECTION) {
Zhu Yi797a54c2007-11-12 11:37:43 +08007653 if (cts_protection && (priv->phymode != MODE_IEEE80211A))
Tomas Winkler220173b2007-10-16 00:50:25 +02007654 priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
7655 else
7656 priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
7657 }
7658
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007659 if (iwl4965_is_associated(priv))
7660 iwl4965_send_rxon_assoc(priv);
Tomas Winkler220173b2007-10-16 00:50:25 +02007661}
Zhu Yib481de92007-09-25 17:54:57 -07007662
7663#define IWL_DELAY_NEXT_SCAN (HZ*2)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007664static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
Zhu Yib481de92007-09-25 17:54:57 -07007665{
7666 int rc = 0;
7667 unsigned long flags;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007668 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007669
7670 IWL_DEBUG_MAC80211("enter\n");
7671
mabbas052c4b92007-10-25 17:15:43 +08007672 mutex_lock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007673 spin_lock_irqsave(&priv->lock, flags);
7674
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007675 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007676 rc = -EIO;
7677 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
7678 goto out_unlock;
7679 }
7680
7681 if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
7682 rc = -EIO;
7683 IWL_ERROR("ERROR: APs don't scan\n");
7684 goto out_unlock;
7685 }
7686
7687 /* if we just finished scan ask for delay */
7688 if (priv->last_scan_jiffies &&
7689 time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
7690 jiffies)) {
7691 rc = -EAGAIN;
7692 goto out_unlock;
7693 }
7694 if (len) {
7695 IWL_DEBUG_SCAN("direct scan for "
7696 "%s [%d]\n ",
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007697 iwl4965_escape_essid(ssid, len), (int)len);
Zhu Yib481de92007-09-25 17:54:57 -07007698
7699 priv->one_direct_scan = 1;
7700 priv->direct_ssid_len = (u8)
7701 min((u8) len, (u8) IW_ESSID_MAX_SIZE);
7702 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
Mohamed Abbas948c1712007-10-25 17:15:45 +08007703 } else
7704 priv->one_direct_scan = 0;
Zhu Yib481de92007-09-25 17:54:57 -07007705
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007706 rc = iwl4965_scan_initiate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007707
7708 IWL_DEBUG_MAC80211("leave\n");
7709
7710out_unlock:
7711 spin_unlock_irqrestore(&priv->lock, flags);
mabbas052c4b92007-10-25 17:15:43 +08007712 mutex_unlock(&priv->mutex);
Zhu Yib481de92007-09-25 17:54:57 -07007713
7714 return rc;
7715}
7716
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007717static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
Zhu Yib481de92007-09-25 17:54:57 -07007718 const u8 *local_addr, const u8 *addr,
7719 struct ieee80211_key_conf *key)
7720{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007721 struct iwl4965_priv *priv = hw->priv;
Joe Perches0795af52007-10-03 17:59:30 -07007722 DECLARE_MAC_BUF(mac);
Zhu Yib481de92007-09-25 17:54:57 -07007723 int rc = 0;
7724 u8 sta_id;
7725
7726 IWL_DEBUG_MAC80211("enter\n");
7727
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007728 if (!iwl4965_param_hwcrypto) {
Zhu Yib481de92007-09-25 17:54:57 -07007729 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
7730 return -EOPNOTSUPP;
7731 }
7732
7733 if (is_zero_ether_addr(addr))
7734 /* only support pairwise keys */
7735 return -EOPNOTSUPP;
7736
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007737 sta_id = iwl4965_hw_find_station(priv, addr);
Zhu Yib481de92007-09-25 17:54:57 -07007738 if (sta_id == IWL_INVALID_STATION) {
Joe Perches0795af52007-10-03 17:59:30 -07007739 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
7740 print_mac(mac, addr));
Zhu Yib481de92007-09-25 17:54:57 -07007741 return -EINVAL;
7742 }
7743
7744 mutex_lock(&priv->mutex);
7745
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007746 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007747
Zhu Yib481de92007-09-25 17:54:57 -07007748 switch (cmd) {
7749 case SET_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007750 rc = iwl4965_update_sta_key_info(priv, key, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007751 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007752 iwl4965_set_rxon_hwcrypto(priv, 1);
7753 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007754 key->hw_key_idx = sta_id;
7755 IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
7756 key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
7757 }
7758 break;
7759 case DISABLE_KEY:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007760 rc = iwl4965_clear_sta_key_info(priv, sta_id);
Zhu Yib481de92007-09-25 17:54:57 -07007761 if (!rc) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007762 iwl4965_set_rxon_hwcrypto(priv, 0);
7763 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007764 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
7765 }
7766 break;
7767 default:
7768 rc = -EINVAL;
7769 }
7770
7771 IWL_DEBUG_MAC80211("leave\n");
7772 mutex_unlock(&priv->mutex);
7773
7774 return rc;
7775}
7776
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007777static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
Zhu Yib481de92007-09-25 17:54:57 -07007778 const struct ieee80211_tx_queue_params *params)
7779{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007780 struct iwl4965_priv *priv = hw->priv;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007781#ifdef CONFIG_IWL4965_QOS
Zhu Yib481de92007-09-25 17:54:57 -07007782 unsigned long flags;
7783 int q;
Reinette Chatre0054b342007-11-29 11:09:42 +08007784#endif /* CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07007785
7786 IWL_DEBUG_MAC80211("enter\n");
7787
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007788 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007789 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7790 return -EIO;
7791 }
7792
7793 if (queue >= AC_NUM) {
7794 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
7795 return 0;
7796 }
7797
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007798#ifdef CONFIG_IWL4965_QOS
Zhu Yib481de92007-09-25 17:54:57 -07007799 if (!priv->qos_data.qos_enable) {
7800 priv->qos_data.qos_active = 0;
7801 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
7802 return 0;
7803 }
7804 q = AC_NUM - 1 - queue;
7805
7806 spin_lock_irqsave(&priv->lock, flags);
7807
7808 priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
7809 priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
7810 priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
7811 priv->qos_data.def_qos_parm.ac[q].edca_txop =
7812 cpu_to_le16((params->burst_time * 100));
7813
7814 priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
7815 priv->qos_data.qos_active = 1;
7816
7817 spin_unlock_irqrestore(&priv->lock, flags);
7818
7819 mutex_lock(&priv->mutex);
7820 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007821 iwl4965_activate_qos(priv, 1);
7822 else if (priv->assoc_id && iwl4965_is_associated(priv))
7823 iwl4965_activate_qos(priv, 0);
Zhu Yib481de92007-09-25 17:54:57 -07007824
7825 mutex_unlock(&priv->mutex);
7826
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007827#endif /*CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07007828
7829 IWL_DEBUG_MAC80211("leave\n");
7830 return 0;
7831}
7832
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007833static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007834 struct ieee80211_tx_queue_stats *stats)
7835{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007836 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007837 int i, avail;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007838 struct iwl4965_tx_queue *txq;
7839 struct iwl4965_queue *q;
Zhu Yib481de92007-09-25 17:54:57 -07007840 unsigned long flags;
7841
7842 IWL_DEBUG_MAC80211("enter\n");
7843
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007844 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007845 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7846 return -EIO;
7847 }
7848
7849 spin_lock_irqsave(&priv->lock, flags);
7850
7851 for (i = 0; i < AC_NUM; i++) {
7852 txq = &priv->txq[i];
7853 q = &txq->q;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007854 avail = iwl4965_queue_space(q);
Zhu Yib481de92007-09-25 17:54:57 -07007855
7856 stats->data[i].len = q->n_window - avail;
7857 stats->data[i].limit = q->n_window - q->high_mark;
7858 stats->data[i].count = q->n_window;
7859
7860 }
7861 spin_unlock_irqrestore(&priv->lock, flags);
7862
7863 IWL_DEBUG_MAC80211("leave\n");
7864
7865 return 0;
7866}
7867
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007868static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07007869 struct ieee80211_low_level_stats *stats)
7870{
7871 IWL_DEBUG_MAC80211("enter\n");
7872 IWL_DEBUG_MAC80211("leave\n");
7873
7874 return 0;
7875}
7876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007877static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007878{
7879 IWL_DEBUG_MAC80211("enter\n");
7880 IWL_DEBUG_MAC80211("leave\n");
7881
7882 return 0;
7883}
7884
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007885static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
Zhu Yib481de92007-09-25 17:54:57 -07007886{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007887 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007888 unsigned long flags;
7889
7890 mutex_lock(&priv->mutex);
7891 IWL_DEBUG_MAC80211("enter\n");
7892
7893 priv->lq_mngr.lq_ready = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007894#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07007895 spin_lock_irqsave(&priv->lock, flags);
7896 memset(&priv->current_assoc_ht, 0, sizeof(struct sta_ht_info));
7897 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007898#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07007899/* if (priv->lq_mngr.agg_ctrl.granted_ba)
7900 iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);*/
7901
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007902 memset(&(priv->lq_mngr.agg_ctrl), 0, sizeof(struct iwl4965_agg_control));
Zhu Yib481de92007-09-25 17:54:57 -07007903 priv->lq_mngr.agg_ctrl.tid_traffic_load_threshold = 10;
7904 priv->lq_mngr.agg_ctrl.ba_timeout = 5000;
7905 priv->lq_mngr.agg_ctrl.auto_agg = 1;
7906
7907 if (priv->lq_mngr.agg_ctrl.auto_agg)
7908 priv->lq_mngr.agg_ctrl.requested_ba = TID_ALL_ENABLED;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007909#endif /*CONFIG_IWL4965_HT_AGG */
7910#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07007911
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08007912#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007913 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007914#endif
7915
7916 cancel_delayed_work(&priv->post_associate);
7917
7918 spin_lock_irqsave(&priv->lock, flags);
7919 priv->assoc_id = 0;
7920 priv->assoc_capability = 0;
7921 priv->call_post_assoc_from_beacon = 0;
7922 priv->assoc_station_added = 0;
7923
7924 /* new association get rid of ibss beacon skb */
7925 if (priv->ibss_beacon)
7926 dev_kfree_skb(priv->ibss_beacon);
7927
7928 priv->ibss_beacon = NULL;
7929
7930 priv->beacon_int = priv->hw->conf.beacon_int;
7931 priv->timestamp1 = 0;
7932 priv->timestamp0 = 0;
7933 if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
7934 priv->beacon_int = 0;
7935
7936 spin_unlock_irqrestore(&priv->lock, flags);
7937
mabbas052c4b92007-10-25 17:15:43 +08007938 /* we are restarting association process
7939 * clear RXON_FILTER_ASSOC_MSK bit
7940 */
7941 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007942 iwl4965_scan_cancel_timeout(priv, 100);
mabbas052c4b92007-10-25 17:15:43 +08007943 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007944 iwl4965_commit_rxon(priv);
mabbas052c4b92007-10-25 17:15:43 +08007945 }
7946
Zhu Yib481de92007-09-25 17:54:57 -07007947 /* Per mac80211.h: This is only used in IBSS mode... */
7948 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
mabbas052c4b92007-10-25 17:15:43 +08007949
Zhu Yib481de92007-09-25 17:54:57 -07007950 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
7951 mutex_unlock(&priv->mutex);
7952 return;
7953 }
7954
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007955 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007956 IWL_DEBUG_MAC80211("leave - not ready\n");
7957 mutex_unlock(&priv->mutex);
7958 return;
7959 }
7960
7961 priv->only_active_channel = 0;
7962
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007963 iwl4965_set_rate(priv);
Zhu Yib481de92007-09-25 17:54:57 -07007964
7965 mutex_unlock(&priv->mutex);
7966
7967 IWL_DEBUG_MAC80211("leave\n");
7968
7969}
7970
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007971static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
Zhu Yib481de92007-09-25 17:54:57 -07007972 struct ieee80211_tx_control *control)
7973{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007974 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07007975 unsigned long flags;
7976
7977 mutex_lock(&priv->mutex);
7978 IWL_DEBUG_MAC80211("enter\n");
7979
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08007980 if (!iwl4965_is_ready_rf(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07007981 IWL_DEBUG_MAC80211("leave - RF not ready\n");
7982 mutex_unlock(&priv->mutex);
7983 return -EIO;
7984 }
7985
7986 if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
7987 IWL_DEBUG_MAC80211("leave - not IBSS\n");
7988 mutex_unlock(&priv->mutex);
7989 return -EIO;
7990 }
7991
7992 spin_lock_irqsave(&priv->lock, flags);
7993
7994 if (priv->ibss_beacon)
7995 dev_kfree_skb(priv->ibss_beacon);
7996
7997 priv->ibss_beacon = skb;
7998
7999 priv->assoc_id = 0;
8000
8001 IWL_DEBUG_MAC80211("leave\n");
8002 spin_unlock_irqrestore(&priv->lock, flags);
8003
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008004#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008005 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008006#endif
8007
8008 queue_work(priv->workqueue, &priv->post_associate.work);
8009
8010 mutex_unlock(&priv->mutex);
8011
8012 return 0;
8013}
8014
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008015#ifdef CONFIG_IWL4965_HT
Zhu Yib481de92007-09-25 17:54:57 -07008016union ht_cap_info {
8017 struct {
8018 u16 advanced_coding_cap :1;
8019 u16 supported_chan_width_set :1;
8020 u16 mimo_power_save_mode :2;
8021 u16 green_field :1;
8022 u16 short_GI20 :1;
8023 u16 short_GI40 :1;
8024 u16 tx_stbc :1;
8025 u16 rx_stbc :1;
8026 u16 beam_forming :1;
8027 u16 delayed_ba :1;
8028 u16 maximal_amsdu_size :1;
8029 u16 cck_mode_at_40MHz :1;
8030 u16 psmp_support :1;
8031 u16 stbc_ctrl_frame_support :1;
8032 u16 sig_txop_protection_support :1;
8033 };
8034 u16 val;
8035} __attribute__ ((packed));
8036
8037union ht_param_info{
8038 struct {
8039 u8 max_rx_ampdu_factor :2;
8040 u8 mpdu_density :3;
8041 u8 reserved :3;
8042 };
8043 u8 val;
8044} __attribute__ ((packed));
8045
8046union ht_exra_param_info {
8047 struct {
8048 u8 ext_chan_offset :2;
8049 u8 tx_chan_width :1;
8050 u8 rifs_mode :1;
8051 u8 controlled_access_only :1;
8052 u8 service_interval_granularity :3;
8053 };
8054 u8 val;
8055} __attribute__ ((packed));
8056
8057union ht_operation_mode{
8058 struct {
8059 u16 op_mode :2;
8060 u16 non_GF :1;
8061 u16 reserved :13;
8062 };
8063 u16 val;
8064} __attribute__ ((packed));
8065
8066
8067static int sta_ht_info_init(struct ieee80211_ht_capability *ht_cap,
8068 struct ieee80211_ht_additional_info *ht_extra,
8069 struct sta_ht_info *ht_info_ap,
8070 struct sta_ht_info *ht_info)
8071{
8072 union ht_cap_info cap;
8073 union ht_operation_mode op_mode;
8074 union ht_param_info param_info;
8075 union ht_exra_param_info extra_param_info;
8076
8077 IWL_DEBUG_MAC80211("enter: \n");
8078
8079 if (!ht_info) {
8080 IWL_DEBUG_MAC80211("leave: ht_info is NULL\n");
8081 return -1;
8082 }
8083
8084 if (ht_cap) {
8085 cap.val = (u16) le16_to_cpu(ht_cap->capabilities_info);
8086 param_info.val = ht_cap->mac_ht_params_info;
8087 ht_info->is_ht = 1;
8088 if (cap.short_GI20)
8089 ht_info->sgf |= 0x1;
8090 if (cap.short_GI40)
8091 ht_info->sgf |= 0x2;
8092 ht_info->is_green_field = cap.green_field;
8093 ht_info->max_amsdu_size = cap.maximal_amsdu_size;
8094 ht_info->supported_chan_width = cap.supported_chan_width_set;
8095 ht_info->tx_mimo_ps_mode = cap.mimo_power_save_mode;
8096 memcpy(ht_info->supp_rates, ht_cap->supported_mcs_set, 16);
8097
8098 ht_info->ampdu_factor = param_info.max_rx_ampdu_factor;
8099 ht_info->mpdu_density = param_info.mpdu_density;
8100
8101 IWL_DEBUG_MAC80211("SISO mask 0x%X MIMO mask 0x%X \n",
8102 ht_cap->supported_mcs_set[0],
8103 ht_cap->supported_mcs_set[1]);
8104
8105 if (ht_info_ap) {
8106 ht_info->control_channel = ht_info_ap->control_channel;
8107 ht_info->extension_chan_offset =
8108 ht_info_ap->extension_chan_offset;
8109 ht_info->tx_chan_width = ht_info_ap->tx_chan_width;
8110 ht_info->operating_mode = ht_info_ap->operating_mode;
8111 }
8112
8113 if (ht_extra) {
8114 extra_param_info.val = ht_extra->ht_param;
8115 ht_info->control_channel = ht_extra->control_chan;
8116 ht_info->extension_chan_offset =
8117 extra_param_info.ext_chan_offset;
8118 ht_info->tx_chan_width = extra_param_info.tx_chan_width;
8119 op_mode.val = (u16)
8120 le16_to_cpu(ht_extra->operation_mode);
8121 ht_info->operating_mode = op_mode.op_mode;
8122 IWL_DEBUG_MAC80211("control channel %d\n",
8123 ht_extra->control_chan);
8124 }
8125 } else
8126 ht_info->is_ht = 0;
8127
8128 IWL_DEBUG_MAC80211("leave\n");
8129 return 0;
8130}
8131
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008132static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07008133 struct ieee80211_ht_capability *ht_cap,
8134 struct ieee80211_ht_additional_info *ht_extra)
8135{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008136 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07008137 int rs;
8138
8139 IWL_DEBUG_MAC80211("enter: \n");
8140
8141 rs = sta_ht_info_init(ht_cap, ht_extra, NULL, &priv->current_assoc_ht);
8142 iwl4965_set_rxon_chain(priv);
8143
8144 if (priv && priv->assoc_id &&
8145 (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
8146 unsigned long flags;
8147
8148 spin_lock_irqsave(&priv->lock, flags);
8149 if (priv->beacon_int)
8150 queue_work(priv->workqueue, &priv->post_associate.work);
8151 else
8152 priv->call_post_assoc_from_beacon = 1;
8153 spin_unlock_irqrestore(&priv->lock, flags);
8154 }
8155
8156 IWL_DEBUG_MAC80211("leave: control channel %d\n",
8157 ht_extra->control_chan);
8158 return rs;
8159
8160}
8161
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008162static void iwl4965_set_ht_capab(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07008163 struct ieee80211_ht_capability *ht_cap,
8164 u8 use_wide_chan)
8165{
8166 union ht_cap_info cap;
8167 union ht_param_info param_info;
8168
8169 memset(&cap, 0, sizeof(union ht_cap_info));
8170 memset(&param_info, 0, sizeof(union ht_param_info));
8171
8172 cap.maximal_amsdu_size = HT_IE_MAX_AMSDU_SIZE_4K;
8173 cap.green_field = 1;
8174 cap.short_GI20 = 1;
8175 cap.short_GI40 = 1;
8176 cap.supported_chan_width_set = use_wide_chan;
8177 cap.mimo_power_save_mode = 0x3;
8178
8179 param_info.max_rx_ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
8180 param_info.mpdu_density = CFG_HT_MPDU_DENSITY_DEF;
8181 ht_cap->capabilities_info = (__le16) cpu_to_le16(cap.val);
8182 ht_cap->mac_ht_params_info = (u8) param_info.val;
8183
8184 ht_cap->supported_mcs_set[0] = 0xff;
8185 ht_cap->supported_mcs_set[1] = 0xff;
8186 ht_cap->supported_mcs_set[4] =
8187 (cap.supported_chan_width_set) ? 0x1: 0x0;
8188}
8189
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008190static void iwl4965_mac_get_ht_capab(struct ieee80211_hw *hw,
Zhu Yib481de92007-09-25 17:54:57 -07008191 struct ieee80211_ht_capability *ht_cap)
8192{
8193 u8 use_wide_channel = 1;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008194 struct iwl4965_priv *priv = hw->priv;
Zhu Yib481de92007-09-25 17:54:57 -07008195
8196 IWL_DEBUG_MAC80211("enter: \n");
8197 if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
8198 use_wide_channel = 0;
8199
8200 /* no fat tx allowed on 2.4GHZ */
8201 if (priv->phymode != MODE_IEEE80211A)
8202 use_wide_channel = 0;
8203
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008204 iwl4965_set_ht_capab(hw, ht_cap, use_wide_channel);
Zhu Yib481de92007-09-25 17:54:57 -07008205 IWL_DEBUG_MAC80211("leave: \n");
8206}
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008207#endif /*CONFIG_IWL4965_HT*/
Zhu Yib481de92007-09-25 17:54:57 -07008208
8209/*****************************************************************************
8210 *
8211 * sysfs attributes
8212 *
8213 *****************************************************************************/
8214
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008215#ifdef CONFIG_IWL4965_DEBUG
Zhu Yib481de92007-09-25 17:54:57 -07008216
8217/*
8218 * The following adds a new attribute to the sysfs representation
8219 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
8220 * used for controlling the debug level.
8221 *
8222 * See the level definitions in iwl for details.
8223 */
8224
8225static ssize_t show_debug_level(struct device_driver *d, char *buf)
8226{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008227 return sprintf(buf, "0x%08X\n", iwl4965_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07008228}
8229static ssize_t store_debug_level(struct device_driver *d,
8230 const char *buf, size_t count)
8231{
8232 char *p = (char *)buf;
8233 u32 val;
8234
8235 val = simple_strtoul(p, &p, 0);
8236 if (p == buf)
8237 printk(KERN_INFO DRV_NAME
8238 ": %s is not in hex or decimal form.\n", buf);
8239 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008240 iwl4965_debug_level = val;
Zhu Yib481de92007-09-25 17:54:57 -07008241
8242 return strnlen(buf, count);
8243}
8244
8245static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
8246 show_debug_level, store_debug_level);
8247
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008248#endif /* CONFIG_IWL4965_DEBUG */
Zhu Yib481de92007-09-25 17:54:57 -07008249
8250static ssize_t show_rf_kill(struct device *d,
8251 struct device_attribute *attr, char *buf)
8252{
8253 /*
8254 * 0 - RF kill not enabled
8255 * 1 - SW based RF kill active (sysfs)
8256 * 2 - HW based RF kill active
8257 * 3 - Both HW and SW based RF kill active
8258 */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008259 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008260 int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
8261 (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
8262
8263 return sprintf(buf, "%i\n", val);
8264}
8265
8266static ssize_t store_rf_kill(struct device *d,
8267 struct device_attribute *attr,
8268 const char *buf, size_t count)
8269{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008270 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008271
8272 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008273 iwl4965_radio_kill_sw(priv, buf[0] == '1');
Zhu Yib481de92007-09-25 17:54:57 -07008274 mutex_unlock(&priv->mutex);
8275
8276 return count;
8277}
8278
8279static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
8280
8281static ssize_t show_temperature(struct device *d,
8282 struct device_attribute *attr, char *buf)
8283{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008284 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008285
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008286 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008287 return -EAGAIN;
8288
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008289 return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
Zhu Yib481de92007-09-25 17:54:57 -07008290}
8291
8292static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
8293
8294static ssize_t show_rs_window(struct device *d,
8295 struct device_attribute *attr,
8296 char *buf)
8297{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008298 struct iwl4965_priv *priv = d->driver_data;
8299 return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
Zhu Yib481de92007-09-25 17:54:57 -07008300}
8301static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
8302
8303static ssize_t show_tx_power(struct device *d,
8304 struct device_attribute *attr, char *buf)
8305{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008306 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008307 return sprintf(buf, "%d\n", priv->user_txpower_limit);
8308}
8309
8310static ssize_t store_tx_power(struct device *d,
8311 struct device_attribute *attr,
8312 const char *buf, size_t count)
8313{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008314 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008315 char *p = (char *)buf;
8316 u32 val;
8317
8318 val = simple_strtoul(p, &p, 10);
8319 if (p == buf)
8320 printk(KERN_INFO DRV_NAME
8321 ": %s is not in decimal form.\n", buf);
8322 else
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008323 iwl4965_hw_reg_set_txpower(priv, val);
Zhu Yib481de92007-09-25 17:54:57 -07008324
8325 return count;
8326}
8327
8328static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
8329
8330static ssize_t show_flags(struct device *d,
8331 struct device_attribute *attr, char *buf)
8332{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008333 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008334
8335 return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
8336}
8337
8338static ssize_t store_flags(struct device *d,
8339 struct device_attribute *attr,
8340 const char *buf, size_t count)
8341{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008342 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008343 u32 flags = simple_strtoul(buf, NULL, 0);
8344
8345 mutex_lock(&priv->mutex);
8346 if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
8347 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008348 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008349 IWL_WARNING("Could not cancel scan.\n");
8350 else {
8351 IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
8352 flags);
8353 priv->staging_rxon.flags = cpu_to_le32(flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008354 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008355 }
8356 }
8357 mutex_unlock(&priv->mutex);
8358
8359 return count;
8360}
8361
8362static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
8363
8364static ssize_t show_filter_flags(struct device *d,
8365 struct device_attribute *attr, char *buf)
8366{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008367 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008368
8369 return sprintf(buf, "0x%04X\n",
8370 le32_to_cpu(priv->active_rxon.filter_flags));
8371}
8372
8373static ssize_t store_filter_flags(struct device *d,
8374 struct device_attribute *attr,
8375 const char *buf, size_t count)
8376{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008377 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008378 u32 filter_flags = simple_strtoul(buf, NULL, 0);
8379
8380 mutex_lock(&priv->mutex);
8381 if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
8382 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008383 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008384 IWL_WARNING("Could not cancel scan.\n");
8385 else {
8386 IWL_DEBUG_INFO("Committing rxon.filter_flags = "
8387 "0x%04X\n", filter_flags);
8388 priv->staging_rxon.filter_flags =
8389 cpu_to_le32(filter_flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008390 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008391 }
8392 }
8393 mutex_unlock(&priv->mutex);
8394
8395 return count;
8396}
8397
8398static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
8399 store_filter_flags);
8400
8401static ssize_t show_tune(struct device *d,
8402 struct device_attribute *attr, char *buf)
8403{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008404 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008405
8406 return sprintf(buf, "0x%04X\n",
8407 (priv->phymode << 8) |
8408 le16_to_cpu(priv->active_rxon.channel));
8409}
8410
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008411static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv, u8 phymode);
Zhu Yib481de92007-09-25 17:54:57 -07008412
8413static ssize_t store_tune(struct device *d,
8414 struct device_attribute *attr,
8415 const char *buf, size_t count)
8416{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008417 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
Zhu Yib481de92007-09-25 17:54:57 -07008418 char *p = (char *)buf;
8419 u16 tune = simple_strtoul(p, &p, 0);
8420 u8 phymode = (tune >> 8) & 0xff;
8421 u16 channel = tune & 0xff;
8422
8423 IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
8424
8425 mutex_lock(&priv->mutex);
8426 if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
8427 (priv->phymode != phymode)) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008428 const struct iwl4965_channel_info *ch_info;
Zhu Yib481de92007-09-25 17:54:57 -07008429
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008430 ch_info = iwl4965_get_channel_info(priv, phymode, channel);
Zhu Yib481de92007-09-25 17:54:57 -07008431 if (!ch_info) {
8432 IWL_WARNING("Requested invalid phymode/channel "
8433 "combination: %d %d\n", phymode, channel);
8434 mutex_unlock(&priv->mutex);
8435 return -EINVAL;
8436 }
8437
8438 /* Cancel any currently running scans... */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008439 if (iwl4965_scan_cancel_timeout(priv, 100))
Zhu Yib481de92007-09-25 17:54:57 -07008440 IWL_WARNING("Could not cancel scan.\n");
8441 else {
8442 IWL_DEBUG_INFO("Committing phymode and "
8443 "rxon.channel = %d %d\n",
8444 phymode, channel);
8445
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008446 iwl4965_set_rxon_channel(priv, phymode, channel);
8447 iwl4965_set_flags_for_phymode(priv, phymode);
Zhu Yib481de92007-09-25 17:54:57 -07008448
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008449 iwl4965_set_rate(priv);
8450 iwl4965_commit_rxon(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008451 }
8452 }
8453 mutex_unlock(&priv->mutex);
8454
8455 return count;
8456}
8457
8458static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
8459
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008460#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008461
8462static ssize_t show_measurement(struct device *d,
8463 struct device_attribute *attr, char *buf)
8464{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008465 struct iwl4965_priv *priv = dev_get_drvdata(d);
8466 struct iwl4965_spectrum_notification measure_report;
Zhu Yib481de92007-09-25 17:54:57 -07008467 u32 size = sizeof(measure_report), len = 0, ofs = 0;
8468 u8 *data = (u8 *) & measure_report;
8469 unsigned long flags;
8470
8471 spin_lock_irqsave(&priv->lock, flags);
8472 if (!(priv->measurement_status & MEASUREMENT_READY)) {
8473 spin_unlock_irqrestore(&priv->lock, flags);
8474 return 0;
8475 }
8476 memcpy(&measure_report, &priv->measure_report, size);
8477 priv->measurement_status = 0;
8478 spin_unlock_irqrestore(&priv->lock, flags);
8479
8480 while (size && (PAGE_SIZE - len)) {
8481 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8482 PAGE_SIZE - len, 1);
8483 len = strlen(buf);
8484 if (PAGE_SIZE - len)
8485 buf[len++] = '\n';
8486
8487 ofs += 16;
8488 size -= min(size, 16U);
8489 }
8490
8491 return len;
8492}
8493
8494static ssize_t store_measurement(struct device *d,
8495 struct device_attribute *attr,
8496 const char *buf, size_t count)
8497{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008498 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008499 struct ieee80211_measurement_params params = {
8500 .channel = le16_to_cpu(priv->active_rxon.channel),
8501 .start_time = cpu_to_le64(priv->last_tsf),
8502 .duration = cpu_to_le16(1),
8503 };
8504 u8 type = IWL_MEASURE_BASIC;
8505 u8 buffer[32];
8506 u8 channel;
8507
8508 if (count) {
8509 char *p = buffer;
8510 strncpy(buffer, buf, min(sizeof(buffer), count));
8511 channel = simple_strtoul(p, NULL, 0);
8512 if (channel)
8513 params.channel = channel;
8514
8515 p = buffer;
8516 while (*p && *p != ' ')
8517 p++;
8518 if (*p)
8519 type = simple_strtoul(p + 1, NULL, 0);
8520 }
8521
8522 IWL_DEBUG_INFO("Invoking measurement of type %d on "
8523 "channel %d (for '%s')\n", type, params.channel, buf);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008524 iwl4965_get_measurement(priv, &params, type);
Zhu Yib481de92007-09-25 17:54:57 -07008525
8526 return count;
8527}
8528
8529static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
8530 show_measurement, store_measurement);
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008531#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
Zhu Yib481de92007-09-25 17:54:57 -07008532
8533static ssize_t store_retry_rate(struct device *d,
8534 struct device_attribute *attr,
8535 const char *buf, size_t count)
8536{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008537 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008538
8539 priv->retry_rate = simple_strtoul(buf, NULL, 0);
8540 if (priv->retry_rate <= 0)
8541 priv->retry_rate = 1;
8542
8543 return count;
8544}
8545
8546static ssize_t show_retry_rate(struct device *d,
8547 struct device_attribute *attr, char *buf)
8548{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008549 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008550 return sprintf(buf, "%d", priv->retry_rate);
8551}
8552
8553static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
8554 store_retry_rate);
8555
8556static ssize_t store_power_level(struct device *d,
8557 struct device_attribute *attr,
8558 const char *buf, size_t count)
8559{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008560 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008561 int rc;
8562 int mode;
8563
8564 mode = simple_strtoul(buf, NULL, 0);
8565 mutex_lock(&priv->mutex);
8566
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008567 if (!iwl4965_is_ready(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07008568 rc = -EAGAIN;
8569 goto out;
8570 }
8571
8572 if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
8573 mode = IWL_POWER_AC;
8574 else
8575 mode |= IWL_POWER_ENABLED;
8576
8577 if (mode != priv->power_mode) {
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008578 rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
Zhu Yib481de92007-09-25 17:54:57 -07008579 if (rc) {
8580 IWL_DEBUG_MAC80211("failed setting power mode.\n");
8581 goto out;
8582 }
8583 priv->power_mode = mode;
8584 }
8585
8586 rc = count;
8587
8588 out:
8589 mutex_unlock(&priv->mutex);
8590 return rc;
8591}
8592
8593#define MAX_WX_STRING 80
8594
8595/* Values are in microsecond */
8596static const s32 timeout_duration[] = {
8597 350000,
8598 250000,
8599 75000,
8600 37000,
8601 25000,
8602};
8603static const s32 period_duration[] = {
8604 400000,
8605 700000,
8606 1000000,
8607 1000000,
8608 1000000
8609};
8610
8611static ssize_t show_power_level(struct device *d,
8612 struct device_attribute *attr, char *buf)
8613{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008614 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008615 int level = IWL_POWER_LEVEL(priv->power_mode);
8616 char *p = buf;
8617
8618 p += sprintf(p, "%d ", level);
8619 switch (level) {
8620 case IWL_POWER_MODE_CAM:
8621 case IWL_POWER_AC:
8622 p += sprintf(p, "(AC)");
8623 break;
8624 case IWL_POWER_BATTERY:
8625 p += sprintf(p, "(BATTERY)");
8626 break;
8627 default:
8628 p += sprintf(p,
8629 "(Timeout %dms, Period %dms)",
8630 timeout_duration[level - 1] / 1000,
8631 period_duration[level - 1] / 1000);
8632 }
8633
8634 if (!(priv->power_mode & IWL_POWER_ENABLED))
8635 p += sprintf(p, " OFF\n");
8636 else
8637 p += sprintf(p, " \n");
8638
8639 return (p - buf + 1);
8640
8641}
8642
8643static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
8644 store_power_level);
8645
8646static ssize_t show_channels(struct device *d,
8647 struct device_attribute *attr, char *buf)
8648{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008649 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008650 int len = 0, i;
8651 struct ieee80211_channel *channels = NULL;
8652 const struct ieee80211_hw_mode *hw_mode = NULL;
8653 int count = 0;
8654
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008655 if (!iwl4965_is_ready(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008656 return -EAGAIN;
8657
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008658 hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211G);
Zhu Yib481de92007-09-25 17:54:57 -07008659 if (!hw_mode)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008660 hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211B);
Zhu Yib481de92007-09-25 17:54:57 -07008661 if (hw_mode) {
8662 channels = hw_mode->channels;
8663 count = hw_mode->num_channels;
8664 }
8665
8666 len +=
8667 sprintf(&buf[len],
8668 "Displaying %d channels in 2.4GHz band "
8669 "(802.11bg):\n", count);
8670
8671 for (i = 0; i < count; i++)
8672 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8673 channels[i].chan,
8674 channels[i].power_level,
8675 channels[i].
8676 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8677 " (IEEE 802.11h required)" : "",
8678 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8679 || (channels[i].
8680 flag &
8681 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8682 ", IBSS",
8683 channels[i].
8684 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8685 "active/passive" : "passive only");
8686
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008687 hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211A);
Zhu Yib481de92007-09-25 17:54:57 -07008688 if (hw_mode) {
8689 channels = hw_mode->channels;
8690 count = hw_mode->num_channels;
8691 } else {
8692 channels = NULL;
8693 count = 0;
8694 }
8695
8696 len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
8697 "(802.11a):\n", count);
8698
8699 for (i = 0; i < count; i++)
8700 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
8701 channels[i].chan,
8702 channels[i].power_level,
8703 channels[i].
8704 flag & IEEE80211_CHAN_W_RADAR_DETECT ?
8705 " (IEEE 802.11h required)" : "",
8706 (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
8707 || (channels[i].
8708 flag &
8709 IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
8710 ", IBSS",
8711 channels[i].
8712 flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
8713 "active/passive" : "passive only");
8714
8715 return len;
8716}
8717
8718static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
8719
8720static ssize_t show_statistics(struct device *d,
8721 struct device_attribute *attr, char *buf)
8722{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008723 struct iwl4965_priv *priv = dev_get_drvdata(d);
8724 u32 size = sizeof(struct iwl4965_notif_statistics);
Zhu Yib481de92007-09-25 17:54:57 -07008725 u32 len = 0, ofs = 0;
8726 u8 *data = (u8 *) & priv->statistics;
8727 int rc = 0;
8728
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008729 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008730 return -EAGAIN;
8731
8732 mutex_lock(&priv->mutex);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008733 rc = iwl4965_send_statistics_request(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008734 mutex_unlock(&priv->mutex);
8735
8736 if (rc) {
8737 len = sprintf(buf,
8738 "Error sending statistics request: 0x%08X\n", rc);
8739 return len;
8740 }
8741
8742 while (size && (PAGE_SIZE - len)) {
8743 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
8744 PAGE_SIZE - len, 1);
8745 len = strlen(buf);
8746 if (PAGE_SIZE - len)
8747 buf[len++] = '\n';
8748
8749 ofs += 16;
8750 size -= min(size, 16U);
8751 }
8752
8753 return len;
8754}
8755
8756static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
8757
8758static ssize_t show_antenna(struct device *d,
8759 struct device_attribute *attr, char *buf)
8760{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008761 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008762
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008763 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008764 return -EAGAIN;
8765
8766 return sprintf(buf, "%d\n", priv->antenna);
8767}
8768
8769static ssize_t store_antenna(struct device *d,
8770 struct device_attribute *attr,
8771 const char *buf, size_t count)
8772{
8773 int ant;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008774 struct iwl4965_priv *priv = dev_get_drvdata(d);
Zhu Yib481de92007-09-25 17:54:57 -07008775
8776 if (count == 0)
8777 return 0;
8778
8779 if (sscanf(buf, "%1i", &ant) != 1) {
8780 IWL_DEBUG_INFO("not in hex or decimal form.\n");
8781 return count;
8782 }
8783
8784 if ((ant >= 0) && (ant <= 2)) {
8785 IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008786 priv->antenna = (enum iwl4965_antenna)ant;
Zhu Yib481de92007-09-25 17:54:57 -07008787 } else
8788 IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
8789
8790
8791 return count;
8792}
8793
8794static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
8795
8796static ssize_t show_status(struct device *d,
8797 struct device_attribute *attr, char *buf)
8798{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008799 struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
8800 if (!iwl4965_is_alive(priv))
Zhu Yib481de92007-09-25 17:54:57 -07008801 return -EAGAIN;
8802 return sprintf(buf, "0x%08x\n", (int)priv->status);
8803}
8804
8805static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
8806
8807static ssize_t dump_error_log(struct device *d,
8808 struct device_attribute *attr,
8809 const char *buf, size_t count)
8810{
8811 char *p = (char *)buf;
8812
8813 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008814 iwl4965_dump_nic_error_log((struct iwl4965_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008815
8816 return strnlen(buf, count);
8817}
8818
8819static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
8820
8821static ssize_t dump_event_log(struct device *d,
8822 struct device_attribute *attr,
8823 const char *buf, size_t count)
8824{
8825 char *p = (char *)buf;
8826
8827 if (p[0] == '1')
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008828 iwl4965_dump_nic_event_log((struct iwl4965_priv *)d->driver_data);
Zhu Yib481de92007-09-25 17:54:57 -07008829
8830 return strnlen(buf, count);
8831}
8832
8833static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
8834
8835/*****************************************************************************
8836 *
8837 * driver setup and teardown
8838 *
8839 *****************************************************************************/
8840
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008841static void iwl4965_setup_deferred_work(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008842{
8843 priv->workqueue = create_workqueue(DRV_NAME);
8844
8845 init_waitqueue_head(&priv->wait_command_queue);
8846
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008847 INIT_WORK(&priv->up, iwl4965_bg_up);
8848 INIT_WORK(&priv->restart, iwl4965_bg_restart);
8849 INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
8850 INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
8851 INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
8852 INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
8853 INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
8854 INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
8855 INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
8856 INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
8857 INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
8858 INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
Zhu Yib481de92007-09-25 17:54:57 -07008859
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008860 iwl4965_hw_setup_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008861
8862 tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008863 iwl4965_irq_tasklet, (unsigned long)priv);
Zhu Yib481de92007-09-25 17:54:57 -07008864}
8865
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008866static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07008867{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008868 iwl4965_hw_cancel_deferred_work(priv);
Zhu Yib481de92007-09-25 17:54:57 -07008869
Joonwoo Park3ae6a052007-11-29 10:43:16 +09008870 cancel_delayed_work_sync(&priv->init_alive_start);
Zhu Yib481de92007-09-25 17:54:57 -07008871 cancel_delayed_work(&priv->scan_check);
8872 cancel_delayed_work(&priv->alive_start);
8873 cancel_delayed_work(&priv->post_associate);
8874 cancel_work_sync(&priv->beacon_update);
8875}
8876
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008877static struct attribute *iwl4965_sysfs_entries[] = {
Zhu Yib481de92007-09-25 17:54:57 -07008878 &dev_attr_antenna.attr,
8879 &dev_attr_channels.attr,
8880 &dev_attr_dump_errors.attr,
8881 &dev_attr_dump_events.attr,
8882 &dev_attr_flags.attr,
8883 &dev_attr_filter_flags.attr,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008884#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
Zhu Yib481de92007-09-25 17:54:57 -07008885 &dev_attr_measurement.attr,
8886#endif
8887 &dev_attr_power_level.attr,
8888 &dev_attr_retry_rate.attr,
8889 &dev_attr_rf_kill.attr,
8890 &dev_attr_rs_window.attr,
8891 &dev_attr_statistics.attr,
8892 &dev_attr_status.attr,
8893 &dev_attr_temperature.attr,
8894 &dev_attr_tune.attr,
8895 &dev_attr_tx_power.attr,
8896
8897 NULL
8898};
8899
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008900static struct attribute_group iwl4965_attribute_group = {
Zhu Yib481de92007-09-25 17:54:57 -07008901 .name = NULL, /* put in device directory */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008902 .attrs = iwl4965_sysfs_entries,
Zhu Yib481de92007-09-25 17:54:57 -07008903};
8904
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008905static struct ieee80211_ops iwl4965_hw_ops = {
8906 .tx = iwl4965_mac_tx,
8907 .start = iwl4965_mac_start,
8908 .stop = iwl4965_mac_stop,
8909 .add_interface = iwl4965_mac_add_interface,
8910 .remove_interface = iwl4965_mac_remove_interface,
8911 .config = iwl4965_mac_config,
8912 .config_interface = iwl4965_mac_config_interface,
8913 .configure_filter = iwl4965_configure_filter,
8914 .set_key = iwl4965_mac_set_key,
8915 .get_stats = iwl4965_mac_get_stats,
8916 .get_tx_stats = iwl4965_mac_get_tx_stats,
8917 .conf_tx = iwl4965_mac_conf_tx,
8918 .get_tsf = iwl4965_mac_get_tsf,
8919 .reset_tsf = iwl4965_mac_reset_tsf,
8920 .beacon_update = iwl4965_mac_beacon_update,
8921 .erp_ie_changed = iwl4965_mac_erp_ie_changed,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008922#ifdef CONFIG_IWL4965_HT
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008923 .conf_ht = iwl4965_mac_conf_ht,
8924 .get_ht_capab = iwl4965_mac_get_ht_capab,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008925#ifdef CONFIG_IWL4965_HT_AGG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008926 .ht_tx_agg_start = iwl4965_mac_ht_tx_agg_start,
8927 .ht_tx_agg_stop = iwl4965_mac_ht_tx_agg_stop,
8928 .ht_rx_agg_start = iwl4965_mac_ht_rx_agg_start,
8929 .ht_rx_agg_stop = iwl4965_mac_ht_rx_agg_stop,
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008930#endif /* CONFIG_IWL4965_HT_AGG */
8931#endif /* CONFIG_IWL4965_HT */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008932 .hw_scan = iwl4965_mac_hw_scan
Zhu Yib481de92007-09-25 17:54:57 -07008933};
8934
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008935static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Zhu Yib481de92007-09-25 17:54:57 -07008936{
8937 int err = 0;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008938 struct iwl4965_priv *priv;
Zhu Yib481de92007-09-25 17:54:57 -07008939 struct ieee80211_hw *hw;
8940 int i;
8941
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008942 if (iwl4965_param_disable_hw_scan) {
Zhu Yib481de92007-09-25 17:54:57 -07008943 IWL_DEBUG_INFO("Disabling hw_scan\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008944 iwl4965_hw_ops.hw_scan = NULL;
Zhu Yib481de92007-09-25 17:54:57 -07008945 }
8946
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008947 if ((iwl4965_param_queues_num > IWL_MAX_NUM_QUEUES) ||
8948 (iwl4965_param_queues_num < IWL_MIN_NUM_QUEUES)) {
Zhu Yib481de92007-09-25 17:54:57 -07008949 IWL_ERROR("invalid queues_num, should be between %d and %d\n",
8950 IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
8951 err = -EINVAL;
8952 goto out;
8953 }
8954
8955 /* mac80211 allocates memory for this device instance, including
8956 * space for this driver's private structure */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008957 hw = ieee80211_alloc_hw(sizeof(struct iwl4965_priv), &iwl4965_hw_ops);
Zhu Yib481de92007-09-25 17:54:57 -07008958 if (hw == NULL) {
8959 IWL_ERROR("Can not allocate network device\n");
8960 err = -ENOMEM;
8961 goto out;
8962 }
8963 SET_IEEE80211_DEV(hw, &pdev->dev);
8964
Johannes Bergf51359a2007-10-28 14:53:36 +01008965 hw->rate_control_algorithm = "iwl-4965-rs";
8966
Zhu Yib481de92007-09-25 17:54:57 -07008967 IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
8968 priv = hw->priv;
8969 priv->hw = hw;
8970
8971 priv->pci_dev = pdev;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008972 priv->antenna = (enum iwl4965_antenna)iwl4965_param_antenna;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008973#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08008974 iwl4965_debug_level = iwl4965_param_debug;
Zhu Yib481de92007-09-25 17:54:57 -07008975 atomic_set(&priv->restrict_refcnt, 0);
8976#endif
8977 priv->retry_rate = 1;
8978
8979 priv->ibss_beacon = NULL;
8980
8981 /* Tell mac80211 and its clients (e.g. Wireless Extensions)
8982 * the range of signal quality values that we'll provide.
8983 * Negative values for level/noise indicate that we'll provide dBm.
8984 * For WE, at least, non-0 values here *enable* display of values
8985 * in app (iwconfig). */
8986 hw->max_rssi = -20; /* signal level, negative indicates dBm */
8987 hw->max_noise = -20; /* noise level, negative indicates dBm */
8988 hw->max_signal = 100; /* link quality indication (%) */
8989
8990 /* Tell mac80211 our Tx characteristics */
8991 hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
8992
8993 hw->queues = 4;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008994#ifdef CONFIG_IWL4965_HT
8995#ifdef CONFIG_IWL4965_HT_AGG
Zhu Yib481de92007-09-25 17:54:57 -07008996 hw->queues = 16;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08008997#endif /* CONFIG_IWL4965_HT_AGG */
8998#endif /* CONFIG_IWL4965_HT */
Zhu Yib481de92007-09-25 17:54:57 -07008999
9000 spin_lock_init(&priv->lock);
9001 spin_lock_init(&priv->power_data.lock);
9002 spin_lock_init(&priv->sta_lock);
9003 spin_lock_init(&priv->hcmd_lock);
9004 spin_lock_init(&priv->lq_mngr.lock);
9005
9006 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
9007 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
9008
9009 INIT_LIST_HEAD(&priv->free_frames);
9010
9011 mutex_init(&priv->mutex);
9012 if (pci_enable_device(pdev)) {
9013 err = -ENODEV;
9014 goto out_ieee80211_free_hw;
9015 }
9016
9017 pci_set_master(pdev);
9018
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009019 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009020
9021 priv->data_retry_limit = -1;
9022 priv->ieee_channels = NULL;
9023 priv->ieee_rates = NULL;
9024 priv->phymode = -1;
9025
9026 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
9027 if (!err)
9028 err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
9029 if (err) {
9030 printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
9031 goto out_pci_disable_device;
9032 }
9033
9034 pci_set_drvdata(pdev, priv);
9035 err = pci_request_regions(pdev, DRV_NAME);
9036 if (err)
9037 goto out_pci_disable_device;
9038 /* We disable the RETRY_TIMEOUT register (0x41) to keep
9039 * PCI Tx retries from interfering with C3 CPU state */
9040 pci_write_config_byte(pdev, 0x41, 0x00);
9041 priv->hw_base = pci_iomap(pdev, 0, 0);
9042 if (!priv->hw_base) {
9043 err = -ENODEV;
9044 goto out_pci_release_regions;
9045 }
9046
9047 IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
9048 (unsigned long long) pci_resource_len(pdev, 0));
9049 IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
9050
9051 /* Initialize module parameter values here */
9052
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009053 if (iwl4965_param_disable) {
Zhu Yib481de92007-09-25 17:54:57 -07009054 set_bit(STATUS_RF_KILL_SW, &priv->status);
9055 IWL_DEBUG_INFO("Radio disabled.\n");
9056 }
9057
9058 priv->iw_mode = IEEE80211_IF_TYPE_STA;
9059
9060 priv->ps_mode = 0;
9061 priv->use_ant_b_for_management_frame = 1; /* start with ant B */
9062 priv->is_ht_enabled = 1;
9063 priv->channel_width = IWL_CHANNEL_WIDTH_40MHZ;
9064 priv->valid_antenna = 0x7; /* assume all 3 connected */
9065 priv->ps_mode = IWL_MIMO_PS_NONE;
Zhu Yib481de92007-09-25 17:54:57 -07009066
9067 iwl4965_set_rxon_chain(priv);
9068
9069 printk(KERN_INFO DRV_NAME
9070 ": Detected Intel Wireless WiFi Link 4965AGN\n");
9071
9072 /* Device-specific setup */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009073 if (iwl4965_hw_set_hw_setting(priv)) {
Zhu Yib481de92007-09-25 17:54:57 -07009074 IWL_ERROR("failed to set hw settings\n");
9075 mutex_unlock(&priv->mutex);
9076 goto out_iounmap;
9077 }
9078
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009079#ifdef CONFIG_IWL4965_QOS
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009080 if (iwl4965_param_qos_enable)
Zhu Yib481de92007-09-25 17:54:57 -07009081 priv->qos_data.qos_enable = 1;
9082
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009083 iwl4965_reset_qos(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009084
9085 priv->qos_data.qos_active = 0;
9086 priv->qos_data.qos_cap.val = 0;
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009087#endif /* CONFIG_IWL4965_QOS */
Zhu Yib481de92007-09-25 17:54:57 -07009088
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009089 iwl4965_set_rxon_channel(priv, MODE_IEEE80211G, 6);
9090 iwl4965_setup_deferred_work(priv);
9091 iwl4965_setup_rx_handlers(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009092
9093 priv->rates_mask = IWL_RATES_MASK;
9094 /* If power management is turned on, default to AC mode */
9095 priv->power_mode = IWL_POWER_AC;
9096 priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
9097
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009098 iwl4965_disable_interrupts(priv);
Jes Sorensen49df2b32007-10-26 16:10:39 +02009099
Zhu Yib481de92007-09-25 17:54:57 -07009100 pci_enable_msi(pdev);
9101
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009102 err = request_irq(pdev->irq, iwl4965_isr, IRQF_SHARED, DRV_NAME, priv);
Zhu Yib481de92007-09-25 17:54:57 -07009103 if (err) {
9104 IWL_ERROR("Error allocating IRQ %d\n", pdev->irq);
9105 goto out_disable_msi;
9106 }
9107
9108 mutex_lock(&priv->mutex);
9109
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009110 err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07009111 if (err) {
9112 IWL_ERROR("failed to create sysfs device attributes\n");
9113 mutex_unlock(&priv->mutex);
9114 goto out_release_irq;
9115 }
9116
9117 /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
9118 * ucode filename and max sizes are card-specific. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009119 err = iwl4965_read_ucode(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009120 if (err) {
9121 IWL_ERROR("Could not read microcode: %d\n", err);
9122 mutex_unlock(&priv->mutex);
9123 goto out_pci_alloc;
9124 }
9125
9126 mutex_unlock(&priv->mutex);
9127
Ian Schram01ebd062007-10-25 17:15:22 +08009128 IWL_DEBUG_INFO("Queueing UP work.\n");
Zhu Yib481de92007-09-25 17:54:57 -07009129
9130 queue_work(priv->workqueue, &priv->up);
9131
9132 return 0;
9133
9134 out_pci_alloc:
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009135 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009136
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009137 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07009138
9139 out_release_irq:
9140 free_irq(pdev->irq, priv);
9141
9142 out_disable_msi:
9143 pci_disable_msi(pdev);
9144 destroy_workqueue(priv->workqueue);
9145 priv->workqueue = NULL;
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009146 iwl4965_unset_hw_setting(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009147
9148 out_iounmap:
9149 pci_iounmap(pdev, priv->hw_base);
9150 out_pci_release_regions:
9151 pci_release_regions(pdev);
9152 out_pci_disable_device:
9153 pci_disable_device(pdev);
9154 pci_set_drvdata(pdev, NULL);
9155 out_ieee80211_free_hw:
9156 ieee80211_free_hw(priv->hw);
9157 out:
9158 return err;
9159}
9160
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009161static void iwl4965_pci_remove(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07009162{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009163 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07009164 struct list_head *p, *q;
9165 int i;
9166
9167 if (!priv)
9168 return;
9169
9170 IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
9171
Zhu Yib481de92007-09-25 17:54:57 -07009172 set_bit(STATUS_EXIT_PENDING, &priv->status);
Zhu Yib24d22b2007-12-19 13:59:52 +08009173
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009174 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009175
9176 /* Free MAC hash list for ADHOC */
9177 for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
9178 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
9179 list_del(p);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009180 kfree(list_entry(p, struct iwl4965_ibss_seq, list));
Zhu Yib481de92007-09-25 17:54:57 -07009181 }
9182 }
9183
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009184 sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
Zhu Yib481de92007-09-25 17:54:57 -07009185
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009186 iwl4965_dealloc_ucode_pci(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009187
9188 if (priv->rxq.bd)
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009189 iwl4965_rx_queue_free(priv, &priv->rxq);
9190 iwl4965_hw_txq_ctx_free(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009191
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009192 iwl4965_unset_hw_setting(priv);
9193 iwl4965_clear_stations_table(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009194
9195 if (priv->mac80211_registered) {
9196 ieee80211_unregister_hw(priv->hw);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009197 iwl4965_rate_control_unregister(priv->hw);
Zhu Yib481de92007-09-25 17:54:57 -07009198 }
9199
Mohamed Abbas948c1712007-10-25 17:15:45 +08009200 /*netif_stop_queue(dev); */
9201 flush_workqueue(priv->workqueue);
9202
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009203 /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
Zhu Yib481de92007-09-25 17:54:57 -07009204 * priv->workqueue... so we can't take down the workqueue
9205 * until now... */
9206 destroy_workqueue(priv->workqueue);
9207 priv->workqueue = NULL;
9208
9209 free_irq(pdev->irq, priv);
9210 pci_disable_msi(pdev);
9211 pci_iounmap(pdev, priv->hw_base);
9212 pci_release_regions(pdev);
9213 pci_disable_device(pdev);
9214 pci_set_drvdata(pdev, NULL);
9215
9216 kfree(priv->channel_info);
9217
9218 kfree(priv->ieee_channels);
9219 kfree(priv->ieee_rates);
9220
9221 if (priv->ibss_beacon)
9222 dev_kfree_skb(priv->ibss_beacon);
9223
9224 ieee80211_free_hw(priv->hw);
9225}
9226
9227#ifdef CONFIG_PM
9228
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009229static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
Zhu Yib481de92007-09-25 17:54:57 -07009230{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009231 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07009232
Zhu Yib481de92007-09-25 17:54:57 -07009233 set_bit(STATUS_IN_SUSPEND, &priv->status);
9234
9235 /* Take down the device; powers it off, etc. */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009236 iwl4965_down(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009237
9238 if (priv->mac80211_registered)
9239 ieee80211_stop_queues(priv->hw);
9240
9241 pci_save_state(pdev);
9242 pci_disable_device(pdev);
9243 pci_set_power_state(pdev, PCI_D3hot);
9244
Zhu Yib481de92007-09-25 17:54:57 -07009245 return 0;
9246}
9247
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009248static void iwl4965_resume(struct iwl4965_priv *priv)
Zhu Yib481de92007-09-25 17:54:57 -07009249{
9250 unsigned long flags;
9251
9252 /* The following it a temporary work around due to the
9253 * suspend / resume not fully initializing the NIC correctly.
9254 * Without all of the following, resume will not attempt to take
9255 * down the NIC (it shouldn't really need to) and will just try
9256 * and bring the NIC back up. However that fails during the
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009257 * ucode verification process. This then causes iwl4965_down to be
9258 * called *after* iwl4965_hw_nic_init() has succeeded -- which
Zhu Yib481de92007-09-25 17:54:57 -07009259 * then lets the next init sequence succeed. So, we've
9260 * replicated all of that NIC init code here... */
9261
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009262 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
Zhu Yib481de92007-09-25 17:54:57 -07009263
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009264 iwl4965_hw_nic_init(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009265
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009266 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9267 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
Zhu Yib481de92007-09-25 17:54:57 -07009268 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009269 iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
9270 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
9271 iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
Zhu Yib481de92007-09-25 17:54:57 -07009272
9273 /* tell the device to stop sending interrupts */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009274 iwl4965_disable_interrupts(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009275
9276 spin_lock_irqsave(&priv->lock, flags);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009277 iwl4965_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
Zhu Yib481de92007-09-25 17:54:57 -07009278
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009279 if (!iwl4965_grab_nic_access(priv)) {
9280 iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
Tomas Winklerac17a942007-10-25 17:15:37 +08009281 APMG_CLK_VAL_DMA_CLK_RQT);
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009282 iwl4965_release_nic_access(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009283 }
9284 spin_unlock_irqrestore(&priv->lock, flags);
9285
9286 udelay(5);
9287
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009288 iwl4965_hw_nic_reset(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009289
9290 /* Bring the device back up */
9291 clear_bit(STATUS_IN_SUSPEND, &priv->status);
9292 queue_work(priv->workqueue, &priv->up);
9293}
9294
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009295static int iwl4965_pci_resume(struct pci_dev *pdev)
Zhu Yib481de92007-09-25 17:54:57 -07009296{
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009297 struct iwl4965_priv *priv = pci_get_drvdata(pdev);
Zhu Yib481de92007-09-25 17:54:57 -07009298 int err;
9299
9300 printk(KERN_INFO "Coming out of suspend...\n");
9301
Zhu Yib481de92007-09-25 17:54:57 -07009302 pci_set_power_state(pdev, PCI_D0);
9303 err = pci_enable_device(pdev);
9304 pci_restore_state(pdev);
9305
9306 /*
9307 * Suspend/Resume resets the PCI configuration space, so we have to
9308 * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
9309 * from interfering with C3 CPU state. pci_restore_state won't help
9310 * here since it only restores the first 64 bytes pci config header.
9311 */
9312 pci_write_config_byte(pdev, 0x41, 0x00);
9313
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009314 iwl4965_resume(priv);
Zhu Yib481de92007-09-25 17:54:57 -07009315
9316 return 0;
9317}
9318
9319#endif /* CONFIG_PM */
9320
9321/*****************************************************************************
9322 *
9323 * driver and module entry point
9324 *
9325 *****************************************************************************/
9326
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009327static struct pci_driver iwl4965_driver = {
Zhu Yib481de92007-09-25 17:54:57 -07009328 .name = DRV_NAME,
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009329 .id_table = iwl4965_hw_card_ids,
9330 .probe = iwl4965_pci_probe,
9331 .remove = __devexit_p(iwl4965_pci_remove),
Zhu Yib481de92007-09-25 17:54:57 -07009332#ifdef CONFIG_PM
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009333 .suspend = iwl4965_pci_suspend,
9334 .resume = iwl4965_pci_resume,
Zhu Yib481de92007-09-25 17:54:57 -07009335#endif
9336};
9337
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009338static int __init iwl4965_init(void)
Zhu Yib481de92007-09-25 17:54:57 -07009339{
9340
9341 int ret;
9342 printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
9343 printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009344 ret = pci_register_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07009345 if (ret) {
9346 IWL_ERROR("Unable to initialize PCI module\n");
9347 return ret;
9348 }
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009349#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009350 ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07009351 if (ret) {
9352 IWL_ERROR("Unable to create driver sysfs file\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009353 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07009354 return ret;
9355 }
9356#endif
9357
9358 return ret;
9359}
9360
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009361static void __exit iwl4965_exit(void)
Zhu Yib481de92007-09-25 17:54:57 -07009362{
Christoph Hellwigc8b0e6e2007-10-25 17:15:51 +08009363#ifdef CONFIG_IWL4965_DEBUG
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009364 driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
Zhu Yib481de92007-09-25 17:54:57 -07009365#endif
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009366 pci_unregister_driver(&iwl4965_driver);
Zhu Yib481de92007-09-25 17:54:57 -07009367}
9368
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009369module_param_named(antenna, iwl4965_param_antenna, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009370MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009371module_param_named(disable, iwl4965_param_disable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009372MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009373module_param_named(hwcrypto, iwl4965_param_hwcrypto, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009374MODULE_PARM_DESC(hwcrypto,
9375 "using hardware crypto engine (default 0 [software])\n");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009376module_param_named(debug, iwl4965_param_debug, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009377MODULE_PARM_DESC(debug, "debug output mask");
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009378module_param_named(disable_hw_scan, iwl4965_param_disable_hw_scan, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009379MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
9380
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009381module_param_named(queues_num, iwl4965_param_queues_num, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009382MODULE_PARM_DESC(queues_num, "number of hw queues.");
9383
9384/* QoS */
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009385module_param_named(qos_enable, iwl4965_param_qos_enable, int, 0444);
Zhu Yib481de92007-09-25 17:54:57 -07009386MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
9387
Christoph Hellwigbb8c0932008-01-27 16:41:47 -08009388module_exit(iwl4965_exit);
9389module_init(iwl4965_init);