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