~ubuntu-branches/ubuntu/hardy/linux-backports-modules-2.6.24/hardy-security

« back to all changes in this revision

Viewing changes to updates/wireless/iwlwifi/iwlwifi/origin/iwl4965-base.c

  • Committer: Bazaar Package Importer
  • Author(s): , Ben Collins
  • Date: 2008-04-02 06:59:04 UTC
  • Revision ID: james.westby@ubuntu.com-20080402065904-e5knh2gn2hms3xbb
Tags: 2.6.24-14.11
[Ben Collins]

* iwlwifi: Update to iwlwifi-1.2.25 and mac80211-10.0.4
  - LP: #200950
* ubuntu: Slight cleanups to module hiearchy and Makefiles
* mac80211: Enable LED triggers
* iwlwifi: Add LED trigger support (rx and tx only)
  - LP: #176090

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
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
 
 
30
#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>
 
41
#include <linux/skbuff.h>
 
42
#include <linux/netdevice.h>
 
43
#include <linux/etherdevice.h>
 
44
#include <linux/if_arp.h>
 
45
 
 
46
#include <net/ieee80211_radiotap.h>
 
47
#include <net/mac80211.h>
 
48
 
 
49
#include <asm/div64.h>
 
50
 
 
51
#include "iwl-4965.h"
 
52
#include "iwl-helpers.h"
 
53
 
 
54
#ifdef CONFIG_IWL4965_DEBUG
 
55
u32 iwl4965_debug_level;
 
56
#endif
 
57
 
 
58
static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
 
59
                                  struct iwl4965_tx_queue *txq);
 
60
 
 
61
/******************************************************************************
 
62
 *
 
63
 * module boiler plate
 
64
 *
 
65
 ******************************************************************************/
 
66
 
 
67
/* module parameters */
 
68
static int iwl4965_param_disable_hw_scan; /* def: 0 = use 4965's h/w scan */
 
69
static int iwl4965_param_debug;    /* def: 0 = minimal debug log messages */
 
70
static int iwl4965_param_disable;  /* def: enable radio */
 
71
static int iwl4965_param_antenna;  /* def: 0 = both antennas (use diversity) */
 
72
int iwl4965_param_hwcrypto;        /* def: using software encryption */
 
73
static int iwl4965_param_qos_enable = 1; /* def: 1 = use quality of service */
 
74
int iwl4965_param_queues_num = IWL_MAX_NUM_QUEUES; /* def: 16 Tx queues */
 
75
 
 
76
/*
 
77
 * module name, copyright, version, etc.
 
78
 * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
 
79
 */
 
80
 
 
81
#define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
 
82
 
 
83
#ifdef CONFIG_IWL4965_DEBUG
 
84
#define VD "d"
 
85
#else
 
86
#define VD
 
87
#endif
 
88
 
 
89
#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
 
90
#define VS "s"
 
91
#else
 
92
#define VS
 
93
#endif
 
94
 
 
95
#define IWLWIFI_VERSION "1.2.25" VD VS
 
96
#define DRV_COPYRIGHT   "Copyright(c) 2003-2007 Intel Corporation"
 
97
#define DRV_VERSION     IWLWIFI_VERSION
 
98
 
 
99
/* Change firmware file name, using "-" and incrementing number,
 
100
 *   *only* when uCode interface or architecture changes so that it
 
101
 *   is not compatible with earlier drivers.
 
102
 * This number will also appear in << 8 position of 1st dword of uCode file */
 
103
#define IWL4965_UCODE_API "-1"
 
104
 
 
105
MODULE_DESCRIPTION(DRV_DESCRIPTION);
 
106
MODULE_VERSION(DRV_VERSION);
 
107
MODULE_AUTHOR(DRV_COPYRIGHT);
 
108
MODULE_LICENSE("GPL");
 
109
 
 
110
__le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
 
111
{
 
112
        u16 fc = le16_to_cpu(hdr->frame_control);
 
113
        int hdr_len = iwlwifi_ieee80211_get_hdrlen(fc);
 
114
 
 
115
        if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
 
116
                return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
 
117
        return NULL;
 
118
}
 
119
 
 
120
static const struct ieee80211_hw_mode *iwl4965_get_hw_mode(
 
121
                struct iwl4965_priv *priv, int mode)
 
122
{
 
123
        int i;
 
124
 
 
125
        for (i = 0; i < 3; i++)
 
126
                if (priv->modes[i].mode == mode)
 
127
                        return &priv->modes[i];
 
128
 
 
129
        return NULL;
 
130
}
 
131
 
 
132
static int iwl4965_is_empty_essid(const char *essid, int essid_len)
 
133
{
 
134
        /* Single white space is for Linksys APs */
 
135
        if (essid_len == 1 && essid[0] == ' ')
 
136
                return 1;
 
137
 
 
138
        /* Otherwise, if the entire essid is 0, we assume it is hidden */
 
139
        while (essid_len) {
 
140
                essid_len--;
 
141
                if (essid[essid_len] != '\0')
 
142
                        return 0;
 
143
        }
 
144
 
 
145
        return 1;
 
146
}
 
147
 
 
148
static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
 
149
{
 
150
        static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
 
151
        const char *s = essid;
 
152
        char *d = escaped;
 
153
 
 
154
        if (iwl4965_is_empty_essid(essid, essid_len)) {
 
155
                memcpy(escaped, "<hidden>", sizeof("<hidden>"));
 
156
                return escaped;
 
157
        }
 
158
 
 
159
        essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
 
160
        while (essid_len--) {
 
161
                if (*s == '\0') {
 
162
                        *d++ = '\\';
 
163
                        *d++ = '0';
 
164
                        s++;
 
165
                } else
 
166
                        *d++ = *s++;
 
167
        }
 
168
        *d = '\0';
 
169
        return escaped;
 
170
}
 
171
 
 
172
static void iwl4965_print_hex_dump(int level, void *p, u32 len)
 
173
{
 
174
#ifdef CONFIG_IWL4965_DEBUG
 
175
        if (!(iwl4965_debug_level & level))
 
176
                return;
 
177
 
 
178
        print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
 
179
                        p, len, 1);
 
180
#endif
 
181
}
 
182
 
 
183
/*************** DMA-QUEUE-GENERAL-FUNCTIONS  *****
 
184
 * DMA services
 
185
 *
 
186
 * Theory of operation
 
187
 *
 
188
 * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
 
189
 * of buffer descriptors, each of which points to one or more data buffers for
 
190
 * the device to read from or fill.  Driver and device exchange status of each
 
191
 * queue via "read" and "write" pointers.  Driver keeps minimum of 2 empty
 
192
 * entries in each circular buffer, to protect against confusing empty and full
 
193
 * queue states.
 
194
 *
 
195
 * The device reads or writes the data in the queues via the device's several
 
196
 * DMA/FIFO channels.  Each queue is mapped to a single DMA channel.
 
197
 *
 
198
 * For Tx queue, there are low mark and high mark limits. If, after queuing
 
199
 * the packet for Tx, free space become < low mark, Tx queue stopped. When
 
200
 * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
 
201
 * Tx queue resumed.
 
202
 *
 
203
 * The 4965 operates with up to 17 queues:  One receive queue, one transmit
 
204
 * queue (#4) for sending commands to the device firmware, and 15 other
 
205
 * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
 
206
 *
 
207
 * See more detailed info in iwl-4965-hw.h.
 
208
 ***************************************************/
 
209
 
 
210
static int iwl4965_queue_space(const struct iwl4965_queue *q)
 
211
{
 
212
        int s = q->read_ptr - q->write_ptr;
 
213
 
 
214
        if (q->read_ptr > q->write_ptr)
 
215
                s -= q->n_bd;
 
216
 
 
217
        if (s <= 0)
 
218
                s += q->n_window;
 
219
        /* keep some reserve to not confuse empty and full situations */
 
220
        s -= 2;
 
221
        if (s < 0)
 
222
                s = 0;
 
223
        return s;
 
224
}
 
225
 
 
226
/**
 
227
 * iwl4965_queue_inc_wrap - increment queue index, wrap back to beginning
 
228
 * @index -- current index
 
229
 * @n_bd -- total number of entries in queue (must be power of 2)
 
230
 */
 
231
static inline int iwl4965_queue_inc_wrap(int index, int n_bd)
 
232
{
 
233
        return ++index & (n_bd - 1);
 
234
}
 
235
 
 
236
/**
 
237
 * iwl4965_queue_dec_wrap - decrement queue index, wrap back to end
 
238
 * @index -- current index
 
239
 * @n_bd -- total number of entries in queue (must be power of 2)
 
240
 */
 
241
static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
 
242
{
 
243
        return --index & (n_bd - 1);
 
244
}
 
245
 
 
246
static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
 
247
{
 
248
        return q->write_ptr > q->read_ptr ?
 
249
                (i >= q->read_ptr && i < q->write_ptr) :
 
250
                !(i < q->read_ptr && i >= q->write_ptr);
 
251
}
 
252
 
 
253
static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
 
254
{
 
255
        /* This is for scan command, the big buffer at end of command array */
 
256
        if (is_huge)
 
257
                return q->n_window;     /* must be power of 2 */
 
258
 
 
259
        /* Otherwise, use normal size buffers */
 
260
        return index & (q->n_window - 1);
 
261
}
 
262
 
 
263
/**
 
264
 * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
 
265
 */
 
266
static int iwl4965_queue_init(struct iwl4965_priv *priv, struct iwl4965_queue *q,
 
267
                          int count, int slots_num, u32 id)
 
268
{
 
269
        q->n_bd = count;
 
270
        q->n_window = slots_num;
 
271
        q->id = id;
 
272
 
 
273
        /* count must be power-of-two size, otherwise iwl4965_queue_inc_wrap
 
274
         * and iwl4965_queue_dec_wrap are broken. */
 
275
        BUG_ON(!is_power_of_2(count));
 
276
 
 
277
        /* slots_num must be power-of-two size, otherwise
 
278
         * get_cmd_index is broken. */
 
279
        BUG_ON(!is_power_of_2(slots_num));
 
280
 
 
281
        q->low_mark = q->n_window / 4;
 
282
        if (q->low_mark < 4)
 
283
                q->low_mark = 4;
 
284
 
 
285
        q->high_mark = q->n_window / 8;
 
286
        if (q->high_mark < 2)
 
287
                q->high_mark = 2;
 
288
 
 
289
        q->write_ptr = q->read_ptr = 0;
 
290
 
 
291
        return 0;
 
292
}
 
293
 
 
294
/**
 
295
 * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
 
296
 */
 
297
static int iwl4965_tx_queue_alloc(struct iwl4965_priv *priv,
 
298
                              struct iwl4965_tx_queue *txq, u32 id)
 
299
{
 
300
        struct pci_dev *dev = priv->pci_dev;
 
301
 
 
302
        /* Driver private data, only for Tx (not command) queues,
 
303
         * not shared with device. */
 
304
        if (id != IWL_CMD_QUEUE_NUM) {
 
305
                txq->txb = kmalloc(sizeof(txq->txb[0]) *
 
306
                                   TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
 
307
                if (!txq->txb) {
 
308
                        IWL_ERROR("kmalloc for auxiliary BD "
 
309
                                  "structures failed\n");
 
310
                        goto error;
 
311
                }
 
312
        } else
 
313
                txq->txb = NULL;
 
314
 
 
315
        /* Circular buffer of transmit frame descriptors (TFDs),
 
316
         * shared with device */
 
317
        txq->bd = pci_alloc_consistent(dev,
 
318
                        sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
 
319
                        &txq->q.dma_addr);
 
320
 
 
321
        if (!txq->bd) {
 
322
                IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
 
323
                          sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
 
324
                goto error;
 
325
        }
 
326
        txq->q.id = id;
 
327
 
 
328
        return 0;
 
329
 
 
330
 error:
 
331
        if (txq->txb) {
 
332
                kfree(txq->txb);
 
333
                txq->txb = NULL;
 
334
        }
 
335
 
 
336
        return -ENOMEM;
 
337
}
 
338
 
 
339
/**
 
340
 * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
 
341
 */
 
342
int iwl4965_tx_queue_init(struct iwl4965_priv *priv,
 
343
                      struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
 
344
{
 
345
        struct pci_dev *dev = priv->pci_dev;
 
346
        int len;
 
347
        int rc = 0;
 
348
 
 
349
        /*
 
350
         * Alloc buffer array for commands (Tx or other types of commands).
 
351
         * For the command queue (#4), allocate command space + one big
 
352
         * command for scan, since scan command is very huge; the system will
 
353
         * not have two scans at the same time, so only one is needed.
 
354
         * For data Tx queues (all other queues), no super-size command
 
355
         * space is needed.
 
356
         */
 
357
        len = sizeof(struct iwl4965_cmd) * slots_num;
 
358
        if (txq_id == IWL_CMD_QUEUE_NUM)
 
359
                len +=  IWL_MAX_SCAN_SIZE;
 
360
        txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
 
361
        if (!txq->cmd)
 
362
                return -ENOMEM;
 
363
 
 
364
        /* Alloc driver data array and TFD circular buffer */
 
365
        rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
 
366
        if (rc) {
 
367
                pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
 
368
 
 
369
                return -ENOMEM;
 
370
        }
 
371
        txq->need_update = 0;
 
372
 
 
373
        /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
 
374
         * iwl4965_queue_inc_wrap and iwl4965_queue_dec_wrap are broken. */
 
375
        BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
 
376
 
 
377
        /* Initialize queue's high/low-water marks, and head/tail indexes */
 
378
        iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
 
379
 
 
380
        /* Tell device where to find queue */
 
381
        iwl4965_hw_tx_queue_init(priv, txq);
 
382
 
 
383
        return 0;
 
384
}
 
385
 
 
386
/**
 
387
 * iwl4965_tx_queue_free - Deallocate DMA queue.
 
388
 * @txq: Transmit queue to deallocate.
 
389
 *
 
390
 * Empty queue by removing and destroying all BD's.
 
391
 * Free all buffers.
 
392
 * 0-fill, but do not free "txq" descriptor structure.
 
393
 */
 
394
void iwl4965_tx_queue_free(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
 
395
{
 
396
        struct iwl4965_queue *q = &txq->q;
 
397
        struct pci_dev *dev = priv->pci_dev;
 
398
        int len;
 
399
 
 
400
        if (q->n_bd == 0)
 
401
                return;
 
402
 
 
403
        /* first, empty all BD's */
 
404
        for (; q->write_ptr != q->read_ptr;
 
405
             q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd))
 
406
                iwl4965_hw_txq_free_tfd(priv, txq);
 
407
 
 
408
        len = sizeof(struct iwl4965_cmd) * q->n_window;
 
409
        if (q->id == IWL_CMD_QUEUE_NUM)
 
410
                len += IWL_MAX_SCAN_SIZE;
 
411
 
 
412
        /* De-alloc array of command/tx buffers */
 
413
        pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
 
414
 
 
415
        /* De-alloc circular buffer of TFDs */
 
416
        if (txq->q.n_bd)
 
417
                pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
 
418
                                    txq->q.n_bd, txq->bd, txq->q.dma_addr);
 
419
 
 
420
        /* De-alloc array of per-TFD driver data */
 
421
        if (txq->txb) {
 
422
                kfree(txq->txb);
 
423
                txq->txb = NULL;
 
424
        }
 
425
 
 
426
        /* 0-fill queue descriptor structure */
 
427
        memset(txq, 0, sizeof(*txq));
 
428
}
 
429
 
 
430
const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
 
431
 
 
432
/*************** STATION TABLE MANAGEMENT ****
 
433
 * mac80211 should be examined to determine if sta_info is duplicating
 
434
 * the functionality provided here
 
435
 */
 
436
 
 
437
/**************************************************************/
 
438
 
 
439
#if 0 /* temporary disable till we add real remove station */
 
440
/**
 
441
 * iwl4965_remove_station - Remove driver's knowledge of station.
 
442
 *
 
443
 * NOTE:  This does not remove station from device's station table.
 
444
 */
 
445
static u8 iwl4965_remove_station(struct iwl4965_priv *priv, const u8 *addr, int is_ap)
 
446
{
 
447
        int index = IWL_INVALID_STATION;
 
448
        int i;
 
449
        unsigned long flags;
 
450
 
 
451
        spin_lock_irqsave(&priv->sta_lock, flags);
 
452
 
 
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 (priv->stations[i].used &&
 
460
                            !compare_ether_addr(priv->stations[i].sta.sta.addr,
 
461
                                                addr)) {
 
462
                                index = i;
 
463
                                break;
 
464
                        }
 
465
 
 
466
        if (unlikely(index == IWL_INVALID_STATION))
 
467
                goto out;
 
468
 
 
469
        if (priv->stations[index].used) {
 
470
                priv->stations[index].used = 0;
 
471
                priv->num_stations--;
 
472
        }
 
473
 
 
474
        BUG_ON(priv->num_stations < 0);
 
475
 
 
476
out:
 
477
        spin_unlock_irqrestore(&priv->sta_lock, flags);
 
478
        return 0;
 
479
}
 
480
#endif
 
481
 
 
482
/**
 
483
 * iwl4965_clear_stations_table - Clear the driver's station table
 
484
 *
 
485
 * NOTE:  This does not clear or otherwise alter the device's station table.
 
486
 */
 
487
static void iwl4965_clear_stations_table(struct iwl4965_priv *priv)
 
488
{
 
489
        unsigned long flags;
 
490
 
 
491
        spin_lock_irqsave(&priv->sta_lock, flags);
 
492
 
 
493
        priv->num_stations = 0;
 
494
        memset(priv->stations, 0, sizeof(priv->stations));
 
495
 
 
496
        spin_unlock_irqrestore(&priv->sta_lock, flags);
 
497
}
 
498
 
 
499
/**
 
500
 * iwl4965_add_station_flags - Add station to tables in driver and device
 
501
 */
 
502
u8 iwl4965_add_station_flags(struct iwl4965_priv *priv, const u8 *addr, int is_ap, u8 flags)
 
503
{
 
504
        int i;
 
505
        int index = IWL_INVALID_STATION;
 
506
        struct iwl4965_station_entry *station;
 
507
        unsigned long flags_spin;
 
508
 
 
509
        spin_lock_irqsave(&priv->sta_lock, flags_spin);
 
510
        if (is_ap)
 
511
                index = IWL_AP_ID;
 
512
        else if (is_broadcast_ether_addr(addr))
 
513
                index = priv->hw_setting.bcast_sta_id;
 
514
        else
 
515
                for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
 
516
                        if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
 
517
                                                addr)) {
 
518
                                index = i;
 
519
                                break;
 
520
                        }
 
521
 
 
522
                        if (!priv->stations[i].used &&
 
523
                            index == IWL_INVALID_STATION)
 
524
                                index = i;
 
525
                }
 
526
 
 
527
 
 
528
        /* These two conditions have the same outcome, but keep them separate
 
529
          since they have different meanings */
 
530
        if (unlikely(index == IWL_INVALID_STATION)) {
 
531
                spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
 
532
                return index;
 
533
        }
 
534
 
 
535
        if (priv->stations[index].used &&
 
536
            !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
 
537
                spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
 
538
                return index;
 
539
        }
 
540
 
 
541
 
 
542
        IWL_DEBUG_ASSOC("Add STA ID %d: " MAC_FMT "\n", index, MAC_ARG(addr));
 
543
        station = &priv->stations[index];
 
544
        station->used = 1;
 
545
        priv->num_stations++;
 
546
 
 
547
        /* Set up the REPLY_ADD_STA command to send to device */
 
548
        memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
 
549
        memcpy(station->sta.sta.addr, addr, ETH_ALEN);
 
550
        station->sta.mode = 0;
 
551
        station->sta.sta.sta_id = index;
 
552
        station->sta.station_flags = 0;
 
553
 
 
554
#ifdef CONFIG_IWL4965_HT
 
555
        /* BCAST station and IBSS stations do not work in HT mode */
 
556
        if (index != priv->hw_setting.bcast_sta_id &&
 
557
            priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
 
558
                iwl4965_set_ht_add_station(priv, index);
 
559
#endif /*CONFIG_IWL4965_HT*/
 
560
 
 
561
        spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
 
562
 
 
563
        /* Add station to device's station table */
 
564
        iwl4965_send_add_station(priv, &station->sta, flags);
 
565
        return index;
 
566
 
 
567
}
 
568
 
 
569
/*************** DRIVER STATUS FUNCTIONS   *****/
 
570
 
 
571
static inline int iwl4965_is_ready(struct iwl4965_priv *priv)
 
572
{
 
573
        /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
 
574
         * set but EXIT_PENDING is not */
 
575
        return test_bit(STATUS_READY, &priv->status) &&
 
576
               test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
 
577
               !test_bit(STATUS_EXIT_PENDING, &priv->status);
 
578
}
 
579
 
 
580
static inline int iwl4965_is_alive(struct iwl4965_priv *priv)
 
581
{
 
582
        return test_bit(STATUS_ALIVE, &priv->status);
 
583
}
 
584
 
 
585
static inline int iwl4965_is_init(struct iwl4965_priv *priv)
 
586
{
 
587
        return test_bit(STATUS_INIT, &priv->status);
 
588
}
 
589
 
 
590
static inline int iwl4965_is_rfkill(struct iwl4965_priv *priv)
 
591
{
 
592
        return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
 
593
               test_bit(STATUS_RF_KILL_SW, &priv->status);
 
594
}
 
595
 
 
596
static inline int iwl4965_is_ready_rf(struct iwl4965_priv *priv)
 
597
{
 
598
 
 
599
        if (iwl4965_is_rfkill(priv))
 
600
                return 0;
 
601
 
 
602
        return iwl4965_is_ready(priv);
 
603
}
 
604
 
 
605
/*************** HOST COMMAND QUEUE FUNCTIONS   *****/
 
606
 
 
607
#define IWL_CMD(x) case x : return #x
 
608
 
 
609
static const char *get_cmd_string(u8 cmd)
 
610
{
 
611
        switch (cmd) {
 
612
                IWL_CMD(REPLY_ALIVE);
 
613
                IWL_CMD(REPLY_ERROR);
 
614
                IWL_CMD(REPLY_RXON);
 
615
                IWL_CMD(REPLY_RXON_ASSOC);
 
616
                IWL_CMD(REPLY_QOS_PARAM);
 
617
                IWL_CMD(REPLY_RXON_TIMING);
 
618
                IWL_CMD(REPLY_ADD_STA);
 
619
                IWL_CMD(REPLY_REMOVE_STA);
 
620
                IWL_CMD(REPLY_REMOVE_ALL_STA);
 
621
                IWL_CMD(REPLY_TX);
 
622
                IWL_CMD(REPLY_RATE_SCALE);
 
623
                IWL_CMD(REPLY_LEDS_CMD);
 
624
                IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
 
625
                IWL_CMD(RADAR_NOTIFICATION);
 
626
                IWL_CMD(REPLY_QUIET_CMD);
 
627
                IWL_CMD(REPLY_CHANNEL_SWITCH);
 
628
                IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
 
629
                IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
 
630
                IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
 
631
                IWL_CMD(POWER_TABLE_CMD);
 
632
                IWL_CMD(PM_SLEEP_NOTIFICATION);
 
633
                IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
 
634
                IWL_CMD(REPLY_SCAN_CMD);
 
635
                IWL_CMD(REPLY_SCAN_ABORT_CMD);
 
636
                IWL_CMD(SCAN_START_NOTIFICATION);
 
637
                IWL_CMD(SCAN_RESULTS_NOTIFICATION);
 
638
                IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
 
639
                IWL_CMD(BEACON_NOTIFICATION);
 
640
                IWL_CMD(REPLY_TX_BEACON);
 
641
                IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
 
642
                IWL_CMD(QUIET_NOTIFICATION);
 
643
                IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
 
644
                IWL_CMD(MEASURE_ABORT_NOTIFICATION);
 
645
                IWL_CMD(REPLY_BT_CONFIG);
 
646
                IWL_CMD(REPLY_STATISTICS_CMD);
 
647
                IWL_CMD(STATISTICS_NOTIFICATION);
 
648
                IWL_CMD(REPLY_CARD_STATE_CMD);
 
649
                IWL_CMD(CARD_STATE_NOTIFICATION);
 
650
                IWL_CMD(MISSED_BEACONS_NOTIFICATION);
 
651
                IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
 
652
                IWL_CMD(SENSITIVITY_CMD);
 
653
                IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
 
654
                IWL_CMD(REPLY_RX_PHY_CMD);
 
655
                IWL_CMD(REPLY_RX_MPDU_CMD);
 
656
                IWL_CMD(REPLY_4965_RX);
 
657
                IWL_CMD(REPLY_COMPRESSED_BA);
 
658
        default:
 
659
                return "UNKNOWN";
 
660
 
 
661
        }
 
662
}
 
663
 
 
664
#define HOST_COMPLETE_TIMEOUT (HZ / 2)
 
665
 
 
666
/**
 
667
 * iwl4965_enqueue_hcmd - enqueue a uCode command
 
668
 * @priv: device private data point
 
669
 * @cmd: a point to the ucode command structure
 
670
 *
 
671
 * The function returns < 0 values to indicate the operation is
 
672
 * failed. On success, it turns the index (> 0) of command in the
 
673
 * command queue.
 
674
 */
 
675
static int iwl4965_enqueue_hcmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
 
676
{
 
677
        struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
 
678
        struct iwl4965_queue *q = &txq->q;
 
679
        struct iwl4965_tfd_frame *tfd;
 
680
        u32 *control_flags;
 
681
        struct iwl4965_cmd *out_cmd;
 
682
        u32 idx;
 
683
        u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
 
684
        dma_addr_t phys_addr;
 
685
        int ret;
 
686
        unsigned long flags;
 
687
 
 
688
        /* If any of the command structures end up being larger than
 
689
         * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
 
690
         * we will need to increase the size of the TFD entries */
 
691
        BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
 
692
               !(cmd->meta.flags & CMD_SIZE_HUGE));
 
693
 
 
694
        if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
 
695
                IWL_ERROR("No space for Tx\n");
 
696
                return -ENOSPC;
 
697
        }
 
698
 
 
699
        spin_lock_irqsave(&priv->hcmd_lock, flags);
 
700
 
 
701
        tfd = &txq->bd[q->write_ptr];
 
702
        memset(tfd, 0, sizeof(*tfd));
 
703
 
 
704
        control_flags = (u32 *) tfd;
 
705
 
 
706
        idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
 
707
        out_cmd = &txq->cmd[idx];
 
708
 
 
709
        out_cmd->hdr.cmd = cmd->id;
 
710
        memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
 
711
        memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
 
712
 
 
713
        /* At this point, the out_cmd now has all of the incoming cmd
 
714
         * information */
 
715
 
 
716
        out_cmd->hdr.flags = 0;
 
717
        out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
 
718
                        INDEX_TO_SEQ(q->write_ptr));
 
719
        if (out_cmd->meta.flags & CMD_SIZE_HUGE)
 
720
                out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
 
721
 
 
722
        phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
 
723
                        offsetof(struct iwl4965_cmd, hdr);
 
724
        iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
 
725
 
 
726
        IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
 
727
                     "%d bytes at %d[%d]:%d\n",
 
728
                     get_cmd_string(out_cmd->hdr.cmd),
 
729
                     out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
 
730
                     fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
 
731
 
 
732
        txq->need_update = 1;
 
733
 
 
734
        /* Set up entry in queue's byte count circular buffer */
 
735
        ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
 
736
 
 
737
        /* Increment and update queue's write index */
 
738
        q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
 
739
        iwl4965_tx_queue_update_write_ptr(priv, txq);
 
740
 
 
741
        spin_unlock_irqrestore(&priv->hcmd_lock, flags);
 
742
        return ret ? ret : idx;
 
743
}
 
744
 
 
745
static int iwl4965_send_cmd_async(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
 
746
{
 
747
        int ret;
 
748
 
 
749
        BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
 
750
 
 
751
        /* An asynchronous command can not expect an SKB to be set. */
 
752
        BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
 
753
 
 
754
        /* An asynchronous command MUST have a callback. */
 
755
        BUG_ON(!cmd->meta.u.callback);
 
756
 
 
757
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 
758
                return -EBUSY;
 
759
 
 
760
        ret = iwl4965_enqueue_hcmd(priv, cmd);
 
761
        if (ret < 0) {
 
762
                IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
 
763
                          get_cmd_string(cmd->id), ret);
 
764
                return ret;
 
765
        }
 
766
        return 0;
 
767
}
 
768
 
 
769
static int iwl4965_send_cmd_sync(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
 
770
{
 
771
        int cmd_idx;
 
772
        int ret;
 
773
        static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
 
774
 
 
775
        BUG_ON(cmd->meta.flags & CMD_ASYNC);
 
776
 
 
777
         /* A synchronous command can not have a callback set. */
 
778
        BUG_ON(cmd->meta.u.callback != NULL);
 
779
 
 
780
        if (atomic_xchg(&entry, 1)) {
 
781
                IWL_ERROR("Error sending %s: Already sending a host command\n",
 
782
                          get_cmd_string(cmd->id));
 
783
                return -EBUSY;
 
784
        }
 
785
 
 
786
        set_bit(STATUS_HCMD_ACTIVE, &priv->status);
 
787
 
 
788
        if (cmd->meta.flags & CMD_WANT_SKB)
 
789
                cmd->meta.source = &cmd->meta;
 
790
 
 
791
        cmd_idx = iwl4965_enqueue_hcmd(priv, cmd);
 
792
        if (cmd_idx < 0) {
 
793
                ret = cmd_idx;
 
794
                IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
 
795
                          get_cmd_string(cmd->id), ret);
 
796
                goto out;
 
797
        }
 
798
 
 
799
        ret = wait_event_interruptible_timeout(priv->wait_command_queue,
 
800
                        !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
 
801
                        HOST_COMPLETE_TIMEOUT);
 
802
        if (!ret) {
 
803
                if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
 
804
                        IWL_ERROR("Error sending %s: time out after %dms.\n",
 
805
                                  get_cmd_string(cmd->id),
 
806
                                  jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
 
807
 
 
808
                        clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
 
809
                        ret = -ETIMEDOUT;
 
810
                        goto cancel;
 
811
                }
 
812
        }
 
813
 
 
814
        if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
 
815
                IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
 
816
                               get_cmd_string(cmd->id));
 
817
                ret = -ECANCELED;
 
818
                goto fail;
 
819
        }
 
820
        if (test_bit(STATUS_FW_ERROR, &priv->status)) {
 
821
                IWL_DEBUG_INFO("Command %s failed: FW Error\n",
 
822
                               get_cmd_string(cmd->id));
 
823
                ret = -EIO;
 
824
                goto fail;
 
825
        }
 
826
        if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
 
827
                IWL_ERROR("Error: Response NULL in '%s'\n",
 
828
                          get_cmd_string(cmd->id));
 
829
                ret = -EIO;
 
830
                goto out;
 
831
        }
 
832
 
 
833
        ret = 0;
 
834
        goto out;
 
835
 
 
836
cancel:
 
837
        if (cmd->meta.flags & CMD_WANT_SKB) {
 
838
                struct iwl4965_cmd *qcmd;
 
839
 
 
840
                /* Cancel the CMD_WANT_SKB flag for the cmd in the
 
841
                 * TX cmd queue. Otherwise in case the cmd comes
 
842
                 * in later, it will possibly set an invalid
 
843
                 * address (cmd->meta.source). */
 
844
                qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
 
845
                qcmd->meta.flags &= ~CMD_WANT_SKB;
 
846
        }
 
847
fail:
 
848
        if (cmd->meta.u.skb) {
 
849
                dev_kfree_skb_any(cmd->meta.u.skb);
 
850
                cmd->meta.u.skb = NULL;
 
851
        }
 
852
out:
 
853
        atomic_set(&entry, 0);
 
854
        return ret;
 
855
}
 
856
 
 
857
int iwl4965_send_cmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
 
858
{
 
859
        if (cmd->meta.flags & CMD_ASYNC)
 
860
                return iwl4965_send_cmd_async(priv, cmd);
 
861
 
 
862
        return iwl4965_send_cmd_sync(priv, cmd);
 
863
}
 
864
 
 
865
int iwl4965_send_cmd_pdu(struct iwl4965_priv *priv, u8 id, u16 len, const void *data)
 
866
{
 
867
        struct iwl4965_host_cmd cmd = {
 
868
                .id = id,
 
869
                .len = len,
 
870
                .data = data,
 
871
        };
 
872
 
 
873
        return iwl4965_send_cmd_sync(priv, &cmd);
 
874
}
 
875
 
 
876
static int __must_check iwl4965_send_cmd_u32(struct iwl4965_priv *priv, u8 id, u32 val)
 
877
{
 
878
        struct iwl4965_host_cmd cmd = {
 
879
                .id = id,
 
880
                .len = sizeof(val),
 
881
                .data = &val,
 
882
        };
 
883
 
 
884
        return iwl4965_send_cmd_sync(priv, &cmd);
 
885
}
 
886
 
 
887
int iwl4965_send_statistics_request(struct iwl4965_priv *priv)
 
888
{
 
889
        return iwl4965_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
 
890
}
 
891
 
 
892
/**
 
893
 * iwl4965_rxon_add_station - add station into station table.
 
894
 *
 
895
 * there is only one AP station with id= IWL_AP_ID
 
896
 * NOTE: mutex must be held before calling this fnction
 
897
 */
 
898
static int iwl4965_rxon_add_station(struct iwl4965_priv *priv,
 
899
                                const u8 *addr, int is_ap)
 
900
{
 
901
        u8 sta_id;
 
902
 
 
903
        /* Add station to device's station table */
 
904
        sta_id = iwl4965_add_station_flags(priv, addr, is_ap, 0);
 
905
 
 
906
        /* Set up default rate scaling table in device's station table */
 
907
        iwl4965_add_station(priv, addr, is_ap);
 
908
 
 
909
        return sta_id;
 
910
}
 
911
 
 
912
/**
 
913
 * iwl4965_set_rxon_channel - Set the phymode and channel values in staging RXON
 
914
 * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
 
915
 * @channel: Any channel valid for the requested phymode
 
916
 
 
917
 * In addition to setting the staging RXON, priv->phymode is also set.
 
918
 *
 
919
 * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
 
920
 * in the staging RXON flag structure based on the phymode
 
921
 */
 
922
static int iwl4965_set_rxon_channel(struct iwl4965_priv *priv, u8 phymode,
 
923
                                 u16 channel)
 
924
{
 
925
        if (!iwl4965_get_channel_info(priv, phymode, channel)) {
 
926
                IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
 
927
                               channel, phymode);
 
928
                return -EINVAL;
 
929
        }
 
930
 
 
931
        if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
 
932
            (priv->phymode == phymode))
 
933
                return 0;
 
934
 
 
935
        priv->staging_rxon.channel = cpu_to_le16(channel);
 
936
        if (phymode == MODE_IEEE80211A)
 
937
                priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
 
938
        else
 
939
                priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
 
940
 
 
941
        priv->phymode = phymode;
 
942
 
 
943
        IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
 
944
 
 
945
        return 0;
 
946
}
 
947
 
 
948
/**
 
949
 * iwl4965_check_rxon_cmd - validate RXON structure is valid
 
950
 *
 
951
 * NOTE:  This is really only useful during development and can eventually
 
952
 * be #ifdef'd out once the driver is stable and folks aren't actively
 
953
 * making changes
 
954
 */
 
955
static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
 
956
{
 
957
        int error = 0;
 
958
        int counter = 1;
 
959
 
 
960
        if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
 
961
                error |= le32_to_cpu(rxon->flags &
 
962
                                (RXON_FLG_TGJ_NARROW_BAND_MSK |
 
963
                                 RXON_FLG_RADAR_DETECT_MSK));
 
964
                if (error)
 
965
                        IWL_WARNING("check 24G fields %d | %d\n",
 
966
                                    counter++, error);
 
967
        } else {
 
968
                error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
 
969
                                0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
 
970
                if (error)
 
971
                        IWL_WARNING("check 52 fields %d | %d\n",
 
972
                                    counter++, error);
 
973
                error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
 
974
                if (error)
 
975
                        IWL_WARNING("check 52 CCK %d | %d\n",
 
976
                                    counter++, error);
 
977
        }
 
978
        error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
 
979
        if (error)
 
980
                IWL_WARNING("check mac addr %d | %d\n", counter++, error);
 
981
 
 
982
        /* make sure basic rates 6Mbps and 1Mbps are supported */
 
983
        error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
 
984
                  ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
 
985
        if (error)
 
986
                IWL_WARNING("check basic rate %d | %d\n", counter++, error);
 
987
 
 
988
        error |= (le16_to_cpu(rxon->assoc_id) > 2007);
 
989
        if (error)
 
990
                IWL_WARNING("check assoc id %d | %d\n", counter++, error);
 
991
 
 
992
        error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
 
993
                        == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
 
994
        if (error)
 
995
                IWL_WARNING("check CCK and short slot %d | %d\n",
 
996
                            counter++, error);
 
997
 
 
998
        error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
 
999
                        == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
 
1000
        if (error)
 
1001
                IWL_WARNING("check CCK & auto detect %d | %d\n",
 
1002
                            counter++, error);
 
1003
 
 
1004
        error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
 
1005
                        RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
 
1006
        if (error)
 
1007
                IWL_WARNING("check TGG and auto detect %d | %d\n",
 
1008
                            counter++, error);
 
1009
 
 
1010
        if (error)
 
1011
                IWL_WARNING("Tuning to channel %d\n",
 
1012
                            le16_to_cpu(rxon->channel));
 
1013
 
 
1014
        if (error) {
 
1015
                IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
 
1016
                return -1;
 
1017
        }
 
1018
        return 0;
 
1019
}
 
1020
 
 
1021
/**
 
1022
 * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
 
1023
 * @priv: staging_rxon is compared to active_rxon
 
1024
 *
 
1025
 * If the RXON structure is changing enough to require a new tune,
 
1026
 * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
 
1027
 * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
 
1028
 */
 
1029
static int iwl4965_full_rxon_required(struct iwl4965_priv *priv)
 
1030
{
 
1031
 
 
1032
        /* These items are only settable from the full RXON command */
 
1033
        if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
 
1034
            compare_ether_addr(priv->staging_rxon.bssid_addr,
 
1035
                               priv->active_rxon.bssid_addr) ||
 
1036
            compare_ether_addr(priv->staging_rxon.node_addr,
 
1037
                               priv->active_rxon.node_addr) ||
 
1038
            compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
 
1039
                               priv->active_rxon.wlap_bssid_addr) ||
 
1040
            (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
 
1041
            (priv->staging_rxon.channel != priv->active_rxon.channel) ||
 
1042
            (priv->staging_rxon.air_propagation !=
 
1043
             priv->active_rxon.air_propagation) ||
 
1044
            (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
 
1045
             priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
 
1046
            (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
 
1047
             priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
 
1048
            (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
 
1049
            (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
 
1050
                return 1;
 
1051
 
 
1052
        /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
 
1053
         * be updated with the RXON_ASSOC command -- however only some
 
1054
         * flag transitions are allowed using RXON_ASSOC */
 
1055
 
 
1056
        /* Check if we are not switching bands */
 
1057
        if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
 
1058
            (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
 
1059
                return 1;
 
1060
 
 
1061
        /* Check if we are switching association toggle */
 
1062
        if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
 
1063
                (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
 
1064
                return 1;
 
1065
 
 
1066
        return 0;
 
1067
}
 
1068
 
 
1069
static int iwl4965_send_rxon_assoc(struct iwl4965_priv *priv)
 
1070
{
 
1071
        int rc = 0;
 
1072
        struct iwl4965_rx_packet *res = NULL;
 
1073
        struct iwl4965_rxon_assoc_cmd rxon_assoc;
 
1074
        struct iwl4965_host_cmd cmd = {
 
1075
                .id = REPLY_RXON_ASSOC,
 
1076
                .len = sizeof(rxon_assoc),
 
1077
                .meta.flags = CMD_WANT_SKB,
 
1078
                .data = &rxon_assoc,
 
1079
        };
 
1080
        const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
 
1081
        const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
 
1082
 
 
1083
        if ((rxon1->flags == rxon2->flags) &&
 
1084
            (rxon1->filter_flags == rxon2->filter_flags) &&
 
1085
            (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
 
1086
            (rxon1->ofdm_ht_single_stream_basic_rates ==
 
1087
             rxon2->ofdm_ht_single_stream_basic_rates) &&
 
1088
            (rxon1->ofdm_ht_dual_stream_basic_rates ==
 
1089
             rxon2->ofdm_ht_dual_stream_basic_rates) &&
 
1090
            (rxon1->rx_chain == rxon2->rx_chain) &&
 
1091
            (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
 
1092
                IWL_DEBUG_INFO("Using current RXON_ASSOC.  Not resending.\n");
 
1093
                return 0;
 
1094
        }
 
1095
 
 
1096
        rxon_assoc.flags = priv->staging_rxon.flags;
 
1097
        rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
 
1098
        rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
 
1099
        rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
 
1100
        rxon_assoc.reserved = 0;
 
1101
        rxon_assoc.ofdm_ht_single_stream_basic_rates =
 
1102
            priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
 
1103
        rxon_assoc.ofdm_ht_dual_stream_basic_rates =
 
1104
            priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
 
1105
        rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
 
1106
 
 
1107
        rc = iwl4965_send_cmd_sync(priv, &cmd);
 
1108
        if (rc)
 
1109
                return rc;
 
1110
 
 
1111
        res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
 
1112
        if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
 
1113
                IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
 
1114
                rc = -EIO;
 
1115
        }
 
1116
 
 
1117
        priv->alloc_rxb_skb--;
 
1118
        dev_kfree_skb_any(cmd.meta.u.skb);
 
1119
 
 
1120
        return rc;
 
1121
}
 
1122
 
 
1123
/**
 
1124
 * iwl4965_commit_rxon - commit staging_rxon to hardware
 
1125
 *
 
1126
 * The RXON command in staging_rxon is committed to the hardware and
 
1127
 * the active_rxon structure is updated with the new data.  This
 
1128
 * function correctly transitions out of the RXON_ASSOC_MSK state if
 
1129
 * a HW tune is required based on the RXON structure changes.
 
1130
 */
 
1131
static int iwl4965_commit_rxon(struct iwl4965_priv *priv)
 
1132
{
 
1133
        /* cast away the const for active_rxon in this function */
 
1134
        struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
 
1135
        int rc = 0;
 
1136
 
 
1137
        if (!iwl4965_is_alive(priv))
 
1138
                return -1;
 
1139
 
 
1140
        /* always get timestamp with Rx frame */
 
1141
        priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
 
1142
 
 
1143
        rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
 
1144
        if (rc) {
 
1145
                IWL_ERROR("Invalid RXON configuration.  Not committing.\n");
 
1146
                return -EINVAL;
 
1147
        }
 
1148
 
 
1149
        /* If we don't need to send a full RXON, we can use
 
1150
         * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
 
1151
         * and other flags for the current radio configuration. */
 
1152
        if (!iwl4965_full_rxon_required(priv)) {
 
1153
                rc = iwl4965_send_rxon_assoc(priv);
 
1154
                if (rc) {
 
1155
                        IWL_ERROR("Error setting RXON_ASSOC "
 
1156
                                  "configuration (%d).\n", rc);
 
1157
                        return rc;
 
1158
                }
 
1159
 
 
1160
                memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
 
1161
 
 
1162
                return 0;
 
1163
        }
 
1164
 
 
1165
        /* station table will be cleared */
 
1166
        priv->assoc_station_added = 0;
 
1167
 
 
1168
#ifdef CONFIG_IWL4965_SENSITIVITY
 
1169
        priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
 
1170
        if (!priv->error_recovering)
 
1171
                priv->start_calib = 0;
 
1172
 
 
1173
        iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
 
1174
#endif /* CONFIG_IWL4965_SENSITIVITY */
 
1175
 
 
1176
        /* If we are currently associated and the new config requires
 
1177
         * an RXON_ASSOC and the new config wants the associated mask enabled,
 
1178
         * we must clear the associated from the active configuration
 
1179
         * before we apply the new config */
 
1180
        if (iwl4965_is_associated(priv) &&
 
1181
            (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
 
1182
                IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
 
1183
                active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 
1184
 
 
1185
                rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
 
1186
                                      sizeof(struct iwl4965_rxon_cmd),
 
1187
                                      &priv->active_rxon);
 
1188
 
 
1189
                /* If the mask clearing failed then we set
 
1190
                 * active_rxon back to what it was previously */
 
1191
                if (rc) {
 
1192
                        active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
 
1193
                        IWL_ERROR("Error clearing ASSOC_MSK on current "
 
1194
                                  "configuration (%d).\n", rc);
 
1195
                        return rc;
 
1196
                }
 
1197
        }
 
1198
 
 
1199
        IWL_DEBUG_INFO("Sending RXON\n"
 
1200
                       "* with%s RXON_FILTER_ASSOC_MSK\n"
 
1201
                       "* channel = %d\n"
 
1202
                       "* bssid = " MAC_FMT "\n",
 
1203
                       ((priv->staging_rxon.filter_flags &
 
1204
                         RXON_FILTER_ASSOC_MSK) ? "" : "out"),
 
1205
                       le16_to_cpu(priv->staging_rxon.channel),
 
1206
                       MAC_ARG(priv->staging_rxon.bssid_addr));
 
1207
 
 
1208
        /* Apply the new configuration */
 
1209
        rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
 
1210
                              sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
 
1211
        if (rc) {
 
1212
                IWL_ERROR("Error setting new configuration (%d).\n", rc);
 
1213
                return rc;
 
1214
        }
 
1215
 
 
1216
        iwl4965_clear_stations_table(priv);
 
1217
 
 
1218
#ifdef CONFIG_IWL4965_SENSITIVITY
 
1219
        if (!priv->error_recovering)
 
1220
                priv->start_calib = 0;
 
1221
 
 
1222
        priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
 
1223
        iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
 
1224
#endif /* CONFIG_IWL4965_SENSITIVITY */
 
1225
 
 
1226
        memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
 
1227
 
 
1228
        /* If we issue a new RXON command which required a tune then we must
 
1229
         * send a new TXPOWER command or we won't be able to Tx any frames */
 
1230
        rc = iwl4965_hw_reg_send_txpower(priv);
 
1231
        if (rc) {
 
1232
                IWL_ERROR("Error setting Tx power (%d).\n", rc);
 
1233
                return rc;
 
1234
        }
 
1235
 
 
1236
        /* Add the broadcast address so we can send broadcast frames */
 
1237
        if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
 
1238
            IWL_INVALID_STATION) {
 
1239
                IWL_ERROR("Error adding BROADCAST address for transmit.\n");
 
1240
                return -EIO;
 
1241
        }
 
1242
 
 
1243
        /* If we have set the ASSOC_MSK and we are in BSS mode then
 
1244
         * add the IWL_AP_ID to the station rate table */
 
1245
        if (iwl4965_is_associated(priv) &&
 
1246
            (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
 
1247
                if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
 
1248
                    == IWL_INVALID_STATION) {
 
1249
                        IWL_ERROR("Error adding AP address for transmit.\n");
 
1250
                        return -EIO;
 
1251
                }
 
1252
                priv->assoc_station_added = 1;
 
1253
        }
 
1254
 
 
1255
        return 0;
 
1256
}
 
1257
 
 
1258
static int iwl4965_send_bt_config(struct iwl4965_priv *priv)
 
1259
{
 
1260
        struct iwl4965_bt_cmd bt_cmd = {
 
1261
                .flags = 3,
 
1262
                .lead_time = 0xAA,
 
1263
                .max_kill = 1,
 
1264
                .kill_ack_mask = 0,
 
1265
                .kill_cts_mask = 0,
 
1266
        };
 
1267
 
 
1268
        return iwl4965_send_cmd_pdu(priv, REPLY_BT_CONFIG,
 
1269
                                sizeof(struct iwl4965_bt_cmd), &bt_cmd);
 
1270
}
 
1271
 
 
1272
static int iwl4965_send_scan_abort(struct iwl4965_priv *priv)
 
1273
{
 
1274
        int rc = 0;
 
1275
        struct iwl4965_rx_packet *res;
 
1276
        struct iwl4965_host_cmd cmd = {
 
1277
                .id = REPLY_SCAN_ABORT_CMD,
 
1278
                .meta.flags = CMD_WANT_SKB,
 
1279
        };
 
1280
 
 
1281
        /* If there isn't a scan actively going on in the hardware
 
1282
         * then we are in between scan bands and not actually
 
1283
         * actively scanning, so don't send the abort command */
 
1284
        if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
 
1285
                clear_bit(STATUS_SCAN_ABORTING, &priv->status);
 
1286
                return 0;
 
1287
        }
 
1288
 
 
1289
        rc = iwl4965_send_cmd_sync(priv, &cmd);
 
1290
        if (rc) {
 
1291
                clear_bit(STATUS_SCAN_ABORTING, &priv->status);
 
1292
                return rc;
 
1293
        }
 
1294
 
 
1295
        res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
 
1296
        if (res->u.status != CAN_ABORT_STATUS) {
 
1297
                /* The scan abort will return 1 for success or
 
1298
                 * 2 for "failure".  A failure condition can be
 
1299
                 * due to simply not being in an active scan which
 
1300
                 * can occur if we send the scan abort before we
 
1301
                 * the microcode has notified us that a scan is
 
1302
                 * completed. */
 
1303
                IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
 
1304
                clear_bit(STATUS_SCAN_ABORTING, &priv->status);
 
1305
                clear_bit(STATUS_SCAN_HW, &priv->status);
 
1306
        }
 
1307
 
 
1308
        dev_kfree_skb_any(cmd.meta.u.skb);
 
1309
 
 
1310
        return rc;
 
1311
}
 
1312
 
 
1313
static int iwl4965_card_state_sync_callback(struct iwl4965_priv *priv,
 
1314
                                        struct iwl4965_cmd *cmd,
 
1315
                                        struct sk_buff *skb)
 
1316
{
 
1317
        return 1;
 
1318
}
 
1319
 
 
1320
/*
 
1321
 * CARD_STATE_CMD
 
1322
 *
 
1323
 * Use: Sets the device's internal card state to enable, disable, or halt
 
1324
 *
 
1325
 * When in the 'enable' state the card operates as normal.
 
1326
 * When in the 'disable' state, the card enters into a low power mode.
 
1327
 * When in the 'halt' state, the card is shut down and must be fully
 
1328
 * restarted to come back on.
 
1329
 */
 
1330
static int iwl4965_send_card_state(struct iwl4965_priv *priv, u32 flags, u8 meta_flag)
 
1331
{
 
1332
        struct iwl4965_host_cmd cmd = {
 
1333
                .id = REPLY_CARD_STATE_CMD,
 
1334
                .len = sizeof(u32),
 
1335
                .data = &flags,
 
1336
                .meta.flags = meta_flag,
 
1337
        };
 
1338
 
 
1339
        if (meta_flag & CMD_ASYNC)
 
1340
                cmd.meta.u.callback = iwl4965_card_state_sync_callback;
 
1341
 
 
1342
        return iwl4965_send_cmd(priv, &cmd);
 
1343
}
 
1344
 
 
1345
static int iwl4965_add_sta_sync_callback(struct iwl4965_priv *priv,
 
1346
                                     struct iwl4965_cmd *cmd, struct sk_buff *skb)
 
1347
{
 
1348
        struct iwl4965_rx_packet *res = NULL;
 
1349
 
 
1350
        if (!skb) {
 
1351
                IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
 
1352
                return 1;
 
1353
        }
 
1354
 
 
1355
        res = (struct iwl4965_rx_packet *)skb->data;
 
1356
        if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
 
1357
                IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
 
1358
                          res->hdr.flags);
 
1359
                return 1;
 
1360
        }
 
1361
 
 
1362
        switch (res->u.add_sta.status) {
 
1363
        case ADD_STA_SUCCESS_MSK:
 
1364
                break;
 
1365
        default:
 
1366
                break;
 
1367
        }
 
1368
 
 
1369
        /* We didn't cache the SKB; let the caller free it */
 
1370
        return 1;
 
1371
}
 
1372
 
 
1373
int iwl4965_send_add_station(struct iwl4965_priv *priv,
 
1374
                         struct iwl4965_addsta_cmd *sta, u8 flags)
 
1375
{
 
1376
        struct iwl4965_rx_packet *res = NULL;
 
1377
        int rc = 0;
 
1378
        struct iwl4965_host_cmd cmd = {
 
1379
                .id = REPLY_ADD_STA,
 
1380
                .len = sizeof(struct iwl4965_addsta_cmd),
 
1381
                .meta.flags = flags,
 
1382
                .data = sta,
 
1383
        };
 
1384
 
 
1385
        if (flags & CMD_ASYNC)
 
1386
                cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
 
1387
        else
 
1388
                cmd.meta.flags |= CMD_WANT_SKB;
 
1389
 
 
1390
        rc = iwl4965_send_cmd(priv, &cmd);
 
1391
 
 
1392
        if (rc || (flags & CMD_ASYNC))
 
1393
                return rc;
 
1394
 
 
1395
        res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
 
1396
        if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
 
1397
                IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
 
1398
                          res->hdr.flags);
 
1399
                rc = -EIO;
 
1400
        }
 
1401
 
 
1402
        if (rc == 0) {
 
1403
                switch (res->u.add_sta.status) {
 
1404
                case ADD_STA_SUCCESS_MSK:
 
1405
                        IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
 
1406
                        break;
 
1407
                default:
 
1408
                        rc = -EIO;
 
1409
                        IWL_WARNING("REPLY_ADD_STA failed\n");
 
1410
                        break;
 
1411
                }
 
1412
        }
 
1413
 
 
1414
        priv->alloc_rxb_skb--;
 
1415
        dev_kfree_skb_any(cmd.meta.u.skb);
 
1416
 
 
1417
        return rc;
 
1418
}
 
1419
 
 
1420
static int iwl4965_update_sta_key_info(struct iwl4965_priv *priv,
 
1421
                                   struct ieee80211_key_conf *keyconf,
 
1422
                                   u8 sta_id)
 
1423
{
 
1424
        unsigned long flags;
 
1425
        __le16 key_flags = 0;
 
1426
 
 
1427
        switch (keyconf->alg) {
 
1428
        case ALG_CCMP:
 
1429
                key_flags |= STA_KEY_FLG_CCMP;
 
1430
                key_flags |= cpu_to_le16(
 
1431
                                keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
 
1432
                key_flags &= ~STA_KEY_FLG_INVALID;
 
1433
                break;
 
1434
        case ALG_TKIP:
 
1435
        case ALG_WEP:
 
1436
        default:
 
1437
                return -EINVAL;
 
1438
        }
 
1439
        spin_lock_irqsave(&priv->sta_lock, flags);
 
1440
        priv->stations[sta_id].keyinfo.alg = keyconf->alg;
 
1441
        priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
 
1442
        memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
 
1443
               keyconf->keylen);
 
1444
 
 
1445
        memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
 
1446
               keyconf->keylen);
 
1447
        priv->stations[sta_id].sta.key.key_flags = key_flags;
 
1448
        priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
 
1449
        priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
 
1450
 
 
1451
        spin_unlock_irqrestore(&priv->sta_lock, flags);
 
1452
 
 
1453
        IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
 
1454
        iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
 
1455
        return 0;
 
1456
}
 
1457
 
 
1458
static int iwl4965_clear_sta_key_info(struct iwl4965_priv *priv, u8 sta_id)
 
1459
{
 
1460
        unsigned long flags;
 
1461
 
 
1462
        spin_lock_irqsave(&priv->sta_lock, flags);
 
1463
        memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
 
1464
        memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
 
1465
        priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
 
1466
        priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
 
1467
        priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
 
1468
        spin_unlock_irqrestore(&priv->sta_lock, flags);
 
1469
 
 
1470
        IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
 
1471
        iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
 
1472
        return 0;
 
1473
}
 
1474
 
 
1475
static void iwl4965_clear_free_frames(struct iwl4965_priv *priv)
 
1476
{
 
1477
        struct list_head *element;
 
1478
 
 
1479
        IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
 
1480
                       priv->frames_count);
 
1481
 
 
1482
        while (!list_empty(&priv->free_frames)) {
 
1483
                element = priv->free_frames.next;
 
1484
                list_del(element);
 
1485
                kfree(list_entry(element, struct iwl4965_frame, list));
 
1486
                priv->frames_count--;
 
1487
        }
 
1488
 
 
1489
        if (priv->frames_count) {
 
1490
                IWL_WARNING("%d frames still in use.  Did we lose one?\n",
 
1491
                            priv->frames_count);
 
1492
                priv->frames_count = 0;
 
1493
        }
 
1494
}
 
1495
 
 
1496
static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl4965_priv *priv)
 
1497
{
 
1498
        struct iwl4965_frame *frame;
 
1499
        struct list_head *element;
 
1500
        if (list_empty(&priv->free_frames)) {
 
1501
                frame = kzalloc(sizeof(*frame), GFP_KERNEL);
 
1502
                if (!frame) {
 
1503
                        IWL_ERROR("Could not allocate frame!\n");
 
1504
                        return NULL;
 
1505
                }
 
1506
 
 
1507
                priv->frames_count++;
 
1508
                return frame;
 
1509
        }
 
1510
 
 
1511
        element = priv->free_frames.next;
 
1512
        list_del(element);
 
1513
        return list_entry(element, struct iwl4965_frame, list);
 
1514
}
 
1515
 
 
1516
static void iwl4965_free_frame(struct iwl4965_priv *priv, struct iwl4965_frame *frame)
 
1517
{
 
1518
        memset(frame, 0, sizeof(*frame));
 
1519
        list_add(&frame->list, &priv->free_frames);
 
1520
}
 
1521
 
 
1522
unsigned int iwl4965_fill_beacon_frame(struct iwl4965_priv *priv,
 
1523
                                struct ieee80211_hdr *hdr,
 
1524
                                const u8 *dest, int left)
 
1525
{
 
1526
 
 
1527
        if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
 
1528
            ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
 
1529
             (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
 
1530
                return 0;
 
1531
 
 
1532
        if (priv->ibss_beacon->len > left)
 
1533
                return 0;
 
1534
 
 
1535
        memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
 
1536
 
 
1537
        return priv->ibss_beacon->len;
 
1538
}
 
1539
 
 
1540
int iwl4965_rate_index_from_plcp(int plcp)
 
1541
{
 
1542
        int i = 0;
 
1543
 
 
1544
        /* 4965 HT rate format */
 
1545
        if (plcp & RATE_MCS_HT_MSK) {
 
1546
                i = (plcp & 0xff);
 
1547
 
 
1548
                if (i >= IWL_RATE_MIMO_6M_PLCP)
 
1549
                        i = i - IWL_RATE_MIMO_6M_PLCP;
 
1550
 
 
1551
                i += IWL_FIRST_OFDM_RATE;
 
1552
                /* skip 9M not supported in ht*/
 
1553
                if (i >= IWL_RATE_9M_INDEX)
 
1554
                        i += 1;
 
1555
                if ((i >= IWL_FIRST_OFDM_RATE) &&
 
1556
                    (i <= IWL_LAST_OFDM_RATE))
 
1557
                        return i;
 
1558
 
 
1559
        /* 4965 legacy rate format, search for match in table */
 
1560
        } else {
 
1561
                for (i = 0; i < ARRAY_SIZE(iwl4965_rates); i++)
 
1562
                        if (iwl4965_rates[i].plcp == (plcp &0xFF))
 
1563
                                return i;
 
1564
        }
 
1565
        return -1;
 
1566
}
 
1567
 
 
1568
static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
 
1569
{
 
1570
        u8 i;
 
1571
 
 
1572
        for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
 
1573
             i = iwl4965_rates[i].next_ieee) {
 
1574
                if (rate_mask & (1 << i))
 
1575
                        return iwl4965_rates[i].plcp;
 
1576
        }
 
1577
 
 
1578
        return IWL_RATE_INVALID;
 
1579
}
 
1580
 
 
1581
static int iwl4965_send_beacon_cmd(struct iwl4965_priv *priv)
 
1582
{
 
1583
        struct iwl4965_frame *frame;
 
1584
        unsigned int frame_size;
 
1585
        int rc;
 
1586
        u8 rate;
 
1587
 
 
1588
        frame = iwl4965_get_free_frame(priv);
 
1589
 
 
1590
        if (!frame) {
 
1591
                IWL_ERROR("Could not obtain free frame buffer for beacon "
 
1592
                          "command.\n");
 
1593
                return -ENOMEM;
 
1594
        }
 
1595
 
 
1596
        if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
 
1597
                rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
 
1598
                                                0xFF0);
 
1599
                if (rate == IWL_INVALID_RATE)
 
1600
                        rate = IWL_RATE_6M_PLCP;
 
1601
        } else {
 
1602
                rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
 
1603
                if (rate == IWL_INVALID_RATE)
 
1604
                        rate = IWL_RATE_1M_PLCP;
 
1605
        }
 
1606
 
 
1607
        frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
 
1608
 
 
1609
        rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
 
1610
                              &frame->u.cmd[0]);
 
1611
 
 
1612
        iwl4965_free_frame(priv, frame);
 
1613
 
 
1614
        return rc;
 
1615
}
 
1616
 
 
1617
/******************************************************************************
 
1618
 *
 
1619
 * EEPROM related functions
 
1620
 *
 
1621
 ******************************************************************************/
 
1622
 
 
1623
static void get_eeprom_mac(struct iwl4965_priv *priv, u8 *mac)
 
1624
{
 
1625
        memcpy(mac, priv->eeprom.mac_address, 6);
 
1626
}
 
1627
 
 
1628
/**
 
1629
 * iwl4965_eeprom_init - read EEPROM contents
 
1630
 *
 
1631
 * Load the EEPROM contents from adapter into priv->eeprom
 
1632
 *
 
1633
 * NOTE:  This routine uses the non-debug IO access functions.
 
1634
 */
 
1635
int iwl4965_eeprom_init(struct iwl4965_priv *priv)
 
1636
{
 
1637
        u16 *e = (u16 *)&priv->eeprom;
 
1638
        u32 gp = iwl4965_read32(priv, CSR_EEPROM_GP);
 
1639
        u32 r;
 
1640
        int sz = sizeof(priv->eeprom);
 
1641
        int rc;
 
1642
        int i;
 
1643
        u16 addr;
 
1644
 
 
1645
        /* The EEPROM structure has several padding buffers within it
 
1646
         * and when adding new EEPROM maps is subject to programmer errors
 
1647
         * which may be very difficult to identify without explicitly
 
1648
         * checking the resulting size of the eeprom map. */
 
1649
        BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
 
1650
 
 
1651
        if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
 
1652
                IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
 
1653
                return -ENOENT;
 
1654
        }
 
1655
 
 
1656
        /* Make sure driver (instead of uCode) is allowed to read EEPROM */
 
1657
        rc = iwl4965_eeprom_acquire_semaphore(priv);
 
1658
        if (rc < 0) {
 
1659
                IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
 
1660
                return -ENOENT;
 
1661
        }
 
1662
 
 
1663
        /* eeprom is an array of 16bit values */
 
1664
        for (addr = 0; addr < sz; addr += sizeof(u16)) {
 
1665
                _iwl4965_write32(priv, CSR_EEPROM_REG, addr << 1);
 
1666
                _iwl4965_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
 
1667
 
 
1668
                for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
 
1669
                                        i += IWL_EEPROM_ACCESS_DELAY) {
 
1670
                        r = _iwl4965_read_direct32(priv, CSR_EEPROM_REG);
 
1671
                        if (r & CSR_EEPROM_REG_READ_VALID_MSK)
 
1672
                                break;
 
1673
                        udelay(IWL_EEPROM_ACCESS_DELAY);
 
1674
                }
 
1675
 
 
1676
                if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
 
1677
                        IWL_ERROR("Time out reading EEPROM[%d]", addr);
 
1678
                        rc = -ETIMEDOUT;
 
1679
                        goto done;
 
1680
                }
 
1681
                e[addr / 2] = le16_to_cpu(r >> 16);
 
1682
        }
 
1683
        rc = 0;
 
1684
 
 
1685
done:
 
1686
        iwl4965_eeprom_release_semaphore(priv);
 
1687
        return rc;
 
1688
}
 
1689
 
 
1690
/******************************************************************************
 
1691
 *
 
1692
 * Misc. internal state and helper functions
 
1693
 *
 
1694
 ******************************************************************************/
 
1695
#ifdef CONFIG_IWL4965_DEBUG
 
1696
 
 
1697
/**
 
1698
 * iwl4965_report_frame - dump frame to syslog during debug sessions
 
1699
 *
 
1700
 * You may hack this function to show different aspects of received frames,
 
1701
 * including selective frame dumps.
 
1702
 * group100 parameter selects whether to show 1 out of 100 good frames.
 
1703
 *
 
1704
 * TODO:  This was originally written for 3945, need to audit for
 
1705
 *        proper operation with 4965.
 
1706
 */
 
1707
void iwl4965_report_frame(struct iwl4965_priv *priv,
 
1708
                      struct iwl4965_rx_packet *pkt,
 
1709
                      struct ieee80211_hdr *header, int group100)
 
1710
{
 
1711
        u32 to_us;
 
1712
        u32 print_summary = 0;
 
1713
        u32 print_dump = 0;     /* set to 1 to dump all frames' contents */
 
1714
        u32 hundred = 0;
 
1715
        u32 dataframe = 0;
 
1716
        u16 fc;
 
1717
        u16 seq_ctl;
 
1718
        u16 channel;
 
1719
        u16 phy_flags;
 
1720
        int rate_sym;
 
1721
        u16 length;
 
1722
        u16 status;
 
1723
        u16 bcn_tmr;
 
1724
        u32 tsf_low;
 
1725
        u64 tsf;
 
1726
        u8 rssi;
 
1727
        u8 agc;
 
1728
        u16 sig_avg;
 
1729
        u16 noise_diff;
 
1730
        struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
 
1731
        struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
 
1732
        struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
 
1733
        u8 *data = IWL_RX_DATA(pkt);
 
1734
 
 
1735
        /* MAC header */
 
1736
        fc = le16_to_cpu(header->frame_control);
 
1737
        seq_ctl = le16_to_cpu(header->seq_ctrl);
 
1738
 
 
1739
        /* metadata */
 
1740
        channel = le16_to_cpu(rx_hdr->channel);
 
1741
        phy_flags = le16_to_cpu(rx_hdr->phy_flags);
 
1742
        rate_sym = rx_hdr->rate;
 
1743
        length = le16_to_cpu(rx_hdr->len);
 
1744
 
 
1745
        /* end-of-frame status and timestamp */
 
1746
        status = le32_to_cpu(rx_end->status);
 
1747
        bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
 
1748
        tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
 
1749
        tsf = le64_to_cpu(rx_end->timestamp);
 
1750
 
 
1751
        /* signal statistics */
 
1752
        rssi = rx_stats->rssi;
 
1753
        agc = rx_stats->agc;
 
1754
        sig_avg = le16_to_cpu(rx_stats->sig_avg);
 
1755
        noise_diff = le16_to_cpu(rx_stats->noise_diff);
 
1756
 
 
1757
        to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
 
1758
 
 
1759
        /* if data frame is to us and all is good,
 
1760
         *   (optionally) print summary for only 1 out of every 100 */
 
1761
        if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
 
1762
            (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
 
1763
                dataframe = 1;
 
1764
                if (!group100)
 
1765
                        print_summary = 1;      /* print each frame */
 
1766
                else if (priv->framecnt_to_us < 100) {
 
1767
                        priv->framecnt_to_us++;
 
1768
                        print_summary = 0;
 
1769
                } else {
 
1770
                        priv->framecnt_to_us = 0;
 
1771
                        print_summary = 1;
 
1772
                        hundred = 1;
 
1773
                }
 
1774
        } else {
 
1775
                /* print summary for all other frames */
 
1776
                print_summary = 1;
 
1777
        }
 
1778
 
 
1779
        if (print_summary) {
 
1780
                char *title;
 
1781
                u32 rate;
 
1782
 
 
1783
                if (hundred)
 
1784
                        title = "100Frames";
 
1785
                else if (fc & IEEE80211_FCTL_RETRY)
 
1786
                        title = "Retry";
 
1787
                else if (ieee80211_is_assoc_response(fc))
 
1788
                        title = "AscRsp";
 
1789
                else if (ieee80211_is_reassoc_response(fc))
 
1790
                        title = "RasRsp";
 
1791
                else if (ieee80211_is_probe_response(fc)) {
 
1792
                        title = "PrbRsp";
 
1793
                        print_dump = 1; /* dump frame contents */
 
1794
                } else if (ieee80211_is_beacon(fc)) {
 
1795
                        title = "Beacon";
 
1796
                        print_dump = 1; /* dump frame contents */
 
1797
                } else if (ieee80211_is_atim(fc))
 
1798
                        title = "ATIM";
 
1799
                else if (ieee80211_is_auth(fc))
 
1800
                        title = "Auth";
 
1801
                else if (ieee80211_is_deauth(fc))
 
1802
                        title = "DeAuth";
 
1803
                else if (ieee80211_is_disassoc(fc))
 
1804
                        title = "DisAssoc";
 
1805
                else
 
1806
                        title = "Frame";
 
1807
 
 
1808
                rate = iwl4965_rate_index_from_plcp(rate_sym);
 
1809
                if (rate == -1)
 
1810
                        rate = 0;
 
1811
                else
 
1812
                        rate = iwl4965_rates[rate].ieee / 2;
 
1813
 
 
1814
                /* print frame summary.
 
1815
                 * MAC addresses show just the last byte (for brevity),
 
1816
                 *    but you can hack it to show more, if you'd like to. */
 
1817
                if (dataframe)
 
1818
                        IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
 
1819
                                     "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
 
1820
                                     title, fc, header->addr1[5],
 
1821
                                     length, rssi, channel, rate);
 
1822
                else {
 
1823
                        /* src/dst addresses assume managed mode */
 
1824
                        IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
 
1825
                                     "src=0x%02x, rssi=%u, tim=%lu usec, "
 
1826
                                     "phy=0x%02x, chnl=%d\n",
 
1827
                                     title, fc, header->addr1[5],
 
1828
                                     header->addr3[5], rssi,
 
1829
                                     tsf_low - priv->scan_start_tsf,
 
1830
                                     phy_flags, channel);
 
1831
                }
 
1832
        }
 
1833
        if (print_dump)
 
1834
                iwl4965_print_hex_dump(IWL_DL_RX, data, length);
 
1835
}
 
1836
#endif
 
1837
 
 
1838
static void iwl4965_unset_hw_setting(struct iwl4965_priv *priv)
 
1839
{
 
1840
        if (priv->hw_setting.shared_virt)
 
1841
                pci_free_consistent(priv->pci_dev,
 
1842
                                    sizeof(struct iwl4965_shared),
 
1843
                                    priv->hw_setting.shared_virt,
 
1844
                                    priv->hw_setting.shared_phys);
 
1845
}
 
1846
 
 
1847
/**
 
1848
 * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
 
1849
 *
 
1850
 * return : set the bit for each supported rate insert in ie
 
1851
 */
 
1852
static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
 
1853
                                    u16 basic_rate, int *left)
 
1854
{
 
1855
        u16 ret_rates = 0, bit;
 
1856
        int i;
 
1857
        u8 *cnt = ie;
 
1858
        u8 *rates = ie + 1;
 
1859
 
 
1860
        for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
 
1861
                if (bit & supported_rate) {
 
1862
                        ret_rates |= bit;
 
1863
                        rates[*cnt] = iwl4965_rates[i].ieee |
 
1864
                                ((bit & basic_rate) ? 0x80 : 0x00);
 
1865
                        (*cnt)++;
 
1866
                        (*left)--;
 
1867
                        if ((*left <= 0) ||
 
1868
                            (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
 
1869
                                break;
 
1870
                }
 
1871
        }
 
1872
 
 
1873
        return ret_rates;
 
1874
}
 
1875
 
 
1876
#ifdef CONFIG_IWL4965_HT
 
1877
void static iwl4965_set_ht_capab(struct ieee80211_hw *hw,
 
1878
                             struct ieee80211_ht_capability *ht_cap,
 
1879
                             u8 use_wide_chan);
 
1880
#endif
 
1881
 
 
1882
/**
 
1883
 * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
 
1884
 */
 
1885
static u16 iwl4965_fill_probe_req(struct iwl4965_priv *priv,
 
1886
                              struct ieee80211_mgmt *frame,
 
1887
                              int left, int is_direct)
 
1888
{
 
1889
        int len = 0;
 
1890
        u8 *pos = NULL;
 
1891
        u16 active_rates, ret_rates, cck_rates, active_rate_basic;
 
1892
 
 
1893
        /* Make sure there is enough space for the probe request,
 
1894
         * two mandatory IEs and the data */
 
1895
        left -= 24;
 
1896
        if (left < 0)
 
1897
                return 0;
 
1898
        len += 24;
 
1899
 
 
1900
        frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
 
1901
        memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
 
1902
        memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
 
1903
        memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
 
1904
        frame->seq_ctrl = 0;
 
1905
 
 
1906
        /* fill in our indirect SSID IE */
 
1907
        /* ...next IE... */
 
1908
 
 
1909
        left -= 2;
 
1910
        if (left < 0)
 
1911
                return 0;
 
1912
        len += 2;
 
1913
        pos = &(frame->u.probe_req.variable[0]);
 
1914
        *pos++ = WLAN_EID_SSID;
 
1915
        *pos++ = 0;
 
1916
 
 
1917
        /* fill in our direct SSID IE... */
 
1918
        if (is_direct) {
 
1919
                /* ...next IE... */
 
1920
                left -= 2 + priv->essid_len;
 
1921
                if (left < 0)
 
1922
                        return 0;
 
1923
                /* ... fill it in... */
 
1924
                *pos++ = WLAN_EID_SSID;
 
1925
                *pos++ = priv->essid_len;
 
1926
                memcpy(pos, priv->essid, priv->essid_len);
 
1927
                pos += priv->essid_len;
 
1928
                len += 2 + priv->essid_len;
 
1929
        }
 
1930
 
 
1931
        /* fill in supported rate */
 
1932
        /* ...next IE... */
 
1933
        left -= 2;
 
1934
        if (left < 0)
 
1935
                return 0;
 
1936
 
 
1937
        /* ... fill it in... */
 
1938
        *pos++ = WLAN_EID_SUPP_RATES;
 
1939
        *pos = 0;
 
1940
 
 
1941
        /* exclude 60M rate */
 
1942
        active_rates = priv->rates_mask;
 
1943
        active_rates &= ~IWL_RATE_60M_MASK;
 
1944
 
 
1945
        active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
 
1946
 
 
1947
        cck_rates = IWL_CCK_RATES_MASK & active_rates;
 
1948
        ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
 
1949
                        active_rate_basic, &left);
 
1950
        active_rates &= ~ret_rates;
 
1951
 
 
1952
        ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
 
1953
                                 active_rate_basic, &left);
 
1954
        active_rates &= ~ret_rates;
 
1955
 
 
1956
        len += 2 + *pos;
 
1957
        pos += (*pos) + 1;
 
1958
        if (active_rates == 0)
 
1959
                goto fill_end;
 
1960
 
 
1961
        /* fill in supported extended rate */
 
1962
        /* ...next IE... */
 
1963
        left -= 2;
 
1964
        if (left < 0)
 
1965
                return 0;
 
1966
        /* ... fill it in... */
 
1967
        *pos++ = WLAN_EID_EXT_SUPP_RATES;
 
1968
        *pos = 0;
 
1969
        iwl4965_supported_rate_to_ie(pos, active_rates,
 
1970
                                 active_rate_basic, &left);
 
1971
        if (*pos > 0)
 
1972
                len += 2 + *pos;
 
1973
 
 
1974
#ifdef CONFIG_IWL4965_HT
 
1975
        if (is_direct && priv->is_ht_enabled) {
 
1976
                u8 use_wide_chan = 1;
 
1977
 
 
1978
                if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
 
1979
                        use_wide_chan = 0;
 
1980
                pos += (*pos) + 1;
 
1981
                *pos++ = WLAN_EID_HT_CAPABILITY;
 
1982
                *pos++ = sizeof(struct ieee80211_ht_capability);
 
1983
                iwl4965_set_ht_capab(NULL, (struct ieee80211_ht_capability *)pos,
 
1984
                                 use_wide_chan);
 
1985
                len += 2 + sizeof(struct ieee80211_ht_capability);
 
1986
        }
 
1987
#endif  /*CONFIG_IWL4965_HT */
 
1988
 
 
1989
 fill_end:
 
1990
        return (u16)len;
 
1991
}
 
1992
 
 
1993
/*
 
1994
 * QoS  support
 
1995
*/
 
1996
#ifdef CONFIG_IWL4965_QOS
 
1997
static int iwl4965_send_qos_params_command(struct iwl4965_priv *priv,
 
1998
                                       struct iwl4965_qosparam_cmd *qos)
 
1999
{
 
2000
 
 
2001
        return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
 
2002
                                sizeof(struct iwl4965_qosparam_cmd), qos);
 
2003
}
 
2004
 
 
2005
static void iwl4965_reset_qos(struct iwl4965_priv *priv)
 
2006
{
 
2007
        u16 cw_min = 15;
 
2008
        u16 cw_max = 1023;
 
2009
        u8 aifs = 2;
 
2010
        u8 is_legacy = 0;
 
2011
        unsigned long flags;
 
2012
        int i;
 
2013
 
 
2014
        spin_lock_irqsave(&priv->lock, flags);
 
2015
        priv->qos_data.qos_active = 0;
 
2016
 
 
2017
        if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
 
2018
                if (priv->qos_data.qos_enable)
 
2019
                        priv->qos_data.qos_active = 1;
 
2020
                if (!(priv->active_rate & 0xfff0)) {
 
2021
                        cw_min = 31;
 
2022
                        is_legacy = 1;
 
2023
                }
 
2024
        } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
 
2025
                if (priv->qos_data.qos_enable)
 
2026
                        priv->qos_data.qos_active = 1;
 
2027
        } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
 
2028
                cw_min = 31;
 
2029
                is_legacy = 1;
 
2030
        }
 
2031
 
 
2032
        if (priv->qos_data.qos_active)
 
2033
                aifs = 3;
 
2034
 
 
2035
        priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
 
2036
        priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
 
2037
        priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
 
2038
        priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
 
2039
        priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
 
2040
 
 
2041
        if (priv->qos_data.qos_active) {
 
2042
                i = 1;
 
2043
                priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
 
2044
                priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
 
2045
                priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
 
2046
                priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
 
2047
                priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
 
2048
 
 
2049
                i = 2;
 
2050
                priv->qos_data.def_qos_parm.ac[i].cw_min =
 
2051
                        cpu_to_le16((cw_min + 1) / 2 - 1);
 
2052
                priv->qos_data.def_qos_parm.ac[i].cw_max =
 
2053
                        cpu_to_le16(cw_max);
 
2054
                priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
 
2055
                if (is_legacy)
 
2056
                        priv->qos_data.def_qos_parm.ac[i].edca_txop =
 
2057
                                cpu_to_le16(6016);
 
2058
                else
 
2059
                        priv->qos_data.def_qos_parm.ac[i].edca_txop =
 
2060
                                cpu_to_le16(3008);
 
2061
                priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
 
2062
 
 
2063
                i = 3;
 
2064
                priv->qos_data.def_qos_parm.ac[i].cw_min =
 
2065
                        cpu_to_le16((cw_min + 1) / 4 - 1);
 
2066
                priv->qos_data.def_qos_parm.ac[i].cw_max =
 
2067
                        cpu_to_le16((cw_max + 1) / 2 - 1);
 
2068
                priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
 
2069
                priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
 
2070
                if (is_legacy)
 
2071
                        priv->qos_data.def_qos_parm.ac[i].edca_txop =
 
2072
                                cpu_to_le16(3264);
 
2073
                else
 
2074
                        priv->qos_data.def_qos_parm.ac[i].edca_txop =
 
2075
                                cpu_to_le16(1504);
 
2076
        } else {
 
2077
                for (i = 1; i < 4; i++) {
 
2078
                        priv->qos_data.def_qos_parm.ac[i].cw_min =
 
2079
                                cpu_to_le16(cw_min);
 
2080
                        priv->qos_data.def_qos_parm.ac[i].cw_max =
 
2081
                                cpu_to_le16(cw_max);
 
2082
                        priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
 
2083
                        priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
 
2084
                        priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
 
2085
                }
 
2086
        }
 
2087
        IWL_DEBUG_QOS("set QoS to default \n");
 
2088
 
 
2089
        spin_unlock_irqrestore(&priv->lock, flags);
 
2090
}
 
2091
 
 
2092
static void iwl4965_activate_qos(struct iwl4965_priv *priv, u8 force)
 
2093
{
 
2094
        unsigned long flags;
 
2095
 
 
2096
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 
2097
                return;
 
2098
 
 
2099
        if (!priv->qos_data.qos_enable)
 
2100
                return;
 
2101
 
 
2102
        spin_lock_irqsave(&priv->lock, flags);
 
2103
        priv->qos_data.def_qos_parm.qos_flags = 0;
 
2104
 
 
2105
        if (priv->qos_data.qos_cap.q_AP.queue_request &&
 
2106
            !priv->qos_data.qos_cap.q_AP.txop_request)
 
2107
                priv->qos_data.def_qos_parm.qos_flags |=
 
2108
                        QOS_PARAM_FLG_TXOP_TYPE_MSK;
 
2109
        if (priv->qos_data.qos_active)
 
2110
                priv->qos_data.def_qos_parm.qos_flags |=
 
2111
                        QOS_PARAM_FLG_UPDATE_EDCA_MSK;
 
2112
 
 
2113
#ifdef CONFIG_IWL4965_HT
 
2114
        if (priv->is_ht_enabled && priv->current_assoc_ht.is_ht)
 
2115
                priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
 
2116
#endif /* CONFIG_IWL4965_HT */
 
2117
 
 
2118
        spin_unlock_irqrestore(&priv->lock, flags);
 
2119
 
 
2120
        if (force || iwl4965_is_associated(priv)) {
 
2121
                IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
 
2122
                                priv->qos_data.qos_active,
 
2123
                                priv->qos_data.def_qos_parm.qos_flags);
 
2124
 
 
2125
                iwl4965_send_qos_params_command(priv,
 
2126
                                &(priv->qos_data.def_qos_parm));
 
2127
        }
 
2128
}
 
2129
 
 
2130
#endif /* CONFIG_IWL4965_QOS */
 
2131
/*
 
2132
 * Power management (not Tx power!) functions
 
2133
 */
 
2134
#define MSEC_TO_USEC 1024
 
2135
 
 
2136
#define NOSLP __constant_cpu_to_le16(0), 0, 0
 
2137
#define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
 
2138
#define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
 
2139
#define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
 
2140
                                     __constant_cpu_to_le32(X1), \
 
2141
                                     __constant_cpu_to_le32(X2), \
 
2142
                                     __constant_cpu_to_le32(X3), \
 
2143
                                     __constant_cpu_to_le32(X4)}
 
2144
 
 
2145
 
 
2146
/* default power management (not Tx power) table values */
 
2147
/* for tim  0-10 */
 
2148
static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
 
2149
        {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
 
2150
        {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
 
2151
        {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
 
2152
        {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
 
2153
        {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
 
2154
        {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
 
2155
};
 
2156
 
 
2157
/* for tim > 10 */
 
2158
static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
 
2159
        {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
 
2160
        {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
 
2161
                 SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
 
2162
        {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
 
2163
                 SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
 
2164
        {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
 
2165
                 SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
 
2166
        {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
 
2167
        {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
 
2168
                 SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
 
2169
};
 
2170
 
 
2171
int iwl4965_power_init_handle(struct iwl4965_priv *priv)
 
2172
{
 
2173
        int rc = 0, i;
 
2174
        struct iwl4965_power_mgr *pow_data;
 
2175
        int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
 
2176
        u16 pci_pm;
 
2177
 
 
2178
        IWL_DEBUG_POWER("Initialize power \n");
 
2179
 
 
2180
        pow_data = &(priv->power_data);
 
2181
 
 
2182
        memset(pow_data, 0, sizeof(*pow_data));
 
2183
 
 
2184
        pow_data->active_index = IWL_POWER_RANGE_0;
 
2185
        pow_data->dtim_val = 0xffff;
 
2186
 
 
2187
        memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
 
2188
        memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
 
2189
 
 
2190
        rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
 
2191
        if (rc != 0)
 
2192
                return 0;
 
2193
        else {
 
2194
                struct iwl4965_powertable_cmd *cmd;
 
2195
 
 
2196
                IWL_DEBUG_POWER("adjust power command flags\n");
 
2197
 
 
2198
                for (i = 0; i < IWL_POWER_AC; i++) {
 
2199
                        cmd = &pow_data->pwr_range_0[i].cmd;
 
2200
 
 
2201
                        if (pci_pm & 0x1)
 
2202
                                cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
 
2203
                        else
 
2204
                                cmd->flags |= IWL_POWER_PCI_PM_MSK;
 
2205
                }
 
2206
        }
 
2207
        return rc;
 
2208
}
 
2209
 
 
2210
static int iwl4965_update_power_cmd(struct iwl4965_priv *priv,
 
2211
                                struct iwl4965_powertable_cmd *cmd, u32 mode)
 
2212
{
 
2213
        int rc = 0, i;
 
2214
        u8 skip;
 
2215
        u32 max_sleep = 0;
 
2216
        struct iwl4965_power_vec_entry *range;
 
2217
        u8 period = 0;
 
2218
        struct iwl4965_power_mgr *pow_data;
 
2219
 
 
2220
        if (mode > IWL_POWER_INDEX_5) {
 
2221
                IWL_DEBUG_POWER("Error invalid power mode \n");
 
2222
                return -1;
 
2223
        }
 
2224
        pow_data = &(priv->power_data);
 
2225
 
 
2226
        if (pow_data->active_index == IWL_POWER_RANGE_0)
 
2227
                range = &pow_data->pwr_range_0[0];
 
2228
        else
 
2229
                range = &pow_data->pwr_range_1[1];
 
2230
 
 
2231
        memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
 
2232
 
 
2233
#ifdef IWL_MAC80211_DISABLE
 
2234
        if (priv->assoc_network != NULL) {
 
2235
                unsigned long flags;
 
2236
 
 
2237
                period = priv->assoc_network->tim.tim_period;
 
2238
        }
 
2239
#endif  /*IWL_MAC80211_DISABLE */
 
2240
        skip = range[mode].no_dtim;
 
2241
 
 
2242
        if (period == 0) {
 
2243
                period = 1;
 
2244
                skip = 0;
 
2245
        }
 
2246
 
 
2247
        if (skip == 0) {
 
2248
                max_sleep = period;
 
2249
                cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
 
2250
        } else {
 
2251
                __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
 
2252
                max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
 
2253
                cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
 
2254
        }
 
2255
 
 
2256
        for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
 
2257
                if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
 
2258
                        cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
 
2259
        }
 
2260
 
 
2261
        IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
 
2262
        IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
 
2263
        IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
 
2264
        IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
 
2265
                        le32_to_cpu(cmd->sleep_interval[0]),
 
2266
                        le32_to_cpu(cmd->sleep_interval[1]),
 
2267
                        le32_to_cpu(cmd->sleep_interval[2]),
 
2268
                        le32_to_cpu(cmd->sleep_interval[3]),
 
2269
                        le32_to_cpu(cmd->sleep_interval[4]));
 
2270
 
 
2271
        return rc;
 
2272
}
 
2273
 
 
2274
static int iwl4965_send_power_mode(struct iwl4965_priv *priv, u32 mode)
 
2275
{
 
2276
        u32 final_mode = mode;
 
2277
        int rc;
 
2278
        struct iwl4965_powertable_cmd cmd;
 
2279
 
 
2280
        /* If on battery, set to 3,
 
2281
         * if plugged into AC power, set to CAM ("continuously aware mode"),
 
2282
         * else user level */
 
2283
        switch (mode) {
 
2284
        case IWL_POWER_BATTERY:
 
2285
                final_mode = IWL_POWER_INDEX_3;
 
2286
                break;
 
2287
        case IWL_POWER_AC:
 
2288
                final_mode = IWL_POWER_MODE_CAM;
 
2289
                break;
 
2290
        default:
 
2291
                final_mode = mode;
 
2292
                break;
 
2293
        }
 
2294
 
 
2295
        cmd.keep_alive_beacons = 0;
 
2296
 
 
2297
        iwl4965_update_power_cmd(priv, &cmd, final_mode);
 
2298
 
 
2299
        rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
 
2300
 
 
2301
        if (final_mode == IWL_POWER_MODE_CAM)
 
2302
                clear_bit(STATUS_POWER_PMI, &priv->status);
 
2303
        else
 
2304
                set_bit(STATUS_POWER_PMI, &priv->status);
 
2305
 
 
2306
        return rc;
 
2307
}
 
2308
 
 
2309
int iwl4965_is_network_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
 
2310
{
 
2311
        /* Filter incoming packets to determine if they are targeted toward
 
2312
         * this network, discarding packets coming from ourselves */
 
2313
        switch (priv->iw_mode) {
 
2314
        case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source    | BSSID */
 
2315
                /* packets from our adapter are dropped (echo) */
 
2316
                if (!compare_ether_addr(header->addr2, priv->mac_addr))
 
2317
                        return 0;
 
2318
                /* {broad,multi}cast packets to our IBSS go through */
 
2319
                if (is_multicast_ether_addr(header->addr1))
 
2320
                        return !compare_ether_addr(header->addr3, priv->bssid);
 
2321
                /* packets to our adapter go through */
 
2322
                return !compare_ether_addr(header->addr1, priv->mac_addr);
 
2323
        case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
 
2324
                /* packets from our adapter are dropped (echo) */
 
2325
                if (!compare_ether_addr(header->addr3, priv->mac_addr))
 
2326
                        return 0;
 
2327
                /* {broad,multi}cast packets to our BSS go through */
 
2328
                if (is_multicast_ether_addr(header->addr1))
 
2329
                        return !compare_ether_addr(header->addr2, priv->bssid);
 
2330
                /* packets to our adapter go through */
 
2331
                return !compare_ether_addr(header->addr1, priv->mac_addr);
 
2332
        }
 
2333
 
 
2334
        return 1;
 
2335
}
 
2336
 
 
2337
#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
 
2338
 
 
2339
static const char *iwl4965_get_tx_fail_reason(u32 status)
 
2340
{
 
2341
        switch (status & TX_STATUS_MSK) {
 
2342
        case TX_STATUS_SUCCESS:
 
2343
                return "SUCCESS";
 
2344
                TX_STATUS_ENTRY(SHORT_LIMIT);
 
2345
                TX_STATUS_ENTRY(LONG_LIMIT);
 
2346
                TX_STATUS_ENTRY(FIFO_UNDERRUN);
 
2347
                TX_STATUS_ENTRY(MGMNT_ABORT);
 
2348
                TX_STATUS_ENTRY(NEXT_FRAG);
 
2349
                TX_STATUS_ENTRY(LIFE_EXPIRE);
 
2350
                TX_STATUS_ENTRY(DEST_PS);
 
2351
                TX_STATUS_ENTRY(ABORTED);
 
2352
                TX_STATUS_ENTRY(BT_RETRY);
 
2353
                TX_STATUS_ENTRY(STA_INVALID);
 
2354
                TX_STATUS_ENTRY(FRAG_DROPPED);
 
2355
                TX_STATUS_ENTRY(TID_DISABLE);
 
2356
                TX_STATUS_ENTRY(FRAME_FLUSHED);
 
2357
                TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
 
2358
                TX_STATUS_ENTRY(TX_LOCKED);
 
2359
                TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
 
2360
        }
 
2361
 
 
2362
        return "UNKNOWN";
 
2363
}
 
2364
 
 
2365
/**
 
2366
 * iwl4965_scan_cancel - Cancel any currently executing HW scan
 
2367
 *
 
2368
 * NOTE: priv->mutex is not required before calling this function
 
2369
 */
 
2370
static int iwl4965_scan_cancel(struct iwl4965_priv *priv)
 
2371
{
 
2372
        if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
 
2373
                clear_bit(STATUS_SCANNING, &priv->status);
 
2374
                return 0;
 
2375
        }
 
2376
 
 
2377
        if (test_bit(STATUS_SCANNING, &priv->status)) {
 
2378
                if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
 
2379
                        IWL_DEBUG_SCAN("Queuing scan abort.\n");
 
2380
                        set_bit(STATUS_SCAN_ABORTING, &priv->status);
 
2381
                        queue_work(priv->workqueue, &priv->abort_scan);
 
2382
 
 
2383
                } else
 
2384
                        IWL_DEBUG_SCAN("Scan abort already in progress.\n");
 
2385
 
 
2386
                return test_bit(STATUS_SCANNING, &priv->status);
 
2387
        }
 
2388
 
 
2389
        return 0;
 
2390
}
 
2391
 
 
2392
/**
 
2393
 * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
 
2394
 * @ms: amount of time to wait (in milliseconds) for scan to abort
 
2395
 *
 
2396
 * NOTE: priv->mutex must be held before calling this function
 
2397
 */
 
2398
static int iwl4965_scan_cancel_timeout(struct iwl4965_priv *priv, unsigned long ms)
 
2399
{
 
2400
        unsigned long now = jiffies;
 
2401
        int ret;
 
2402
 
 
2403
        ret = iwl4965_scan_cancel(priv);
 
2404
        if (ret && ms) {
 
2405
                mutex_unlock(&priv->mutex);
 
2406
                while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
 
2407
                                test_bit(STATUS_SCANNING, &priv->status))
 
2408
                        msleep(1);
 
2409
                mutex_lock(&priv->mutex);
 
2410
 
 
2411
                return test_bit(STATUS_SCANNING, &priv->status);
 
2412
        }
 
2413
 
 
2414
        return ret;
 
2415
}
 
2416
 
 
2417
static void iwl4965_sequence_reset(struct iwl4965_priv *priv)
 
2418
{
 
2419
        /* Reset ieee stats */
 
2420
 
 
2421
        /* We don't reset the net_device_stats (ieee->stats) on
 
2422
         * re-association */
 
2423
 
 
2424
        priv->last_seq_num = -1;
 
2425
        priv->last_frag_num = -1;
 
2426
        priv->last_packet_time = 0;
 
2427
 
 
2428
        iwl4965_scan_cancel(priv);
 
2429
}
 
2430
 
 
2431
#define MAX_UCODE_BEACON_INTERVAL       4096
 
2432
#define INTEL_CONN_LISTEN_INTERVAL      __constant_cpu_to_le16(0xA)
 
2433
 
 
2434
static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
 
2435
{
 
2436
        u16 new_val = 0;
 
2437
        u16 beacon_factor = 0;
 
2438
 
 
2439
        beacon_factor =
 
2440
            (beacon_val + MAX_UCODE_BEACON_INTERVAL)
 
2441
                / MAX_UCODE_BEACON_INTERVAL;
 
2442
        new_val = beacon_val / beacon_factor;
 
2443
 
 
2444
        return cpu_to_le16(new_val);
 
2445
}
 
2446
 
 
2447
static void iwl4965_setup_rxon_timing(struct iwl4965_priv *priv)
 
2448
{
 
2449
        u64 interval_tm_unit;
 
2450
        u64 tsf, result;
 
2451
        unsigned long flags;
 
2452
        struct ieee80211_conf *conf = NULL;
 
2453
        u16 beacon_int = 0;
 
2454
 
 
2455
        conf = ieee80211_get_hw_conf(priv->hw);
 
2456
 
 
2457
        spin_lock_irqsave(&priv->lock, flags);
 
2458
        priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
 
2459
        priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
 
2460
 
 
2461
        priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
 
2462
 
 
2463
        tsf = priv->timestamp1;
 
2464
        tsf = ((tsf << 32) | priv->timestamp0);
 
2465
 
 
2466
        beacon_int = priv->beacon_int;
 
2467
        spin_unlock_irqrestore(&priv->lock, flags);
 
2468
 
 
2469
        if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
 
2470
                if (beacon_int == 0) {
 
2471
                        priv->rxon_timing.beacon_interval = cpu_to_le16(100);
 
2472
                        priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
 
2473
                } else {
 
2474
                        priv->rxon_timing.beacon_interval =
 
2475
                                cpu_to_le16(beacon_int);
 
2476
                        priv->rxon_timing.beacon_interval =
 
2477
                            iwl4965_adjust_beacon_interval(
 
2478
                                le16_to_cpu(priv->rxon_timing.beacon_interval));
 
2479
                }
 
2480
 
 
2481
                priv->rxon_timing.atim_window = 0;
 
2482
        } else {
 
2483
                priv->rxon_timing.beacon_interval =
 
2484
                        iwl4965_adjust_beacon_interval(conf->beacon_int);
 
2485
                /* TODO: we need to get atim_window from upper stack
 
2486
                 * for now we set to 0 */
 
2487
                priv->rxon_timing.atim_window = 0;
 
2488
        }
 
2489
 
 
2490
        interval_tm_unit =
 
2491
                (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
 
2492
        result = do_div(tsf, interval_tm_unit);
 
2493
        priv->rxon_timing.beacon_init_val =
 
2494
            cpu_to_le32((u32) ((u64) interval_tm_unit - result));
 
2495
 
 
2496
        IWL_DEBUG_ASSOC
 
2497
            ("beacon interval %d beacon timer %d beacon tim %d\n",
 
2498
                le16_to_cpu(priv->rxon_timing.beacon_interval),
 
2499
                le32_to_cpu(priv->rxon_timing.beacon_init_val),
 
2500
                le16_to_cpu(priv->rxon_timing.atim_window));
 
2501
}
 
2502
 
 
2503
static int iwl4965_scan_initiate(struct iwl4965_priv *priv)
 
2504
{
 
2505
        if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
 
2506
                IWL_ERROR("APs don't scan.\n");
 
2507
                return 0;
 
2508
        }
 
2509
 
 
2510
        if (!iwl4965_is_ready_rf(priv)) {
 
2511
                IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
 
2512
                return -EIO;
 
2513
        }
 
2514
 
 
2515
        if (test_bit(STATUS_SCANNING, &priv->status)) {
 
2516
                IWL_DEBUG_SCAN("Scan already in progress.\n");
 
2517
                return -EAGAIN;
 
2518
        }
 
2519
 
 
2520
        if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
 
2521
                IWL_DEBUG_SCAN("Scan request while abort pending.  "
 
2522
                               "Queuing.\n");
 
2523
                return -EAGAIN;
 
2524
        }
 
2525
 
 
2526
        IWL_DEBUG_INFO("Starting scan...\n");
 
2527
        priv->scan_bands = 2;
 
2528
        set_bit(STATUS_SCANNING, &priv->status);
 
2529
        priv->scan_start = jiffies;
 
2530
        priv->scan_pass_start = priv->scan_start;
 
2531
 
 
2532
        queue_work(priv->workqueue, &priv->request_scan);
 
2533
 
 
2534
        return 0;
 
2535
}
 
2536
 
 
2537
static int iwl4965_set_rxon_hwcrypto(struct iwl4965_priv *priv, int hw_decrypt)
 
2538
{
 
2539
        struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
 
2540
 
 
2541
        if (hw_decrypt)
 
2542
                rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
 
2543
        else
 
2544
                rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
 
2545
 
 
2546
        return 0;
 
2547
}
 
2548
 
 
2549
static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv, u8 phymode)
 
2550
{
 
2551
        if (phymode == MODE_IEEE80211A) {
 
2552
                priv->staging_rxon.flags &=
 
2553
                    ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
 
2554
                      | RXON_FLG_CCK_MSK);
 
2555
                priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
 
2556
        } else {
 
2557
                /* Copied from iwl4965_bg_post_associate() */
 
2558
                if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
 
2559
                        priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
 
2560
                else
 
2561
                        priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
 
2562
 
 
2563
                if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
 
2564
                        priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
 
2565
 
 
2566
                priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
 
2567
                priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
 
2568
                priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
 
2569
        }
 
2570
}
 
2571
 
 
2572
/*
 
2573
 * initialize rxon structure with default values from eeprom
 
2574
 */
 
2575
static void iwl4965_connection_init_rx_config(struct iwl4965_priv *priv)
 
2576
{
 
2577
        const struct iwl4965_channel_info *ch_info;
 
2578
 
 
2579
        memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
 
2580
 
 
2581
        switch (priv->iw_mode) {
 
2582
        case IEEE80211_IF_TYPE_AP:
 
2583
                priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
 
2584
                break;
 
2585
 
 
2586
        case IEEE80211_IF_TYPE_STA:
 
2587
                priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
 
2588
                priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
 
2589
                break;
 
2590
 
 
2591
        case IEEE80211_IF_TYPE_IBSS:
 
2592
                priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
 
2593
                priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
 
2594
                priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
 
2595
                                                  RXON_FILTER_ACCEPT_GRP_MSK;
 
2596
                break;
 
2597
 
 
2598
        case IEEE80211_IF_TYPE_MNTR:
 
2599
                priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
 
2600
                priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
 
2601
                    RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
 
2602
                break;
 
2603
        }
 
2604
 
 
2605
#if 0
 
2606
        /* TODO:  Figure out when short_preamble would be set and cache from
 
2607
         * that */
 
2608
        if (!hw_to_local(priv->hw)->short_preamble)
 
2609
                priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
 
2610
        else
 
2611
                priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
 
2612
#endif
 
2613
 
 
2614
        ch_info = iwl4965_get_channel_info(priv, priv->phymode,
 
2615
                                       le16_to_cpu(priv->staging_rxon.channel));
 
2616
 
 
2617
        if (!ch_info)
 
2618
                ch_info = &priv->channel_info[0];
 
2619
 
 
2620
        /*
 
2621
         * in some case A channels are all non IBSS
 
2622
         * in this case force B/G channel
 
2623
         */
 
2624
        if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
 
2625
            !(is_channel_ibss(ch_info)))
 
2626
                ch_info = &priv->channel_info[0];
 
2627
 
 
2628
        priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
 
2629
        if (is_channel_a_band(ch_info))
 
2630
                priv->phymode = MODE_IEEE80211A;
 
2631
        else
 
2632
                priv->phymode = MODE_IEEE80211G;
 
2633
 
 
2634
        iwl4965_set_flags_for_phymode(priv, priv->phymode);
 
2635
 
 
2636
        priv->staging_rxon.ofdm_basic_rates =
 
2637
            (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
 
2638
        priv->staging_rxon.cck_basic_rates =
 
2639
            (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
 
2640
 
 
2641
        priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
 
2642
                                        RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
 
2643
        memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
 
2644
        memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
 
2645
        priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
 
2646
        priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
 
2647
        iwl4965_set_rxon_chain(priv);
 
2648
}
 
2649
 
 
2650
static int iwl4965_set_mode(struct iwl4965_priv *priv, int mode)
 
2651
{
 
2652
        if (mode == IEEE80211_IF_TYPE_IBSS) {
 
2653
                const struct iwl4965_channel_info *ch_info;
 
2654
 
 
2655
                ch_info = iwl4965_get_channel_info(priv,
 
2656
                        priv->phymode,
 
2657
                        le16_to_cpu(priv->staging_rxon.channel));
 
2658
 
 
2659
                if (!ch_info || !is_channel_ibss(ch_info)) {
 
2660
                        IWL_ERROR("channel %d not IBSS channel\n",
 
2661
                                  le16_to_cpu(priv->staging_rxon.channel));
 
2662
                        return -EINVAL;
 
2663
                }
 
2664
        }
 
2665
 
 
2666
        priv->iw_mode = mode;
 
2667
 
 
2668
        iwl4965_connection_init_rx_config(priv);
 
2669
        memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
 
2670
 
 
2671
        iwl4965_clear_stations_table(priv);
 
2672
 
 
2673
        /* dont commit rxon if rf-kill is on*/
 
2674
        if (!iwl4965_is_ready_rf(priv))
 
2675
                return -EAGAIN;
 
2676
 
 
2677
        cancel_delayed_work(&priv->scan_check);
 
2678
        if (iwl4965_scan_cancel_timeout(priv, 100)) {
 
2679
                IWL_WARNING("Aborted scan still in progress after 100ms\n");
 
2680
                IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
 
2681
                return -EAGAIN;
 
2682
        }
 
2683
 
 
2684
        iwl4965_commit_rxon(priv);
 
2685
 
 
2686
        return 0;
 
2687
}
 
2688
 
 
2689
static void iwl4965_build_tx_cmd_hwcrypto(struct iwl4965_priv *priv,
 
2690
                                      struct ieee80211_tx_control *ctl,
 
2691
                                      struct iwl4965_cmd *cmd,
 
2692
                                      struct sk_buff *skb_frag,
 
2693
                                      int last_frag)
 
2694
{
 
2695
        struct iwl4965_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
 
2696
 
 
2697
        switch (keyinfo->alg) {
 
2698
        case ALG_CCMP:
 
2699
                cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
 
2700
                memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
 
2701
                IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
 
2702
                break;
 
2703
 
 
2704
        case ALG_TKIP:
 
2705
#if 0
 
2706
                cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
 
2707
 
 
2708
                if (last_frag)
 
2709
                        memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
 
2710
                               8);
 
2711
                else
 
2712
                        memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
 
2713
#endif
 
2714
                break;
 
2715
 
 
2716
        case ALG_WEP:
 
2717
                cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
 
2718
                        (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
 
2719
 
 
2720
                if (keyinfo->keylen == 13)
 
2721
                        cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
 
2722
 
 
2723
                memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
 
2724
 
 
2725
                IWL_DEBUG_TX("Configuring packet for WEP encryption "
 
2726
                             "with key %d\n", ctl->key_idx);
 
2727
                break;
 
2728
 
 
2729
        case ALG_NONE:
 
2730
                IWL_DEBUG_TX("Tx packet in the clear (encrypt requested).\n");
 
2731
                break;
 
2732
 
 
2733
        default:
 
2734
                printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
 
2735
                break;
 
2736
        }
 
2737
}
 
2738
 
 
2739
/*
 
2740
 * handle build REPLY_TX command notification.
 
2741
 */
 
2742
static void iwl4965_build_tx_cmd_basic(struct iwl4965_priv *priv,
 
2743
                                  struct iwl4965_cmd *cmd,
 
2744
                                  struct ieee80211_tx_control *ctrl,
 
2745
                                  struct ieee80211_hdr *hdr,
 
2746
                                  int is_unicast, u8 std_id)
 
2747
{
 
2748
        __le16 *qc;
 
2749
        u16 fc = le16_to_cpu(hdr->frame_control);
 
2750
        __le32 tx_flags = cmd->cmd.tx.tx_flags;
 
2751
 
 
2752
        cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
 
2753
        if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
 
2754
                tx_flags |= TX_CMD_FLG_ACK_MSK;
 
2755
                if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
 
2756
                        tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
 
2757
                if (ieee80211_is_probe_response(fc) &&
 
2758
                    !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
 
2759
                        tx_flags |= TX_CMD_FLG_TSF_MSK;
 
2760
        } else {
 
2761
                tx_flags &= (~TX_CMD_FLG_ACK_MSK);
 
2762
                tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
 
2763
        }
 
2764
 
 
2765
        if (ieee80211_is_back_request(fc))
 
2766
                tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
 
2767
 
 
2768
 
 
2769
        cmd->cmd.tx.sta_id = std_id;
 
2770
        if (ieee80211_get_morefrag(hdr))
 
2771
                tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
 
2772
 
 
2773
        qc = ieee80211_get_qos_ctrl(hdr);
 
2774
        if (qc) {
 
2775
                cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
 
2776
                tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
 
2777
        } else
 
2778
                tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
 
2779
 
 
2780
        if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
 
2781
                tx_flags |= TX_CMD_FLG_RTS_MSK;
 
2782
                tx_flags &= ~TX_CMD_FLG_CTS_MSK;
 
2783
        } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
 
2784
                tx_flags &= ~TX_CMD_FLG_RTS_MSK;
 
2785
                tx_flags |= TX_CMD_FLG_CTS_MSK;
 
2786
        }
 
2787
 
 
2788
        if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
 
2789
                tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
 
2790
 
 
2791
        tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
 
2792
        if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
 
2793
                if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
 
2794
                    (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
 
2795
                        cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
 
2796
                else
 
2797
                        cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
 
2798
        } else
 
2799
                cmd->cmd.tx.timeout.pm_frame_timeout = 0;
 
2800
 
 
2801
        cmd->cmd.tx.driver_txop = 0;
 
2802
        cmd->cmd.tx.tx_flags = tx_flags;
 
2803
        cmd->cmd.tx.next_frame_len = 0;
 
2804
}
 
2805
 
 
2806
/**
 
2807
 * iwl4965_get_sta_id - Find station's index within station table
 
2808
 *
 
2809
 * If new IBSS station, create new entry in station table
 
2810
 */
 
2811
static int iwl4965_get_sta_id(struct iwl4965_priv *priv,
 
2812
                                struct ieee80211_hdr *hdr)
 
2813
{
 
2814
        int sta_id;
 
2815
        u16 fc = le16_to_cpu(hdr->frame_control);
 
2816
 
 
2817
        /* If this frame is broadcast or management, use broadcast station id */
 
2818
        if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
 
2819
            is_multicast_ether_addr(hdr->addr1))
 
2820
                return priv->hw_setting.bcast_sta_id;
 
2821
 
 
2822
        switch (priv->iw_mode) {
 
2823
 
 
2824
        /* If we are a client station in a BSS network, use the special
 
2825
         * AP station entry (that's the only station we communicate with) */
 
2826
        case IEEE80211_IF_TYPE_STA:
 
2827
                return IWL_AP_ID;
 
2828
 
 
2829
        /* If we are an AP, then find the station, or use BCAST */
 
2830
        case IEEE80211_IF_TYPE_AP:
 
2831
                sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
 
2832
                if (sta_id != IWL_INVALID_STATION)
 
2833
                        return sta_id;
 
2834
                return priv->hw_setting.bcast_sta_id;
 
2835
 
 
2836
        /* If this frame is going out to an IBSS network, find the station,
 
2837
         * or create a new station table entry */
 
2838
        case IEEE80211_IF_TYPE_IBSS:
 
2839
                sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
 
2840
                if (sta_id != IWL_INVALID_STATION)
 
2841
                        return sta_id;
 
2842
 
 
2843
                /* Create new station table entry */
 
2844
                sta_id = iwl4965_add_station_flags(priv, hdr->addr1, 0, CMD_ASYNC);
 
2845
 
 
2846
                if (sta_id != IWL_INVALID_STATION)
 
2847
                        return sta_id;
 
2848
 
 
2849
                IWL_DEBUG_DROP("Station " MAC_FMT " not in station map. "
 
2850
                               "Defaulting to broadcast...\n",
 
2851
                               MAC_ARG(hdr->addr1));
 
2852
                iwl4965_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
 
2853
                return priv->hw_setting.bcast_sta_id;
 
2854
 
 
2855
        default:
 
2856
                IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
 
2857
                return priv->hw_setting.bcast_sta_id;
 
2858
        }
 
2859
}
 
2860
 
 
2861
/*
 
2862
 * start REPLY_TX command process
 
2863
 */
 
2864
static int iwl4965_tx_skb(struct iwl4965_priv *priv,
 
2865
                      struct sk_buff *skb, struct ieee80211_tx_control *ctl)
 
2866
{
 
2867
        struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
 
2868
        struct iwl4965_tfd_frame *tfd;
 
2869
        u32 *control_flags;
 
2870
        int txq_id = ctl->queue;
 
2871
        struct iwl4965_tx_queue *txq = NULL;
 
2872
        struct iwl4965_queue *q = NULL;
 
2873
        dma_addr_t phys_addr;
 
2874
        dma_addr_t txcmd_phys;
 
2875
        dma_addr_t scratch_phys;
 
2876
        struct iwl4965_cmd *out_cmd = NULL;
 
2877
        u16 len, idx, len_org;
 
2878
        u8 id, hdr_len, unicast;
 
2879
        u8 sta_id;
 
2880
        u16 seq_number = 0;
 
2881
        u16 fc;
 
2882
        __le16 *qc;
 
2883
        u8 wait_write_ptr = 0;
 
2884
        unsigned long flags;
 
2885
        int rc;
 
2886
 
 
2887
        spin_lock_irqsave(&priv->lock, flags);
 
2888
        if (iwl4965_is_rfkill(priv)) {
 
2889
                IWL_DEBUG_DROP("Dropping - RF KILL\n");
 
2890
                goto drop_unlock;
 
2891
        }
 
2892
 
 
2893
        if (!priv->interface_id) {
 
2894
                IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
 
2895
                goto drop_unlock;
 
2896
        }
 
2897
 
 
2898
        if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
 
2899
                IWL_ERROR("ERROR: No TX rate available.\n");
 
2900
                goto drop_unlock;
 
2901
        }
 
2902
 
 
2903
        unicast = !is_multicast_ether_addr(hdr->addr1);
 
2904
        id = 0;
 
2905
 
 
2906
        fc = le16_to_cpu(hdr->frame_control);
 
2907
 
 
2908
#ifdef CONFIG_IWL4965_DEBUG
 
2909
        if (ieee80211_is_auth(fc))
 
2910
                IWL_DEBUG_TX("Sending AUTH frame\n");
 
2911
        else if (ieee80211_is_assoc_request(fc))
 
2912
                IWL_DEBUG_TX("Sending ASSOC frame\n");
 
2913
        else if (ieee80211_is_reassoc_request(fc))
 
2914
                IWL_DEBUG_TX("Sending REASSOC frame\n");
 
2915
#endif
 
2916
 
 
2917
        /* drop all data frame if we are not associated */
 
2918
        if (!iwl4965_is_associated(priv) && !priv->assoc_id &&
 
2919
            ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
 
2920
                IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
 
2921
                goto drop_unlock;
 
2922
        }
 
2923
 
 
2924
        spin_unlock_irqrestore(&priv->lock, flags);
 
2925
 
 
2926
        hdr_len = iwlwifi_ieee80211_get_hdrlen(fc);
 
2927
 
 
2928
        /* Find (or create) index into station table for destination station */
 
2929
        sta_id = iwl4965_get_sta_id(priv, hdr);
 
2930
        if (sta_id == IWL_INVALID_STATION) {
 
2931
                IWL_DEBUG_DROP("Dropping - INVALID STATION: " MAC_FMT "\n",
 
2932
                               MAC_ARG(hdr->addr1));
 
2933
                goto drop;
 
2934
        }
 
2935
 
 
2936
        IWL_DEBUG_RATE("station Id %d\n", sta_id);
 
2937
 
 
2938
        qc = ieee80211_get_qos_ctrl(hdr);
 
2939
        if (qc) {
 
2940
                u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
 
2941
                seq_number = priv->stations[sta_id].tid[tid].seq_number &
 
2942
                                IEEE80211_SCTL_SEQ;
 
2943
                hdr->seq_ctrl = cpu_to_le16(seq_number) |
 
2944
                        (hdr->seq_ctrl &
 
2945
                                __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
 
2946
                seq_number += 0x10;
 
2947
#ifdef CONFIG_IWL4965_HT
 
2948
#ifdef CONFIG_IWL4965_HT_AGG
 
2949
                /* aggregation is on for this <sta,tid> */
 
2950
                if (ctl->flags & IEEE80211_TXCTL_HT_MPDU_AGG)
 
2951
                        txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
 
2952
#endif /* CONFIG_IWL4965_HT_AGG */
 
2953
#endif /* CONFIG_IWL4965_HT */
 
2954
        }
 
2955
 
 
2956
        /* Descriptor for chosen Tx queue */
 
2957
        txq = &priv->txq[txq_id];
 
2958
        q = &txq->q;
 
2959
 
 
2960
        spin_lock_irqsave(&priv->lock, flags);
 
2961
 
 
2962
        /* Set up first empty TFD within this queue's circular TFD buffer */
 
2963
        tfd = &txq->bd[q->write_ptr];
 
2964
        memset(tfd, 0, sizeof(*tfd));
 
2965
        control_flags = (u32 *) tfd;
 
2966
        idx = get_cmd_index(q, q->write_ptr, 0);
 
2967
 
 
2968
        /* Set up driver data for this TFD */
 
2969
        memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
 
2970
        txq->txb[q->write_ptr].skb[0] = skb;
 
2971
        memcpy(&(txq->txb[q->write_ptr].status.control),
 
2972
               ctl, sizeof(struct ieee80211_tx_control));
 
2973
 
 
2974
        /* Set up first empty entry in queue's array of Tx/cmd buffers */
 
2975
        out_cmd = &txq->cmd[idx];
 
2976
        memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
 
2977
        memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
 
2978
 
 
2979
        /*
 
2980
         * Set up the Tx-command (not MAC!) header.
 
2981
         * Store the chosen Tx queue and TFD index within the sequence field;
 
2982
         * after Tx, uCode's Tx response will return this value so driver can
 
2983
         * locate the frame within the tx queue and do post-tx processing.
 
2984
         */
 
2985
        out_cmd->hdr.cmd = REPLY_TX;
 
2986
        out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
 
2987
                                INDEX_TO_SEQ(q->write_ptr)));
 
2988
 
 
2989
        /* Copy MAC header from skb into command buffer */
 
2990
        memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
 
2991
 
 
2992
        /*
 
2993
         * Use the first empty entry in this queue's command buffer array
 
2994
         * to contain the Tx command and MAC header concatenated together
 
2995
         * (payload data will be in another buffer).
 
2996
         * Size of this varies, due to varying MAC header length.
 
2997
         * If end is not dword aligned, we'll have 2 extra bytes at the end
 
2998
         * of the MAC header (device reads on dword boundaries).
 
2999
         * We'll tell device about this padding later.
 
3000
         */
 
3001
        len = priv->hw_setting.tx_cmd_len +
 
3002
                sizeof(struct iwl4965_cmd_header) + hdr_len;
 
3003
 
 
3004
        len_org = len;
 
3005
        len = (len + 3) & ~3;
 
3006
 
 
3007
        if (len_org != len)
 
3008
                len_org = 1;
 
3009
        else
 
3010
                len_org = 0;
 
3011
 
 
3012
        /* Physical address of this Tx command's header (not MAC header!),
 
3013
         * within command buffer array. */
 
3014
        txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
 
3015
                     offsetof(struct iwl4965_cmd, hdr);
 
3016
 
 
3017
        /* Add buffer containing Tx command and MAC(!) header to TFD's
 
3018
         * first entry */
 
3019
        iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
 
3020
 
 
3021
        if (ctl->key_idx != -1)
 
3022
                iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
 
3023
 
 
3024
        /* Set up TFD's 2nd entry to point directly to remainder of skb,
 
3025
         * if any (802.11 null frames have no payload). */
 
3026
        len = skb->len - hdr_len;
 
3027
        if (len) {
 
3028
                phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
 
3029
                                           len, PCI_DMA_TODEVICE);
 
3030
                iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
 
3031
        }
 
3032
 
 
3033
        /* Tell 4965 about any 2-byte padding after MAC header */
 
3034
        if (len_org)
 
3035
                out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
 
3036
 
 
3037
        /* Total # bytes to be transmitted */
 
3038
        len = (u16)skb->len;
 
3039
        out_cmd->cmd.tx.len = cpu_to_le16(len);
 
3040
 
 
3041
        /* TODO need this for burst mode later on */
 
3042
        iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
 
3043
 
 
3044
        /* set is_hcca to 0; it probably will never be implemented */
 
3045
        iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
 
3046
 
 
3047
        scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
 
3048
                offsetof(struct iwl4965_tx_cmd, scratch);
 
3049
        out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
 
3050
        out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
 
3051
 
 
3052
#ifdef CONFIG_IWL4965_HT_AGG
 
3053
#ifdef CONFIG_IWL4965_HT
 
3054
        /* TODO: move this functionality to rate scaling */
 
3055
        iwl4965_tl_get_stats(priv, hdr);
 
3056
#endif /* CONFIG_IWL4965_HT_AGG */
 
3057
#endif /*CONFIG_IWL4965_HT */
 
3058
 
 
3059
 
 
3060
        if (!ieee80211_get_morefrag(hdr)) {
 
3061
                txq->need_update = 1;
 
3062
                if (qc) {
 
3063
                        u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
 
3064
                        priv->stations[sta_id].tid[tid].seq_number = seq_number;
 
3065
                }
 
3066
        } else {
 
3067
                wait_write_ptr = 1;
 
3068
                txq->need_update = 0;
 
3069
        }
 
3070
 
 
3071
        iwl4965_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
 
3072
                           sizeof(out_cmd->cmd.tx));
 
3073
 
 
3074
        iwl4965_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
 
3075
                           iwlwifi_ieee80211_get_hdrlen(fc));
 
3076
 
 
3077
        /* Set up entry for this TFD in Tx byte-count array */
 
3078
        iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
 
3079
 
 
3080
        /* Tell device the write index *just past* this latest filled TFD */
 
3081
        q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
 
3082
        rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
 
3083
        spin_unlock_irqrestore(&priv->lock, flags);
 
3084
 
 
3085
        if (rc)
 
3086
                return rc;
 
3087
 
 
3088
        if ((iwl4965_queue_space(q) < q->high_mark)
 
3089
            && priv->mac80211_registered) {
 
3090
                if (wait_write_ptr) {
 
3091
                        spin_lock_irqsave(&priv->lock, flags);
 
3092
                        txq->need_update = 1;
 
3093
                        iwl4965_tx_queue_update_write_ptr(priv, txq);
 
3094
                        spin_unlock_irqrestore(&priv->lock, flags);
 
3095
                }
 
3096
 
 
3097
                iwlwifi_ieee80211_stop_queue(priv->hw, ctl->queue);
 
3098
        }
 
3099
 
 
3100
        return 0;
 
3101
 
 
3102
drop_unlock:
 
3103
        spin_unlock_irqrestore(&priv->lock, flags);
 
3104
drop:
 
3105
        return -1;
 
3106
}
 
3107
 
 
3108
static void iwl4965_set_rate(struct iwl4965_priv *priv)
 
3109
{
 
3110
        const struct ieee80211_hw_mode *hw = NULL;
 
3111
        struct ieee80211_rate *rate;
 
3112
        int i;
 
3113
 
 
3114
        hw = iwl4965_get_hw_mode(priv, priv->phymode);
 
3115
        if (!hw) {
 
3116
                IWL_ERROR("Failed to set rate: unable to get hw mode\n");
 
3117
                return;
 
3118
        }
 
3119
 
 
3120
        priv->active_rate = 0;
 
3121
        priv->active_rate_basic = 0;
 
3122
 
 
3123
        IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
 
3124
                       hw->mode == MODE_IEEE80211A ?
 
3125
                       'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
 
3126
 
 
3127
        for (i = 0; i < hw->num_rates; i++) {
 
3128
                rate = &(hw->rates[i]);
 
3129
                if ((rate->val < IWL_RATE_COUNT) &&
 
3130
                    (rate->flags & IEEE80211_RATE_SUPPORTED)) {
 
3131
                        IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
 
3132
                                       rate->val, iwl4965_rates[rate->val].plcp,
 
3133
                                       (rate->flags & IEEE80211_RATE_BASIC) ?
 
3134
                                       "*" : "");
 
3135
                        priv->active_rate |= (1 << rate->val);
 
3136
                        if (rate->flags & IEEE80211_RATE_BASIC)
 
3137
                                priv->active_rate_basic |= (1 << rate->val);
 
3138
                } else
 
3139
                        IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
 
3140
                                       rate->val, iwl4965_rates[rate->val].plcp);
 
3141
        }
 
3142
 
 
3143
        IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
 
3144
                       priv->active_rate, priv->active_rate_basic);
 
3145
 
 
3146
        /*
 
3147
         * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
 
3148
         * otherwise set it to the default of all CCK rates and 6, 12, 24 for
 
3149
         * OFDM
 
3150
         */
 
3151
        if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
 
3152
                priv->staging_rxon.cck_basic_rates =
 
3153
                    ((priv->active_rate_basic &
 
3154
                      IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
 
3155
        else
 
3156
                priv->staging_rxon.cck_basic_rates =
 
3157
                    (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
 
3158
 
 
3159
        if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
 
3160
                priv->staging_rxon.ofdm_basic_rates =
 
3161
                    ((priv->active_rate_basic &
 
3162
                      (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
 
3163
                      IWL_FIRST_OFDM_RATE) & 0xFF;
 
3164
        else
 
3165
                priv->staging_rxon.ofdm_basic_rates =
 
3166
                   (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
 
3167
}
 
3168
 
 
3169
static void iwl4965_radio_kill_sw(struct iwl4965_priv *priv, int disable_radio)
 
3170
{
 
3171
        unsigned long flags;
 
3172
 
 
3173
        if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
 
3174
                return;
 
3175
 
 
3176
        IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
 
3177
                          disable_radio ? "OFF" : "ON");
 
3178
 
 
3179
        if (disable_radio) {
 
3180
                iwl4965_scan_cancel(priv);
 
3181
                /* FIXME: This is a workaround for AP */
 
3182
                if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
 
3183
                        spin_lock_irqsave(&priv->lock, flags);
 
3184
                        iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
 
3185
                                    CSR_UCODE_SW_BIT_RFKILL);
 
3186
                        spin_unlock_irqrestore(&priv->lock, flags);
 
3187
                        iwl4965_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
 
3188
                        set_bit(STATUS_RF_KILL_SW, &priv->status);
 
3189
                }
 
3190
                return;
 
3191
        }
 
3192
 
 
3193
        spin_lock_irqsave(&priv->lock, flags);
 
3194
        iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
 
3195
 
 
3196
        clear_bit(STATUS_RF_KILL_SW, &priv->status);
 
3197
        spin_unlock_irqrestore(&priv->lock, flags);
 
3198
 
 
3199
        /* wake up ucode */
 
3200
        msleep(10);
 
3201
 
 
3202
        spin_lock_irqsave(&priv->lock, flags);
 
3203
        iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
 
3204
        if (!iwl4965_grab_nic_access(priv))
 
3205
                iwl4965_release_nic_access(priv);
 
3206
        spin_unlock_irqrestore(&priv->lock, flags);
 
3207
 
 
3208
        if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
 
3209
                IWL_DEBUG_RF_KILL("Can not turn radio back on - "
 
3210
                                  "disabled by HW switch\n");
 
3211
                return;
 
3212
        }
 
3213
 
 
3214
        queue_work(priv->workqueue, &priv->restart);
 
3215
        return;
 
3216
}
 
3217
 
 
3218
void iwl4965_set_decrypted_flag(struct iwl4965_priv *priv, struct sk_buff *skb,
 
3219
                            u32 decrypt_res, struct ieee80211_rx_status *stats)
 
3220
{
 
3221
        u16 fc =
 
3222
            le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
 
3223
 
 
3224
        if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
 
3225
                return;
 
3226
 
 
3227
        if (!(fc & IEEE80211_FCTL_PROTECTED))
 
3228
                return;
 
3229
 
 
3230
        IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
 
3231
        switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
 
3232
        case RX_RES_STATUS_SEC_TYPE_TKIP:
 
3233
                if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
 
3234
                    RX_RES_STATUS_BAD_ICV_MIC)
 
3235
                        stats->flag |= RX_FLAG_MMIC_ERROR;
 
3236
        case RX_RES_STATUS_SEC_TYPE_WEP:
 
3237
        case RX_RES_STATUS_SEC_TYPE_CCMP:
 
3238
                if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
 
3239
                    RX_RES_STATUS_DECRYPT_OK) {
 
3240
                        IWL_DEBUG_RX("hw decrypt successfully!!!\n");
 
3241
                        stats->flag |= RX_FLAG_DECRYPTED;
 
3242
                }
 
3243
                break;
 
3244
 
 
3245
        default:
 
3246
                break;
 
3247
        }
 
3248
}
 
3249
 
 
3250
void iwl4965_handle_data_packet_monitor(struct iwl4965_priv *priv,
 
3251
                                    struct iwl4965_rx_mem_buffer *rxb,
 
3252
                                    void *data, short len,
 
3253
                                    struct ieee80211_rx_status *stats,
 
3254
                                    u16 phy_flags)
 
3255
{
 
3256
        struct iwl4965_rt_rx_hdr *iwl4965_rt;
 
3257
 
 
3258
        /* First cache any information we need before we overwrite
 
3259
         * the information provided in the skb from the hardware */
 
3260
        s8 signal = stats->ssi;
 
3261
        s8 noise = 0;
 
3262
        int rate = stats->rate;
 
3263
        u64 tsf = stats->mactime;
 
3264
        __le16 phy_flags_hw = cpu_to_le16(phy_flags);
 
3265
 
 
3266
        /* We received data from the HW, so stop the watchdog */
 
3267
        if (len > IWL_RX_BUF_SIZE - sizeof(*iwl4965_rt)) {
 
3268
                IWL_DEBUG_DROP("Dropping too large packet in monitor\n");
 
3269
                return;
 
3270
        }
 
3271
 
 
3272
        /* copy the frame data to write after where the radiotap header goes */
 
3273
        iwl4965_rt = (void *)rxb->skb->data;
 
3274
        memmove(iwl4965_rt->payload, data, len);
 
3275
 
 
3276
        iwl4965_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
 
3277
        iwl4965_rt->rt_hdr.it_pad = 0; /* always good to zero */
 
3278
 
 
3279
        /* total header + data */
 
3280
        iwl4965_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*iwl4965_rt));
 
3281
 
 
3282
        /* Set the size of the skb to the size of the frame */
 
3283
        skb_put(rxb->skb, sizeof(*iwl4965_rt) + len);
 
3284
 
 
3285
        /* Big bitfield of all the fields we provide in radiotap */
 
3286
        iwl4965_rt->rt_hdr.it_present =
 
3287
            cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
 
3288
                        (1 << IEEE80211_RADIOTAP_FLAGS) |
 
3289
                        (1 << IEEE80211_RADIOTAP_RATE) |
 
3290
                        (1 << IEEE80211_RADIOTAP_CHANNEL) |
 
3291
                        (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
 
3292
                        (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
 
3293
                        (1 << IEEE80211_RADIOTAP_ANTENNA));
 
3294
 
 
3295
        /* Zero the flags, we'll add to them as we go */
 
3296
        iwl4965_rt->rt_flags = 0;
 
3297
 
 
3298
        iwl4965_rt->rt_tsf = cpu_to_le64(tsf);
 
3299
 
 
3300
        /* Convert to dBm */
 
3301
        iwl4965_rt->rt_dbmsignal = signal;
 
3302
        iwl4965_rt->rt_dbmnoise = noise;
 
3303
 
 
3304
        /* Convert the channel frequency and set the flags */
 
3305
        iwl4965_rt->rt_channelMHz = cpu_to_le16(stats->freq);
 
3306
        if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
 
3307
                iwl4965_rt->rt_chbitmask =
 
3308
                    cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
 
3309
        else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
 
3310
                iwl4965_rt->rt_chbitmask =
 
3311
                    cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
 
3312
        else    /* 802.11g */
 
3313
                iwl4965_rt->rt_chbitmask =
 
3314
                    cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ));
 
3315
 
 
3316
        rate = iwl4965_rate_index_from_plcp(rate);
 
3317
        if (rate == -1)
 
3318
                iwl4965_rt->rt_rate = 0;
 
3319
        else
 
3320
                iwl4965_rt->rt_rate = iwl4965_rates[rate].ieee;
 
3321
 
 
3322
        /* antenna number */
 
3323
        iwl4965_rt->rt_antenna =
 
3324
                le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
 
3325
 
 
3326
        /* set the preamble flag if we have it */
 
3327
        if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
 
3328
                iwl4965_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
 
3329
 
 
3330
        IWL_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
 
3331
 
 
3332
        stats->flag |= RX_FLAG_RADIOTAP;
 
3333
        iwlwifi_ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
 
3334
        rxb->skb = NULL;
 
3335
}
 
3336
 
 
3337
 
 
3338
#define IWL_PACKET_RETRY_TIME HZ
 
3339
 
 
3340
int iwl4965_is_duplicate_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
 
3341
{
 
3342
        u16 sc = le16_to_cpu(header->seq_ctrl);
 
3343
        u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
 
3344
        u16 frag = sc & IEEE80211_SCTL_FRAG;
 
3345
        u16 *last_seq, *last_frag;
 
3346
        unsigned long *last_time;
 
3347
 
 
3348
        switch (priv->iw_mode) {
 
3349
        case IEEE80211_IF_TYPE_IBSS:{
 
3350
                struct list_head *p;
 
3351
                struct iwl4965_ibss_seq *entry = NULL;
 
3352
                u8 *mac = header->addr2;
 
3353
                int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
 
3354
 
 
3355
                __list_for_each(p, &priv->ibss_mac_hash[index]) {
 
3356
                        entry = list_entry(p, struct iwl4965_ibss_seq, list);
 
3357
                        if (!compare_ether_addr(entry->mac, mac))
 
3358
                                break;
 
3359
                }
 
3360
                if (p == &priv->ibss_mac_hash[index]) {
 
3361
                        entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
 
3362
                        if (!entry) {
 
3363
                                IWL_ERROR("Cannot malloc new mac entry\n");
 
3364
                                return 0;
 
3365
                        }
 
3366
                        memcpy(entry->mac, mac, ETH_ALEN);
 
3367
                        entry->seq_num = seq;
 
3368
                        entry->frag_num = frag;
 
3369
                        entry->packet_time = jiffies;
 
3370
                        list_add(&entry->list, &priv->ibss_mac_hash[index]);
 
3371
                        return 0;
 
3372
                }
 
3373
                last_seq = &entry->seq_num;
 
3374
                last_frag = &entry->frag_num;
 
3375
                last_time = &entry->packet_time;
 
3376
                break;
 
3377
        }
 
3378
        case IEEE80211_IF_TYPE_STA:
 
3379
                last_seq = &priv->last_seq_num;
 
3380
                last_frag = &priv->last_frag_num;
 
3381
                last_time = &priv->last_packet_time;
 
3382
                break;
 
3383
        default:
 
3384
                return 0;
 
3385
        }
 
3386
        if ((*last_seq == seq) &&
 
3387
            time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
 
3388
                if (*last_frag == frag)
 
3389
                        goto drop;
 
3390
                if (*last_frag + 1 != frag)
 
3391
                        /* out-of-order fragment */
 
3392
                        goto drop;
 
3393
        } else
 
3394
                *last_seq = seq;
 
3395
 
 
3396
        *last_frag = frag;
 
3397
        *last_time = jiffies;
 
3398
        return 0;
 
3399
 
 
3400
 drop:
 
3401
        return 1;
 
3402
}
 
3403
 
 
3404
#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
 
3405
 
 
3406
#include "iwl-spectrum.h"
 
3407
 
 
3408
#define BEACON_TIME_MASK_LOW    0x00FFFFFF
 
3409
#define BEACON_TIME_MASK_HIGH   0xFF000000
 
3410
#define TIME_UNIT               1024
 
3411
 
 
3412
/*
 
3413
 * extended beacon time format
 
3414
 * time in usec will be changed into a 32-bit value in 8:24 format
 
3415
 * the high 1 byte is the beacon counts
 
3416
 * the lower 3 bytes is the time in usec within one beacon interval
 
3417
 */
 
3418
 
 
3419
static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
 
3420
{
 
3421
        u32 quot;
 
3422
        u32 rem;
 
3423
        u32 interval = beacon_interval * 1024;
 
3424
 
 
3425
        if (!interval || !usec)
 
3426
                return 0;
 
3427
 
 
3428
        quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
 
3429
        rem = (usec % interval) & BEACON_TIME_MASK_LOW;
 
3430
 
 
3431
        return (quot << 24) + rem;
 
3432
}
 
3433
 
 
3434
/* base is usually what we get from ucode with each received frame,
 
3435
 * the same as HW timer counter counting down
 
3436
 */
 
3437
 
 
3438
static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
 
3439
{
 
3440
        u32 base_low = base & BEACON_TIME_MASK_LOW;
 
3441
        u32 addon_low = addon & BEACON_TIME_MASK_LOW;
 
3442
        u32 interval = beacon_interval * TIME_UNIT;
 
3443
        u32 res = (base & BEACON_TIME_MASK_HIGH) +
 
3444
            (addon & BEACON_TIME_MASK_HIGH);
 
3445
 
 
3446
        if (base_low > addon_low)
 
3447
                res += base_low - addon_low;
 
3448
        else if (base_low < addon_low) {
 
3449
                res += interval + base_low - addon_low;
 
3450
                res += (1 << 24);
 
3451
        } else
 
3452
                res += (1 << 24);
 
3453
 
 
3454
        return cpu_to_le32(res);
 
3455
}
 
3456
 
 
3457
static int iwl4965_get_measurement(struct iwl4965_priv *priv,
 
3458
                               struct ieee80211_measurement_params *params,
 
3459
                               u8 type)
 
3460
{
 
3461
        struct iwl4965_spectrum_cmd spectrum;
 
3462
        struct iwl4965_rx_packet *res;
 
3463
        struct iwl4965_host_cmd cmd = {
 
3464
                .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
 
3465
                .data = (void *)&spectrum,
 
3466
                .meta.flags = CMD_WANT_SKB,
 
3467
        };
 
3468
        u32 add_time = le64_to_cpu(params->start_time);
 
3469
        int rc;
 
3470
        int spectrum_resp_status;
 
3471
        int duration = le16_to_cpu(params->duration);
 
3472
 
 
3473
        if (iwl4965_is_associated(priv))
 
3474
                add_time =
 
3475
                    iwl4965_usecs_to_beacons(
 
3476
                        le64_to_cpu(params->start_time) - priv->last_tsf,
 
3477
                        le16_to_cpu(priv->rxon_timing.beacon_interval));
 
3478
 
 
3479
        memset(&spectrum, 0, sizeof(spectrum));
 
3480
 
 
3481
        spectrum.channel_count = cpu_to_le16(1);
 
3482
        spectrum.flags =
 
3483
            RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
 
3484
        spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
 
3485
        cmd.len = sizeof(spectrum);
 
3486
        spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
 
3487
 
 
3488
        if (iwl4965_is_associated(priv))
 
3489
                spectrum.start_time =
 
3490
                    iwl4965_add_beacon_time(priv->last_beacon_time,
 
3491
                                add_time,
 
3492
                                le16_to_cpu(priv->rxon_timing.beacon_interval));
 
3493
        else
 
3494
                spectrum.start_time = 0;
 
3495
 
 
3496
        spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
 
3497
        spectrum.channels[0].channel = params->channel;
 
3498
        spectrum.channels[0].type = type;
 
3499
        if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
 
3500
                spectrum.flags |= RXON_FLG_BAND_24G_MSK |
 
3501
                    RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
 
3502
 
 
3503
        rc = iwl4965_send_cmd_sync(priv, &cmd);
 
3504
        if (rc)
 
3505
                return rc;
 
3506
 
 
3507
        res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
 
3508
        if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
 
3509
                IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
 
3510
                rc = -EIO;
 
3511
        }
 
3512
 
 
3513
        spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
 
3514
        switch (spectrum_resp_status) {
 
3515
        case 0:         /* Command will be handled */
 
3516
                if (res->u.spectrum.id != 0xff) {
 
3517
                        IWL_DEBUG_INFO
 
3518
                            ("Replaced existing measurement: %d\n",
 
3519
                             res->u.spectrum.id);
 
3520
                        priv->measurement_status &= ~MEASUREMENT_READY;
 
3521
                }
 
3522
                priv->measurement_status |= MEASUREMENT_ACTIVE;
 
3523
                rc = 0;
 
3524
                break;
 
3525
 
 
3526
        case 1:         /* Command will not be handled */
 
3527
                rc = -EAGAIN;
 
3528
                break;
 
3529
        }
 
3530
 
 
3531
        dev_kfree_skb_any(cmd.meta.u.skb);
 
3532
 
 
3533
        return rc;
 
3534
}
 
3535
#endif
 
3536
 
 
3537
static void iwl4965_txstatus_to_ieee(struct iwl4965_priv *priv,
 
3538
                                 struct iwl4965_tx_info *tx_sta)
 
3539
{
 
3540
 
 
3541
        tx_sta->status.ack_signal = 0;
 
3542
        tx_sta->status.excessive_retries = 0;
 
3543
        tx_sta->status.queue_length = 0;
 
3544
        tx_sta->status.queue_number = 0;
 
3545
 
 
3546
        if (in_interrupt())
 
3547
                iwlwifi_iwlwifi_ieee80211_tx_status_irqsafe(priv->hw,
 
3548
                                            tx_sta->skb[0], &(tx_sta->status));
 
3549
        else
 
3550
                iwlwifi_ieee80211_tx_status(priv->hw,
 
3551
                                    tx_sta->skb[0], &(tx_sta->status));
 
3552
 
 
3553
        tx_sta->skb[0] = NULL;
 
3554
}
 
3555
 
 
3556
/**
 
3557
 * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
 
3558
 *
 
3559
 * When FW advances 'R' index, all entries between old and new 'R' index
 
3560
 * need to be reclaimed. As result, some free space forms.  If there is
 
3561
 * enough free space (> low mark), wake the stack that feeds us.
 
3562
 */
 
3563
int iwl4965_tx_queue_reclaim(struct iwl4965_priv *priv, int txq_id, int index)
 
3564
{
 
3565
        struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
 
3566
        struct iwl4965_queue *q = &txq->q;
 
3567
        int nfreed = 0;
 
3568
 
 
3569
        if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
 
3570
                IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
 
3571
                          "is out of range [0-%d] %d %d.\n", txq_id,
 
3572
                          index, q->n_bd, q->write_ptr, q->read_ptr);
 
3573
                return 0;
 
3574
        }
 
3575
 
 
3576
        for (index = iwl4965_queue_inc_wrap(index, q->n_bd);
 
3577
                q->read_ptr != index;
 
3578
                q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd)) {
 
3579
                if (txq_id != IWL_CMD_QUEUE_NUM) {
 
3580
                        iwl4965_txstatus_to_ieee(priv,
 
3581
                                        &(txq->txb[txq->q.read_ptr]));
 
3582
                        iwl4965_hw_txq_free_tfd(priv, txq);
 
3583
                } else if (nfreed > 1) {
 
3584
                        IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
 
3585
                                        q->write_ptr, q->read_ptr);
 
3586
                        queue_work(priv->workqueue, &priv->restart);
 
3587
                }
 
3588
                nfreed++;
 
3589
        }
 
3590
 
 
3591
        if (iwl4965_queue_space(q) > q->low_mark && (txq_id >= 0) &&
 
3592
                        (txq_id != IWL_CMD_QUEUE_NUM) &&
 
3593
                        priv->mac80211_registered)
 
3594
                iwlwifi_ieee80211_wake_queue(priv->hw, txq_id);
 
3595
 
 
3596
 
 
3597
        return nfreed;
 
3598
}
 
3599
 
 
3600
static int iwl4965_is_tx_success(u32 status)
 
3601
{
 
3602
        status &= TX_STATUS_MSK;
 
3603
        return (status == TX_STATUS_SUCCESS)
 
3604
            || (status == TX_STATUS_DIRECT_DONE);
 
3605
}
 
3606
 
 
3607
/******************************************************************************
 
3608
 *
 
3609
 * Generic RX handler implementations
 
3610
 *
 
3611
 ******************************************************************************/
 
3612
#ifdef CONFIG_IWL4965_HT
 
3613
#ifdef CONFIG_IWL4965_HT_AGG
 
3614
 
 
3615
static inline int iwl4965_get_ra_sta_id(struct iwl4965_priv *priv,
 
3616
                                    struct ieee80211_hdr *hdr)
 
3617
{
 
3618
        if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
 
3619
                return IWL_AP_ID;
 
3620
        else {
 
3621
                u8 *da = ieee80211_get_DA(hdr);
 
3622
                return iwl4965_hw_find_station(priv, da);
 
3623
        }
 
3624
}
 
3625
 
 
3626
static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
 
3627
        struct iwl4965_priv *priv, int txq_id, int idx)
 
3628
{
 
3629
        if (priv->txq[txq_id].txb[idx].skb[0])
 
3630
                return (struct ieee80211_hdr *)priv->txq[txq_id].
 
3631
                                txb[idx].skb[0]->data;
 
3632
        return NULL;
 
3633
}
 
3634
 
 
3635
static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
 
3636
{
 
3637
        __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
 
3638
                                tx_resp->frame_count);
 
3639
        return le32_to_cpu(*scd_ssn) & MAX_SN;
 
3640
 
 
3641
}
 
3642
 
 
3643
/**
 
3644
 * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
 
3645
 */
 
3646
static int iwl4965_tx_status_reply_tx(struct iwl4965_priv *priv,
 
3647
                                      struct iwl4965_ht_agg *agg,
 
3648
                                      struct iwl4965_tx_resp *tx_resp,
 
3649
                                      u16 start_idx)
 
3650
{
 
3651
        u32 status;
 
3652
        __le32 *frame_status = &tx_resp->status;
 
3653
        struct iwlwifi_ieee80211_tx_status *tx_status = NULL;
 
3654
        struct ieee80211_hdr *hdr = NULL;
 
3655
        int i, sh;
 
3656
        int txq_id, idx;
 
3657
        u16 seq;
 
3658
 
 
3659
        if (agg->wait_for_ba)
 
3660
                IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
 
3661
 
 
3662
        agg->frame_count = tx_resp->frame_count;
 
3663
        agg->start_idx = start_idx;
 
3664
        agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
 
3665
        agg->bitmap0 = agg->bitmap1 = 0;
 
3666
 
 
3667
        /* # frames attempted by Tx command */
 
3668
        if (agg->frame_count == 1) {
 
3669
                /* Only one frame was attempted; no block-ack will arrive */
 
3670
                struct iwl4965_tx_queue *txq ;
 
3671
                status = le32_to_cpu(frame_status[0]);
 
3672
 
 
3673
                txq_id = agg->txq_id;
 
3674
                txq = &priv->txq[txq_id];
 
3675
                /* FIXME: code repetition */
 
3676
                IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d \n",
 
3677
                                   agg->frame_count, agg->start_idx);
 
3678
 
 
3679
                tx_status = &(priv->txq[txq_id].txb[txq->q.read_ptr].status);
 
3680
                tx_status->retry_count = tx_resp->failure_frame;
 
3681
                tx_status->queue_number = status & 0xff;
 
3682
                tx_status->queue_length = tx_resp->bt_kill_count;
 
3683
                tx_status->queue_length |= tx_resp->failure_rts;
 
3684
 
 
3685
                tx_status->flags = iwl4965_is_tx_success(status)?
 
3686
                        IEEE80211_TX_STATUS_ACK : 0;
 
3687
                tx_status->control.tx_rate =
 
3688
                                iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags);
 
3689
                /* FIXME: code repetition end */
 
3690
 
 
3691
                IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
 
3692
                                    status & 0xff, tx_resp->failure_frame);
 
3693
                IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
 
3694
                                iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
 
3695
 
 
3696
                agg->wait_for_ba = 0;
 
3697
        } else {
 
3698
                /* Two or more frames were attempted; expect block-ack */
 
3699
                u64 bitmap = 0;
 
3700
                int start = agg->start_idx;
 
3701
 
 
3702
                /* Construct bit-map of pending frames within Tx window */
 
3703
                for (i = 0; i < agg->frame_count; i++) {
 
3704
                        u16 sc;
 
3705
                        status = le32_to_cpu(frame_status[i]);
 
3706
                        seq  = status >> 16;
 
3707
                        idx = SEQ_TO_INDEX(seq);
 
3708
                        txq_id = SEQ_TO_QUEUE(seq);
 
3709
 
 
3710
                        if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
 
3711
                                      AGG_TX_STATE_ABORT_MSK))
 
3712
                                continue;
 
3713
 
 
3714
                        IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
 
3715
                                           agg->frame_count, txq_id, idx);
 
3716
 
 
3717
                        hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
 
3718
 
 
3719
                        sc = le16_to_cpu(hdr->seq_ctrl);
 
3720
                        if (idx != (SEQ_TO_SN(sc) & 0xff)) {
 
3721
                                IWL_ERROR("BUG_ON idx doesn't match seq control"
 
3722
                                          " idx=%d, seq_idx=%d, seq=%d\n",
 
3723
                                          idx, SEQ_TO_SN(sc),
 
3724
                                          hdr->seq_ctrl);
 
3725
                                return -1;
 
3726
                        }
 
3727
 
 
3728
                        IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
 
3729
                                           i, idx, SEQ_TO_SN(sc));
 
3730
 
 
3731
                        sh = idx - start;
 
3732
                        if (sh > 64) {
 
3733
                                sh = (start - idx) + 0xff;
 
3734
                                bitmap = bitmap << sh;
 
3735
                                sh = 0;
 
3736
                                start = idx;
 
3737
                        } else if (sh < -64)
 
3738
                                sh  = 0xff - (start - idx);
 
3739
                        else if (sh < 0) {
 
3740
                                sh = start - idx;
 
3741
                                start = idx;
 
3742
                                bitmap = bitmap << sh;
 
3743
                                sh = 0;
 
3744
                        }
 
3745
                        bitmap |= (1 << sh);
 
3746
                        IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
 
3747
                                           start, (u32)(bitmap & 0xFFFFFFFF));
 
3748
                }
 
3749
 
 
3750
                agg->bitmap0 = bitmap & 0xFFFFFFFF;
 
3751
                agg->bitmap1 = bitmap >> 32;
 
3752
                agg->start_idx = start;
 
3753
                agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
 
3754
                IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%x\n",
 
3755
                                   agg->frame_count, agg->start_idx,
 
3756
                                   agg->bitmap0);
 
3757
 
 
3758
                if (bitmap)
 
3759
                        agg->wait_for_ba = 1;
 
3760
        }
 
3761
        return 0;
 
3762
}
 
3763
#endif
 
3764
#endif
 
3765
 
 
3766
/**
 
3767
 * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
 
3768
 */
 
3769
static void iwl4965_rx_reply_tx(struct iwl4965_priv *priv,
 
3770
                            struct iwl4965_rx_mem_buffer *rxb)
 
3771
{
 
3772
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
3773
        u16 sequence = le16_to_cpu(pkt->hdr.sequence);
 
3774
        int txq_id = SEQ_TO_QUEUE(sequence);
 
3775
        int index = SEQ_TO_INDEX(sequence);
 
3776
        struct iwl4965_tx_queue *txq = &priv->txq[txq_id];
 
3777
        struct iwlwifi_ieee80211_tx_status *tx_status;
 
3778
        struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
 
3779
        u32  status = le32_to_cpu(tx_resp->status);
 
3780
#ifdef CONFIG_IWL4965_HT
 
3781
#ifdef CONFIG_IWL4965_HT_AGG
 
3782
        int tid, sta_id;
 
3783
#endif
 
3784
#endif
 
3785
 
 
3786
        if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
 
3787
                IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
 
3788
                          "is out of range [0-%d] %d %d\n", txq_id,
 
3789
                          index, txq->q.n_bd, txq->q.write_ptr,
 
3790
                          txq->q.read_ptr);
 
3791
                return;
 
3792
        }
 
3793
 
 
3794
#ifdef CONFIG_IWL4965_HT
 
3795
#ifdef CONFIG_IWL4965_HT_AGG
 
3796
        if (txq->sched_retry) {
 
3797
                const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
 
3798
                struct ieee80211_hdr *hdr =
 
3799
                        iwl4965_tx_queue_get_hdr(priv, txq_id, index);
 
3800
                struct iwl4965_ht_agg *agg = NULL;
 
3801
                __le16 *qc = ieee80211_get_qos_ctrl(hdr);
 
3802
 
 
3803
                if (qc == NULL) {
 
3804
                        IWL_ERROR("BUG_ON qc is null!!!!\n");
 
3805
                        return;
 
3806
                }
 
3807
 
 
3808
                tid = le16_to_cpu(*qc) & 0xf;
 
3809
 
 
3810
                sta_id = iwl4965_get_ra_sta_id(priv, hdr);
 
3811
                if (unlikely(sta_id == IWL_INVALID_STATION)) {
 
3812
                        IWL_ERROR("Station not known for\n");
 
3813
                        return;
 
3814
                }
 
3815
 
 
3816
                agg = &priv->stations[sta_id].tid[tid].agg;
 
3817
 
 
3818
                iwl4965_tx_status_reply_tx(priv, agg, tx_resp, index);
 
3819
 
 
3820
                if ((tx_resp->frame_count == 1) &&
 
3821
                    !iwl4965_is_tx_success(status)) {
 
3822
                        /* TODO: send BAR */
 
3823
                }
 
3824
 
 
3825
                if ((txq->q.read_ptr != (scd_ssn & 0xff))) {
 
3826
                        index = iwl4965_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
 
3827
                        IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
 
3828
                                           "%d index %d\n", scd_ssn , index);
 
3829
                        iwl4965_tx_queue_reclaim(priv, txq_id, index);
 
3830
                }
 
3831
        } else {
 
3832
#endif /* CONFIG_IWL4965_HT_AGG */
 
3833
#endif /* CONFIG_IWL4965_HT */
 
3834
        tx_status = &(txq->txb[txq->q.read_ptr].status);
 
3835
 
 
3836
        tx_status->retry_count = tx_resp->failure_frame;
 
3837
        tx_status->queue_number = status;
 
3838
        tx_status->queue_length = tx_resp->bt_kill_count;
 
3839
        tx_status->queue_length |= tx_resp->failure_rts;
 
3840
 
 
3841
        tx_status->flags =
 
3842
            iwl4965_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
 
3843
 
 
3844
        tx_status->control.tx_rate =
 
3845
                iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags);
 
3846
 
 
3847
        IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
 
3848
                     "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
 
3849
                     status, le32_to_cpu(tx_resp->rate_n_flags),
 
3850
                     tx_resp->failure_frame);
 
3851
 
 
3852
        IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
 
3853
        if (index != -1)
 
3854
                iwl4965_tx_queue_reclaim(priv, txq_id, index);
 
3855
#ifdef CONFIG_IWL4965_HT
 
3856
#ifdef CONFIG_IWL4965_HT_AGG
 
3857
        }
 
3858
#endif /* CONFIG_IWL4965_HT_AGG */
 
3859
#endif /* CONFIG_IWL4965_HT */
 
3860
 
 
3861
        if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
 
3862
                IWL_ERROR("TODO:  Implement Tx ABORT REQUIRED!!!\n");
 
3863
}
 
3864
 
 
3865
 
 
3866
static void iwl4965_rx_reply_alive(struct iwl4965_priv *priv,
 
3867
                               struct iwl4965_rx_mem_buffer *rxb)
 
3868
{
 
3869
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
3870
        struct iwl4965_alive_resp *palive;
 
3871
        struct delayed_work *pwork;
 
3872
 
 
3873
        palive = &pkt->u.alive_frame;
 
3874
 
 
3875
        IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
 
3876
                       "0x%01X 0x%01X\n",
 
3877
                       palive->is_valid, palive->ver_type,
 
3878
                       palive->ver_subtype);
 
3879
 
 
3880
        if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
 
3881
                IWL_DEBUG_INFO("Initialization Alive received.\n");
 
3882
                memcpy(&priv->card_alive_init,
 
3883
                       &pkt->u.alive_frame,
 
3884
                       sizeof(struct iwl4965_init_alive_resp));
 
3885
                pwork = &priv->init_alive_start;
 
3886
        } else {
 
3887
                IWL_DEBUG_INFO("Runtime Alive received.\n");
 
3888
                memcpy(&priv->card_alive, &pkt->u.alive_frame,
 
3889
                       sizeof(struct iwl4965_alive_resp));
 
3890
                pwork = &priv->alive_start;
 
3891
        }
 
3892
 
 
3893
        /* We delay the ALIVE response by 5ms to
 
3894
         * give the HW RF Kill time to activate... */
 
3895
        if (palive->is_valid == UCODE_VALID_OK)
 
3896
                queue_delayed_work(priv->workqueue, pwork,
 
3897
                                   msecs_to_jiffies(5));
 
3898
        else
 
3899
                IWL_WARNING("uCode did not respond OK.\n");
 
3900
}
 
3901
 
 
3902
static void iwl4965_rx_reply_add_sta(struct iwl4965_priv *priv,
 
3903
                                 struct iwl4965_rx_mem_buffer *rxb)
 
3904
{
 
3905
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
3906
 
 
3907
        IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
 
3908
        return;
 
3909
}
 
3910
 
 
3911
static void iwl4965_rx_reply_error(struct iwl4965_priv *priv,
 
3912
                               struct iwl4965_rx_mem_buffer *rxb)
 
3913
{
 
3914
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
3915
 
 
3916
        IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
 
3917
                "seq 0x%04X ser 0x%08X\n",
 
3918
                le32_to_cpu(pkt->u.err_resp.error_type),
 
3919
                get_cmd_string(pkt->u.err_resp.cmd_id),
 
3920
                pkt->u.err_resp.cmd_id,
 
3921
                le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
 
3922
                le32_to_cpu(pkt->u.err_resp.error_info));
 
3923
}
 
3924
 
 
3925
#define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
 
3926
 
 
3927
static void iwl4965_rx_csa(struct iwl4965_priv *priv, struct iwl4965_rx_mem_buffer *rxb)
 
3928
{
 
3929
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
3930
        struct iwl4965_rxon_cmd *rxon = (void *)&priv->active_rxon;
 
3931
        struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
 
3932
        IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
 
3933
                      le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
 
3934
        rxon->channel = csa->channel;
 
3935
        priv->staging_rxon.channel = csa->channel;
 
3936
}
 
3937
 
 
3938
static void iwl4965_rx_spectrum_measure_notif(struct iwl4965_priv *priv,
 
3939
                                          struct iwl4965_rx_mem_buffer *rxb)
 
3940
{
 
3941
#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
 
3942
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
3943
        struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
 
3944
 
 
3945
        if (!report->state) {
 
3946
                IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
 
3947
                          "Spectrum Measure Notification: Start\n");
 
3948
                return;
 
3949
        }
 
3950
 
 
3951
        memcpy(&priv->measure_report, report, sizeof(*report));
 
3952
        priv->measurement_status |= MEASUREMENT_READY;
 
3953
#endif
 
3954
}
 
3955
 
 
3956
static void iwl4965_rx_pm_sleep_notif(struct iwl4965_priv *priv,
 
3957
                                  struct iwl4965_rx_mem_buffer *rxb)
 
3958
{
 
3959
#ifdef CONFIG_IWL4965_DEBUG
 
3960
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
3961
        struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
 
3962
        IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
 
3963
                     sleep->pm_sleep_mode, sleep->pm_wakeup_src);
 
3964
#endif
 
3965
}
 
3966
 
 
3967
static void iwl4965_rx_pm_debug_statistics_notif(struct iwl4965_priv *priv,
 
3968
                                             struct iwl4965_rx_mem_buffer *rxb)
 
3969
{
 
3970
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
3971
        IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
 
3972
                        "notification for %s:\n",
 
3973
                        le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
 
3974
        iwl4965_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
 
3975
}
 
3976
 
 
3977
static void iwl4965_bg_beacon_update(struct work_struct *work)
 
3978
{
 
3979
        struct iwl4965_priv *priv =
 
3980
                container_of(work, struct iwl4965_priv, beacon_update);
 
3981
        struct sk_buff *beacon;
 
3982
 
 
3983
        /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
 
3984
        beacon = iwlwifi_ieee80211_beacon_get(priv->hw, priv->interface_id, NULL);
 
3985
 
 
3986
        if (!beacon) {
 
3987
                IWL_ERROR("update beacon failed\n");
 
3988
                return;
 
3989
        }
 
3990
 
 
3991
        mutex_lock(&priv->mutex);
 
3992
        /* new beacon skb is allocated every time; dispose previous.*/
 
3993
        if (priv->ibss_beacon)
 
3994
                dev_kfree_skb(priv->ibss_beacon);
 
3995
 
 
3996
        priv->ibss_beacon = beacon;
 
3997
        mutex_unlock(&priv->mutex);
 
3998
 
 
3999
        iwl4965_send_beacon_cmd(priv);
 
4000
}
 
4001
 
 
4002
static void iwl4965_rx_beacon_notif(struct iwl4965_priv *priv,
 
4003
                                struct iwl4965_rx_mem_buffer *rxb)
 
4004
{
 
4005
#ifdef CONFIG_IWL4965_DEBUG
 
4006
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
4007
        struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
 
4008
        u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
 
4009
 
 
4010
        IWL_DEBUG_RX("beacon status %x retries %d iss %d "
 
4011
                "tsf %d %d rate %d\n",
 
4012
                le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
 
4013
                beacon->beacon_notify_hdr.failure_frame,
 
4014
                le32_to_cpu(beacon->ibss_mgr_status),
 
4015
                le32_to_cpu(beacon->high_tsf),
 
4016
                le32_to_cpu(beacon->low_tsf), rate);
 
4017
#endif
 
4018
 
 
4019
        if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
 
4020
            (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
 
4021
                queue_work(priv->workqueue, &priv->beacon_update);
 
4022
}
 
4023
 
 
4024
/* Service response to REPLY_SCAN_CMD (0x80) */
 
4025
static void iwl4965_rx_reply_scan(struct iwl4965_priv *priv,
 
4026
                              struct iwl4965_rx_mem_buffer *rxb)
 
4027
{
 
4028
#ifdef CONFIG_IWL4965_DEBUG
 
4029
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
4030
        struct iwl4965_scanreq_notification *notif =
 
4031
            (struct iwl4965_scanreq_notification *)pkt->u.raw;
 
4032
 
 
4033
        IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
 
4034
#endif
 
4035
}
 
4036
 
 
4037
/* Service SCAN_START_NOTIFICATION (0x82) */
 
4038
static void iwl4965_rx_scan_start_notif(struct iwl4965_priv *priv,
 
4039
                                    struct iwl4965_rx_mem_buffer *rxb)
 
4040
{
 
4041
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
4042
        struct iwl4965_scanstart_notification *notif =
 
4043
            (struct iwl4965_scanstart_notification *)pkt->u.raw;
 
4044
        priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
 
4045
        IWL_DEBUG_SCAN("Scan start: "
 
4046
                       "%d [802.11%s] "
 
4047
                       "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
 
4048
                       notif->channel,
 
4049
                       notif->band ? "bg" : "a",
 
4050
                       notif->tsf_high,
 
4051
                       notif->tsf_low, notif->status, notif->beacon_timer);
 
4052
}
 
4053
 
 
4054
/* Service SCAN_RESULTS_NOTIFICATION (0x83) */
 
4055
static void iwl4965_rx_scan_results_notif(struct iwl4965_priv *priv,
 
4056
                                      struct iwl4965_rx_mem_buffer *rxb)
 
4057
{
 
4058
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
4059
        struct iwl4965_scanresults_notification *notif =
 
4060
            (struct iwl4965_scanresults_notification *)pkt->u.raw;
 
4061
 
 
4062
        IWL_DEBUG_SCAN("Scan ch.res: "
 
4063
                       "%d [802.11%s] "
 
4064
                       "(TSF: 0x%08X:%08X) - %d "
 
4065
                       "elapsed=%lu usec (%dms since last)\n",
 
4066
                       notif->channel,
 
4067
                       notif->band ? "bg" : "a",
 
4068
                       le32_to_cpu(notif->tsf_high),
 
4069
                       le32_to_cpu(notif->tsf_low),
 
4070
                       le32_to_cpu(notif->statistics[0]),
 
4071
                       le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
 
4072
                       jiffies_to_msecs(elapsed_jiffies
 
4073
                                        (priv->last_scan_jiffies, jiffies)));
 
4074
 
 
4075
        priv->last_scan_jiffies = jiffies;
 
4076
        priv->next_scan_jiffies = 0;
 
4077
}
 
4078
 
 
4079
/* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
 
4080
static void iwl4965_rx_scan_complete_notif(struct iwl4965_priv *priv,
 
4081
                                       struct iwl4965_rx_mem_buffer *rxb)
 
4082
{
 
4083
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
4084
        struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
 
4085
 
 
4086
        IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
 
4087
                       scan_notif->scanned_channels,
 
4088
                       scan_notif->tsf_low,
 
4089
                       scan_notif->tsf_high, scan_notif->status);
 
4090
 
 
4091
        /* The HW is no longer scanning */
 
4092
        clear_bit(STATUS_SCAN_HW, &priv->status);
 
4093
 
 
4094
        /* The scan completion notification came in, so kill that timer... */
 
4095
        cancel_delayed_work(&priv->scan_check);
 
4096
 
 
4097
        IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
 
4098
                       (priv->scan_bands == 2) ? "2.4" : "5.2",
 
4099
                       jiffies_to_msecs(elapsed_jiffies
 
4100
                                        (priv->scan_pass_start, jiffies)));
 
4101
 
 
4102
        /* Remove this scanned band from the list
 
4103
         * of pending bands to scan */
 
4104
        priv->scan_bands--;
 
4105
 
 
4106
        /* If a request to abort was given, or the scan did not succeed
 
4107
         * then we reset the scan state machine and terminate,
 
4108
         * re-queuing another scan if one has been requested */
 
4109
        if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
 
4110
                IWL_DEBUG_INFO("Aborted scan completed.\n");
 
4111
                clear_bit(STATUS_SCAN_ABORTING, &priv->status);
 
4112
        } else {
 
4113
                /* If there are more bands on this scan pass reschedule */
 
4114
                if (priv->scan_bands > 0)
 
4115
                        goto reschedule;
 
4116
        }
 
4117
 
 
4118
        priv->last_scan_jiffies = jiffies;
 
4119
        priv->next_scan_jiffies = 0;
 
4120
        IWL_DEBUG_INFO("Setting scan to off\n");
 
4121
 
 
4122
        clear_bit(STATUS_SCANNING, &priv->status);
 
4123
 
 
4124
        IWL_DEBUG_INFO("Scan took %dms\n",
 
4125
                jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
 
4126
 
 
4127
        queue_work(priv->workqueue, &priv->scan_completed);
 
4128
 
 
4129
        return;
 
4130
 
 
4131
reschedule:
 
4132
        priv->scan_pass_start = jiffies;
 
4133
        queue_work(priv->workqueue, &priv->request_scan);
 
4134
}
 
4135
 
 
4136
/* Handle notification from uCode that card's power state is changing
 
4137
 * due to software, hardware, or critical temperature RFKILL */
 
4138
static void iwl4965_rx_card_state_notif(struct iwl4965_priv *priv,
 
4139
                                    struct iwl4965_rx_mem_buffer *rxb)
 
4140
{
 
4141
        struct iwl4965_rx_packet *pkt = (void *)rxb->skb->data;
 
4142
        u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
 
4143
        unsigned long status = priv->status;
 
4144
 
 
4145
        IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
 
4146
                          (flags & HW_CARD_DISABLED) ? "Kill" : "On",
 
4147
                          (flags & SW_CARD_DISABLED) ? "Kill" : "On");
 
4148
 
 
4149
        if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
 
4150
                     RF_CARD_DISABLED)) {
 
4151
 
 
4152
                iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
 
4153
                            CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
 
4154
 
 
4155
                if (!iwl4965_grab_nic_access(priv)) {
 
4156
                        iwl4965_write_direct32(
 
4157
                                priv, HBUS_TARG_MBX_C,
 
4158
                                HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
 
4159
 
 
4160
                        iwl4965_release_nic_access(priv);
 
4161
                }
 
4162
 
 
4163
                if (!(flags & RXON_CARD_DISABLED)) {
 
4164
                        iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
 
4165
                                    CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
 
4166
                        if (!iwl4965_grab_nic_access(priv)) {
 
4167
                                iwl4965_write_direct32(
 
4168
                                        priv, HBUS_TARG_MBX_C,
 
4169
                                        HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
 
4170
 
 
4171
                                iwl4965_release_nic_access(priv);
 
4172
                        }
 
4173
                }
 
4174
 
 
4175
                if (flags & RF_CARD_DISABLED) {
 
4176
                        iwl4965_write32(priv, CSR_UCODE_DRV_GP1_SET,
 
4177
                                    CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
 
4178
                        iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
 
4179
                        if (!iwl4965_grab_nic_access(priv))
 
4180
                                iwl4965_release_nic_access(priv);
 
4181
                }
 
4182
        }
 
4183
 
 
4184
        if (flags & HW_CARD_DISABLED)
 
4185
                set_bit(STATUS_RF_KILL_HW, &priv->status);
 
4186
        else
 
4187
                clear_bit(STATUS_RF_KILL_HW, &priv->status);
 
4188
 
 
4189
 
 
4190
        if (flags & SW_CARD_DISABLED)
 
4191
                set_bit(STATUS_RF_KILL_SW, &priv->status);
 
4192
        else
 
4193
                clear_bit(STATUS_RF_KILL_SW, &priv->status);
 
4194
 
 
4195
        if (!(flags & RXON_CARD_DISABLED))
 
4196
                iwl4965_scan_cancel(priv);
 
4197
 
 
4198
        if ((test_bit(STATUS_RF_KILL_HW, &status) !=
 
4199
             test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
 
4200
            (test_bit(STATUS_RF_KILL_SW, &status) !=
 
4201
             test_bit(STATUS_RF_KILL_SW, &priv->status)))
 
4202
                queue_work(priv->workqueue, &priv->rf_kill);
 
4203
        else
 
4204
                wake_up_interruptible(&priv->wait_command_queue);
 
4205
}
 
4206
 
 
4207
/**
 
4208
 * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
 
4209
 *
 
4210
 * Setup the RX handlers for each of the reply types sent from the uCode
 
4211
 * to the host.
 
4212
 *
 
4213
 * This function chains into the hardware specific files for them to setup
 
4214
 * any hardware specific handlers as well.
 
4215
 */
 
4216
static void iwl4965_setup_rx_handlers(struct iwl4965_priv *priv)
 
4217
{
 
4218
        priv->rx_handlers[REPLY_ALIVE] = iwl4965_rx_reply_alive;
 
4219
        priv->rx_handlers[REPLY_ADD_STA] = iwl4965_rx_reply_add_sta;
 
4220
        priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
 
4221
        priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
 
4222
        priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
 
4223
            iwl4965_rx_spectrum_measure_notif;
 
4224
        priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
 
4225
        priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
 
4226
            iwl4965_rx_pm_debug_statistics_notif;
 
4227
        priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
 
4228
 
 
4229
        /*
 
4230
         * The same handler is used for both the REPLY to a discrete
 
4231
         * statistics request from the host as well as for the periodic
 
4232
         * statistics notifications (after received beacons) from the uCode.
 
4233
         */
 
4234
        priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
 
4235
        priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
 
4236
 
 
4237
        priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
 
4238
        priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
 
4239
        priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
 
4240
            iwl4965_rx_scan_results_notif;
 
4241
        priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
 
4242
            iwl4965_rx_scan_complete_notif;
 
4243
        priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
 
4244
        priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
 
4245
 
 
4246
        /* Set up hardware specific Rx handlers */
 
4247
        iwl4965_hw_rx_handler_setup(priv);
 
4248
}
 
4249
 
 
4250
/**
 
4251
 * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
 
4252
 * @rxb: Rx buffer to reclaim
 
4253
 *
 
4254
 * If an Rx buffer has an async callback associated with it the callback
 
4255
 * will be executed.  The attached skb (if present) will only be freed
 
4256
 * if the callback returns 1
 
4257
 */
 
4258
static void iwl4965_tx_cmd_complete(struct iwl4965_priv *priv,
 
4259
                                struct iwl4965_rx_mem_buffer *rxb)
 
4260
{
 
4261
        struct iwl4965_rx_packet *pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
 
4262
        u16 sequence = le16_to_cpu(pkt->hdr.sequence);
 
4263
        int txq_id = SEQ_TO_QUEUE(sequence);
 
4264
        int index = SEQ_TO_INDEX(sequence);
 
4265
        int huge = sequence & SEQ_HUGE_FRAME;
 
4266
        int cmd_index;
 
4267
        struct iwl4965_cmd *cmd;
 
4268
 
 
4269
        /* If a Tx command is being handled and it isn't in the actual
 
4270
         * command queue then there a command routing bug has been introduced
 
4271
         * in the queue management code. */
 
4272
        if (txq_id != IWL_CMD_QUEUE_NUM)
 
4273
                IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
 
4274
                          txq_id, pkt->hdr.cmd);
 
4275
        BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
 
4276
 
 
4277
        cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
 
4278
        cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
 
4279
 
 
4280
        /* Input error checking is done when commands are added to queue. */
 
4281
        if (cmd->meta.flags & CMD_WANT_SKB) {
 
4282
                cmd->meta.source->u.skb = rxb->skb;
 
4283
                rxb->skb = NULL;
 
4284
        } else if (cmd->meta.u.callback &&
 
4285
                   !cmd->meta.u.callback(priv, cmd, rxb->skb))
 
4286
                rxb->skb = NULL;
 
4287
 
 
4288
        iwl4965_tx_queue_reclaim(priv, txq_id, index);
 
4289
 
 
4290
        if (!(cmd->meta.flags & CMD_ASYNC)) {
 
4291
                clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
 
4292
                wake_up_interruptible(&priv->wait_command_queue);
 
4293
        }
 
4294
}
 
4295
 
 
4296
/************************** RX-FUNCTIONS ****************************/
 
4297
/*
 
4298
 * Rx theory of operation
 
4299
 *
 
4300
 * Driver allocates a circular buffer of Receive Buffer Descriptors (RBDs),
 
4301
 * each of which point to Receive Buffers to be filled by 4965.  These get
 
4302
 * used not only for Rx frames, but for any command response or notification
 
4303
 * from the 4965.  The driver and 4965 manage the Rx buffers by means
 
4304
 * of indexes into the circular buffer.
 
4305
 *
 
4306
 * Rx Queue Indexes
 
4307
 * The host/firmware share two index registers for managing the Rx buffers.
 
4308
 *
 
4309
 * The READ index maps to the first position that the firmware may be writing
 
4310
 * to -- the driver can read up to (but not including) this position and get
 
4311
 * good data.
 
4312
 * The READ index is managed by the firmware once the card is enabled.
 
4313
 *
 
4314
 * The WRITE index maps to the last position the driver has read from -- the
 
4315
 * position preceding WRITE is the last slot the firmware can place a packet.
 
4316
 *
 
4317
 * The queue is empty (no good data) if WRITE = READ - 1, and is full if
 
4318
 * WRITE = READ.
 
4319
 *
 
4320
 * During initialization, the host sets up the READ queue position to the first
 
4321
 * INDEX position, and WRITE to the last (READ - 1 wrapped)
 
4322
 *
 
4323
 * When the firmware places a packet in a buffer, it will advance the READ index
 
4324
 * and fire the RX interrupt.  The driver can then query the READ index and
 
4325
 * process as many packets as possible, moving the WRITE index forward as it
 
4326
 * resets the Rx queue buffers with new memory.
 
4327
 *
 
4328
 * The management in the driver is as follows:
 
4329
 * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free.  When
 
4330
 *   iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
 
4331
 *   to replenish the iwl->rxq->rx_free.
 
4332
 * + In iwl4965_rx_replenish (scheduled) if 'processed' != 'read' then the
 
4333
 *   iwl->rxq is replenished and the READ INDEX is updated (updating the
 
4334
 *   'processed' and 'read' driver indexes as well)
 
4335
 * + A received packet is processed and handed to the kernel network stack,
 
4336
 *   detached from the iwl->rxq.  The driver 'processed' index is updated.
 
4337
 * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
 
4338
 *   list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
 
4339
 *   INDEX is not incremented and iwl->status(RX_STALLED) is set.  If there
 
4340
 *   were enough free buffers and RX_STALLED is set it is cleared.
 
4341
 *
 
4342
 *
 
4343
 * Driver sequence:
 
4344
 *
 
4345
 * iwl4965_rx_queue_alloc()   Allocates rx_free
 
4346
 * iwl4965_rx_replenish()     Replenishes rx_free list from rx_used, and calls
 
4347
 *                            iwl4965_rx_queue_restock
 
4348
 * iwl4965_rx_queue_restock() Moves available buffers from rx_free into Rx
 
4349
 *                            queue, updates firmware pointers, and updates
 
4350
 *                            the WRITE index.  If insufficient rx_free buffers
 
4351
 *                            are available, schedules iwl4965_rx_replenish
 
4352
 *
 
4353
 * -- enable interrupts --
 
4354
 * ISR - iwl4965_rx()         Detach iwl4965_rx_mem_buffers from pool up to the
 
4355
 *                            READ INDEX, detaching the SKB from the pool.
 
4356
 *                            Moves the packet buffer from queue to rx_used.
 
4357
 *                            Calls iwl4965_rx_queue_restock to refill any empty
 
4358
 *                            slots.
 
4359
 * ...
 
4360
 *
 
4361
 */
 
4362
 
 
4363
/**
 
4364
 * iwl4965_rx_queue_space - Return number of free slots available in queue.
 
4365
 */
 
4366
static int iwl4965_rx_queue_space(const struct iwl4965_rx_queue *q)
 
4367
{
 
4368
        int s = q->read - q->write;
 
4369
        if (s <= 0)
 
4370
                s += RX_QUEUE_SIZE;
 
4371
        /* keep some buffer to not confuse full and empty queue */
 
4372
        s -= 2;
 
4373
        if (s < 0)
 
4374
                s = 0;
 
4375
        return s;
 
4376
}
 
4377
 
 
4378
/**
 
4379
 * iwl4965_rx_queue_update_write_ptr - Update the write pointer for the RX queue
 
4380
 */
 
4381
int iwl4965_rx_queue_update_write_ptr(struct iwl4965_priv *priv, struct iwl4965_rx_queue *q)
 
4382
{
 
4383
        u32 reg = 0;
 
4384
        int rc = 0;
 
4385
        unsigned long flags;
 
4386
 
 
4387
        spin_lock_irqsave(&q->lock, flags);
 
4388
 
 
4389
        if (q->need_update == 0)
 
4390
                goto exit_unlock;
 
4391
 
 
4392
        /* If power-saving is in use, make sure device is awake */
 
4393
        if (test_bit(STATUS_POWER_PMI, &priv->status)) {
 
4394
                reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
 
4395
 
 
4396
                if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
 
4397
                        iwl4965_set_bit(priv, CSR_GP_CNTRL,
 
4398
                                    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
 
4399
                        goto exit_unlock;
 
4400
                }
 
4401
 
 
4402
                rc = iwl4965_grab_nic_access(priv);
 
4403
                if (rc)
 
4404
                        goto exit_unlock;
 
4405
 
 
4406
                /* Device expects a multiple of 8 */
 
4407
                iwl4965_write_direct32(priv, FH_RSCSR_CHNL0_WPTR,
 
4408
                                     q->write & ~0x7);
 
4409
                iwl4965_release_nic_access(priv);
 
4410
 
 
4411
        /* Else device is assumed to be awake */
 
4412
        } else
 
4413
                /* Device expects a multiple of 8 */
 
4414
                iwl4965_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
 
4415
 
 
4416
 
 
4417
        q->need_update = 0;
 
4418
 
 
4419
 exit_unlock:
 
4420
        spin_unlock_irqrestore(&q->lock, flags);
 
4421
        return rc;
 
4422
}
 
4423
 
 
4424
/**
 
4425
 * iwl4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
 
4426
 */
 
4427
static inline __le32 iwl4965_dma_addr2rbd_ptr(struct iwl4965_priv *priv,
 
4428
                                          dma_addr_t dma_addr)
 
4429
{
 
4430
        return cpu_to_le32((u32)(dma_addr >> 8));
 
4431
}
 
4432
 
 
4433
 
 
4434
/**
 
4435
 * iwl4965_rx_queue_restock - refill RX queue from pre-allocated pool
 
4436
 *
 
4437
 * If there are slots in the RX queue that need to be restocked,
 
4438
 * and we have free pre-allocated buffers, fill the ranks as much
 
4439
 * as we can, pulling from rx_free.
 
4440
 *
 
4441
 * This moves the 'write' index forward to catch up with 'processed', and
 
4442
 * also updates the memory address in the firmware to reference the new
 
4443
 * target buffer.
 
4444
 */
 
4445
static int iwl4965_rx_queue_restock(struct iwl4965_priv *priv)
 
4446
{
 
4447
        struct iwl4965_rx_queue *rxq = &priv->rxq;
 
4448
        struct list_head *element;
 
4449
        struct iwl4965_rx_mem_buffer *rxb;
 
4450
        unsigned long flags;
 
4451
        int write, rc;
 
4452
 
 
4453
        spin_lock_irqsave(&rxq->lock, flags);
 
4454
        write = rxq->write & ~0x7;
 
4455
        while ((iwl4965_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
 
4456
                /* Get next free Rx buffer, remove from free list */
 
4457
                element = rxq->rx_free.next;
 
4458
                rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
 
4459
                list_del(element);
 
4460
 
 
4461
                /* Point to Rx buffer via next RBD in circular buffer */
 
4462
                rxq->bd[rxq->write] = iwl4965_dma_addr2rbd_ptr(priv, rxb->dma_addr);
 
4463
                rxq->queue[rxq->write] = rxb;
 
4464
                rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
 
4465
                rxq->free_count--;
 
4466
        }
 
4467
        spin_unlock_irqrestore(&rxq->lock, flags);
 
4468
        /* If the pre-allocated buffer pool is dropping low, schedule to
 
4469
         * refill it */
 
4470
        if (rxq->free_count <= RX_LOW_WATERMARK)
 
4471
                queue_work(priv->workqueue, &priv->rx_replenish);
 
4472
 
 
4473
 
 
4474
        /* If we've added more space for the firmware to place data, tell it.
 
4475
         * Increment device's write pointer in multiples of 8. */
 
4476
        if ((write != (rxq->write & ~0x7))
 
4477
            || (abs(rxq->write - rxq->read) > 7)) {
 
4478
                spin_lock_irqsave(&rxq->lock, flags);
 
4479
                rxq->need_update = 1;
 
4480
                spin_unlock_irqrestore(&rxq->lock, flags);
 
4481
                rc = iwl4965_rx_queue_update_write_ptr(priv, rxq);
 
4482
                if (rc)
 
4483
                        return rc;
 
4484
        }
 
4485
 
 
4486
        return 0;
 
4487
}
 
4488
 
 
4489
/**
 
4490
 * iwl4965_rx_replenish - Move all used packet from rx_used to rx_free
 
4491
 *
 
4492
 * When moving to rx_free an SKB is allocated for the slot.
 
4493
 *
 
4494
 * Also restock the Rx queue via iwl4965_rx_queue_restock.
 
4495
 * This is called as a scheduled work item (except for during initialization)
 
4496
 */
 
4497
static void iwl4965_rx_allocate(struct iwl4965_priv *priv)
 
4498
{
 
4499
        struct iwl4965_rx_queue *rxq = &priv->rxq;
 
4500
        struct list_head *element;
 
4501
        struct iwl4965_rx_mem_buffer *rxb;
 
4502
        unsigned long flags;
 
4503
        spin_lock_irqsave(&rxq->lock, flags);
 
4504
        while (!list_empty(&rxq->rx_used)) {
 
4505
                element = rxq->rx_used.next;
 
4506
                rxb = list_entry(element, struct iwl4965_rx_mem_buffer, list);
 
4507
 
 
4508
                /* Alloc a new receive buffer */
 
4509
                rxb->skb =
 
4510
                    alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
 
4511
                if (!rxb->skb) {
 
4512
                        if (net_ratelimit())
 
4513
                                printk(KERN_CRIT DRV_NAME
 
4514
                                       ": Can not allocate SKB buffers\n");
 
4515
                        /* We don't reschedule replenish work here -- we will
 
4516
                         * call the restock method and if it still needs
 
4517
                         * more buffers it will schedule replenish */
 
4518
                        break;
 
4519
                }
 
4520
                priv->alloc_rxb_skb++;
 
4521
                list_del(element);
 
4522
 
 
4523
                /* Get physical address of RB/SKB */
 
4524
                rxb->dma_addr =
 
4525
                    pci_map_single(priv->pci_dev, rxb->skb->data,
 
4526
                                   IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
 
4527
                list_add_tail(&rxb->list, &rxq->rx_free);
 
4528
                rxq->free_count++;
 
4529
        }
 
4530
        spin_unlock_irqrestore(&rxq->lock, flags);
 
4531
}
 
4532
 
 
4533
/*
 
4534
 * this should be called while priv->lock is locked
 
4535
*/
 
4536
static void __iwl4965_rx_replenish(void *data)
 
4537
{
 
4538
        struct iwl4965_priv *priv = data;
 
4539
 
 
4540
        iwl4965_rx_allocate(priv);
 
4541
        iwl4965_rx_queue_restock(priv);
 
4542
}
 
4543
 
 
4544
 
 
4545
void iwl4965_rx_replenish(void *data)
 
4546
{
 
4547
        struct iwl4965_priv *priv = data;
 
4548
        unsigned long flags;
 
4549
 
 
4550
        iwl4965_rx_allocate(priv);
 
4551
 
 
4552
        spin_lock_irqsave(&priv->lock, flags);
 
4553
        iwl4965_rx_queue_restock(priv);
 
4554
        spin_unlock_irqrestore(&priv->lock, flags);
 
4555
}
 
4556
 
 
4557
/* Assumes that the skb field of the buffers in 'pool' is kept accurate.
 
4558
 * If an SKB has been detached, the POOL needs to have its SKB set to NULL
 
4559
 * This free routine walks the list of POOL entries and if SKB is set to
 
4560
 * non NULL it is unmapped and freed
 
4561
 */
 
4562
static void iwl4965_rx_queue_free(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
 
4563
{
 
4564
        int i;
 
4565
        for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
 
4566
                if (rxq->pool[i].skb != NULL) {
 
4567
                        pci_unmap_single(priv->pci_dev,
 
4568
                                         rxq->pool[i].dma_addr,
 
4569
                                         IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
 
4570
                        dev_kfree_skb(rxq->pool[i].skb);
 
4571
                }
 
4572
        }
 
4573
 
 
4574
        pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
 
4575
                            rxq->dma_addr);
 
4576
        rxq->bd = NULL;
 
4577
}
 
4578
 
 
4579
int iwl4965_rx_queue_alloc(struct iwl4965_priv *priv)
 
4580
{
 
4581
        struct iwl4965_rx_queue *rxq = &priv->rxq;
 
4582
        struct pci_dev *dev = priv->pci_dev;
 
4583
        int i;
 
4584
 
 
4585
        spin_lock_init(&rxq->lock);
 
4586
        INIT_LIST_HEAD(&rxq->rx_free);
 
4587
        INIT_LIST_HEAD(&rxq->rx_used);
 
4588
 
 
4589
        /* Alloc the circular buffer of Read Buffer Descriptors (RBDs) */
 
4590
        rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
 
4591
        if (!rxq->bd)
 
4592
                return -ENOMEM;
 
4593
 
 
4594
        /* Fill the rx_used queue with _all_ of the Rx buffers */
 
4595
        for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
 
4596
                list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
 
4597
 
 
4598
        /* Set us so that we have processed and used all buffers, but have
 
4599
         * not restocked the Rx queue with fresh buffers */
 
4600
        rxq->read = rxq->write = 0;
 
4601
        rxq->free_count = 0;
 
4602
        rxq->need_update = 0;
 
4603
        return 0;
 
4604
}
 
4605
 
 
4606
void iwl4965_rx_queue_reset(struct iwl4965_priv *priv, struct iwl4965_rx_queue *rxq)
 
4607
{
 
4608
        unsigned long flags;
 
4609
        int i;
 
4610
        spin_lock_irqsave(&rxq->lock, flags);
 
4611
        INIT_LIST_HEAD(&rxq->rx_free);
 
4612
        INIT_LIST_HEAD(&rxq->rx_used);
 
4613
        /* Fill the rx_used queue with _all_ of the Rx buffers */
 
4614
        for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
 
4615
                /* In the reset function, these buffers may have been allocated
 
4616
                 * to an SKB, so we need to unmap and free potential storage */
 
4617
                if (rxq->pool[i].skb != NULL) {
 
4618
                        pci_unmap_single(priv->pci_dev,
 
4619
                                         rxq->pool[i].dma_addr,
 
4620
                                         IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
 
4621
                        priv->alloc_rxb_skb--;
 
4622
                        dev_kfree_skb(rxq->pool[i].skb);
 
4623
                        rxq->pool[i].skb = NULL;
 
4624
                }
 
4625
                list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
 
4626
        }
 
4627
 
 
4628
        /* Set us so that we have processed and used all buffers, but have
 
4629
         * not restocked the Rx queue with fresh buffers */
 
4630
        rxq->read = rxq->write = 0;
 
4631
        rxq->free_count = 0;
 
4632
        spin_unlock_irqrestore(&rxq->lock, flags);
 
4633
}
 
4634
 
 
4635
/* Convert linear signal-to-noise ratio into dB */
 
4636
static u8 ratio2dB[100] = {
 
4637
/*       0   1   2   3   4   5   6   7   8   9 */
 
4638
         0,  0,  6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
 
4639
        20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
 
4640
        26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
 
4641
        29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
 
4642
        32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
 
4643
        34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
 
4644
        36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
 
4645
        37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
 
4646
        38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
 
4647
        39, 39, 39, 39, 39, 40, 40, 40, 40, 40  /* 90 - 99 */
 
4648
};
 
4649
 
 
4650
/* Calculates a relative dB value from a ratio of linear
 
4651
 *   (i.e. not dB) signal levels.
 
4652
 * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
 
4653
int iwl4965_calc_db_from_ratio(int sig_ratio)
 
4654
{
 
4655
        /* 1000:1 or higher just report as 60 dB */
 
4656
        if (sig_ratio >= 1000)
 
4657
                return 60;
 
4658
 
 
4659
        /* 100:1 or higher, divide by 10 and use table,
 
4660
         *   add 20 dB to make up for divide by 10 */
 
4661
        if (sig_ratio >= 100)
 
4662
                return (20 + (int)ratio2dB[sig_ratio/10]);
 
4663
 
 
4664
        /* We shouldn't see this */
 
4665
        if (sig_ratio < 1)
 
4666
                return 0;
 
4667
 
 
4668
        /* Use table for ratios 1:1 - 99:1 */
 
4669
        return (int)ratio2dB[sig_ratio];
 
4670
}
 
4671
 
 
4672
#define PERFECT_RSSI (-20) /* dBm */
 
4673
#define WORST_RSSI (-95)   /* dBm */
 
4674
#define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
 
4675
 
 
4676
/* Calculate an indication of rx signal quality (a percentage, not dBm!).
 
4677
 * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
 
4678
 *   about formulas used below. */
 
4679
int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
 
4680
{
 
4681
        int sig_qual;
 
4682
        int degradation = PERFECT_RSSI - rssi_dbm;
 
4683
 
 
4684
        /* If we get a noise measurement, use signal-to-noise ratio (SNR)
 
4685
         * as indicator; formula is (signal dbm - noise dbm).
 
4686
         * SNR at or above 40 is a great signal (100%).
 
4687
         * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
 
4688
         * Weakest usable signal is usually 10 - 15 dB SNR. */
 
4689
        if (noise_dbm) {
 
4690
                if (rssi_dbm - noise_dbm >= 40)
 
4691
                        return 100;
 
4692
                else if (rssi_dbm < noise_dbm)
 
4693
                        return 0;
 
4694
                sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
 
4695
 
 
4696
        /* Else use just the signal level.
 
4697
         * This formula is a least squares fit of data points collected and
 
4698
         *   compared with a reference system that had a percentage (%) display
 
4699
         *   for signal quality. */
 
4700
        } else
 
4701
                sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
 
4702
                            (15 * RSSI_RANGE + 62 * degradation)) /
 
4703
                           (RSSI_RANGE * RSSI_RANGE);
 
4704
 
 
4705
        if (sig_qual > 100)
 
4706
                sig_qual = 100;
 
4707
        else if (sig_qual < 1)
 
4708
                sig_qual = 0;
 
4709
 
 
4710
        return sig_qual;
 
4711
}
 
4712
 
 
4713
/**
 
4714
 * iwl4965_rx_handle - Main entry function for receiving responses from uCode
 
4715
 *
 
4716
 * Uses the priv->rx_handlers callback function array to invoke
 
4717
 * the appropriate handlers, including command responses,
 
4718
 * frame-received notifications, and other notifications.
 
4719
 */
 
4720
static void iwl4965_rx_handle(struct iwl4965_priv *priv)
 
4721
{
 
4722
        struct iwl4965_rx_mem_buffer *rxb;
 
4723
        struct iwl4965_rx_packet *pkt;
 
4724
        struct iwl4965_rx_queue *rxq = &priv->rxq;
 
4725
        u32 r, i;
 
4726
        int reclaim;
 
4727
        unsigned long flags;
 
4728
        u8 fill_rx = 0;
 
4729
        u32 count = 0;
 
4730
 
 
4731
        /* uCode's read index (stored in shared DRAM) indicates the last Rx
 
4732
         * buffer that the driver may process (last buffer filled by ucode). */
 
4733
        r = iwl4965_hw_get_rx_read(priv);
 
4734
        i = rxq->read;
 
4735
 
 
4736
        /* Rx interrupt, but nothing sent from uCode */
 
4737
        if (i == r)
 
4738
                IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
 
4739
 
 
4740
        if (iwl4965_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
 
4741
                fill_rx = 1;
 
4742
 
 
4743
        while (i != r) {
 
4744
                rxb = rxq->queue[i];
 
4745
 
 
4746
                /* If an RXB doesn't have a Rx queue slot associated with it,
 
4747
                 * then a bug has been introduced in the queue refilling
 
4748
                 * routines -- catch it here */
 
4749
                BUG_ON(rxb == NULL);
 
4750
 
 
4751
                rxq->queue[i] = NULL;
 
4752
 
 
4753
                pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
 
4754
                                            IWL_RX_BUF_SIZE,
 
4755
                                            PCI_DMA_FROMDEVICE);
 
4756
                pkt = (struct iwl4965_rx_packet *)rxb->skb->data;
 
4757
 
 
4758
                /* Reclaim a command buffer only if this packet is a response
 
4759
                 *   to a (driver-originated) command.
 
4760
                 * If the packet (e.g. Rx frame) originated from uCode,
 
4761
                 *   there is no command buffer to reclaim.
 
4762
                 * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
 
4763
                 *   but apparently a few don't get set; catch them here. */
 
4764
                reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
 
4765
                        (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
 
4766
                        (pkt->hdr.cmd != REPLY_4965_RX) &&
 
4767
                        (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
 
4768
                        (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
 
4769
                        (pkt->hdr.cmd != REPLY_TX);
 
4770
 
 
4771
                /* Based on type of command response or notification,
 
4772
                 *   handle those that need handling via function in
 
4773
                 *   rx_handlers table.  See iwl4965_setup_rx_handlers() */
 
4774
                if (priv->rx_handlers[pkt->hdr.cmd]) {
 
4775
                        IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
 
4776
                                "r = %d, i = %d, %s, 0x%02x\n", r, i,
 
4777
                                get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
 
4778
                        priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
 
4779
                } else {
 
4780
                        /* No handling needed */
 
4781
                        IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
 
4782
                                "r %d i %d No handler needed for %s, 0x%02x\n",
 
4783
                                r, i, get_cmd_string(pkt->hdr.cmd),
 
4784
                                pkt->hdr.cmd);
 
4785
                }
 
4786
 
 
4787
                if (reclaim) {
 
4788
                        /* Invoke any callbacks, transfer the skb to caller, and
 
4789
                         * fire off the (possibly) blocking iwl4965_send_cmd()
 
4790
                         * as we reclaim the driver command queue */
 
4791
                        if (rxb && rxb->skb)
 
4792
                                iwl4965_tx_cmd_complete(priv, rxb);
 
4793
                        else
 
4794
                                IWL_WARNING("Claim null rxb?\n");
 
4795
                }
 
4796
 
 
4797
                /* For now we just don't re-use anything.  We can tweak this
 
4798
                 * later to try and re-use notification packets and SKBs that
 
4799
                 * fail to Rx correctly */
 
4800
                if (rxb->skb != NULL) {
 
4801
                        priv->alloc_rxb_skb--;
 
4802
                        dev_kfree_skb_any(rxb->skb);
 
4803
                        rxb->skb = NULL;
 
4804
                }
 
4805
 
 
4806
                pci_unmap_single(priv->pci_dev, rxb->dma_addr,
 
4807
                                 IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
 
4808
                spin_lock_irqsave(&rxq->lock, flags);
 
4809
                list_add_tail(&rxb->list, &priv->rxq.rx_used);
 
4810
                spin_unlock_irqrestore(&rxq->lock, flags);
 
4811
                i = (i + 1) & RX_QUEUE_MASK;
 
4812
                /* If there are a lot of unused frames,
 
4813
                 * restock the Rx queue so ucode wont assert. */
 
4814
                if (fill_rx) {
 
4815
                        count++;
 
4816
                        if (count >= 8) {
 
4817
                                priv->rxq.read = i;
 
4818
                                __iwl4965_rx_replenish(priv);
 
4819
                                count = 0;
 
4820
                        }
 
4821
                }
 
4822
        }
 
4823
 
 
4824
        /* Backtrack one entry */
 
4825
        priv->rxq.read = i;
 
4826
        iwl4965_rx_queue_restock(priv);
 
4827
}
 
4828
 
 
4829
/**
 
4830
 * iwl4965_tx_queue_update_write_ptr - Send new write index to hardware
 
4831
 */
 
4832
static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
 
4833
                                  struct iwl4965_tx_queue *txq)
 
4834
{
 
4835
        u32 reg = 0;
 
4836
        int rc = 0;
 
4837
        int txq_id = txq->q.id;
 
4838
 
 
4839
        if (txq->need_update == 0)
 
4840
                return rc;
 
4841
 
 
4842
        /* if we're trying to save power */
 
4843
        if (test_bit(STATUS_POWER_PMI, &priv->status)) {
 
4844
                /* wake up nic if it's powered down ...
 
4845
                 * uCode will wake up, and interrupt us again, so next
 
4846
                 * time we'll skip this part. */
 
4847
                reg = iwl4965_read32(priv, CSR_UCODE_DRV_GP1);
 
4848
 
 
4849
                if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
 
4850
                        IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
 
4851
                        iwl4965_set_bit(priv, CSR_GP_CNTRL,
 
4852
                                    CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
 
4853
                        return rc;
 
4854
                }
 
4855
 
 
4856
                /* restore this queue's parameters in nic hardware. */
 
4857
                rc = iwl4965_grab_nic_access(priv);
 
4858
                if (rc)
 
4859
                        return rc;
 
4860
                iwl4965_write_direct32(priv, HBUS_TARG_WRPTR,
 
4861
                                     txq->q.write_ptr | (txq_id << 8));
 
4862
                iwl4965_release_nic_access(priv);
 
4863
 
 
4864
        /* else not in power-save mode, uCode will never sleep when we're
 
4865
         * trying to tx (during RFKILL, we're not trying to tx). */
 
4866
        } else
 
4867
                iwl4965_write32(priv, HBUS_TARG_WRPTR,
 
4868
                            txq->q.write_ptr | (txq_id << 8));
 
4869
 
 
4870
        txq->need_update = 0;
 
4871
 
 
4872
        return rc;
 
4873
}
 
4874
 
 
4875
#ifdef CONFIG_IWL4965_DEBUG
 
4876
static void iwl4965_print_rx_config_cmd(struct iwl4965_rxon_cmd *rxon)
 
4877
{
 
4878
        IWL_DEBUG_RADIO("RX CONFIG:\n");
 
4879
        iwl4965_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
 
4880
        IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
 
4881
        IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
 
4882
        IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
 
4883
                        le32_to_cpu(rxon->filter_flags));
 
4884
        IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
 
4885
        IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
 
4886
                        rxon->ofdm_basic_rates);
 
4887
        IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
 
4888
        IWL_DEBUG_RADIO("u8[6] node_addr: " MAC_FMT "\n",
 
4889
                        MAC_ARG(rxon->node_addr));
 
4890
        IWL_DEBUG_RADIO("u8[6] bssid_addr: " MAC_FMT "\n",
 
4891
                        MAC_ARG(rxon->bssid_addr));
 
4892
        IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
 
4893
}
 
4894
#endif
 
4895
 
 
4896
static void iwl4965_enable_interrupts(struct iwl4965_priv *priv)
 
4897
{
 
4898
        IWL_DEBUG_ISR("Enabling interrupts\n");
 
4899
        set_bit(STATUS_INT_ENABLED, &priv->status);
 
4900
        iwl4965_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
 
4901
}
 
4902
 
 
4903
static inline void iwl4965_disable_interrupts(struct iwl4965_priv *priv)
 
4904
{
 
4905
        clear_bit(STATUS_INT_ENABLED, &priv->status);
 
4906
 
 
4907
        /* disable interrupts from uCode/NIC to host */
 
4908
        iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
 
4909
 
 
4910
        /* acknowledge/clear/reset any interrupts still pending
 
4911
         * from uCode or flow handler (Rx/Tx DMA) */
 
4912
        iwl4965_write32(priv, CSR_INT, 0xffffffff);
 
4913
        iwl4965_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
 
4914
        IWL_DEBUG_ISR("Disabled interrupts\n");
 
4915
}
 
4916
 
 
4917
static const char *desc_lookup(int i)
 
4918
{
 
4919
        switch (i) {
 
4920
        case 1:
 
4921
                return "FAIL";
 
4922
        case 2:
 
4923
                return "BAD_PARAM";
 
4924
        case 3:
 
4925
                return "BAD_CHECKSUM";
 
4926
        case 4:
 
4927
                return "NMI_INTERRUPT";
 
4928
        case 5:
 
4929
                return "SYSASSERT";
 
4930
        case 6:
 
4931
                return "FATAL_ERROR";
 
4932
        }
 
4933
 
 
4934
        return "UNKNOWN";
 
4935
}
 
4936
 
 
4937
#define ERROR_START_OFFSET  (1 * sizeof(u32))
 
4938
#define ERROR_ELEM_SIZE     (7 * sizeof(u32))
 
4939
 
 
4940
static void iwl4965_dump_nic_error_log(struct iwl4965_priv *priv)
 
4941
{
 
4942
        u32 data2, line;
 
4943
        u32 desc, time, count, base, data1;
 
4944
        u32 blink1, blink2, ilink1, ilink2;
 
4945
        int rc;
 
4946
 
 
4947
        base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
 
4948
 
 
4949
        if (!iwl4965_hw_valid_rtc_data_addr(base)) {
 
4950
                IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
 
4951
                return;
 
4952
        }
 
4953
 
 
4954
        rc = iwl4965_grab_nic_access(priv);
 
4955
        if (rc) {
 
4956
                IWL_WARNING("Can not read from adapter at this time.\n");
 
4957
                return;
 
4958
        }
 
4959
 
 
4960
        count = iwl4965_read_targ_mem(priv, base);
 
4961
 
 
4962
        if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
 
4963
                IWL_ERROR("Start IWL Error Log Dump:\n");
 
4964
                IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
 
4965
                          priv->status, priv->config, count);
 
4966
        }
 
4967
 
 
4968
        desc = iwl4965_read_targ_mem(priv, base + 1 * sizeof(u32));
 
4969
        blink1 = iwl4965_read_targ_mem(priv, base + 3 * sizeof(u32));
 
4970
        blink2 = iwl4965_read_targ_mem(priv, base + 4 * sizeof(u32));
 
4971
        ilink1 = iwl4965_read_targ_mem(priv, base + 5 * sizeof(u32));
 
4972
        ilink2 = iwl4965_read_targ_mem(priv, base + 6 * sizeof(u32));
 
4973
        data1 = iwl4965_read_targ_mem(priv, base + 7 * sizeof(u32));
 
4974
        data2 = iwl4965_read_targ_mem(priv, base + 8 * sizeof(u32));
 
4975
        line = iwl4965_read_targ_mem(priv, base + 9 * sizeof(u32));
 
4976
        time = iwl4965_read_targ_mem(priv, base + 11 * sizeof(u32));
 
4977
 
 
4978
        IWL_ERROR("Desc               Time       "
 
4979
                  "data1      data2      line\n");
 
4980
        IWL_ERROR("%-13s (#%d) %010u 0x%08X 0x%08X %u\n",
 
4981
                  desc_lookup(desc), desc, time, data1, data2, line);
 
4982
        IWL_ERROR("blink1  blink2  ilink1  ilink2\n");
 
4983
        IWL_ERROR("0x%05X 0x%05X 0x%05X 0x%05X\n", blink1, blink2,
 
4984
                  ilink1, ilink2);
 
4985
 
 
4986
        iwl4965_release_nic_access(priv);
 
4987
}
 
4988
 
 
4989
#define EVENT_START_OFFSET  (4 * sizeof(u32))
 
4990
 
 
4991
/**
 
4992
 * iwl4965_print_event_log - Dump error event log to syslog
 
4993
 *
 
4994
 * NOTE: Must be called with iwl4965_grab_nic_access() already obtained!
 
4995
 */
 
4996
static void iwl4965_print_event_log(struct iwl4965_priv *priv, u32 start_idx,
 
4997
                                u32 num_events, u32 mode)
 
4998
{
 
4999
        u32 i;
 
5000
        u32 base;       /* SRAM byte address of event log header */
 
5001
        u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
 
5002
        u32 ptr;        /* SRAM byte address of log data */
 
5003
        u32 ev, time, data; /* event log data */
 
5004
 
 
5005
        if (num_events == 0)
 
5006
                return;
 
5007
 
 
5008
        base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
 
5009
 
 
5010
        if (mode == 0)
 
5011
                event_size = 2 * sizeof(u32);
 
5012
        else
 
5013
                event_size = 3 * sizeof(u32);
 
5014
 
 
5015
        ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
 
5016
 
 
5017
        /* "time" is actually "data" for mode 0 (no timestamp).
 
5018
         * place event id # at far right for easier visual parsing. */
 
5019
        for (i = 0; i < num_events; i++) {
 
5020
                ev = iwl4965_read_targ_mem(priv, ptr);
 
5021
                ptr += sizeof(u32);
 
5022
                time = iwl4965_read_targ_mem(priv, ptr);
 
5023
                ptr += sizeof(u32);
 
5024
                if (mode == 0)
 
5025
                        IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
 
5026
                else {
 
5027
                        data = iwl4965_read_targ_mem(priv, ptr);
 
5028
                        ptr += sizeof(u32);
 
5029
                        IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
 
5030
                }
 
5031
        }
 
5032
}
 
5033
 
 
5034
static void iwl4965_dump_nic_event_log(struct iwl4965_priv *priv)
 
5035
{
 
5036
        int rc;
 
5037
        u32 base;       /* SRAM byte address of event log header */
 
5038
        u32 capacity;   /* event log capacity in # entries */
 
5039
        u32 mode;       /* 0 - no timestamp, 1 - timestamp recorded */
 
5040
        u32 num_wraps;  /* # times uCode wrapped to top of log */
 
5041
        u32 next_entry; /* index of next entry to be written by uCode */
 
5042
        u32 size;       /* # entries that we'll print */
 
5043
 
 
5044
        base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
 
5045
        if (!iwl4965_hw_valid_rtc_data_addr(base)) {
 
5046
                IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
 
5047
                return;
 
5048
        }
 
5049
 
 
5050
        rc = iwl4965_grab_nic_access(priv);
 
5051
        if (rc) {
 
5052
                IWL_WARNING("Can not read from adapter at this time.\n");
 
5053
                return;
 
5054
        }
 
5055
 
 
5056
        /* event log header */
 
5057
        capacity = iwl4965_read_targ_mem(priv, base);
 
5058
        mode = iwl4965_read_targ_mem(priv, base + (1 * sizeof(u32)));
 
5059
        num_wraps = iwl4965_read_targ_mem(priv, base + (2 * sizeof(u32)));
 
5060
        next_entry = iwl4965_read_targ_mem(priv, base + (3 * sizeof(u32)));
 
5061
 
 
5062
        size = num_wraps ? capacity : next_entry;
 
5063
 
 
5064
        /* bail out if nothing in log */
 
5065
        if (size == 0) {
 
5066
                IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
 
5067
                iwl4965_release_nic_access(priv);
 
5068
                return;
 
5069
        }
 
5070
 
 
5071
        IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
 
5072
                  size, num_wraps);
 
5073
 
 
5074
        /* if uCode has wrapped back to top of log, start at the oldest entry,
 
5075
         * i.e the next one that uCode would fill. */
 
5076
        if (num_wraps)
 
5077
                iwl4965_print_event_log(priv, next_entry,
 
5078
                                    capacity - next_entry, mode);
 
5079
 
 
5080
        /* (then/else) start at top of log */
 
5081
        iwl4965_print_event_log(priv, 0, next_entry, mode);
 
5082
 
 
5083
        iwl4965_release_nic_access(priv);
 
5084
}
 
5085
 
 
5086
/**
 
5087
 * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
 
5088
 */
 
5089
static void iwl4965_irq_handle_error(struct iwl4965_priv *priv)
 
5090
{
 
5091
        /* Set the FW error flag -- cleared on iwl4965_down */
 
5092
        set_bit(STATUS_FW_ERROR, &priv->status);
 
5093
 
 
5094
        /* Cancel currently queued command. */
 
5095
        clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
 
5096
 
 
5097
#ifdef CONFIG_IWL4965_DEBUG
 
5098
        if (iwl4965_debug_level & IWL_DL_FW_ERRORS) {
 
5099
                iwl4965_dump_nic_error_log(priv);
 
5100
                iwl4965_dump_nic_event_log(priv);
 
5101
                iwl4965_print_rx_config_cmd(&priv->staging_rxon);
 
5102
        }
 
5103
#endif
 
5104
 
 
5105
        wake_up_interruptible(&priv->wait_command_queue);
 
5106
 
 
5107
        /* Keep the restart process from trying to send host
 
5108
         * commands by clearing the INIT status bit */
 
5109
        clear_bit(STATUS_READY, &priv->status);
 
5110
 
 
5111
        if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
 
5112
                IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
 
5113
                          "Restarting adapter due to uCode error.\n");
 
5114
 
 
5115
                if (iwl4965_is_associated(priv)) {
 
5116
                        memcpy(&priv->recovery_rxon, &priv->active_rxon,
 
5117
                               sizeof(priv->recovery_rxon));
 
5118
                        priv->error_recovering = 1;
 
5119
                }
 
5120
                queue_work(priv->workqueue, &priv->restart);
 
5121
        }
 
5122
}
 
5123
 
 
5124
static void iwl4965_error_recovery(struct iwl4965_priv *priv)
 
5125
{
 
5126
        unsigned long flags;
 
5127
 
 
5128
        memcpy(&priv->staging_rxon, &priv->recovery_rxon,
 
5129
               sizeof(priv->staging_rxon));
 
5130
        priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 
5131
        iwl4965_commit_rxon(priv);
 
5132
 
 
5133
        iwl4965_rxon_add_station(priv, priv->bssid, 1);
 
5134
 
 
5135
        spin_lock_irqsave(&priv->lock, flags);
 
5136
        priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
 
5137
        priv->error_recovering = 0;
 
5138
        spin_unlock_irqrestore(&priv->lock, flags);
 
5139
}
 
5140
 
 
5141
static void iwl4965_irq_tasklet(struct iwl4965_priv *priv)
 
5142
{
 
5143
        u32 inta, handled = 0;
 
5144
        u32 inta_fh;
 
5145
        unsigned long flags;
 
5146
#ifdef CONFIG_IWL4965_DEBUG
 
5147
        u32 inta_mask;
 
5148
#endif
 
5149
 
 
5150
        spin_lock_irqsave(&priv->lock, flags);
 
5151
 
 
5152
        /* Ack/clear/reset pending uCode interrupts.
 
5153
         * Note:  Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
 
5154
         *  and will clear only when CSR_FH_INT_STATUS gets cleared. */
 
5155
        inta = iwl4965_read32(priv, CSR_INT);
 
5156
        iwl4965_write32(priv, CSR_INT, inta);
 
5157
 
 
5158
        /* Ack/clear/reset pending flow-handler (DMA) interrupts.
 
5159
         * Any new interrupts that happen after this, either while we're
 
5160
         * in this tasklet, or later, will show up in next ISR/tasklet. */
 
5161
        inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
 
5162
        iwl4965_write32(priv, CSR_FH_INT_STATUS, inta_fh);
 
5163
 
 
5164
#ifdef CONFIG_IWL4965_DEBUG
 
5165
        if (iwl4965_debug_level & IWL_DL_ISR) {
 
5166
                /* just for debug */
 
5167
                inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
 
5168
                IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
 
5169
                              inta, inta_mask, inta_fh);
 
5170
        }
 
5171
#endif
 
5172
 
 
5173
        /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
 
5174
         * atomic, make sure that inta covers all the interrupts that
 
5175
         * we've discovered, even if FH interrupt came in just after
 
5176
         * reading CSR_INT. */
 
5177
        if (inta_fh & CSR_FH_INT_RX_MASK)
 
5178
                inta |= CSR_INT_BIT_FH_RX;
 
5179
        if (inta_fh & CSR_FH_INT_TX_MASK)
 
5180
                inta |= CSR_INT_BIT_FH_TX;
 
5181
 
 
5182
        /* Now service all interrupt bits discovered above. */
 
5183
        if (inta & CSR_INT_BIT_HW_ERR) {
 
5184
                IWL_ERROR("Microcode HW error detected.  Restarting.\n");
 
5185
 
 
5186
                /* Tell the device to stop sending interrupts */
 
5187
                iwl4965_disable_interrupts(priv);
 
5188
 
 
5189
                iwl4965_irq_handle_error(priv);
 
5190
 
 
5191
                handled |= CSR_INT_BIT_HW_ERR;
 
5192
 
 
5193
                spin_unlock_irqrestore(&priv->lock, flags);
 
5194
 
 
5195
                return;
 
5196
        }
 
5197
 
 
5198
#ifdef CONFIG_IWL4965_DEBUG
 
5199
        if (iwl4965_debug_level & (IWL_DL_ISR)) {
 
5200
                /* NIC fires this, but we don't use it, redundant with WAKEUP */
 
5201
                if (inta & CSR_INT_BIT_MAC_CLK_ACTV)
 
5202
                        IWL_DEBUG_ISR("Microcode started or stopped.\n");
 
5203
 
 
5204
                /* Alive notification via Rx interrupt will do the real work */
 
5205
                if (inta & CSR_INT_BIT_ALIVE)
 
5206
                        IWL_DEBUG_ISR("Alive interrupt\n");
 
5207
        }
 
5208
#endif
 
5209
        /* Safely ignore these bits for debug checks below */
 
5210
        inta &= ~(CSR_INT_BIT_MAC_CLK_ACTV | CSR_INT_BIT_ALIVE);
 
5211
 
 
5212
        /* HW RF KILL switch toggled */
 
5213
        if (inta & CSR_INT_BIT_RF_KILL) {
 
5214
                int hw_rf_kill = 0;
 
5215
                if (!(iwl4965_read32(priv, CSR_GP_CNTRL) &
 
5216
                                CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
 
5217
                        hw_rf_kill = 1;
 
5218
 
 
5219
                IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
 
5220
                                "RF_KILL bit toggled to %s.\n",
 
5221
                                hw_rf_kill ? "disable radio":"enable radio");
 
5222
 
 
5223
                /* Queue restart only if RF_KILL switch was set to "kill"
 
5224
                 *   when we loaded driver, and is now set to "enable".
 
5225
                 * After we're Alive, RF_KILL gets handled by
 
5226
                 *   iwl4965_rx_card_state_notif() */
 
5227
                if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
 
5228
                        clear_bit(STATUS_RF_KILL_HW, &priv->status);
 
5229
                        queue_work(priv->workqueue, &priv->restart);
 
5230
                }
 
5231
 
 
5232
                handled |= CSR_INT_BIT_RF_KILL;
 
5233
        }
 
5234
 
 
5235
        /* Chip got too hot and stopped itself */
 
5236
        if (inta & CSR_INT_BIT_CT_KILL) {
 
5237
                IWL_ERROR("Microcode CT kill error detected.\n");
 
5238
                handled |= CSR_INT_BIT_CT_KILL;
 
5239
        }
 
5240
 
 
5241
        /* Error detected by uCode */
 
5242
        if (inta & CSR_INT_BIT_SW_ERR) {
 
5243
                IWL_ERROR("Microcode SW error detected.  Restarting 0x%X.\n",
 
5244
                          inta);
 
5245
                iwl4965_irq_handle_error(priv);
 
5246
                handled |= CSR_INT_BIT_SW_ERR;
 
5247
        }
 
5248
 
 
5249
        /* uCode wakes up after power-down sleep */
 
5250
        if (inta & CSR_INT_BIT_WAKEUP) {
 
5251
                IWL_DEBUG_ISR("Wakeup interrupt\n");
 
5252
                iwl4965_rx_queue_update_write_ptr(priv, &priv->rxq);
 
5253
                iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[0]);
 
5254
                iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[1]);
 
5255
                iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[2]);
 
5256
                iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[3]);
 
5257
                iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[4]);
 
5258
                iwl4965_tx_queue_update_write_ptr(priv, &priv->txq[5]);
 
5259
 
 
5260
                handled |= CSR_INT_BIT_WAKEUP;
 
5261
        }
 
5262
 
 
5263
        /* All uCode command responses, including Tx command responses,
 
5264
         * Rx "responses" (frame-received notification), and other
 
5265
         * notifications from uCode come through here*/
 
5266
        if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
 
5267
                iwl4965_rx_handle(priv);
 
5268
                handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
 
5269
        }
 
5270
 
 
5271
        if (inta & CSR_INT_BIT_FH_TX) {
 
5272
                IWL_DEBUG_ISR("Tx interrupt\n");
 
5273
                handled |= CSR_INT_BIT_FH_TX;
 
5274
        }
 
5275
 
 
5276
        if (inta & ~handled)
 
5277
                IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
 
5278
 
 
5279
        if (inta & ~CSR_INI_SET_MASK) {
 
5280
                IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
 
5281
                         inta & ~CSR_INI_SET_MASK);
 
5282
                IWL_WARNING("   with FH_INT = 0x%08x\n", inta_fh);
 
5283
        }
 
5284
 
 
5285
        /* Re-enable all interrupts */
 
5286
        iwl4965_enable_interrupts(priv);
 
5287
 
 
5288
#ifdef CONFIG_IWL4965_DEBUG
 
5289
        if (iwl4965_debug_level & (IWL_DL_ISR)) {
 
5290
                inta = iwl4965_read32(priv, CSR_INT);
 
5291
                inta_mask = iwl4965_read32(priv, CSR_INT_MASK);
 
5292
                inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
 
5293
                IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
 
5294
                        "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
 
5295
        }
 
5296
#endif
 
5297
        spin_unlock_irqrestore(&priv->lock, flags);
 
5298
}
 
5299
 
 
5300
static irqreturn_t iwl4965_isr(int irq, void *data)
 
5301
{
 
5302
        struct iwl4965_priv *priv = data;
 
5303
        u32 inta, inta_mask;
 
5304
        u32 inta_fh;
 
5305
        if (!priv)
 
5306
                return IRQ_NONE;
 
5307
 
 
5308
        spin_lock(&priv->lock);
 
5309
 
 
5310
        /* Disable (but don't clear!) interrupts here to avoid
 
5311
         *    back-to-back ISRs and sporadic interrupts from our NIC.
 
5312
         * If we have something to service, the tasklet will re-enable ints.
 
5313
         * If we *don't* have something, we'll re-enable before leaving here. */
 
5314
        inta_mask = iwl4965_read32(priv, CSR_INT_MASK);  /* just for debug */
 
5315
        iwl4965_write32(priv, CSR_INT_MASK, 0x00000000);
 
5316
 
 
5317
        /* Discover which interrupts are active/pending */
 
5318
        inta = iwl4965_read32(priv, CSR_INT);
 
5319
        inta_fh = iwl4965_read32(priv, CSR_FH_INT_STATUS);
 
5320
 
 
5321
        /* Ignore interrupt if there's nothing in NIC to service.
 
5322
         * This may be due to IRQ shared with another device,
 
5323
         * or due to sporadic interrupts thrown from our NIC. */
 
5324
        if (!inta && !inta_fh) {
 
5325
                IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
 
5326
                goto none;
 
5327
        }
 
5328
 
 
5329
        if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
 
5330
                /* Hardware disappeared. It might have already raised
 
5331
                 * an interrupt */
 
5332
                IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
 
5333
                goto unplugged;
 
5334
        }
 
5335
 
 
5336
        IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
 
5337
                      inta, inta_mask, inta_fh);
 
5338
 
 
5339
        /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
 
5340
        tasklet_schedule(&priv->irq_tasklet);
 
5341
 
 
5342
 unplugged:
 
5343
        spin_unlock(&priv->lock);
 
5344
        return IRQ_HANDLED;
 
5345
 
 
5346
 none:
 
5347
        /* re-enable interrupts here since we don't have anything to service. */
 
5348
        iwl4965_enable_interrupts(priv);
 
5349
        spin_unlock(&priv->lock);
 
5350
        return IRQ_NONE;
 
5351
}
 
5352
 
 
5353
/************************** EEPROM BANDS ****************************
 
5354
 *
 
5355
 * The iwl4965_eeprom_band definitions below provide the mapping from the
 
5356
 * EEPROM contents to the specific channel number supported for each
 
5357
 * band.
 
5358
 *
 
5359
 * For example, iwl4965_priv->eeprom.band_3_channels[4] from the band_3
 
5360
 * definition below maps to physical channel 42 in the 5.2GHz spectrum.
 
5361
 * The specific geography and calibration information for that channel
 
5362
 * is contained in the eeprom map itself.
 
5363
 *
 
5364
 * During init, we copy the eeprom information and channel map
 
5365
 * information into priv->channel_info_24/52 and priv->channel_map_24/52
 
5366
 *
 
5367
 * channel_map_24/52 provides the index in the channel_info array for a
 
5368
 * given channel.  We have to have two separate maps as there is channel
 
5369
 * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
 
5370
 * band_2
 
5371
 *
 
5372
 * A value of 0xff stored in the channel_map indicates that the channel
 
5373
 * is not supported by the hardware at all.
 
5374
 *
 
5375
 * A value of 0xfe in the channel_map indicates that the channel is not
 
5376
 * valid for Tx with the current hardware.  This means that
 
5377
 * while the system can tune and receive on a given channel, it may not
 
5378
 * be able to associate or transmit any frames on that
 
5379
 * channel.  There is no corresponding channel information for that
 
5380
 * entry.
 
5381
 *
 
5382
 *********************************************************************/
 
5383
 
 
5384
/* 2.4 GHz */
 
5385
static const u8 iwl4965_eeprom_band_1[14] = {
 
5386
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
 
5387
};
 
5388
 
 
5389
/* 5.2 GHz bands */
 
5390
static const u8 iwl4965_eeprom_band_2[] = {     /* 4915-5080MHz */
 
5391
        183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
 
5392
};
 
5393
 
 
5394
static const u8 iwl4965_eeprom_band_3[] = {     /* 5170-5320MHz */
 
5395
        34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
 
5396
};
 
5397
 
 
5398
static const u8 iwl4965_eeprom_band_4[] = {     /* 5500-5700MHz */
 
5399
        100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
 
5400
};
 
5401
 
 
5402
static const u8 iwl4965_eeprom_band_5[] = {     /* 5725-5825MHz */
 
5403
        145, 149, 153, 157, 161, 165
 
5404
};
 
5405
 
 
5406
static u8 iwl4965_eeprom_band_6[] = {       /* 2.4 FAT channel */
 
5407
        1, 2, 3, 4, 5, 6, 7
 
5408
};
 
5409
 
 
5410
static u8 iwl4965_eeprom_band_7[] = {       /* 5.2 FAT channel */
 
5411
        36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157
 
5412
};
 
5413
 
 
5414
static void iwl4965_init_band_reference(const struct iwl4965_priv *priv,
 
5415
                                    int band,
 
5416
                                    int *eeprom_ch_count,
 
5417
                                    const struct iwl4965_eeprom_channel
 
5418
                                    **eeprom_ch_info,
 
5419
                                    const u8 **eeprom_ch_index)
 
5420
{
 
5421
        switch (band) {
 
5422
        case 1:         /* 2.4GHz band */
 
5423
                *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_1);
 
5424
                *eeprom_ch_info = priv->eeprom.band_1_channels;
 
5425
                *eeprom_ch_index = iwl4965_eeprom_band_1;
 
5426
                break;
 
5427
        case 2:         /* 4.9GHz band */
 
5428
                *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_2);
 
5429
                *eeprom_ch_info = priv->eeprom.band_2_channels;
 
5430
                *eeprom_ch_index = iwl4965_eeprom_band_2;
 
5431
                break;
 
5432
        case 3:         /* 5.2GHz band */
 
5433
                *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_3);
 
5434
                *eeprom_ch_info = priv->eeprom.band_3_channels;
 
5435
                *eeprom_ch_index = iwl4965_eeprom_band_3;
 
5436
                break;
 
5437
        case 4:         /* 5.5GHz band */
 
5438
                *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_4);
 
5439
                *eeprom_ch_info = priv->eeprom.band_4_channels;
 
5440
                *eeprom_ch_index = iwl4965_eeprom_band_4;
 
5441
                break;
 
5442
        case 5:         /* 5.7GHz band */
 
5443
                *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_5);
 
5444
                *eeprom_ch_info = priv->eeprom.band_5_channels;
 
5445
                *eeprom_ch_index = iwl4965_eeprom_band_5;
 
5446
                break;
 
5447
        case 6:         /* 2.4GHz FAT channels */
 
5448
                *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_6);
 
5449
                *eeprom_ch_info = priv->eeprom.band_24_channels;
 
5450
                *eeprom_ch_index = iwl4965_eeprom_band_6;
 
5451
                break;
 
5452
        case 7:         /* 5 GHz FAT channels */
 
5453
                *eeprom_ch_count = ARRAY_SIZE(iwl4965_eeprom_band_7);
 
5454
                *eeprom_ch_info = priv->eeprom.band_52_channels;
 
5455
                *eeprom_ch_index = iwl4965_eeprom_band_7;
 
5456
                break;
 
5457
        default:
 
5458
                BUG();
 
5459
                return;
 
5460
        }
 
5461
}
 
5462
 
 
5463
/**
 
5464
 * iwl4965_get_channel_info - Find driver's private channel info
 
5465
 *
 
5466
 * Based on band and channel number.
 
5467
 */
 
5468
const struct iwl4965_channel_info *iwl4965_get_channel_info(const struct iwl4965_priv *priv,
 
5469
                                                    int phymode, u16 channel)
 
5470
{
 
5471
        int i;
 
5472
 
 
5473
        switch (phymode) {
 
5474
        case MODE_IEEE80211A:
 
5475
                for (i = 14; i < priv->channel_count; i++) {
 
5476
                        if (priv->channel_info[i].channel == channel)
 
5477
                                return &priv->channel_info[i];
 
5478
                }
 
5479
                break;
 
5480
 
 
5481
        case MODE_IEEE80211B:
 
5482
        case MODE_IEEE80211G:
 
5483
                if (channel >= 1 && channel <= 14)
 
5484
                        return &priv->channel_info[channel - 1];
 
5485
                break;
 
5486
 
 
5487
        }
 
5488
 
 
5489
        return NULL;
 
5490
}
 
5491
 
 
5492
#define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
 
5493
                            ? # x " " : "")
 
5494
 
 
5495
/**
 
5496
 * iwl4965_init_channel_map - Set up driver's info for all possible channels
 
5497
 */
 
5498
static int iwl4965_init_channel_map(struct iwl4965_priv *priv)
 
5499
{
 
5500
        int eeprom_ch_count = 0;
 
5501
        const u8 *eeprom_ch_index = NULL;
 
5502
        const struct iwl4965_eeprom_channel *eeprom_ch_info = NULL;
 
5503
        int band, ch;
 
5504
        struct iwl4965_channel_info *ch_info;
 
5505
 
 
5506
        if (priv->channel_count) {
 
5507
                IWL_DEBUG_INFO("Channel map already initialized.\n");
 
5508
                return 0;
 
5509
        }
 
5510
 
 
5511
        if (priv->eeprom.version < 0x2f) {
 
5512
                IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
 
5513
                            priv->eeprom.version);
 
5514
                return -EINVAL;
 
5515
        }
 
5516
 
 
5517
        IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
 
5518
 
 
5519
        priv->channel_count =
 
5520
            ARRAY_SIZE(iwl4965_eeprom_band_1) +
 
5521
            ARRAY_SIZE(iwl4965_eeprom_band_2) +
 
5522
            ARRAY_SIZE(iwl4965_eeprom_band_3) +
 
5523
            ARRAY_SIZE(iwl4965_eeprom_band_4) +
 
5524
            ARRAY_SIZE(iwl4965_eeprom_band_5);
 
5525
 
 
5526
        IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
 
5527
 
 
5528
        priv->channel_info = kzalloc(sizeof(struct iwl4965_channel_info) *
 
5529
                                     priv->channel_count, GFP_KERNEL);
 
5530
        if (!priv->channel_info) {
 
5531
                IWL_ERROR("Could not allocate channel_info\n");
 
5532
                priv->channel_count = 0;
 
5533
                return -ENOMEM;
 
5534
        }
 
5535
 
 
5536
        ch_info = priv->channel_info;
 
5537
 
 
5538
        /* Loop through the 5 EEPROM bands adding them in order to the
 
5539
         * channel map we maintain (that contains additional information than
 
5540
         * what just in the EEPROM) */
 
5541
        for (band = 1; band <= 5; band++) {
 
5542
 
 
5543
                iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
 
5544
                                        &eeprom_ch_info, &eeprom_ch_index);
 
5545
 
 
5546
                /* Loop through each band adding each of the channels */
 
5547
                for (ch = 0; ch < eeprom_ch_count; ch++) {
 
5548
                        ch_info->channel = eeprom_ch_index[ch];
 
5549
                        ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
 
5550
                            MODE_IEEE80211A;
 
5551
 
 
5552
                        /* permanently store EEPROM's channel regulatory flags
 
5553
                         *   and max power in channel info database. */
 
5554
                        ch_info->eeprom = eeprom_ch_info[ch];
 
5555
 
 
5556
                        /* Copy the run-time flags so they are there even on
 
5557
                         * invalid channels */
 
5558
                        ch_info->flags = eeprom_ch_info[ch].flags;
 
5559
 
 
5560
                        if (!(is_channel_valid(ch_info))) {
 
5561
                                IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
 
5562
                                               "No traffic\n",
 
5563
                                               ch_info->channel,
 
5564
                                               ch_info->flags,
 
5565
                                               is_channel_a_band(ch_info) ?
 
5566
                                               "5.2" : "2.4");
 
5567
                                ch_info++;
 
5568
                                continue;
 
5569
                        }
 
5570
 
 
5571
                        /* Initialize regulatory-based run-time data */
 
5572
                        ch_info->max_power_avg = ch_info->curr_txpow =
 
5573
                            eeprom_ch_info[ch].max_power_avg;
 
5574
                        ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
 
5575
                        ch_info->min_power = 0;
 
5576
 
 
5577
                        IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
 
5578
                                       " %ddBm): Ad-Hoc %ssupported\n",
 
5579
                                       ch_info->channel,
 
5580
                                       is_channel_a_band(ch_info) ?
 
5581
                                       "5.2" : "2.4",
 
5582
                                       CHECK_AND_PRINT(IBSS),
 
5583
                                       CHECK_AND_PRINT(ACTIVE),
 
5584
                                       CHECK_AND_PRINT(RADAR),
 
5585
                                       CHECK_AND_PRINT(WIDE),
 
5586
                                       CHECK_AND_PRINT(NARROW),
 
5587
                                       CHECK_AND_PRINT(DFS),
 
5588
                                       eeprom_ch_info[ch].flags,
 
5589
                                       eeprom_ch_info[ch].max_power_avg,
 
5590
                                       ((eeprom_ch_info[ch].
 
5591
                                         flags & EEPROM_CHANNEL_IBSS)
 
5592
                                        && !(eeprom_ch_info[ch].
 
5593
                                             flags & EEPROM_CHANNEL_RADAR))
 
5594
                                       ? "" : "not ");
 
5595
 
 
5596
                        /* Set the user_txpower_limit to the highest power
 
5597
                         * supported by any channel */
 
5598
                        if (eeprom_ch_info[ch].max_power_avg >
 
5599
                            priv->user_txpower_limit)
 
5600
                                priv->user_txpower_limit =
 
5601
                                    eeprom_ch_info[ch].max_power_avg;
 
5602
 
 
5603
                        ch_info++;
 
5604
                }
 
5605
        }
 
5606
 
 
5607
        /* Two additional EEPROM bands for 2.4 and 5 GHz FAT channels */
 
5608
        for (band = 6; band <= 7; band++) {
 
5609
                int phymode;
 
5610
                u8 fat_extension_chan;
 
5611
 
 
5612
                iwl4965_init_band_reference(priv, band, &eeprom_ch_count,
 
5613
                                        &eeprom_ch_info, &eeprom_ch_index);
 
5614
 
 
5615
                /* EEPROM band 6 is 2.4, band 7 is 5 GHz */
 
5616
                phymode = (band == 6) ? MODE_IEEE80211B : MODE_IEEE80211A;
 
5617
 
 
5618
                /* Loop through each band adding each of the channels */
 
5619
                for (ch = 0; ch < eeprom_ch_count; ch++) {
 
5620
 
 
5621
                        if ((band == 6) &&
 
5622
                            ((eeprom_ch_index[ch] == 5) ||
 
5623
                            (eeprom_ch_index[ch] == 6) ||
 
5624
                            (eeprom_ch_index[ch] == 7)))
 
5625
                               fat_extension_chan = HT_IE_EXT_CHANNEL_MAX;
 
5626
                        else
 
5627
                                fat_extension_chan = HT_IE_EXT_CHANNEL_ABOVE;
 
5628
 
 
5629
                        /* Set up driver's info for lower half */
 
5630
                        iwl4965_set_fat_chan_info(priv, phymode,
 
5631
                                                  eeprom_ch_index[ch],
 
5632
                                                  &(eeprom_ch_info[ch]),
 
5633
                                                  fat_extension_chan);
 
5634
 
 
5635
                        /* Set up driver's info for upper half */
 
5636
                        iwl4965_set_fat_chan_info(priv, phymode,
 
5637
                                                  (eeprom_ch_index[ch] + 4),
 
5638
                                                  &(eeprom_ch_info[ch]),
 
5639
                                                  HT_IE_EXT_CHANNEL_BELOW);
 
5640
                }
 
5641
        }
 
5642
 
 
5643
        return 0;
 
5644
}
 
5645
 
 
5646
/* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
 
5647
 * sending probe req.  This should be set long enough to hear probe responses
 
5648
 * from more than one AP.  */
 
5649
#define IWL_ACTIVE_DWELL_TIME_24    (20)        /* all times in msec */
 
5650
#define IWL_ACTIVE_DWELL_TIME_52    (10)
 
5651
 
 
5652
/* For faster active scanning, scan will move to the next channel if fewer than
 
5653
 * PLCP_QUIET_THRESH packets are heard on this channel within
 
5654
 * ACTIVE_QUIET_TIME after sending probe request.  This shortens the dwell
 
5655
 * time if it's a quiet channel (nothing responded to our probe, and there's
 
5656
 * no other traffic).
 
5657
 * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
 
5658
#define IWL_PLCP_QUIET_THRESH       __constant_cpu_to_le16(1)   /* packets */
 
5659
#define IWL_ACTIVE_QUIET_TIME       __constant_cpu_to_le16(5)   /* msec */
 
5660
 
 
5661
/* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
 
5662
 * Must be set longer than active dwell time.
 
5663
 * For the most reliable scan, set > AP beacon interval (typically 100msec). */
 
5664
#define IWL_PASSIVE_DWELL_TIME_24   (20)        /* all times in msec */
 
5665
#define IWL_PASSIVE_DWELL_TIME_52   (10)
 
5666
#define IWL_PASSIVE_DWELL_BASE      (100)
 
5667
#define IWL_CHANNEL_TUNE_TIME       5
 
5668
 
 
5669
static inline u16 iwl4965_get_active_dwell_time(struct iwl4965_priv *priv, int phymode)
 
5670
{
 
5671
        if (phymode == MODE_IEEE80211A)
 
5672
                return IWL_ACTIVE_DWELL_TIME_52;
 
5673
        else
 
5674
                return IWL_ACTIVE_DWELL_TIME_24;
 
5675
}
 
5676
 
 
5677
static u16 iwl4965_get_passive_dwell_time(struct iwl4965_priv *priv, int phymode)
 
5678
{
 
5679
        u16 active = iwl4965_get_active_dwell_time(priv, phymode);
 
5680
        u16 passive = (phymode != MODE_IEEE80211A) ?
 
5681
            IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
 
5682
            IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
 
5683
 
 
5684
        if (iwl4965_is_associated(priv)) {
 
5685
                /* If we're associated, we clamp the maximum passive
 
5686
                 * dwell time to be 98% of the beacon interval (minus
 
5687
                 * 2 * channel tune time) */
 
5688
                passive = priv->beacon_int;
 
5689
                if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
 
5690
                        passive = IWL_PASSIVE_DWELL_BASE;
 
5691
                passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
 
5692
        }
 
5693
 
 
5694
        if (passive <= active)
 
5695
                passive = active + 1;
 
5696
 
 
5697
        return passive;
 
5698
}
 
5699
 
 
5700
static int iwl4965_get_channels_for_scan(struct iwl4965_priv *priv, int phymode,
 
5701
                                     u8 is_active, u8 direct_mask,
 
5702
                                     struct iwl4965_scan_channel *scan_ch)
 
5703
{
 
5704
        const struct ieee80211_channel *channels = NULL;
 
5705
        const struct ieee80211_hw_mode *hw_mode;
 
5706
        const struct iwl4965_channel_info *ch_info;
 
5707
        u16 passive_dwell = 0;
 
5708
        u16 active_dwell = 0;
 
5709
        int added, i;
 
5710
 
 
5711
        hw_mode = iwl4965_get_hw_mode(priv, phymode);
 
5712
        if (!hw_mode)
 
5713
                return 0;
 
5714
 
 
5715
        channels = hw_mode->channels;
 
5716
 
 
5717
        active_dwell = iwl4965_get_active_dwell_time(priv, phymode);
 
5718
        passive_dwell = iwl4965_get_passive_dwell_time(priv, phymode);
 
5719
 
 
5720
        for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
 
5721
                if (channels[i].chan ==
 
5722
                    le16_to_cpu(priv->active_rxon.channel)) {
 
5723
                        if (iwl4965_is_associated(priv)) {
 
5724
                                IWL_DEBUG_SCAN
 
5725
                                    ("Skipping current channel %d\n",
 
5726
                                     le16_to_cpu(priv->active_rxon.channel));
 
5727
                                continue;
 
5728
                        }
 
5729
                } else if (priv->only_active_channel)
 
5730
                        continue;
 
5731
 
 
5732
                scan_ch->channel = channels[i].chan;
 
5733
 
 
5734
                ch_info = iwl4965_get_channel_info(priv, phymode,
 
5735
                                         scan_ch->channel);
 
5736
                if (!is_channel_valid(ch_info)) {
 
5737
                        IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
 
5738
                                       scan_ch->channel);
 
5739
                        continue;
 
5740
                }
 
5741
 
 
5742
                if (!is_active || is_channel_passive(ch_info) ||
 
5743
                    !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
 
5744
                        scan_ch->type = 0;      /* passive */
 
5745
                else
 
5746
                        scan_ch->type = 1;      /* active */
 
5747
 
 
5748
                if (scan_ch->type & 1)
 
5749
                        scan_ch->type |= (direct_mask << 1);
 
5750
 
 
5751
                if (is_channel_narrow(ch_info))
 
5752
                        scan_ch->type |= (1 << 7);
 
5753
 
 
5754
                scan_ch->active_dwell = cpu_to_le16(active_dwell);
 
5755
                scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
 
5756
 
 
5757
                /* Set txpower levels to defaults */
 
5758
                scan_ch->tpc.dsp_atten = 110;
 
5759
                /* scan_pwr_info->tpc.dsp_atten; */
 
5760
 
 
5761
                /*scan_pwr_info->tpc.tx_gain; */
 
5762
                if (phymode == MODE_IEEE80211A)
 
5763
                        scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
 
5764
                else {
 
5765
                        scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
 
5766
                        /* NOTE: if we were doing 6Mb OFDM for scans we'd use
 
5767
                         * power level:
 
5768
                         * scan_ch->tpc.tx_gain = ((1<<5) | (2 << 3)) | 3;
 
5769
                         */
 
5770
                }
 
5771
 
 
5772
                IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
 
5773
                               scan_ch->channel,
 
5774
                               (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
 
5775
                               (scan_ch->type & 1) ?
 
5776
                               active_dwell : passive_dwell);
 
5777
 
 
5778
                scan_ch++;
 
5779
                added++;
 
5780
        }
 
5781
 
 
5782
        IWL_DEBUG_SCAN("total channels to scan %d \n", added);
 
5783
        return added;
 
5784
}
 
5785
 
 
5786
static void iwl4965_reset_channel_flag(struct iwl4965_priv *priv)
 
5787
{
 
5788
        int i, j;
 
5789
        for (i = 0; i < 3; i++) {
 
5790
                struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
 
5791
                for (j = 0; j < hw_mode->num_channels; j++)
 
5792
                        hw_mode->channels[j].flag = hw_mode->channels[j].val;
 
5793
        }
 
5794
}
 
5795
 
 
5796
static void iwl4965_init_hw_rates(struct iwl4965_priv *priv,
 
5797
                              struct ieee80211_rate *rates)
 
5798
{
 
5799
        int i;
 
5800
 
 
5801
        for (i = 0; i < IWL_RATE_COUNT; i++) {
 
5802
                rates[i].rate = iwl4965_rates[i].ieee * 5;
 
5803
                rates[i].val = i; /* Rate scaling will work on indexes */
 
5804
                rates[i].val2 = i;
 
5805
                rates[i].flags = IEEE80211_RATE_SUPPORTED;
 
5806
                /* Only OFDM have the bits-per-symbol set */
 
5807
                if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
 
5808
                        rates[i].flags |= IEEE80211_RATE_OFDM;
 
5809
                else {
 
5810
                        /*
 
5811
                         * If CCK 1M then set rate flag to CCK else CCK_2
 
5812
                         * which is CCK | PREAMBLE2
 
5813
                         */
 
5814
                        rates[i].flags |= (iwl4965_rates[i].plcp == 10) ?
 
5815
                                IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
 
5816
                }
 
5817
 
 
5818
                /* Set up which ones are basic rates... */
 
5819
                if (IWL_BASIC_RATES_MASK & (1 << i))
 
5820
                        rates[i].flags |= IEEE80211_RATE_BASIC;
 
5821
        }
 
5822
}
 
5823
 
 
5824
/**
 
5825
 * iwl4965_init_geos - Initialize mac80211's geo/channel info based from eeprom
 
5826
 */
 
5827
static int iwl4965_init_geos(struct iwl4965_priv *priv)
 
5828
{
 
5829
        struct iwl4965_channel_info *ch;
 
5830
        struct ieee80211_hw_mode *modes;
 
5831
        struct ieee80211_channel *channels;
 
5832
        struct ieee80211_channel *geo_ch;
 
5833
        struct ieee80211_rate *rates;
 
5834
        int i = 0;
 
5835
        enum {
 
5836
                A = 0,
 
5837
                B = 1,
 
5838
                G = 2,
 
5839
                A_11N = 3,
 
5840
                G_11N = 4,
 
5841
        };
 
5842
        int mode_count = 5;
 
5843
 
 
5844
        if (priv->modes) {
 
5845
                IWL_DEBUG_INFO("Geography modes already initialized.\n");
 
5846
                set_bit(STATUS_GEO_CONFIGURED, &priv->status);
 
5847
                return 0;
 
5848
        }
 
5849
 
 
5850
        modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
 
5851
                        GFP_KERNEL);
 
5852
        if (!modes)
 
5853
                return -ENOMEM;
 
5854
 
 
5855
        channels = kzalloc(sizeof(struct ieee80211_channel) *
 
5856
                           priv->channel_count, GFP_KERNEL);
 
5857
        if (!channels) {
 
5858
                kfree(modes);
 
5859
                return -ENOMEM;
 
5860
        }
 
5861
 
 
5862
        rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
 
5863
                        GFP_KERNEL);
 
5864
        if (!rates) {
 
5865
                kfree(modes);
 
5866
                kfree(channels);
 
5867
                return -ENOMEM;
 
5868
        }
 
5869
 
 
5870
        /* 0 = 802.11a
 
5871
         * 1 = 802.11b
 
5872
         * 2 = 802.11g
 
5873
         */
 
5874
 
 
5875
        /* 5.2GHz channels start after the 2.4GHz channels */
 
5876
        modes[A].mode = MODE_IEEE80211A;
 
5877
        modes[A].channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
 
5878
        modes[A].rates = rates;
 
5879
        modes[A].num_rates = 8; /* just OFDM */
 
5880
        modes[A].rates = &rates[4];
 
5881
        modes[A].num_channels = 0;
 
5882
 
 
5883
        modes[B].mode = MODE_IEEE80211B;
 
5884
        modes[B].channels = channels;
 
5885
        modes[B].rates = rates;
 
5886
        modes[B].num_rates = 4; /* just CCK */
 
5887
        modes[B].num_channels = 0;
 
5888
 
 
5889
        modes[G].mode = MODE_IEEE80211G;
 
5890
        modes[G].channels = channels;
 
5891
        modes[G].rates = rates;
 
5892
        modes[G].num_rates = 12;        /* OFDM & CCK */
 
5893
        modes[G].num_channels = 0;
 
5894
 
 
5895
        modes[G_11N].mode = MODE_IEEE80211G;
 
5896
        modes[G_11N].channels = channels;
 
5897
        modes[G_11N].num_rates = 13;        /* OFDM & CCK */
 
5898
        modes[G_11N].rates = rates;
 
5899
        modes[G_11N].num_channels = 0;
 
5900
 
 
5901
        modes[A_11N].mode = MODE_IEEE80211A;
 
5902
        modes[A_11N].channels = &channels[ARRAY_SIZE(iwl4965_eeprom_band_1)];
 
5903
        modes[A_11N].rates = &rates[4];
 
5904
        modes[A_11N].num_rates = 9; /* just OFDM */
 
5905
        modes[A_11N].num_channels = 0;
 
5906
 
 
5907
        priv->ieee_channels = channels;
 
5908
        priv->ieee_rates = rates;
 
5909
 
 
5910
        iwl4965_init_hw_rates(priv, rates);
 
5911
 
 
5912
        for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
 
5913
                ch = &priv->channel_info[i];
 
5914
 
 
5915
                if (!is_channel_valid(ch)) {
 
5916
                        IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
 
5917
                                    "skipping.\n",
 
5918
                                    ch->channel, is_channel_a_band(ch) ?
 
5919
                                    "5.2" : "2.4");
 
5920
                        continue;
 
5921
                }
 
5922
 
 
5923
                if (is_channel_a_band(ch)) {
 
5924
                        geo_ch = &modes[A].channels[modes[A].num_channels++];
 
5925
                        modes[A_11N].num_channels++;
 
5926
                } else {
 
5927
                        geo_ch = &modes[B].channels[modes[B].num_channels++];
 
5928
                        modes[G].num_channels++;
 
5929
                        modes[G_11N].num_channels++;
 
5930
                }
 
5931
 
 
5932
                geo_ch->freq = ieee80211chan2mhz(ch->channel);
 
5933
                geo_ch->chan = ch->channel;
 
5934
                geo_ch->power_level = ch->max_power_avg;
 
5935
                geo_ch->antenna_max = 0xff;
 
5936
 
 
5937
                if (is_channel_valid(ch)) {
 
5938
                        geo_ch->flag = IEEE80211_CHAN_W_SCAN;
 
5939
                        if (ch->flags & EEPROM_CHANNEL_IBSS)
 
5940
                                geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
 
5941
 
 
5942
                        if (ch->flags & EEPROM_CHANNEL_ACTIVE)
 
5943
                                geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
 
5944
 
 
5945
                        if (ch->flags & EEPROM_CHANNEL_RADAR)
 
5946
                                geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
 
5947
 
 
5948
                        if (ch->max_power_avg > priv->max_channel_txpower_limit)
 
5949
                                priv->max_channel_txpower_limit =
 
5950
                                    ch->max_power_avg;
 
5951
                }
 
5952
 
 
5953
                geo_ch->val = geo_ch->flag;
 
5954
        }
 
5955
 
 
5956
        if ((modes[A].num_channels == 0) && priv->is_abg) {
 
5957
                printk(KERN_INFO DRV_NAME
 
5958
                       ": Incorrectly detected BG card as ABG.  Please send "
 
5959
                       "your PCI ID 0x%04X:0x%04X to maintainer.\n",
 
5960
                       priv->pci_dev->device, priv->pci_dev->subsystem_device);
 
5961
                priv->is_abg = 0;
 
5962
        }
 
5963
 
 
5964
        printk(KERN_INFO DRV_NAME
 
5965
               ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
 
5966
               modes[G].num_channels, modes[A].num_channels);
 
5967
 
 
5968
        /*
 
5969
         * NOTE:  We register these in preference of order -- the
 
5970
         * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
 
5971
         * a phymode based on rates or AP capabilities but seems to
 
5972
         * configure it purely on if the channel being configured
 
5973
         * is supported by a mode -- and the first match is taken
 
5974
         */
 
5975
 
 
5976
        if (modes[G].num_channels)
 
5977
                iwlwifi_iwlwifi_ieee80211_register_hwmode(priv->hw, &modes[G]);
 
5978
        if (modes[B].num_channels)
 
5979
                iwlwifi_iwlwifi_ieee80211_register_hwmode(priv->hw, &modes[B]);
 
5980
        if (modes[A].num_channels)
 
5981
                iwlwifi_iwlwifi_ieee80211_register_hwmode(priv->hw, &modes[A]);
 
5982
 
 
5983
        priv->modes = modes;
 
5984
        set_bit(STATUS_GEO_CONFIGURED, &priv->status);
 
5985
 
 
5986
        return 0;
 
5987
}
 
5988
 
 
5989
/******************************************************************************
 
5990
 *
 
5991
 * uCode download functions
 
5992
 *
 
5993
 ******************************************************************************/
 
5994
 
 
5995
static void iwl4965_dealloc_ucode_pci(struct iwl4965_priv *priv)
 
5996
{
 
5997
        iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
 
5998
        iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
 
5999
        iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
 
6000
        iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
 
6001
        iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
 
6002
        iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
 
6003
}
 
6004
 
 
6005
/**
 
6006
 * iwl4965_verify_inst_full - verify runtime uCode image in card vs. host,
 
6007
 *     looking at all data.
 
6008
 */
 
6009
static int iwl4965_verify_inst_full(struct iwl4965_priv *priv, __le32 * image,
 
6010
                                 u32 len)
 
6011
{
 
6012
        u32 val;
 
6013
        u32 save_len = len;
 
6014
        int rc = 0;
 
6015
        u32 errcnt;
 
6016
 
 
6017
        IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
 
6018
 
 
6019
        rc = iwl4965_grab_nic_access(priv);
 
6020
        if (rc)
 
6021
                return rc;
 
6022
 
 
6023
        iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
 
6024
 
 
6025
        errcnt = 0;
 
6026
        for (; len > 0; len -= sizeof(u32), image++) {
 
6027
                /* read data comes through single port, auto-incr addr */
 
6028
                /* NOTE: Use the debugless read so we don't flood kernel log
 
6029
                 * if IWL_DL_IO is set */
 
6030
                val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
 
6031
                if (val != le32_to_cpu(*image)) {
 
6032
                        IWL_ERROR("uCode INST section is invalid at "
 
6033
                                  "offset 0x%x, is 0x%x, s/b 0x%x\n",
 
6034
                                  save_len - len, val, le32_to_cpu(*image));
 
6035
                        rc = -EIO;
 
6036
                        errcnt++;
 
6037
                        if (errcnt >= 20)
 
6038
                                break;
 
6039
                }
 
6040
        }
 
6041
 
 
6042
        iwl4965_release_nic_access(priv);
 
6043
 
 
6044
        if (!errcnt)
 
6045
                IWL_DEBUG_INFO
 
6046
                    ("ucode image in INSTRUCTION memory is good\n");
 
6047
 
 
6048
        return rc;
 
6049
}
 
6050
 
 
6051
 
 
6052
/**
 
6053
 * iwl4965_verify_inst_sparse - verify runtime uCode image in card vs. host,
 
6054
 *   using sample data 100 bytes apart.  If these sample points are good,
 
6055
 *   it's a pretty good bet that everything between them is good, too.
 
6056
 */
 
6057
static int iwl4965_verify_inst_sparse(struct iwl4965_priv *priv, __le32 *image, u32 len)
 
6058
{
 
6059
        u32 val;
 
6060
        int rc = 0;
 
6061
        u32 errcnt = 0;
 
6062
        u32 i;
 
6063
 
 
6064
        IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
 
6065
 
 
6066
        rc = iwl4965_grab_nic_access(priv);
 
6067
        if (rc)
 
6068
                return rc;
 
6069
 
 
6070
        for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
 
6071
                /* read data comes through single port, auto-incr addr */
 
6072
                /* NOTE: Use the debugless read so we don't flood kernel log
 
6073
                 * if IWL_DL_IO is set */
 
6074
                iwl4965_write_direct32(priv, HBUS_TARG_MEM_RADDR,
 
6075
                        i + RTC_INST_LOWER_BOUND);
 
6076
                val = _iwl4965_read_direct32(priv, HBUS_TARG_MEM_RDAT);
 
6077
                if (val != le32_to_cpu(*image)) {
 
6078
#if 0 /* Enable this if you want to see details */
 
6079
                        IWL_ERROR("uCode INST section is invalid at "
 
6080
                                  "offset 0x%x, is 0x%x, s/b 0x%x\n",
 
6081
                                  i, val, *image);
 
6082
#endif
 
6083
                        rc = -EIO;
 
6084
                        errcnt++;
 
6085
                        if (errcnt >= 3)
 
6086
                                break;
 
6087
                }
 
6088
        }
 
6089
 
 
6090
        iwl4965_release_nic_access(priv);
 
6091
 
 
6092
        return rc;
 
6093
}
 
6094
 
 
6095
 
 
6096
/**
 
6097
 * iwl4965_verify_ucode - determine which instruction image is in SRAM,
 
6098
 *    and verify its contents
 
6099
 */
 
6100
static int iwl4965_verify_ucode(struct iwl4965_priv *priv)
 
6101
{
 
6102
        __le32 *image;
 
6103
        u32 len;
 
6104
        int rc = 0;
 
6105
 
 
6106
        /* Try bootstrap */
 
6107
        image = (__le32 *)priv->ucode_boot.v_addr;
 
6108
        len = priv->ucode_boot.len;
 
6109
        rc = iwl4965_verify_inst_sparse(priv, image, len);
 
6110
        if (rc == 0) {
 
6111
                IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
 
6112
                return 0;
 
6113
        }
 
6114
 
 
6115
        /* Try initialize */
 
6116
        image = (__le32 *)priv->ucode_init.v_addr;
 
6117
        len = priv->ucode_init.len;
 
6118
        rc = iwl4965_verify_inst_sparse(priv, image, len);
 
6119
        if (rc == 0) {
 
6120
                IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
 
6121
                return 0;
 
6122
        }
 
6123
 
 
6124
        /* Try runtime/protocol */
 
6125
        image = (__le32 *)priv->ucode_code.v_addr;
 
6126
        len = priv->ucode_code.len;
 
6127
        rc = iwl4965_verify_inst_sparse(priv, image, len);
 
6128
        if (rc == 0) {
 
6129
                IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
 
6130
                return 0;
 
6131
        }
 
6132
 
 
6133
        IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
 
6134
 
 
6135
        /* Since nothing seems to match, show first several data entries in
 
6136
         * instruction SRAM, so maybe visual inspection will give a clue.
 
6137
         * Selection of bootstrap image (vs. other images) is arbitrary. */
 
6138
        image = (__le32 *)priv->ucode_boot.v_addr;
 
6139
        len = priv->ucode_boot.len;
 
6140
        rc = iwl4965_verify_inst_full(priv, image, len);
 
6141
 
 
6142
        return rc;
 
6143
}
 
6144
 
 
6145
 
 
6146
/* check contents of special bootstrap uCode SRAM */
 
6147
static int iwl4965_verify_bsm(struct iwl4965_priv *priv)
 
6148
{
 
6149
        __le32 *image = priv->ucode_boot.v_addr;
 
6150
        u32 len = priv->ucode_boot.len;
 
6151
        u32 reg;
 
6152
        u32 val;
 
6153
 
 
6154
        IWL_DEBUG_INFO("Begin verify bsm\n");
 
6155
 
 
6156
        /* verify BSM SRAM contents */
 
6157
        val = iwl4965_read_prph(priv, BSM_WR_DWCOUNT_REG);
 
6158
        for (reg = BSM_SRAM_LOWER_BOUND;
 
6159
             reg < BSM_SRAM_LOWER_BOUND + len;
 
6160
             reg += sizeof(u32), image ++) {
 
6161
                val = iwl4965_read_prph(priv, reg);
 
6162
                if (val != le32_to_cpu(*image)) {
 
6163
                        IWL_ERROR("BSM uCode verification failed at "
 
6164
                                  "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
 
6165
                                  BSM_SRAM_LOWER_BOUND,
 
6166
                                  reg - BSM_SRAM_LOWER_BOUND, len,
 
6167
                                  val, le32_to_cpu(*image));
 
6168
                        return -EIO;
 
6169
                }
 
6170
        }
 
6171
 
 
6172
        IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
 
6173
 
 
6174
        return 0;
 
6175
}
 
6176
 
 
6177
/**
 
6178
 * iwl4965_load_bsm - Load bootstrap instructions
 
6179
 *
 
6180
 * BSM operation:
 
6181
 *
 
6182
 * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
 
6183
 * in special SRAM that does not power down during RFKILL.  When powering back
 
6184
 * up after power-saving sleeps (or during initial uCode load), the BSM loads
 
6185
 * the bootstrap program into the on-board processor, and starts it.
 
6186
 *
 
6187
 * The bootstrap program loads (via DMA) instructions and data for a new
 
6188
 * program from host DRAM locations indicated by the host driver in the
 
6189
 * BSM_DRAM_* registers.  Once the new program is loaded, it starts
 
6190
 * automatically.
 
6191
 *
 
6192
 * When initializing the NIC, the host driver points the BSM to the
 
6193
 * "initialize" uCode image.  This uCode sets up some internal data, then
 
6194
 * notifies host via "initialize alive" that it is complete.
 
6195
 *
 
6196
 * The host then replaces the BSM_DRAM_* pointer values to point to the
 
6197
 * normal runtime uCode instructions and a backup uCode data cache buffer
 
6198
 * (filled initially with starting data values for the on-board processor),
 
6199
 * then triggers the "initialize" uCode to load and launch the runtime uCode,
 
6200
 * which begins normal operation.
 
6201
 *
 
6202
 * When doing a power-save shutdown, runtime uCode saves data SRAM into
 
6203
 * the backup data cache in DRAM before SRAM is powered down.
 
6204
 *
 
6205
 * When powering back up, the BSM loads the bootstrap program.  This reloads
 
6206
 * the runtime uCode instructions and the backup data cache into SRAM,
 
6207
 * and re-launches the runtime uCode from where it left off.
 
6208
 */
 
6209
static int iwl4965_load_bsm(struct iwl4965_priv *priv)
 
6210
{
 
6211
        __le32 *image = priv->ucode_boot.v_addr;
 
6212
        u32 len = priv->ucode_boot.len;
 
6213
        dma_addr_t pinst;
 
6214
        dma_addr_t pdata;
 
6215
        u32 inst_len;
 
6216
        u32 data_len;
 
6217
        int rc;
 
6218
        int i;
 
6219
        u32 done;
 
6220
        u32 reg_offset;
 
6221
 
 
6222
        IWL_DEBUG_INFO("Begin load bsm\n");
 
6223
 
 
6224
        /* make sure bootstrap program is no larger than BSM's SRAM size */
 
6225
        if (len > IWL_MAX_BSM_SIZE)
 
6226
                return -EINVAL;
 
6227
 
 
6228
        /* Tell bootstrap uCode where to find the "Initialize" uCode
 
6229
         *   in host DRAM ... host DRAM physical address bits 35:4 for 4965.
 
6230
         * NOTE:  iwl4965_initialize_alive_start() will replace these values,
 
6231
         *        after the "initialize" uCode has run, to point to
 
6232
         *        runtime/protocol instructions and backup data cache. */
 
6233
        pinst = priv->ucode_init.p_addr >> 4;
 
6234
        pdata = priv->ucode_init_data.p_addr >> 4;
 
6235
        inst_len = priv->ucode_init.len;
 
6236
        data_len = priv->ucode_init_data.len;
 
6237
 
 
6238
        rc = iwl4965_grab_nic_access(priv);
 
6239
        if (rc)
 
6240
                return rc;
 
6241
 
 
6242
        iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
 
6243
        iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
 
6244
        iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
 
6245
        iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
 
6246
 
 
6247
        /* Fill BSM memory with bootstrap instructions */
 
6248
        for (reg_offset = BSM_SRAM_LOWER_BOUND;
 
6249
             reg_offset < BSM_SRAM_LOWER_BOUND + len;
 
6250
             reg_offset += sizeof(u32), image++)
 
6251
                _iwl4965_write_prph(priv, reg_offset,
 
6252
                                          le32_to_cpu(*image));
 
6253
 
 
6254
        rc = iwl4965_verify_bsm(priv);
 
6255
        if (rc) {
 
6256
                iwl4965_release_nic_access(priv);
 
6257
                return rc;
 
6258
        }
 
6259
 
 
6260
        /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
 
6261
        iwl4965_write_prph(priv, BSM_WR_MEM_SRC_REG, 0x0);
 
6262
        iwl4965_write_prph(priv, BSM_WR_MEM_DST_REG,
 
6263
                                 RTC_INST_LOWER_BOUND);
 
6264
        iwl4965_write_prph(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
 
6265
 
 
6266
        /* Load bootstrap code into instruction SRAM now,
 
6267
         *   to prepare to load "initialize" uCode */
 
6268
        iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
 
6269
                BSM_WR_CTRL_REG_BIT_START);
 
6270
 
 
6271
        /* Wait for load of bootstrap uCode to finish */
 
6272
        for (i = 0; i < 100; i++) {
 
6273
                done = iwl4965_read_prph(priv, BSM_WR_CTRL_REG);
 
6274
                if (!(done & BSM_WR_CTRL_REG_BIT_START))
 
6275
                        break;
 
6276
                udelay(10);
 
6277
        }
 
6278
        if (i < 100)
 
6279
                IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
 
6280
        else {
 
6281
                IWL_ERROR("BSM write did not complete!\n");
 
6282
                return -EIO;
 
6283
        }
 
6284
 
 
6285
        /* Enable future boot loads whenever power management unit triggers it
 
6286
         *   (e.g. when powering back up after power-save shutdown) */
 
6287
        iwl4965_write_prph(priv, BSM_WR_CTRL_REG,
 
6288
                BSM_WR_CTRL_REG_BIT_START_EN);
 
6289
 
 
6290
        iwl4965_release_nic_access(priv);
 
6291
 
 
6292
        return 0;
 
6293
}
 
6294
 
 
6295
static void iwl4965_nic_start(struct iwl4965_priv *priv)
 
6296
{
 
6297
        /* Remove all resets to allow NIC to operate */
 
6298
        iwl4965_write32(priv, CSR_RESET, 0);
 
6299
}
 
6300
 
 
6301
 
 
6302
/**
 
6303
 * iwl4965_read_ucode - Read uCode images from disk file.
 
6304
 *
 
6305
 * Copy into buffers for card to fetch via bus-mastering
 
6306
 */
 
6307
static int iwl4965_read_ucode(struct iwl4965_priv *priv)
 
6308
{
 
6309
        struct iwl4965_ucode *ucode;
 
6310
        int ret;
 
6311
        const struct firmware *ucode_raw;
 
6312
        const char *name = "iwlwifi-4965" IWL4965_UCODE_API ".ucode";
 
6313
        u8 *src;
 
6314
        size_t len;
 
6315
        u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
 
6316
 
 
6317
        /* Ask kernel firmware_class module to get the boot firmware off disk.
 
6318
         * request_firmware() is synchronous, file is in memory on return. */
 
6319
        ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
 
6320
        if (ret < 0) {
 
6321
                IWL_ERROR("%s firmware file req failed: Reason %d\n",
 
6322
                                        name, ret);
 
6323
                goto error;
 
6324
        }
 
6325
 
 
6326
        IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
 
6327
                       name, ucode_raw->size);
 
6328
 
 
6329
        /* Make sure that we got at least our header! */
 
6330
        if (ucode_raw->size < sizeof(*ucode)) {
 
6331
                IWL_ERROR("File size way too small!\n");
 
6332
                ret = -EINVAL;
 
6333
                goto err_release;
 
6334
        }
 
6335
 
 
6336
        /* Data from ucode file:  header followed by uCode images */
 
6337
        ucode = (void *)ucode_raw->data;
 
6338
 
 
6339
        ver = le32_to_cpu(ucode->ver);
 
6340
        inst_size = le32_to_cpu(ucode->inst_size);
 
6341
        data_size = le32_to_cpu(ucode->data_size);
 
6342
        init_size = le32_to_cpu(ucode->init_size);
 
6343
        init_data_size = le32_to_cpu(ucode->init_data_size);
 
6344
        boot_size = le32_to_cpu(ucode->boot_size);
 
6345
 
 
6346
        IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
 
6347
        IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
 
6348
                       inst_size);
 
6349
        IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
 
6350
                       data_size);
 
6351
        IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
 
6352
                       init_size);
 
6353
        IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
 
6354
                       init_data_size);
 
6355
        IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
 
6356
                       boot_size);
 
6357
 
 
6358
        /* Verify size of file vs. image size info in file's header */
 
6359
        if (ucode_raw->size < sizeof(*ucode) +
 
6360
                inst_size + data_size + init_size +
 
6361
                init_data_size + boot_size) {
 
6362
 
 
6363
                IWL_DEBUG_INFO("uCode file size %d too small\n",
 
6364
                               (int)ucode_raw->size);
 
6365
                ret = -EINVAL;
 
6366
                goto err_release;
 
6367
        }
 
6368
 
 
6369
        /* Verify that uCode images will fit in card's SRAM */
 
6370
        if (inst_size > IWL_MAX_INST_SIZE) {
 
6371
                IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
 
6372
                               inst_size);
 
6373
                ret = -EINVAL;
 
6374
                goto err_release;
 
6375
        }
 
6376
 
 
6377
        if (data_size > IWL_MAX_DATA_SIZE) {
 
6378
                IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
 
6379
                                data_size);
 
6380
                ret = -EINVAL;
 
6381
                goto err_release;
 
6382
        }
 
6383
        if (init_size > IWL_MAX_INST_SIZE) {
 
6384
                IWL_DEBUG_INFO
 
6385
                    ("uCode init instr len %d too large to fit in\n",
 
6386
                      init_size);
 
6387
                ret = -EINVAL;
 
6388
                goto err_release;
 
6389
        }
 
6390
        if (init_data_size > IWL_MAX_DATA_SIZE) {
 
6391
                IWL_DEBUG_INFO
 
6392
                    ("uCode init data len %d too large to fit in\n",
 
6393
                      init_data_size);
 
6394
                ret = -EINVAL;
 
6395
                goto err_release;
 
6396
        }
 
6397
        if (boot_size > IWL_MAX_BSM_SIZE) {
 
6398
                IWL_DEBUG_INFO
 
6399
                    ("uCode boot instr len %d too large to fit in\n",
 
6400
                      boot_size);
 
6401
                ret = -EINVAL;
 
6402
                goto err_release;
 
6403
        }
 
6404
 
 
6405
        /* Allocate ucode buffers for card's bus-master loading ... */
 
6406
 
 
6407
        /* Runtime instructions and 2 copies of data:
 
6408
         * 1) unmodified from disk
 
6409
         * 2) backup cache for save/restore during power-downs */
 
6410
        priv->ucode_code.len = inst_size;
 
6411
        iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
 
6412
 
 
6413
        priv->ucode_data.len = data_size;
 
6414
        iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
 
6415
 
 
6416
        priv->ucode_data_backup.len = data_size;
 
6417
        iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
 
6418
 
 
6419
        /* Initialization instructions and data */
 
6420
        if (init_size && init_data_size) {
 
6421
                priv->ucode_init.len = init_size;
 
6422
                iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
 
6423
 
 
6424
                priv->ucode_init_data.len = init_data_size;
 
6425
                iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
 
6426
 
 
6427
                if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
 
6428
                        goto err_pci_alloc;
 
6429
        }
 
6430
 
 
6431
        /* Bootstrap (instructions only, no data) */
 
6432
        if (boot_size) {
 
6433
                priv->ucode_boot.len = boot_size;
 
6434
                iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
 
6435
 
 
6436
                if (!priv->ucode_boot.v_addr)
 
6437
                        goto err_pci_alloc;
 
6438
        }
 
6439
 
 
6440
        /* Copy images into buffers for card's bus-master reads ... */
 
6441
 
 
6442
        /* Runtime instructions (first block of data in file) */
 
6443
        src = &ucode->data[0];
 
6444
        len = priv->ucode_code.len;
 
6445
        IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
 
6446
        memcpy(priv->ucode_code.v_addr, src, len);
 
6447
        IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
 
6448
                priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
 
6449
 
 
6450
        /* Runtime data (2nd block)
 
6451
         * NOTE:  Copy into backup buffer will be done in iwl4965_up()  */
 
6452
        src = &ucode->data[inst_size];
 
6453
        len = priv->ucode_data.len;
 
6454
        IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
 
6455
        memcpy(priv->ucode_data.v_addr, src, len);
 
6456
        memcpy(priv->ucode_data_backup.v_addr, src, len);
 
6457
 
 
6458
        /* Initialization instructions (3rd block) */
 
6459
        if (init_size) {
 
6460
                src = &ucode->data[inst_size + data_size];
 
6461
                len = priv->ucode_init.len;
 
6462
                IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
 
6463
                                len);
 
6464
                memcpy(priv->ucode_init.v_addr, src, len);
 
6465
        }
 
6466
 
 
6467
        /* Initialization data (4th block) */
 
6468
        if (init_data_size) {
 
6469
                src = &ucode->data[inst_size + data_size + init_size];
 
6470
                len = priv->ucode_init_data.len;
 
6471
                IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
 
6472
                               len);
 
6473
                memcpy(priv->ucode_init_data.v_addr, src, len);
 
6474
        }
 
6475
 
 
6476
        /* Bootstrap instructions (5th block) */
 
6477
        src = &ucode->data[inst_size + data_size + init_size + init_data_size];
 
6478
        len = priv->ucode_boot.len;
 
6479
        IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
 
6480
        memcpy(priv->ucode_boot.v_addr, src, len);
 
6481
 
 
6482
        /* We have our copies now, allow OS release its copies */
 
6483
        release_firmware(ucode_raw);
 
6484
        return 0;
 
6485
 
 
6486
 err_pci_alloc:
 
6487
        IWL_ERROR("failed to allocate pci memory\n");
 
6488
        ret = -ENOMEM;
 
6489
        iwl4965_dealloc_ucode_pci(priv);
 
6490
 
 
6491
 err_release:
 
6492
        release_firmware(ucode_raw);
 
6493
 
 
6494
 error:
 
6495
        return ret;
 
6496
}
 
6497
 
 
6498
 
 
6499
/**
 
6500
 * iwl4965_set_ucode_ptrs - Set uCode address location
 
6501
 *
 
6502
 * Tell initialization uCode where to find runtime uCode.
 
6503
 *
 
6504
 * BSM registers initially contain pointers to initialization uCode.
 
6505
 * We need to replace them to load runtime uCode inst and data,
 
6506
 * and to save runtime data when powering down.
 
6507
 */
 
6508
static int iwl4965_set_ucode_ptrs(struct iwl4965_priv *priv)
 
6509
{
 
6510
        dma_addr_t pinst;
 
6511
        dma_addr_t pdata;
 
6512
        int rc = 0;
 
6513
        unsigned long flags;
 
6514
 
 
6515
        /* bits 35:4 for 4965 */
 
6516
        pinst = priv->ucode_code.p_addr >> 4;
 
6517
        pdata = priv->ucode_data_backup.p_addr >> 4;
 
6518
 
 
6519
        spin_lock_irqsave(&priv->lock, flags);
 
6520
        rc = iwl4965_grab_nic_access(priv);
 
6521
        if (rc) {
 
6522
                spin_unlock_irqrestore(&priv->lock, flags);
 
6523
                return rc;
 
6524
        }
 
6525
 
 
6526
        /* Tell bootstrap uCode where to find image to load */
 
6527
        iwl4965_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
 
6528
        iwl4965_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
 
6529
        iwl4965_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
 
6530
                                 priv->ucode_data.len);
 
6531
 
 
6532
        /* Inst bytecount must be last to set up, bit 31 signals uCode
 
6533
         *   that all new ptr/size info is in place */
 
6534
        iwl4965_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
 
6535
                                 priv->ucode_code.len | BSM_DRAM_INST_LOAD);
 
6536
 
 
6537
        iwl4965_release_nic_access(priv);
 
6538
 
 
6539
        spin_unlock_irqrestore(&priv->lock, flags);
 
6540
 
 
6541
        IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
 
6542
 
 
6543
        return rc;
 
6544
}
 
6545
 
 
6546
/**
 
6547
 * iwl4965_init_alive_start - Called after REPLY_ALIVE notification received
 
6548
 *
 
6549
 * Called after REPLY_ALIVE notification received from "initialize" uCode.
 
6550
 *
 
6551
 * The 4965 "initialize" ALIVE reply contains calibration data for:
 
6552
 *   Voltage, temperature, and MIMO tx gain correction, now stored in priv
 
6553
 *   (3945 does not contain this data).
 
6554
 *
 
6555
 * Tell "initialize" uCode to go ahead and load the runtime uCode.
 
6556
*/
 
6557
static void iwl4965_init_alive_start(struct iwl4965_priv *priv)
 
6558
{
 
6559
        /* Check alive response for "valid" sign from uCode */
 
6560
        if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
 
6561
                /* We had an error bringing up the hardware, so take it
 
6562
                 * all the way back down so we can try again */
 
6563
                IWL_DEBUG_INFO("Initialize Alive failed.\n");
 
6564
                goto restart;
 
6565
        }
 
6566
 
 
6567
        /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
 
6568
         * This is a paranoid check, because we would not have gotten the
 
6569
         * "initialize" alive if code weren't properly loaded.  */
 
6570
        if (iwl4965_verify_ucode(priv)) {
 
6571
                /* Runtime instruction load was bad;
 
6572
                 * take it all the way back down so we can try again */
 
6573
                IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
 
6574
                goto restart;
 
6575
        }
 
6576
 
 
6577
        /* Calculate temperature */
 
6578
        priv->temperature = iwl4965_get_temperature(priv);
 
6579
 
 
6580
        /* Send pointers to protocol/runtime uCode image ... init code will
 
6581
         * load and launch runtime uCode, which will send us another "Alive"
 
6582
         * notification. */
 
6583
        IWL_DEBUG_INFO("Initialization Alive received.\n");
 
6584
        if (iwl4965_set_ucode_ptrs(priv)) {
 
6585
                /* Runtime instruction load won't happen;
 
6586
                 * take it all the way back down so we can try again */
 
6587
                IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
 
6588
                goto restart;
 
6589
        }
 
6590
        return;
 
6591
 
 
6592
 restart:
 
6593
        queue_work(priv->workqueue, &priv->restart);
 
6594
}
 
6595
 
 
6596
 
 
6597
/**
 
6598
 * iwl4965_alive_start - called after REPLY_ALIVE notification received
 
6599
 *                   from protocol/runtime uCode (initialization uCode's
 
6600
 *                   Alive gets handled by iwl4965_init_alive_start()).
 
6601
 */
 
6602
static void iwl4965_alive_start(struct iwl4965_priv *priv)
 
6603
{
 
6604
        int rc = 0;
 
6605
 
 
6606
        IWL_DEBUG_INFO("Runtime Alive received.\n");
 
6607
 
 
6608
        if (priv->card_alive.is_valid != UCODE_VALID_OK) {
 
6609
                /* We had an error bringing up the hardware, so take it
 
6610
                 * all the way back down so we can try again */
 
6611
                IWL_DEBUG_INFO("Alive failed.\n");
 
6612
                goto restart;
 
6613
        }
 
6614
 
 
6615
        /* Initialize uCode has loaded Runtime uCode ... verify inst image.
 
6616
         * This is a paranoid check, because we would not have gotten the
 
6617
         * "runtime" alive if code weren't properly loaded.  */
 
6618
        if (iwl4965_verify_ucode(priv)) {
 
6619
                /* Runtime instruction load was bad;
 
6620
                 * take it all the way back down so we can try again */
 
6621
                IWL_DEBUG_INFO("Bad runtime uCode load.\n");
 
6622
                goto restart;
 
6623
        }
 
6624
 
 
6625
        iwl4965_clear_stations_table(priv);
 
6626
 
 
6627
        rc = iwl4965_alive_notify(priv);
 
6628
        if (rc) {
 
6629
                IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
 
6630
                            rc);
 
6631
                goto restart;
 
6632
        }
 
6633
 
 
6634
        /* After the ALIVE response, we can send host commands to 4965 uCode */
 
6635
        set_bit(STATUS_ALIVE, &priv->status);
 
6636
 
 
6637
        /* Clear out the uCode error bit if it is set */
 
6638
        clear_bit(STATUS_FW_ERROR, &priv->status);
 
6639
 
 
6640
        rc = iwl4965_init_channel_map(priv);
 
6641
        if (rc) {
 
6642
                IWL_ERROR("initializing regulatory failed: %d\n", rc);
 
6643
                return;
 
6644
        }
 
6645
 
 
6646
        iwl4965_init_geos(priv);
 
6647
        iwl4965_reset_channel_flag(priv);
 
6648
 
 
6649
        if (iwl4965_is_rfkill(priv))
 
6650
                return;
 
6651
 
 
6652
        iwlwifi_ieee80211_start_queues(priv->hw);
 
6653
 
 
6654
        priv->active_rate = priv->rates_mask;
 
6655
        priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
 
6656
 
 
6657
        iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
 
6658
 
 
6659
        if (iwl4965_is_associated(priv)) {
 
6660
                struct iwl4965_rxon_cmd *active_rxon =
 
6661
                                (struct iwl4965_rxon_cmd *)(&priv->active_rxon);
 
6662
 
 
6663
                memcpy(&priv->staging_rxon, &priv->active_rxon,
 
6664
                       sizeof(priv->staging_rxon));
 
6665
                active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 
6666
        } else {
 
6667
                /* Initialize our rx_config data */
 
6668
                iwl4965_connection_init_rx_config(priv);
 
6669
                memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
 
6670
        }
 
6671
 
 
6672
        /* Configure Bluetooth device coexistence support */
 
6673
        iwl4965_send_bt_config(priv);
 
6674
 
 
6675
        /* Configure the adapter for unassociated operation */
 
6676
        iwl4965_commit_rxon(priv);
 
6677
 
 
6678
        /* At this point, the NIC is initialized and operational */
 
6679
        priv->notif_missed_beacons = 0;
 
6680
        set_bit(STATUS_READY, &priv->status);
 
6681
 
 
6682
        iwl4965_rf_kill_ct_config(priv);
 
6683
 
 
6684
        IWL_DEBUG_INFO("ALIVE processing complete.\n");
 
6685
        wake_up_interruptible(&priv->wait_command_queue);
 
6686
 
 
6687
        if (priv->error_recovering)
 
6688
                iwl4965_error_recovery(priv);
 
6689
 
 
6690
        return;
 
6691
 
 
6692
 restart:
 
6693
        queue_work(priv->workqueue, &priv->restart);
 
6694
}
 
6695
 
 
6696
static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv);
 
6697
 
 
6698
static void __iwl4965_down(struct iwl4965_priv *priv)
 
6699
{
 
6700
        unsigned long flags;
 
6701
        int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
 
6702
        struct ieee80211_conf *conf = NULL;
 
6703
 
 
6704
        IWL_DEBUG_INFO(DRV_NAME " is going down\n");
 
6705
 
 
6706
        conf = ieee80211_get_hw_conf(priv->hw);
 
6707
 
 
6708
        if (!exit_pending)
 
6709
                set_bit(STATUS_EXIT_PENDING, &priv->status);
 
6710
 
 
6711
        iwl4965_clear_stations_table(priv);
 
6712
 
 
6713
        /* Unblock any waiting calls */
 
6714
        wake_up_interruptible_all(&priv->wait_command_queue);
 
6715
 
 
6716
        /* Wipe out the EXIT_PENDING status bit if we are not actually
 
6717
         * exiting the module */
 
6718
        if (!exit_pending)
 
6719
                clear_bit(STATUS_EXIT_PENDING, &priv->status);
 
6720
 
 
6721
        /* stop and reset the on-board processor */
 
6722
        iwl4965_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
 
6723
 
 
6724
        /* tell the device to stop sending interrupts */
 
6725
        iwl4965_disable_interrupts(priv);
 
6726
 
 
6727
        if (priv->mac80211_registered)
 
6728
                iwlwifi_iwlwifi_ieee80211_stop_queues(priv->hw);
 
6729
 
 
6730
        /* If we have not previously called iwl4965_init() then
 
6731
         * clear all bits but the RF Kill and SUSPEND bits and return */
 
6732
        if (!iwl4965_is_init(priv)) {
 
6733
                priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
 
6734
                                        STATUS_RF_KILL_HW |
 
6735
                               test_bit(STATUS_RF_KILL_SW, &priv->status) <<
 
6736
                                        STATUS_RF_KILL_SW |
 
6737
                               test_bit(STATUS_IN_SUSPEND, &priv->status) <<
 
6738
                                        STATUS_IN_SUSPEND;
 
6739
                goto exit;
 
6740
        }
 
6741
 
 
6742
        /* ...otherwise clear out all the status bits but the RF Kill and
 
6743
         * SUSPEND bits and continue taking the NIC down. */
 
6744
        priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
 
6745
                                STATUS_RF_KILL_HW |
 
6746
                        test_bit(STATUS_RF_KILL_SW, &priv->status) <<
 
6747
                                STATUS_RF_KILL_SW |
 
6748
                        test_bit(STATUS_IN_SUSPEND, &priv->status) <<
 
6749
                                STATUS_IN_SUSPEND |
 
6750
                        test_bit(STATUS_FW_ERROR, &priv->status) <<
 
6751
                                STATUS_FW_ERROR;
 
6752
 
 
6753
        spin_lock_irqsave(&priv->lock, flags);
 
6754
        iwl4965_clear_bit(priv, CSR_GP_CNTRL,
 
6755
                         CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
 
6756
        spin_unlock_irqrestore(&priv->lock, flags);
 
6757
 
 
6758
        iwl4965_hw_txq_ctx_stop(priv);
 
6759
        iwl4965_hw_rxq_stop(priv);
 
6760
 
 
6761
        spin_lock_irqsave(&priv->lock, flags);
 
6762
        if (!iwl4965_grab_nic_access(priv)) {
 
6763
                iwl4965_write_prph(priv, APMG_CLK_DIS_REG,
 
6764
                                         APMG_CLK_VAL_DMA_CLK_RQT);
 
6765
                iwl4965_release_nic_access(priv);
 
6766
        }
 
6767
        spin_unlock_irqrestore(&priv->lock, flags);
 
6768
 
 
6769
        udelay(5);
 
6770
 
 
6771
        iwl4965_hw_nic_stop_master(priv);
 
6772
        iwl4965_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
 
6773
        iwl4965_hw_nic_reset(priv);
 
6774
 
 
6775
 exit:
 
6776
        memset(&priv->card_alive, 0, sizeof(struct iwl4965_alive_resp));
 
6777
 
 
6778
        if (priv->ibss_beacon)
 
6779
                dev_kfree_skb(priv->ibss_beacon);
 
6780
        priv->ibss_beacon = NULL;
 
6781
 
 
6782
        /* clear out any free frames */
 
6783
        iwl4965_clear_free_frames(priv);
 
6784
}
 
6785
 
 
6786
static void iwl4965_down(struct iwl4965_priv *priv)
 
6787
{
 
6788
        mutex_lock(&priv->mutex);
 
6789
        __iwl4965_down(priv);
 
6790
        mutex_unlock(&priv->mutex);
 
6791
 
 
6792
        iwl4965_cancel_deferred_work(priv);
 
6793
}
 
6794
 
 
6795
#define MAX_HW_RESTARTS 5
 
6796
 
 
6797
static int __iwl4965_up(struct iwl4965_priv *priv)
 
6798
{
 
6799
        int rc, i;
 
6800
 
 
6801
        if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
 
6802
                IWL_WARNING("Exit pending; will not bring the NIC up\n");
 
6803
                return -EIO;
 
6804
        }
 
6805
 
 
6806
        if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
 
6807
                IWL_WARNING("Radio disabled by SW RF kill (module "
 
6808
                            "parameter)\n");
 
6809
                return -ENODEV;
 
6810
        }
 
6811
 
 
6812
        /* If platform's RF_KILL switch is NOT set to KILL */
 
6813
        if (iwl4965_read32(priv, CSR_GP_CNTRL) &
 
6814
                                CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
 
6815
                clear_bit(STATUS_RF_KILL_HW, &priv->status);
 
6816
        else {
 
6817
                set_bit(STATUS_RF_KILL_HW, &priv->status);
 
6818
                if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
 
6819
                        IWL_WARNING("Radio disabled by HW RF Kill switch\n");
 
6820
                        return -ENODEV;
 
6821
                }
 
6822
        }
 
6823
 
 
6824
        iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
 
6825
 
 
6826
        rc = iwl4965_hw_nic_init(priv);
 
6827
        if (rc) {
 
6828
                IWL_ERROR("Unable to int nic\n");
 
6829
                return rc;
 
6830
        }
 
6831
 
 
6832
        /* make sure rfkill handshake bits are cleared */
 
6833
        iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
 
6834
        iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR,
 
6835
                    CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
 
6836
 
 
6837
        /* clear (again), then enable host interrupts */
 
6838
        iwl4965_write32(priv, CSR_INT, 0xFFFFFFFF);
 
6839
        iwl4965_enable_interrupts(priv);
 
6840
 
 
6841
        /* really make sure rfkill handshake bits are cleared */
 
6842
        iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
 
6843
        iwl4965_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
 
6844
 
 
6845
        /* Copy original ucode data image from disk into backup cache.
 
6846
         * This will be used to initialize the on-board processor's
 
6847
         * data SRAM for a clean start when the runtime program first loads. */
 
6848
        memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
 
6849
               priv->ucode_data.len);
 
6850
 
 
6851
        /* We return success when we resume from suspend and rf_kill is on. */
 
6852
        if (test_bit(STATUS_RF_KILL_HW, &priv->status))
 
6853
                return 0;
 
6854
 
 
6855
        for (i = 0; i < MAX_HW_RESTARTS; i++) {
 
6856
 
 
6857
                iwl4965_clear_stations_table(priv);
 
6858
 
 
6859
                /* load bootstrap state machine,
 
6860
                 * load bootstrap program into processor's memory,
 
6861
                 * prepare to load the "initialize" uCode */
 
6862
                rc = iwl4965_load_bsm(priv);
 
6863
 
 
6864
                if (rc) {
 
6865
                        IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
 
6866
                        continue;
 
6867
                }
 
6868
 
 
6869
                /* start card; "initialize" will load runtime ucode */
 
6870
                iwl4965_nic_start(priv);
 
6871
 
 
6872
                IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
 
6873
 
 
6874
                return 0;
 
6875
        }
 
6876
 
 
6877
        set_bit(STATUS_EXIT_PENDING, &priv->status);
 
6878
        __iwl4965_down(priv);
 
6879
 
 
6880
        /* tried to restart and config the device for as long as our
 
6881
         * patience could withstand */
 
6882
        IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
 
6883
        return -EIO;
 
6884
}
 
6885
 
 
6886
 
 
6887
/*****************************************************************************
 
6888
 *
 
6889
 * Workqueue callbacks
 
6890
 *
 
6891
 *****************************************************************************/
 
6892
 
 
6893
static void iwl4965_bg_init_alive_start(struct work_struct *data)
 
6894
{
 
6895
        struct iwl4965_priv *priv =
 
6896
            container_of(data, struct iwl4965_priv, init_alive_start.work);
 
6897
 
 
6898
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 
6899
                return;
 
6900
 
 
6901
        mutex_lock(&priv->mutex);
 
6902
        iwl4965_init_alive_start(priv);
 
6903
        mutex_unlock(&priv->mutex);
 
6904
}
 
6905
 
 
6906
static void iwl4965_bg_alive_start(struct work_struct *data)
 
6907
{
 
6908
        struct iwl4965_priv *priv =
 
6909
            container_of(data, struct iwl4965_priv, alive_start.work);
 
6910
 
 
6911
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 
6912
                return;
 
6913
 
 
6914
        mutex_lock(&priv->mutex);
 
6915
        iwl4965_alive_start(priv);
 
6916
        mutex_unlock(&priv->mutex);
 
6917
}
 
6918
 
 
6919
static void iwl4965_bg_rf_kill(struct work_struct *work)
 
6920
{
 
6921
        struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, rf_kill);
 
6922
 
 
6923
        wake_up_interruptible(&priv->wait_command_queue);
 
6924
 
 
6925
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 
6926
                return;
 
6927
 
 
6928
        mutex_lock(&priv->mutex);
 
6929
 
 
6930
        if (!iwl4965_is_rfkill(priv)) {
 
6931
                IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
 
6932
                          "HW and/or SW RF Kill no longer active, restarting "
 
6933
                          "device\n");
 
6934
                if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
 
6935
                        queue_work(priv->workqueue, &priv->restart);
 
6936
        } else {
 
6937
 
 
6938
                if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
 
6939
                        IWL_DEBUG_RF_KILL("Can not turn radio back on - "
 
6940
                                          "disabled by SW switch\n");
 
6941
                else
 
6942
                        IWL_WARNING("Radio Frequency Kill Switch is On:\n"
 
6943
                                    "Kill switch must be turned off for "
 
6944
                                    "wireless networking to work.\n");
 
6945
        }
 
6946
        mutex_unlock(&priv->mutex);
 
6947
}
 
6948
 
 
6949
#define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
 
6950
 
 
6951
static void iwl4965_bg_scan_check(struct work_struct *data)
 
6952
{
 
6953
        struct iwl4965_priv *priv =
 
6954
            container_of(data, struct iwl4965_priv, scan_check.work);
 
6955
 
 
6956
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 
6957
                return;
 
6958
 
 
6959
        mutex_lock(&priv->mutex);
 
6960
        if (test_bit(STATUS_SCANNING, &priv->status) ||
 
6961
            test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
 
6962
                IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
 
6963
                          "Scan completion watchdog resetting adapter (%dms)\n",
 
6964
                          jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
 
6965
 
 
6966
                if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
 
6967
                        iwl4965_send_scan_abort(priv);
 
6968
        }
 
6969
        mutex_unlock(&priv->mutex);
 
6970
}
 
6971
 
 
6972
static void iwl4965_bg_request_scan(struct work_struct *data)
 
6973
{
 
6974
        struct iwl4965_priv *priv =
 
6975
            container_of(data, struct iwl4965_priv, request_scan);
 
6976
        struct iwl4965_host_cmd cmd = {
 
6977
                .id = REPLY_SCAN_CMD,
 
6978
                .len = sizeof(struct iwl4965_scan_cmd),
 
6979
                .meta.flags = CMD_SIZE_HUGE,
 
6980
        };
 
6981
        int rc = 0;
 
6982
        struct iwl4965_scan_cmd *scan;
 
6983
        struct ieee80211_conf *conf = NULL;
 
6984
        u8 direct_mask;
 
6985
        int phymode;
 
6986
 
 
6987
        conf = ieee80211_get_hw_conf(priv->hw);
 
6988
 
 
6989
        mutex_lock(&priv->mutex);
 
6990
 
 
6991
        if (!iwl4965_is_ready(priv)) {
 
6992
                IWL_WARNING("request scan called when driver not ready.\n");
 
6993
                goto done;
 
6994
        }
 
6995
 
 
6996
        /* Make sure the scan wasn't cancelled before this queued work
 
6997
         * was given the chance to run... */
 
6998
        if (!test_bit(STATUS_SCANNING, &priv->status))
 
6999
                goto done;
 
7000
 
 
7001
        /* This should never be called or scheduled if there is currently
 
7002
         * a scan active in the hardware. */
 
7003
        if (test_bit(STATUS_SCAN_HW, &priv->status)) {
 
7004
                IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
 
7005
                               "Ignoring second request.\n");
 
7006
                rc = -EIO;
 
7007
                goto done;
 
7008
        }
 
7009
 
 
7010
        if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
 
7011
                IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
 
7012
                goto done;
 
7013
        }
 
7014
 
 
7015
        if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
 
7016
                IWL_DEBUG_HC("Scan request while abort pending.  Queuing.\n");
 
7017
                goto done;
 
7018
        }
 
7019
 
 
7020
        if (iwl4965_is_rfkill(priv)) {
 
7021
                IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
 
7022
                goto done;
 
7023
        }
 
7024
 
 
7025
        if (!test_bit(STATUS_READY, &priv->status)) {
 
7026
                IWL_DEBUG_HC("Scan request while uninitialized.  Queuing.\n");
 
7027
                goto done;
 
7028
        }
 
7029
 
 
7030
        if (!priv->scan_bands) {
 
7031
                IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
 
7032
                goto done;
 
7033
        }
 
7034
 
 
7035
        if (!priv->scan) {
 
7036
                priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
 
7037
                                     IWL_MAX_SCAN_SIZE, GFP_KERNEL);
 
7038
                if (!priv->scan) {
 
7039
                        rc = -ENOMEM;
 
7040
                        goto done;
 
7041
                }
 
7042
        }
 
7043
        scan = priv->scan;
 
7044
        memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
 
7045
 
 
7046
        scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
 
7047
        scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
 
7048
 
 
7049
        if (iwl4965_is_associated(priv)) {
 
7050
                u16 interval = 0;
 
7051
                u32 extra;
 
7052
                u32 suspend_time = 100;
 
7053
                u32 scan_suspend_time = 100;
 
7054
                unsigned long flags;
 
7055
 
 
7056
                IWL_DEBUG_INFO("Scanning while associated...\n");
 
7057
 
 
7058
                spin_lock_irqsave(&priv->lock, flags);
 
7059
                interval = priv->beacon_int;
 
7060
                spin_unlock_irqrestore(&priv->lock, flags);
 
7061
 
 
7062
                scan->suspend_time = 0;
 
7063
                scan->max_out_time = cpu_to_le32(200 * 1024);
 
7064
                if (!interval)
 
7065
                        interval = suspend_time;
 
7066
 
 
7067
                extra = (suspend_time / interval) << 22;
 
7068
                scan_suspend_time = (extra |
 
7069
                    ((suspend_time % interval) * 1024));
 
7070
                scan->suspend_time = cpu_to_le32(scan_suspend_time);
 
7071
                IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
 
7072
                               scan_suspend_time, interval);
 
7073
        }
 
7074
 
 
7075
        /* We should add the ability for user to lock to PASSIVE ONLY */
 
7076
        if (priv->one_direct_scan) {
 
7077
                IWL_DEBUG_SCAN
 
7078
                    ("Kicking off one direct scan for '%s'\n",
 
7079
                     iwl4965_escape_essid(priv->direct_ssid,
 
7080
                                      priv->direct_ssid_len));
 
7081
                scan->direct_scan[0].id = WLAN_EID_SSID;
 
7082
                scan->direct_scan[0].len = priv->direct_ssid_len;
 
7083
                memcpy(scan->direct_scan[0].ssid,
 
7084
                       priv->direct_ssid, priv->direct_ssid_len);
 
7085
                direct_mask = 1;
 
7086
        } else if (!iwl4965_is_associated(priv) && priv->essid_len) {
 
7087
                scan->direct_scan[0].id = WLAN_EID_SSID;
 
7088
                scan->direct_scan[0].len = priv->essid_len;
 
7089
                memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
 
7090
                direct_mask = 1;
 
7091
        } else
 
7092
                direct_mask = 0;
 
7093
 
 
7094
        /* We don't build a direct scan probe request; the uCode will do
 
7095
         * that based on the direct_mask added to each channel entry */
 
7096
        scan->tx_cmd.len = cpu_to_le16(
 
7097
                iwl4965_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
 
7098
                        IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
 
7099
        scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
 
7100
        scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
 
7101
        scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
 
7102
 
 
7103
        /* flags + rate selection */
 
7104
 
 
7105
        scan->tx_cmd.tx_flags |= cpu_to_le32(0x200);
 
7106
 
 
7107
        switch (priv->scan_bands) {
 
7108
        case 2:
 
7109
                scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
 
7110
                scan->tx_cmd.rate_n_flags =
 
7111
                                iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
 
7112
                                RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
 
7113
 
 
7114
                scan->good_CRC_th = 0;
 
7115
                phymode = MODE_IEEE80211G;
 
7116
                break;
 
7117
 
 
7118
        case 1:
 
7119
                scan->tx_cmd.rate_n_flags =
 
7120
                                iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
 
7121
                                RATE_MCS_ANT_B_MSK);
 
7122
                scan->good_CRC_th = IWL_GOOD_CRC_TH;
 
7123
                phymode = MODE_IEEE80211A;
 
7124
                break;
 
7125
 
 
7126
        default:
 
7127
                IWL_WARNING("Invalid scan band count\n");
 
7128
                goto done;
 
7129
        }
 
7130
 
 
7131
        /* select Rx chains */
 
7132
 
 
7133
        /* Force use of chains B and C (0x6) for scan Rx.
 
7134
         * Avoid A (0x1) because of its off-channel reception on A-band.
 
7135
         * MIMO is not used here, but value is required to make uCode happy. */
 
7136
        scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
 
7137
                        cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
 
7138
                        (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
 
7139
                        (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
 
7140
 
 
7141
        if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
 
7142
                scan->filter_flags = RXON_FILTER_PROMISC_MSK;
 
7143
 
 
7144
        if (direct_mask)
 
7145
                IWL_DEBUG_SCAN
 
7146
                    ("Initiating direct scan for %s.\n",
 
7147
                     iwl4965_escape_essid(priv->essid, priv->essid_len));
 
7148
        else
 
7149
                IWL_DEBUG_SCAN("Initiating indirect scan.\n");
 
7150
 
 
7151
        scan->channel_count =
 
7152
                iwl4965_get_channels_for_scan(
 
7153
                        priv, phymode, 1, /* active */
 
7154
                        direct_mask,
 
7155
                        (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
 
7156
 
 
7157
        cmd.len += le16_to_cpu(scan->tx_cmd.len) +
 
7158
            scan->channel_count * sizeof(struct iwl4965_scan_channel);
 
7159
        cmd.data = scan;
 
7160
        scan->len = cpu_to_le16(cmd.len);
 
7161
 
 
7162
        set_bit(STATUS_SCAN_HW, &priv->status);
 
7163
        rc = iwl4965_send_cmd_sync(priv, &cmd);
 
7164
        if (rc)
 
7165
                goto done;
 
7166
 
 
7167
        queue_delayed_work(priv->workqueue, &priv->scan_check,
 
7168
                           IWL_SCAN_CHECK_WATCHDOG);
 
7169
 
 
7170
        mutex_unlock(&priv->mutex);
 
7171
        return;
 
7172
 
 
7173
 done:
 
7174
        /* inform mac80211 scan aborted */
 
7175
        queue_work(priv->workqueue, &priv->scan_completed);
 
7176
        mutex_unlock(&priv->mutex);
 
7177
}
 
7178
 
 
7179
static void iwl4965_bg_up(struct work_struct *data)
 
7180
{
 
7181
        struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, up);
 
7182
 
 
7183
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 
7184
                return;
 
7185
 
 
7186
        mutex_lock(&priv->mutex);
 
7187
        __iwl4965_up(priv);
 
7188
        mutex_unlock(&priv->mutex);
 
7189
}
 
7190
 
 
7191
static void iwl4965_bg_restart(struct work_struct *data)
 
7192
{
 
7193
        struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv, restart);
 
7194
 
 
7195
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 
7196
                return;
 
7197
 
 
7198
        iwl4965_down(priv);
 
7199
        queue_work(priv->workqueue, &priv->up);
 
7200
}
 
7201
 
 
7202
static void iwl4965_bg_rx_replenish(struct work_struct *data)
 
7203
{
 
7204
        struct iwl4965_priv *priv =
 
7205
            container_of(data, struct iwl4965_priv, rx_replenish);
 
7206
 
 
7207
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 
7208
                return;
 
7209
 
 
7210
        mutex_lock(&priv->mutex);
 
7211
        iwl4965_rx_replenish(priv);
 
7212
        mutex_unlock(&priv->mutex);
 
7213
}
 
7214
 
 
7215
#define IWL_DELAY_NEXT_SCAN (HZ*2)
 
7216
 
 
7217
static void iwl4965_bg_post_associate(struct work_struct *data)
 
7218
{
 
7219
        struct iwl4965_priv *priv = container_of(data, struct iwl4965_priv,
 
7220
                                             post_associate.work);
 
7221
 
 
7222
        int rc = 0;
 
7223
        struct ieee80211_conf *conf = NULL;
 
7224
 
 
7225
        if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
 
7226
                IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
 
7227
                return;
 
7228
        }
 
7229
 
 
7230
        IWL_DEBUG_ASSOC("Associated as %d to: " MAC_FMT "\n",
 
7231
                        priv->assoc_id, MAC_ARG(priv->active_rxon.bssid_addr));
 
7232
 
 
7233
 
 
7234
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 
7235
                return;
 
7236
 
 
7237
        mutex_lock(&priv->mutex);
 
7238
 
 
7239
        if (!priv->interface_id || !priv->is_open) {
 
7240
                mutex_unlock(&priv->mutex);
 
7241
                return;
 
7242
        }
 
7243
        iwl4965_scan_cancel_timeout(priv, 200);
 
7244
 
 
7245
        conf = ieee80211_get_hw_conf(priv->hw);
 
7246
 
 
7247
        priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 
7248
        iwl4965_commit_rxon(priv);
 
7249
 
 
7250
        memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
 
7251
        iwl4965_setup_rxon_timing(priv);
 
7252
        rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
 
7253
                              sizeof(priv->rxon_timing), &priv->rxon_timing);
 
7254
        if (rc)
 
7255
                IWL_WARNING("REPLY_RXON_TIMING failed - "
 
7256
                            "Attempting to continue.\n");
 
7257
 
 
7258
        priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
 
7259
 
 
7260
#ifdef CONFIG_IWL4965_HT
 
7261
        if (priv->is_ht_enabled && priv->current_assoc_ht.is_ht)
 
7262
                iwl4965_set_rxon_ht(priv, &priv->current_assoc_ht);
 
7263
        else {
 
7264
                priv->active_rate_ht[0] = 0;
 
7265
                priv->active_rate_ht[1] = 0;
 
7266
                priv->current_channel_width = IWL_CHANNEL_WIDTH_20MHZ;
 
7267
        }
 
7268
#endif /* CONFIG_IWL4965_HT*/
 
7269
        iwl4965_set_rxon_chain(priv);
 
7270
        priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
 
7271
 
 
7272
        IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
 
7273
                        priv->assoc_id, priv->beacon_int);
 
7274
 
 
7275
        if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
 
7276
                priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
 
7277
        else
 
7278
                priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
 
7279
 
 
7280
        if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
 
7281
                if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
 
7282
                        priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
 
7283
                else
 
7284
                        priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
 
7285
 
 
7286
                if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
 
7287
                        priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
 
7288
 
 
7289
        }
 
7290
 
 
7291
        iwl4965_commit_rxon(priv);
 
7292
 
 
7293
        switch (priv->iw_mode) {
 
7294
        case IEEE80211_IF_TYPE_STA:
 
7295
                iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
 
7296
                break;
 
7297
 
 
7298
        case IEEE80211_IF_TYPE_IBSS:
 
7299
 
 
7300
                /* clear out the station table */
 
7301
                iwl4965_clear_stations_table(priv);
 
7302
 
 
7303
                iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
 
7304
                iwl4965_rxon_add_station(priv, priv->bssid, 0);
 
7305
                iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
 
7306
                iwl4965_send_beacon_cmd(priv);
 
7307
 
 
7308
                break;
 
7309
 
 
7310
        default:
 
7311
                IWL_ERROR("%s Should not be called in %d mode\n",
 
7312
                                __FUNCTION__, priv->iw_mode);
 
7313
                break;
 
7314
        }
 
7315
 
 
7316
        iwl4965_sequence_reset(priv);
 
7317
 
 
7318
#ifdef CONFIG_IWL4965_SENSITIVITY
 
7319
        /* Enable Rx differential gain and sensitivity calibrations */
 
7320
        iwl4965_chain_noise_reset(priv);
 
7321
        priv->start_calib = 1;
 
7322
#endif /* CONFIG_IWL4965_SENSITIVITY */
 
7323
 
 
7324
        if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
 
7325
                priv->assoc_station_added = 1;
 
7326
 
 
7327
#ifdef CONFIG_IWL4965_QOS
 
7328
        iwl4965_activate_qos(priv, 0);
 
7329
#endif /* CONFIG_IWL4965_QOS */
 
7330
        /* we have just associated, don't start scan too early */
 
7331
        priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
 
7332
        mutex_unlock(&priv->mutex);
 
7333
}
 
7334
 
 
7335
static void iwl4965_bg_abort_scan(struct work_struct *work)
 
7336
{
 
7337
        struct iwl4965_priv *priv = container_of(work, struct iwl4965_priv, abort_scan);
 
7338
 
 
7339
        if (!iwl4965_is_ready(priv))
 
7340
                return;
 
7341
 
 
7342
        mutex_lock(&priv->mutex);
 
7343
 
 
7344
        set_bit(STATUS_SCAN_ABORTING, &priv->status);
 
7345
        iwl4965_send_scan_abort(priv);
 
7346
 
 
7347
        mutex_unlock(&priv->mutex);
 
7348
}
 
7349
 
 
7350
static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
 
7351
 
 
7352
static void iwl4965_bg_scan_completed(struct work_struct *work)
 
7353
{
 
7354
        struct iwl4965_priv *priv =
 
7355
            container_of(work, struct iwl4965_priv, scan_completed);
 
7356
 
 
7357
        IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
 
7358
 
 
7359
        if (test_bit(STATUS_EXIT_PENDING, &priv->status))
 
7360
                return;
 
7361
 
 
7362
        if (test_bit(STATUS_CONF_PENDING, &priv->status))
 
7363
                iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
 
7364
 
 
7365
        iwlwifi_ieee80211_scan_completed(priv->hw);
 
7366
 
 
7367
        /* Since setting the TXPOWER may have been deferred while
 
7368
         * performing the scan, fire one off */
 
7369
        mutex_lock(&priv->mutex);
 
7370
        iwl4965_hw_reg_send_txpower(priv);
 
7371
        mutex_unlock(&priv->mutex);
 
7372
}
 
7373
 
 
7374
/*****************************************************************************
 
7375
 *
 
7376
 * mac80211 entry point functions
 
7377
 *
 
7378
 *****************************************************************************/
 
7379
 
 
7380
#define UCODE_READY_TIMEOUT     (2 * HZ)
 
7381
 
 
7382
static int iwl4965_mac_open(struct ieee80211_hw *hw)
 
7383
{
 
7384
        struct iwl4965_priv *priv = hw->priv;
 
7385
        int ret;
 
7386
 
 
7387
        IWL_DEBUG_MAC80211("enter\n");
 
7388
 
 
7389
        if (pci_enable_device(priv->pci_dev)) {
 
7390
                IWL_ERROR("Fail to pci_enable_device\n");
 
7391
                return -ENODEV;
 
7392
        }
 
7393
        pci_restore_state(priv->pci_dev);
 
7394
        pci_enable_msi(priv->pci_dev);
 
7395
 
 
7396
        ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
 
7397
                          DRV_NAME, priv);
 
7398
        if (ret) {
 
7399
                IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
 
7400
                goto out_disable_msi;
 
7401
        }
 
7402
 
 
7403
        /* we should be verifying the device is ready to be opened */
 
7404
        mutex_lock(&priv->mutex);
 
7405
 
 
7406
        memset(&priv->staging_rxon, 0, sizeof(struct iwl4965_rxon_cmd));
 
7407
        /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
 
7408
         * ucode filename and max sizes are card-specific. */
 
7409
 
 
7410
        if (!priv->ucode_code.len) {
 
7411
                ret = iwl4965_read_ucode(priv);
 
7412
                if (ret) {
 
7413
                        IWL_ERROR("Could not read microcode: %d\n", ret);
 
7414
                        mutex_unlock(&priv->mutex);
 
7415
                        goto out_release_irq;
 
7416
                }
 
7417
        }
 
7418
 
 
7419
        ret = __iwl4965_up(priv);
 
7420
 
 
7421
        mutex_unlock(&priv->mutex);
 
7422
 
 
7423
        if (ret)
 
7424
                goto out_release_irq;
 
7425
 
 
7426
        IWL_DEBUG_INFO("Start UP work done.\n");
 
7427
 
 
7428
        if (test_bit(STATUS_IN_SUSPEND, &priv->status))
 
7429
                return 0;
 
7430
 
 
7431
        /* Wait for START_ALIVE from ucode. Otherwise callbacks from
 
7432
         * mac80211 will not be run successfully. */
 
7433
        ret = wait_event_interruptible_timeout(priv->wait_command_queue,
 
7434
                        test_bit(STATUS_READY, &priv->status),
 
7435
                        UCODE_READY_TIMEOUT);
 
7436
        if (!ret) {
 
7437
                if (!test_bit(STATUS_READY, &priv->status)) {
 
7438
                        IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
 
7439
                                  jiffies_to_msecs(UCODE_READY_TIMEOUT));
 
7440
                        ret = -ETIMEDOUT;
 
7441
                        goto out_release_irq;
 
7442
                }
 
7443
        }
 
7444
 
 
7445
        priv->is_open = 1;
 
7446
        IWL_DEBUG_MAC80211("leave\n");
 
7447
        return 0;
 
7448
 
 
7449
out_release_irq:
 
7450
        free_irq(priv->pci_dev->irq, priv);
 
7451
out_disable_msi:
 
7452
        pci_disable_msi(priv->pci_dev);
 
7453
        pci_disable_device(priv->pci_dev);
 
7454
        priv->is_open = 0;
 
7455
        IWL_DEBUG_MAC80211("leave - failed\n");
 
7456
        return ret;
 
7457
}
 
7458
 
 
7459
static int iwl4965_mac_stop(struct ieee80211_hw *hw)
 
7460
{
 
7461
        struct iwl4965_priv *priv = hw->priv;
 
7462
 
 
7463
        IWL_DEBUG_MAC80211("enter\n");
 
7464
 
 
7465
        if (!priv->is_open) {
 
7466
                IWL_DEBUG_MAC80211("leave - skip\n");
 
7467
                return 0;
 
7468
        }
 
7469
 
 
7470
        priv->is_open = 0;
 
7471
 
 
7472
        if (iwl4965_is_ready_rf(priv)) {
 
7473
                /* stop mac, cancel any scan request and clear
 
7474
                 * RXON_FILTER_ASSOC_MSK BIT
 
7475
                 */
 
7476
                mutex_lock(&priv->mutex);
 
7477
                iwl4965_scan_cancel_timeout(priv, 100);
 
7478
                cancel_delayed_work(&priv->post_associate);
 
7479
                mutex_unlock(&priv->mutex);
 
7480
        }
 
7481
 
 
7482
        iwl4965_down(priv);
 
7483
 
 
7484
        flush_workqueue(priv->workqueue);
 
7485
        free_irq(priv->pci_dev->irq, priv);
 
7486
        pci_disable_msi(priv->pci_dev);
 
7487
        pci_save_state(priv->pci_dev);
 
7488
        pci_disable_device(priv->pci_dev);
 
7489
 
 
7490
        IWL_DEBUG_MAC80211("leave\n");
 
7491
        return 0;
 
7492
}
 
7493
 
 
7494
static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
 
7495
                      struct ieee80211_tx_control *ctl)
 
7496
{
 
7497
        struct iwl4965_priv *priv = hw->priv;
 
7498
 
 
7499
        IWL_DEBUG_MAC80211("enter\n");
 
7500
 
 
7501
        if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
 
7502
                IWL_DEBUG_MAC80211("leave - monitor\n");
 
7503
                return -1;
 
7504
        }
 
7505
 
 
7506
        IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
 
7507
                     ctl->tx_rate);
 
7508
 
 
7509
        if (iwl4965_tx_skb(priv, skb, ctl))
 
7510
                dev_kfree_skb_any(skb);
 
7511
 
 
7512
        IWL_DEBUG_MAC80211("leave\n");
 
7513
        return 0;
 
7514
}
 
7515
 
 
7516
static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
 
7517
                                 struct ieee80211_if_init_conf *conf)
 
7518
{
 
7519
        struct iwl4965_priv *priv = hw->priv;
 
7520
        unsigned long flags;
 
7521
 
 
7522
        IWL_DEBUG_MAC80211("enter: id %d, type %d\n", conf->if_id, conf->type);
 
7523
 
 
7524
        if (priv->interface_id) {
 
7525
                IWL_DEBUG_MAC80211("leave - interface_id != 0\n");
 
7526
                return 0;
 
7527
        }
 
7528
 
 
7529
        spin_lock_irqsave(&priv->lock, flags);
 
7530
        priv->interface_id = conf->if_id;
 
7531
 
 
7532
        spin_unlock_irqrestore(&priv->lock, flags);
 
7533
 
 
7534
        mutex_lock(&priv->mutex);
 
7535
 
 
7536
        if (conf->mac_addr) {
 
7537
                IWL_DEBUG_MAC80211("Set: " MAC_FMT "\n", MAC_ARG(conf->mac_addr));
 
7538
                memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
 
7539
        }
 
7540
 
 
7541
        if (iwl4965_is_ready(priv))
 
7542
                iwl4965_set_mode(priv, conf->type);
 
7543
 
 
7544
        mutex_unlock(&priv->mutex);
 
7545
 
 
7546
        IWL_DEBUG_MAC80211("leave\n");
 
7547
        return 0;
 
7548
}
 
7549
 
 
7550
/**
 
7551
 * iwl4965_mac_config - mac80211 config callback
 
7552
 *
 
7553
 * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
 
7554
 * be set inappropriately and the driver currently sets the hardware up to
 
7555
 * use it whenever needed.
 
7556
 */
 
7557
static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
 
7558
{
 
7559
        struct iwl4965_priv *priv = hw->priv;
 
7560
        const struct iwl4965_channel_info *ch_info;
 
7561
        unsigned long flags;
 
7562
        int ret = 0;
 
7563
 
 
7564
        mutex_lock(&priv->mutex);
 
7565
        IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
 
7566
 
 
7567
        if (!iwl4965_is_ready(priv)) {
 
7568
                IWL_DEBUG_MAC80211("leave - not ready\n");
 
7569
                ret = -EIO;
 
7570
                goto out;
 
7571
        }
 
7572
 
 
7573
        /* TODO: Figure out how to get ieee80211_local->sta_scanning w/ only
 
7574
         * what is exposed through include/ declarations */
 
7575
        if (unlikely(!iwl4965_param_disable_hw_scan &&
 
7576
                     test_bit(STATUS_SCANNING, &priv->status))) {
 
7577
                IWL_DEBUG_MAC80211("leave - scanning\n");
 
7578
                set_bit(STATUS_CONF_PENDING, &priv->status);
 
7579
                mutex_unlock(&priv->mutex);
 
7580
                return 0;
 
7581
        }
 
7582
 
 
7583
        spin_lock_irqsave(&priv->lock, flags);
 
7584
 
 
7585
        ch_info = iwl4965_get_channel_info(priv, conf->phymode, conf->channel);
 
7586
        if (!is_channel_valid(ch_info)) {
 
7587
                IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
 
7588
                               conf->channel, conf->phymode);
 
7589
                IWL_DEBUG_MAC80211("leave - invalid channel\n");
 
7590
                spin_unlock_irqrestore(&priv->lock, flags);
 
7591
                ret = -EINVAL;
 
7592
                goto out;
 
7593
        }
 
7594
 
 
7595
#ifdef CONFIG_IWL4965_HT
 
7596
        /* if we are switching fron ht to 2.4 clear flags
 
7597
         * from any ht related info since 2.4 does not
 
7598
         * support ht */
 
7599
        if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel)
 
7600
#ifdef IEEE80211_CONF_CHANNEL_SWITCH
 
7601
            && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
 
7602
#endif
 
7603
        )
 
7604
                priv->staging_rxon.flags = 0;
 
7605
#endif /* CONFIG_IWL4965_HT */
 
7606
 
 
7607
        iwl4965_set_rxon_channel(priv, conf->phymode, conf->channel);
 
7608
 
 
7609
        iwl4965_set_flags_for_phymode(priv, conf->phymode);
 
7610
 
 
7611
        /* The list of supported rates and rate mask can be different
 
7612
         * for each phymode; since the phymode may have changed, reset
 
7613
         * the rate mask to what mac80211 lists */
 
7614
        iwl4965_set_rate(priv);
 
7615
 
 
7616
        spin_unlock_irqrestore(&priv->lock, flags);
 
7617
 
 
7618
#ifdef IEEE80211_CONF_CHANNEL_SWITCH
 
7619
        if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
 
7620
                iwl4965_hw_channel_switch(priv, conf->channel);
 
7621
                goto out;
 
7622
        }
 
7623
#endif
 
7624
 
 
7625
        iwl4965_radio_kill_sw(priv, !conf->radio_enabled);
 
7626
 
 
7627
        if (!conf->radio_enabled) {
 
7628
                IWL_DEBUG_MAC80211("leave - radio disabled\n");
 
7629
                goto out;
 
7630
        }
 
7631
 
 
7632
        if (iwl4965_is_rfkill(priv)) {
 
7633
                IWL_DEBUG_MAC80211("leave - RF kill\n");
 
7634
                ret = -EIO;
 
7635
                goto out;
 
7636
        }
 
7637
 
 
7638
        iwl4965_set_rate(priv);
 
7639
 
 
7640
        if (memcmp(&priv->active_rxon,
 
7641
                   &priv->staging_rxon, sizeof(priv->staging_rxon)))
 
7642
                iwl4965_commit_rxon(priv);
 
7643
        else
 
7644
                IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
 
7645
 
 
7646
        IWL_DEBUG_MAC80211("leave\n");
 
7647
 
 
7648
out:
 
7649
        clear_bit(STATUS_CONF_PENDING, &priv->status);
 
7650
        mutex_unlock(&priv->mutex);
 
7651
        return ret;
 
7652
}
 
7653
 
 
7654
static void iwl4965_config_ap(struct iwl4965_priv *priv)
 
7655
{
 
7656
        int rc = 0;
 
7657
 
 
7658
        if (priv->status & STATUS_EXIT_PENDING)
 
7659
                return;
 
7660
 
 
7661
        /* The following should be done only at AP bring up */
 
7662
        if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
 
7663
 
 
7664
                /* RXON - unassoc (to set timing command) */
 
7665
                priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 
7666
                iwl4965_commit_rxon(priv);
 
7667
 
 
7668
                /* RXON Timing */
 
7669
                memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
 
7670
                iwl4965_setup_rxon_timing(priv);
 
7671
                rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON_TIMING,
 
7672
                                sizeof(priv->rxon_timing), &priv->rxon_timing);
 
7673
                if (rc)
 
7674
                        IWL_WARNING("REPLY_RXON_TIMING failed - "
 
7675
                                        "Attempting to continue.\n");
 
7676
 
 
7677
                iwl4965_set_rxon_chain(priv);
 
7678
 
 
7679
                /* FIXME: what should be the assoc_id for AP? */
 
7680
                priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
 
7681
                if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
 
7682
                        priv->staging_rxon.flags |=
 
7683
                                RXON_FLG_SHORT_PREAMBLE_MSK;
 
7684
                else
 
7685
                        priv->staging_rxon.flags &=
 
7686
                                ~RXON_FLG_SHORT_PREAMBLE_MSK;
 
7687
 
 
7688
                if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
 
7689
                        if (priv->assoc_capability &
 
7690
                                WLAN_CAPABILITY_SHORT_SLOT_TIME)
 
7691
                                priv->staging_rxon.flags |=
 
7692
                                        RXON_FLG_SHORT_SLOT_MSK;
 
7693
                        else
 
7694
                                priv->staging_rxon.flags &=
 
7695
                                        ~RXON_FLG_SHORT_SLOT_MSK;
 
7696
 
 
7697
                        if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
 
7698
                                priv->staging_rxon.flags &=
 
7699
                                        ~RXON_FLG_SHORT_SLOT_MSK;
 
7700
                }
 
7701
                /* restore RXON assoc */
 
7702
                priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
 
7703
                iwl4965_commit_rxon(priv);
 
7704
#ifdef CONFIG_IWL4965_QOS
 
7705
                iwl4965_activate_qos(priv, 1);
 
7706
#endif
 
7707
                iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0);
 
7708
        }
 
7709
        iwl4965_send_beacon_cmd(priv);
 
7710
 
 
7711
        /* FIXME - we need to add code here to detect a totally new
 
7712
         * configuration, reset the AP, unassoc, rxon timing, assoc,
 
7713
         * clear sta table, add BCAST sta... */
 
7714
}
 
7715
 
 
7716
static int iwl4965_mac_config_interface(struct ieee80211_hw *hw, int if_id,
 
7717
                                    struct ieee80211_if_conf *conf)
 
7718
{
 
7719
        struct iwl4965_priv *priv = hw->priv;
 
7720
        unsigned long flags;
 
7721
        int rc;
 
7722
 
 
7723
        if (conf == NULL)
 
7724
                return -EIO;
 
7725
 
 
7726
        if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
 
7727
            (!conf->beacon || !conf->ssid_len)) {
 
7728
                IWL_DEBUG_MAC80211
 
7729
                    ("Leaving in AP mode because HostAPD is not ready.\n");
 
7730
                return 0;
 
7731
        }
 
7732
 
 
7733
        if (!iwl4965_is_alive(priv))
 
7734
                return -EAGAIN;
 
7735
 
 
7736
        mutex_lock(&priv->mutex);
 
7737
 
 
7738
        IWL_DEBUG_MAC80211("enter: interface id %d\n", if_id);
 
7739
        if (conf->bssid)
 
7740
                IWL_DEBUG_MAC80211("bssid: " MAC_FMT "\n",
 
7741
                                   MAC_ARG(conf->bssid));
 
7742
 
 
7743
        if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
 
7744
            !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
 
7745
                IWL_DEBUG_MAC80211("leave - scanning\n");
 
7746
                mutex_unlock(&priv->mutex);
 
7747
                return 0;
 
7748
        }
 
7749
 
 
7750
        if (priv->interface_id != if_id) {
 
7751
                IWL_DEBUG_MAC80211("leave - interface_id != if_id\n");
 
7752
                mutex_unlock(&priv->mutex);
 
7753
                return 0;
 
7754
        }
 
7755
 
 
7756
        if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
 
7757
                if (!conf->bssid) {
 
7758
                        conf->bssid = priv->mac_addr;
 
7759
                        memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
 
7760
                        IWL_DEBUG_MAC80211("bssid was set to: " MAC_FMT "\n",
 
7761
                                           MAC_ARG(conf->bssid));
 
7762
                }
 
7763
                if (priv->ibss_beacon)
 
7764
                        dev_kfree_skb(priv->ibss_beacon);
 
7765
 
 
7766
                priv->ibss_beacon = conf->beacon;
 
7767
        }
 
7768
 
 
7769
        if (iwl4965_is_rfkill(priv))
 
7770
                goto done;
 
7771
 
 
7772
        if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
 
7773
            !is_multicast_ether_addr(conf->bssid)) {
 
7774
                /* If there is currently a HW scan going on in the background
 
7775
                 * then we need to cancel it else the RXON below will fail. */
 
7776
                if (iwl4965_scan_cancel_timeout(priv, 100)) {
 
7777
                        IWL_WARNING("Aborted scan still in progress "
 
7778
                                    "after 100ms\n");
 
7779
                        IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
 
7780
                        mutex_unlock(&priv->mutex);
 
7781
                        return -EAGAIN;
 
7782
                }
 
7783
                memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
 
7784
 
 
7785
                /* TODO: Audit driver for usage of these members and see
 
7786
                 * if mac80211 deprecates them (priv->bssid looks like it
 
7787
                 * shouldn't be there, but I haven't scanned the IBSS code
 
7788
                 * to verify) - jpk */
 
7789
                memcpy(priv->bssid, conf->bssid, ETH_ALEN);
 
7790
 
 
7791
                if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
 
7792
                        iwl4965_config_ap(priv);
 
7793
                else {
 
7794
                        rc = iwl4965_commit_rxon(priv);
 
7795
                        if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
 
7796
                                iwl4965_rxon_add_station(
 
7797
                                        priv, priv->active_rxon.bssid_addr, 1);
 
7798
                }
 
7799
 
 
7800
        } else {
 
7801
                iwl4965_scan_cancel_timeout(priv, 100);
 
7802
                priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 
7803
                iwl4965_commit_rxon(priv);
 
7804
        }
 
7805
 
 
7806
 done:
 
7807
        spin_lock_irqsave(&priv->lock, flags);
 
7808
        if (!conf->ssid_len)
 
7809
                memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
 
7810
        else
 
7811
                memcpy(priv->essid, conf->ssid, conf->ssid_len);
 
7812
 
 
7813
        priv->essid_len = conf->ssid_len;
 
7814
        spin_unlock_irqrestore(&priv->lock, flags);
 
7815
 
 
7816
        IWL_DEBUG_MAC80211("leave\n");
 
7817
        mutex_unlock(&priv->mutex);
 
7818
 
 
7819
        return 0;
 
7820
}
 
7821
 
 
7822
static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
 
7823
                                     struct ieee80211_if_init_conf *conf)
 
7824
{
 
7825
        struct iwl4965_priv *priv = hw->priv;
 
7826
 
 
7827
        IWL_DEBUG_MAC80211("enter\n");
 
7828
 
 
7829
        mutex_lock(&priv->mutex);
 
7830
 
 
7831
        if (iwl4965_is_ready_rf(priv)) {
 
7832
                iwl4965_scan_cancel_timeout(priv, 100);
 
7833
                cancel_delayed_work(&priv->post_associate);
 
7834
                priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 
7835
                iwl4965_commit_rxon(priv);
 
7836
        }
 
7837
        if (priv->interface_id == conf->if_id) {
 
7838
                priv->interface_id = 0;
 
7839
                memset(priv->bssid, 0, ETH_ALEN);
 
7840
                memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
 
7841
                priv->essid_len = 0;
 
7842
        }
 
7843
        mutex_unlock(&priv->mutex);
 
7844
 
 
7845
        IWL_DEBUG_MAC80211("leave\n");
 
7846
}
 
7847
 
 
7848
static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
 
7849
{
 
7850
        int rc = 0;
 
7851
        unsigned long flags;
 
7852
        struct iwl4965_priv *priv = hw->priv;
 
7853
 
 
7854
        IWL_DEBUG_MAC80211("enter\n");
 
7855
 
 
7856
        mutex_lock(&priv->mutex);
 
7857
        spin_lock_irqsave(&priv->lock, flags);
 
7858
 
 
7859
        if (!iwl4965_is_ready_rf(priv)) {
 
7860
                rc = -EIO;
 
7861
                IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
 
7862
                goto out_unlock;
 
7863
        }
 
7864
 
 
7865
        if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {    /* APs don't scan */
 
7866
                rc = -EIO;
 
7867
                IWL_ERROR("ERROR: APs don't scan\n");
 
7868
                goto out_unlock;
 
7869
        }
 
7870
 
 
7871
        /* we don't schedule scan within next_scan_jiffies period */
 
7872
        if (priv->next_scan_jiffies &&
 
7873
                        time_after(priv->next_scan_jiffies, jiffies)) {
 
7874
                rc = -EAGAIN;
 
7875
                goto out_unlock;
 
7876
        }
 
7877
        /* if we just finished scan ask for delay */
 
7878
        if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
 
7879
                                IWL_DELAY_NEXT_SCAN, jiffies)) {
 
7880
                rc = -EAGAIN;
 
7881
                goto out_unlock;
 
7882
        }
 
7883
        if (len) {
 
7884
                IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
 
7885
                               iwl4965_escape_essid(ssid, len), (int)len);
 
7886
 
 
7887
                priv->one_direct_scan = 1;
 
7888
                priv->direct_ssid_len = (u8)
 
7889
                    min((u8) len, (u8) IW_ESSID_MAX_SIZE);
 
7890
                memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
 
7891
        } else
 
7892
                priv->one_direct_scan = 0;
 
7893
 
 
7894
        rc = iwl4965_scan_initiate(priv);
 
7895
 
 
7896
        IWL_DEBUG_MAC80211("leave\n");
 
7897
 
 
7898
out_unlock:
 
7899
        spin_unlock_irqrestore(&priv->lock, flags);
 
7900
        mutex_unlock(&priv->mutex);
 
7901
 
 
7902
        return rc;
 
7903
}
 
7904
 
 
7905
static int iwl4965_mac_set_key(struct ieee80211_hw *hw, set_key_cmd cmd, u8 *addr,
 
7906
                           struct ieee80211_key_conf *key, int aid)
 
7907
{
 
7908
        struct iwl4965_priv *priv = hw->priv;
 
7909
        int rc = 0;
 
7910
        u8 sta_id;
 
7911
 
 
7912
        IWL_DEBUG_MAC80211("enter\n");
 
7913
 
 
7914
        if (!iwl4965_param_hwcrypto) {
 
7915
                IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
 
7916
                return -EOPNOTSUPP;
 
7917
        }
 
7918
 
 
7919
        sta_id = iwl4965_hw_find_station(priv, addr);
 
7920
        if (sta_id == IWL_INVALID_STATION) {
 
7921
                IWL_DEBUG_MAC80211("leave - " MAC_FMT " not in station map.\n",
 
7922
                                   MAC_ARG(addr));
 
7923
                return -EINVAL;
 
7924
        }
 
7925
 
 
7926
        mutex_lock(&priv->mutex);
 
7927
 
 
7928
        iwl4965_scan_cancel_timeout(priv, 100);
 
7929
 
 
7930
        switch (cmd) {
 
7931
        case  SET_KEY:
 
7932
                rc = iwl4965_update_sta_key_info(priv, key, sta_id);
 
7933
                if (!rc) {
 
7934
                        iwl4965_set_rxon_hwcrypto(priv, 1);
 
7935
                        iwl4965_commit_rxon(priv);
 
7936
                        key->hw_key_idx = sta_id;
 
7937
                        IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
 
7938
                }
 
7939
                break;
 
7940
        case DISABLE_KEY:
 
7941
                rc = iwl4965_clear_sta_key_info(priv, sta_id);
 
7942
                if (!rc) {
 
7943
                        iwl4965_set_rxon_hwcrypto(priv, 0);
 
7944
                        iwl4965_commit_rxon(priv);
 
7945
                        IWL_DEBUG_MAC80211("disable hwcrypto key\n");
 
7946
                }
 
7947
                break;
 
7948
        default:
 
7949
                rc = -EINVAL;
 
7950
        }
 
7951
 
 
7952
        IWL_DEBUG_MAC80211("leave\n");
 
7953
        mutex_unlock(&priv->mutex);
 
7954
 
 
7955
        return rc;
 
7956
}
 
7957
 
 
7958
static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, int queue,
 
7959
                           const struct ieee80211_tx_queue_params *params)
 
7960
{
 
7961
        struct iwl4965_priv *priv = hw->priv;
 
7962
#ifdef CONFIG_IWL4965_QOS
 
7963
        unsigned long flags;
 
7964
        int q;
 
7965
#endif /* CONFIG_IWL4965_QOS */
 
7966
 
 
7967
        IWL_DEBUG_MAC80211("enter\n");
 
7968
 
 
7969
        if (!iwl4965_is_ready_rf(priv)) {
 
7970
                IWL_DEBUG_MAC80211("leave - RF not ready\n");
 
7971
                return -EIO;
 
7972
        }
 
7973
 
 
7974
        if (queue >= AC_NUM) {
 
7975
                IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
 
7976
                return 0;
 
7977
        }
 
7978
 
 
7979
#ifdef CONFIG_IWL4965_QOS
 
7980
        if (!priv->qos_data.qos_enable) {
 
7981
                priv->qos_data.qos_active = 0;
 
7982
                IWL_DEBUG_MAC80211("leave - qos not enabled\n");
 
7983
                return 0;
 
7984
        }
 
7985
        q = AC_NUM - 1 - queue;
 
7986
 
 
7987
        spin_lock_irqsave(&priv->lock, flags);
 
7988
 
 
7989
        priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
 
7990
        priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
 
7991
        priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
 
7992
        priv->qos_data.def_qos_parm.ac[q].edca_txop =
 
7993
                        cpu_to_le16((params->burst_time * 100));
 
7994
 
 
7995
        priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
 
7996
        priv->qos_data.qos_active = 1;
 
7997
 
 
7998
        spin_unlock_irqrestore(&priv->lock, flags);
 
7999
 
 
8000
        mutex_lock(&priv->mutex);
 
8001
        if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
 
8002
                iwl4965_activate_qos(priv, 1);
 
8003
        else if (priv->assoc_id && iwl4965_is_associated(priv))
 
8004
                iwl4965_activate_qos(priv, 0);
 
8005
 
 
8006
        mutex_unlock(&priv->mutex);
 
8007
 
 
8008
#endif /*CONFIG_IWL4965_QOS */
 
8009
 
 
8010
        IWL_DEBUG_MAC80211("leave\n");
 
8011
        return 0;
 
8012
}
 
8013
 
 
8014
static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
 
8015
                                struct ieee80211_tx_queue_stats *stats)
 
8016
{
 
8017
        struct iwl4965_priv *priv = hw->priv;
 
8018
        int i, avail;
 
8019
        struct iwl4965_tx_queue *txq;
 
8020
        struct iwl4965_queue *q;
 
8021
        unsigned long flags;
 
8022
 
 
8023
        IWL_DEBUG_MAC80211("enter\n");
 
8024
 
 
8025
        if (!iwl4965_is_ready_rf(priv)) {
 
8026
                IWL_DEBUG_MAC80211("leave - RF not ready\n");
 
8027
                return -EIO;
 
8028
        }
 
8029
 
 
8030
        spin_lock_irqsave(&priv->lock, flags);
 
8031
 
 
8032
        for (i = 0; i < AC_NUM; i++) {
 
8033
                txq = &priv->txq[i];
 
8034
                q = &txq->q;
 
8035
                avail = iwl4965_queue_space(q);
 
8036
 
 
8037
                stats->data[i].len = q->n_window - avail;
 
8038
                stats->data[i].limit = q->n_window - q->high_mark;
 
8039
                stats->data[i].count = q->n_window;
 
8040
 
 
8041
        }
 
8042
        spin_unlock_irqrestore(&priv->lock, flags);
 
8043
 
 
8044
        IWL_DEBUG_MAC80211("leave\n");
 
8045
 
 
8046
        return 0;
 
8047
}
 
8048
 
 
8049
static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
 
8050
                             struct ieee80211_low_level_stats *stats)
 
8051
{
 
8052
        IWL_DEBUG_MAC80211("enter\n");
 
8053
        IWL_DEBUG_MAC80211("leave\n");
 
8054
 
 
8055
        return 0;
 
8056
}
 
8057
 
 
8058
static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
 
8059
{
 
8060
        IWL_DEBUG_MAC80211("enter\n");
 
8061
        IWL_DEBUG_MAC80211("leave\n");
 
8062
 
 
8063
        return 0;
 
8064
}
 
8065
 
 
8066
static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
 
8067
{
 
8068
        struct iwl4965_priv *priv = hw->priv;
 
8069
        unsigned long flags;
 
8070
 
 
8071
        mutex_lock(&priv->mutex);
 
8072
        IWL_DEBUG_MAC80211("enter\n");
 
8073
 
 
8074
        priv->lq_mngr.lq_ready = 0;
 
8075
#ifdef CONFIG_IWL4965_HT
 
8076
        spin_lock_irqsave(&priv->lock, flags);
 
8077
        memset(&priv->current_assoc_ht, 0, sizeof(struct sta_ht_info));
 
8078
        spin_unlock_irqrestore(&priv->lock, flags);
 
8079
#ifdef CONFIG_IWL4965_HT_AGG
 
8080
/*      if (priv->lq_mngr.agg_ctrl.granted_ba)
 
8081
                iwl4965_turn_off_agg(priv, TID_ALL_SPECIFIED);*/
 
8082
 
 
8083
        memset(&(priv->lq_mngr.agg_ctrl), 0, sizeof(struct iwl4965_agg_control));
 
8084
        priv->lq_mngr.agg_ctrl.tid_traffic_load_threshold = 10;
 
8085
        priv->lq_mngr.agg_ctrl.ba_timeout = 5000;
 
8086
        priv->lq_mngr.agg_ctrl.auto_agg = 1;
 
8087
 
 
8088
        if (priv->lq_mngr.agg_ctrl.auto_agg)
 
8089
                priv->lq_mngr.agg_ctrl.requested_ba = TID_ALL_ENABLED;
 
8090
#endif /*CONFIG_IWL4965_HT_AGG */
 
8091
#endif /* CONFIG_IWL4965_HT */
 
8092
 
 
8093
#ifdef CONFIG_IWL4965_QOS
 
8094
        iwl4965_reset_qos(priv);
 
8095
#endif
 
8096
 
 
8097
        cancel_delayed_work(&priv->post_associate);
 
8098
 
 
8099
        spin_lock_irqsave(&priv->lock, flags);
 
8100
        priv->assoc_id = 0;
 
8101
        priv->assoc_capability = 0;
 
8102
        priv->call_post_assoc_from_beacon = 0;
 
8103
        priv->assoc_station_added = 0;
 
8104
 
 
8105
        /* new association get rid of ibss beacon skb */
 
8106
        if (priv->ibss_beacon)
 
8107
                dev_kfree_skb(priv->ibss_beacon);
 
8108
 
 
8109
        priv->ibss_beacon = NULL;
 
8110
 
 
8111
        priv->beacon_int = priv->hw->conf.beacon_int;
 
8112
        priv->timestamp1 = 0;
 
8113
        priv->timestamp0 = 0;
 
8114
        if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
 
8115
                priv->beacon_int = 0;
 
8116
 
 
8117
        spin_unlock_irqrestore(&priv->lock, flags);
 
8118
 
 
8119
        if (!iwl4965_is_ready_rf(priv)) {
 
8120
                IWL_DEBUG_MAC80211("leave - not ready\n");
 
8121
                mutex_unlock(&priv->mutex);
 
8122
                return;
 
8123
        }
 
8124
 
 
8125
        /* we are restarting association process
 
8126
         * clear RXON_FILTER_ASSOC_MSK bit
 
8127
         */
 
8128
        if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
 
8129
                iwl4965_scan_cancel_timeout(priv, 100);
 
8130
                priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
 
8131
                iwl4965_commit_rxon(priv);
 
8132
        }
 
8133
 
 
8134
        /* Per mac80211.h: This is only used in IBSS mode... */
 
8135
        if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
 
8136
 
 
8137
                IWL_DEBUG_MAC80211("leave - not in IBSS\n");
 
8138
                mutex_unlock(&priv->mutex);
 
8139
                return;
 
8140
        }
 
8141
 
 
8142
        priv->only_active_channel = 0;
 
8143
 
 
8144
        iwl4965_set_rate(priv);
 
8145
 
 
8146
        mutex_unlock(&priv->mutex);
 
8147
 
 
8148
        IWL_DEBUG_MAC80211("leave\n");
 
8149
 
 
8150
}
 
8151
 
 
8152
static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
 
8153
                                 struct ieee80211_tx_control *control)
 
8154
{
 
8155
        struct iwl4965_priv *priv = hw->priv;
 
8156
        unsigned long flags;
 
8157
 
 
8158
        mutex_lock(&priv->mutex);
 
8159
        IWL_DEBUG_MAC80211("enter\n");
 
8160
 
 
8161
        if (!iwl4965_is_ready_rf(priv)) {
 
8162
                IWL_DEBUG_MAC80211("leave - RF not ready\n");
 
8163
                mutex_unlock(&priv->mutex);
 
8164
                return -EIO;
 
8165
        }
 
8166
 
 
8167
        if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
 
8168
                IWL_DEBUG_MAC80211("leave - not IBSS\n");
 
8169
                mutex_unlock(&priv->mutex);
 
8170
                return -EIO;
 
8171
        }
 
8172
 
 
8173
        spin_lock_irqsave(&priv->lock, flags);
 
8174
 
 
8175
        if (priv->ibss_beacon)
 
8176
                dev_kfree_skb(priv->ibss_beacon);
 
8177
 
 
8178
        priv->ibss_beacon = skb;
 
8179
 
 
8180
        priv->assoc_id = 0;
 
8181
 
 
8182
        IWL_DEBUG_MAC80211("leave\n");
 
8183
        spin_unlock_irqrestore(&priv->lock, flags);
 
8184
 
 
8185
#ifdef CONFIG_IWL4965_QOS
 
8186
        iwl4965_reset_qos(priv);
 
8187
#endif
 
8188
 
 
8189
        queue_work(priv->workqueue, &priv->post_associate.work);
 
8190
 
 
8191
        mutex_unlock(&priv->mutex);
 
8192
 
 
8193
        return 0;
 
8194
}
 
8195
 
 
8196
#ifdef CONFIG_IWL4965_HT
 
8197
union ht_cap_info {
 
8198
        struct {
 
8199
                u16 advanced_coding_cap         :1;
 
8200
                u16 supported_chan_width_set    :1;
 
8201
                u16 mimo_power_save_mode        :2;
 
8202
                u16 green_field                 :1;
 
8203
                u16 short_GI20                  :1;
 
8204
                u16 short_GI40                  :1;
 
8205
                u16 tx_stbc                     :1;
 
8206
                u16 rx_stbc                     :1;
 
8207
                u16 beam_forming                :1;
 
8208
                u16 delayed_ba                  :1;
 
8209
                u16 maximal_amsdu_size          :1;
 
8210
                u16 cck_mode_at_40MHz           :1;
 
8211
                u16 psmp_support                :1;
 
8212
                u16 stbc_ctrl_frame_support     :1;
 
8213
                u16 sig_txop_protection_support :1;
 
8214
        };
 
8215
        u16 val;
 
8216
} __attribute__ ((packed));
 
8217
 
 
8218
union ht_param_info{
 
8219
        struct {
 
8220
                u8 max_rx_ampdu_factor  :2;
 
8221
                u8 mpdu_density         :3;
 
8222
                u8 reserved             :3;
 
8223
        };
 
8224
        u8 val;
 
8225
} __attribute__ ((packed));
 
8226
 
 
8227
union ht_exra_param_info {
 
8228
        struct {
 
8229
                u8 ext_chan_offset              :2;
 
8230
                u8 tx_chan_width                :1;
 
8231
                u8 rifs_mode                    :1;
 
8232
                u8 controlled_access_only       :1;
 
8233
                u8 service_interval_granularity :3;
 
8234
        };
 
8235
        u8 val;
 
8236
} __attribute__ ((packed));
 
8237
 
 
8238
union ht_operation_mode{
 
8239
        struct {
 
8240
                u16 op_mode     :2;
 
8241
                u16 non_GF      :1;
 
8242
                u16 reserved    :13;
 
8243
        };
 
8244
        u16 val;
 
8245
} __attribute__ ((packed));
 
8246
 
 
8247
 
 
8248
static int sta_ht_info_init(struct ieee80211_ht_capability *ht_cap,
 
8249
                            struct ieee80211_ht_additional_info *ht_extra,
 
8250
                            struct sta_ht_info *ht_info_ap,
 
8251
                            struct sta_ht_info *ht_info)
 
8252
{
 
8253
        union ht_cap_info cap;
 
8254
        union ht_operation_mode op_mode;
 
8255
        union ht_param_info param_info;
 
8256
        union ht_exra_param_info extra_param_info;
 
8257
 
 
8258
        IWL_DEBUG_MAC80211("enter: \n");
 
8259
 
 
8260
        if (!ht_info) {
 
8261
                IWL_DEBUG_MAC80211("leave: ht_info is NULL\n");
 
8262
                return -1;
 
8263
        }
 
8264
 
 
8265
        if (ht_cap) {
 
8266
                cap.val = (u16) le16_to_cpu(ht_cap->capabilities_info);
 
8267
                param_info.val = ht_cap->mac_ht_params_info;
 
8268
                ht_info->is_ht = 1;
 
8269
                if (cap.short_GI20)
 
8270
                        ht_info->sgf |= 0x1;
 
8271
                if (cap.short_GI40)
 
8272
                        ht_info->sgf |= 0x2;
 
8273
                ht_info->is_green_field = cap.green_field;
 
8274
                ht_info->max_amsdu_size = cap.maximal_amsdu_size;
 
8275
                ht_info->supported_chan_width = cap.supported_chan_width_set;
 
8276
                ht_info->tx_mimo_ps_mode = cap.mimo_power_save_mode;
 
8277
                memcpy(ht_info->supp_rates, ht_cap->supported_mcs_set, 16);
 
8278
 
 
8279
                ht_info->ampdu_factor = param_info.max_rx_ampdu_factor;
 
8280
                ht_info->mpdu_density = param_info.mpdu_density;
 
8281
 
 
8282
                IWL_DEBUG_MAC80211("SISO mask 0x%X MIMO mask 0x%X \n",
 
8283
                                    ht_cap->supported_mcs_set[0],
 
8284
                                    ht_cap->supported_mcs_set[1]);
 
8285
 
 
8286
                if (ht_info_ap) {
 
8287
                        ht_info->control_channel = ht_info_ap->control_channel;
 
8288
                        ht_info->extension_chan_offset =
 
8289
                                ht_info_ap->extension_chan_offset;
 
8290
                        ht_info->tx_chan_width = ht_info_ap->tx_chan_width;
 
8291
                        ht_info->operating_mode = ht_info_ap->operating_mode;
 
8292
                }
 
8293
 
 
8294
                if (ht_extra) {
 
8295
                        extra_param_info.val = ht_extra->ht_param;
 
8296
                        ht_info->control_channel = ht_extra->control_chan;
 
8297
                        ht_info->extension_chan_offset =
 
8298
                            extra_param_info.ext_chan_offset;
 
8299
                        ht_info->tx_chan_width = extra_param_info.tx_chan_width;
 
8300
                        op_mode.val = (u16)
 
8301
                            le16_to_cpu(ht_extra->operation_mode);
 
8302
                        ht_info->operating_mode = op_mode.op_mode;
 
8303
                        IWL_DEBUG_MAC80211("control channel %d\n",
 
8304
                                            ht_extra->control_chan);
 
8305
                }
 
8306
        } else
 
8307
                ht_info->is_ht = 0;
 
8308
 
 
8309
        IWL_DEBUG_MAC80211("leave\n");
 
8310
        return 0;
 
8311
}
 
8312
 
 
8313
static int iwl4965_mac_conf_ht(struct ieee80211_hw *hw,
 
8314
                           struct ieee80211_ht_capability *ht_cap,
 
8315
                           struct ieee80211_ht_additional_info *ht_extra)
 
8316
{
 
8317
        struct iwl4965_priv *priv = hw->priv;
 
8318
        int rs;
 
8319
 
 
8320
        IWL_DEBUG_MAC80211("enter: \n");
 
8321
 
 
8322
        rs = sta_ht_info_init(ht_cap, ht_extra, NULL, &priv->current_assoc_ht);
 
8323
        iwl4965_set_rxon_chain(priv);
 
8324
 
 
8325
        if (priv && priv->assoc_id &&
 
8326
            (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
 
8327
                unsigned long flags;
 
8328
 
 
8329
                spin_lock_irqsave(&priv->lock, flags);
 
8330
                if (priv->beacon_int)
 
8331
                        queue_work(priv->workqueue, &priv->post_associate.work);
 
8332
                else
 
8333
                        priv->call_post_assoc_from_beacon = 1;
 
8334
                spin_unlock_irqrestore(&priv->lock, flags);
 
8335
        }
 
8336
 
 
8337
        IWL_DEBUG_MAC80211("leave: control channel %d\n",
 
8338
                        ht_extra->control_chan);
 
8339
        return rs;
 
8340
 
 
8341
}
 
8342
 
 
8343
static void iwl4965_set_ht_capab(struct ieee80211_hw *hw,
 
8344
                             struct ieee80211_ht_capability *ht_cap,
 
8345
                             u8 use_wide_chan)
 
8346
{
 
8347
        union ht_cap_info cap;
 
8348
        union ht_param_info param_info;
 
8349
 
 
8350
        memset(&cap, 0, sizeof(union ht_cap_info));
 
8351
        memset(&param_info, 0, sizeof(union ht_param_info));
 
8352
 
 
8353
        cap.maximal_amsdu_size = HT_IE_MAX_AMSDU_SIZE_4K;
 
8354
        cap.green_field = 1;
 
8355
        cap.short_GI20 = 1;
 
8356
        cap.short_GI40 = 1;
 
8357
        cap.supported_chan_width_set = use_wide_chan;
 
8358
        cap.mimo_power_save_mode = 0x3;
 
8359
 
 
8360
        param_info.max_rx_ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
 
8361
        param_info.mpdu_density = CFG_HT_MPDU_DENSITY_DEF;
 
8362
        ht_cap->capabilities_info = (__le16) cpu_to_le16(cap.val);
 
8363
        ht_cap->mac_ht_params_info = (u8) param_info.val;
 
8364
 
 
8365
        ht_cap->supported_mcs_set[0] = 0xff;
 
8366
        ht_cap->supported_mcs_set[1] = 0xff;
 
8367
        ht_cap->supported_mcs_set[4] =
 
8368
            (cap.supported_chan_width_set) ? 0x1: 0x0;
 
8369
}
 
8370
 
 
8371
static void iwl4965_mac_get_ht_capab(struct ieee80211_hw *hw,
 
8372
                                 struct ieee80211_ht_capability *ht_cap)
 
8373
{
 
8374
        u8 use_wide_channel = 1;
 
8375
        struct iwl4965_priv *priv = hw->priv;
 
8376
 
 
8377
        IWL_DEBUG_MAC80211("enter: \n");
 
8378
        if (priv->channel_width != IWL_CHANNEL_WIDTH_40MHZ)
 
8379
                use_wide_channel = 0;
 
8380
 
 
8381
        /* no fat tx allowed on 2.4GHZ */
 
8382
        if (priv->phymode != MODE_IEEE80211A)
 
8383
                use_wide_channel = 0;
 
8384
 
 
8385
        iwl4965_set_ht_capab(hw, ht_cap, use_wide_channel);
 
8386
        IWL_DEBUG_MAC80211("leave: \n");
 
8387
}
 
8388
#endif /*CONFIG_IWL4965_HT*/
 
8389
 
 
8390
/*****************************************************************************
 
8391
 *
 
8392
 * sysfs attributes
 
8393
 *
 
8394
 *****************************************************************************/
 
8395
 
 
8396
#ifdef CONFIG_IWL4965_DEBUG
 
8397
 
 
8398
/*
 
8399
 * The following adds a new attribute to the sysfs representation
 
8400
 * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
 
8401
 * used for controlling the debug level.
 
8402
 *
 
8403
 * See the level definitions in iwl for details.
 
8404
 */
 
8405
 
 
8406
static ssize_t show_debug_level(struct device_driver *d, char *buf)
 
8407
{
 
8408
        return sprintf(buf, "0x%08X\n", iwl4965_debug_level);
 
8409
}
 
8410
static ssize_t store_debug_level(struct device_driver *d,
 
8411
                                 const char *buf, size_t count)
 
8412
{
 
8413
        char *p = (char *)buf;
 
8414
        u32 val;
 
8415
 
 
8416
        val = simple_strtoul(p, &p, 0);
 
8417
        if (p == buf)
 
8418
                printk(KERN_INFO DRV_NAME
 
8419
                       ": %s is not in hex or decimal form.\n", buf);
 
8420
        else
 
8421
                iwl4965_debug_level = val;
 
8422
 
 
8423
        return strnlen(buf, count);
 
8424
}
 
8425
 
 
8426
static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
 
8427
                   show_debug_level, store_debug_level);
 
8428
 
 
8429
#endif /* CONFIG_IWL4965_DEBUG */
 
8430
 
 
8431
static ssize_t show_rf_kill(struct device *d,
 
8432
                            struct device_attribute *attr, char *buf)
 
8433
{
 
8434
        /*
 
8435
         * 0 - RF kill not enabled
 
8436
         * 1 - SW based RF kill active (sysfs)
 
8437
         * 2 - HW based RF kill active
 
8438
         * 3 - Both HW and SW based RF kill active
 
8439
         */
 
8440
        struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
 
8441
        int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
 
8442
                  (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
 
8443
 
 
8444
        return sprintf(buf, "%i\n", val);
 
8445
}
 
8446
 
 
8447
static ssize_t store_rf_kill(struct device *d,
 
8448
                             struct device_attribute *attr,
 
8449
                             const char *buf, size_t count)
 
8450
{
 
8451
        struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
 
8452
 
 
8453
        mutex_lock(&priv->mutex);
 
8454
        iwl4965_radio_kill_sw(priv, buf[0] == '1');
 
8455
        mutex_unlock(&priv->mutex);
 
8456
 
 
8457
        return count;
 
8458
}
 
8459
 
 
8460
static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
 
8461
 
 
8462
static ssize_t show_temperature(struct device *d,
 
8463
                                struct device_attribute *attr, char *buf)
 
8464
{
 
8465
        struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
 
8466
 
 
8467
        if (!iwl4965_is_alive(priv))
 
8468
                return -EAGAIN;
 
8469
 
 
8470
        return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
 
8471
}
 
8472
 
 
8473
static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
 
8474
 
 
8475
static ssize_t show_rs_window(struct device *d,
 
8476
                              struct device_attribute *attr,
 
8477
                              char *buf)
 
8478
{
 
8479
        struct iwl4965_priv *priv = d->driver_data;
 
8480
        return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
 
8481
}
 
8482
static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
 
8483
 
 
8484
static ssize_t show_tx_power(struct device *d,
 
8485
                             struct device_attribute *attr, char *buf)
 
8486
{
 
8487
        struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
 
8488
        return sprintf(buf, "%d\n", priv->user_txpower_limit);
 
8489
}
 
8490
 
 
8491
static ssize_t store_tx_power(struct device *d,
 
8492
                              struct device_attribute *attr,
 
8493
                              const char *buf, size_t count)
 
8494
{
 
8495
        struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
 
8496
        char *p = (char *)buf;
 
8497
        u32 val;
 
8498
 
 
8499
        val = simple_strtoul(p, &p, 10);
 
8500
        if (p == buf)
 
8501
                printk(KERN_INFO DRV_NAME
 
8502
                       ": %s is not in decimal form.\n", buf);
 
8503
        else
 
8504
                iwl4965_hw_reg_set_txpower(priv, val);
 
8505
 
 
8506
        return count;
 
8507
}
 
8508
 
 
8509
static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
 
8510
 
 
8511
static ssize_t show_flags(struct device *d,
 
8512
                          struct device_attribute *attr, char *buf)
 
8513
{
 
8514
        struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
 
8515
 
 
8516
        return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
 
8517
}
 
8518
 
 
8519
static ssize_t store_flags(struct device *d,
 
8520
                           struct device_attribute *attr,
 
8521
                           const char *buf, size_t count)
 
8522
{
 
8523
        struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
 
8524
        u32 flags = simple_strtoul(buf, NULL, 0);
 
8525
 
 
8526
        mutex_lock(&priv->mutex);
 
8527
        if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
 
8528
                /* Cancel any currently running scans... */
 
8529
                if (iwl4965_scan_cancel_timeout(priv, 100))
 
8530
                        IWL_WARNING("Could not cancel scan.\n");
 
8531
                else {
 
8532
                        IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
 
8533
                                       flags);
 
8534
                        priv->staging_rxon.flags = cpu_to_le32(flags);
 
8535
                        iwl4965_commit_rxon(priv);
 
8536
                }
 
8537
        }
 
8538
        mutex_unlock(&priv->mutex);
 
8539
 
 
8540
        return count;
 
8541
}
 
8542
 
 
8543
static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
 
8544
 
 
8545
static ssize_t show_filter_flags(struct device *d,
 
8546
                                 struct device_attribute *attr, char *buf)
 
8547
{
 
8548
        struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
 
8549
 
 
8550
        return sprintf(buf, "0x%04X\n",
 
8551
                le32_to_cpu(priv->active_rxon.filter_flags));
 
8552
}
 
8553
 
 
8554
static ssize_t store_filter_flags(struct device *d,
 
8555
                                  struct device_attribute *attr,
 
8556
                                  const char *buf, size_t count)
 
8557
{
 
8558
        struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
 
8559
        u32 filter_flags = simple_strtoul(buf, NULL, 0);
 
8560
 
 
8561
        mutex_lock(&priv->mutex);
 
8562
        if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
 
8563
                /* Cancel any currently running scans... */
 
8564
                if (iwl4965_scan_cancel_timeout(priv, 100))
 
8565
                        IWL_WARNING("Could not cancel scan.\n");
 
8566
                else {
 
8567
                        IWL_DEBUG_INFO("Committing rxon.filter_flags = "
 
8568
                                       "0x%04X\n", filter_flags);
 
8569
                        priv->staging_rxon.filter_flags =
 
8570
                                cpu_to_le32(filter_flags);
 
8571
                        iwl4965_commit_rxon(priv);
 
8572
                }
 
8573
        }
 
8574
        mutex_unlock(&priv->mutex);
 
8575
 
 
8576
        return count;
 
8577
}
 
8578
 
 
8579
static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
 
8580
                   store_filter_flags);
 
8581
 
 
8582
static ssize_t show_tune(struct device *d,
 
8583
                         struct device_attribute *attr, char *buf)
 
8584
{
 
8585
        struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
 
8586
 
 
8587
        return sprintf(buf, "0x%04X\n",
 
8588
                       (priv->phymode << 8) |
 
8589
                        le16_to_cpu(priv->active_rxon.channel));
 
8590
}
 
8591
 
 
8592
static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv, u8 phymode);
 
8593
 
 
8594
static ssize_t store_tune(struct device *d,
 
8595
                          struct device_attribute *attr,
 
8596
                          const char *buf, size_t count)
 
8597
{
 
8598
        struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
 
8599
        char *p = (char *)buf;
 
8600
        u16 tune = simple_strtoul(p, &p, 0);
 
8601
        u8 phymode = (tune >> 8) & 0xff;
 
8602
        u16 channel = tune & 0xff;
 
8603
 
 
8604
        IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
 
8605
 
 
8606
        mutex_lock(&priv->mutex);
 
8607
        if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
 
8608
            (priv->phymode != phymode)) {
 
8609
                const struct iwl4965_channel_info *ch_info;
 
8610
 
 
8611
                ch_info = iwl4965_get_channel_info(priv, phymode, channel);
 
8612
                if (!ch_info) {
 
8613
                        IWL_WARNING("Requested invalid phymode/channel "
 
8614
                                    "combination: %d %d\n", phymode, channel);
 
8615
                        mutex_unlock(&priv->mutex);
 
8616
                        return -EINVAL;
 
8617
                }
 
8618
 
 
8619
                /* Cancel any currently running scans... */
 
8620
                if (iwl4965_scan_cancel_timeout(priv, 100))
 
8621
                        IWL_WARNING("Could not cancel scan.\n");
 
8622
                else {
 
8623
                        IWL_DEBUG_INFO("Committing phymode and "
 
8624
                                       "rxon.channel = %d %d\n",
 
8625
                                       phymode, channel);
 
8626
 
 
8627
                        iwl4965_set_rxon_channel(priv, phymode, channel);
 
8628
                        iwl4965_set_flags_for_phymode(priv, phymode);
 
8629
 
 
8630
                        iwl4965_set_rate(priv);
 
8631
                        iwl4965_commit_rxon(priv);
 
8632
                }
 
8633
        }
 
8634
        mutex_unlock(&priv->mutex);
 
8635
 
 
8636
        return count;
 
8637
}
 
8638
 
 
8639
static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
 
8640
 
 
8641
#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
 
8642
 
 
8643
static ssize_t show_measurement(struct device *d,
 
8644
                                struct device_attribute *attr, char *buf)
 
8645
{
 
8646
        struct iwl4965_priv *priv = dev_get_drvdata(d);
 
8647
        struct iwl4965_spectrum_notification measure_report;
 
8648
        u32 size = sizeof(measure_report), len = 0, ofs = 0;
 
8649
        u8 *data = (u8 *) & measure_report;
 
8650
        unsigned long flags;
 
8651
 
 
8652
        spin_lock_irqsave(&priv->lock, flags);
 
8653
        if (!(priv->measurement_status & MEASUREMENT_READY)) {
 
8654
                spin_unlock_irqrestore(&priv->lock, flags);
 
8655
                return 0;
 
8656
        }
 
8657
        memcpy(&measure_report, &priv->measure_report, size);
 
8658
        priv->measurement_status = 0;
 
8659
        spin_unlock_irqrestore(&priv->lock, flags);
 
8660
 
 
8661
        while (size && (PAGE_SIZE - len)) {
 
8662
                hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
 
8663
                                   PAGE_SIZE - len, 1);
 
8664
                len = strlen(buf);
 
8665
                if (PAGE_SIZE - len)
 
8666
                        buf[len++] = '\n';
 
8667
 
 
8668
                ofs += 16;
 
8669
                size -= min(size, 16U);
 
8670
        }
 
8671
 
 
8672
        return len;
 
8673
}
 
8674
 
 
8675
static ssize_t store_measurement(struct device *d,
 
8676
                                 struct device_attribute *attr,
 
8677
                                 const char *buf, size_t count)
 
8678
{
 
8679
        struct iwl4965_priv *priv = dev_get_drvdata(d);
 
8680
        struct ieee80211_measurement_params params = {
 
8681
                .channel = le16_to_cpu(priv->active_rxon.channel),
 
8682
                .start_time = cpu_to_le64(priv->last_tsf),
 
8683
                .duration = cpu_to_le16(1),
 
8684
        };
 
8685
        u8 type = IWL_MEASURE_BASIC;
 
8686
        u8 buffer[32];
 
8687
        u8 channel;
 
8688
 
 
8689
        if (count) {
 
8690
                char *p = buffer;
 
8691
                strncpy(buffer, buf, min(sizeof(buffer), count));
 
8692
                channel = simple_strtoul(p, NULL, 0);
 
8693
                if (channel)
 
8694
                        params.channel = channel;
 
8695
 
 
8696
                p = buffer;
 
8697
                while (*p && *p != ' ')
 
8698
                        p++;
 
8699
                if (*p)
 
8700
                        type = simple_strtoul(p + 1, NULL, 0);
 
8701
        }
 
8702
 
 
8703
        IWL_DEBUG_INFO("Invoking measurement of type %d on "
 
8704
                       "channel %d (for '%s')\n", type, params.channel, buf);
 
8705
        iwl4965_get_measurement(priv, &params, type);
 
8706
 
 
8707
        return count;
 
8708
}
 
8709
 
 
8710
static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
 
8711
                   show_measurement, store_measurement);
 
8712
#endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
 
8713
 
 
8714
static ssize_t store_retry_rate(struct device *d,
 
8715
                                struct device_attribute *attr,
 
8716
                                const char *buf, size_t count)
 
8717
{
 
8718
        struct iwl4965_priv *priv = dev_get_drvdata(d);
 
8719
 
 
8720
        priv->retry_rate = simple_strtoul(buf, NULL, 0);
 
8721
        if (priv->retry_rate <= 0)
 
8722
                priv->retry_rate = 1;
 
8723
 
 
8724
        return count;
 
8725
}
 
8726
 
 
8727
static ssize_t show_retry_rate(struct device *d,
 
8728
                               struct device_attribute *attr, char *buf)
 
8729
{
 
8730
        struct iwl4965_priv *priv = dev_get_drvdata(d);
 
8731
        return sprintf(buf, "%d", priv->retry_rate);
 
8732
}
 
8733
 
 
8734
static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
 
8735
                   store_retry_rate);
 
8736
 
 
8737
static ssize_t store_power_level(struct device *d,
 
8738
                                 struct device_attribute *attr,
 
8739
                                 const char *buf, size_t count)
 
8740
{
 
8741
        struct iwl4965_priv *priv = dev_get_drvdata(d);
 
8742
        int rc;
 
8743
        int mode;
 
8744
 
 
8745
        mode = simple_strtoul(buf, NULL, 0);
 
8746
        mutex_lock(&priv->mutex);
 
8747
 
 
8748
        if (!iwl4965_is_ready(priv)) {
 
8749
                rc = -EAGAIN;
 
8750
                goto out;
 
8751
        }
 
8752
 
 
8753
        if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
 
8754
                mode = IWL_POWER_AC;
 
8755
        else
 
8756
                mode |= IWL_POWER_ENABLED;
 
8757
 
 
8758
        if (mode != priv->power_mode) {
 
8759
                rc = iwl4965_send_power_mode(priv, IWL_POWER_LEVEL(mode));
 
8760
                if (rc) {
 
8761
                        IWL_DEBUG_MAC80211("failed setting power mode.\n");
 
8762
                        goto out;
 
8763
                }
 
8764
                priv->power_mode = mode;
 
8765
        }
 
8766
 
 
8767
        rc = count;
 
8768
 
 
8769
 out:
 
8770
        mutex_unlock(&priv->mutex);
 
8771
        return rc;
 
8772
}
 
8773
 
 
8774
#define MAX_WX_STRING 80
 
8775
 
 
8776
/* Values are in microsecond */
 
8777
static const s32 timeout_duration[] = {
 
8778
        350000,
 
8779
        250000,
 
8780
        75000,
 
8781
        37000,
 
8782
        25000,
 
8783
};
 
8784
static const s32 period_duration[] = {
 
8785
        400000,
 
8786
        700000,
 
8787
        1000000,
 
8788
        1000000,
 
8789
        1000000
 
8790
};
 
8791
 
 
8792
static ssize_t show_power_level(struct device *d,
 
8793
                                struct device_attribute *attr, char *buf)
 
8794
{
 
8795
        struct iwl4965_priv *priv = dev_get_drvdata(d);
 
8796
        int level = IWL_POWER_LEVEL(priv->power_mode);
 
8797
        char *p = buf;
 
8798
 
 
8799
        p += sprintf(p, "%d ", level);
 
8800
        switch (level) {
 
8801
        case IWL_POWER_MODE_CAM:
 
8802
        case IWL_POWER_AC:
 
8803
                p += sprintf(p, "(AC)");
 
8804
                break;
 
8805
        case IWL_POWER_BATTERY:
 
8806
                p += sprintf(p, "(BATTERY)");
 
8807
                break;
 
8808
        default:
 
8809
                p += sprintf(p,
 
8810
                             "(Timeout %dms, Period %dms)",
 
8811
                             timeout_duration[level - 1] / 1000,
 
8812
                             period_duration[level - 1] / 1000);
 
8813
        }
 
8814
 
 
8815
        if (!(priv->power_mode & IWL_POWER_ENABLED))
 
8816
                p += sprintf(p, " OFF\n");
 
8817
        else
 
8818
                p += sprintf(p, " \n");
 
8819
 
 
8820
        return (p - buf + 1);
 
8821
 
 
8822
}
 
8823
 
 
8824
static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
 
8825
                   store_power_level);
 
8826
 
 
8827
static ssize_t show_channels(struct device *d,
 
8828
                             struct device_attribute *attr, char *buf)
 
8829
{
 
8830
        struct iwl4965_priv *priv = dev_get_drvdata(d);
 
8831
        int len = 0, i;
 
8832
        struct ieee80211_channel *channels = NULL;
 
8833
        const struct ieee80211_hw_mode *hw_mode = NULL;
 
8834
        int count = 0;
 
8835
 
 
8836
        if (!iwl4965_is_ready(priv))
 
8837
                return -EAGAIN;
 
8838
 
 
8839
        hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211G);
 
8840
        if (!hw_mode)
 
8841
                hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211B);
 
8842
        if (hw_mode) {
 
8843
                channels = hw_mode->channels;
 
8844
                count = hw_mode->num_channels;
 
8845
        }
 
8846
 
 
8847
        len +=
 
8848
            sprintf(&buf[len],
 
8849
                    "Displaying %d channels in 2.4GHz band "
 
8850
                    "(802.11bg):\n", count);
 
8851
 
 
8852
        for (i = 0; i < count; i++)
 
8853
                len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
 
8854
                               channels[i].chan,
 
8855
                               channels[i].power_level,
 
8856
                               channels[i].
 
8857
                               flag & IEEE80211_CHAN_W_RADAR_DETECT ?
 
8858
                               " (IEEE 802.11h required)" : "",
 
8859
                               (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
 
8860
                                || (channels[i].
 
8861
                                    flag &
 
8862
                                    IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
 
8863
                               ", IBSS",
 
8864
                               channels[i].
 
8865
                               flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
 
8866
                               "active/passive" : "passive only");
 
8867
 
 
8868
        hw_mode = iwl4965_get_hw_mode(priv, MODE_IEEE80211A);
 
8869
        if (hw_mode) {
 
8870
                channels = hw_mode->channels;
 
8871
                count = hw_mode->num_channels;
 
8872
        } else {
 
8873
                channels = NULL;
 
8874
                count = 0;
 
8875
        }
 
8876
 
 
8877
        len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
 
8878
                       "(802.11a):\n", count);
 
8879
 
 
8880
        for (i = 0; i < count; i++)
 
8881
                len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
 
8882
                               channels[i].chan,
 
8883
                               channels[i].power_level,
 
8884
                               channels[i].
 
8885
                               flag & IEEE80211_CHAN_W_RADAR_DETECT ?
 
8886
                               " (IEEE 802.11h required)" : "",
 
8887
                               (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
 
8888
                                || (channels[i].
 
8889
                                    flag &
 
8890
                                    IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
 
8891
                               ", IBSS",
 
8892
                               channels[i].
 
8893
                               flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
 
8894
                               "active/passive" : "passive only");
 
8895
 
 
8896
        return len;
 
8897
}
 
8898
 
 
8899
static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
 
8900
 
 
8901
static ssize_t show_statistics(struct device *d,
 
8902
                               struct device_attribute *attr, char *buf)
 
8903
{
 
8904
        struct iwl4965_priv *priv = dev_get_drvdata(d);
 
8905
        u32 size = sizeof(struct iwl4965_notif_statistics);
 
8906
        u32 len = 0, ofs = 0;
 
8907
        u8 *data = (u8 *) & priv->statistics;
 
8908
        int rc = 0;
 
8909
 
 
8910
        if (!iwl4965_is_alive(priv))
 
8911
                return -EAGAIN;
 
8912
 
 
8913
        mutex_lock(&priv->mutex);
 
8914
        rc = iwl4965_send_statistics_request(priv);
 
8915
        mutex_unlock(&priv->mutex);
 
8916
 
 
8917
        if (rc) {
 
8918
                len = sprintf(buf,
 
8919
                              "Error sending statistics request: 0x%08X\n", rc);
 
8920
                return len;
 
8921
        }
 
8922
 
 
8923
        while (size && (PAGE_SIZE - len)) {
 
8924
                hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
 
8925
                                   PAGE_SIZE - len, 1);
 
8926
                len = strlen(buf);
 
8927
                if (PAGE_SIZE - len)
 
8928
                        buf[len++] = '\n';
 
8929
 
 
8930
                ofs += 16;
 
8931
                size -= min(size, 16U);
 
8932
        }
 
8933
 
 
8934
        return len;
 
8935
}
 
8936
 
 
8937
static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
 
8938
 
 
8939
static ssize_t show_antenna(struct device *d,
 
8940
                            struct device_attribute *attr, char *buf)
 
8941
{
 
8942
        struct iwl4965_priv *priv = dev_get_drvdata(d);
 
8943
 
 
8944
        if (!iwl4965_is_alive(priv))
 
8945
                return -EAGAIN;
 
8946
 
 
8947
        return sprintf(buf, "%d\n", priv->antenna);
 
8948
}
 
8949
 
 
8950
static ssize_t store_antenna(struct device *d,
 
8951
                             struct device_attribute *attr,
 
8952
                             const char *buf, size_t count)
 
8953
{
 
8954
        int ant;
 
8955
        struct iwl4965_priv *priv = dev_get_drvdata(d);
 
8956
 
 
8957
        if (count == 0)
 
8958
                return 0;
 
8959
 
 
8960
        if (sscanf(buf, "%1i", &ant) != 1) {
 
8961
                IWL_DEBUG_INFO("not in hex or decimal form.\n");
 
8962
                return count;
 
8963
        }
 
8964
 
 
8965
        if ((ant >= 0) && (ant <= 2)) {
 
8966
                IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
 
8967
                priv->antenna = (enum iwl4965_antenna)ant;
 
8968
        } else
 
8969
                IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
 
8970
 
 
8971
 
 
8972
        return count;
 
8973
}
 
8974
 
 
8975
static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
 
8976
 
 
8977
static ssize_t show_status(struct device *d,
 
8978
                           struct device_attribute *attr, char *buf)
 
8979
{
 
8980
        struct iwl4965_priv *priv = (struct iwl4965_priv *)d->driver_data;
 
8981
        if (!iwl4965_is_alive(priv))
 
8982
                return -EAGAIN;
 
8983
        return sprintf(buf, "0x%08x\n", (int)priv->status);
 
8984
}
 
8985
 
 
8986
static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
 
8987
 
 
8988
static ssize_t dump_error_log(struct device *d,
 
8989
                              struct device_attribute *attr,
 
8990
                              const char *buf, size_t count)
 
8991
{
 
8992
        char *p = (char *)buf;
 
8993
 
 
8994
        if (p[0] == '1')
 
8995
                iwl4965_dump_nic_error_log((struct iwl4965_priv *)d->driver_data);
 
8996
 
 
8997
        return strnlen(buf, count);
 
8998
}
 
8999
 
 
9000
static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
 
9001
 
 
9002
static ssize_t dump_event_log(struct device *d,
 
9003
                              struct device_attribute *attr,
 
9004
                              const char *buf, size_t count)
 
9005
{
 
9006
        char *p = (char *)buf;
 
9007
 
 
9008
        if (p[0] == '1')
 
9009
                iwl4965_dump_nic_event_log((struct iwl4965_priv *)d->driver_data);
 
9010
 
 
9011
        return strnlen(buf, count);
 
9012
}
 
9013
 
 
9014
static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
 
9015
 
 
9016
/*****************************************************************************
 
9017
 *
 
9018
 * driver setup and teardown
 
9019
 *
 
9020
 *****************************************************************************/
 
9021
 
 
9022
static void iwl4965_setup_deferred_work(struct iwl4965_priv *priv)
 
9023
{
 
9024
        priv->workqueue = create_workqueue(DRV_NAME);
 
9025
 
 
9026
        init_waitqueue_head(&priv->wait_command_queue);
 
9027
 
 
9028
        INIT_WORK(&priv->up, iwl4965_bg_up);
 
9029
        INIT_WORK(&priv->restart, iwl4965_bg_restart);
 
9030
        INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
 
9031
        INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
 
9032
        INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
 
9033
        INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
 
9034
        INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
 
9035
        INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
 
9036
        INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
 
9037
        INIT_DELAYED_WORK(&priv->init_alive_start, iwl4965_bg_init_alive_start);
 
9038
        INIT_DELAYED_WORK(&priv->alive_start, iwl4965_bg_alive_start);
 
9039
        INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
 
9040
 
 
9041
        iwl4965_hw_setup_deferred_work(priv);
 
9042
 
 
9043
        tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
 
9044
                     iwl4965_irq_tasklet, (unsigned long)priv);
 
9045
}
 
9046
 
 
9047
static void iwl4965_cancel_deferred_work(struct iwl4965_priv *priv)
 
9048
{
 
9049
        iwl4965_hw_cancel_deferred_work(priv);
 
9050
 
 
9051
        cancel_delayed_work(&priv->init_alive_start);
 
9052
        cancel_delayed_work(&priv->scan_check);
 
9053
        cancel_delayed_work(&priv->alive_start);
 
9054
        cancel_delayed_work(&priv->post_associate);
 
9055
        cancel_work_sync(&priv->beacon_update);
 
9056
}
 
9057
 
 
9058
static struct attribute *iwl4965_sysfs_entries[] = {
 
9059
        &dev_attr_antenna.attr,
 
9060
        &dev_attr_channels.attr,
 
9061
        &dev_attr_dump_errors.attr,
 
9062
        &dev_attr_dump_events.attr,
 
9063
        &dev_attr_flags.attr,
 
9064
        &dev_attr_filter_flags.attr,
 
9065
#ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
 
9066
        &dev_attr_measurement.attr,
 
9067
#endif
 
9068
        &dev_attr_power_level.attr,
 
9069
        &dev_attr_retry_rate.attr,
 
9070
        &dev_attr_rf_kill.attr,
 
9071
        &dev_attr_rs_window.attr,
 
9072
        &dev_attr_statistics.attr,
 
9073
        &dev_attr_status.attr,
 
9074
        &dev_attr_temperature.attr,
 
9075
        &dev_attr_tune.attr,
 
9076
        &dev_attr_tx_power.attr,
 
9077
 
 
9078
        NULL
 
9079
};
 
9080
 
 
9081
static struct attribute_group iwl4965_attribute_group = {
 
9082
        .name = NULL,           /* put in device directory */
 
9083
        .attrs = iwl4965_sysfs_entries,
 
9084
};
 
9085
 
 
9086
static struct ieee80211_ops iwl4965_hw_ops = {
 
9087
        .tx = iwl4965_mac_tx,
 
9088
        .open = iwl4965_mac_open,
 
9089
        .stop = iwl4965_mac_stop,
 
9090
        .add_interface = iwl4965_mac_add_interface,
 
9091
        .remove_interface = iwl4965_mac_remove_interface,
 
9092
        .config = iwl4965_mac_config,
 
9093
        .config_interface = iwl4965_mac_config_interface,
 
9094
        .set_key = iwl4965_mac_set_key,
 
9095
        .get_stats = iwl4965_mac_get_stats,
 
9096
        .get_tx_stats = iwl4965_mac_get_tx_stats,
 
9097
        .conf_tx = iwl4965_mac_conf_tx,
 
9098
        .get_tsf = iwl4965_mac_get_tsf,
 
9099
        .reset_tsf = iwl4965_mac_reset_tsf,
 
9100
        .beacon_update = iwl4965_mac_beacon_update,
 
9101
#ifdef CONFIG_IWL4965_HT
 
9102
        .conf_ht = iwl4965_mac_conf_ht,
 
9103
        .get_ht_capab = iwl4965_mac_get_ht_capab,
 
9104
#ifdef CONFIG_IWL4965_HT_AGG
 
9105
        .ht_tx_agg_start = iwl4965_mac_ht_tx_agg_start,
 
9106
        .ht_tx_agg_stop = iwl4965_mac_ht_tx_agg_stop,
 
9107
        .ht_rx_agg_start = iwl4965_mac_ht_rx_agg_start,
 
9108
        .ht_rx_agg_stop = iwl4965_mac_ht_rx_agg_stop,
 
9109
#endif  /* CONFIG_IWL4965_HT_AGG */
 
9110
#endif  /* CONFIG_IWL4965_HT */
 
9111
        .hw_scan = iwl4965_mac_hw_scan
 
9112
};
 
9113
 
 
9114
static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
 
9115
{
 
9116
        int err = 0;
 
9117
        struct iwl4965_priv *priv;
 
9118
        struct ieee80211_hw *hw;
 
9119
        int i;
 
9120
 
 
9121
        /* Disabling hardware scan means that mac80211 will perform scans
 
9122
         * "the hard way", rather than using device's scan. */
 
9123
        if (iwl4965_param_disable_hw_scan) {
 
9124
                IWL_DEBUG_INFO("Disabling hw_scan\n");
 
9125
                iwl4965_hw_ops.hw_scan = NULL;
 
9126
        }
 
9127
 
 
9128
        if ((iwl4965_param_queues_num > IWL_MAX_NUM_QUEUES) ||
 
9129
            (iwl4965_param_queues_num < IWL_MIN_NUM_QUEUES)) {
 
9130
                IWL_ERROR("invalid queues_num, should be between %d and %d\n",
 
9131
                          IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
 
9132
                err = -EINVAL;
 
9133
                goto out;
 
9134
        }
 
9135
 
 
9136
        /* mac80211 allocates memory for this device instance, including
 
9137
         *   space for this driver's private structure */
 
9138
        hw = iwlwifi_ieee80211_alloc_hw(sizeof(struct iwl4965_priv), &iwl4965_hw_ops);
 
9139
        if (hw == NULL) {
 
9140
                IWL_ERROR("Can not allocate network device\n");
 
9141
                err = -ENOMEM;
 
9142
                goto out;
 
9143
        }
 
9144
        SET_IEEE80211_DEV(hw, &pdev->dev);
 
9145
 
 
9146
        IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
 
9147
        priv = hw->priv;
 
9148
        priv->hw = hw;
 
9149
 
 
9150
        priv->pci_dev = pdev;
 
9151
        priv->antenna = (enum iwl4965_antenna)iwl4965_param_antenna;
 
9152
#ifdef CONFIG_IWL4965_DEBUG
 
9153
        iwl4965_debug_level = iwl4965_param_debug;
 
9154
        atomic_set(&priv->restrict_refcnt, 0);
 
9155
#endif
 
9156
        priv->retry_rate = 1;
 
9157
 
 
9158
        priv->ibss_beacon = NULL;
 
9159
 
 
9160
        /* Tell mac80211 and its clients (e.g. Wireless Extensions)
 
9161
         *   the range of signal quality values that we'll provide.
 
9162
         * Negative values for level/noise indicate that we'll provide dBm.
 
9163
         * For WE, at least, non-0 values here *enable* display of values
 
9164
         *   in app (iwconfig). */
 
9165
        hw->max_rssi = -20;     /* signal level, negative indicates dBm */
 
9166
        hw->max_noise = -20;    /* noise level, negative indicates dBm */
 
9167
        hw->max_signal = 100;   /* link quality indication (%) */
 
9168
 
 
9169
        /* Tell mac80211 our Tx characteristics */
 
9170
        hw->flags = IEEE80211_HW_WEP_INCLUDE_IV |
 
9171
            IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
 
9172
 
 
9173
        /* Default value; 4 EDCA QOS priorities */
 
9174
        hw->queues = 4;
 
9175
#ifdef CONFIG_IWL4965_HT
 
9176
#ifdef CONFIG_IWL4965_HT_AGG
 
9177
        /* Enhanced value; more queues, to support 11n aggregation */
 
9178
        hw->queues = 16;
 
9179
#endif /* CONFIG_IWL4965_HT_AGG */
 
9180
#endif /* CONFIG_IWL4965_HT */
 
9181
 
 
9182
        spin_lock_init(&priv->lock);
 
9183
        spin_lock_init(&priv->power_data.lock);
 
9184
        spin_lock_init(&priv->sta_lock);
 
9185
        spin_lock_init(&priv->hcmd_lock);
 
9186
        spin_lock_init(&priv->lq_mngr.lock);
 
9187
 
 
9188
        for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
 
9189
                INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
 
9190
 
 
9191
        INIT_LIST_HEAD(&priv->free_frames);
 
9192
 
 
9193
        mutex_init(&priv->mutex);
 
9194
        if (pci_enable_device(pdev)) {
 
9195
                err = -ENODEV;
 
9196
                goto out_iwlwifi_ieee80211_free_hw;
 
9197
        }
 
9198
 
 
9199
        pci_set_master(pdev);
 
9200
 
 
9201
        /* Clear the driver's (not device's) station table */
 
9202
        iwl4965_clear_stations_table(priv);
 
9203
 
 
9204
        priv->data_retry_limit = -1;
 
9205
        priv->ieee_channels = NULL;
 
9206
        priv->ieee_rates = NULL;
 
9207
        priv->phymode = -1;
 
9208
 
 
9209
        err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
 
9210
        if (!err)
 
9211
                err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
 
9212
        if (err) {
 
9213
                printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
 
9214
                goto out_pci_disable_device;
 
9215
        }
 
9216
 
 
9217
        pci_set_drvdata(pdev, priv);
 
9218
        err = pci_request_regions(pdev, DRV_NAME);
 
9219
        if (err)
 
9220
                goto out_pci_disable_device;
 
9221
 
 
9222
        /* We disable the RETRY_TIMEOUT register (0x41) to keep
 
9223
         * PCI Tx retries from interfering with C3 CPU state */
 
9224
        pci_write_config_byte(pdev, 0x41, 0x00);
 
9225
 
 
9226
        priv->hw_base = pci_iomap(pdev, 0, 0);
 
9227
        if (!priv->hw_base) {
 
9228
                err = -ENODEV;
 
9229
                goto out_pci_release_regions;
 
9230
        }
 
9231
 
 
9232
        IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
 
9233
                        (unsigned long long) pci_resource_len(pdev, 0));
 
9234
        IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
 
9235
 
 
9236
        /* Initialize module parameter values here */
 
9237
 
 
9238
        /* Disable radio (SW RF KILL) via parameter when loading driver */
 
9239
        if (iwl4965_param_disable) {
 
9240
                set_bit(STATUS_RF_KILL_SW, &priv->status);
 
9241
                IWL_DEBUG_INFO("Radio disabled.\n");
 
9242
        }
 
9243
 
 
9244
        priv->iw_mode = IEEE80211_IF_TYPE_STA;
 
9245
 
 
9246
        priv->ps_mode = 0;
 
9247
        priv->use_ant_b_for_management_frame = 1; /* start with ant B */
 
9248
        priv->is_ht_enabled = 1;
 
9249
        priv->channel_width = IWL_CHANNEL_WIDTH_40MHZ;
 
9250
        priv->valid_antenna = 0x7;      /* assume all 3 connected */
 
9251
        priv->ps_mode = IWL_MIMO_PS_NONE;
 
9252
 
 
9253
        /* Choose which receivers/antennas to use */
 
9254
        iwl4965_set_rxon_chain(priv);
 
9255
 
 
9256
        printk(KERN_INFO DRV_NAME
 
9257
               ": Detected Intel Wireless WiFi Link 4965AGN\n");
 
9258
 
 
9259
        /* Device-specific setup */
 
9260
        if (iwl4965_hw_set_hw_setting(priv)) {
 
9261
                IWL_ERROR("failed to set hw settings\n");
 
9262
                goto out_iounmap;
 
9263
        }
 
9264
 
 
9265
#ifdef CONFIG_IWL4965_QOS
 
9266
        if (iwl4965_param_qos_enable)
 
9267
                priv->qos_data.qos_enable = 1;
 
9268
 
 
9269
        iwl4965_reset_qos(priv);
 
9270
 
 
9271
        priv->qos_data.qos_active = 0;
 
9272
        priv->qos_data.qos_cap.val = 0;
 
9273
#endif /* CONFIG_IWL4965_QOS */
 
9274
 
 
9275
        iwl4965_set_rxon_channel(priv, MODE_IEEE80211G, 6);
 
9276
        iwl4965_setup_deferred_work(priv);
 
9277
        iwl4965_setup_rx_handlers(priv);
 
9278
 
 
9279
        priv->rates_mask = IWL_RATES_MASK;
 
9280
        /* If power management is turned on, default to AC mode */
 
9281
        priv->power_mode = IWL_POWER_AC;
 
9282
        priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
 
9283
 
 
9284
        iwl4965_disable_interrupts(priv);
 
9285
 
 
9286
        err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
 
9287
        if (err) {
 
9288
                IWL_ERROR("failed to create sysfs device attributes\n");
 
9289
                goto out_release_irq;
 
9290
        }
 
9291
 
 
9292
        /* nic init */
 
9293
        iwl4965_set_bit(priv, CSR_GIO_CHICKEN_BITS,
 
9294
                    CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
 
9295
 
 
9296
        iwl4965_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
 
9297
        err = iwl4965_poll_bit(priv, CSR_GP_CNTRL,
 
9298
                          CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
 
9299
                          CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
 
9300
        if (err < 0) {
 
9301
                IWL_DEBUG_INFO("Failed to init the card\n");
 
9302
                goto out_remove_sysfs;
 
9303
        }
 
9304
        /* Read the EEPROM */
 
9305
        err = iwl4965_eeprom_init(priv);
 
9306
        if (err) {
 
9307
                IWL_ERROR("Unable to init EEPROM\n");
 
9308
                goto out_remove_sysfs;
 
9309
        }
 
9310
        /* MAC Address location in EEPROM same for 3945/4965 */
 
9311
        get_eeprom_mac(priv, priv->mac_addr);
 
9312
        IWL_DEBUG_INFO("MAC address: " MAC_FMT "\n", MAC_ARG(priv->mac_addr));
 
9313
        SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
 
9314
 
 
9315
        iwl4965_rate_control_register(priv->hw);
 
9316
        err = iwlwifi_ieee80211_register_hw(priv->hw);
 
9317
        if (err) {
 
9318
                IWL_ERROR("Failed to register network device (error %d)\n", err);
 
9319
                goto out_remove_sysfs;
 
9320
        }
 
9321
 
 
9322
        priv->hw->conf.beacon_int = 100;
 
9323
        priv->mac80211_registered = 1;
 
9324
        pci_save_state(pdev);
 
9325
        pci_disable_device(pdev);
 
9326
 
 
9327
        return 0;
 
9328
 
 
9329
 out_remove_sysfs:
 
9330
        iwl4965_rate_control_unregister(priv->hw);
 
9331
        sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
 
9332
 
 
9333
 out_release_irq:
 
9334
        destroy_workqueue(priv->workqueue);
 
9335
        priv->workqueue = NULL;
 
9336
        iwl4965_unset_hw_setting(priv);
 
9337
 
 
9338
 out_iounmap:
 
9339
        pci_iounmap(pdev, priv->hw_base);
 
9340
 out_pci_release_regions:
 
9341
        pci_release_regions(pdev);
 
9342
 out_pci_disable_device:
 
9343
        pci_disable_device(pdev);
 
9344
        pci_set_drvdata(pdev, NULL);
 
9345
 out_iwlwifi_ieee80211_free_hw:
 
9346
        iwlwifi_ieee80211_free_hw(priv->hw);
 
9347
 out:
 
9348
        return err;
 
9349
}
 
9350
 
 
9351
static void iwl4965_pci_remove(struct pci_dev *pdev)
 
9352
{
 
9353
        struct iwl4965_priv *priv = pci_get_drvdata(pdev);
 
9354
        struct list_head *p, *q;
 
9355
        int i;
 
9356
 
 
9357
        if (!priv)
 
9358
                return;
 
9359
 
 
9360
        IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
 
9361
 
 
9362
        set_bit(STATUS_EXIT_PENDING, &priv->status);
 
9363
 
 
9364
        iwl4965_down(priv);
 
9365
 
 
9366
        /* Free MAC hash list for ADHOC */
 
9367
        for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
 
9368
                list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
 
9369
                        list_del(p);
 
9370
                        kfree(list_entry(p, struct iwl4965_ibss_seq, list));
 
9371
                }
 
9372
        }
 
9373
 
 
9374
        sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
 
9375
 
 
9376
        iwl4965_dealloc_ucode_pci(priv);
 
9377
 
 
9378
        if (priv->rxq.bd)
 
9379
                iwl4965_rx_queue_free(priv, &priv->rxq);
 
9380
        iwl4965_hw_txq_ctx_free(priv);
 
9381
 
 
9382
        iwl4965_unset_hw_setting(priv);
 
9383
        iwl4965_clear_stations_table(priv);
 
9384
 
 
9385
        if (priv->mac80211_registered) {
 
9386
                iwlwifi_ieee80211_unregister_hw(priv->hw);
 
9387
                iwl4965_rate_control_unregister(priv->hw);
 
9388
        }
 
9389
 
 
9390
        /*netif_stop_queue(dev); */
 
9391
        flush_workqueue(priv->workqueue);
 
9392
 
 
9393
        /* iwlwifi_ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
 
9394
         * priv->workqueue... so we can't take down the workqueue
 
9395
         * until now... */
 
9396
        destroy_workqueue(priv->workqueue);
 
9397
        priv->workqueue = NULL;
 
9398
 
 
9399
        pci_iounmap(pdev, priv->hw_base);
 
9400
        pci_release_regions(pdev);
 
9401
        pci_disable_device(pdev);
 
9402
        pci_set_drvdata(pdev, NULL);
 
9403
 
 
9404
        kfree(priv->channel_info);
 
9405
 
 
9406
        kfree(priv->ieee_channels);
 
9407
        kfree(priv->ieee_rates);
 
9408
 
 
9409
        if (priv->ibss_beacon)
 
9410
                dev_kfree_skb(priv->ibss_beacon);
 
9411
 
 
9412
        iwlwifi_ieee80211_free_hw(priv->hw);
 
9413
}
 
9414
 
 
9415
#ifdef CONFIG_PM
 
9416
 
 
9417
static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
 
9418
{
 
9419
        struct iwl4965_priv *priv = pci_get_drvdata(pdev);
 
9420
 
 
9421
        if (priv->is_open) {
 
9422
                set_bit(STATUS_IN_SUSPEND, &priv->status);
 
9423
                iwl4965_mac_stop(priv->hw);
 
9424
                priv->is_open = 1;
 
9425
        }
 
9426
 
 
9427
        pci_set_power_state(pdev, PCI_D3hot);
 
9428
 
 
9429
        return 0;
 
9430
}
 
9431
 
 
9432
static int iwl4965_pci_resume(struct pci_dev *pdev)
 
9433
{
 
9434
        struct iwl4965_priv *priv = pci_get_drvdata(pdev);
 
9435
 
 
9436
        pci_set_power_state(pdev, PCI_D0);
 
9437
 
 
9438
        if (priv->is_open)
 
9439
                iwl4965_mac_open(priv->hw);
 
9440
 
 
9441
        clear_bit(STATUS_IN_SUSPEND, &priv->status);
 
9442
        return 0;
 
9443
}
 
9444
 
 
9445
#endif /* CONFIG_PM */
 
9446
 
 
9447
/*****************************************************************************
 
9448
 *
 
9449
 * driver and module entry point
 
9450
 *
 
9451
 *****************************************************************************/
 
9452
 
 
9453
static struct pci_driver iwl4965_driver = {
 
9454
        .name = DRV_NAME,
 
9455
        .id_table = iwl4965_hw_card_ids,
 
9456
        .probe = iwl4965_pci_probe,
 
9457
        .remove = __devexit_p(iwl4965_pci_remove),
 
9458
#ifdef CONFIG_PM
 
9459
        .suspend = iwl4965_pci_suspend,
 
9460
        .resume = iwl4965_pci_resume,
 
9461
#endif
 
9462
};
 
9463
 
 
9464
static int __init iwl4965_init(void)
 
9465
{
 
9466
 
 
9467
        int ret;
 
9468
        printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
 
9469
        printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
 
9470
        ret = pci_register_driver(&iwl4965_driver);
 
9471
        if (ret) {
 
9472
                IWL_ERROR("Unable to initialize PCI module\n");
 
9473
                return ret;
 
9474
        }
 
9475
#ifdef CONFIG_IWL4965_DEBUG
 
9476
        ret = driver_create_file(&iwl4965_driver.driver, &driver_attr_debug_level);
 
9477
        if (ret) {
 
9478
                IWL_ERROR("Unable to create driver sysfs file\n");
 
9479
                pci_unregister_driver(&iwl4965_driver);
 
9480
                return ret;
 
9481
        }
 
9482
#endif
 
9483
 
 
9484
        return ret;
 
9485
}
 
9486
 
 
9487
static void __exit iwl4965_exit(void)
 
9488
{
 
9489
#ifdef CONFIG_IWL4965_DEBUG
 
9490
        driver_remove_file(&iwl4965_driver.driver, &driver_attr_debug_level);
 
9491
#endif
 
9492
        pci_unregister_driver(&iwl4965_driver);
 
9493
}
 
9494
 
 
9495
module_param_named(antenna, iwl4965_param_antenna, int, 0444);
 
9496
MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
 
9497
module_param_named(disable, iwl4965_param_disable, int, 0444);
 
9498
MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
 
9499
module_param_named(hwcrypto, iwl4965_param_hwcrypto, int, 0444);
 
9500
MODULE_PARM_DESC(hwcrypto,
 
9501
                 "using hardware crypto engine (default 0 [software])\n");
 
9502
module_param_named(debug, iwl4965_param_debug, int, 0444);
 
9503
MODULE_PARM_DESC(debug, "debug output mask");
 
9504
module_param_named(disable_hw_scan, iwl4965_param_disable_hw_scan, int, 0444);
 
9505
MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
 
9506
 
 
9507
module_param_named(queues_num, iwl4965_param_queues_num, int, 0444);
 
9508
MODULE_PARM_DESC(queues_num, "number of hw queues.");
 
9509
 
 
9510
/* QoS */
 
9511
module_param_named(qos_enable, iwl4965_param_qos_enable, int, 0444);
 
9512
MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
 
9513
 
 
9514
module_exit(iwl4965_exit);
 
9515
module_init(iwl4965_init);