Merge branch 'fixes' of master.kernel.org:/pub/scm/linux/kernel/git/linville/wireless-2.6
[linux-2.6.git] / drivers / net / wireless / iwlwifi / iwl4965-base.c
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/etherdevice.h>
42 #include <linux/if_arp.h>
43
44 #include <net/mac80211.h>
45
46 #include <asm/div64.h>
47
48 #include "iwl-4965.h"
49 #include "iwl-helpers.h"
50
51 #ifdef CONFIG_IWL4965_DEBUG
52 u32 iwl4965_debug_level;
53 #endif
54
55 static int iwl4965_tx_queue_update_write_ptr(struct iwl4965_priv *priv,
56                                   struct iwl4965_tx_queue *txq);
57
58 /******************************************************************************
59  *
60  * module boiler plate
61  *
62  ******************************************************************************/
63
64 /* module parameters */
65 static int iwl4965_param_disable_hw_scan; /* def: 0 = use 4965's h/w scan */
66 static int iwl4965_param_debug;    /* def: 0 = minimal debug log messages */
67 static int iwl4965_param_disable;  /* def: enable radio */
68 static int iwl4965_param_antenna;  /* def: 0 = both antennas (use diversity) */
69 int iwl4965_param_hwcrypto;        /* def: using software encryption */
70 static int iwl4965_param_qos_enable = 1; /* def: 1 = use quality of service */
71 int iwl4965_param_queues_num = IWL_MAX_NUM_QUEUES; /* def: 16 Tx queues */
72 int iwl4965_param_amsdu_size_8K;   /* def: enable 8K amsdu size */
73
74 /*
75  * module name, copyright, version, etc.
76  * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
77  */
78
79 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
80
81 #ifdef CONFIG_IWL4965_DEBUG
82 #define VD "d"
83 #else
84 #define VD
85 #endif
86
87 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
88 #define VS "s"
89 #else
90 #define VS
91 #endif
92
93 #define IWLWIFI_VERSION "1.2.23k" VD VS
94 #define DRV_COPYRIGHT   "Copyright(c) 2003-2007 Intel Corporation"
95 #define DRV_VERSION     IWLWIFI_VERSION
96
97 /* Change firmware file name, using "-" and incrementing number,
98  *   *only* when uCode interface or architecture changes so that it
99  *   is not compatible with earlier drivers.
100  * This number will also appear in << 8 position of 1st dword of uCode file */
101 #define IWL4965_UCODE_API "-1"
102
103 MODULE_DESCRIPTION(DRV_DESCRIPTION);
104 MODULE_VERSION(DRV_VERSION);
105 MODULE_AUTHOR(DRV_COPYRIGHT);
106 MODULE_LICENSE("GPL");
107
108 __le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
109 {
110         u16 fc = le16_to_cpu(hdr->frame_control);
111         int hdr_len = ieee80211_get_hdrlen(fc);
112
113         if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
114                 return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
115         return NULL;
116 }
117
118 static const struct ieee80211_hw_mode *iwl4965_get_hw_mode(
119                 struct iwl4965_priv *priv, int mode)
120 {
121         int i;
122
123         for (i = 0; i < 3; i++)
124                 if (priv->modes[i].mode == mode)
125                         return &priv->modes[i];
126
127         return NULL;
128 }
129
130 static int iwl4965_is_empty_essid(const char *essid, int essid_len)
131 {
132         /* Single white space is for Linksys APs */
133         if (essid_len == 1 && essid[0] == ' ')
134                 return 1;
135
136         /* Otherwise, if the entire essid is 0, we assume it is hidden */
137         while (essid_len) {
138                 essid_len--;
139                 if (essid[essid_len] != '\0')
140                         return 0;
141         }
142
143         return 1;
144 }
145
146 static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
147 {
148         static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
149         const char *s = essid;
150         char *d = escaped;
151
152         if (iwl4965_is_empty_essid(essid, essid_len)) {
153                 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
154                 return escaped;
155         }
156
157         essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
158         while (essid_len--) {
159                 if (*s == '\0') {
160                         *d++ = '\\';
161                         *d++ = '0';
162                         s++;
163                 } else
164                         *d++ = *s++;
165         }
166         *d = '\0';
167         return escaped;
168 }
169
170 static void iwl4965_print_hex_dump(int level, void *p, u32 len)
171 {
172 #ifdef CONFIG_IWL4965_DEBUG
173         if (!(iwl4965_debug_level & level))
174                 return;
175
176         print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
177                         p, len, 1);
178 #endif
179 }
180
181 /*************** DMA-QUEUE-GENERAL-FUNCTIONS  *****
182  * DMA services
183  *
184  * Theory of operation
185  *
186  * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
187  * of buffer descriptors, each of which points to one or more data buffers for
188  * the device to read from or fill.  Driver and device exchange status of each
189  * queue via "read" and "write" pointers.  Driver keeps minimum of 2 empty
190  * entries in each circular buffer, to protect against confusing empty and full
191  * queue states.
192  *
193  * The device reads or writes the data in the queues via the device's several
194  * DMA/FIFO channels.  Each queue is mapped to a single DMA channel.
195  *
196  * For Tx queue, there are low mark and high mark limits. If, after queuing
197  * the packet for Tx, free space become < low mark, Tx queue stopped. When
198  * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
199  * Tx queue resumed.
200  *
201  * The 4965 operates with up to 17 queues:  One receive queue, one transmit
202  * queue (#4) for sending commands to the device firmware, and 15 other
203  * Tx queues that may be mapped to prioritized Tx DMA/FIFO channels.
204  *
205  * See more detailed info in iwl-4965-hw.h.
206  ***************************************************/
207
208 static int iwl4965_queue_space(const struct iwl4965_queue *q)
209 {
210         int s = q->read_ptr - q->write_ptr;
211
212         if (q->read_ptr > q->write_ptr)
213                 s -= q->n_bd;
214
215         if (s <= 0)
216                 s += q->n_window;
217         /* keep some reserve to not confuse empty and full situations */
218         s -= 2;
219         if (s < 0)
220                 s = 0;
221         return s;
222 }
223
224 /**
225  * iwl4965_queue_inc_wrap - increment queue index, wrap back to beginning
226  * @index -- current index
227  * @n_bd -- total number of entries in queue (must be power of 2)
228  */
229 static inline int iwl4965_queue_inc_wrap(int index, int n_bd)
230 {
231         return ++index & (n_bd - 1);
232 }
233
234 /**
235  * iwl4965_queue_dec_wrap - decrement queue index, wrap back to end
236  * @index -- current index
237  * @n_bd -- total number of entries in queue (must be power of 2)
238  */
239 static inline int iwl4965_queue_dec_wrap(int index, int n_bd)
240 {
241         return --index & (n_bd - 1);
242 }
243
244 static inline int x2_queue_used(const struct iwl4965_queue *q, int i)
245 {
246         return q->write_ptr > q->read_ptr ?
247                 (i >= q->read_ptr && i < q->write_ptr) :
248                 !(i < q->read_ptr && i >= q->write_ptr);
249 }
250
251 static inline u8 get_cmd_index(struct iwl4965_queue *q, u32 index, int is_huge)
252 {
253         /* This is for scan command, the big buffer at end of command array */
254         if (is_huge)
255                 return q->n_window;     /* must be power of 2 */
256
257         /* Otherwise, use normal size buffers */
258         return index & (q->n_window - 1);
259 }
260
261 /**
262  * iwl4965_queue_init - Initialize queue's high/low-water and read/write indexes
263  */
264 static int iwl4965_queue_init(struct iwl4965_priv *priv, struct iwl4965_queue *q,
265                           int count, int slots_num, u32 id)
266 {
267         q->n_bd = count;
268         q->n_window = slots_num;
269         q->id = id;
270
271         /* count must be power-of-two size, otherwise iwl4965_queue_inc_wrap
272          * and iwl4965_queue_dec_wrap are broken. */
273         BUG_ON(!is_power_of_2(count));
274
275         /* slots_num must be power-of-two size, otherwise
276          * get_cmd_index is broken. */
277         BUG_ON(!is_power_of_2(slots_num));
278
279         q->low_mark = q->n_window / 4;
280         if (q->low_mark < 4)
281                 q->low_mark = 4;
282
283         q->high_mark = q->n_window / 8;
284         if (q->high_mark < 2)
285                 q->high_mark = 2;
286
287         q->write_ptr = q->read_ptr = 0;
288
289         return 0;
290 }
291
292 /**
293  * iwl4965_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
294  */
295 static int iwl4965_tx_queue_alloc(struct iwl4965_priv *priv,
296                               struct iwl4965_tx_queue *txq, u32 id)
297 {
298         struct pci_dev *dev = priv->pci_dev;
299
300         /* Driver private data, only for Tx (not command) queues,
301          * not shared with device. */
302         if (id != IWL_CMD_QUEUE_NUM) {
303                 txq->txb = kmalloc(sizeof(txq->txb[0]) *
304                                    TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
305                 if (!txq->txb) {
306                         IWL_ERROR("kmalloc for auxiliary BD "
307                                   "structures failed\n");
308                         goto error;
309                 }
310         } else
311                 txq->txb = NULL;
312
313         /* Circular buffer of transmit frame descriptors (TFDs),
314          * shared with device */
315         txq->bd = pci_alloc_consistent(dev,
316                         sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
317                         &txq->q.dma_addr);
318
319         if (!txq->bd) {
320                 IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
321                           sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
322                 goto error;
323         }
324         txq->q.id = id;
325
326         return 0;
327
328  error:
329         if (txq->txb) {
330                 kfree(txq->txb);
331                 txq->txb = NULL;
332         }
333
334         return -ENOMEM;
335 }
336
337 /**
338  * iwl4965_tx_queue_init - Allocate and initialize one tx/cmd queue
339  */
340 int iwl4965_tx_queue_init(struct iwl4965_priv *priv,
341                       struct iwl4965_tx_queue *txq, int slots_num, u32 txq_id)
342 {
343         struct pci_dev *dev = priv->pci_dev;
344         int len;
345         int rc = 0;
346
347         /*
348          * Alloc buffer array for commands (Tx or other types of commands).
349          * For the command queue (#4), allocate command space + one big
350          * command for scan, since scan command is very huge; the system will
351          * not have two scans at the same time, so only one is needed.
352          * For normal Tx queues (all other queues), no super-size command
353          * space is needed.
354          */
355         len = sizeof(struct iwl4965_cmd) * slots_num;
356         if (txq_id == IWL_CMD_QUEUE_NUM)
357                 len +=  IWL_MAX_SCAN_SIZE;
358         txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
359         if (!txq->cmd)
360                 return -ENOMEM;
361
362         /* Alloc driver data array and TFD circular buffer */
363         rc = iwl4965_tx_queue_alloc(priv, txq, txq_id);
364         if (rc) {
365                 pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
366
367                 return -ENOMEM;
368         }
369         txq->need_update = 0;
370
371         /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
372          * iwl4965_queue_inc_wrap and iwl4965_queue_dec_wrap are broken. */
373         BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
374
375         /* Initialize queue's high/low-water marks, and head/tail indexes */
376         iwl4965_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
377
378         /* Tell device where to find queue */
379         iwl4965_hw_tx_queue_init(priv, txq);
380
381         return 0;
382 }
383
384 /**
385  * iwl4965_tx_queue_free - Deallocate DMA queue.
386  * @txq: Transmit queue to deallocate.
387  *
388  * Empty queue by removing and destroying all BD's.
389  * Free all buffers.
390  * 0-fill, but do not free "txq" descriptor structure.
391  */
392 void iwl4965_tx_queue_free(struct iwl4965_priv *priv, struct iwl4965_tx_queue *txq)
393 {
394         struct iwl4965_queue *q = &txq->q;
395         struct pci_dev *dev = priv->pci_dev;
396         int len;
397
398         if (q->n_bd == 0)
399                 return;
400
401         /* first, empty all BD's */
402         for (; q->write_ptr != q->read_ptr;
403              q->read_ptr = iwl4965_queue_inc_wrap(q->read_ptr, q->n_bd))
404                 iwl4965_hw_txq_free_tfd(priv, txq);
405
406         len = sizeof(struct iwl4965_cmd) * q->n_window;
407         if (q->id == IWL_CMD_QUEUE_NUM)
408                 len += IWL_MAX_SCAN_SIZE;
409
410         /* De-alloc array of command/tx buffers */
411         pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
412
413         /* De-alloc circular buffer of TFDs */
414         if (txq->q.n_bd)
415                 pci_free_consistent(dev, sizeof(struct iwl4965_tfd_frame) *
416                                     txq->q.n_bd, txq->bd, txq->q.dma_addr);
417
418         /* De-alloc array of per-TFD driver data */
419         if (txq->txb) {
420                 kfree(txq->txb);
421                 txq->txb = NULL;
422         }
423
424         /* 0-fill queue descriptor structure */
425         memset(txq, 0, sizeof(*txq));
426 }
427
428 const u8 iwl4965_broadcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
429
430 /*************** STATION TABLE MANAGEMENT ****
431  * mac80211 should be examined to determine if sta_info is duplicating
432  * the functionality provided here
433  */
434
435 /**************************************************************/
436
437 #if 0 /* temporary disable till we add real remove station */
438 /**
439  * iwl4965_remove_station - Remove driver's knowledge of station.
440  *
441  * NOTE:  This does not remove station from device's station table.
442  */
443 static u8 iwl4965_remove_station(struct iwl4965_priv *priv, const u8 *addr, int is_ap)
444 {
445         int index = IWL_INVALID_STATION;
446         int i;
447         unsigned long flags;
448
449         spin_lock_irqsave(&priv->sta_lock, flags);
450
451         if (is_ap)
452                 index = IWL_AP_ID;
453         else if (is_broadcast_ether_addr(addr))
454                 index = priv->hw_setting.bcast_sta_id;
455         else
456                 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
457                         if (priv->stations[i].used &&
458                             !compare_ether_addr(priv->stations[i].sta.sta.addr,
459                                                 addr)) {
460                                 index = i;
461                                 break;
462                         }
463
464         if (unlikely(index == IWL_INVALID_STATION))
465                 goto out;
466
467         if (priv->stations[index].used) {
468                 priv->stations[index].used = 0;
469                 priv->num_stations--;
470         }
471
472         BUG_ON(priv->num_stations < 0);
473
474 out:
475         spin_unlock_irqrestore(&priv->sta_lock, flags);
476         return 0;
477 }
478 #endif
479
480 /**
481  * iwl4965_clear_stations_table - Clear the driver's station table
482  *
483  * NOTE:  This does not clear or otherwise alter the device's station table.
484  */
485 static void iwl4965_clear_stations_table(struct iwl4965_priv *priv)
486 {
487         unsigned long flags;
488
489         spin_lock_irqsave(&priv->sta_lock, flags);
490
491         priv->num_stations = 0;
492         memset(priv->stations, 0, sizeof(priv->stations));
493
494         spin_unlock_irqrestore(&priv->sta_lock, flags);
495 }
496
497 /**
498  * iwl4965_add_station_flags - Add station to tables in driver and device
499  */
500 u8 iwl4965_add_station_flags(struct iwl4965_priv *priv, const u8 *addr,
501                                 int is_ap, u8 flags, void *ht_data)
502 {
503         int i;
504         int index = IWL_INVALID_STATION;
505         struct iwl4965_station_entry *station;
506         unsigned long flags_spin;
507         DECLARE_MAC_BUF(mac);
508
509         spin_lock_irqsave(&priv->sta_lock, flags_spin);
510         if (is_ap)
511                 index = IWL_AP_ID;
512         else if (is_broadcast_ether_addr(addr))
513                 index = priv->hw_setting.bcast_sta_id;
514         else
515                 for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
516                         if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
517                                                 addr)) {
518                                 index = i;
519                                 break;
520                         }
521
522                         if (!priv->stations[i].used &&
523                             index == IWL_INVALID_STATION)
524                                 index = i;
525                 }
526
527
528         /* These two conditions have the same outcome, but keep them separate
529           since they have different meanings */
530         if (unlikely(index == IWL_INVALID_STATION)) {
531                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
532                 return index;
533         }
534
535         if (priv->stations[index].used &&
536             !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
537                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
538                 return index;
539         }
540
541
542         IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
543         station = &priv->stations[index];
544         station->used = 1;
545         priv->num_stations++;
546
547         /* Set up the REPLY_ADD_STA command to send to device */
548         memset(&station->sta, 0, sizeof(struct iwl4965_addsta_cmd));
549         memcpy(station->sta.sta.addr, addr, ETH_ALEN);
550         station->sta.mode = 0;
551         station->sta.sta.sta_id = index;
552         station->sta.station_flags = 0;
553
554 #ifdef CONFIG_IWL4965_HT
555         /* BCAST station and IBSS stations do not work in HT mode */
556         if (index != priv->hw_setting.bcast_sta_id &&
557             priv->iw_mode != IEEE80211_IF_TYPE_IBSS)
558                 iwl4965_set_ht_add_station(priv, index,
559                                  (struct ieee80211_ht_info *) ht_data);
560 #endif /*CONFIG_IWL4965_HT*/
561
562         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
563
564         /* Add station to device's station table */
565         iwl4965_send_add_station(priv, &station->sta, flags);
566         return index;
567
568 }
569
570 /*************** DRIVER STATUS FUNCTIONS   *****/
571
572 static inline int iwl4965_is_ready(struct iwl4965_priv *priv)
573 {
574         /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
575          * set but EXIT_PENDING is not */
576         return test_bit(STATUS_READY, &priv->status) &&
577                test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
578                !test_bit(STATUS_EXIT_PENDING, &priv->status);
579 }
580
581 static inline int iwl4965_is_alive(struct iwl4965_priv *priv)
582 {
583         return test_bit(STATUS_ALIVE, &priv->status);
584 }
585
586 static inline int iwl4965_is_init(struct iwl4965_priv *priv)
587 {
588         return test_bit(STATUS_INIT, &priv->status);
589 }
590
591 static inline int iwl4965_is_rfkill(struct iwl4965_priv *priv)
592 {
593         return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
594                test_bit(STATUS_RF_KILL_SW, &priv->status);
595 }
596
597 static inline int iwl4965_is_ready_rf(struct iwl4965_priv *priv)
598 {
599
600         if (iwl4965_is_rfkill(priv))
601                 return 0;
602
603         return iwl4965_is_ready(priv);
604 }
605
606 /*************** HOST COMMAND QUEUE FUNCTIONS   *****/
607
608 #define IWL_CMD(x) case x : return #x
609
610 static const char *get_cmd_string(u8 cmd)
611 {
612         switch (cmd) {
613                 IWL_CMD(REPLY_ALIVE);
614                 IWL_CMD(REPLY_ERROR);
615                 IWL_CMD(REPLY_RXON);
616                 IWL_CMD(REPLY_RXON_ASSOC);
617                 IWL_CMD(REPLY_QOS_PARAM);
618                 IWL_CMD(REPLY_RXON_TIMING);
619                 IWL_CMD(REPLY_ADD_STA);
620                 IWL_CMD(REPLY_REMOVE_STA);
621                 IWL_CMD(REPLY_REMOVE_ALL_STA);
622                 IWL_CMD(REPLY_TX);
623                 IWL_CMD(REPLY_RATE_SCALE);
624                 IWL_CMD(REPLY_LEDS_CMD);
625                 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
626                 IWL_CMD(RADAR_NOTIFICATION);
627                 IWL_CMD(REPLY_QUIET_CMD);
628                 IWL_CMD(REPLY_CHANNEL_SWITCH);
629                 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
630                 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
631                 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
632                 IWL_CMD(POWER_TABLE_CMD);
633                 IWL_CMD(PM_SLEEP_NOTIFICATION);
634                 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
635                 IWL_CMD(REPLY_SCAN_CMD);
636                 IWL_CMD(REPLY_SCAN_ABORT_CMD);
637                 IWL_CMD(SCAN_START_NOTIFICATION);
638                 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
639                 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
640                 IWL_CMD(BEACON_NOTIFICATION);
641                 IWL_CMD(REPLY_TX_BEACON);
642                 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
643                 IWL_CMD(QUIET_NOTIFICATION);
644                 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
645                 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
646                 IWL_CMD(REPLY_BT_CONFIG);
647                 IWL_CMD(REPLY_STATISTICS_CMD);
648                 IWL_CMD(STATISTICS_NOTIFICATION);
649                 IWL_CMD(REPLY_CARD_STATE_CMD);
650                 IWL_CMD(CARD_STATE_NOTIFICATION);
651                 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
652                 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
653                 IWL_CMD(SENSITIVITY_CMD);
654                 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
655                 IWL_CMD(REPLY_RX_PHY_CMD);
656                 IWL_CMD(REPLY_RX_MPDU_CMD);
657                 IWL_CMD(REPLY_4965_RX);
658                 IWL_CMD(REPLY_COMPRESSED_BA);
659         default:
660                 return "UNKNOWN";
661
662         }
663 }
664
665 #define HOST_COMPLETE_TIMEOUT (HZ / 2)
666
667 /**
668  * iwl4965_enqueue_hcmd - enqueue a uCode command
669  * @priv: device private data point
670  * @cmd: a point to the ucode command structure
671  *
672  * The function returns < 0 values to indicate the operation is
673  * failed. On success, it turns the index (> 0) of command in the
674  * command queue.
675  */
676 static int iwl4965_enqueue_hcmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
677 {
678         struct iwl4965_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
679         struct iwl4965_queue *q = &txq->q;
680         struct iwl4965_tfd_frame *tfd;
681         u32 *control_flags;
682         struct iwl4965_cmd *out_cmd;
683         u32 idx;
684         u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
685         dma_addr_t phys_addr;
686         int ret;
687         unsigned long flags;
688
689         /* If any of the command structures end up being larger than
690          * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
691          * we will need to increase the size of the TFD entries */
692         BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
693                !(cmd->meta.flags & CMD_SIZE_HUGE));
694
695         if (iwl4965_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
696                 IWL_ERROR("No space for Tx\n");
697                 return -ENOSPC;
698         }
699
700         spin_lock_irqsave(&priv->hcmd_lock, flags);
701
702         tfd = &txq->bd[q->write_ptr];
703         memset(tfd, 0, sizeof(*tfd));
704
705         control_flags = (u32 *) tfd;
706
707         idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
708         out_cmd = &txq->cmd[idx];
709
710         out_cmd->hdr.cmd = cmd->id;
711         memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
712         memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
713
714         /* At this point, the out_cmd now has all of the incoming cmd
715          * information */
716
717         out_cmd->hdr.flags = 0;
718         out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
719                         INDEX_TO_SEQ(q->write_ptr));
720         if (out_cmd->meta.flags & CMD_SIZE_HUGE)
721                 out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
722
723         phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
724                         offsetof(struct iwl4965_cmd, hdr);
725         iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
726
727         IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
728                      "%d bytes at %d[%d]:%d\n",
729                      get_cmd_string(out_cmd->hdr.cmd),
730                      out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
731                      fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
732
733         txq->need_update = 1;
734
735         /* Set up entry in queue's byte count circular buffer */
736         ret = iwl4965_tx_queue_update_wr_ptr(priv, txq, 0);
737
738         /* Increment and update queue's write index */
739         q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
740         iwl4965_tx_queue_update_write_ptr(priv, txq);
741
742         spin_unlock_irqrestore(&priv->hcmd_lock, flags);
743         return ret ? ret : idx;
744 }
745
746 static int iwl4965_send_cmd_async(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
747 {
748         int ret;
749
750         BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
751
752         /* An asynchronous command can not expect an SKB to be set. */
753         BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
754
755         /* An asynchronous command MUST have a callback. */
756         BUG_ON(!cmd->meta.u.callback);
757
758         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
759                 return -EBUSY;
760
761         ret = iwl4965_enqueue_hcmd(priv, cmd);
762         if (ret < 0) {
763                 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
764                           get_cmd_string(cmd->id), ret);
765                 return ret;
766         }
767         return 0;
768 }
769
770 static int iwl4965_send_cmd_sync(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
771 {
772         int cmd_idx;
773         int ret;
774         static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
775
776         BUG_ON(cmd->meta.flags & CMD_ASYNC);
777
778          /* A synchronous command can not have a callback set. */
779         BUG_ON(cmd->meta.u.callback != NULL);
780
781         if (atomic_xchg(&entry, 1)) {
782                 IWL_ERROR("Error sending %s: Already sending a host command\n",
783                           get_cmd_string(cmd->id));
784                 return -EBUSY;
785         }
786
787         set_bit(STATUS_HCMD_ACTIVE, &priv->status);
788
789         if (cmd->meta.flags & CMD_WANT_SKB)
790                 cmd->meta.source = &cmd->meta;
791
792         cmd_idx = iwl4965_enqueue_hcmd(priv, cmd);
793         if (cmd_idx < 0) {
794                 ret = cmd_idx;
795                 IWL_ERROR("Error sending %s: iwl4965_enqueue_hcmd failed: %d\n",
796                           get_cmd_string(cmd->id), ret);
797                 goto out;
798         }
799
800         ret = wait_event_interruptible_timeout(priv->wait_command_queue,
801                         !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
802                         HOST_COMPLETE_TIMEOUT);
803         if (!ret) {
804                 if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
805                         IWL_ERROR("Error sending %s: time out after %dms.\n",
806                                   get_cmd_string(cmd->id),
807                                   jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
808
809                         clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
810                         ret = -ETIMEDOUT;
811                         goto cancel;
812                 }
813         }
814
815         if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
816                 IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
817                                get_cmd_string(cmd->id));
818                 ret = -ECANCELED;
819                 goto fail;
820         }
821         if (test_bit(STATUS_FW_ERROR, &priv->status)) {
822                 IWL_DEBUG_INFO("Command %s failed: FW Error\n",
823                                get_cmd_string(cmd->id));
824                 ret = -EIO;
825                 goto fail;
826         }
827         if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
828                 IWL_ERROR("Error: Response NULL in '%s'\n",
829                           get_cmd_string(cmd->id));
830                 ret = -EIO;
831                 goto out;
832         }
833
834         ret = 0;
835         goto out;
836
837 cancel:
838         if (cmd->meta.flags & CMD_WANT_SKB) {
839                 struct iwl4965_cmd *qcmd;
840
841                 /* Cancel the CMD_WANT_SKB flag for the cmd in the
842                  * TX cmd queue. Otherwise in case the cmd comes
843                  * in later, it will possibly set an invalid
844                  * address (cmd->meta.source). */
845                 qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
846                 qcmd->meta.flags &= ~CMD_WANT_SKB;
847         }
848 fail:
849         if (cmd->meta.u.skb) {
850                 dev_kfree_skb_any(cmd->meta.u.skb);
851                 cmd->meta.u.skb = NULL;
852         }
853 out:
854         atomic_set(&entry, 0);
855         return ret;
856 }
857
858 int iwl4965_send_cmd(struct iwl4965_priv *priv, struct iwl4965_host_cmd *cmd)
859 {
860         if (cmd->meta.flags & CMD_ASYNC)
861                 return iwl4965_send_cmd_async(priv, cmd);
862
863         return iwl4965_send_cmd_sync(priv, cmd);
864 }
865
866 int iwl4965_send_cmd_pdu(struct iwl4965_priv *priv, u8 id, u16 len, const void *data)
867 {
868         struct iwl4965_host_cmd cmd = {
869                 .id = id,
870                 .len = len,
871                 .data = data,
872         };
873
874         return iwl4965_send_cmd_sync(priv, &cmd);
875 }
876
877 static int __must_check iwl4965_send_cmd_u32(struct iwl4965_priv *priv, u8 id, u32 val)
878 {
879         struct iwl4965_host_cmd cmd = {
880                 .id = id,
881                 .len = sizeof(val),
882                 .data = &val,
883         };
884
885         return iwl4965_send_cmd_sync(priv, &cmd);
886 }
887
888 int iwl4965_send_statistics_request(struct iwl4965_priv *priv)
889 {
890         return iwl4965_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
891 }
892
893 /**
894  * iwl4965_rxon_add_station - add station into station table.
895  *
896  * there is only one AP station with id= IWL_AP_ID
897  * NOTE: mutex must be held before calling this fnction
898  */
899 static int iwl4965_rxon_add_station(struct iwl4965_priv *priv,
900                                 const u8 *addr, int is_ap)
901 {
902         u8 sta_id;
903
904         /* Add station to device's station table */
905 #ifdef CONFIG_IWL4965_HT
906         struct ieee80211_conf *conf = &priv->hw->conf;
907         struct ieee80211_ht_info *cur_ht_config = &conf->ht_conf;
908
909         if ((is_ap) &&
910             (conf->flags & IEEE80211_CONF_SUPPORT_HT_MODE) &&
911             (priv->iw_mode == IEEE80211_IF_TYPE_STA))
912                 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
913                                                    0, cur_ht_config);
914         else
915 #endif /* CONFIG_IWL4965_HT */
916                 sta_id = iwl4965_add_station_flags(priv, addr, is_ap,
917                                                    0, NULL);
918
919         /* Set up default rate scaling table in device's station table */
920         iwl4965_add_station(priv, addr, is_ap);
921
922         return sta_id;
923 }
924
925 /**
926  * iwl4965_set_rxon_channel - Set the phymode and channel values in staging RXON
927  * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
928  * @channel: Any channel valid for the requested phymode
929
930  * In addition to setting the staging RXON, priv->phymode is also set.
931  *
932  * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
933  * in the staging RXON flag structure based on the phymode
934  */
935 static int iwl4965_set_rxon_channel(struct iwl4965_priv *priv, u8 phymode,
936                                  u16 channel)
937 {
938         if (!iwl4965_get_channel_info(priv, phymode, channel)) {
939                 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
940                                channel, phymode);
941                 return -EINVAL;
942         }
943
944         if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
945             (priv->phymode == phymode))
946                 return 0;
947
948         priv->staging_rxon.channel = cpu_to_le16(channel);
949         if (phymode == MODE_IEEE80211A)
950                 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
951         else
952                 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
953
954         priv->phymode = phymode;
955
956         IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
957
958         return 0;
959 }
960
961 /**
962  * iwl4965_check_rxon_cmd - validate RXON structure is valid
963  *
964  * NOTE:  This is really only useful during development and can eventually
965  * be #ifdef'd out once the driver is stable and folks aren't actively
966  * making changes
967  */
968 static int iwl4965_check_rxon_cmd(struct iwl4965_rxon_cmd *rxon)
969 {
970         int error = 0;
971         int counter = 1;
972
973         if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
974                 error |= le32_to_cpu(rxon->flags &
975                                 (RXON_FLG_TGJ_NARROW_BAND_MSK |
976                                  RXON_FLG_RADAR_DETECT_MSK));
977                 if (error)
978                         IWL_WARNING("check 24G fields %d | %d\n",
979                                     counter++, error);
980         } else {
981                 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
982                                 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
983                 if (error)
984                         IWL_WARNING("check 52 fields %d | %d\n",
985                                     counter++, error);
986                 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
987                 if (error)
988                         IWL_WARNING("check 52 CCK %d | %d\n",
989                                     counter++, error);
990         }
991         error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
992         if (error)
993                 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
994
995         /* make sure basic rates 6Mbps and 1Mbps are supported */
996         error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
997                   ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
998         if (error)
999                 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
1000
1001         error |= (le16_to_cpu(rxon->assoc_id) > 2007);
1002         if (error)
1003                 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
1004
1005         error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
1006                         == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
1007         if (error)
1008                 IWL_WARNING("check CCK and short slot %d | %d\n",
1009                             counter++, error);
1010
1011         error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
1012                         == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
1013         if (error)
1014                 IWL_WARNING("check CCK & auto detect %d | %d\n",
1015                             counter++, error);
1016
1017         error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
1018                         RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
1019         if (error)
1020                 IWL_WARNING("check TGG and auto detect %d | %d\n",
1021                             counter++, error);
1022
1023         if (error)
1024                 IWL_WARNING("Tuning to channel %d\n",
1025                             le16_to_cpu(rxon->channel));
1026
1027         if (error) {
1028                 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
1029                 return -1;
1030         }
1031         return 0;
1032 }
1033
1034 /**
1035  * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
1036  * @priv: staging_rxon is compared to active_rxon
1037  *
1038  * If the RXON structure is changing enough to require a new tune,
1039  * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
1040  * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
1041  */
1042 static int iwl4965_full_rxon_required(struct iwl4965_priv *priv)
1043 {
1044
1045         /* These items are only settable from the full RXON command */
1046         if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
1047             compare_ether_addr(priv->staging_rxon.bssid_addr,
1048                                priv->active_rxon.bssid_addr) ||
1049             compare_ether_addr(priv->staging_rxon.node_addr,
1050                                priv->active_rxon.node_addr) ||
1051             compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
1052                                priv->active_rxon.wlap_bssid_addr) ||
1053             (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
1054             (priv->staging_rxon.channel != priv->active_rxon.channel) ||
1055             (priv->staging_rxon.air_propagation !=
1056              priv->active_rxon.air_propagation) ||
1057             (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
1058              priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
1059             (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
1060              priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
1061             (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
1062             (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
1063                 return 1;
1064
1065         /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
1066          * be updated with the RXON_ASSOC command -- however only some
1067          * flag transitions are allowed using RXON_ASSOC */
1068
1069         /* Check if we are not switching bands */
1070         if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
1071             (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
1072                 return 1;
1073
1074         /* Check if we are switching association toggle */
1075         if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
1076                 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
1077                 return 1;
1078
1079         return 0;
1080 }
1081
1082 static int iwl4965_send_rxon_assoc(struct iwl4965_priv *priv)
1083 {
1084         int rc = 0;
1085         struct iwl4965_rx_packet *res = NULL;
1086         struct iwl4965_rxon_assoc_cmd rxon_assoc;
1087         struct iwl4965_host_cmd cmd = {
1088                 .id = REPLY_RXON_ASSOC,
1089                 .len = sizeof(rxon_assoc),
1090                 .meta.flags = CMD_WANT_SKB,
1091                 .data = &rxon_assoc,
1092         };
1093         const struct iwl4965_rxon_cmd *rxon1 = &priv->staging_rxon;
1094         const struct iwl4965_rxon_cmd *rxon2 = &priv->active_rxon;
1095
1096         if ((rxon1->flags == rxon2->flags) &&
1097             (rxon1->filter_flags == rxon2->filter_flags) &&
1098             (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
1099             (rxon1->ofdm_ht_single_stream_basic_rates ==
1100              rxon2->ofdm_ht_single_stream_basic_rates) &&
1101             (rxon1->ofdm_ht_dual_stream_basic_rates ==
1102              rxon2->ofdm_ht_dual_stream_basic_rates) &&
1103             (rxon1->rx_chain == rxon2->rx_chain) &&
1104             (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
1105                 IWL_DEBUG_INFO("Using current RXON_ASSOC.  Not resending.\n");
1106                 return 0;
1107         }
1108
1109         rxon_assoc.flags = priv->staging_rxon.flags;
1110         rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
1111         rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
1112         rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
1113         rxon_assoc.reserved = 0;
1114         rxon_assoc.ofdm_ht_single_stream_basic_rates =
1115             priv->staging_rxon.ofdm_ht_single_stream_basic_rates;
1116         rxon_assoc.ofdm_ht_dual_stream_basic_rates =
1117             priv->staging_rxon.ofdm_ht_dual_stream_basic_rates;
1118         rxon_assoc.rx_chain_select_flags = priv->staging_rxon.rx_chain;
1119
1120         rc = iwl4965_send_cmd_sync(priv, &cmd);
1121         if (rc)
1122                 return rc;
1123
1124         res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
1125         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1126                 IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
1127                 rc = -EIO;
1128         }
1129
1130         priv->alloc_rxb_skb--;
1131         dev_kfree_skb_any(cmd.meta.u.skb);
1132
1133         return rc;
1134 }
1135
1136 /**
1137  * iwl4965_commit_rxon - commit staging_rxon to hardware
1138  *
1139  * The RXON command in staging_rxon is committed to the hardware and
1140  * the active_rxon structure is updated with the new data.  This
1141  * function correctly transitions out of the RXON_ASSOC_MSK state if
1142  * a HW tune is required based on the RXON structure changes.
1143  */
1144 static int iwl4965_commit_rxon(struct iwl4965_priv *priv)
1145 {
1146         /* cast away the const for active_rxon in this function */
1147         struct iwl4965_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
1148         DECLARE_MAC_BUF(mac);
1149         int rc = 0;
1150
1151         if (!iwl4965_is_alive(priv))
1152                 return -1;
1153
1154         /* always get timestamp with Rx frame */
1155         priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
1156
1157         rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
1158         if (rc) {
1159                 IWL_ERROR("Invalid RXON configuration.  Not committing.\n");
1160                 return -EINVAL;
1161         }
1162
1163         /* If we don't need to send a full RXON, we can use
1164          * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
1165          * and other flags for the current radio configuration. */
1166         if (!iwl4965_full_rxon_required(priv)) {
1167                 rc = iwl4965_send_rxon_assoc(priv);
1168                 if (rc) {
1169                         IWL_ERROR("Error setting RXON_ASSOC "
1170                                   "configuration (%d).\n", rc);
1171                         return rc;
1172                 }
1173
1174                 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1175
1176                 return 0;
1177         }
1178
1179         /* station table will be cleared */
1180         priv->assoc_station_added = 0;
1181
1182 #ifdef CONFIG_IWL4965_SENSITIVITY
1183         priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1184         if (!priv->error_recovering)
1185                 priv->start_calib = 0;
1186
1187         iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
1188 #endif /* CONFIG_IWL4965_SENSITIVITY */
1189
1190         /* If we are currently associated and the new config requires
1191          * an RXON_ASSOC and the new config wants the associated mask enabled,
1192          * we must clear the associated from the active configuration
1193          * before we apply the new config */
1194         if (iwl4965_is_associated(priv) &&
1195             (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
1196                 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
1197                 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1198
1199                 rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1200                                       sizeof(struct iwl4965_rxon_cmd),
1201                                       &priv->active_rxon);
1202
1203                 /* If the mask clearing failed then we set
1204                  * active_rxon back to what it was previously */
1205                 if (rc) {
1206                         active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
1207                         IWL_ERROR("Error clearing ASSOC_MSK on current "
1208                                   "configuration (%d).\n", rc);
1209                         return rc;
1210                 }
1211         }
1212
1213         IWL_DEBUG_INFO("Sending RXON\n"
1214                        "* with%s RXON_FILTER_ASSOC_MSK\n"
1215                        "* channel = %d\n"
1216                        "* bssid = %s\n",
1217                        ((priv->staging_rxon.filter_flags &
1218                          RXON_FILTER_ASSOC_MSK) ? "" : "out"),
1219                        le16_to_cpu(priv->staging_rxon.channel),
1220                        print_mac(mac, priv->staging_rxon.bssid_addr));
1221
1222         /* Apply the new configuration */
1223         rc = iwl4965_send_cmd_pdu(priv, REPLY_RXON,
1224                               sizeof(struct iwl4965_rxon_cmd), &priv->staging_rxon);
1225         if (rc) {
1226                 IWL_ERROR("Error setting new configuration (%d).\n", rc);
1227                 return rc;
1228         }
1229
1230         iwl4965_clear_stations_table(priv);
1231
1232 #ifdef CONFIG_IWL4965_SENSITIVITY
1233         if (!priv->error_recovering)
1234                 priv->start_calib = 0;
1235
1236         priv->sensitivity_data.state = IWL_SENS_CALIB_NEED_REINIT;
1237         iwl4965_init_sensitivity(priv, CMD_ASYNC, 1);
1238 #endif /* CONFIG_IWL4965_SENSITIVITY */
1239
1240         memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
1241
1242         /* If we issue a new RXON command which required a tune then we must
1243          * send a new TXPOWER command or we won't be able to Tx any frames */
1244         rc = iwl4965_hw_reg_send_txpower(priv);
1245         if (rc) {
1246                 IWL_ERROR("Error setting Tx power (%d).\n", rc);
1247                 return rc;
1248         }
1249
1250         /* Add the broadcast address so we can send broadcast frames */
1251         if (iwl4965_rxon_add_station(priv, iwl4965_broadcast_addr, 0) ==
1252             IWL_INVALID_STATION) {
1253                 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
1254                 return -EIO;
1255         }
1256
1257         /* If we have set the ASSOC_MSK and we are in BSS mode then
1258          * add the IWL_AP_ID to the station rate table */
1259         if (iwl4965_is_associated(priv) &&
1260             (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
1261                 if (iwl4965_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
1262                     == IWL_INVALID_STATION) {
1263                         IWL_ERROR("Error adding AP address for transmit.\n");
1264                         return -EIO;
1265                 }
1266                 priv->assoc_station_added = 1;
1267         }
1268
1269         return 0;
1270 }
1271
1272 static int iwl4965_send_bt_config(struct iwl4965_priv *priv)
1273 {
1274         struct iwl4965_bt_cmd bt_cmd = {
1275                 .flags = 3,
1276                 .lead_time = 0xAA,
1277                 .max_kill = 1,
1278                 .kill_ack_mask = 0,
1279                 .kill_cts_mask = 0,
1280         };
1281
1282         return iwl4965_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1283                                 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
1284 }
1285
1286 static int iwl4965_send_scan_abort(struct iwl4965_priv *priv)
1287 {
1288         int rc = 0;
1289         struct iwl4965_rx_packet *res;
1290         struct iwl4965_host_cmd cmd = {
1291                 .id = REPLY_SCAN_ABORT_CMD,
1292                 .meta.flags = CMD_WANT_SKB,
1293         };
1294
1295         /* If there isn't a scan actively going on in the hardware
1296          * then we are in between scan bands and not actually
1297          * actively scanning, so don't send the abort command */
1298         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
1299                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1300                 return 0;
1301         }
1302
1303         rc = iwl4965_send_cmd_sync(priv, &cmd);
1304         if (rc) {
1305                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1306                 return rc;
1307         }
1308
1309         res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
1310         if (res->u.status != CAN_ABORT_STATUS) {
1311                 /* The scan abort will return 1 for success or
1312                  * 2 for "failure".  A failure condition can be
1313                  * due to simply not being in an active scan which
1314                  * can occur if we send the scan abort before we
1315                  * the microcode has notified us that a scan is
1316                  * completed. */
1317                 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
1318                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
1319                 clear_bit(STATUS_SCAN_HW, &priv->status);
1320         }
1321
1322         dev_kfree_skb_any(cmd.meta.u.skb);
1323
1324         return rc;
1325 }
1326
1327 static int iwl4965_card_state_sync_callback(struct iwl4965_priv *priv,
1328                                         struct iwl4965_cmd *cmd,
1329                                         struct sk_buff *skb)
1330 {
1331         return 1;
1332 }
1333
1334 /*
1335  * CARD_STATE_CMD
1336  *
1337  * Use: Sets the device's internal card state to enable, disable, or halt
1338  *
1339  * When in the 'enable' state the card operates as normal.
1340  * When in the 'disable' state, the card enters into a low power mode.
1341  * When in the 'halt' state, the card is shut down and must be fully
1342  * restarted to come back on.
1343  */
1344 static int iwl4965_send_card_state(struct iwl4965_priv *priv, u32 flags, u8 meta_flag)
1345 {
1346         struct iwl4965_host_cmd cmd = {
1347                 .id = REPLY_CARD_STATE_CMD,
1348                 .len = sizeof(u32),
1349                 .data = &flags,
1350                 .meta.flags = meta_flag,
1351         };
1352
1353         if (meta_flag & CMD_ASYNC)
1354                 cmd.meta.u.callback = iwl4965_card_state_sync_callback;
1355
1356         return iwl4965_send_cmd(priv, &cmd);
1357 }
1358
1359 static int iwl4965_add_sta_sync_callback(struct iwl4965_priv *priv,
1360                                      struct iwl4965_cmd *cmd, struct sk_buff *skb)
1361 {
1362         struct iwl4965_rx_packet *res = NULL;
1363
1364         if (!skb) {
1365                 IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
1366                 return 1;
1367         }
1368
1369         res = (struct iwl4965_rx_packet *)skb->data;
1370         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1371                 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1372                           res->hdr.flags);
1373                 return 1;
1374         }
1375
1376         switch (res->u.add_sta.status) {
1377         case ADD_STA_SUCCESS_MSK:
1378                 break;
1379         default:
1380                 break;
1381         }
1382
1383         /* We didn't cache the SKB; let the caller free it */
1384         return 1;
1385 }
1386
1387 int iwl4965_send_add_station(struct iwl4965_priv *priv,
1388                          struct iwl4965_addsta_cmd *sta, u8 flags)
1389 {
1390         struct iwl4965_rx_packet *res = NULL;
1391         int rc = 0;
1392         struct iwl4965_host_cmd cmd = {
1393                 .id = REPLY_ADD_STA,
1394                 .len = sizeof(struct iwl4965_addsta_cmd),
1395                 .meta.flags = flags,
1396                 .data = sta,
1397         };
1398
1399         if (flags & CMD_ASYNC)
1400                 cmd.meta.u.callback = iwl4965_add_sta_sync_callback;
1401         else
1402                 cmd.meta.flags |= CMD_WANT_SKB;
1403
1404         rc = iwl4965_send_cmd(priv, &cmd);
1405
1406         if (rc || (flags & CMD_ASYNC))
1407                 return rc;
1408
1409         res = (struct iwl4965_rx_packet *)cmd.meta.u.skb->data;
1410         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1411                 IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
1412                           res->hdr.flags);
1413                 rc = -EIO;
1414         }
1415
1416         if (rc == 0) {
1417                 switch (res->u.add_sta.status) {
1418                 case ADD_STA_SUCCESS_MSK:
1419                         IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
1420                         break;
1421                 default:
1422                         rc = -EIO;
1423                         IWL_WARNING("REPLY_ADD_STA failed\n");
1424                         break;
1425                 }
1426         }
1427
1428         priv->alloc_rxb_skb--;
1429         dev_kfree_skb_any(cmd.meta.u.skb);
1430
1431         return rc;
1432 }
1433
1434 static int iwl4965_update_sta_key_info(struct iwl4965_priv *priv,
1435                                    struct ieee80211_key_conf *keyconf,
1436                                    u8 sta_id)
1437 {
1438         unsigned long flags;
1439         __le16 key_flags = 0;
1440
1441         switch (keyconf->alg) {
1442         case ALG_CCMP:
1443                 key_flags |= STA_KEY_FLG_CCMP;
1444                 key_flags |= cpu_to_le16(
1445                                 keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1446                 key_flags &= ~STA_KEY_FLG_INVALID;
1447                 break;
1448         case ALG_TKIP:
1449         case ALG_WEP:
1450         default:
1451                 return -EINVAL;
1452         }
1453         spin_lock_irqsave(&priv->sta_lock, flags);
1454         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
1455         priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
1456         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
1457                keyconf->keylen);
1458
1459         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
1460                keyconf->keylen);
1461         priv->stations[sta_id].sta.key.key_flags = key_flags;
1462         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1463         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1464
1465         spin_unlock_irqrestore(&priv->sta_lock, flags);
1466
1467         IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
1468         iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1469         return 0;
1470 }
1471
1472 static int iwl4965_clear_sta_key_info(struct iwl4965_priv *priv, u8 sta_id)
1473 {
1474         unsigned long flags;
1475
1476         spin_lock_irqsave(&priv->sta_lock, flags);
1477         memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl4965_hw_key));
1478         memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl4965_keyinfo));
1479         priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
1480         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1481         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1482         spin_unlock_irqrestore(&priv->sta_lock, flags);
1483
1484         IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
1485         iwl4965_send_add_station(priv, &priv->stations[sta_id].sta, 0);
1486         return 0;
1487 }
1488
1489 static void iwl4965_clear_free_frames(struct iwl4965_priv *priv)
1490 {
1491         struct list_head *element;
1492
1493         IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
1494                        priv->frames_count);
1495
1496         while (!list_empty(&priv->free_frames)) {
1497                 element = priv->free_frames.next;
1498                 list_del(element);
1499                 kfree(list_entry(element, struct iwl4965_frame, list));
1500                 priv->frames_count--;
1501         }
1502
1503         if (priv->frames_count) {
1504                 IWL_WARNING("%d frames still in use.  Did we lose one?\n",
1505                             priv->frames_count);
1506                 priv->frames_count = 0;
1507         }
1508 }
1509
1510 static struct iwl4965_frame *iwl4965_get_free_frame(struct iwl4965_priv *priv)
1511 {
1512         struct iwl4965_frame *frame;
1513         struct list_head *element;
1514         if (list_empty(&priv->free_frames)) {
1515                 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
1516                 if (!frame) {
1517                         IWL_ERROR("Could not allocate frame!\n");
1518                         return NULL;
1519                 }
1520
1521                 priv->frames_count++;
1522                 return frame;
1523         }
1524
1525         element = priv->free_frames.next;
1526         list_del(element);
1527         return list_entry(element, struct iwl4965_frame, list);
1528 }
1529
1530 static void iwl4965_free_frame(struct iwl4965_priv *priv, struct iwl4965_frame *frame)
1531 {
1532         memset(frame, 0, sizeof(*frame));
1533         list_add(&frame->list, &priv->free_frames);
1534 }
1535
1536 unsigned int iwl4965_fill_beacon_frame(struct iwl4965_priv *priv,
1537                                 struct ieee80211_hdr *hdr,
1538                                 const u8 *dest, int left)
1539 {
1540
1541         if (!iwl4965_is_associated(priv) || !priv->ibss_beacon ||
1542             ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
1543              (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
1544                 return 0;
1545
1546         if (priv->ibss_beacon->len > left)
1547                 return 0;
1548
1549         memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
1550
1551         return priv->ibss_beacon->len;
1552 }
1553
1554 int iwl4965_rate_index_from_plcp(int plcp)
1555 {
1556         int i = 0;
1557
1558         /* 4965 HT rate format */
1559         if (plcp & RATE_MCS_HT_MSK) {
1560                 i = (plcp & 0xff);
1561
1562                 if (i >= IWL_RATE_MIMO_6M_PLCP)
1563                         i = i - IWL_RATE_MIMO_6M_PLCP;
1564
1565                 i += IWL_FIRST_OFDM_RATE;
1566                 /* skip 9M not supported in ht*/
1567                 if (i >= IWL_RATE_9M_INDEX)
1568                         i += 1;
1569                 if ((i >= IWL_FIRST_OFDM_RATE) &&
1570                     (i <= IWL_LAST_OFDM_RATE))
1571                         return i;
1572
1573         /* 4965 legacy rate format, search for match in table */
1574         } else {
1575                 for (i = 0; i < ARRAY_SIZE(iwl4965_rates); i++)
1576                         if (iwl4965_rates[i].plcp == (plcp &0xFF))
1577                                 return i;
1578         }
1579         return -1;
1580 }
1581
1582 static u8 iwl4965_rate_get_lowest_plcp(int rate_mask)
1583 {
1584         u8 i;
1585
1586         for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
1587              i = iwl4965_rates[i].next_ieee) {
1588                 if (rate_mask & (1 << i))
1589                         return iwl4965_rates[i].plcp;
1590         }
1591
1592         return IWL_RATE_INVALID;
1593 }
1594
1595 static int iwl4965_send_beacon_cmd(struct iwl4965_priv *priv)
1596 {
1597         struct iwl4965_frame *frame;
1598         unsigned int frame_size;
1599         int rc;
1600         u8 rate;
1601
1602         frame = iwl4965_get_free_frame(priv);
1603
1604         if (!frame) {
1605                 IWL_ERROR("Could not obtain free frame buffer for beacon "
1606                           "command.\n");
1607                 return -ENOMEM;
1608         }
1609
1610         if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
1611                 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic &
1612                                                 0xFF0);
1613                 if (rate == IWL_INVALID_RATE)
1614                         rate = IWL_RATE_6M_PLCP;
1615         } else {
1616                 rate = iwl4965_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
1617                 if (rate == IWL_INVALID_RATE)
1618                         rate = IWL_RATE_1M_PLCP;
1619         }
1620
1621         frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
1622
1623         rc = iwl4965_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
1624                               &frame->u.cmd[0]);
1625
1626         iwl4965_free_frame(priv, frame);
1627
1628         return rc;
1629 }
1630
1631 /******************************************************************************
1632  *
1633  * EEPROM related functions
1634  *
1635  ******************************************************************************/
1636
1637 static void get_eeprom_mac(struct iwl4965_priv *priv, u8 *mac)
1638 {
1639         memcpy(mac, priv->eeprom.mac_address, 6);
1640 }
1641
1642 static inline void iwl4965_eeprom_release_semaphore(struct iwl4965_priv *priv)
1643 {
1644         iwl4965_clear_bit(priv, CSR_HW_IF_CONFIG_REG,
1645                 CSR_HW_IF_CONFIG_REG_BIT_EEPROM_OWN_SEM);
1646 }
1647
1648 /**
1649  * iwl4965_eeprom_init - read EEPROM contents
1650  *
1651  * Load the EEPROM contents from adapter into priv->eeprom
1652  *
1653  * NOTE:  This routine uses the non-debug IO access functions.
1654  */
1655 int iwl4965_eeprom_init(struct iwl4965_priv *priv)
1656 {
1657         __le16 *e = (__le16 *)&priv->eeprom;
1658         u32 gp = iwl4965_read32(priv, CSR_EEPROM_GP);
1659         u32 r;
1660         int sz = sizeof(priv->eeprom);
1661         int rc;
1662         int i;
1663         u16 addr;
1664
1665         /* The EEPROM structure has several padding buffers within it
1666          * and when adding new EEPROM maps is subject to programmer errors
1667          * which may be very difficult to identify without explicitly
1668          * checking the resulting size of the eeprom map. */
1669         BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1670
1671         if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1672                 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1673                 return -ENOENT;
1674         }
1675
1676         /* Make sure driver (instead of uCode) is allowed to read EEPROM */
1677         rc = iwl4965_eeprom_acquire_semaphore(priv);
1678         if (rc < 0) {
1679                 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
1680                 return -ENOENT;
1681         }
1682
1683         /* eeprom is an array of 16bit values */
1684         for (addr = 0; addr < sz; addr += sizeof(u16)) {
1685                 _iwl4965_write32(priv, CSR_EEPROM_REG, addr << 1);
1686                 _iwl4965_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1687
1688                 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1689                                         i += IWL_EEPROM_ACCESS_DELAY) {
1690                         r = _iwl4965_read_direct32(priv, CSR_EEPROM_REG);
1691                         if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1692                                 break;
1693                         udelay(IWL_EEPROM_ACCESS_DELAY);
1694                 }
1695
1696                 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1697                         IWL_ERROR("Time out reading EEPROM[%d]", addr);
1698                         rc = -ETIMEDOUT;
1699                         goto done;
1700                 }
1701                 e[addr / 2] = cpu_to_le16(r >> 16);
1702         }
1703         rc = 0;
1704
1705 done:
1706         iwl4965_eeprom_release_semaphore(priv);
1707         return rc;
1708 }
1709
1710 /******************************************************************************
1711  *
1712  * Misc. internal state and helper functions
1713  *
1714  ******************************************************************************/
1715 #ifdef CONFIG_IWL4965_DEBUG
1716
1717 /**
1718  * iwl4965_report_frame - dump frame to syslog during debug sessions
1719  *
1720  * You may hack this function to show different aspects of received frames,
1721  * including selective frame dumps.
1722  * group100 parameter selects whether to show 1 out of 100 good frames.
1723  *
1724  * TODO:  This was originally written for 3945, need to audit for
1725  *        proper operation with 4965.
1726  */
1727 void iwl4965_report_frame(struct iwl4965_priv *priv,
1728                       struct iwl4965_rx_packet *pkt,
1729                       struct ieee80211_hdr *header, int group100)
1730 {
1731         u32 to_us;
1732         u32 print_summary = 0;
1733         u32 print_dump = 0;     /* set to 1 to dump all frames' contents */
1734         u32 hundred = 0;
1735         u32 dataframe = 0;
1736         u16 fc;
1737         u16 seq_ctl;
1738         u16 channel;
1739         u16 phy_flags;
1740         int rate_sym;
1741         u16 length;
1742         u16 status;
1743         u16 bcn_tmr;
1744         u32 tsf_low;
1745         u64 tsf;
1746         u8 rssi;
1747         u8 agc;
1748         u16 sig_avg;
1749         u16 noise_diff;
1750         struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1751         struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1752         struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
1753         u8 *data = IWL_RX_DATA(pkt);
1754
1755         /* MAC header */
1756         fc = le16_to_cpu(header->frame_control);
1757         seq_ctl = le16_to_cpu(header->seq_ctrl);
1758
1759         /* metadata */
1760         channel = le16_to_cpu(rx_hdr->channel);
1761         phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1762         rate_sym = rx_hdr->rate;
1763         length = le16_to_cpu(rx_hdr->len);
1764
1765         /* end-of-frame status and timestamp */
1766         status = le32_to_cpu(rx_end->status);
1767         bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1768         tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1769         tsf = le64_to_cpu(rx_end->timestamp);
1770
1771         /* signal statistics */
1772         rssi = rx_stats->rssi;
1773         agc = rx_stats->agc;
1774         sig_avg = le16_to_cpu(rx_stats->sig_avg);
1775         noise_diff = le16_to_cpu(rx_stats->noise_diff);
1776
1777         to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1778
1779         /* if data frame is to us and all is good,
1780          *   (optionally) print summary for only 1 out of every 100 */
1781         if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1782             (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1783                 dataframe = 1;
1784                 if (!group100)
1785                         print_summary = 1;      /* print each frame */
1786                 else if (priv->framecnt_to_us < 100) {
1787                         priv->framecnt_to_us++;
1788                         print_summary = 0;
1789                 } else {
1790                         priv->framecnt_to_us = 0;
1791                         print_summary = 1;
1792                         hundred = 1;
1793                 }
1794         } else {
1795                 /* print summary for all other frames */
1796                 print_summary = 1;
1797         }
1798
1799         if (print_summary) {
1800                 char *title;
1801                 u32 rate;
1802
1803                 if (hundred)
1804                         title = "100Frames";
1805                 else if (fc & IEEE80211_FCTL_RETRY)
1806                         title = "Retry";
1807                 else if (ieee80211_is_assoc_response(fc))
1808                         title = "AscRsp";
1809                 else if (ieee80211_is_reassoc_response(fc))
1810                         title = "RasRsp";
1811                 else if (ieee80211_is_probe_response(fc)) {
1812                         title = "PrbRsp";
1813                         print_dump = 1; /* dump frame contents */
1814                 } else if (ieee80211_is_beacon(fc)) {
1815                         title = "Beacon";
1816                         print_dump = 1; /* dump frame contents */
1817                 } else if (ieee80211_is_atim(fc))
1818                         title = "ATIM";
1819                 else if (ieee80211_is_auth(fc))
1820                         title = "Auth";
1821                 else if (ieee80211_is_deauth(fc))
1822                         title = "DeAuth";
1823                 else if (ieee80211_is_disassoc(fc))
1824                         title = "DisAssoc";
1825                 else
1826                         title = "Frame";
1827
1828                 rate = iwl4965_rate_index_from_plcp(rate_sym);
1829                 if (rate == -1)
1830                         rate = 0;
1831                 else
1832                         rate = iwl4965_rates[rate].ieee / 2;
1833
1834                 /* print frame summary.
1835                  * MAC addresses show just the last byte (for brevity),
1836                  *    but you can hack it to show more, if you'd like to. */
1837                 if (dataframe)
1838                         IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1839                                      "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1840                                      title, fc, header->addr1[5],
1841                                      length, rssi, channel, rate);
1842                 else {
1843                         /* src/dst addresses assume managed mode */
1844                         IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1845                                      "src=0x%02x, rssi=%u, tim=%lu usec, "
1846                                      "phy=0x%02x, chnl=%d\n",
1847                                      title, fc, header->addr1[5],
1848                                      header->addr3[5], rssi,
1849                                      tsf_low - priv->scan_start_tsf,
1850                                      phy_flags, channel);
1851                 }
1852         }
1853         if (print_dump)
1854                 iwl4965_print_hex_dump(IWL_DL_RX, data, length);
1855 }
1856 #endif
1857
1858 static void iwl4965_unset_hw_setting(struct iwl4965_priv *priv)
1859 {
1860         if (priv->hw_setting.shared_virt)
1861                 pci_free_consistent(priv->pci_dev,
1862                                     sizeof(struct iwl4965_shared),
1863                                     priv->hw_setting.shared_virt,
1864                                     priv->hw_setting.shared_phys);
1865 }
1866
1867 /**
1868  * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
1869  *
1870  * return : set the bit for each supported rate insert in ie
1871  */
1872 static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
1873                                     u16 basic_rate, int *left)
1874 {
1875         u16 ret_rates = 0, bit;
1876         int i;
1877         u8 *cnt = ie;
1878         u8 *rates = ie + 1;
1879
1880         for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1881                 if (bit & supported_rate) {
1882                         ret_rates |= bit;
1883                         rates[*cnt] = iwl4965_rates[i].ieee |
1884                                 ((bit & basic_rate) ? 0x80 : 0x00);
1885                         (*cnt)++;
1886                         (*left)--;
1887                         if ((*left <= 0) ||
1888                             (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
1889                                 break;
1890                 }
1891         }
1892
1893         return ret_rates;
1894 }
1895
1896 #ifdef CONFIG_IWL4965_HT
1897 void static iwl4965_set_ht_capab(struct ieee80211_hw *hw,
1898                              struct ieee80211_ht_cap *ht_cap,
1899                              u8 use_current_config);
1900 #endif
1901
1902 /**
1903  * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
1904  */
1905 static u16 iwl4965_fill_probe_req(struct iwl4965_priv *priv,
1906                               struct ieee80211_mgmt *frame,
1907                               int left, int is_direct)
1908 {
1909         int len = 0;
1910         u8 *pos = NULL;
1911         u16 active_rates, ret_rates, cck_rates, active_rate_basic;
1912 #ifdef CONFIG_IWL4965_HT
1913         struct ieee80211_hw_mode *mode;
1914 #endif /* CONFIG_IWL4965_HT */
1915
1916         /* Make sure there is enough space for the probe request,
1917          * two mandatory IEs and the data */
1918         left -= 24;
1919         if (left < 0)
1920                 return 0;
1921         len += 24;
1922
1923         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1924         memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
1925         memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
1926         memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
1927         frame->seq_ctrl = 0;
1928
1929         /* fill in our indirect SSID IE */
1930         /* ...next IE... */
1931
1932         left -= 2;
1933         if (left < 0)
1934                 return 0;
1935         len += 2;
1936         pos = &(frame->u.probe_req.variable[0]);
1937         *pos++ = WLAN_EID_SSID;
1938         *pos++ = 0;
1939
1940         /* fill in our direct SSID IE... */
1941         if (is_direct) {
1942                 /* ...next IE... */
1943                 left -= 2 + priv->essid_len;
1944                 if (left < 0)
1945                         return 0;
1946                 /* ... fill it in... */
1947                 *pos++ = WLAN_EID_SSID;
1948                 *pos++ = priv->essid_len;
1949                 memcpy(pos, priv->essid, priv->essid_len);
1950                 pos += priv->essid_len;
1951                 len += 2 + priv->essid_len;
1952         }
1953
1954         /* fill in supported rate */
1955         /* ...next IE... */
1956         left -= 2;
1957         if (left < 0)
1958                 return 0;
1959
1960         /* ... fill it in... */
1961         *pos++ = WLAN_EID_SUPP_RATES;
1962         *pos = 0;
1963
1964         /* exclude 60M rate */
1965         active_rates = priv->rates_mask;
1966         active_rates &= ~IWL_RATE_60M_MASK;
1967
1968         active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
1969
1970         cck_rates = IWL_CCK_RATES_MASK & active_rates;
1971         ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
1972                         active_rate_basic, &left);
1973         active_rates &= ~ret_rates;
1974
1975         ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
1976                                  active_rate_basic, &left);
1977         active_rates &= ~ret_rates;
1978
1979         len += 2 + *pos;
1980         pos += (*pos) + 1;
1981         if (active_rates == 0)
1982                 goto fill_end;
1983
1984         /* fill in supported extended rate */
1985         /* ...next IE... */
1986         left -= 2;
1987         if (left < 0)
1988                 return 0;
1989         /* ... fill it in... */
1990         *pos++ = WLAN_EID_EXT_SUPP_RATES;
1991         *pos = 0;
1992         iwl4965_supported_rate_to_ie(pos, active_rates,
1993                                  active_rate_basic, &left);
1994         if (*pos > 0)
1995                 len += 2 + *pos;
1996
1997 #ifdef CONFIG_IWL4965_HT
1998         mode = priv->hw->conf.mode;
1999         if (mode->ht_info.ht_supported) {
2000                 pos += (*pos) + 1;
2001                 *pos++ = WLAN_EID_HT_CAPABILITY;
2002                 *pos++ = sizeof(struct ieee80211_ht_cap);
2003                 iwl4965_set_ht_capab(priv->hw,
2004                                 (struct ieee80211_ht_cap *)pos, 0);
2005                 len += 2 + sizeof(struct ieee80211_ht_cap);
2006         }
2007 #endif  /*CONFIG_IWL4965_HT */
2008
2009  fill_end:
2010         return (u16)len;
2011 }
2012
2013 /*
2014  * QoS  support
2015 */
2016 #ifdef CONFIG_IWL4965_QOS
2017 static int iwl4965_send_qos_params_command(struct iwl4965_priv *priv,
2018                                        struct iwl4965_qosparam_cmd *qos)
2019 {
2020
2021         return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
2022                                 sizeof(struct iwl4965_qosparam_cmd), qos);
2023 }
2024
2025 static void iwl4965_reset_qos(struct iwl4965_priv *priv)
2026 {
2027         u16 cw_min = 15;
2028         u16 cw_max = 1023;
2029         u8 aifs = 2;
2030         u8 is_legacy = 0;
2031         unsigned long flags;
2032         int i;
2033
2034         spin_lock_irqsave(&priv->lock, flags);
2035         priv->qos_data.qos_active = 0;
2036
2037         if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
2038                 if (priv->qos_data.qos_enable)
2039                         priv->qos_data.qos_active = 1;
2040                 if (!(priv->active_rate & 0xfff0)) {
2041                         cw_min = 31;
2042                         is_legacy = 1;
2043                 }
2044         } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2045                 if (priv->qos_data.qos_enable)
2046                         priv->qos_data.qos_active = 1;
2047         } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
2048                 cw_min = 31;
2049                 is_legacy = 1;
2050         }
2051
2052         if (priv->qos_data.qos_active)
2053                 aifs = 3;
2054
2055         priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
2056         priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
2057         priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
2058         priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
2059         priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
2060
2061         if (priv->qos_data.qos_active) {
2062                 i = 1;
2063                 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
2064                 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
2065                 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
2066                 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2067                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2068
2069                 i = 2;
2070                 priv->qos_data.def_qos_parm.ac[i].cw_min =
2071                         cpu_to_le16((cw_min + 1) / 2 - 1);
2072                 priv->qos_data.def_qos_parm.ac[i].cw_max =
2073                         cpu_to_le16(cw_max);
2074                 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2075                 if (is_legacy)
2076                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
2077                                 cpu_to_le16(6016);
2078                 else
2079                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
2080                                 cpu_to_le16(3008);
2081                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2082
2083                 i = 3;
2084                 priv->qos_data.def_qos_parm.ac[i].cw_min =
2085                         cpu_to_le16((cw_min + 1) / 4 - 1);
2086                 priv->qos_data.def_qos_parm.ac[i].cw_max =
2087                         cpu_to_le16((cw_max + 1) / 2 - 1);
2088                 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2089                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2090                 if (is_legacy)
2091                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
2092                                 cpu_to_le16(3264);
2093                 else
2094                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
2095                                 cpu_to_le16(1504);
2096         } else {
2097                 for (i = 1; i < 4; i++) {
2098                         priv->qos_data.def_qos_parm.ac[i].cw_min =
2099                                 cpu_to_le16(cw_min);
2100                         priv->qos_data.def_qos_parm.ac[i].cw_max =
2101                                 cpu_to_le16(cw_max);
2102                         priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
2103                         priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2104                         priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2105                 }
2106         }
2107         IWL_DEBUG_QOS("set QoS to default \n");
2108
2109         spin_unlock_irqrestore(&priv->lock, flags);
2110 }
2111
2112 static void iwl4965_activate_qos(struct iwl4965_priv *priv, u8 force)
2113 {
2114         unsigned long flags;
2115
2116         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2117                 return;
2118
2119         if (!priv->qos_data.qos_enable)
2120                 return;
2121
2122         spin_lock_irqsave(&priv->lock, flags);
2123         priv->qos_data.def_qos_parm.qos_flags = 0;
2124
2125         if (priv->qos_data.qos_cap.q_AP.queue_request &&
2126             !priv->qos_data.qos_cap.q_AP.txop_request)
2127                 priv->qos_data.def_qos_parm.qos_flags |=
2128                         QOS_PARAM_FLG_TXOP_TYPE_MSK;
2129         if (priv->qos_data.qos_active)
2130                 priv->qos_data.def_qos_parm.qos_flags |=
2131                         QOS_PARAM_FLG_UPDATE_EDCA_MSK;
2132
2133 #ifdef CONFIG_IWL4965_HT
2134         if (priv->current_ht_config.is_ht)
2135                 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
2136 #endif /* CONFIG_IWL4965_HT */
2137
2138         spin_unlock_irqrestore(&priv->lock, flags);
2139
2140         if (force || iwl4965_is_associated(priv)) {
2141                 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
2142                                 priv->qos_data.qos_active,
2143                                 priv->qos_data.def_qos_parm.qos_flags);
2144
2145                 iwl4965_send_qos_params_command(priv,
2146                                 &(priv->qos_data.def_qos_parm));
2147         }
2148 }
2149
2150 #endif /* CONFIG_IWL4965_QOS */
2151 /*
2152  * Power management (not Tx power!) functions
2153  */
2154 #define MSEC_TO_USEC 1024
2155
2156 #define NOSLP __constant_cpu_to_le16(0), 0, 0
2157 #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
2158 #define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2159 #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2160                                      __constant_cpu_to_le32(X1), \
2161                                      __constant_cpu_to_le32(X2), \
2162                                      __constant_cpu_to_le32(X3), \
2163                                      __constant_cpu_to_le32(X4)}
2164
2165
2166 /* default power management (not Tx power) table values */
2167 /* for tim  0-10 */
2168 static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
2169         {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2170         {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2171         {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2172         {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2173         {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2174         {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2175 };
2176
2177 /* for tim > 10 */
2178 static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
2179         {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2180         {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2181                  SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2182         {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2183                  SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2184         {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2185                  SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2186         {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2187         {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2188                  SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2189 };
2190
2191 int iwl4965_power_init_handle(struct iwl4965_priv *priv)
2192 {
2193         int rc = 0, i;
2194         struct iwl4965_power_mgr *pow_data;
2195         int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
2196         u16 pci_pm;
2197
2198         IWL_DEBUG_POWER("Initialize power \n");
2199
2200         pow_data = &(priv->power_data);
2201
2202         memset(pow_data, 0, sizeof(*pow_data));
2203
2204         pow_data->active_index = IWL_POWER_RANGE_0;
2205         pow_data->dtim_val = 0xffff;
2206
2207         memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2208         memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2209
2210         rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2211         if (rc != 0)
2212                 return 0;
2213         else {
2214                 struct iwl4965_powertable_cmd *cmd;
2215
2216                 IWL_DEBUG_POWER("adjust power command flags\n");
2217
2218                 for (i = 0; i < IWL_POWER_AC; i++) {
2219                         cmd = &pow_data->pwr_range_0[i].cmd;
2220
2221                         if (pci_pm & 0x1)
2222                                 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2223                         else
2224                                 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2225                 }
2226         }
2227         return rc;
2228 }
2229
2230 static int iwl4965_update_power_cmd(struct iwl4965_priv *priv,
2231                                 struct iwl4965_powertable_cmd *cmd, u32 mode)
2232 {
2233         int rc = 0, i;
2234         u8 skip;
2235         u32 max_sleep = 0;
2236         struct iwl4965_power_vec_entry *range;
2237         u8 period = 0;
2238         struct iwl4965_power_mgr *pow_data;
2239
2240         if (mode > IWL_POWER_INDEX_5) {
2241                 IWL_DEBUG_POWER("Error invalid power mode \n");
2242                 return -1;
2243         }
2244         pow_data = &(priv->power_data);
2245
2246         if (pow_data->active_index == IWL_POWER_RANGE_0)
2247                 range = &pow_data->pwr_range_0[0];
2248         else
2249                 range = &pow_data->pwr_range_1[1];
2250
2251         memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
2252
2253 #ifdef IWL_MAC80211_DISABLE
2254         if (priv->assoc_network != NULL) {
2255                 unsigned long flags;
2256
2257                 period = priv->assoc_network->tim.tim_period;
2258         }
2259 #endif  /*IWL_MAC80211_DISABLE */
2260         skip = range[mode].no_dtim;
2261
2262         if (period == 0) {
2263                 period = 1;
2264                 skip = 0;
2265         }
2266
2267         if (skip == 0) {
2268                 max_sleep = period;
2269                 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2270         } else {
2271                 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2272                 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2273                 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2274         }
2275
2276         for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2277                 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2278                         cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2279         }
2280
2281         IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2282         IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2283         IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2284         IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2285                         le32_to_cpu(cmd->sleep_interval[0]),
2286                         le32_to_cpu(cmd->sleep_interval[1]),
2287                         le32_to_cpu(cmd->sleep_interval[2]),
2288                         le32_to_cpu(cmd->sleep_interval[3]),
2289                         le32_to_cpu(cmd->sleep_interval[4]));
2290
2291         return rc;
2292 }
2293
2294 static int iwl4965_send_power_mode(struct iwl4965_priv *priv, u32 mode)
2295 {
2296         u32 uninitialized_var(final_mode);
2297         int rc;
2298         struct iwl4965_powertable_cmd cmd;
2299
2300         /* If on battery, set to 3,
2301          * if plugged into AC power, set to CAM ("continuously aware mode"),
2302          * else user level */
2303         switch (mode) {
2304         case IWL_POWER_BATTERY:
2305                 final_mode = IWL_POWER_INDEX_3;
2306                 break;
2307         case IWL_POWER_AC:
2308                 final_mode = IWL_POWER_MODE_CAM;
2309                 break;
2310         default:
2311                 final_mode = mode;
2312                 break;
2313         }
2314
2315         cmd.keep_alive_beacons = 0;
2316
2317         iwl4965_update_power_cmd(priv, &cmd, final_mode);
2318
2319         rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
2320
2321         if (final_mode == IWL_POWER_MODE_CAM)
2322                 clear_bit(STATUS_POWER_PMI, &priv->status);
2323         else
2324                 set_bit(STATUS_POWER_PMI, &priv->status);
2325
2326         return rc;
2327 }
2328
2329 int iwl4965_is_network_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
2330 {
2331         /* Filter incoming packets to determine if they are targeted toward
2332          * this network, discarding packets coming from ourselves */
2333         switch (priv->iw_mode) {
2334         case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source    | BSSID */
2335                 /* packets from our adapter are dropped (echo) */
2336                 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2337                         return 0;
2338                 /* {broad,multi}cast packets to our IBSS go through */
2339                 if (is_multicast_ether_addr(header->addr1))
2340                         return !compare_ether_addr(header->addr3, priv->bssid);
2341                 /* packets to our adapter go through */
2342                 return !compare_ether_addr(header->addr1, priv->mac_addr);
2343         case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2344                 /* packets from our adapter are dropped (echo) */
2345                 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2346                         return 0;
2347                 /* {broad,multi}cast packets to our BSS go through */
2348                 if (is_multicast_ether_addr(header->addr1))
2349                         return !compare_ether_addr(header->addr2, priv->bssid);
2350                 /* packets to our adapter go through */
2351                 return !compare_ether_addr(header->addr1, priv->mac_addr);
2352         }
2353
2354         return 1;
2355 }
2356
2357 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2358
2359 static const char *iwl4965_get_tx_fail_reason(u32 status)
2360 {
2361         switch (status & TX_STATUS_MSK) {
2362         case TX_STATUS_SUCCESS:
2363                 return "SUCCESS";
2364                 TX_STATUS_ENTRY(SHORT_LIMIT);
2365                 TX_STATUS_ENTRY(LONG_LIMIT);
2366                 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2367                 TX_STATUS_ENTRY(MGMNT_ABORT);
2368                 TX_STATUS_ENTRY(NEXT_FRAG);
2369                 TX_STATUS_ENTRY(LIFE_EXPIRE);
2370                 TX_STATUS_ENTRY(DEST_PS);
2371                 TX_STATUS_ENTRY(ABORTED);
2372                 TX_STATUS_ENTRY(BT_RETRY);
2373                 TX_STATUS_ENTRY(STA_INVALID);
2374                 TX_STATUS_ENTRY(FRAG_DROPPED);
2375                 TX_STATUS_ENTRY(TID_DISABLE);
2376                 TX_STATUS_ENTRY(FRAME_FLUSHED);
2377                 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2378                 TX_STATUS_ENTRY(TX_LOCKED);
2379                 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2380         }
2381
2382         return "UNKNOWN";
2383 }
2384
2385 /**
2386  * iwl4965_scan_cancel - Cancel any currently executing HW scan
2387  *
2388  * NOTE: priv->mutex is not required before calling this function
2389  */
2390 static int iwl4965_scan_cancel(struct iwl4965_priv *priv)
2391 {
2392         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2393                 clear_bit(STATUS_SCANNING, &priv->status);
2394                 return 0;
2395         }
2396
2397         if (test_bit(STATUS_SCANNING, &priv->status)) {
2398                 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2399                         IWL_DEBUG_SCAN("Queuing scan abort.\n");
2400                         set_bit(STATUS_SCAN_ABORTING, &priv->status);
2401                         queue_work(priv->workqueue, &priv->abort_scan);
2402
2403                 } else
2404                         IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2405
2406                 return test_bit(STATUS_SCANNING, &priv->status);
2407         }
2408
2409         return 0;
2410 }
2411
2412 /**
2413  * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
2414  * @ms: amount of time to wait (in milliseconds) for scan to abort
2415  *
2416  * NOTE: priv->mutex must be held before calling this function
2417  */
2418 static int iwl4965_scan_cancel_timeout(struct iwl4965_priv *priv, unsigned long ms)
2419 {
2420         unsigned long now = jiffies;
2421         int ret;
2422
2423         ret = iwl4965_scan_cancel(priv);
2424         if (ret && ms) {
2425                 mutex_unlock(&priv->mutex);
2426                 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2427                                 test_bit(STATUS_SCANNING, &priv->status))
2428                         msleep(1);
2429                 mutex_lock(&priv->mutex);
2430
2431                 return test_bit(STATUS_SCANNING, &priv->status);
2432         }
2433
2434         return ret;
2435 }
2436
2437 static void iwl4965_sequence_reset(struct iwl4965_priv *priv)
2438 {
2439         /* Reset ieee stats */
2440
2441         /* We don't reset the net_device_stats (ieee->stats) on
2442          * re-association */
2443
2444         priv->last_seq_num = -1;
2445         priv->last_frag_num = -1;
2446         priv->last_packet_time = 0;
2447
2448         iwl4965_scan_cancel(priv);
2449 }
2450
2451 #define MAX_UCODE_BEACON_INTERVAL       4096
2452 #define INTEL_CONN_LISTEN_INTERVAL      __constant_cpu_to_le16(0xA)
2453
2454 static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
2455 {
2456         u16 new_val = 0;
2457         u16 beacon_factor = 0;
2458
2459         beacon_factor =
2460             (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2461                 / MAX_UCODE_BEACON_INTERVAL;
2462         new_val = beacon_val / beacon_factor;
2463
2464         return cpu_to_le16(new_val);
2465 }
2466
2467 static void iwl4965_setup_rxon_timing(struct iwl4965_priv *priv)
2468 {
2469         u64 interval_tm_unit;
2470         u64 tsf, result;
2471         unsigned long flags;
2472         struct ieee80211_conf *conf = NULL;
2473         u16 beacon_int = 0;
2474
2475         conf = ieee80211_get_hw_conf(priv->hw);
2476
2477         spin_lock_irqsave(&priv->lock, flags);
2478         priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2479         priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2480
2481         priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2482
2483         tsf = priv->timestamp1;
2484         tsf = ((tsf << 32) | priv->timestamp0);
2485
2486         beacon_int = priv->beacon_int;
2487         spin_unlock_irqrestore(&priv->lock, flags);
2488
2489         if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2490                 if (beacon_int == 0) {
2491                         priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2492                         priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2493                 } else {
2494                         priv->rxon_timing.beacon_interval =
2495                                 cpu_to_le16(beacon_int);
2496                         priv->rxon_timing.beacon_interval =
2497                             iwl4965_adjust_beacon_interval(
2498                                 le16_to_cpu(priv->rxon_timing.beacon_interval));
2499                 }
2500
2501                 priv->rxon_timing.atim_window = 0;
2502         } else {
2503                 priv->rxon_timing.beacon_interval =
2504                         iwl4965_adjust_beacon_interval(conf->beacon_int);
2505                 /* TODO: we need to get atim_window from upper stack
2506                  * for now we set to 0 */
2507                 priv->rxon_timing.atim_window = 0;
2508         }
2509
2510         interval_tm_unit =
2511                 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2512         result = do_div(tsf, interval_tm_unit);
2513         priv->rxon_timing.beacon_init_val =
2514             cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2515
2516         IWL_DEBUG_ASSOC
2517             ("beacon interval %d beacon timer %d beacon tim %d\n",
2518                 le16_to_cpu(priv->rxon_timing.beacon_interval),
2519                 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2520                 le16_to_cpu(priv->rxon_timing.atim_window));
2521 }
2522
2523 static int iwl4965_scan_initiate(struct iwl4965_priv *priv)
2524 {
2525         if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2526                 IWL_ERROR("APs don't scan.\n");
2527                 return 0;
2528         }
2529
2530         if (!iwl4965_is_ready_rf(priv)) {
2531                 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2532                 return -EIO;
2533         }
2534
2535         if (test_bit(STATUS_SCANNING, &priv->status)) {
2536                 IWL_DEBUG_SCAN("Scan already in progress.\n");
2537                 return -EAGAIN;
2538         }
2539
2540         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2541                 IWL_DEBUG_SCAN("Scan request while abort pending.  "
2542                                "Queuing.\n");
2543                 return -EAGAIN;
2544         }
2545
2546         IWL_DEBUG_INFO("Starting scan...\n");
2547         priv->scan_bands = 2;
2548         set_bit(STATUS_SCANNING, &priv->status);
2549         priv->scan_start = jiffies;
2550         priv->scan_pass_start = priv->scan_start;
2551
2552         queue_work(priv->workqueue, &priv->request_scan);
2553
2554         return 0;
2555 }
2556
2557 static int iwl4965_set_rxon_hwcrypto(struct iwl4965_priv *priv, int hw_decrypt)
2558 {
2559         struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
2560
2561         if (hw_decrypt)
2562                 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2563         else
2564                 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2565
2566         return 0;
2567 }
2568
2569 static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv, u8 phymode)
2570 {
2571         if (phymode == MODE_IEEE80211A) {
2572                 priv->staging_rxon.flags &=
2573                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2574                       | RXON_FLG_CCK_MSK);
2575                 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2576         } else {
2577                 /* Copied from iwl4965_bg_post_associate() */
2578                 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2579                         priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2580                 else
2581                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2582
2583                 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2584                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2585
2586                 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2587                 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2588                 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2589         }
2590 }
2591
2592 /*
2593  * initialize rxon structure with default values from eeprom
2594  */
2595 static void iwl4965_connection_init_rx_config(struct iwl4965_priv *priv)
2596 {
2597         const struct iwl4965_channel_info *ch_info;
2598
2599         memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2600
2601         switch (priv->iw_mode) {
2602         case IEEE80211_IF_TYPE_AP:
2603                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2604                 break;
2605
2606         case IEEE80211_IF_TYPE_STA:
2607                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2608                 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2609                 break;
2610
2611         case IEEE80211_IF_TYPE_IBSS:
2612                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2613                 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2614                 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2615                                                   RXON_FILTER_ACCEPT_GRP_MSK;
2616                 break;
2617
2618         case IEEE80211_IF_TYPE_MNTR:
2619                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2620                 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2621                     RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2622                 break;
2623         }
2624
2625 #if 0
2626         /* TODO:  Figure out when short_preamble would be set and cache from
2627          * that */
2628         if (!hw_to_local(priv->hw)->short_preamble)
2629                 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2630         else
2631                 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2632 #endif
2633
2634         ch_info = iwl4965_get_channel_info(priv, priv->phymode,
2635                                        le16_to_cpu(priv->staging_rxon.channel));
2636
2637         if (!ch_info)
2638                 ch_info = &priv->channel_info[0];
2639
2640         /*
2641          * in some case A channels are all non IBSS
2642          * in this case force B/G channel
2643          */
2644         if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2645             !(is_channel_ibss(ch_info)))
2646                 ch_info = &priv->channel_info[0];
2647
2648         priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2649         if (is_channel_a_band(ch_info))
2650                 priv->phymode = MODE_IEEE80211A;
2651         else
2652                 priv->phymode = MODE_IEEE80211G;
2653
2654         iwl4965_set_flags_for_phymode(priv, priv->phymode);
2655
2656         priv->staging_rxon.ofdm_basic_rates =
2657             (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2658         priv->staging_rxon.cck_basic_rates =
2659             (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2660
2661         priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2662                                         RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2663         memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2664         memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2665         priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2666         priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2667         iwl4965_set_rxon_chain(priv);
2668 }
2669
2670 static int iwl4965_set_mode(struct iwl4965_priv *priv, int mode)
2671 {
2672         if (mode == IEEE80211_IF_TYPE_IBSS) {
2673                 const struct iwl4965_channel_info *ch_info;
2674
2675                 ch_info = iwl4965_get_channel_info(priv,
2676                         priv->phymode,
2677                         le16_to_cpu(priv->staging_rxon.channel));
2678
2679                 if (!ch_info || !is_channel_ibss(ch_info)) {
2680                         IWL_ERROR("channel %d not IBSS channel\n",
2681                                   le16_to_cpu(priv->staging_rxon.channel));
2682                         return -EINVAL;
2683                 }
2684         }
2685
2686         priv->iw_mode = mode;
2687
2688         iwl4965_connection_init_rx_config(priv);
2689         memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2690
2691         iwl4965_clear_stations_table(priv);
2692
2693         /* dont commit rxon if rf-kill is on*/
2694         if (!iwl4965_is_ready_rf(priv))
2695                 return -EAGAIN;
2696
2697         cancel_delayed_work(&priv->scan_check);
2698         if (iwl4965_scan_cancel_timeout(priv, 100)) {
2699                 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2700                 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2701                 return -EAGAIN;
2702         }
2703
2704         iwl4965_commit_rxon(priv);
2705
2706         return 0;
2707 }
2708
2709 static void iwl4965_build_tx_cmd_hwcrypto(struct iwl4965_priv *priv,
2710                                       struct ieee80211_tx_control *ctl,
2711                                       struct iwl4965_cmd *cmd,
2712                                       struct sk_buff *skb_frag,
2713                                       int last_frag)
2714 {
2715         struct iwl4965_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
2716
2717         switch (keyinfo->alg) {
2718         case ALG_CCMP:
2719                 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2720                 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2721                 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2722                 break;
2723
2724         case ALG_TKIP:
2725 #if 0
2726                 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2727
2728                 if (last_frag)
2729                         memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2730                                8);
2731                 else
2732                         memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2733 #endif
2734                 break;
2735
2736         case ALG_WEP:
2737                 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2738                         (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2739
2740                 if (keyinfo->keylen == 13)
2741                         cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2742
2743                 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2744
2745                 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2746                              "with key %d\n", ctl->key_idx);
2747                 break;
2748
2749         default:
2750                 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2751                 break;
2752         }
2753 }
2754
2755 /*
2756  * handle build REPLY_TX command notification.
2757  */
2758 static void iwl4965_build_tx_cmd_basic(struct iwl4965_priv *priv,
2759                                   struct iwl4965_cmd *cmd,
2760                                   struct ieee80211_tx_control *ctrl,
2761                                   struct ieee80211_hdr *hdr,
2762                                   int is_unicast, u8 std_id)
2763 {
2764         __le16 *qc;
2765         u16 fc = le16_to_cpu(hdr->frame_control);
2766         __le32 tx_flags = cmd->cmd.tx.tx_flags;
2767
2768         cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2769         if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2770                 tx_flags |= TX_CMD_FLG_ACK_MSK;
2771                 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2772                         tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2773                 if (ieee80211_is_probe_response(fc) &&
2774                     !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2775                         tx_flags |= TX_CMD_FLG_TSF_MSK;
2776         } else {
2777                 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2778                 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2779         }
2780
2781         if (ieee80211_is_back_request(fc))
2782                 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2783
2784
2785         cmd->cmd.tx.sta_id = std_id;
2786         if (ieee80211_get_morefrag(hdr))
2787                 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2788
2789         qc = ieee80211_get_qos_ctrl(hdr);
2790         if (qc) {
2791                 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2792                 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2793         } else
2794                 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2795
2796         if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2797                 tx_flags |= TX_CMD_FLG_RTS_MSK;
2798                 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2799         } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2800                 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2801                 tx_flags |= TX_CMD_FLG_CTS_MSK;
2802         }
2803
2804         if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2805                 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2806
2807         tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2808         if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2809                 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2810                     (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
2811                         cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
2812                 else
2813                         cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
2814         } else
2815                 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2816
2817         cmd->cmd.tx.driver_txop = 0;
2818         cmd->cmd.tx.tx_flags = tx_flags;
2819         cmd->cmd.tx.next_frame_len = 0;
2820 }
2821
2822 /**
2823  * iwl4965_get_sta_id - Find station's index within station table
2824  *
2825  * If new IBSS station, create new entry in station table
2826  */
2827 static int iwl4965_get_sta_id(struct iwl4965_priv *priv,
2828                                 struct ieee80211_hdr *hdr)
2829 {
2830         int sta_id;
2831         u16 fc = le16_to_cpu(hdr->frame_control);
2832         DECLARE_MAC_BUF(mac);
2833
2834         /* If this frame is broadcast or management, use broadcast station id */
2835         if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2836             is_multicast_ether_addr(hdr->addr1))
2837                 return priv->hw_setting.bcast_sta_id;
2838
2839         switch (priv->iw_mode) {
2840
2841         /* If we are a client station in a BSS network, use the special
2842          * AP station entry (that's the only station we communicate with) */
2843         case IEEE80211_IF_TYPE_STA:
2844                 return IWL_AP_ID;
2845
2846         /* If we are an AP, then find the station, or use BCAST */
2847         case IEEE80211_IF_TYPE_AP:
2848                 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
2849                 if (sta_id != IWL_INVALID_STATION)
2850                         return sta_id;
2851                 return priv->hw_setting.bcast_sta_id;
2852
2853         /* If this frame is going out to an IBSS network, find the station,
2854          * or create a new station table entry */
2855         case IEEE80211_IF_TYPE_IBSS:
2856                 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
2857                 if (sta_id != IWL_INVALID_STATION)
2858                         return sta_id;
2859
2860                 /* Create new station table entry */
2861                 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2862                                                    0, CMD_ASYNC, NULL);
2863
2864                 if (sta_id != IWL_INVALID_STATION)
2865                         return sta_id;
2866
2867                 IWL_DEBUG_DROP("Station %s not in station map. "
2868                                "Defaulting to broadcast...\n",
2869                                print_mac(mac, hdr->addr1));
2870                 iwl4965_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
2871                 return priv->hw_setting.bcast_sta_id;
2872
2873         default:
2874                 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
2875                 return priv->hw_setting.bcast_sta_id;
2876         }
2877 }
2878
2879 /*
2880  * start REPLY_TX command process
2881  */
2882 static int iwl4965_tx_skb(struct iwl4965_priv *priv,
2883                       struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2884 {
2885         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2886         struct iwl4965_tfd_frame *tfd;
2887         u32 *control_flags;
2888         int txq_id = ctl->queue;
2889         struct iwl4965_tx_queue *txq = NULL;
2890         struct iwl4965_queue *q = NULL;
2891         dma_addr_t phys_addr;
2892         dma_addr_t txcmd_phys;
2893         dma_addr_t scratch_phys;
2894         struct iwl4965_cmd *out_cmd = NULL;
2895         u16 len, idx, len_org;
2896         u8 id, hdr_len, unicast;
2897         u8 sta_id;
2898         u16 seq_number = 0;
2899         u16 fc;
2900         __le16 *qc;
2901         u8 wait_write_ptr = 0;
2902         unsigned long flags;
2903         int rc;
2904
2905         spin_lock_irqsave(&priv->lock, flags);
2906         if (iwl4965_is_rfkill(priv)) {
2907                 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2908                 goto drop_unlock;
2909         }
2910
2911         if (!priv->vif) {
2912                 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
2913                 goto drop_unlock;
2914         }
2915
2916         if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2917                 IWL_ERROR("ERROR: No TX rate available.\n");
2918                 goto drop_unlock;
2919         }
2920
2921         unicast = !is_multicast_ether_addr(hdr->addr1);
2922         id = 0;
2923
2924         fc = le16_to_cpu(hdr->frame_control);
2925
2926 #ifdef CONFIG_IWL4965_DEBUG
2927         if (ieee80211_is_auth(fc))
2928                 IWL_DEBUG_TX("Sending AUTH frame\n");
2929         else if (ieee80211_is_assoc_request(fc))
2930                 IWL_DEBUG_TX("Sending ASSOC frame\n");
2931         else if (ieee80211_is_reassoc_request(fc))
2932                 IWL_DEBUG_TX("Sending REASSOC frame\n");
2933 #endif
2934
2935         /* drop all data frame if we are not associated */
2936         if (((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA) &&
2937            (!iwl4965_is_associated(priv) ||
2938             !priv->assoc_id ||
2939             !priv->assoc_station_added)) {
2940                 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
2941                 goto drop_unlock;
2942         }
2943
2944         spin_unlock_irqrestore(&priv->lock, flags);
2945
2946         hdr_len = ieee80211_get_hdrlen(fc);
2947
2948         /* Find (or create) index into station table for destination station */
2949         sta_id = iwl4965_get_sta_id(priv, hdr);
2950         if (sta_id == IWL_INVALID_STATION) {
2951                 DECLARE_MAC_BUF(mac);
2952
2953                 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2954                                print_mac(mac, hdr->addr1));
2955                 goto drop;
2956         }
2957
2958         IWL_DEBUG_RATE("station Id %d\n", sta_id);
2959
2960         qc = ieee80211_get_qos_ctrl(hdr);
2961         if (qc) {
2962                 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2963                 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2964                                 IEEE80211_SCTL_SEQ;
2965                 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2966                         (hdr->seq_ctrl &
2967                                 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2968                 seq_number += 0x10;
2969 #ifdef CONFIG_IWL4965_HT
2970 #ifdef CONFIG_IWL4965_HT_AGG
2971                 /* aggregation is on for this <sta,tid> */
2972                 if (ctl->flags & IEEE80211_TXCTL_HT_MPDU_AGG)
2973                         txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
2974 #endif /* CONFIG_IWL4965_HT_AGG */
2975 #endif /* CONFIG_IWL4965_HT */
2976         }
2977
2978         /* Descriptor for chosen Tx queue */
2979         txq = &priv->txq[txq_id];
2980         q = &txq->q;
2981
2982         spin_lock_irqsave(&priv->lock, flags);
2983
2984         /* Set up first empty TFD within this queue's circular TFD buffer */
2985         tfd = &txq->bd[q->write_ptr];
2986         memset(tfd, 0, sizeof(*tfd));
2987         control_flags = (u32 *) tfd;
2988         idx = get_cmd_index(q, q->write_ptr, 0);
2989
2990         /* Set up driver data for this TFD */
2991         memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
2992         txq->txb[q->write_ptr].skb[0] = skb;
2993         memcpy(&(txq->txb[q->write_ptr].status.control),
2994                ctl, sizeof(struct ieee80211_tx_control));
2995
2996         /* Set up first empty entry in queue's array of Tx/cmd buffers */
2997         out_cmd = &txq->cmd[idx];
2998         memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2999         memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
3000
3001         /*
3002          * Set up the Tx-command (not MAC!) header.
3003          * Store the chosen Tx queue and TFD index within the sequence field;
3004          * after Tx, uCode's Tx response will return this value so driver can
3005          * locate the frame within the tx queue and do post-tx processing.
3006          */
3007         out_cmd->hdr.cmd = REPLY_TX;
3008         out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
3009                                 INDEX_TO_SEQ(q->write_ptr)));
3010
3011         /* Copy MAC header from skb into command buffer */
3012         memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
3013
3014         /*
3015          * Use the first empty entry in this queue's command buffer array
3016          * to contain the Tx command and MAC header concatenated together
3017          * (payload data will be in another buffer).
3018          * Size of this varies, due to varying MAC header length.
3019          * If end is not dword aligned, we'll have 2 extra bytes at the end
3020          * of the MAC header (device reads on dword boundaries).
3021          * We'll tell device about this padding later.
3022          */
3023         len = priv->hw_setting.tx_cmd_len +
3024                 sizeof(struct iwl4965_cmd_header) + hdr_len;
3025
3026         len_org = len;
3027         len = (len + 3) & ~3;
3028
3029         if (len_org != len)
3030                 len_org = 1;
3031         else
3032                 len_org = 0;
3033
3034         /* Physical address of this Tx command's header (not MAC header!),
3035          * within command buffer array. */
3036         txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
3037                      offsetof(struct iwl4965_cmd, hdr);
3038
3039         /* Add buffer containing Tx command and MAC(!) header to TFD's
3040          * first entry */
3041         iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
3042
3043         if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
3044                 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
3045
3046         /* Set up TFD's 2nd entry to point directly to remainder of skb,
3047          * if any (802.11 null frames have no payload). */
3048         len = skb->len - hdr_len;
3049         if (len) {
3050                 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
3051                                            len, PCI_DMA_TODEVICE);
3052                 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
3053         }
3054
3055         /* Tell 4965 about any 2-byte padding after MAC header */
3056         if (len_org)
3057                 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
3058
3059         /* Total # bytes to be transmitted */
3060         len = (u16)skb->len;
3061         out_cmd->cmd.tx.len = cpu_to_le16(len);
3062
3063         /* TODO need this for burst mode later on */
3064         iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
3065
3066         /* set is_hcca to 0; it probably will never be implemented */
3067         iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
3068
3069         scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
3070                 offsetof(struct iwl4965_tx_cmd, scratch);
3071         out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
3072         out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
3073
3074 #ifdef CONFIG_IWL4965_HT_AGG
3075 #ifdef CONFIG_IWL4965_HT
3076         /* TODO: move this functionality to rate scaling */
3077         iwl4965_tl_get_stats(priv, hdr);
3078 #endif /* CONFIG_IWL4965_HT_AGG */
3079 #endif /*CONFIG_IWL4965_HT */
3080
3081
3082         if (!ieee80211_get_morefrag(hdr)) {
3083                 txq->need_update = 1;
3084                 if (qc) {
3085                         u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
3086                         priv->stations[sta_id].tid[tid].seq_number = seq_number;
3087                 }
3088         } else {
3089                 wait_write_ptr = 1;
3090                 txq->need_update = 0;
3091         }
3092
3093         iwl4965_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
3094                            sizeof(out_cmd->cmd.tx));
3095
3096         iwl4965_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
3097                            ieee80211_get_hdrlen(fc));
3098
3099         /* Set up entry for this TFD in Tx byte-count array */
3100         iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
3101
3102         /* Tell device the write index *just past* this latest filled TFD */
3103         q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
3104         rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
3105         spin_unlock_irqrestore(&priv->lock, flags);
3106
3107         if (rc)
3108                 return rc;
3109
3110         if ((iwl4965_queue_space(q) < q->high_mark)
3111             && priv->mac80211_registered) {
3112                 if (wait_write_ptr) {
3113                         spin_lock_irqsave(&priv->lock, flags);
3114                         txq->need_update = 1;
3115                         iwl4965_tx_queue_update_write_ptr(priv, txq);
3116                         spin_unlock_irqrestore(&priv->lock, flags);
3117                 }
3118
3119                 ieee80211_stop_queue(priv->hw, ctl->queue);
3120         }
3121
3122         return 0;
3123
3124 drop_unlock:
3125         spin_unlock_irqrestore(&priv->lock, flags);
3126 drop:
3127         return -1;
3128 }
3129
3130 static void iwl4965_set_rate(struct iwl4965_priv *priv)
3131 {
3132         const struct ieee80211_hw_mode *hw = NULL;
3133         struct ieee80211_rate *rate;
3134         int i;
3135
3136         hw = iwl4965_get_hw_mode(priv, priv->phymode);
3137         if (!hw) {
3138             &n