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