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