iwlwifi: remove iwl4965_tx_cmd
[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.22k" 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 /**
1643  * iwl4965_eeprom_init - read EEPROM contents
1644  *
1645  * Load the EEPROM contents from adapter into priv->eeprom
1646  *
1647  * NOTE:  This routine uses the non-debug IO access functions.
1648  */
1649 int iwl4965_eeprom_init(struct iwl4965_priv *priv)
1650 {
1651         __le16 *e = (__le16 *)&priv->eeprom;
1652         u32 gp = iwl4965_read32(priv, CSR_EEPROM_GP);
1653         u32 r;
1654         int sz = sizeof(priv->eeprom);
1655         int rc;
1656         int i;
1657         u16 addr;
1658
1659         /* The EEPROM structure has several padding buffers within it
1660          * and when adding new EEPROM maps is subject to programmer errors
1661          * which may be very difficult to identify without explicitly
1662          * checking the resulting size of the eeprom map. */
1663         BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
1664
1665         if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
1666                 IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
1667                 return -ENOENT;
1668         }
1669
1670         /* Make sure driver (instead of uCode) is allowed to read EEPROM */
1671         rc = iwl4965_eeprom_acquire_semaphore(priv);
1672         if (rc < 0) {
1673                 IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
1674                 return -ENOENT;
1675         }
1676
1677         /* eeprom is an array of 16bit values */
1678         for (addr = 0; addr < sz; addr += sizeof(u16)) {
1679                 _iwl4965_write32(priv, CSR_EEPROM_REG, addr << 1);
1680                 _iwl4965_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
1681
1682                 for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
1683                                         i += IWL_EEPROM_ACCESS_DELAY) {
1684                         r = _iwl4965_read_direct32(priv, CSR_EEPROM_REG);
1685                         if (r & CSR_EEPROM_REG_READ_VALID_MSK)
1686                                 break;
1687                         udelay(IWL_EEPROM_ACCESS_DELAY);
1688                 }
1689
1690                 if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
1691                         IWL_ERROR("Time out reading EEPROM[%d]", addr);
1692                         rc = -ETIMEDOUT;
1693                         goto done;
1694                 }
1695                 e[addr / 2] = cpu_to_le16(r >> 16);
1696         }
1697         rc = 0;
1698
1699 done:
1700         iwl4965_eeprom_release_semaphore(priv);
1701         return rc;
1702 }
1703
1704 /******************************************************************************
1705  *
1706  * Misc. internal state and helper functions
1707  *
1708  ******************************************************************************/
1709 #ifdef CONFIG_IWL4965_DEBUG
1710
1711 /**
1712  * iwl4965_report_frame - dump frame to syslog during debug sessions
1713  *
1714  * You may hack this function to show different aspects of received frames,
1715  * including selective frame dumps.
1716  * group100 parameter selects whether to show 1 out of 100 good frames.
1717  *
1718  * TODO:  This was originally written for 3945, need to audit for
1719  *        proper operation with 4965.
1720  */
1721 void iwl4965_report_frame(struct iwl4965_priv *priv,
1722                       struct iwl4965_rx_packet *pkt,
1723                       struct ieee80211_hdr *header, int group100)
1724 {
1725         u32 to_us;
1726         u32 print_summary = 0;
1727         u32 print_dump = 0;     /* set to 1 to dump all frames' contents */
1728         u32 hundred = 0;
1729         u32 dataframe = 0;
1730         u16 fc;
1731         u16 seq_ctl;
1732         u16 channel;
1733         u16 phy_flags;
1734         int rate_sym;
1735         u16 length;
1736         u16 status;
1737         u16 bcn_tmr;
1738         u32 tsf_low;
1739         u64 tsf;
1740         u8 rssi;
1741         u8 agc;
1742         u16 sig_avg;
1743         u16 noise_diff;
1744         struct iwl4965_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
1745         struct iwl4965_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
1746         struct iwl4965_rx_frame_end *rx_end = IWL_RX_END(pkt);
1747         u8 *data = IWL_RX_DATA(pkt);
1748
1749         /* MAC header */
1750         fc = le16_to_cpu(header->frame_control);
1751         seq_ctl = le16_to_cpu(header->seq_ctrl);
1752
1753         /* metadata */
1754         channel = le16_to_cpu(rx_hdr->channel);
1755         phy_flags = le16_to_cpu(rx_hdr->phy_flags);
1756         rate_sym = rx_hdr->rate;
1757         length = le16_to_cpu(rx_hdr->len);
1758
1759         /* end-of-frame status and timestamp */
1760         status = le32_to_cpu(rx_end->status);
1761         bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
1762         tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
1763         tsf = le64_to_cpu(rx_end->timestamp);
1764
1765         /* signal statistics */
1766         rssi = rx_stats->rssi;
1767         agc = rx_stats->agc;
1768         sig_avg = le16_to_cpu(rx_stats->sig_avg);
1769         noise_diff = le16_to_cpu(rx_stats->noise_diff);
1770
1771         to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
1772
1773         /* if data frame is to us and all is good,
1774          *   (optionally) print summary for only 1 out of every 100 */
1775         if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
1776             (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
1777                 dataframe = 1;
1778                 if (!group100)
1779                         print_summary = 1;      /* print each frame */
1780                 else if (priv->framecnt_to_us < 100) {
1781                         priv->framecnt_to_us++;
1782                         print_summary = 0;
1783                 } else {
1784                         priv->framecnt_to_us = 0;
1785                         print_summary = 1;
1786                         hundred = 1;
1787                 }
1788         } else {
1789                 /* print summary for all other frames */
1790                 print_summary = 1;
1791         }
1792
1793         if (print_summary) {
1794                 char *title;
1795                 u32 rate;
1796
1797                 if (hundred)
1798                         title = "100Frames";
1799                 else if (fc & IEEE80211_FCTL_RETRY)
1800                         title = "Retry";
1801                 else if (ieee80211_is_assoc_response(fc))
1802                         title = "AscRsp";
1803                 else if (ieee80211_is_reassoc_response(fc))
1804                         title = "RasRsp";
1805                 else if (ieee80211_is_probe_response(fc)) {
1806                         title = "PrbRsp";
1807                         print_dump = 1; /* dump frame contents */
1808                 } else if (ieee80211_is_beacon(fc)) {
1809                         title = "Beacon";
1810                         print_dump = 1; /* dump frame contents */
1811                 } else if (ieee80211_is_atim(fc))
1812                         title = "ATIM";
1813                 else if (ieee80211_is_auth(fc))
1814                         title = "Auth";
1815                 else if (ieee80211_is_deauth(fc))
1816                         title = "DeAuth";
1817                 else if (ieee80211_is_disassoc(fc))
1818                         title = "DisAssoc";
1819                 else
1820                         title = "Frame";
1821
1822                 rate = iwl4965_rate_index_from_plcp(rate_sym);
1823                 if (rate == -1)
1824                         rate = 0;
1825                 else
1826                         rate = iwl4965_rates[rate].ieee / 2;
1827
1828                 /* print frame summary.
1829                  * MAC addresses show just the last byte (for brevity),
1830                  *    but you can hack it to show more, if you'd like to. */
1831                 if (dataframe)
1832                         IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
1833                                      "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
1834                                      title, fc, header->addr1[5],
1835                                      length, rssi, channel, rate);
1836                 else {
1837                         /* src/dst addresses assume managed mode */
1838                         IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
1839                                      "src=0x%02x, rssi=%u, tim=%lu usec, "
1840                                      "phy=0x%02x, chnl=%d\n",
1841                                      title, fc, header->addr1[5],
1842                                      header->addr3[5], rssi,
1843                                      tsf_low - priv->scan_start_tsf,
1844                                      phy_flags, channel);
1845                 }
1846         }
1847         if (print_dump)
1848                 iwl4965_print_hex_dump(IWL_DL_RX, data, length);
1849 }
1850 #endif
1851
1852 static void iwl4965_unset_hw_setting(struct iwl4965_priv *priv)
1853 {
1854         if (priv->hw_setting.shared_virt)
1855                 pci_free_consistent(priv->pci_dev,
1856                                     sizeof(struct iwl4965_shared),
1857                                     priv->hw_setting.shared_virt,
1858                                     priv->hw_setting.shared_phys);
1859 }
1860
1861 /**
1862  * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
1863  *
1864  * return : set the bit for each supported rate insert in ie
1865  */
1866 static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
1867                                     u16 basic_rate, int *left)
1868 {
1869         u16 ret_rates = 0, bit;
1870         int i;
1871         u8 *cnt = ie;
1872         u8 *rates = ie + 1;
1873
1874         for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
1875                 if (bit & supported_rate) {
1876                         ret_rates |= bit;
1877                         rates[*cnt] = iwl4965_rates[i].ieee |
1878                                 ((bit & basic_rate) ? 0x80 : 0x00);
1879                         (*cnt)++;
1880                         (*left)--;
1881                         if ((*left <= 0) ||
1882                             (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
1883                                 break;
1884                 }
1885         }
1886
1887         return ret_rates;
1888 }
1889
1890 #ifdef CONFIG_IWL4965_HT
1891 void static iwl4965_set_ht_capab(struct ieee80211_hw *hw,
1892                              struct ieee80211_ht_cap *ht_cap,
1893                              u8 use_current_config);
1894 #endif
1895
1896 /**
1897  * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
1898  */
1899 static u16 iwl4965_fill_probe_req(struct iwl4965_priv *priv,
1900                               struct ieee80211_mgmt *frame,
1901                               int left, int is_direct)
1902 {
1903         int len = 0;
1904         u8 *pos = NULL;
1905         u16 active_rates, ret_rates, cck_rates, active_rate_basic;
1906 #ifdef CONFIG_IWL4965_HT
1907         struct ieee80211_hw_mode *mode;
1908 #endif /* CONFIG_IWL4965_HT */
1909
1910         /* Make sure there is enough space for the probe request,
1911          * two mandatory IEs and the data */
1912         left -= 24;
1913         if (left < 0)
1914                 return 0;
1915         len += 24;
1916
1917         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
1918         memcpy(frame->da, iwl4965_broadcast_addr, ETH_ALEN);
1919         memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
1920         memcpy(frame->bssid, iwl4965_broadcast_addr, ETH_ALEN);
1921         frame->seq_ctrl = 0;
1922
1923         /* fill in our indirect SSID IE */
1924         /* ...next IE... */
1925
1926         left -= 2;
1927         if (left < 0)
1928                 return 0;
1929         len += 2;
1930         pos = &(frame->u.probe_req.variable[0]);
1931         *pos++ = WLAN_EID_SSID;
1932         *pos++ = 0;
1933
1934         /* fill in our direct SSID IE... */
1935         if (is_direct) {
1936                 /* ...next IE... */
1937                 left -= 2 + priv->essid_len;
1938                 if (left < 0)
1939                         return 0;
1940                 /* ... fill it in... */
1941                 *pos++ = WLAN_EID_SSID;
1942                 *pos++ = priv->essid_len;
1943                 memcpy(pos, priv->essid, priv->essid_len);
1944                 pos += priv->essid_len;
1945                 len += 2 + priv->essid_len;
1946         }
1947
1948         /* fill in supported rate */
1949         /* ...next IE... */
1950         left -= 2;
1951         if (left < 0)
1952                 return 0;
1953
1954         /* ... fill it in... */
1955         *pos++ = WLAN_EID_SUPP_RATES;
1956         *pos = 0;
1957
1958         /* exclude 60M rate */
1959         active_rates = priv->rates_mask;
1960         active_rates &= ~IWL_RATE_60M_MASK;
1961
1962         active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
1963
1964         cck_rates = IWL_CCK_RATES_MASK & active_rates;
1965         ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
1966                         active_rate_basic, &left);
1967         active_rates &= ~ret_rates;
1968
1969         ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
1970                                  active_rate_basic, &left);
1971         active_rates &= ~ret_rates;
1972
1973         len += 2 + *pos;
1974         pos += (*pos) + 1;
1975         if (active_rates == 0)
1976                 goto fill_end;
1977
1978         /* fill in supported extended rate */
1979         /* ...next IE... */
1980         left -= 2;
1981         if (left < 0)
1982                 return 0;
1983         /* ... fill it in... */
1984         *pos++ = WLAN_EID_EXT_SUPP_RATES;
1985         *pos = 0;
1986         iwl4965_supported_rate_to_ie(pos, active_rates,
1987                                  active_rate_basic, &left);
1988         if (*pos > 0)
1989                 len += 2 + *pos;
1990
1991 #ifdef CONFIG_IWL4965_HT
1992         mode = priv->hw->conf.mode;
1993         if (mode->ht_info.ht_supported) {
1994                 pos += (*pos) + 1;
1995                 *pos++ = WLAN_EID_HT_CAPABILITY;
1996                 *pos++ = sizeof(struct ieee80211_ht_cap);
1997                 iwl4965_set_ht_capab(priv->hw,
1998                                 (struct ieee80211_ht_cap *)pos, 0);
1999                 len += 2 + sizeof(struct ieee80211_ht_cap);
2000         }
2001 #endif  /*CONFIG_IWL4965_HT */
2002
2003  fill_end:
2004         return (u16)len;
2005 }
2006
2007 /*
2008  * QoS  support
2009 */
2010 #ifdef CONFIG_IWL4965_QOS
2011 static int iwl4965_send_qos_params_command(struct iwl4965_priv *priv,
2012                                        struct iwl4965_qosparam_cmd *qos)
2013 {
2014
2015         return iwl4965_send_cmd_pdu(priv, REPLY_QOS_PARAM,
2016                                 sizeof(struct iwl4965_qosparam_cmd), qos);
2017 }
2018
2019 static void iwl4965_reset_qos(struct iwl4965_priv *priv)
2020 {
2021         u16 cw_min = 15;
2022         u16 cw_max = 1023;
2023         u8 aifs = 2;
2024         u8 is_legacy = 0;
2025         unsigned long flags;
2026         int i;
2027
2028         spin_lock_irqsave(&priv->lock, flags);
2029         priv->qos_data.qos_active = 0;
2030
2031         if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
2032                 if (priv->qos_data.qos_enable)
2033                         priv->qos_data.qos_active = 1;
2034                 if (!(priv->active_rate & 0xfff0)) {
2035                         cw_min = 31;
2036                         is_legacy = 1;
2037                 }
2038         } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2039                 if (priv->qos_data.qos_enable)
2040                         priv->qos_data.qos_active = 1;
2041         } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
2042                 cw_min = 31;
2043                 is_legacy = 1;
2044         }
2045
2046         if (priv->qos_data.qos_active)
2047                 aifs = 3;
2048
2049         priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
2050         priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
2051         priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
2052         priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
2053         priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
2054
2055         if (priv->qos_data.qos_active) {
2056                 i = 1;
2057                 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
2058                 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
2059                 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
2060                 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2061                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2062
2063                 i = 2;
2064                 priv->qos_data.def_qos_parm.ac[i].cw_min =
2065                         cpu_to_le16((cw_min + 1) / 2 - 1);
2066                 priv->qos_data.def_qos_parm.ac[i].cw_max =
2067                         cpu_to_le16(cw_max);
2068                 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2069                 if (is_legacy)
2070                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
2071                                 cpu_to_le16(6016);
2072                 else
2073                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
2074                                 cpu_to_le16(3008);
2075                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2076
2077                 i = 3;
2078                 priv->qos_data.def_qos_parm.ac[i].cw_min =
2079                         cpu_to_le16((cw_min + 1) / 4 - 1);
2080                 priv->qos_data.def_qos_parm.ac[i].cw_max =
2081                         cpu_to_le16((cw_max + 1) / 2 - 1);
2082                 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
2083                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2084                 if (is_legacy)
2085                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
2086                                 cpu_to_le16(3264);
2087                 else
2088                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
2089                                 cpu_to_le16(1504);
2090         } else {
2091                 for (i = 1; i < 4; i++) {
2092                         priv->qos_data.def_qos_parm.ac[i].cw_min =
2093                                 cpu_to_le16(cw_min);
2094                         priv->qos_data.def_qos_parm.ac[i].cw_max =
2095                                 cpu_to_le16(cw_max);
2096                         priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
2097                         priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
2098                         priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
2099                 }
2100         }
2101         IWL_DEBUG_QOS("set QoS to default \n");
2102
2103         spin_unlock_irqrestore(&priv->lock, flags);
2104 }
2105
2106 static void iwl4965_activate_qos(struct iwl4965_priv *priv, u8 force)
2107 {
2108         unsigned long flags;
2109
2110         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2111                 return;
2112
2113         if (!priv->qos_data.qos_enable)
2114                 return;
2115
2116         spin_lock_irqsave(&priv->lock, flags);
2117         priv->qos_data.def_qos_parm.qos_flags = 0;
2118
2119         if (priv->qos_data.qos_cap.q_AP.queue_request &&
2120             !priv->qos_data.qos_cap.q_AP.txop_request)
2121                 priv->qos_data.def_qos_parm.qos_flags |=
2122                         QOS_PARAM_FLG_TXOP_TYPE_MSK;
2123         if (priv->qos_data.qos_active)
2124                 priv->qos_data.def_qos_parm.qos_flags |=
2125                         QOS_PARAM_FLG_UPDATE_EDCA_MSK;
2126
2127 #ifdef CONFIG_IWL4965_HT
2128         if (priv->current_ht_config.is_ht)
2129                 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
2130 #endif /* CONFIG_IWL4965_HT */
2131
2132         spin_unlock_irqrestore(&priv->lock, flags);
2133
2134         if (force || iwl4965_is_associated(priv)) {
2135                 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
2136                                 priv->qos_data.qos_active,
2137                                 priv->qos_data.def_qos_parm.qos_flags);
2138
2139                 iwl4965_send_qos_params_command(priv,
2140                                 &(priv->qos_data.def_qos_parm));
2141         }
2142 }
2143
2144 #endif /* CONFIG_IWL4965_QOS */
2145 /*
2146  * Power management (not Tx power!) functions
2147  */
2148 #define MSEC_TO_USEC 1024
2149
2150 #define NOSLP __constant_cpu_to_le16(0), 0, 0
2151 #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
2152 #define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
2153 #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
2154                                      __constant_cpu_to_le32(X1), \
2155                                      __constant_cpu_to_le32(X2), \
2156                                      __constant_cpu_to_le32(X3), \
2157                                      __constant_cpu_to_le32(X4)}
2158
2159
2160 /* default power management (not Tx power) table values */
2161 /* for tim  0-10 */
2162 static struct iwl4965_power_vec_entry range_0[IWL_POWER_AC] = {
2163         {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2164         {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
2165         {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
2166         {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
2167         {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
2168         {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
2169 };
2170
2171 /* for tim > 10 */
2172 static struct iwl4965_power_vec_entry range_1[IWL_POWER_AC] = {
2173         {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
2174         {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
2175                  SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
2176         {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
2177                  SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
2178         {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
2179                  SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
2180         {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
2181         {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
2182                  SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
2183 };
2184
2185 int iwl4965_power_init_handle(struct iwl4965_priv *priv)
2186 {
2187         int rc = 0, i;
2188         struct iwl4965_power_mgr *pow_data;
2189         int size = sizeof(struct iwl4965_power_vec_entry) * IWL_POWER_AC;
2190         u16 pci_pm;
2191
2192         IWL_DEBUG_POWER("Initialize power \n");
2193
2194         pow_data = &(priv->power_data);
2195
2196         memset(pow_data, 0, sizeof(*pow_data));
2197
2198         pow_data->active_index = IWL_POWER_RANGE_0;
2199         pow_data->dtim_val = 0xffff;
2200
2201         memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
2202         memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
2203
2204         rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
2205         if (rc != 0)
2206                 return 0;
2207         else {
2208                 struct iwl4965_powertable_cmd *cmd;
2209
2210                 IWL_DEBUG_POWER("adjust power command flags\n");
2211
2212                 for (i = 0; i < IWL_POWER_AC; i++) {
2213                         cmd = &pow_data->pwr_range_0[i].cmd;
2214
2215                         if (pci_pm & 0x1)
2216                                 cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
2217                         else
2218                                 cmd->flags |= IWL_POWER_PCI_PM_MSK;
2219                 }
2220         }
2221         return rc;
2222 }
2223
2224 static int iwl4965_update_power_cmd(struct iwl4965_priv *priv,
2225                                 struct iwl4965_powertable_cmd *cmd, u32 mode)
2226 {
2227         int rc = 0, i;
2228         u8 skip;
2229         u32 max_sleep = 0;
2230         struct iwl4965_power_vec_entry *range;
2231         u8 period = 0;
2232         struct iwl4965_power_mgr *pow_data;
2233
2234         if (mode > IWL_POWER_INDEX_5) {
2235                 IWL_DEBUG_POWER("Error invalid power mode \n");
2236                 return -1;
2237         }
2238         pow_data = &(priv->power_data);
2239
2240         if (pow_data->active_index == IWL_POWER_RANGE_0)
2241                 range = &pow_data->pwr_range_0[0];
2242         else
2243                 range = &pow_data->pwr_range_1[1];
2244
2245         memcpy(cmd, &range[mode].cmd, sizeof(struct iwl4965_powertable_cmd));
2246
2247 #ifdef IWL_MAC80211_DISABLE
2248         if (priv->assoc_network != NULL) {
2249                 unsigned long flags;
2250
2251                 period = priv->assoc_network->tim.tim_period;
2252         }
2253 #endif  /*IWL_MAC80211_DISABLE */
2254         skip = range[mode].no_dtim;
2255
2256         if (period == 0) {
2257                 period = 1;
2258                 skip = 0;
2259         }
2260
2261         if (skip == 0) {
2262                 max_sleep = period;
2263                 cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
2264         } else {
2265                 __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
2266                 max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
2267                 cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
2268         }
2269
2270         for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
2271                 if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
2272                         cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
2273         }
2274
2275         IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
2276         IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
2277         IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
2278         IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
2279                         le32_to_cpu(cmd->sleep_interval[0]),
2280                         le32_to_cpu(cmd->sleep_interval[1]),
2281                         le32_to_cpu(cmd->sleep_interval[2]),
2282                         le32_to_cpu(cmd->sleep_interval[3]),
2283                         le32_to_cpu(cmd->sleep_interval[4]));
2284
2285         return rc;
2286 }
2287
2288 static int iwl4965_send_power_mode(struct iwl4965_priv *priv, u32 mode)
2289 {
2290         u32 uninitialized_var(final_mode);
2291         int rc;
2292         struct iwl4965_powertable_cmd cmd;
2293
2294         /* If on battery, set to 3,
2295          * if plugged into AC power, set to CAM ("continuously aware mode"),
2296          * else user level */
2297         switch (mode) {
2298         case IWL_POWER_BATTERY:
2299                 final_mode = IWL_POWER_INDEX_3;
2300                 break;
2301         case IWL_POWER_AC:
2302                 final_mode = IWL_POWER_MODE_CAM;
2303                 break;
2304         default:
2305                 final_mode = mode;
2306                 break;
2307         }
2308
2309         cmd.keep_alive_beacons = 0;
2310
2311         iwl4965_update_power_cmd(priv, &cmd, final_mode);
2312
2313         rc = iwl4965_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
2314
2315         if (final_mode == IWL_POWER_MODE_CAM)
2316                 clear_bit(STATUS_POWER_PMI, &priv->status);
2317         else
2318                 set_bit(STATUS_POWER_PMI, &priv->status);
2319
2320         return rc;
2321 }
2322
2323 int iwl4965_is_network_packet(struct iwl4965_priv *priv, struct ieee80211_hdr *header)
2324 {
2325         /* Filter incoming packets to determine if they are targeted toward
2326          * this network, discarding packets coming from ourselves */
2327         switch (priv->iw_mode) {
2328         case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source    | BSSID */
2329                 /* packets from our adapter are dropped (echo) */
2330                 if (!compare_ether_addr(header->addr2, priv->mac_addr))
2331                         return 0;
2332                 /* {broad,multi}cast packets to our IBSS go through */
2333                 if (is_multicast_ether_addr(header->addr1))
2334                         return !compare_ether_addr(header->addr3, priv->bssid);
2335                 /* packets to our adapter go through */
2336                 return !compare_ether_addr(header->addr1, priv->mac_addr);
2337         case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
2338                 /* packets from our adapter are dropped (echo) */
2339                 if (!compare_ether_addr(header->addr3, priv->mac_addr))
2340                         return 0;
2341                 /* {broad,multi}cast packets to our BSS go through */
2342                 if (is_multicast_ether_addr(header->addr1))
2343                         return !compare_ether_addr(header->addr2, priv->bssid);
2344                 /* packets to our adapter go through */
2345                 return !compare_ether_addr(header->addr1, priv->mac_addr);
2346         }
2347
2348         return 1;
2349 }
2350
2351 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
2352
2353 static const char *iwl4965_get_tx_fail_reason(u32 status)
2354 {
2355         switch (status & TX_STATUS_MSK) {
2356         case TX_STATUS_SUCCESS:
2357                 return "SUCCESS";
2358                 TX_STATUS_ENTRY(SHORT_LIMIT);
2359                 TX_STATUS_ENTRY(LONG_LIMIT);
2360                 TX_STATUS_ENTRY(FIFO_UNDERRUN);
2361                 TX_STATUS_ENTRY(MGMNT_ABORT);
2362                 TX_STATUS_ENTRY(NEXT_FRAG);
2363                 TX_STATUS_ENTRY(LIFE_EXPIRE);
2364                 TX_STATUS_ENTRY(DEST_PS);
2365                 TX_STATUS_ENTRY(ABORTED);
2366                 TX_STATUS_ENTRY(BT_RETRY);
2367                 TX_STATUS_ENTRY(STA_INVALID);
2368                 TX_STATUS_ENTRY(FRAG_DROPPED);
2369                 TX_STATUS_ENTRY(TID_DISABLE);
2370                 TX_STATUS_ENTRY(FRAME_FLUSHED);
2371                 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
2372                 TX_STATUS_ENTRY(TX_LOCKED);
2373                 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
2374         }
2375
2376         return "UNKNOWN";
2377 }
2378
2379 /**
2380  * iwl4965_scan_cancel - Cancel any currently executing HW scan
2381  *
2382  * NOTE: priv->mutex is not required before calling this function
2383  */
2384 static int iwl4965_scan_cancel(struct iwl4965_priv *priv)
2385 {
2386         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
2387                 clear_bit(STATUS_SCANNING, &priv->status);
2388                 return 0;
2389         }
2390
2391         if (test_bit(STATUS_SCANNING, &priv->status)) {
2392                 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2393                         IWL_DEBUG_SCAN("Queuing scan abort.\n");
2394                         set_bit(STATUS_SCAN_ABORTING, &priv->status);
2395                         queue_work(priv->workqueue, &priv->abort_scan);
2396
2397                 } else
2398                         IWL_DEBUG_SCAN("Scan abort already in progress.\n");
2399
2400                 return test_bit(STATUS_SCANNING, &priv->status);
2401         }
2402
2403         return 0;
2404 }
2405
2406 /**
2407  * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
2408  * @ms: amount of time to wait (in milliseconds) for scan to abort
2409  *
2410  * NOTE: priv->mutex must be held before calling this function
2411  */
2412 static int iwl4965_scan_cancel_timeout(struct iwl4965_priv *priv, unsigned long ms)
2413 {
2414         unsigned long now = jiffies;
2415         int ret;
2416
2417         ret = iwl4965_scan_cancel(priv);
2418         if (ret && ms) {
2419                 mutex_unlock(&priv->mutex);
2420                 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
2421                                 test_bit(STATUS_SCANNING, &priv->status))
2422                         msleep(1);
2423                 mutex_lock(&priv->mutex);
2424
2425                 return test_bit(STATUS_SCANNING, &priv->status);
2426         }
2427
2428         return ret;
2429 }
2430
2431 static void iwl4965_sequence_reset(struct iwl4965_priv *priv)
2432 {
2433         /* Reset ieee stats */
2434
2435         /* We don't reset the net_device_stats (ieee->stats) on
2436          * re-association */
2437
2438         priv->last_seq_num = -1;
2439         priv->last_frag_num = -1;
2440         priv->last_packet_time = 0;
2441
2442         iwl4965_scan_cancel(priv);
2443 }
2444
2445 #define MAX_UCODE_BEACON_INTERVAL       4096
2446 #define INTEL_CONN_LISTEN_INTERVAL      __constant_cpu_to_le16(0xA)
2447
2448 static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
2449 {
2450         u16 new_val = 0;
2451         u16 beacon_factor = 0;
2452
2453         beacon_factor =
2454             (beacon_val + MAX_UCODE_BEACON_INTERVAL)
2455                 / MAX_UCODE_BEACON_INTERVAL;
2456         new_val = beacon_val / beacon_factor;
2457
2458         return cpu_to_le16(new_val);
2459 }
2460
2461 static void iwl4965_setup_rxon_timing(struct iwl4965_priv *priv)
2462 {
2463         u64 interval_tm_unit;
2464         u64 tsf, result;
2465         unsigned long flags;
2466         struct ieee80211_conf *conf = NULL;
2467         u16 beacon_int = 0;
2468
2469         conf = ieee80211_get_hw_conf(priv->hw);
2470
2471         spin_lock_irqsave(&priv->lock, flags);
2472         priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
2473         priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
2474
2475         priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
2476
2477         tsf = priv->timestamp1;
2478         tsf = ((tsf << 32) | priv->timestamp0);
2479
2480         beacon_int = priv->beacon_int;
2481         spin_unlock_irqrestore(&priv->lock, flags);
2482
2483         if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
2484                 if (beacon_int == 0) {
2485                         priv->rxon_timing.beacon_interval = cpu_to_le16(100);
2486                         priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
2487                 } else {
2488                         priv->rxon_timing.beacon_interval =
2489                                 cpu_to_le16(beacon_int);
2490                         priv->rxon_timing.beacon_interval =
2491                             iwl4965_adjust_beacon_interval(
2492                                 le16_to_cpu(priv->rxon_timing.beacon_interval));
2493                 }
2494
2495                 priv->rxon_timing.atim_window = 0;
2496         } else {
2497                 priv->rxon_timing.beacon_interval =
2498                         iwl4965_adjust_beacon_interval(conf->beacon_int);
2499                 /* TODO: we need to get atim_window from upper stack
2500                  * for now we set to 0 */
2501                 priv->rxon_timing.atim_window = 0;
2502         }
2503
2504         interval_tm_unit =
2505                 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
2506         result = do_div(tsf, interval_tm_unit);
2507         priv->rxon_timing.beacon_init_val =
2508             cpu_to_le32((u32) ((u64) interval_tm_unit - result));
2509
2510         IWL_DEBUG_ASSOC
2511             ("beacon interval %d beacon timer %d beacon tim %d\n",
2512                 le16_to_cpu(priv->rxon_timing.beacon_interval),
2513                 le32_to_cpu(priv->rxon_timing.beacon_init_val),
2514                 le16_to_cpu(priv->rxon_timing.atim_window));
2515 }
2516
2517 static int iwl4965_scan_initiate(struct iwl4965_priv *priv)
2518 {
2519         if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2520                 IWL_ERROR("APs don't scan.\n");
2521                 return 0;
2522         }
2523
2524         if (!iwl4965_is_ready_rf(priv)) {
2525                 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
2526                 return -EIO;
2527         }
2528
2529         if (test_bit(STATUS_SCANNING, &priv->status)) {
2530                 IWL_DEBUG_SCAN("Scan already in progress.\n");
2531                 return -EAGAIN;
2532         }
2533
2534         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2535                 IWL_DEBUG_SCAN("Scan request while abort pending.  "
2536                                "Queuing.\n");
2537                 return -EAGAIN;
2538         }
2539
2540         IWL_DEBUG_INFO("Starting scan...\n");
2541         priv->scan_bands = 2;
2542         set_bit(STATUS_SCANNING, &priv->status);
2543         priv->scan_start = jiffies;
2544         priv->scan_pass_start = priv->scan_start;
2545
2546         queue_work(priv->workqueue, &priv->request_scan);
2547
2548         return 0;
2549 }
2550
2551 static int iwl4965_set_rxon_hwcrypto(struct iwl4965_priv *priv, int hw_decrypt)
2552 {
2553         struct iwl4965_rxon_cmd *rxon = &priv->staging_rxon;
2554
2555         if (hw_decrypt)
2556                 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
2557         else
2558                 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
2559
2560         return 0;
2561 }
2562
2563 static void iwl4965_set_flags_for_phymode(struct iwl4965_priv *priv, u8 phymode)
2564 {
2565         if (phymode == MODE_IEEE80211A) {
2566                 priv->staging_rxon.flags &=
2567                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
2568                       | RXON_FLG_CCK_MSK);
2569                 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2570         } else {
2571                 /* Copied from iwl4965_bg_post_associate() */
2572                 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2573                         priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2574                 else
2575                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2576
2577                 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2578                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2579
2580                 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
2581                 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
2582                 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
2583         }
2584 }
2585
2586 /*
2587  * initialize rxon structure with default values from eeprom
2588  */
2589 static void iwl4965_connection_init_rx_config(struct iwl4965_priv *priv)
2590 {
2591         const struct iwl4965_channel_info *ch_info;
2592
2593         memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
2594
2595         switch (priv->iw_mode) {
2596         case IEEE80211_IF_TYPE_AP:
2597                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
2598                 break;
2599
2600         case IEEE80211_IF_TYPE_STA:
2601                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
2602                 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
2603                 break;
2604
2605         case IEEE80211_IF_TYPE_IBSS:
2606                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
2607                 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
2608                 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
2609                                                   RXON_FILTER_ACCEPT_GRP_MSK;
2610                 break;
2611
2612         case IEEE80211_IF_TYPE_MNTR:
2613                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
2614                 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
2615                     RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
2616                 break;
2617         }
2618
2619 #if 0
2620         /* TODO:  Figure out when short_preamble would be set and cache from
2621          * that */
2622         if (!hw_to_local(priv->hw)->short_preamble)
2623                 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2624         else
2625                 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2626 #endif
2627
2628         ch_info = iwl4965_get_channel_info(priv, priv->phymode,
2629                                        le16_to_cpu(priv->staging_rxon.channel));
2630
2631         if (!ch_info)
2632                 ch_info = &priv->channel_info[0];
2633
2634         /*
2635          * in some case A channels are all non IBSS
2636          * in this case force B/G channel
2637          */
2638         if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
2639             !(is_channel_ibss(ch_info)))
2640                 ch_info = &priv->channel_info[0];
2641
2642         priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
2643         if (is_channel_a_band(ch_info))
2644                 priv->phymode = MODE_IEEE80211A;
2645         else
2646                 priv->phymode = MODE_IEEE80211G;
2647
2648         iwl4965_set_flags_for_phymode(priv, priv->phymode);
2649
2650         priv->staging_rxon.ofdm_basic_rates =
2651             (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
2652         priv->staging_rxon.cck_basic_rates =
2653             (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
2654
2655         priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
2656                                         RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
2657         memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2658         memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
2659         priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
2660         priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
2661         iwl4965_set_rxon_chain(priv);
2662 }
2663
2664 static int iwl4965_set_mode(struct iwl4965_priv *priv, int mode)
2665 {
2666         if (mode == IEEE80211_IF_TYPE_IBSS) {
2667                 const struct iwl4965_channel_info *ch_info;
2668
2669                 ch_info = iwl4965_get_channel_info(priv,
2670                         priv->phymode,
2671                         le16_to_cpu(priv->staging_rxon.channel));
2672
2673                 if (!ch_info || !is_channel_ibss(ch_info)) {
2674                         IWL_ERROR("channel %d not IBSS channel\n",
2675                                   le16_to_cpu(priv->staging_rxon.channel));
2676                         return -EINVAL;
2677                 }
2678         }
2679
2680         priv->iw_mode = mode;
2681
2682         iwl4965_connection_init_rx_config(priv);
2683         memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2684
2685         iwl4965_clear_stations_table(priv);
2686
2687         /* dont commit rxon if rf-kill is on*/
2688         if (!iwl4965_is_ready_rf(priv))
2689                 return -EAGAIN;
2690
2691         cancel_delayed_work(&priv->scan_check);
2692         if (iwl4965_scan_cancel_timeout(priv, 100)) {
2693                 IWL_WARNING("Aborted scan still in progress after 100ms\n");
2694                 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2695                 return -EAGAIN;
2696         }
2697
2698         iwl4965_commit_rxon(priv);
2699
2700         return 0;
2701 }
2702
2703 static void iwl4965_build_tx_cmd_hwcrypto(struct iwl4965_priv *priv,
2704                                       struct ieee80211_tx_control *ctl,
2705                                       struct iwl4965_cmd *cmd,
2706                                       struct sk_buff *skb_frag,
2707                                       int last_frag)
2708 {
2709         struct iwl4965_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
2710
2711         switch (keyinfo->alg) {
2712         case ALG_CCMP:
2713                 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
2714                 memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
2715                 IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
2716                 break;
2717
2718         case ALG_TKIP:
2719 #if 0
2720                 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
2721
2722                 if (last_frag)
2723                         memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
2724                                8);
2725                 else
2726                         memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
2727 #endif
2728                 break;
2729
2730         case ALG_WEP:
2731                 cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
2732                         (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
2733
2734                 if (keyinfo->keylen == 13)
2735                         cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
2736
2737                 memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
2738
2739                 IWL_DEBUG_TX("Configuring packet for WEP encryption "
2740                              "with key %d\n", ctl->key_idx);
2741                 break;
2742
2743         default:
2744                 printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
2745                 break;
2746         }
2747 }
2748
2749 /*
2750  * handle build REPLY_TX command notification.
2751  */
2752 static void iwl4965_build_tx_cmd_basic(struct iwl4965_priv *priv,
2753                                   struct iwl4965_cmd *cmd,
2754                                   struct ieee80211_tx_control *ctrl,
2755                                   struct ieee80211_hdr *hdr,
2756                                   int is_unicast, u8 std_id)
2757 {
2758         __le16 *qc;
2759         u16 fc = le16_to_cpu(hdr->frame_control);
2760         __le32 tx_flags = cmd->cmd.tx.tx_flags;
2761
2762         cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
2763         if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
2764                 tx_flags |= TX_CMD_FLG_ACK_MSK;
2765                 if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
2766                         tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2767                 if (ieee80211_is_probe_response(fc) &&
2768                     !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
2769                         tx_flags |= TX_CMD_FLG_TSF_MSK;
2770         } else {
2771                 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
2772                 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2773         }
2774
2775         if (ieee80211_is_back_request(fc))
2776                 tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
2777
2778
2779         cmd->cmd.tx.sta_id = std_id;
2780         if (ieee80211_get_morefrag(hdr))
2781                 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
2782
2783         qc = ieee80211_get_qos_ctrl(hdr);
2784         if (qc) {
2785                 cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
2786                 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
2787         } else
2788                 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
2789
2790         if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
2791                 tx_flags |= TX_CMD_FLG_RTS_MSK;
2792                 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2793         } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
2794                 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2795                 tx_flags |= TX_CMD_FLG_CTS_MSK;
2796         }
2797
2798         if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
2799                 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2800
2801         tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
2802         if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
2803                 if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
2804                     (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
2805                         cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
2806                 else
2807                         cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
2808         } else
2809                 cmd->cmd.tx.timeout.pm_frame_timeout = 0;
2810
2811         cmd->cmd.tx.driver_txop = 0;
2812         cmd->cmd.tx.tx_flags = tx_flags;
2813         cmd->cmd.tx.next_frame_len = 0;
2814 }
2815
2816 /**
2817  * iwl4965_get_sta_id - Find station's index within station table
2818  *
2819  * If new IBSS station, create new entry in station table
2820  */
2821 static int iwl4965_get_sta_id(struct iwl4965_priv *priv,
2822                                 struct ieee80211_hdr *hdr)
2823 {
2824         int sta_id;
2825         u16 fc = le16_to_cpu(hdr->frame_control);
2826         DECLARE_MAC_BUF(mac);
2827
2828         /* If this frame is broadcast or management, use broadcast station id */
2829         if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
2830             is_multicast_ether_addr(hdr->addr1))
2831                 return priv->hw_setting.bcast_sta_id;
2832
2833         switch (priv->iw_mode) {
2834
2835         /* If we are a client station in a BSS network, use the special
2836          * AP station entry (that's the only station we communicate with) */
2837         case IEEE80211_IF_TYPE_STA:
2838                 return IWL_AP_ID;
2839
2840         /* If we are an AP, then find the station, or use BCAST */
2841         case IEEE80211_IF_TYPE_AP:
2842                 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
2843                 if (sta_id != IWL_INVALID_STATION)
2844                         return sta_id;
2845                 return priv->hw_setting.bcast_sta_id;
2846
2847         /* If this frame is going out to an IBSS network, find the station,
2848          * or create a new station table entry */
2849         case IEEE80211_IF_TYPE_IBSS:
2850                 sta_id = iwl4965_hw_find_station(priv, hdr->addr1);
2851                 if (sta_id != IWL_INVALID_STATION)
2852                         return sta_id;
2853
2854                 /* Create new station table entry */
2855                 sta_id = iwl4965_add_station_flags(priv, hdr->addr1,
2856                                                    0, CMD_ASYNC, NULL);
2857
2858                 if (sta_id != IWL_INVALID_STATION)
2859                         return sta_id;
2860
2861                 IWL_DEBUG_DROP("Station %s not in station map. "
2862                                "Defaulting to broadcast...\n",
2863                                print_mac(mac, hdr->addr1));
2864                 iwl4965_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
2865                 return priv->hw_setting.bcast_sta_id;
2866
2867         default:
2868                 IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
2869                 return priv->hw_setting.bcast_sta_id;
2870         }
2871 }
2872
2873 /*
2874  * start REPLY_TX command process
2875  */
2876 static int iwl4965_tx_skb(struct iwl4965_priv *priv,
2877                       struct sk_buff *skb, struct ieee80211_tx_control *ctl)
2878 {
2879         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2880         struct iwl4965_tfd_frame *tfd;
2881         u32 *control_flags;
2882         int txq_id = ctl->queue;
2883         struct iwl4965_tx_queue *txq = NULL;
2884         struct iwl4965_queue *q = NULL;
2885         dma_addr_t phys_addr;
2886         dma_addr_t txcmd_phys;
2887         dma_addr_t scratch_phys;
2888         struct iwl4965_cmd *out_cmd = NULL;
2889         u16 len, idx, len_org;
2890         u8 id, hdr_len, unicast;
2891         u8 sta_id;
2892         u16 seq_number = 0;
2893         u16 fc;
2894         __le16 *qc;
2895         u8 wait_write_ptr = 0;
2896         unsigned long flags;
2897         int rc;
2898
2899         spin_lock_irqsave(&priv->lock, flags);
2900         if (iwl4965_is_rfkill(priv)) {
2901                 IWL_DEBUG_DROP("Dropping - RF KILL\n");
2902                 goto drop_unlock;
2903         }
2904
2905         if (!priv->vif) {
2906                 IWL_DEBUG_DROP("Dropping - !priv->vif\n");
2907                 goto drop_unlock;
2908         }
2909
2910         if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
2911                 IWL_ERROR("ERROR: No TX rate available.\n");
2912                 goto drop_unlock;
2913         }
2914
2915         unicast = !is_multicast_ether_addr(hdr->addr1);
2916         id = 0;
2917
2918         fc = le16_to_cpu(hdr->frame_control);
2919
2920 #ifdef CONFIG_IWL4965_DEBUG
2921         if (ieee80211_is_auth(fc))
2922                 IWL_DEBUG_TX("Sending AUTH frame\n");
2923         else if (ieee80211_is_assoc_request(fc))
2924                 IWL_DEBUG_TX("Sending ASSOC frame\n");
2925         else if (ieee80211_is_reassoc_request(fc))
2926                 IWL_DEBUG_TX("Sending REASSOC frame\n");
2927 #endif
2928
2929         /* drop all data frame if we are not associated */
2930         if (!iwl4965_is_associated(priv) && !priv->assoc_id &&
2931             ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
2932                 IWL_DEBUG_DROP("Dropping - !iwl4965_is_associated\n");
2933                 goto drop_unlock;
2934         }
2935
2936         spin_unlock_irqrestore(&priv->lock, flags);
2937
2938         hdr_len = ieee80211_get_hdrlen(fc);
2939
2940         /* Find (or create) index into station table for destination station */
2941         sta_id = iwl4965_get_sta_id(priv, hdr);
2942         if (sta_id == IWL_INVALID_STATION) {
2943                 DECLARE_MAC_BUF(mac);
2944
2945                 IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
2946                                print_mac(mac, hdr->addr1));
2947                 goto drop;
2948         }
2949
2950         IWL_DEBUG_RATE("station Id %d\n", sta_id);
2951
2952         qc = ieee80211_get_qos_ctrl(hdr);
2953         if (qc) {
2954                 u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
2955                 seq_number = priv->stations[sta_id].tid[tid].seq_number &
2956                                 IEEE80211_SCTL_SEQ;
2957                 hdr->seq_ctrl = cpu_to_le16(seq_number) |
2958                         (hdr->seq_ctrl &
2959                                 __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
2960                 seq_number += 0x10;
2961 #ifdef CONFIG_IWL4965_HT
2962 #ifdef CONFIG_IWL4965_HT_AGG
2963                 /* aggregation is on for this <sta,tid> */
2964                 if (ctl->flags & IEEE80211_TXCTL_HT_MPDU_AGG)
2965                         txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
2966 #endif /* CONFIG_IWL4965_HT_AGG */
2967 #endif /* CONFIG_IWL4965_HT */
2968         }
2969
2970         /* Descriptor for chosen Tx queue */
2971         txq = &priv->txq[txq_id];
2972         q = &txq->q;
2973
2974         spin_lock_irqsave(&priv->lock, flags);
2975
2976         /* Set up first empty TFD within this queue's circular TFD buffer */
2977         tfd = &txq->bd[q->write_ptr];
2978         memset(tfd, 0, sizeof(*tfd));
2979         control_flags = (u32 *) tfd;
2980         idx = get_cmd_index(q, q->write_ptr, 0);
2981
2982         /* Set up driver data for this TFD */
2983         memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl4965_tx_info));
2984         txq->txb[q->write_ptr].skb[0] = skb;
2985         memcpy(&(txq->txb[q->write_ptr].status.control),
2986                ctl, sizeof(struct ieee80211_tx_control));
2987
2988         /* Set up first empty entry in queue's array of Tx/cmd buffers */
2989         out_cmd = &txq->cmd[idx];
2990         memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
2991         memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
2992
2993         /*
2994          * Set up the Tx-command (not MAC!) header.
2995          * Store the chosen Tx queue and TFD index within the sequence field;
2996          * after Tx, uCode's Tx response will return this value so driver can
2997          * locate the frame within the tx queue and do post-tx processing.
2998          */
2999         out_cmd->hdr.cmd = REPLY_TX;
3000         out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
3001                                 INDEX_TO_SEQ(q->write_ptr)));
3002
3003         /* Copy MAC header from skb into command buffer */
3004         memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
3005
3006         /*
3007          * Use the first empty entry in this queue's command buffer array
3008          * to contain the Tx command and MAC header concatenated together
3009          * (payload data will be in another buffer).
3010          * Size of this varies, due to varying MAC header length.
3011          * If end is not dword aligned, we'll have 2 extra bytes at the end
3012          * of the MAC header (device reads on dword boundaries).
3013          * We'll tell device about this padding later.
3014          */
3015         len = priv->hw_setting.tx_cmd_len +
3016                 sizeof(struct iwl4965_cmd_header) + hdr_len;
3017
3018         len_org = len;
3019         len = (len + 3) & ~3;
3020
3021         if (len_org != len)
3022                 len_org = 1;
3023         else
3024                 len_org = 0;
3025
3026         /* Physical address of this Tx command's header (not MAC header!),
3027          * within command buffer array. */
3028         txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl4965_cmd) * idx +
3029                      offsetof(struct iwl4965_cmd, hdr);
3030
3031         /* Add buffer containing Tx command and MAC(!) header to TFD's
3032          * first entry */
3033         iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
3034
3035         if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
3036                 iwl4965_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
3037
3038         /* Set up TFD's 2nd entry to point directly to remainder of skb,
3039          * if any (802.11 null frames have no payload). */
3040         len = skb->len - hdr_len;
3041         if (len) {
3042                 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
3043                                            len, PCI_DMA_TODEVICE);
3044                 iwl4965_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
3045         }
3046
3047         /* Tell 4965 about any 2-byte padding after MAC header */
3048         if (len_org)
3049                 out_cmd->cmd.tx.tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
3050
3051         /* Total # bytes to be transmitted */
3052         len = (u16)skb->len;
3053         out_cmd->cmd.tx.len = cpu_to_le16(len);
3054
3055         /* TODO need this for burst mode later on */
3056         iwl4965_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
3057
3058         /* set is_hcca to 0; it probably will never be implemented */
3059         iwl4965_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
3060
3061         scratch_phys = txcmd_phys + sizeof(struct iwl4965_cmd_header) +
3062                 offsetof(struct iwl4965_tx_cmd, scratch);
3063         out_cmd->cmd.tx.dram_lsb_ptr = cpu_to_le32(scratch_phys);
3064         out_cmd->cmd.tx.dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
3065
3066 #ifdef CONFIG_IWL4965_HT_AGG
3067 #ifdef CONFIG_IWL4965_HT
3068         /* TODO: move this functionality to rate scaling */
3069         iwl4965_tl_get_stats(priv, hdr);
3070 #endif /* CONFIG_IWL4965_HT_AGG */
3071 #endif /*CONFIG_IWL4965_HT */
3072
3073
3074         if (!ieee80211_get_morefrag(hdr)) {
3075                 txq->need_update = 1;
3076                 if (qc) {
3077                         u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
3078                         priv->stations[sta_id].tid[tid].seq_number = seq_number;
3079                 }
3080         } else {
3081                 wait_write_ptr = 1;
3082                 txq->need_update = 0;
3083         }
3084
3085         iwl4965_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
3086                            sizeof(out_cmd->cmd.tx));
3087
3088         iwl4965_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
3089                            ieee80211_get_hdrlen(fc));
3090
3091         /* Set up entry for this TFD in Tx byte-count array */
3092         iwl4965_tx_queue_update_wr_ptr(priv, txq, len);
3093
3094         /* Tell device the write index *just past* this latest filled TFD */
3095         q->write_ptr = iwl4965_queue_inc_wrap(q->write_ptr, q->n_bd);
3096         rc = iwl4965_tx_queue_update_write_ptr(priv, txq);
3097         spin_unlock_irqrestore(&priv->lock, flags);
3098
3099         if (rc)
3100                 return rc;
3101
3102         if ((iwl4965_queue_space(q) < q->high_mark)
3103             && priv->mac80211_registered) {
3104                 if (wait_write_ptr) {
3105                         spin_lock_irqsave(&priv->lock, flags);
3106                         txq->need_update = 1;
3107                         iwl4965_tx_queue_update_write_ptr(priv, txq);
3108                         spin_unlock_irqrestore(&priv->lock, flags);
3109                 }
3110
3111                 ieee80211_stop_queue(priv->hw, ctl->queue);
3112         }
3113
3114         return 0;
3115
3116 drop_unlock:
3117         spin_unlock_irqrestore(&priv->lock, flags);
3118 drop:
3119         return -1;
3120 }
3121
3122 static void iwl4965_set_rate(struct iwl4965_priv *priv)
3123 {
3124         const struct ieee80211_hw_mode *hw = NULL;
3125         struct ieee80211_rate *rate;
3126         int i;
3127
3128         hw = iwl4965_get_hw_mode(priv, priv->phymode);
3129         if (!hw) {
3130                 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
3131                 return;
3132         }
3133
3134         priv->active_rate = 0;
3135         priv->active_rate_basic = 0;
3136
3137         IWL_DEBUG_RATE("Setting rates for 802.11%c\n",