]> err.no Git - linux-2.6/blob - drivers/net/wireless/iwlwifi/iwl4965-base.c
iwlwifi: move add sta handler to iwl-sta.c
[linux-2.6] / drivers / net / wireless / iwlwifi / iwl4965-base.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
4  *
5  * Portions of this file are derived from the ipw3945 project, as well
6  * as portions of the ieee80211 subsystem header files.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of version 2 of the GNU General Public License as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20  *
21  * The full GNU General Public License is included in this distribution in the
22  * file called LICENSE.
23  *
24  * Contact Information:
25  * James P. Ketrenos <ipw2100-admin@linux.intel.com>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/version.h>
33 #include <linux/init.h>
34 #include <linux/pci.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/delay.h>
37 #include <linux/skbuff.h>
38 #include <linux/netdevice.h>
39 #include <linux/wireless.h>
40 #include <linux/firmware.h>
41 #include <linux/etherdevice.h>
42 #include <linux/if_arp.h>
43
44 #include <net/mac80211.h>
45
46 #include <asm/div64.h>
47
48 #include "iwl-eeprom.h"
49 #include "iwl-dev.h"
50 #include "iwl-core.h"
51 #include "iwl-io.h"
52 #include "iwl-helpers.h"
53 #include "iwl-sta.h"
54 #include "iwl-calib.h"
55
56
57 /******************************************************************************
58  *
59  * module boiler plate
60  *
61  ******************************************************************************/
62
63 /*
64  * module name, copyright, version, etc.
65  * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
66  */
67
68 #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link 4965AGN driver for Linux"
69
70 #ifdef CONFIG_IWLWIFI_DEBUG
71 #define VD "d"
72 #else
73 #define VD
74 #endif
75
76 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
77 #define VS "s"
78 #else
79 #define VS
80 #endif
81
82 #define DRV_VERSION     IWLWIFI_VERSION VD VS
83
84
85 MODULE_DESCRIPTION(DRV_DESCRIPTION);
86 MODULE_VERSION(DRV_VERSION);
87 MODULE_AUTHOR(DRV_COPYRIGHT);
88 MODULE_LICENSE("GPL");
89
90 static const struct ieee80211_supported_band *iwl_get_hw_mode(
91                 struct iwl_priv *priv, enum ieee80211_band band)
92 {
93         return priv->hw->wiphy->bands[band];
94 }
95
96 static int iwl4965_is_empty_essid(const char *essid, int essid_len)
97 {
98         /* Single white space is for Linksys APs */
99         if (essid_len == 1 && essid[0] == ' ')
100                 return 1;
101
102         /* Otherwise, if the entire essid is 0, we assume it is hidden */
103         while (essid_len) {
104                 essid_len--;
105                 if (essid[essid_len] != '\0')
106                         return 0;
107         }
108
109         return 1;
110 }
111
112 static const char *iwl4965_escape_essid(const char *essid, u8 essid_len)
113 {
114         static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
115         const char *s = essid;
116         char *d = escaped;
117
118         if (iwl4965_is_empty_essid(essid, essid_len)) {
119                 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
120                 return escaped;
121         }
122
123         essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
124         while (essid_len--) {
125                 if (*s == '\0') {
126                         *d++ = '\\';
127                         *d++ = '0';
128                         s++;
129                 } else
130                         *d++ = *s++;
131         }
132         *d = '\0';
133         return escaped;
134 }
135
136 /*************** STATION TABLE MANAGEMENT ****
137  * mac80211 should be examined to determine if sta_info is duplicating
138  * the functionality provided here
139  */
140
141 /**************************************************************/
142
143
144
145 static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
146 {
147         struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
148
149         if (hw_decrypt)
150                 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
151         else
152                 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
153
154 }
155
156 /**
157  * iwl4965_check_rxon_cmd - validate RXON structure is valid
158  *
159  * NOTE:  This is really only useful during development and can eventually
160  * be #ifdef'd out once the driver is stable and folks aren't actively
161  * making changes
162  */
163 static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
164 {
165         int error = 0;
166         int counter = 1;
167
168         if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
169                 error |= le32_to_cpu(rxon->flags &
170                                 (RXON_FLG_TGJ_NARROW_BAND_MSK |
171                                  RXON_FLG_RADAR_DETECT_MSK));
172                 if (error)
173                         IWL_WARNING("check 24G fields %d | %d\n",
174                                     counter++, error);
175         } else {
176                 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
177                                 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
178                 if (error)
179                         IWL_WARNING("check 52 fields %d | %d\n",
180                                     counter++, error);
181                 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
182                 if (error)
183                         IWL_WARNING("check 52 CCK %d | %d\n",
184                                     counter++, error);
185         }
186         error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
187         if (error)
188                 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
189
190         /* make sure basic rates 6Mbps and 1Mbps are supported */
191         error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
192                   ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
193         if (error)
194                 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
195
196         error |= (le16_to_cpu(rxon->assoc_id) > 2007);
197         if (error)
198                 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
199
200         error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
201                         == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
202         if (error)
203                 IWL_WARNING("check CCK and short slot %d | %d\n",
204                             counter++, error);
205
206         error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
207                         == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
208         if (error)
209                 IWL_WARNING("check CCK & auto detect %d | %d\n",
210                             counter++, error);
211
212         error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
213                         RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
214         if (error)
215                 IWL_WARNING("check TGG and auto detect %d | %d\n",
216                             counter++, error);
217
218         if (error)
219                 IWL_WARNING("Tuning to channel %d\n",
220                             le16_to_cpu(rxon->channel));
221
222         if (error) {
223                 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
224                 return -1;
225         }
226         return 0;
227 }
228
229 /**
230  * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
231  * @priv: staging_rxon is compared to active_rxon
232  *
233  * If the RXON structure is changing enough to require a new tune,
234  * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
235  * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
236  */
237 static int iwl4965_full_rxon_required(struct iwl_priv *priv)
238 {
239
240         /* These items are only settable from the full RXON command */
241         if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
242             compare_ether_addr(priv->staging_rxon.bssid_addr,
243                                priv->active_rxon.bssid_addr) ||
244             compare_ether_addr(priv->staging_rxon.node_addr,
245                                priv->active_rxon.node_addr) ||
246             compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
247                                priv->active_rxon.wlap_bssid_addr) ||
248             (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
249             (priv->staging_rxon.channel != priv->active_rxon.channel) ||
250             (priv->staging_rxon.air_propagation !=
251              priv->active_rxon.air_propagation) ||
252             (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
253              priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
254             (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
255              priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
256             (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
257             (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
258                 return 1;
259
260         /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
261          * be updated with the RXON_ASSOC command -- however only some
262          * flag transitions are allowed using RXON_ASSOC */
263
264         /* Check if we are not switching bands */
265         if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
266             (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
267                 return 1;
268
269         /* Check if we are switching association toggle */
270         if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
271                 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
272                 return 1;
273
274         return 0;
275 }
276
277 /**
278  * iwl4965_commit_rxon - commit staging_rxon to hardware
279  *
280  * The RXON command in staging_rxon is committed to the hardware and
281  * the active_rxon structure is updated with the new data.  This
282  * function correctly transitions out of the RXON_ASSOC_MSK state if
283  * a HW tune is required based on the RXON structure changes.
284  */
285 static int iwl4965_commit_rxon(struct iwl_priv *priv)
286 {
287         /* cast away the const for active_rxon in this function */
288         struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
289         DECLARE_MAC_BUF(mac);
290         int rc = 0;
291
292         if (!iwl_is_alive(priv))
293                 return -1;
294
295         /* always get timestamp with Rx frame */
296         priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
297
298         rc = iwl4965_check_rxon_cmd(&priv->staging_rxon);
299         if (rc) {
300                 IWL_ERROR("Invalid RXON configuration.  Not committing.\n");
301                 return -EINVAL;
302         }
303
304         /* If we don't need to send a full RXON, we can use
305          * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
306          * and other flags for the current radio configuration. */
307         if (!iwl4965_full_rxon_required(priv)) {
308                 rc = iwl_send_rxon_assoc(priv);
309                 if (rc) {
310                         IWL_ERROR("Error setting RXON_ASSOC "
311                                   "configuration (%d).\n", rc);
312                         return rc;
313                 }
314
315                 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
316
317                 return 0;
318         }
319
320         /* station table will be cleared */
321         priv->assoc_station_added = 0;
322
323         /* If we are currently associated and the new config requires
324          * an RXON_ASSOC and the new config wants the associated mask enabled,
325          * we must clear the associated from the active configuration
326          * before we apply the new config */
327         if (iwl_is_associated(priv) &&
328             (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
329                 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
330                 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
331
332                 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
333                                       sizeof(struct iwl_rxon_cmd),
334                                       &priv->active_rxon);
335
336                 /* If the mask clearing failed then we set
337                  * active_rxon back to what it was previously */
338                 if (rc) {
339                         active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
340                         IWL_ERROR("Error clearing ASSOC_MSK on current "
341                                   "configuration (%d).\n", rc);
342                         return rc;
343                 }
344         }
345
346         IWL_DEBUG_INFO("Sending RXON\n"
347                        "* with%s RXON_FILTER_ASSOC_MSK\n"
348                        "* channel = %d\n"
349                        "* bssid = %s\n",
350                        ((priv->staging_rxon.filter_flags &
351                          RXON_FILTER_ASSOC_MSK) ? "" : "out"),
352                        le16_to_cpu(priv->staging_rxon.channel),
353                        print_mac(mac, priv->staging_rxon.bssid_addr));
354
355         iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
356         /* Apply the new configuration */
357         rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
358                               sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
359         if (rc) {
360                 IWL_ERROR("Error setting new configuration (%d).\n", rc);
361                 return rc;
362         }
363
364         iwl_remove_station(priv, iwl_bcast_addr, 0);
365         iwlcore_clear_stations_table(priv);
366
367         if (!priv->error_recovering)
368                 priv->start_calib = 0;
369
370         iwl_init_sensitivity(priv);
371
372         memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
373
374         /* If we issue a new RXON command which required a tune then we must
375          * send a new TXPOWER command or we won't be able to Tx any frames */
376         rc = iwl4965_hw_reg_send_txpower(priv);
377         if (rc) {
378                 IWL_ERROR("Error setting Tx power (%d).\n", rc);
379                 return rc;
380         }
381
382         /* Add the broadcast address so we can send broadcast frames */
383         if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
384             IWL_INVALID_STATION) {
385                 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
386                 return -EIO;
387         }
388
389         /* If we have set the ASSOC_MSK and we are in BSS mode then
390          * add the IWL_AP_ID to the station rate table */
391         if (iwl_is_associated(priv) &&
392             (priv->iw_mode == IEEE80211_IF_TYPE_STA)) {
393                 if (iwl_rxon_add_station(priv, priv->active_rxon.bssid_addr, 1)
394                     == IWL_INVALID_STATION) {
395                         IWL_ERROR("Error adding AP address for transmit.\n");
396                         return -EIO;
397                 }
398                 priv->assoc_station_added = 1;
399                 if (priv->default_wep_key &&
400                     iwl_send_static_wepkey_cmd(priv, 0))
401                         IWL_ERROR("Could not send WEP static key.\n");
402         }
403
404         return 0;
405 }
406
407 void iwl4965_update_chain_flags(struct iwl_priv *priv)
408 {
409
410         iwl_set_rxon_chain(priv);
411         iwl4965_commit_rxon(priv);
412 }
413
414 static int iwl4965_send_bt_config(struct iwl_priv *priv)
415 {
416         struct iwl4965_bt_cmd bt_cmd = {
417                 .flags = 3,
418                 .lead_time = 0xAA,
419                 .max_kill = 1,
420                 .kill_ack_mask = 0,
421                 .kill_cts_mask = 0,
422         };
423
424         return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
425                                 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
426 }
427
428 static int iwl4965_send_scan_abort(struct iwl_priv *priv)
429 {
430         int ret = 0;
431         struct iwl_rx_packet *res;
432         struct iwl_host_cmd cmd = {
433                 .id = REPLY_SCAN_ABORT_CMD,
434                 .meta.flags = CMD_WANT_SKB,
435         };
436
437         /* If there isn't a scan actively going on in the hardware
438          * then we are in between scan bands and not actually
439          * actively scanning, so don't send the abort command */
440         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
441                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
442                 return 0;
443         }
444
445         ret = iwl_send_cmd_sync(priv, &cmd);
446         if (ret) {
447                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
448                 return ret;
449         }
450
451         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
452         if (res->u.status != CAN_ABORT_STATUS) {
453                 /* The scan abort will return 1 for success or
454                  * 2 for "failure".  A failure condition can be
455                  * due to simply not being in an active scan which
456                  * can occur if we send the scan abort before we
457                  * the microcode has notified us that a scan is
458                  * completed. */
459                 IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
460                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
461                 clear_bit(STATUS_SCAN_HW, &priv->status);
462         }
463
464         dev_kfree_skb_any(cmd.meta.u.skb);
465
466         return ret;
467 }
468
469 /*
470  * CARD_STATE_CMD
471  *
472  * Use: Sets the device's internal card state to enable, disable, or halt
473  *
474  * When in the 'enable' state the card operates as normal.
475  * When in the 'disable' state, the card enters into a low power mode.
476  * When in the 'halt' state, the card is shut down and must be fully
477  * restarted to come back on.
478  */
479 static int iwl4965_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
480 {
481         struct iwl_host_cmd cmd = {
482                 .id = REPLY_CARD_STATE_CMD,
483                 .len = sizeof(u32),
484                 .data = &flags,
485                 .meta.flags = meta_flag,
486         };
487
488         return iwl_send_cmd(priv, &cmd);
489 }
490
491 static void iwl_clear_free_frames(struct iwl_priv *priv)
492 {
493         struct list_head *element;
494
495         IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
496                        priv->frames_count);
497
498         while (!list_empty(&priv->free_frames)) {
499                 element = priv->free_frames.next;
500                 list_del(element);
501                 kfree(list_entry(element, struct iwl_frame, list));
502                 priv->frames_count--;
503         }
504
505         if (priv->frames_count) {
506                 IWL_WARNING("%d frames still in use.  Did we lose one?\n",
507                             priv->frames_count);
508                 priv->frames_count = 0;
509         }
510 }
511
512 static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
513 {
514         struct iwl_frame *frame;
515         struct list_head *element;
516         if (list_empty(&priv->free_frames)) {
517                 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
518                 if (!frame) {
519                         IWL_ERROR("Could not allocate frame!\n");
520                         return NULL;
521                 }
522
523                 priv->frames_count++;
524                 return frame;
525         }
526
527         element = priv->free_frames.next;
528         list_del(element);
529         return list_entry(element, struct iwl_frame, list);
530 }
531
532 static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
533 {
534         memset(frame, 0, sizeof(*frame));
535         list_add(&frame->list, &priv->free_frames);
536 }
537
538 unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
539                                 struct ieee80211_hdr *hdr,
540                                 const u8 *dest, int left)
541 {
542
543         if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
544             ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
545              (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
546                 return 0;
547
548         if (priv->ibss_beacon->len > left)
549                 return 0;
550
551         memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
552
553         return priv->ibss_beacon->len;
554 }
555
556 static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
557 {
558         int i;
559         int rate_mask;
560
561         /* Set rate mask*/
562         if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
563                 rate_mask = priv->active_rate_basic & 0xF;
564         else
565                 rate_mask = priv->active_rate_basic & 0xFF0;
566
567         /* Find lowest valid rate */
568         for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
569                                         i = iwl_rates[i].next_ieee) {
570                 if (rate_mask & (1 << i))
571                         return iwl_rates[i].plcp;
572         }
573
574         /* No valid rate was found. Assign the lowest one */
575         if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
576                 return IWL_RATE_1M_PLCP;
577         else
578                 return IWL_RATE_6M_PLCP;
579 }
580
581 static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
582 {
583         struct iwl_frame *frame;
584         unsigned int frame_size;
585         int rc;
586         u8 rate;
587
588         frame = iwl_get_free_frame(priv);
589
590         if (!frame) {
591                 IWL_ERROR("Could not obtain free frame buffer for beacon "
592                           "command.\n");
593                 return -ENOMEM;
594         }
595
596         rate = iwl4965_rate_get_lowest_plcp(priv);
597
598         frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
599
600         rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
601                               &frame->u.cmd[0]);
602
603         iwl_free_frame(priv, frame);
604
605         return rc;
606 }
607
608 /******************************************************************************
609  *
610  * Misc. internal state and helper functions
611  *
612  ******************************************************************************/
613
614 /**
615  * iwl4965_supported_rate_to_ie - fill in the supported rate in IE field
616  *
617  * return : set the bit for each supported rate insert in ie
618  */
619 static u16 iwl4965_supported_rate_to_ie(u8 *ie, u16 supported_rate,
620                                     u16 basic_rate, int *left)
621 {
622         u16 ret_rates = 0, bit;
623         int i;
624         u8 *cnt = ie;
625         u8 *rates = ie + 1;
626
627         for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
628                 if (bit & supported_rate) {
629                         ret_rates |= bit;
630                         rates[*cnt] = iwl_rates[i].ieee |
631                                 ((bit & basic_rate) ? 0x80 : 0x00);
632                         (*cnt)++;
633                         (*left)--;
634                         if ((*left <= 0) ||
635                             (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
636                                 break;
637                 }
638         }
639
640         return ret_rates;
641 }
642
643 #ifdef CONFIG_IWL4965_HT
644 static void iwl4965_ht_conf(struct iwl_priv *priv,
645                             struct ieee80211_bss_conf *bss_conf)
646 {
647         struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
648         struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
649         struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
650
651         IWL_DEBUG_MAC80211("enter: \n");
652
653         iwl_conf->is_ht = bss_conf->assoc_ht;
654
655         if (!iwl_conf->is_ht)
656                 return;
657
658         priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
659
660         if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
661                 iwl_conf->sgf |= HT_SHORT_GI_20MHZ;
662         if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
663                 iwl_conf->sgf |= HT_SHORT_GI_40MHZ;
664
665         iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
666         iwl_conf->max_amsdu_size =
667                 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
668
669         iwl_conf->supported_chan_width =
670                 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
671         iwl_conf->extension_chan_offset =
672                 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
673         /* If no above or below channel supplied disable FAT channel */
674         if (iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_ABOVE &&
675             iwl_conf->extension_chan_offset != IWL_EXT_CHANNEL_OFFSET_BELOW)
676                 iwl_conf->supported_chan_width = 0;
677
678         iwl_conf->tx_mimo_ps_mode =
679                 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
680         memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
681
682         iwl_conf->control_channel = ht_bss_conf->primary_channel;
683         iwl_conf->tx_chan_width =
684                 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
685         iwl_conf->ht_protection =
686                 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
687         iwl_conf->non_GF_STA_present =
688                 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
689
690         IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
691         IWL_DEBUG_MAC80211("leave\n");
692 }
693
694 static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
695                         u8 *pos, int *left)
696 {
697         struct ieee80211_ht_cap *ht_cap;
698
699         if (!sband || !sband->ht_info.ht_supported)
700                 return;
701
702         if (*left < sizeof(struct ieee80211_ht_cap))
703                 return;
704
705         *pos++ = sizeof(struct ieee80211_ht_cap);
706         ht_cap = (struct ieee80211_ht_cap *) pos;
707
708         ht_cap->cap_info = cpu_to_le16(sband->ht_info.cap);
709         memcpy(ht_cap->supp_mcs_set, sband->ht_info.supp_mcs_set, 16);
710         ht_cap->ampdu_params_info =
711                 (sband->ht_info.ampdu_factor & IEEE80211_HT_CAP_AMPDU_FACTOR) |
712                 ((sband->ht_info.ampdu_density << 2) &
713                         IEEE80211_HT_CAP_AMPDU_DENSITY);
714         *left -= sizeof(struct ieee80211_ht_cap);
715 }
716 #else
717 static inline void iwl4965_ht_conf(struct iwl_priv *priv,
718                                    struct ieee80211_bss_conf *bss_conf)
719 {
720 }
721 static void iwl_ht_cap_to_ie(const struct ieee80211_supported_band *sband,
722                         u8 *pos, int *left)
723 {
724 }
725 #endif
726
727
728 /**
729  * iwl4965_fill_probe_req - fill in all required fields and IE for probe request
730  */
731 static u16 iwl4965_fill_probe_req(struct iwl_priv *priv,
732                                   enum ieee80211_band band,
733                                   struct ieee80211_mgmt *frame,
734                                   int left, int is_direct)
735 {
736         int len = 0;
737         u8 *pos = NULL;
738         u16 active_rates, ret_rates, cck_rates, active_rate_basic;
739         const struct ieee80211_supported_band *sband =
740                                                 iwl_get_hw_mode(priv, band);
741
742         /* Make sure there is enough space for the probe request,
743          * two mandatory IEs and the data */
744         left -= 24;
745         if (left < 0)
746                 return 0;
747         len += 24;
748
749         frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
750         memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
751         memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
752         memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
753         frame->seq_ctrl = 0;
754
755         /* fill in our indirect SSID IE */
756         /* ...next IE... */
757
758         left -= 2;
759         if (left < 0)
760                 return 0;
761         len += 2;
762         pos = &(frame->u.probe_req.variable[0]);
763         *pos++ = WLAN_EID_SSID;
764         *pos++ = 0;
765
766         /* fill in our direct SSID IE... */
767         if (is_direct) {
768                 /* ...next IE... */
769                 left -= 2 + priv->essid_len;
770                 if (left < 0)
771                         return 0;
772                 /* ... fill it in... */
773                 *pos++ = WLAN_EID_SSID;
774                 *pos++ = priv->essid_len;
775                 memcpy(pos, priv->essid, priv->essid_len);
776                 pos += priv->essid_len;
777                 len += 2 + priv->essid_len;
778         }
779
780         /* fill in supported rate */
781         /* ...next IE... */
782         left -= 2;
783         if (left < 0)
784                 return 0;
785
786         /* ... fill it in... */
787         *pos++ = WLAN_EID_SUPP_RATES;
788         *pos = 0;
789
790         /* exclude 60M rate */
791         active_rates = priv->rates_mask;
792         active_rates &= ~IWL_RATE_60M_MASK;
793
794         active_rate_basic = active_rates & IWL_BASIC_RATES_MASK;
795
796         cck_rates = IWL_CCK_RATES_MASK & active_rates;
797         ret_rates = iwl4965_supported_rate_to_ie(pos, cck_rates,
798                         active_rate_basic, &left);
799         active_rates &= ~ret_rates;
800
801         ret_rates = iwl4965_supported_rate_to_ie(pos, active_rates,
802                                  active_rate_basic, &left);
803         active_rates &= ~ret_rates;
804
805         len += 2 + *pos;
806         pos += (*pos) + 1;
807         if (active_rates == 0)
808                 goto fill_end;
809
810         /* fill in supported extended rate */
811         /* ...next IE... */
812         left -= 2;
813         if (left < 0)
814                 return 0;
815         /* ... fill it in... */
816         *pos++ = WLAN_EID_EXT_SUPP_RATES;
817         *pos = 0;
818         iwl4965_supported_rate_to_ie(pos, active_rates,
819                                  active_rate_basic, &left);
820         if (*pos > 0)
821                 len += 2 + *pos;
822
823  fill_end:
824         /* fill in HT IE */
825         left -= 2;
826         if (left < 0)
827                 return 0;
828
829         *pos++ = WLAN_EID_HT_CAPABILITY;
830         *pos = 0;
831
832         iwl_ht_cap_to_ie(sband, pos, &left);
833
834         if (*pos > 0)
835                 len += 2 + *pos;
836         return (u16)len;
837 }
838
839 /*
840  * QoS  support
841 */
842 static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
843                                        struct iwl4965_qosparam_cmd *qos)
844 {
845
846         return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
847                                 sizeof(struct iwl4965_qosparam_cmd), qos);
848 }
849
850 static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
851 {
852         unsigned long flags;
853
854         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
855                 return;
856
857         if (!priv->qos_data.qos_enable)
858                 return;
859
860         spin_lock_irqsave(&priv->lock, flags);
861         priv->qos_data.def_qos_parm.qos_flags = 0;
862
863         if (priv->qos_data.qos_cap.q_AP.queue_request &&
864             !priv->qos_data.qos_cap.q_AP.txop_request)
865                 priv->qos_data.def_qos_parm.qos_flags |=
866                         QOS_PARAM_FLG_TXOP_TYPE_MSK;
867         if (priv->qos_data.qos_active)
868                 priv->qos_data.def_qos_parm.qos_flags |=
869                         QOS_PARAM_FLG_UPDATE_EDCA_MSK;
870
871 #ifdef CONFIG_IWL4965_HT
872         if (priv->current_ht_config.is_ht)
873                 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
874 #endif /* CONFIG_IWL4965_HT */
875
876         spin_unlock_irqrestore(&priv->lock, flags);
877
878         if (force || iwl_is_associated(priv)) {
879                 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
880                                 priv->qos_data.qos_active,
881                                 priv->qos_data.def_qos_parm.qos_flags);
882
883                 iwl4965_send_qos_params_command(priv,
884                                 &(priv->qos_data.def_qos_parm));
885         }
886 }
887
888 int iwl4965_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
889 {
890         /* Filter incoming packets to determine if they are targeted toward
891          * this network, discarding packets coming from ourselves */
892         switch (priv->iw_mode) {
893         case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source    | BSSID */
894                 /* packets from our adapter are dropped (echo) */
895                 if (!compare_ether_addr(header->addr2, priv->mac_addr))
896                         return 0;
897                 /* {broad,multi}cast packets to our IBSS go through */
898                 if (is_multicast_ether_addr(header->addr1))
899                         return !compare_ether_addr(header->addr3, priv->bssid);
900                 /* packets to our adapter go through */
901                 return !compare_ether_addr(header->addr1, priv->mac_addr);
902         case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
903                 /* packets from our adapter are dropped (echo) */
904                 if (!compare_ether_addr(header->addr3, priv->mac_addr))
905                         return 0;
906                 /* {broad,multi}cast packets to our BSS go through */
907                 if (is_multicast_ether_addr(header->addr1))
908                         return !compare_ether_addr(header->addr2, priv->bssid);
909                 /* packets to our adapter go through */
910                 return !compare_ether_addr(header->addr1, priv->mac_addr);
911         default:
912                 break;
913         }
914
915         return 1;
916 }
917
918 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
919
920 static const char *iwl4965_get_tx_fail_reason(u32 status)
921 {
922         switch (status & TX_STATUS_MSK) {
923         case TX_STATUS_SUCCESS:
924                 return "SUCCESS";
925                 TX_STATUS_ENTRY(SHORT_LIMIT);
926                 TX_STATUS_ENTRY(LONG_LIMIT);
927                 TX_STATUS_ENTRY(FIFO_UNDERRUN);
928                 TX_STATUS_ENTRY(MGMNT_ABORT);
929                 TX_STATUS_ENTRY(NEXT_FRAG);
930                 TX_STATUS_ENTRY(LIFE_EXPIRE);
931                 TX_STATUS_ENTRY(DEST_PS);
932                 TX_STATUS_ENTRY(ABORTED);
933                 TX_STATUS_ENTRY(BT_RETRY);
934                 TX_STATUS_ENTRY(STA_INVALID);
935                 TX_STATUS_ENTRY(FRAG_DROPPED);
936                 TX_STATUS_ENTRY(TID_DISABLE);
937                 TX_STATUS_ENTRY(FRAME_FLUSHED);
938                 TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
939                 TX_STATUS_ENTRY(TX_LOCKED);
940                 TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
941         }
942
943         return "UNKNOWN";
944 }
945
946 /**
947  * iwl4965_scan_cancel - Cancel any currently executing HW scan
948  *
949  * NOTE: priv->mutex is not required before calling this function
950  */
951 static int iwl4965_scan_cancel(struct iwl_priv *priv)
952 {
953         if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
954                 clear_bit(STATUS_SCANNING, &priv->status);
955                 return 0;
956         }
957
958         if (test_bit(STATUS_SCANNING, &priv->status)) {
959                 if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
960                         IWL_DEBUG_SCAN("Queuing scan abort.\n");
961                         set_bit(STATUS_SCAN_ABORTING, &priv->status);
962                         queue_work(priv->workqueue, &priv->abort_scan);
963
964                 } else
965                         IWL_DEBUG_SCAN("Scan abort already in progress.\n");
966
967                 return test_bit(STATUS_SCANNING, &priv->status);
968         }
969
970         return 0;
971 }
972
973 /**
974  * iwl4965_scan_cancel_timeout - Cancel any currently executing HW scan
975  * @ms: amount of time to wait (in milliseconds) for scan to abort
976  *
977  * NOTE: priv->mutex must be held before calling this function
978  */
979 static int iwl4965_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
980 {
981         unsigned long now = jiffies;
982         int ret;
983
984         ret = iwl4965_scan_cancel(priv);
985         if (ret && ms) {
986                 mutex_unlock(&priv->mutex);
987                 while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
988                                 test_bit(STATUS_SCANNING, &priv->status))
989                         msleep(1);
990                 mutex_lock(&priv->mutex);
991
992                 return test_bit(STATUS_SCANNING, &priv->status);
993         }
994
995         return ret;
996 }
997
998 static void iwl4965_sequence_reset(struct iwl_priv *priv)
999 {
1000         /* Reset ieee stats */
1001
1002         /* We don't reset the net_device_stats (ieee->stats) on
1003          * re-association */
1004
1005         priv->last_seq_num = -1;
1006         priv->last_frag_num = -1;
1007         priv->last_packet_time = 0;
1008
1009         iwl4965_scan_cancel(priv);
1010 }
1011
1012 #define MAX_UCODE_BEACON_INTERVAL       4096
1013 #define INTEL_CONN_LISTEN_INTERVAL      __constant_cpu_to_le16(0xA)
1014
1015 static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
1016 {
1017         u16 new_val = 0;
1018         u16 beacon_factor = 0;
1019
1020         beacon_factor =
1021             (beacon_val + MAX_UCODE_BEACON_INTERVAL)
1022                 / MAX_UCODE_BEACON_INTERVAL;
1023         new_val = beacon_val / beacon_factor;
1024
1025         return cpu_to_le16(new_val);
1026 }
1027
1028 static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
1029 {
1030         u64 interval_tm_unit;
1031         u64 tsf, result;
1032         unsigned long flags;
1033         struct ieee80211_conf *conf = NULL;
1034         u16 beacon_int = 0;
1035
1036         conf = ieee80211_get_hw_conf(priv->hw);
1037
1038         spin_lock_irqsave(&priv->lock, flags);
1039         priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
1040         priv->rxon_timing.timestamp.dw[0] =
1041                                 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
1042
1043         priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
1044
1045         tsf = priv->timestamp;
1046
1047         beacon_int = priv->beacon_int;
1048         spin_unlock_irqrestore(&priv->lock, flags);
1049
1050         if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
1051                 if (beacon_int == 0) {
1052                         priv->rxon_timing.beacon_interval = cpu_to_le16(100);
1053                         priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
1054                 } else {
1055                         priv->rxon_timing.beacon_interval =
1056                                 cpu_to_le16(beacon_int);
1057                         priv->rxon_timing.beacon_interval =
1058                             iwl4965_adjust_beacon_interval(
1059                                 le16_to_cpu(priv->rxon_timing.beacon_interval));
1060                 }
1061
1062                 priv->rxon_timing.atim_window = 0;
1063         } else {
1064                 priv->rxon_timing.beacon_interval =
1065                         iwl4965_adjust_beacon_interval(conf->beacon_int);
1066                 /* TODO: we need to get atim_window from upper stack
1067                  * for now we set to 0 */
1068                 priv->rxon_timing.atim_window = 0;
1069         }
1070
1071         interval_tm_unit =
1072                 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
1073         result = do_div(tsf, interval_tm_unit);
1074         priv->rxon_timing.beacon_init_val =
1075             cpu_to_le32((u32) ((u64) interval_tm_unit - result));
1076
1077         IWL_DEBUG_ASSOC
1078             ("beacon interval %d beacon timer %d beacon tim %d\n",
1079                 le16_to_cpu(priv->rxon_timing.beacon_interval),
1080                 le32_to_cpu(priv->rxon_timing.beacon_init_val),
1081                 le16_to_cpu(priv->rxon_timing.atim_window));
1082 }
1083
1084 static int iwl4965_scan_initiate(struct iwl_priv *priv)
1085 {
1086         if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
1087                 IWL_ERROR("APs don't scan.\n");
1088                 return 0;
1089         }
1090
1091         if (!iwl_is_ready_rf(priv)) {
1092                 IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
1093                 return -EIO;
1094         }
1095
1096         if (test_bit(STATUS_SCANNING, &priv->status)) {
1097                 IWL_DEBUG_SCAN("Scan already in progress.\n");
1098                 return -EAGAIN;
1099         }
1100
1101         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
1102                 IWL_DEBUG_SCAN("Scan request while abort pending.  "
1103                                "Queuing.\n");
1104                 return -EAGAIN;
1105         }
1106
1107         IWL_DEBUG_INFO("Starting scan...\n");
1108         priv->scan_bands = 2;
1109         set_bit(STATUS_SCANNING, &priv->status);
1110         priv->scan_start = jiffies;
1111         priv->scan_pass_start = priv->scan_start;
1112
1113         queue_work(priv->workqueue, &priv->request_scan);
1114
1115         return 0;
1116 }
1117
1118
1119 static void iwl4965_set_flags_for_phymode(struct iwl_priv *priv,
1120                                           enum ieee80211_band band)
1121 {
1122         if (band == IEEE80211_BAND_5GHZ) {
1123                 priv->staging_rxon.flags &=
1124                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1125                       | RXON_FLG_CCK_MSK);
1126                 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1127         } else {
1128                 /* Copied from iwl4965_post_associate() */
1129                 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1130                         priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1131                 else
1132                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1133
1134                 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
1135                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1136
1137                 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1138                 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1139                 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1140         }
1141 }
1142
1143 /*
1144  * initialize rxon structure with default values from eeprom
1145  */
1146 static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
1147 {
1148         const struct iwl_channel_info *ch_info;
1149
1150         memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1151
1152         switch (priv->iw_mode) {
1153         case IEEE80211_IF_TYPE_AP:
1154                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1155                 break;
1156
1157         case IEEE80211_IF_TYPE_STA:
1158                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1159                 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1160                 break;
1161
1162         case IEEE80211_IF_TYPE_IBSS:
1163                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1164                 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1165                 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1166                                                   RXON_FILTER_ACCEPT_GRP_MSK;
1167                 break;
1168
1169         case IEEE80211_IF_TYPE_MNTR:
1170                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
1171                 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
1172                     RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
1173                 break;
1174         default:
1175                 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
1176                 break;
1177         }
1178
1179 #if 0
1180         /* TODO:  Figure out when short_preamble would be set and cache from
1181          * that */
1182         if (!hw_to_local(priv->hw)->short_preamble)
1183                 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1184         else
1185                 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1186 #endif
1187
1188         ch_info = iwl_get_channel_info(priv, priv->band,
1189                                        le16_to_cpu(priv->staging_rxon.channel));
1190
1191         if (!ch_info)
1192                 ch_info = &priv->channel_info[0];
1193
1194         /*
1195          * in some case A channels are all non IBSS
1196          * in this case force B/G channel
1197          */
1198         if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
1199             !(is_channel_ibss(ch_info)))
1200                 ch_info = &priv->channel_info[0];
1201
1202         priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
1203         priv->band = ch_info->band;
1204
1205         iwl4965_set_flags_for_phymode(priv, priv->band);
1206
1207         priv->staging_rxon.ofdm_basic_rates =
1208             (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1209         priv->staging_rxon.cck_basic_rates =
1210             (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1211
1212         priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
1213                                         RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
1214         memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1215         memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1216         priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1217         priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
1218         iwl_set_rxon_chain(priv);
1219 }
1220
1221 static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
1222 {
1223         if (mode == IEEE80211_IF_TYPE_IBSS) {
1224                 const struct iwl_channel_info *ch_info;
1225
1226                 ch_info = iwl_get_channel_info(priv,
1227                         priv->band,
1228                         le16_to_cpu(priv->staging_rxon.channel));
1229
1230                 if (!ch_info || !is_channel_ibss(ch_info)) {
1231                         IWL_ERROR("channel %d not IBSS channel\n",
1232                                   le16_to_cpu(priv->staging_rxon.channel));
1233                         return -EINVAL;
1234                 }
1235         }
1236
1237         priv->iw_mode = mode;
1238
1239         iwl4965_connection_init_rx_config(priv);
1240         memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1241
1242         iwlcore_clear_stations_table(priv);
1243
1244         /* dont commit rxon if rf-kill is on*/
1245         if (!iwl_is_ready_rf(priv))
1246                 return -EAGAIN;
1247
1248         cancel_delayed_work(&priv->scan_check);
1249         if (iwl4965_scan_cancel_timeout(priv, 100)) {
1250                 IWL_WARNING("Aborted scan still in progress after 100ms\n");
1251                 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
1252                 return -EAGAIN;
1253         }
1254
1255         iwl4965_commit_rxon(priv);
1256
1257         return 0;
1258 }
1259
1260 static void iwl4965_set_rate(struct iwl_priv *priv)
1261 {
1262         const struct ieee80211_supported_band *hw = NULL;
1263         struct ieee80211_rate *rate;
1264         int i;
1265
1266         hw = iwl_get_hw_mode(priv, priv->band);
1267         if (!hw) {
1268                 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
1269                 return;
1270         }
1271
1272         priv->active_rate = 0;
1273         priv->active_rate_basic = 0;
1274
1275         for (i = 0; i < hw->n_bitrates; i++) {
1276                 rate = &(hw->bitrates[i]);
1277                 if (rate->hw_value < IWL_RATE_COUNT)
1278                         priv->active_rate |= (1 << rate->hw_value);
1279         }
1280
1281         IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
1282                        priv->active_rate, priv->active_rate_basic);
1283
1284         /*
1285          * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
1286          * otherwise set it to the default of all CCK rates and 6, 12, 24 for
1287          * OFDM
1288          */
1289         if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
1290                 priv->staging_rxon.cck_basic_rates =
1291                     ((priv->active_rate_basic &
1292                       IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
1293         else
1294                 priv->staging_rxon.cck_basic_rates =
1295                     (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1296
1297         if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
1298                 priv->staging_rxon.ofdm_basic_rates =
1299                     ((priv->active_rate_basic &
1300                       (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
1301                       IWL_FIRST_OFDM_RATE) & 0xFF;
1302         else
1303                 priv->staging_rxon.ofdm_basic_rates =
1304                    (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1305 }
1306
1307 void iwl4965_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
1308 {
1309         unsigned long flags;
1310
1311         if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
1312                 return;
1313
1314         IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
1315                           disable_radio ? "OFF" : "ON");
1316
1317         if (disable_radio) {
1318                 iwl4965_scan_cancel(priv);
1319                 /* FIXME: This is a workaround for AP */
1320                 if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
1321                         spin_lock_irqsave(&priv->lock, flags);
1322                         iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1323                                     CSR_UCODE_SW_BIT_RFKILL);
1324                         spin_unlock_irqrestore(&priv->lock, flags);
1325                         /* call the host command only if no hw rf-kill set */
1326                         if (!test_bit(STATUS_RF_KILL_HW, &priv->status) &&
1327                             iwl_is_ready(priv))
1328                                 iwl4965_send_card_state(priv,
1329                                                         CARD_STATE_CMD_DISABLE,
1330                                                         0);
1331                         set_bit(STATUS_RF_KILL_SW, &priv->status);
1332
1333                         /* make sure mac80211 stop sending Tx frame */
1334                         if (priv->mac80211_registered)
1335                                 ieee80211_stop_queues(priv->hw);
1336                 }
1337                 return;
1338         }
1339
1340         spin_lock_irqsave(&priv->lock, flags);
1341         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
1342
1343         clear_bit(STATUS_RF_KILL_SW, &priv->status);
1344         spin_unlock_irqrestore(&priv->lock, flags);
1345
1346         /* wake up ucode */
1347         msleep(10);
1348
1349         spin_lock_irqsave(&priv->lock, flags);
1350         iwl_read32(priv, CSR_UCODE_DRV_GP1);
1351         if (!iwl_grab_nic_access(priv))
1352                 iwl_release_nic_access(priv);
1353         spin_unlock_irqrestore(&priv->lock, flags);
1354
1355         if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
1356                 IWL_DEBUG_RF_KILL("Can not turn radio back on - "
1357                                   "disabled by HW switch\n");
1358                 return;
1359         }
1360
1361         queue_work(priv->workqueue, &priv->restart);
1362         return;
1363 }
1364
1365 #define IWL_PACKET_RETRY_TIME HZ
1366
1367 int iwl4965_is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
1368 {
1369         u16 sc = le16_to_cpu(header->seq_ctrl);
1370         u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
1371         u16 frag = sc & IEEE80211_SCTL_FRAG;
1372         u16 *last_seq, *last_frag;
1373         unsigned long *last_time;
1374
1375         switch (priv->iw_mode) {
1376         case IEEE80211_IF_TYPE_IBSS:{
1377                 struct list_head *p;
1378                 struct iwl4965_ibss_seq *entry = NULL;
1379                 u8 *mac = header->addr2;
1380                 int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
1381
1382                 __list_for_each(p, &priv->ibss_mac_hash[index]) {
1383                         entry = list_entry(p, struct iwl4965_ibss_seq, list);
1384                         if (!compare_ether_addr(entry->mac, mac))
1385                                 break;
1386                 }
1387                 if (p == &priv->ibss_mac_hash[index]) {
1388                         entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
1389                         if (!entry) {
1390                                 IWL_ERROR("Cannot malloc new mac entry\n");
1391                                 return 0;
1392                         }
1393                         memcpy(entry->mac, mac, ETH_ALEN);
1394                         entry->seq_num = seq;
1395                         entry->frag_num = frag;
1396                         entry->packet_time = jiffies;
1397                         list_add(&entry->list, &priv->ibss_mac_hash[index]);
1398                         return 0;
1399                 }
1400                 last_seq = &entry->seq_num;
1401                 last_frag = &entry->frag_num;
1402                 last_time = &entry->packet_time;
1403                 break;
1404         }
1405         case IEEE80211_IF_TYPE_STA:
1406                 last_seq = &priv->last_seq_num;
1407                 last_frag = &priv->last_frag_num;
1408                 last_time = &priv->last_packet_time;
1409                 break;
1410         default:
1411                 return 0;
1412         }
1413         if ((*last_seq == seq) &&
1414             time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
1415                 if (*last_frag == frag)
1416                         goto drop;
1417                 if (*last_frag + 1 != frag)
1418                         /* out-of-order fragment */
1419                         goto drop;
1420         } else
1421                 *last_seq = seq;
1422
1423         *last_frag = frag;
1424         *last_time = jiffies;
1425         return 0;
1426
1427  drop:
1428         return 1;
1429 }
1430
1431 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
1432
1433 #include "iwl-spectrum.h"
1434
1435 #define BEACON_TIME_MASK_LOW    0x00FFFFFF
1436 #define BEACON_TIME_MASK_HIGH   0xFF000000
1437 #define TIME_UNIT               1024
1438
1439 /*
1440  * extended beacon time format
1441  * time in usec will be changed into a 32-bit value in 8:24 format
1442  * the high 1 byte is the beacon counts
1443  * the lower 3 bytes is the time in usec within one beacon interval
1444  */
1445
1446 static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
1447 {
1448         u32 quot;
1449         u32 rem;
1450         u32 interval = beacon_interval * 1024;
1451
1452         if (!interval || !usec)
1453                 return 0;
1454
1455         quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1456         rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1457
1458         return (quot << 24) + rem;
1459 }
1460
1461 /* base is usually what we get from ucode with each received frame,
1462  * the same as HW timer counter counting down
1463  */
1464
1465 static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
1466 {
1467         u32 base_low = base & BEACON_TIME_MASK_LOW;
1468         u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1469         u32 interval = beacon_interval * TIME_UNIT;
1470         u32 res = (base & BEACON_TIME_MASK_HIGH) +
1471             (addon & BEACON_TIME_MASK_HIGH);
1472
1473         if (base_low > addon_low)
1474                 res += base_low - addon_low;
1475         else if (base_low < addon_low) {
1476                 res += interval + base_low - addon_low;
1477                 res += (1 << 24);
1478         } else
1479                 res += (1 << 24);
1480
1481         return cpu_to_le32(res);
1482 }
1483
1484 static int iwl4965_get_measurement(struct iwl_priv *priv,
1485                                struct ieee80211_measurement_params *params,
1486                                u8 type)
1487 {
1488         struct iwl4965_spectrum_cmd spectrum;
1489         struct iwl_rx_packet *res;
1490         struct iwl_host_cmd cmd = {
1491                 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1492                 .data = (void *)&spectrum,
1493                 .meta.flags = CMD_WANT_SKB,
1494         };
1495         u32 add_time = le64_to_cpu(params->start_time);
1496         int rc;
1497         int spectrum_resp_status;
1498         int duration = le16_to_cpu(params->duration);
1499
1500         if (iwl_is_associated(priv))
1501                 add_time =
1502                     iwl4965_usecs_to_beacons(
1503                         le64_to_cpu(params->start_time) - priv->last_tsf,
1504                         le16_to_cpu(priv->rxon_timing.beacon_interval));
1505
1506         memset(&spectrum, 0, sizeof(spectrum));
1507
1508         spectrum.channel_count = cpu_to_le16(1);
1509         spectrum.flags =
1510             RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1511         spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1512         cmd.len = sizeof(spectrum);
1513         spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1514
1515         if (iwl_is_associated(priv))
1516                 spectrum.start_time =
1517                     iwl4965_add_beacon_time(priv->last_beacon_time,
1518                                 add_time,
1519                                 le16_to_cpu(priv->rxon_timing.beacon_interval));
1520         else
1521                 spectrum.start_time = 0;
1522
1523         spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1524         spectrum.channels[0].channel = params->channel;
1525         spectrum.channels[0].type = type;
1526         if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
1527                 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1528                     RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1529
1530         rc = iwl_send_cmd_sync(priv, &cmd);
1531         if (rc)
1532                 return rc;
1533
1534         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1535         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1536                 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
1537                 rc = -EIO;
1538         }
1539
1540         spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1541         switch (spectrum_resp_status) {
1542         case 0:         /* Command will be handled */
1543                 if (res->u.spectrum.id != 0xff) {
1544                         IWL_DEBUG_INFO
1545                             ("Replaced existing measurement: %d\n",
1546                              res->u.spectrum.id);
1547                         priv->measurement_status &= ~MEASUREMENT_READY;
1548                 }
1549                 priv->measurement_status |= MEASUREMENT_ACTIVE;
1550                 rc = 0;
1551                 break;
1552
1553         case 1:         /* Command will not be handled */
1554                 rc = -EAGAIN;
1555                 break;
1556         }
1557
1558         dev_kfree_skb_any(cmd.meta.u.skb);
1559
1560         return rc;
1561 }
1562 #endif
1563
1564 static void iwl4965_txstatus_to_ieee(struct iwl_priv *priv,
1565                                      struct iwl_tx_info *tx_sta)
1566 {
1567         ieee80211_tx_status_irqsafe(priv->hw, tx_sta->skb[0]);
1568         tx_sta->skb[0] = NULL;
1569 }
1570
1571 /**
1572  * iwl4965_tx_queue_reclaim - Reclaim Tx queue entries already Tx'd
1573  *
1574  * When FW advances 'R' index, all entries between old and new 'R' index
1575  * need to be reclaimed. As result, some free space forms.  If there is
1576  * enough free space (> low mark), wake the stack that feeds us.
1577  */
1578 int iwl4965_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
1579 {
1580         struct iwl_tx_queue *txq = &priv->txq[txq_id];
1581         struct iwl_queue *q = &txq->q;
1582         int nfreed = 0;
1583
1584         if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
1585                 IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
1586                           "is out of range [0-%d] %d %d.\n", txq_id,
1587                           index, q->n_bd, q->write_ptr, q->read_ptr);
1588                 return 0;
1589         }
1590
1591         for (index = iwl_queue_inc_wrap(index, q->n_bd);
1592                 q->read_ptr != index;
1593                 q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
1594                 if (txq_id != IWL_CMD_QUEUE_NUM) {
1595                         iwl4965_txstatus_to_ieee(priv,
1596                                         &(txq->txb[txq->q.read_ptr]));
1597                         iwl_hw_txq_free_tfd(priv, txq);
1598                 } else if (nfreed > 1) {
1599                         IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
1600                                         q->write_ptr, q->read_ptr);
1601                         queue_work(priv->workqueue, &priv->restart);
1602                 }
1603                 nfreed++;
1604         }
1605
1606         return nfreed;
1607 }
1608
1609 static int iwl4965_is_tx_success(u32 status)
1610 {
1611         status &= TX_STATUS_MSK;
1612         return (status == TX_STATUS_SUCCESS)
1613             || (status == TX_STATUS_DIRECT_DONE);
1614 }
1615
1616 /******************************************************************************
1617  *
1618  * Generic RX handler implementations
1619  *
1620  ******************************************************************************/
1621 #ifdef CONFIG_IWL4965_HT
1622
1623 static inline int iwl4965_get_ra_sta_id(struct iwl_priv *priv,
1624                                     struct ieee80211_hdr *hdr)
1625 {
1626         if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
1627                 return IWL_AP_ID;
1628         else {
1629                 u8 *da = ieee80211_get_DA(hdr);
1630                 return iwl_find_station(priv, da);
1631         }
1632 }
1633
1634 static struct ieee80211_hdr *iwl4965_tx_queue_get_hdr(
1635         struct iwl_priv *priv, int txq_id, int idx)
1636 {
1637         if (priv->txq[txq_id].txb[idx].skb[0])
1638                 return (struct ieee80211_hdr *)priv->txq[txq_id].
1639                                 txb[idx].skb[0]->data;
1640         return NULL;
1641 }
1642
1643 static inline u32 iwl4965_get_scd_ssn(struct iwl4965_tx_resp *tx_resp)
1644 {
1645         __le32 *scd_ssn = (__le32 *)((u32 *)&tx_resp->status +
1646                                 tx_resp->frame_count);
1647         return le32_to_cpu(*scd_ssn) & MAX_SN;
1648
1649 }
1650
1651 /**
1652  * iwl4965_tx_status_reply_tx - Handle Tx rspnse for frames in aggregation queue
1653  */
1654 static int iwl4965_tx_status_reply_tx(struct iwl_priv *priv,
1655                                       struct iwl_ht_agg *agg,
1656                                       struct iwl4965_tx_resp_agg *tx_resp,
1657                                       u16 start_idx)
1658 {
1659         u16 status;
1660         struct agg_tx_status *frame_status = &tx_resp->status;
1661         struct ieee80211_tx_info *info = NULL;
1662         struct ieee80211_hdr *hdr = NULL;
1663         int i, sh;
1664         int txq_id, idx;
1665         u16 seq;
1666
1667         if (agg->wait_for_ba)
1668                 IWL_DEBUG_TX_REPLY("got tx response w/o block-ack\n");
1669
1670         agg->frame_count = tx_resp->frame_count;
1671         agg->start_idx = start_idx;
1672         agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
1673         agg->bitmap = 0;
1674
1675         /* # frames attempted by Tx command */
1676         if (agg->frame_count == 1) {
1677                 /* Only one frame was attempted; no block-ack will arrive */
1678                 status = le16_to_cpu(frame_status[0].status);
1679                 seq  = le16_to_cpu(frame_status[0].sequence);
1680                 idx = SEQ_TO_INDEX(seq);
1681                 txq_id = SEQ_TO_QUEUE(seq);
1682
1683                 /* FIXME: code repetition */
1684                 IWL_DEBUG_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
1685                                    agg->frame_count, agg->start_idx, idx);
1686
1687                 info = IEEE80211_SKB_CB(priv->txq[txq_id].txb[idx].skb[0]);
1688                 info->status.retry_count = tx_resp->failure_frame;
1689                 info->flags &= ~IEEE80211_TX_CTL_AMPDU;
1690                 info->flags |= iwl4965_is_tx_success(status)?
1691                         IEEE80211_TX_STAT_ACK : 0;
1692                 iwl4965_hwrate_to_tx_control(priv,
1693                                              le32_to_cpu(tx_resp->rate_n_flags),
1694                                              info);
1695                 /* FIXME: code repetition end */
1696
1697                 IWL_DEBUG_TX_REPLY("1 Frame 0x%x failure :%d\n",
1698                                     status & 0xff, tx_resp->failure_frame);
1699                 IWL_DEBUG_TX_REPLY("Rate Info rate_n_flags=%x\n",
1700                                 iwl4965_hw_get_rate_n_flags(tx_resp->rate_n_flags));
1701
1702                 agg->wait_for_ba = 0;
1703         } else {
1704                 /* Two or more frames were attempted; expect block-ack */
1705                 u64 bitmap = 0;
1706                 int start = agg->start_idx;
1707
1708                 /* Construct bit-map of pending frames within Tx window */
1709                 for (i = 0; i < agg->frame_count; i++) {
1710                         u16 sc;
1711                         status = le16_to_cpu(frame_status[i].status);
1712                         seq  = le16_to_cpu(frame_status[i].sequence);
1713                         idx = SEQ_TO_INDEX(seq);
1714                         txq_id = SEQ_TO_QUEUE(seq);
1715
1716                         if (status & (AGG_TX_STATE_FEW_BYTES_MSK |
1717                                       AGG_TX_STATE_ABORT_MSK))
1718                                 continue;
1719
1720                         IWL_DEBUG_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
1721                                            agg->frame_count, txq_id, idx);
1722
1723                         hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, idx);
1724
1725                         sc = le16_to_cpu(hdr->seq_ctrl);
1726                         if (idx != (SEQ_TO_SN(sc) & 0xff)) {
1727                                 IWL_ERROR("BUG_ON idx doesn't match seq control"
1728                                           " idx=%d, seq_idx=%d, seq=%d\n",
1729                                           idx, SEQ_TO_SN(sc),
1730                                           hdr->seq_ctrl);
1731                                 return -1;
1732                         }
1733
1734                         IWL_DEBUG_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n",
1735                                            i, idx, SEQ_TO_SN(sc));
1736
1737                         sh = idx - start;
1738                         if (sh > 64) {
1739                                 sh = (start - idx) + 0xff;
1740                                 bitmap = bitmap << sh;
1741                                 sh = 0;
1742                                 start = idx;
1743                         } else if (sh < -64)
1744                                 sh  = 0xff - (start - idx);
1745                         else if (sh < 0) {
1746                                 sh = start - idx;
1747                                 start = idx;
1748                                 bitmap = bitmap << sh;
1749                                 sh = 0;
1750                         }
1751                         bitmap |= (1 << sh);
1752                         IWL_DEBUG_TX_REPLY("start=%d bitmap=0x%x\n",
1753                                            start, (u32)(bitmap & 0xFFFFFFFF));
1754                 }
1755
1756                 agg->bitmap = bitmap;
1757                 agg->start_idx = start;
1758                 agg->rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
1759                 IWL_DEBUG_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
1760                                    agg->frame_count, agg->start_idx,
1761                                    (unsigned long long)agg->bitmap);
1762
1763                 if (bitmap)
1764                         agg->wait_for_ba = 1;
1765         }
1766         return 0;
1767 }
1768 #endif
1769
1770 /**
1771  * iwl4965_rx_reply_tx - Handle standard (non-aggregation) Tx response
1772  */
1773 static void iwl4965_rx_reply_tx(struct iwl_priv *priv,
1774                                 struct iwl_rx_mem_buffer *rxb)
1775 {
1776         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1777         u16 sequence = le16_to_cpu(pkt->hdr.sequence);
1778         int txq_id = SEQ_TO_QUEUE(sequence);
1779         int index = SEQ_TO_INDEX(sequence);
1780         struct iwl_tx_queue *txq = &priv->txq[txq_id];
1781         struct ieee80211_tx_info *info;
1782         struct iwl4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
1783         u32  status = le32_to_cpu(tx_resp->status);
1784 #ifdef CONFIG_IWL4965_HT
1785         int tid = MAX_TID_COUNT, sta_id = IWL_INVALID_STATION;
1786         u16 fc;
1787         struct ieee80211_hdr *hdr;
1788         u8 *qc = NULL;
1789 #endif
1790
1791         if ((index >= txq->q.n_bd) || (iwl_queue_used(&txq->q, index) == 0)) {
1792                 IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
1793                           "is out of range [0-%d] %d %d\n", txq_id,
1794                           index, txq->q.n_bd, txq->q.write_ptr,
1795                           txq->q.read_ptr);
1796                 return;
1797         }
1798
1799         info = IEEE80211_SKB_CB(txq->txb[txq->q.read_ptr].skb[0]);
1800         memset(&info->status, 0, sizeof(info->status));
1801
1802 #ifdef CONFIG_IWL4965_HT
1803         hdr = iwl4965_tx_queue_get_hdr(priv, txq_id, index);
1804         fc = le16_to_cpu(hdr->frame_control);
1805         if (ieee80211_is_qos_data(fc)) {
1806                 qc = ieee80211_get_qos_ctrl(hdr, ieee80211_get_hdrlen(fc));
1807                 tid = qc[0] & 0xf;
1808         }
1809
1810         sta_id = iwl4965_get_ra_sta_id(priv, hdr);
1811         if (txq->sched_retry && unlikely(sta_id == IWL_INVALID_STATION)) {
1812                 IWL_ERROR("Station not known\n");
1813                 return;
1814         }
1815
1816         if (txq->sched_retry) {
1817                 const u32 scd_ssn = iwl4965_get_scd_ssn(tx_resp);
1818                 struct iwl_ht_agg *agg = NULL;
1819
1820                 if (!qc)
1821                         return;
1822
1823                 agg = &priv->stations[sta_id].tid[tid].agg;
1824
1825                 iwl4965_tx_status_reply_tx(priv, agg,
1826                                 (struct iwl4965_tx_resp_agg *)tx_resp, index);
1827
1828                 if ((tx_resp->frame_count == 1) &&
1829                     !iwl4965_is_tx_success(status)) {
1830                         /* TODO: send BAR */
1831                 }
1832
1833                 if (txq->q.read_ptr != (scd_ssn & 0xff)) {
1834                         int freed, ampdu_q;
1835                         index = iwl_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
1836                         IWL_DEBUG_TX_REPLY("Retry scheduler reclaim scd_ssn "
1837                                            "%d index %d\n", scd_ssn , index);
1838                         freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
1839                         priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1840
1841                         if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
1842                             txq_id >= 0 && priv->mac80211_registered &&
1843                             agg->state != IWL_EMPTYING_HW_QUEUE_DELBA) {
1844                                 /* calculate mac80211 ampdu sw queue to wake */
1845                                 ampdu_q = txq_id - IWL_BACK_QUEUE_FIRST_ID +
1846                                           priv->hw->queues;
1847                                 if (agg->state == IWL_AGG_OFF)
1848                                         ieee80211_wake_queue(priv->hw, txq_id);
1849                                 else
1850                                         ieee80211_wake_queue(priv->hw, ampdu_q);
1851                         }
1852                         iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
1853                 }
1854         } else {
1855 #endif /* CONFIG_IWL4965_HT */
1856
1857         info->status.retry_count = tx_resp->failure_frame;
1858         info->flags |=
1859             iwl4965_is_tx_success(status) ? IEEE80211_TX_STAT_ACK : 0;
1860         iwl4965_hwrate_to_tx_control(priv, le32_to_cpu(tx_resp->rate_n_flags),
1861                                      info);
1862
1863         IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) rate_n_flags 0x%x "
1864                      "retries %d\n", txq_id, iwl4965_get_tx_fail_reason(status),
1865                      status, le32_to_cpu(tx_resp->rate_n_flags),
1866                      tx_resp->failure_frame);
1867
1868         IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
1869 #ifdef CONFIG_IWL4965_HT
1870         if (index != -1) {
1871                 int freed = iwl4965_tx_queue_reclaim(priv, txq_id, index);
1872                 if (tid != MAX_TID_COUNT)
1873                         priv->stations[sta_id].tid[tid].tfds_in_queue -= freed;
1874                 if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
1875                         (txq_id >= 0) && priv->mac80211_registered)
1876                         ieee80211_wake_queue(priv->hw, txq_id);
1877                 if (tid != MAX_TID_COUNT)
1878                         iwl4965_check_empty_hw_queue(priv, sta_id, tid, txq_id);
1879         }
1880         }
1881 #endif /* CONFIG_IWL4965_HT */
1882
1883         if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
1884                 IWL_ERROR("TODO:  Implement Tx ABORT REQUIRED!!!\n");
1885 }
1886
1887
1888 static void iwl_rx_reply_alive(struct iwl_priv *priv,
1889                                 struct iwl_rx_mem_buffer *rxb)
1890 {
1891         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1892         struct iwl_alive_resp *palive;
1893         struct delayed_work *pwork;
1894
1895         palive = &pkt->u.alive_frame;
1896
1897         IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
1898                        "0x%01X 0x%01X\n",
1899                        palive->is_valid, palive->ver_type,
1900                        palive->ver_subtype);
1901
1902         if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1903                 IWL_DEBUG_INFO("Initialization Alive received.\n");
1904                 memcpy(&priv->card_alive_init,
1905                        &pkt->u.alive_frame,
1906                        sizeof(struct iwl_init_alive_resp));
1907                 pwork = &priv->init_alive_start;
1908         } else {
1909                 IWL_DEBUG_INFO("Runtime Alive received.\n");
1910                 memcpy(&priv->card_alive, &pkt->u.alive_frame,
1911                        sizeof(struct iwl_alive_resp));
1912                 pwork = &priv->alive_start;
1913         }
1914
1915         /* We delay the ALIVE response by 5ms to
1916          * give the HW RF Kill time to activate... */
1917         if (palive->is_valid == UCODE_VALID_OK)
1918                 queue_delayed_work(priv->workqueue, pwork,
1919                                    msecs_to_jiffies(5));
1920         else
1921                 IWL_WARNING("uCode did not respond OK.\n");
1922 }
1923
1924 static void iwl4965_rx_reply_error(struct iwl_priv *priv,
1925                                    struct iwl_rx_mem_buffer *rxb)
1926 {
1927         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1928
1929         IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
1930                 "seq 0x%04X ser 0x%08X\n",
1931                 le32_to_cpu(pkt->u.err_resp.error_type),
1932                 get_cmd_string(pkt->u.err_resp.cmd_id),
1933                 pkt->u.err_resp.cmd_id,
1934                 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1935                 le32_to_cpu(pkt->u.err_resp.error_info));
1936 }
1937
1938 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1939
1940 static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
1941 {
1942         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1943         struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
1944         struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
1945         IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
1946                       le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
1947         rxon->channel = csa->channel;
1948         priv->staging_rxon.channel = csa->channel;
1949 }
1950
1951 static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
1952                                           struct iwl_rx_mem_buffer *rxb)
1953 {
1954 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
1955         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1956         struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
1957
1958         if (!report->state) {
1959                 IWL_DEBUG(IWL_DL_11H,
1960                         "Spectrum Measure Notification: Start\n");
1961                 return;
1962         }
1963
1964         memcpy(&priv->measure_report, report, sizeof(*report));
1965         priv->measurement_status |= MEASUREMENT_READY;
1966 #endif
1967 }
1968
1969 static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
1970                                       struct iwl_rx_mem_buffer *rxb)
1971 {
1972 #ifdef CONFIG_IWLWIFI_DEBUG
1973         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1974         struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
1975         IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
1976                      sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1977 #endif
1978 }
1979
1980 static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1981                                              struct iwl_rx_mem_buffer *rxb)
1982 {
1983         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1984         IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
1985                         "notification for %s:\n",
1986                         le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
1987         iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
1988 }
1989
1990 static void iwl4965_bg_beacon_update(struct work_struct *work)
1991 {
1992         struct iwl_priv *priv =
1993                 container_of(work, struct iwl_priv, beacon_update);
1994         struct sk_buff *beacon;
1995
1996         /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
1997         beacon = ieee80211_beacon_get(priv->hw, priv->vif);
1998
1999         if (!beacon) {
2000                 IWL_ERROR("update beacon failed\n");
2001                 return;
2002         }
2003
2004         mutex_lock(&priv->mutex);
2005         /* new beacon skb is allocated every time; dispose previous.*/
2006         if (priv->ibss_beacon)
2007                 dev_kfree_skb(priv->ibss_beacon);
2008
2009         priv->ibss_beacon = beacon;
2010         mutex_unlock(&priv->mutex);
2011
2012         iwl4965_send_beacon_cmd(priv);
2013 }
2014
2015 static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
2016                                 struct iwl_rx_mem_buffer *rxb)
2017 {
2018 #ifdef CONFIG_IWLWIFI_DEBUG
2019         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2020         struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
2021         u8 rate = iwl4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
2022
2023         IWL_DEBUG_RX("beacon status %x retries %d iss %d "
2024                 "tsf %d %d rate %d\n",
2025                 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
2026                 beacon->beacon_notify_hdr.failure_frame,
2027                 le32_to_cpu(beacon->ibss_mgr_status),
2028                 le32_to_cpu(beacon->high_tsf),
2029                 le32_to_cpu(beacon->low_tsf), rate);
2030 #endif
2031
2032         if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
2033             (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
2034                 queue_work(priv->workqueue, &priv->beacon_update);
2035 }
2036
2037 /* Service response to REPLY_SCAN_CMD (0x80) */
2038 static void iwl4965_rx_reply_scan(struct iwl_priv *priv,
2039                               struct iwl_rx_mem_buffer *rxb)
2040 {
2041 #ifdef CONFIG_IWLWIFI_DEBUG
2042         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2043         struct iwl4965_scanreq_notification *notif =
2044             (struct iwl4965_scanreq_notification *)pkt->u.raw;
2045
2046         IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
2047 #endif
2048 }
2049
2050 /* Service SCAN_START_NOTIFICATION (0x82) */
2051 static void iwl4965_rx_scan_start_notif(struct iwl_priv *priv,
2052                                     struct iwl_rx_mem_buffer *rxb)
2053 {
2054         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2055         struct iwl4965_scanstart_notification *notif =
2056             (struct iwl4965_scanstart_notification *)pkt->u.raw;
2057         priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
2058         IWL_DEBUG_SCAN("Scan start: "
2059                        "%d [802.11%s] "
2060                        "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
2061                        notif->channel,
2062                        notif->band ? "bg" : "a",
2063                        notif->tsf_high,
2064                        notif->tsf_low, notif->status, notif->beacon_timer);
2065 }
2066
2067 /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
2068 static void iwl4965_rx_scan_results_notif(struct iwl_priv *priv,
2069                                       struct iwl_rx_mem_buffer *rxb)
2070 {
2071         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2072         struct iwl4965_scanresults_notification *notif =
2073             (struct iwl4965_scanresults_notification *)pkt->u.raw;
2074
2075         IWL_DEBUG_SCAN("Scan ch.res: "
2076                        "%d [802.11%s] "
2077                        "(TSF: 0x%08X:%08X) - %d "
2078                        "elapsed=%lu usec (%dms since last)\n",
2079                        notif->channel,
2080                        notif->band ? "bg" : "a",
2081                        le32_to_cpu(notif->tsf_high),
2082                        le32_to_cpu(notif->tsf_low),
2083                        le32_to_cpu(notif->statistics[0]),
2084                        le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
2085                        jiffies_to_msecs(elapsed_jiffies
2086                                         (priv->last_scan_jiffies, jiffies)));
2087
2088         priv->last_scan_jiffies = jiffies;
2089         priv->next_scan_jiffies = 0;
2090 }
2091
2092 /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
2093 static void iwl4965_rx_scan_complete_notif(struct iwl_priv *priv,
2094                                        struct iwl_rx_mem_buffer *rxb)
2095 {
2096         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2097         struct iwl4965_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
2098
2099         IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
2100                        scan_notif->scanned_channels,
2101                        scan_notif->tsf_low,
2102                        scan_notif->tsf_high, scan_notif->status);
2103
2104         /* The HW is no longer scanning */
2105         clear_bit(STATUS_SCAN_HW, &priv->status);
2106
2107         /* The scan completion notification came in, so kill that timer... */
2108         cancel_delayed_work(&priv->scan_check);
2109
2110         IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
2111                        (priv->scan_bands == 2) ? "2.4" : "5.2",
2112                        jiffies_to_msecs(elapsed_jiffies
2113                                         (priv->scan_pass_start, jiffies)));
2114
2115         /* Remove this scanned band from the list
2116          * of pending bands to scan */
2117         priv->scan_bands--;
2118
2119         /* If a request to abort was given, or the scan did not succeed
2120          * then we reset the scan state machine and terminate,
2121          * re-queuing another scan if one has been requested */
2122         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
2123                 IWL_DEBUG_INFO("Aborted scan completed.\n");
2124                 clear_bit(STATUS_SCAN_ABORTING, &priv->status);
2125         } else {
2126                 /* If there are more bands on this scan pass reschedule */
2127                 if (priv->scan_bands > 0)
2128                         goto reschedule;
2129         }
2130
2131         priv->last_scan_jiffies = jiffies;
2132         priv->next_scan_jiffies = 0;
2133         IWL_DEBUG_INFO("Setting scan to off\n");
2134
2135         clear_bit(STATUS_SCANNING, &priv->status);
2136
2137         IWL_DEBUG_INFO("Scan took %dms\n",
2138                 jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
2139
2140         queue_work(priv->workqueue, &priv->scan_completed);
2141
2142         return;
2143
2144 reschedule:
2145         priv->scan_pass_start = jiffies;
2146         queue_work(priv->workqueue, &priv->request_scan);
2147 }
2148
2149 /* Handle notification from uCode that card's power state is changing
2150  * due to software, hardware, or critical temperature RFKILL */
2151 static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
2152                                     struct iwl_rx_mem_buffer *rxb)
2153 {
2154         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2155         u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
2156         unsigned long status = priv->status;
2157
2158         IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
2159                           (flags & HW_CARD_DISABLED) ? "Kill" : "On",
2160                           (flags & SW_CARD_DISABLED) ? "Kill" : "On");
2161
2162         if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
2163                      RF_CARD_DISABLED)) {
2164
2165                 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
2166                             CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2167
2168                 if (!iwl_grab_nic_access(priv)) {
2169                         iwl_write_direct32(
2170                                 priv, HBUS_TARG_MBX_C,
2171                                 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2172
2173                         iwl_release_nic_access(priv);
2174                 }
2175
2176                 if (!(flags & RXON_CARD_DISABLED)) {
2177                         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
2178                                     CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2179                         if (!iwl_grab_nic_access(priv)) {
2180                                 iwl_write_direct32(
2181                                         priv, HBUS_TARG_MBX_C,
2182                                         HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
2183
2184                                 iwl_release_nic_access(priv);
2185                         }
2186                 }
2187
2188                 if (flags & RF_CARD_DISABLED) {
2189                         iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
2190                                     CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
2191                         iwl_read32(priv, CSR_UCODE_DRV_GP1);
2192                         if (!iwl_grab_nic_access(priv))
2193                                 iwl_release_nic_access(priv);
2194                 }
2195         }
2196
2197         if (flags & HW_CARD_DISABLED)
2198                 set_bit(STATUS_RF_KILL_HW, &priv->status);
2199         else
2200                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2201
2202
2203         if (flags & SW_CARD_DISABLED)
2204                 set_bit(STATUS_RF_KILL_SW, &priv->status);
2205         else
2206                 clear_bit(STATUS_RF_KILL_SW, &priv->status);
2207
2208         if (!(flags & RXON_CARD_DISABLED))
2209                 iwl4965_scan_cancel(priv);
2210
2211         if ((test_bit(STATUS_RF_KILL_HW, &status) !=
2212              test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
2213             (test_bit(STATUS_RF_KILL_SW, &status) !=
2214              test_bit(STATUS_RF_KILL_SW, &priv->status)))
2215                 queue_work(priv->workqueue, &priv->rf_kill);
2216         else
2217                 wake_up_interruptible(&priv->wait_command_queue);
2218 }
2219
2220 /**
2221  * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
2222  *
2223  * Setup the RX handlers for each of the reply types sent from the uCode
2224  * to the host.
2225  *
2226  * This function chains into the hardware specific files for them to setup
2227  * any hardware specific handlers as well.
2228  */
2229 static void iwl4965_setup_rx_handlers(struct iwl_priv *priv)
2230 {
2231         priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
2232         priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
2233         priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
2234         priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
2235             iwl4965_rx_spectrum_measure_notif;
2236         priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
2237         priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
2238             iwl4965_rx_pm_debug_statistics_notif;
2239         priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
2240
2241         /*
2242          * The same handler is used for both the REPLY to a discrete
2243          * statistics request from the host as well as for the periodic
2244          * statistics notifications (after received beacons) from the uCode.
2245          */
2246         priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl4965_hw_rx_statistics;
2247         priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl4965_hw_rx_statistics;
2248
2249         priv->rx_handlers[REPLY_SCAN_CMD] = iwl4965_rx_reply_scan;
2250         priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl4965_rx_scan_start_notif;
2251         priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
2252             iwl4965_rx_scan_results_notif;
2253         priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
2254             iwl4965_rx_scan_complete_notif;
2255         priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
2256         priv->rx_handlers[REPLY_TX] = iwl4965_rx_reply_tx;
2257
2258         /* Set up hardware specific Rx handlers */
2259         priv->cfg->ops->lib->rx_handler_setup(priv);
2260 }
2261
2262 /**
2263  * iwl4965_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
2264  * @rxb: Rx buffer to reclaim
2265  *
2266  * If an Rx buffer has an async callback associated with it the callback
2267  * will be executed.  The attached skb (if present) will only be freed
2268  * if the callback returns 1
2269  */
2270 static void iwl4965_tx_cmd_complete(struct iwl_priv *priv,
2271                                     struct iwl_rx_mem_buffer *rxb)
2272 {
2273         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
2274         u16 sequence = le16_to_cpu(pkt->hdr.sequence);
2275         int txq_id = SEQ_TO_QUEUE(sequence);
2276         int index = SEQ_TO_INDEX(sequence);
2277         int huge = sequence & SEQ_HUGE_FRAME;
2278         int cmd_index;
2279         struct iwl_cmd *cmd;
2280
2281         /* If a Tx command is being handled and it isn't in the actual
2282          * command queue then there a command routing bug has been introduced
2283          * in the queue management code. */
2284         if (txq_id != IWL_CMD_QUEUE_NUM)
2285                 IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
2286                           txq_id, pkt->hdr.cmd);
2287         BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
2288
2289         cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
2290         cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
2291
2292         /* Input error checking is done when commands are added to queue. */
2293         if (cmd->meta.flags & CMD_WANT_SKB) {
2294                 cmd->meta.source->u.skb = rxb->skb;
2295                 rxb->skb = NULL;
2296         } else if (cmd->meta.u.callback &&
2297                    !cmd->meta.u.callback(priv, cmd, rxb->skb))
2298                 rxb->skb = NULL;
2299
2300         iwl4965_tx_queue_reclaim(priv, txq_id, index);
2301
2302         if (!(cmd->meta.flags & CMD_ASYNC)) {
2303                 clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2304                 wake_up_interruptible(&priv->wait_command_queue);
2305         }
2306 }
2307
2308 /*
2309  * this should be called while priv->lock is locked
2310 */
2311 static void __iwl_rx_replenish(struct iwl_priv *priv)
2312 {
2313         iwl_rx_allocate(priv);
2314         iwl_rx_queue_restock(priv);
2315 }
2316
2317
2318 /**
2319  * iwl_rx_handle - Main entry function for receiving responses from uCode
2320  *
2321  * Uses the priv->rx_handlers callback function array to invoke
2322  * the appropriate handlers, including command responses,
2323  * frame-received notifications, and other notifications.
2324  */
2325 void iwl_rx_handle(struct iwl_priv *priv)
2326 {
2327         struct iwl_rx_mem_buffer *rxb;
2328         struct iwl_rx_packet *pkt;
2329         struct iwl_rx_queue *rxq = &priv->rxq;
2330         u32 r, i;
2331         int reclaim;
2332         unsigned long flags;
2333         u8 fill_rx = 0;
2334         u32 count = 8;
2335
2336         /* uCode's read index (stored in shared DRAM) indicates the last Rx
2337          * buffer that the driver may process (last buffer filled by ucode). */
2338         r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
2339         i = rxq->read;
2340
2341         /* Rx interrupt, but nothing sent from uCode */
2342         if (i == r)
2343                 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
2344
2345         if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
2346                 fill_rx = 1;
2347
2348         while (i != r) {
2349                 rxb = rxq->queue[i];
2350
2351                 /* If an RXB doesn't have a Rx queue slot associated with it,
2352                  * then a bug has been introduced in the queue refilling
2353                  * routines -- catch it here */
2354                 BUG_ON(rxb == NULL);
2355
2356                 rxq->queue[i] = NULL;
2357
2358                 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
2359                                             priv->hw_params.rx_buf_size,
2360                                             PCI_DMA_FROMDEVICE);
2361                 pkt = (struct iwl_rx_packet *)rxb->skb->data;
2362
2363                 /* Reclaim a command buffer only if this packet is a response
2364                  *   to a (driver-originated) command.
2365                  * If the packet (e.g. Rx frame) originated from uCode,
2366                  *   there is no command buffer to reclaim.
2367                  * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
2368                  *   but apparently a few don't get set; catch them here. */
2369                 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
2370                         (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
2371                         (pkt->hdr.cmd != REPLY_RX) &&
2372                         (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
2373                         (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
2374                         (pkt->hdr.cmd != REPLY_TX);
2375
2376                 /* Based on type of command response or notification,
2377                  *   handle those that need handling via function in
2378                  *   rx_handlers table.  See iwl4965_setup_rx_handlers() */
2379                 if (priv->rx_handlers[pkt->hdr.cmd]) {
2380                         IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
2381                                 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
2382                         priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
2383                 } else {
2384                         /* No handling needed */
2385                         IWL_DEBUG(IWL_DL_RX,
2386                                 "r %d i %d No handler needed for %s, 0x%02x\n",
2387                                 r, i, get_cmd_string(pkt->hdr.cmd),
2388                                 pkt->hdr.cmd);
2389                 }
2390
2391                 if (reclaim) {
2392                         /* Invoke any callbacks, transfer the skb to caller, and
2393                          * fire off the (possibly) blocking iwl_send_cmd()
2394                          * as we reclaim the driver command queue */
2395                         if (rxb && rxb->skb)
2396                                 iwl4965_tx_cmd_complete(priv, rxb);
2397                         else
2398                                 IWL_WARNING("Claim null rxb?\n");
2399                 }
2400
2401                 /* For now we just don't re-use anything.  We can tweak this
2402                  * later to try and re-use notification packets and SKBs that
2403                  * fail to Rx correctly */
2404                 if (rxb->skb != NULL) {
2405                         priv->alloc_rxb_skb--;
2406                         dev_kfree_skb_any(rxb->skb);
2407                         rxb->skb = NULL;
2408                 }
2409
2410                 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
2411                                  priv->hw_params.rx_buf_size,
2412                                  PCI_DMA_FROMDEVICE);
2413                 spin_lock_irqsave(&rxq->lock, flags);
2414                 list_add_tail(&rxb->list, &priv->rxq.rx_used);
2415                 spin_unlock_irqrestore(&rxq->lock, flags);
2416                 i = (i + 1) & RX_QUEUE_MASK;
2417                 /* If there are a lot of unused frames,
2418                  * restock the Rx queue so ucode wont assert. */
2419                 if (fill_rx) {
2420                         count++;
2421                         if (count >= 8) {
2422                                 priv->rxq.read = i;
2423                                 __iwl_rx_replenish(priv);
2424                                 count = 0;
2425                         }
2426                 }
2427         }
2428
2429         /* Backtrack one entry */
2430         priv->rxq.read = i;
2431         iwl_rx_queue_restock(priv);
2432 }
2433 /* Convert linear signal-to-noise ratio into dB */
2434 static u8 ratio2dB[100] = {
2435 /*       0   1   2   3   4   5   6   7   8   9 */
2436          0,  0,  6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
2437         20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
2438         26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
2439         29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
2440         32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
2441         34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
2442         36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
2443         37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
2444         38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
2445         39, 39, 39, 39, 39, 40, 40, 40, 40, 40  /* 90 - 99 */
2446 };
2447
2448 /* Calculates a relative dB value from a ratio of linear
2449  *   (i.e. not dB) signal levels.
2450  * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
2451 int iwl4965_calc_db_from_ratio(int sig_ratio)
2452 {
2453         /* 1000:1 or higher just report as 60 dB */
2454         if (sig_ratio >= 1000)
2455                 return 60;
2456
2457         /* 100:1 or higher, divide by 10 and use table,
2458          *   add 20 dB to make up for divide by 10 */
2459         if (sig_ratio >= 100)
2460                 return (20 + (int)ratio2dB[sig_ratio/10]);
2461
2462         /* We shouldn't see this */
2463         if (sig_ratio < 1)
2464                 return 0;
2465
2466         /* Use table for ratios 1:1 - 99:1 */
2467         return (int)ratio2dB[sig_ratio];
2468 }
2469
2470 #define PERFECT_RSSI (-20) /* dBm */
2471 #define WORST_RSSI (-95)   /* dBm */
2472 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
2473
2474 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
2475  * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
2476  *   about formulas used below. */
2477 int iwl4965_calc_sig_qual(int rssi_dbm, int noise_dbm)
2478 {
2479         int sig_qual;
2480         int degradation = PERFECT_RSSI - rssi_dbm;
2481
2482         /* If we get a noise measurement, use signal-to-noise ratio (SNR)
2483          * as indicator; formula is (signal dbm - noise dbm).
2484          * SNR at or above 40 is a great signal (100%).
2485          * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
2486          * Weakest usable signal is usually 10 - 15 dB SNR. */
2487         if (noise_dbm) {
2488                 if (rssi_dbm - noise_dbm >= 40)
2489                         return 100;
2490                 else if (rssi_dbm < noise_dbm)
2491                         return 0;
2492                 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
2493
2494         /* Else use just the signal level.
2495          * This formula is a least squares fit of data points collected and
2496          *   compared with a reference system that had a percentage (%) display
2497          *   for signal quality. */
2498         } else
2499                 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
2500                             (15 * RSSI_RANGE + 62 * degradation)) /
2501                            (RSSI_RANGE * RSSI_RANGE);
2502
2503         if (sig_qual > 100)
2504                 sig_qual = 100;
2505         else if (sig_qual < 1)
2506                 sig_qual = 0;
2507
2508         return sig_qual;
2509 }
2510
2511 #ifdef CONFIG_IWLWIFI_DEBUG
2512 static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
2513 {
2514         struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
2515         DECLARE_MAC_BUF(mac);
2516
2517         IWL_DEBUG_RADIO("RX CONFIG:\n");
2518         iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
2519         IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
2520         IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
2521         IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
2522                         le32_to_cpu(rxon->filter_flags));
2523         IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
2524         IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
2525                         rxon->ofdm_basic_rates);
2526         IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
2527         IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
2528                         print_mac(mac, rxon->node_addr));
2529         IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
2530                         print_mac(mac, rxon->bssid_addr));
2531         IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
2532 }
2533 #endif
2534
2535 static void iwl4965_enable_interrupts(struct iwl_priv *priv)
2536 {
2537         IWL_DEBUG_ISR("Enabling interrupts\n");
2538         set_bit(STATUS_INT_ENABLED, &priv->status);
2539         iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
2540 }
2541
2542 /* call this function to flush any scheduled tasklet */
2543 static inline void iwl_synchronize_irq(struct iwl_priv *priv)
2544 {
2545         /* wait to make sure we flush pedding tasklet*/
2546         synchronize_irq(priv->pci_dev->irq);
2547         tasklet_kill(&priv->irq_tasklet);
2548 }
2549
2550 static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
2551 {
2552         clear_bit(STATUS_INT_ENABLED, &priv->status);
2553
2554         /* disable interrupts from uCode/NIC to host */
2555         iwl_write32(priv, CSR_INT_MASK, 0x00000000);
2556
2557         /* acknowledge/clear/reset any interrupts still pending
2558          * from uCode or flow handler (Rx/Tx DMA) */
2559         iwl_write32(priv, CSR_INT, 0xffffffff);
2560         iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
2561         IWL_DEBUG_ISR("Disabled interrupts\n");
2562 }
2563
2564
2565 /**
2566  * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
2567  */
2568 static void iwl4965_irq_handle_error(struct iwl_priv *priv)
2569 {
2570         /* Set the FW error flag -- cleared on iwl4965_down */
2571         set_bit(STATUS_FW_ERROR, &priv->status);
2572
2573         /* Cancel currently queued command. */
2574         clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
2575
2576 #ifdef CONFIG_IWLWIFI_DEBUG
2577         if (priv->debug_level & IWL_DL_FW_ERRORS) {
2578                 iwl_dump_nic_error_log(priv);
2579                 iwl_dump_nic_event_log(priv);
2580                 iwl4965_print_rx_config_cmd(priv);
2581         }
2582 #endif
2583
2584         wake_up_interruptible(&priv->wait_command_queue);
2585
2586         /* Keep the restart process from trying to send host
2587          * commands by clearing the INIT status bit */
2588         clear_bit(STATUS_READY, &priv->status);
2589
2590         if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
2591                 IWL_DEBUG(IWL_DL_FW_ERRORS,
2592                           "Restarting adapter due to uCode error.\n");
2593
2594                 if (iwl_is_associated(priv)) {
2595                         memcpy(&priv->recovery_rxon, &priv->active_rxon,
2596                                sizeof(priv->recovery_rxon));
2597                         priv->error_recovering = 1;
2598                 }
2599                 if (priv->cfg->mod_params->restart_fw)
2600                         queue_work(priv->workqueue, &priv->restart);
2601         }
2602 }
2603
2604 static void iwl4965_error_recovery(struct iwl_priv *priv)
2605 {
2606         unsigned long flags;
2607
2608         memcpy(&priv->staging_rxon, &priv->recovery_rxon,
2609                sizeof(priv->staging_rxon));
2610         priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2611         iwl4965_commit_rxon(priv);
2612
2613         iwl_rxon_add_station(priv, priv->bssid, 1);
2614
2615         spin_lock_irqsave(&priv->lock, flags);
2616         priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
2617         priv->error_recovering = 0;
2618         spin_unlock_irqrestore(&priv->lock, flags);
2619 }
2620
2621 static void iwl4965_irq_tasklet(struct iwl_priv *priv)
2622 {
2623         u32 inta, handled = 0;
2624         u32 inta_fh;
2625         unsigned long flags;
2626 #ifdef CONFIG_IWLWIFI_DEBUG
2627         u32 inta_mask;
2628 #endif
2629
2630         spin_lock_irqsave(&priv->lock, flags);
2631
2632         /* Ack/clear/reset pending uCode interrupts.
2633          * Note:  Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
2634          *  and will clear only when CSR_FH_INT_STATUS gets cleared. */
2635         inta = iwl_read32(priv, CSR_INT);
2636         iwl_write32(priv, CSR_INT, inta);
2637
2638         /* Ack/clear/reset pending flow-handler (DMA) interrupts.
2639          * Any new interrupts that happen after this, either while we're
2640          * in this tasklet, or later, will show up in next ISR/tasklet. */
2641         inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2642         iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
2643
2644 #ifdef CONFIG_IWLWIFI_DEBUG
2645         if (priv->debug_level & IWL_DL_ISR) {
2646                 /* just for debug */
2647                 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2648                 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2649                               inta, inta_mask, inta_fh);
2650         }
2651 #endif
2652
2653         /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
2654          * atomic, make sure that inta covers all the interrupts that
2655          * we've discovered, even if FH interrupt came in just after
2656          * reading CSR_INT. */
2657         if (inta_fh & CSR49_FH_INT_RX_MASK)
2658                 inta |= CSR_INT_BIT_FH_RX;
2659         if (inta_fh & CSR49_FH_INT_TX_MASK)
2660                 inta |= CSR_INT_BIT_FH_TX;
2661
2662         /* Now service all interrupt bits discovered above. */
2663         if (inta & CSR_INT_BIT_HW_ERR) {
2664                 IWL_ERROR("Microcode HW error detected.  Restarting.\n");
2665
2666                 /* Tell the device to stop sending interrupts */
2667                 iwl4965_disable_interrupts(priv);
2668
2669                 iwl4965_irq_handle_error(priv);
2670
2671                 handled |= CSR_INT_BIT_HW_ERR;
2672
2673                 spin_unlock_irqrestore(&priv->lock, flags);
2674
2675                 return;
2676         }
2677
2678 #ifdef CONFIG_IWLWIFI_DEBUG
2679         if (priv->debug_level & (IWL_DL_ISR)) {
2680                 /* NIC fires this, but we don't use it, redundant with WAKEUP */
2681                 if (inta & CSR_INT_BIT_SCD)
2682                         IWL_DEBUG_ISR("Scheduler finished to transmit "
2683                                       "the frame/frames.\n");
2684
2685                 /* Alive notification via Rx interrupt will do the real work */
2686                 if (inta & CSR_INT_BIT_ALIVE)
2687                         IWL_DEBUG_ISR("Alive interrupt\n");
2688         }
2689 #endif
2690         /* Safely ignore these bits for debug checks below */
2691         inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
2692
2693         /* HW RF KILL switch toggled */
2694         if (inta & CSR_INT_BIT_RF_KILL) {
2695                 int hw_rf_kill = 0;
2696                 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
2697                                 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2698                         hw_rf_kill = 1;
2699
2700                 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
2701                                 hw_rf_kill ? "disable radio":"enable radio");
2702
2703                 /* Queue restart only if RF_KILL switch was set to "kill"
2704                  *   when we loaded driver, and is now set to "enable".
2705                  * After we're Alive, RF_KILL gets handled by
2706                  *   iwl4965_rx_card_state_notif() */
2707                 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
2708                         clear_bit(STATUS_RF_KILL_HW, &priv->status);
2709                         queue_work(priv->workqueue, &priv->restart);
2710                 }
2711
2712                 handled |= CSR_INT_BIT_RF_KILL;
2713         }
2714
2715         /* Chip got too hot and stopped itself */
2716         if (inta & CSR_INT_BIT_CT_KILL) {
2717                 IWL_ERROR("Microcode CT kill error detected.\n");
2718                 handled |= CSR_INT_BIT_CT_KILL;
2719         }
2720
2721         /* Error detected by uCode */
2722         if (inta & CSR_INT_BIT_SW_ERR) {
2723                 IWL_ERROR("Microcode SW error detected.  Restarting 0x%X.\n",
2724                           inta);
2725                 iwl4965_irq_handle_error(priv);
2726                 handled |= CSR_INT_BIT_SW_ERR;
2727         }
2728
2729         /* uCode wakes up after power-down sleep */
2730         if (inta & CSR_INT_BIT_WAKEUP) {
2731                 IWL_DEBUG_ISR("Wakeup interrupt\n");
2732                 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
2733                 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
2734                 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
2735                 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
2736                 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
2737                 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
2738                 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
2739
2740                 handled |= CSR_INT_BIT_WAKEUP;
2741         }
2742
2743         /* All uCode command responses, including Tx command responses,
2744          * Rx "responses" (frame-received notification), and other
2745          * notifications from uCode come through here*/
2746         if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
2747                 iwl_rx_handle(priv);
2748                 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
2749         }
2750
2751         if (inta & CSR_INT_BIT_FH_TX) {
2752                 IWL_DEBUG_ISR("Tx interrupt\n");
2753                 handled |= CSR_INT_BIT_FH_TX;
2754                 /* FH finished to write, send event */
2755                 priv->ucode_write_complete = 1;
2756                 wake_up_interruptible(&priv->wait_command_queue);
2757         }
2758
2759         if (inta & ~handled)
2760                 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
2761
2762         if (inta & ~CSR_INI_SET_MASK) {
2763                 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
2764                          inta & ~CSR_INI_SET_MASK);
2765                 IWL_WARNING("   with FH_INT = 0x%08x\n", inta_fh);
2766         }
2767
2768         /* Re-enable all interrupts */
2769         /* only Re-enable if diabled by irq */
2770         if (test_bit(STATUS_INT_ENABLED, &priv->status))
2771                 iwl4965_enable_interrupts(priv);
2772
2773 #ifdef CONFIG_IWLWIFI_DEBUG
2774         if (priv->debug_level & (IWL_DL_ISR)) {
2775                 inta = iwl_read32(priv, CSR_INT);
2776                 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2777                 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2778                 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
2779                         "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
2780         }
2781 #endif
2782         spin_unlock_irqrestore(&priv->lock, flags);
2783 }
2784
2785 static irqreturn_t iwl4965_isr(int irq, void *data)
2786 {
2787         struct iwl_priv *priv = data;
2788         u32 inta, inta_mask;
2789         u32 inta_fh;
2790         if (!priv)
2791                 return IRQ_NONE;
2792
2793         spin_lock(&priv->lock);
2794
2795         /* Disable (but don't clear!) interrupts here to avoid
2796          *    back-to-back ISRs and sporadic interrupts from our NIC.
2797          * If we have something to service, the tasklet will re-enable ints.
2798          * If we *don't* have something, we'll re-enable before leaving here. */
2799         inta_mask = iwl_read32(priv, CSR_INT_MASK);  /* just for debug */
2800         iwl_write32(priv, CSR_INT_MASK, 0x00000000);
2801
2802         /* Discover which interrupts are active/pending */
2803         inta = iwl_read32(priv, CSR_INT);
2804         inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2805
2806         /* Ignore interrupt if there's nothing in NIC to service.
2807          * This may be due to IRQ shared with another device,
2808          * or due to sporadic interrupts thrown from our NIC. */
2809         if (!inta && !inta_fh) {
2810                 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
2811                 goto none;
2812         }
2813
2814         if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
2815                 /* Hardware disappeared. It might have already raised
2816                  * an interrupt */
2817                 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
2818                 goto unplugged;
2819         }
2820
2821         IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2822                       inta, inta_mask, inta_fh);
2823
2824         inta &= ~CSR_INT_BIT_SCD;
2825
2826         /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
2827         if (likely(inta || inta_fh))
2828                 tasklet_schedule(&priv->irq_tasklet);
2829
2830  unplugged:
2831         spin_unlock(&priv->lock);
2832         return IRQ_HANDLED;
2833
2834  none:
2835         /* re-enable interrupts here since we don't have anything to service. */
2836         /* only Re-enable if diabled by irq */
2837         if (test_bit(STATUS_INT_ENABLED, &priv->status))
2838                 iwl4965_enable_interrupts(priv);
2839         spin_unlock(&priv->lock);
2840         return IRQ_NONE;
2841 }
2842
2843 /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
2844  * sending probe req.  This should be set long enough to hear probe responses
2845  * from more than one AP.  */
2846 #define IWL_ACTIVE_DWELL_TIME_24    (20)        /* all times in msec */
2847 #define IWL_ACTIVE_DWELL_TIME_52    (10)
2848
2849 /* For faster active scanning, scan will move to the next channel if fewer than
2850  * PLCP_QUIET_THRESH packets are heard on this channel within
2851  * ACTIVE_QUIET_TIME after sending probe request.  This shortens the dwell
2852  * time if it's a quiet channel (nothing responded to our probe, and there's
2853  * no other traffic).
2854  * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
2855 #define IWL_PLCP_QUIET_THRESH       __constant_cpu_to_le16(1)   /* packets */
2856 #define IWL_ACTIVE_QUIET_TIME       __constant_cpu_to_le16(5)   /* msec */
2857
2858 /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
2859  * Must be set longer than active dwell time.
2860  * For the most reliable scan, set > AP beacon interval (typically 100msec). */
2861 #define IWL_PASSIVE_DWELL_TIME_24   (20)        /* all times in msec */
2862 #define IWL_PASSIVE_DWELL_TIME_52   (10)
2863 #define IWL_PASSIVE_DWELL_BASE      (100)
2864 #define IWL_CHANNEL_TUNE_TIME       5
2865
2866 static inline u16 iwl4965_get_active_dwell_time(struct iwl_priv *priv,
2867                                                 enum ieee80211_band band)
2868 {
2869         if (band == IEEE80211_BAND_5GHZ)
2870                 return IWL_ACTIVE_DWELL_TIME_52;
2871         else
2872                 return IWL_ACTIVE_DWELL_TIME_24;
2873 }
2874
2875 static u16 iwl4965_get_passive_dwell_time(struct iwl_priv *priv,
2876                                           enum ieee80211_band band)
2877 {
2878         u16 active = iwl4965_get_active_dwell_time(priv, band);
2879         u16 passive = (band != IEEE80211_BAND_5GHZ) ?
2880             IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
2881             IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
2882
2883         if (iwl_is_associated(priv)) {
2884                 /* If we're associated, we clamp the maximum passive
2885                  * dwell time to be 98% of the beacon interval (minus
2886                  * 2 * channel tune time) */
2887                 passive = priv->beacon_int;
2888                 if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
2889                         passive = IWL_PASSIVE_DWELL_BASE;
2890                 passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
2891         }
2892
2893         if (passive <= active)
2894                 passive = active + 1;
2895
2896         return passive;
2897 }
2898
2899 static int iwl4965_get_channels_for_scan(struct iwl_priv *priv,
2900                                          enum ieee80211_band band,
2901                                      u8 is_active, u8 direct_mask,
2902                                      struct iwl4965_scan_channel *scan_ch)
2903 {
2904         const struct ieee80211_channel *channels = NULL;
2905         const struct ieee80211_supported_band *sband;
2906         const struct iwl_channel_info *ch_info;
2907         u16 passive_dwell = 0;
2908         u16 active_dwell = 0;
2909         int added, i;
2910
2911         sband = iwl_get_hw_mode(priv, band);
2912         if (!sband)
2913                 return 0;
2914
2915         channels = sband->channels;
2916
2917         active_dwell = iwl4965_get_active_dwell_time(priv, band);
2918         passive_dwell = iwl4965_get_passive_dwell_time(priv, band);
2919
2920         for (i = 0, added = 0; i < sband->n_channels; i++) {
2921                 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2922                         continue;
2923
2924                 scan_ch->channel = ieee80211_frequency_to_channel(channels[i].center_freq);
2925
2926                 ch_info = iwl_get_channel_info(priv, band,
2927                                          scan_ch->channel);
2928                 if (!is_channel_valid(ch_info)) {
2929                         IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
2930                                        scan_ch->channel);
2931                         continue;
2932                 }
2933
2934                 if (!is_active || is_channel_passive(ch_info) ||
2935                     (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN))
2936                         scan_ch->type = 0;      /* passive */
2937                 else
2938                         scan_ch->type = 1;      /* active */
2939
2940                 if (scan_ch->type & 1)
2941                         scan_ch->type |= (direct_mask << 1);
2942
2943                 scan_ch->active_dwell = cpu_to_le16(active_dwell);
2944                 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
2945
2946                 /* Set txpower levels to defaults */
2947                 scan_ch->tpc.dsp_atten = 110;
2948                 /* scan_pwr_info->tpc.dsp_atten; */
2949
2950                 /*scan_pwr_info->tpc.tx_gain; */
2951                 if (band == IEEE80211_BAND_5GHZ)
2952                         scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
2953                 else {
2954                         scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
2955                         /* NOTE: if we were doing 6Mb OFDM for scans we'd use
2956                          * power level:
2957                          * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
2958                          */
2959                 }
2960
2961                 IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
2962                                scan_ch->channel,
2963                                (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
2964                                (scan_ch->type & 1) ?
2965                                active_dwell : passive_dwell);
2966
2967                 scan_ch++;
2968                 added++;
2969         }
2970
2971         IWL_DEBUG_SCAN("total channels to scan %d \n", added);
2972         return added;
2973 }
2974
2975 /******************************************************************************
2976  *
2977  * uCode download functions
2978  *
2979  ******************************************************************************/
2980
2981 static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
2982 {
2983         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
2984         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
2985         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2986         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
2987         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2988         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
2989 }
2990
2991 static void iwl4965_nic_start(struct iwl_priv *priv)
2992 {
2993         /* Remove all resets to allow NIC to operate */
2994         iwl_write32(priv, CSR_RESET, 0);
2995 }
2996
2997
2998 /**
2999  * iwl4965_read_ucode - Read uCode images from disk file.
3000  *
3001  * Copy into buffers for card to fetch via bus-mastering
3002  */
3003 static int iwl4965_read_ucode(struct iwl_priv *priv)
3004 {
3005         struct iwl4965_ucode *ucode;
3006         int ret;
3007         const struct firmware *ucode_raw;
3008         const char *name = priv->cfg->fw_name;
3009         u8 *src;
3010         size_t len;
3011         u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
3012
3013         /* Ask kernel firmware_class module to get the boot firmware off disk.
3014          * request_firmware() is synchronous, file is in memory on return. */
3015         ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
3016         if (ret < 0) {
3017                 IWL_ERROR("%s firmware file req failed: Reason %d\n",
3018                                         name, ret);
3019                 goto error;
3020         }
3021
3022         IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
3023                        name, ucode_raw->size);
3024
3025         /* Make sure that we got at least our header! */
3026         if (ucode_raw->size < sizeof(*ucode)) {
3027                 IWL_ERROR("File size way too small!\n");
3028                 ret = -EINVAL;
3029                 goto err_release;
3030         }
3031
3032         /* Data from ucode file:  header followed by uCode images */
3033         ucode = (void *)ucode_raw->data;
3034
3035         ver = le32_to_cpu(ucode->ver);
3036         inst_size = le32_to_cpu(ucode->inst_size);
3037         data_size = le32_to_cpu(ucode->data_size);
3038         init_size = le32_to_cpu(ucode->init_size);
3039         init_data_size = le32_to_cpu(ucode->init_data_size);
3040         boot_size = le32_to_cpu(ucode->boot_size);
3041
3042         IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
3043         IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
3044                        inst_size);
3045         IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
3046                        data_size);
3047         IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
3048                        init_size);
3049         IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
3050                        init_data_size);
3051         IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
3052                        boot_size);
3053
3054         /* Verify size of file vs. image size info in file's header */
3055         if (ucode_raw->size < sizeof(*ucode) +
3056                 inst_size + data_size + init_size +
3057                 init_data_size + boot_size) {
3058
3059                 IWL_DEBUG_INFO("uCode file size %d too small\n",
3060                                (int)ucode_raw->size);
3061                 ret = -EINVAL;
3062                 goto err_release;
3063         }
3064
3065         /* Verify that uCode images will fit in card's SRAM */
3066         if (inst_size > priv->hw_params.max_inst_size) {
3067                 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
3068                                inst_size);
3069                 ret = -EINVAL;
3070                 goto err_release;
3071         }
3072
3073         if (data_size > priv->hw_params.max_data_size) {
3074                 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
3075                                 data_size);
3076                 ret = -EINVAL;
3077                 goto err_release;
3078         }
3079         if (init_size > priv->hw_params.max_inst_size) {
3080                 IWL_DEBUG_INFO
3081                     ("uCode init instr len %d too large to fit in\n",
3082                       init_size);
3083                 ret = -EINVAL;
3084                 goto err_release;
3085         }
3086         if (init_data_size > priv->hw_params.max_data_size) {
3087                 IWL_DEBUG_INFO
3088                     ("uCode init data len %d too large to fit in\n",
3089                       init_data_size);
3090                 ret = -EINVAL;
3091                 goto err_release;
3092         }
3093         if (boot_size > priv->hw_params.max_bsm_size) {
3094                 IWL_DEBUG_INFO
3095                     ("uCode boot instr len %d too large to fit in\n",
3096                       boot_size);
3097                 ret = -EINVAL;
3098                 goto err_release;
3099         }
3100
3101         /* Allocate ucode buffers for card's bus-master loading ... */
3102
3103         /* Runtime instructions and 2 copies of data:
3104          * 1) unmodified from disk
3105          * 2) backup cache for save/restore during power-downs */
3106         priv->ucode_code.len = inst_size;
3107         iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
3108
3109         priv->ucode_data.len = data_size;
3110         iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
3111
3112         priv->ucode_data_backup.len = data_size;
3113         iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
3114
3115         /* Initialization instructions and data */
3116         if (init_size && init_data_size) {
3117                 priv->ucode_init.len = init_size;
3118                 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
3119
3120                 priv->ucode_init_data.len = init_data_size;
3121                 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
3122
3123                 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
3124                         goto err_pci_alloc;
3125         }
3126
3127         /* Bootstrap (instructions only, no data) */
3128         if (boot_size) {
3129                 priv->ucode_boot.len = boot_size;
3130                 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
3131
3132                 if (!priv->ucode_boot.v_addr)
3133                         goto err_pci_alloc;
3134         }
3135
3136         /* Copy images into buffers for card's bus-master reads ... */
3137
3138         /* Runtime instructions (first block of data in file) */
3139         src = &ucode->data[0];
3140         len = priv->ucode_code.len;
3141         IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
3142         memcpy(priv->ucode_code.v_addr, src, len);
3143         IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
3144                 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
3145
3146         /* Runtime data (2nd block)
3147          * NOTE:  Copy into backup buffer will be done in iwl4965_up()  */
3148         src = &ucode->data[inst_size];
3149         len = priv->ucode_data.len;
3150         IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
3151         memcpy(priv->ucode_data.v_addr, src, len);
3152         memcpy(priv->ucode_data_backup.v_addr, src, len);
3153
3154         /* Initialization instructions (3rd block) */
3155         if (init_size) {
3156                 src = &ucode->data[inst_size + data_size];
3157                 len = priv->ucode_init.len;
3158                 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
3159                                 len);
3160                 memcpy(priv->ucode_init.v_addr, src, len);
3161         }
3162
3163         /* Initialization data (4th block) */
3164         if (init_data_size) {
3165                 src = &ucode->data[inst_size + data_size + init_size];
3166                 len = priv->ucode_init_data.len;
3167                 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
3168                                len);
3169                 memcpy(priv->ucode_init_data.v_addr, src, len);
3170         }
3171
3172         /* Bootstrap instructions (5th block) */
3173         src = &ucode->data[inst_size + data_size + init_size + init_data_size];
3174         len = priv->ucode_boot.len;
3175         IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
3176         memcpy(priv->ucode_boot.v_addr, src, len);
3177
3178         /* We have our copies now, allow OS release its copies */
3179         release_firmware(ucode_raw);
3180         return 0;
3181
3182  err_pci_alloc:
3183         IWL_ERROR("failed to allocate pci memory\n");
3184         ret = -ENOMEM;
3185         iwl4965_dealloc_ucode_pci(priv);
3186
3187  err_release:
3188         release_firmware(ucode_raw);
3189
3190  error:
3191         return ret;
3192 }
3193
3194 /**
3195  * iwl_alive_start - called after REPLY_ALIVE notification received
3196  *                   from protocol/runtime uCode (initialization uCode's
3197  *                   Alive gets handled by iwl_init_alive_start()).
3198  */
3199 static void iwl_alive_start(struct iwl_priv *priv)
3200 {
3201         int ret = 0;
3202
3203         IWL_DEBUG_INFO("Runtime Alive received.\n");
3204
3205         if (priv->card_alive.is_valid != UCODE_VALID_OK) {
3206                 /* We had an error bringing up the hardware, so take it
3207                  * all the way back down so we can try again */
3208                 IWL_DEBUG_INFO("Alive failed.\n");
3209                 goto restart;
3210         }
3211
3212         /* Initialize uCode has loaded Runtime uCode ... verify inst image.
3213          * This is a paranoid check, because we would not have gotten the
3214          * "runtime" alive if code weren't properly loaded.  */
3215         if (iwl_verify_ucode(priv)) {
3216                 /* Runtime instruction load was bad;
3217                  * take it all the way back down so we can try again */
3218                 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
3219                 goto restart;
3220         }
3221
3222         iwlcore_clear_stations_table(priv);
3223         ret = priv->cfg->ops->lib->alive_notify(priv);
3224         if (ret) {
3225                 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
3226                             ret);
3227                 goto restart;
3228         }
3229
3230         /* After the ALIVE response, we can send host commands to 4965 uCode */
3231         set_bit(STATUS_ALIVE, &priv->status);
3232
3233         /* Clear out the uCode error bit if it is set */
3234         clear_bit(STATUS_FW_ERROR, &priv->status);
3235
3236         if (iwl_is_rfkill(priv))
3237                 return;
3238
3239         ieee80211_wake_queues(priv->hw);
3240
3241         priv->active_rate = priv->rates_mask;
3242         priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
3243
3244         if (iwl_is_associated(priv)) {
3245                 struct iwl_rxon_cmd *active_rxon =
3246                                 (struct iwl_rxon_cmd *)&priv->active_rxon;
3247
3248                 memcpy(&priv->staging_rxon, &priv->active_rxon,
3249                        sizeof(priv->staging_rxon));
3250                 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3251         } else {
3252                 /* Initialize our rx_config data */
3253                 iwl4965_connection_init_rx_config(priv);
3254                 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
3255         }
3256
3257         /* Configure Bluetooth device coexistence support */
3258         iwl4965_send_bt_config(priv);
3259
3260         iwl_reset_run_time_calib(priv);
3261
3262         /* Configure the adapter for unassociated operation */
3263         iwl4965_commit_rxon(priv);
3264
3265         /* At this point, the NIC is initialized and operational */
3266         iwl4965_rf_kill_ct_config(priv);
3267
3268         iwl_leds_register(priv);
3269
3270         IWL_DEBUG_INFO("ALIVE processing complete.\n");
3271         set_bit(STATUS_READY, &priv->status);
3272         wake_up_interruptible(&priv->wait_command_queue);
3273
3274         if (priv->error_recovering)
3275                 iwl4965_error_recovery(priv);
3276
3277         iwlcore_low_level_notify(priv, IWLCORE_START_EVT);
3278         ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
3279         return;
3280
3281  restart:
3282         queue_work(priv->workqueue, &priv->restart);
3283 }
3284
3285 static void iwl4965_cancel_deferred_work(struct iwl_priv *priv);
3286
3287 static void __iwl4965_down(struct iwl_priv *priv)
3288 {
3289         unsigned long flags;
3290         int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
3291
3292         IWL_DEBUG_INFO(DRV_NAME " is going down\n");
3293
3294         if (!exit_pending)
3295                 set_bit(STATUS_EXIT_PENDING, &priv->status);
3296
3297         iwl_leds_unregister(priv);
3298
3299         iwlcore_low_level_notify(priv, IWLCORE_STOP_EVT);
3300
3301         iwlcore_clear_stations_table(priv);
3302
3303         /* Unblock any waiting calls */
3304         wake_up_interruptible_all(&priv->wait_command_queue);
3305
3306         /* Wipe out the EXIT_PENDING status bit if we are not actually
3307          * exiting the module */
3308         if (!exit_pending)
3309                 clear_bit(STATUS_EXIT_PENDING, &priv->status);
3310
3311         /* stop and reset the on-board processor */
3312         iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
3313
3314         /* tell the device to stop sending interrupts */
3315         spin_lock_irqsave(&priv->lock, flags);
3316         iwl4965_disable_interrupts(priv);
3317         spin_unlock_irqrestore(&priv->lock, flags);
3318         iwl_synchronize_irq(priv);
3319
3320         if (priv->mac80211_registered)
3321                 ieee80211_stop_queues(priv->hw);
3322
3323         /* If we have not previously called iwl4965_init() then
3324          * clear all bits but the RF Kill and SUSPEND bits and return */
3325         if (!iwl_is_init(priv)) {
3326                 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
3327                                         STATUS_RF_KILL_HW |
3328                                test_bit(STATUS_RF_KILL_SW, &priv->status) <<
3329                                         STATUS_RF_KILL_SW |
3330                                test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
3331                                         STATUS_GEO_CONFIGURED |
3332                                test_bit(STATUS_IN_SUSPEND, &priv->status) <<
3333                                         STATUS_IN_SUSPEND;
3334                 goto exit;
3335         }
3336
3337         /* ...otherwise clear out all the status bits but the RF Kill and
3338          * SUSPEND bits and continue taking the NIC down. */
3339         priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
3340                                 STATUS_RF_KILL_HW |
3341                         test_bit(STATUS_RF_KILL_SW, &priv->status) <<
3342                                 STATUS_RF_KILL_SW |
3343                         test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
3344                                 STATUS_GEO_CONFIGURED |
3345                         test_bit(STATUS_IN_SUSPEND, &priv->status) <<
3346                                 STATUS_IN_SUSPEND |
3347                         test_bit(STATUS_FW_ERROR, &priv->status) <<
3348                                 STATUS_FW_ERROR;
3349
3350         spin_lock_irqsave(&priv->lock, flags);
3351         iwl_clear_bit(priv, CSR_GP_CNTRL,
3352                          CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3353         spin_unlock_irqrestore(&priv->lock, flags);
3354
3355         iwl_txq_ctx_stop(priv);
3356         iwl_rxq_stop(priv);
3357
3358         spin_lock_irqsave(&priv->lock, flags);
3359         if (!iwl_grab_nic_access(priv)) {
3360                 iwl_write_prph(priv, APMG_CLK_DIS_REG,
3361                                          APMG_CLK_VAL_DMA_CLK_RQT);
3362                 iwl_release_nic_access(priv);
3363         }
3364         spin_unlock_irqrestore(&priv->lock, flags);
3365
3366         udelay(5);
3367
3368         /* FIXME: apm_ops.suspend(priv) */
3369         priv->cfg->ops->lib->apm_ops.reset(priv);
3370         priv->cfg->ops->lib->free_shared_mem(priv);
3371
3372  exit:
3373         memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
3374
3375         if (priv->ibss_beacon)
3376                 dev_kfree_skb(priv->ibss_beacon);
3377         priv->ibss_beacon = NULL;
3378
3379         /* clear out any free frames */
3380         iwl_clear_free_frames(priv);
3381 }
3382
3383 static void iwl4965_down(struct iwl_priv *priv)
3384 {
3385         mutex_lock(&priv->mutex);
3386         __iwl4965_down(priv);
3387         mutex_unlock(&priv->mutex);
3388
3389         iwl4965_cancel_deferred_work(priv);
3390 }
3391
3392 #define MAX_HW_RESTARTS 5
3393
3394 static int __iwl4965_up(struct iwl_priv *priv)
3395 {
3396         int i;
3397         int ret;
3398
3399         if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3400                 IWL_WARNING("Exit pending; will not bring the NIC up\n");
3401                 return -EIO;
3402         }
3403
3404         if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
3405                 IWL_WARNING("Radio disabled by SW RF kill (module "
3406                             "parameter)\n");
3407                 iwl_rfkill_set_hw_state(priv);
3408                 return -ENODEV;
3409         }
3410
3411         if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
3412                 IWL_ERROR("ucode not available for device bringup\n");
3413                 return -EIO;
3414         }
3415
3416         /* If platform's RF_KILL switch is NOT set to KILL */
3417         if (iwl_read32(priv, CSR_GP_CNTRL) &
3418                                 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3419                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3420         else {
3421                 set_bit(STATUS_RF_KILL_HW, &priv->status);
3422                 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
3423                         iwl_rfkill_set_hw_state(priv);
3424                         IWL_WARNING("Radio disabled by HW RF Kill switch\n");
3425                         return -ENODEV;
3426                 }
3427         }
3428
3429         iwl_rfkill_set_hw_state(priv);
3430         iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
3431
3432         ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
3433         if (ret) {
3434                 IWL_ERROR("Unable to allocate shared memory\n");
3435                 return ret;
3436         }
3437
3438         ret = iwl_hw_nic_init(priv);
3439         if (ret) {
3440                 IWL_ERROR("Unable to init nic\n");
3441                 return ret;
3442         }
3443
3444         /* make sure rfkill handshake bits are cleared */
3445         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3446         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
3447                     CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3448
3449         /* clear (again), then enable host interrupts */
3450         iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
3451         iwl4965_enable_interrupts(priv);
3452
3453         /* really make sure rfkill handshake bits are cleared */
3454         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3455         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3456
3457         /* Copy original ucode data image from disk into backup cache.
3458          * This will be used to initialize the on-board processor's
3459          * data SRAM for a clean start when the runtime program first loads. */
3460         memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
3461                priv->ucode_data.len);
3462
3463         /* We return success when we resume from suspend and rf_kill is on. */
3464         if (test_bit(STATUS_RF_KILL_HW, &priv->status))
3465                 return 0;
3466
3467         for (i = 0; i < MAX_HW_RESTARTS; i++) {
3468
3469                 iwlcore_clear_stations_table(priv);
3470
3471                 /* load bootstrap state machine,
3472                  * load bootstrap program into processor's memory,
3473                  * prepare to load the "initialize" uCode */
3474                 ret = priv->cfg->ops->lib->load_ucode(priv);
3475
3476                 if (ret) {
3477                         IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
3478                         continue;
3479                 }
3480
3481                 /* start card; "initialize" will load runtime ucode */
3482                 iwl4965_nic_start(priv);
3483
3484                 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
3485
3486                 return 0;
3487         }
3488
3489         set_bit(STATUS_EXIT_PENDING, &priv->status);
3490         __iwl4965_down(priv);
3491
3492         /* tried to restart and config the device for as long as our
3493          * patience could withstand */
3494         IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
3495         return -EIO;
3496 }
3497
3498
3499 /*****************************************************************************
3500  *
3501  * Workqueue callbacks
3502  *
3503  *****************************************************************************/
3504
3505 static void iwl_bg_init_alive_start(struct work_struct *data)
3506 {
3507         struct iwl_priv *priv =
3508             container_of(data, struct iwl_priv, init_alive_start.work);
3509
3510         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3511                 return;
3512
3513         mutex_lock(&priv->mutex);
3514         priv->cfg->ops->lib->init_alive_start(priv);
3515         mutex_unlock(&priv->mutex);
3516 }
3517
3518 static void iwl_bg_alive_start(struct work_struct *data)
3519 {
3520         struct iwl_priv *priv =
3521             container_of(data, struct iwl_priv, alive_start.work);
3522
3523         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3524                 return;
3525
3526         mutex_lock(&priv->mutex);
3527         iwl_alive_start(priv);
3528         mutex_unlock(&priv->mutex);
3529 }
3530
3531 static void iwl4965_bg_rf_kill(struct work_struct *work)
3532 {
3533         struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
3534
3535         wake_up_interruptible(&priv->wait_command_queue);
3536
3537         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3538                 return;
3539
3540         mutex_lock(&priv->mutex);
3541
3542         if (!iwl_is_rfkill(priv)) {
3543                 IWL_DEBUG(IWL_DL_RF_KILL,
3544                           "HW and/or SW RF Kill no longer active, restarting "
3545                           "device\n");
3546                 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
3547                         queue_work(priv->workqueue, &priv->restart);
3548         } else {
3549                 /* make sure mac80211 stop sending Tx frame */
3550                 if (priv->mac80211_registered)
3551                         ieee80211_stop_queues(priv->hw);
3552
3553                 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
3554                         IWL_DEBUG_RF_KILL("Can not turn radio back on - "
3555                                           "disabled by SW switch\n");
3556                 else
3557                         IWL_WARNING("Radio Frequency Kill Switch is On:\n"
3558                                     "Kill switch must be turned off for "
3559                                     "wireless networking to work.\n");
3560         }
3561         iwl_rfkill_set_hw_state(priv);
3562
3563         mutex_unlock(&priv->mutex);
3564 }
3565
3566 static void iwl4965_bg_set_monitor(struct work_struct *work)
3567 {
3568         struct iwl_priv *priv = container_of(work,
3569                                 struct iwl_priv, set_monitor);
3570
3571         IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
3572
3573         mutex_lock(&priv->mutex);
3574
3575         if (!iwl_is_ready(priv))
3576                 IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
3577         else
3578                 if (iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR) != 0)
3579                         IWL_ERROR("iwl4965_set_mode() failed\n");
3580
3581         mutex_unlock(&priv->mutex);
3582 }
3583
3584 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
3585
3586 static void iwl4965_bg_scan_check(struct work_struct *data)
3587 {
3588         struct iwl_priv *priv =
3589             container_of(data, struct iwl_priv, scan_check.work);
3590
3591         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3592                 return;
3593
3594         mutex_lock(&priv->mutex);
3595         if (test_bit(STATUS_SCANNING, &priv->status) ||
3596             test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3597                 IWL_DEBUG(IWL_DL_SCAN, "Scan completion watchdog resetting "
3598                         "adapter (%dms)\n",
3599                         jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
3600
3601                 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
3602                         iwl4965_send_scan_abort(priv);
3603         }
3604         mutex_unlock(&priv->mutex);
3605 }
3606
3607 static void iwl4965_bg_request_scan(struct work_struct *data)
3608 {
3609         struct iwl_priv *priv =
3610             container_of(data, struct iwl_priv, request_scan);
3611         struct iwl_host_cmd cmd = {
3612                 .id = REPLY_SCAN_CMD,
3613                 .len = sizeof(struct iwl4965_scan_cmd),
3614                 .meta.flags = CMD_SIZE_HUGE,
3615         };
3616         struct iwl4965_scan_cmd *scan;
3617         struct ieee80211_conf *conf = NULL;
3618         u16 cmd_len;
3619         enum ieee80211_band band;
3620         u8 direct_mask;
3621         int ret = 0;
3622
3623         conf = ieee80211_get_hw_conf(priv->hw);
3624
3625         mutex_lock(&priv->mutex);
3626
3627         if (!iwl_is_ready(priv)) {
3628                 IWL_WARNING("request scan called when driver not ready.\n");
3629                 goto done;
3630         }
3631
3632         /* Make sure the scan wasn't cancelled before this queued work
3633          * was given the chance to run... */
3634         if (!test_bit(STATUS_SCANNING, &priv->status))
3635                 goto done;
3636
3637         /* This should never be called or scheduled if there is currently
3638          * a scan active in the hardware. */
3639         if (test_bit(STATUS_SCAN_HW, &priv->status)) {
3640                 IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
3641                                "Ignoring second request.\n");
3642                 ret = -EIO;
3643                 goto done;
3644         }
3645
3646         if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3647                 IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
3648                 goto done;
3649         }
3650
3651         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3652                 IWL_DEBUG_HC("Scan request while abort pending.  Queuing.\n");
3653                 goto done;
3654         }
3655
3656         if (iwl_is_rfkill(priv)) {
3657                 IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
3658                 goto done;
3659         }
3660
3661         if (!test_bit(STATUS_READY, &priv->status)) {
3662                 IWL_DEBUG_HC("Scan request while uninitialized.  Queuing.\n");
3663                 goto done;
3664         }
3665
3666         if (!priv->scan_bands) {
3667                 IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
3668                 goto done;
3669         }
3670
3671         if (!priv->scan) {
3672                 priv->scan = kmalloc(sizeof(struct iwl4965_scan_cmd) +
3673                                      IWL_MAX_SCAN_SIZE, GFP_KERNEL);
3674                 if (!priv->scan) {
3675                         ret = -ENOMEM;
3676                         goto done;
3677                 }
3678         }
3679         scan = priv->scan;
3680         memset(scan, 0, sizeof(struct iwl4965_scan_cmd) + IWL_MAX_SCAN_SIZE);
3681
3682         scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
3683         scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
3684
3685         if (iwl_is_associated(priv)) {
3686                 u16 interval = 0;
3687                 u32 extra;
3688                 u32 suspend_time = 100;
3689                 u32 scan_suspend_time = 100;
3690                 unsigned long flags;
3691
3692                 IWL_DEBUG_INFO("Scanning while associated...\n");
3693
3694                 spin_lock_irqsave(&priv->lock, flags);
3695                 interval = priv->beacon_int;
3696                 spin_unlock_irqrestore(&priv->lock, flags);
3697
3698                 scan->suspend_time = 0;
3699                 scan->max_out_time = cpu_to_le32(200 * 1024);
3700                 if (!interval)
3701                         interval = suspend_time;
3702
3703                 extra = (suspend_time / interval) << 22;
3704                 scan_suspend_time = (extra |
3705                     ((suspend_time % interval) * 1024));
3706                 scan->suspend_time = cpu_to_le32(scan_suspend_time);
3707                 IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
3708                                scan_suspend_time, interval);
3709         }
3710
3711         /* We should add the ability for user to lock to PASSIVE ONLY */
3712         if (priv->one_direct_scan) {
3713                 IWL_DEBUG_SCAN
3714                     ("Kicking off one direct scan for '%s'\n",
3715                      iwl4965_escape_essid(priv->direct_ssid,
3716                                       priv->direct_ssid_len));
3717                 scan->direct_scan[0].id = WLAN_EID_SSID;
3718                 scan->direct_scan[0].len = priv->direct_ssid_len;
3719                 memcpy(scan->direct_scan[0].ssid,
3720                        priv->direct_ssid, priv->direct_ssid_len);
3721                 direct_mask = 1;
3722         } else if (!iwl_is_associated(priv) && priv->essid_len) {
3723                 IWL_DEBUG_SCAN
3724                   ("Kicking off one direct scan for '%s' when not associated\n",
3725                    iwl4965_escape_essid(priv->essid, priv->essid_len));
3726                 scan->direct_scan[0].id = WLAN_EID_SSID;
3727                 scan->direct_scan[0].len = priv->essid_len;
3728                 memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
3729                 direct_mask = 1;
3730         } else {
3731                 IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
3732                 direct_mask = 0;
3733         }
3734
3735         scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
3736         scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
3737         scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3738
3739
3740         switch (priv->scan_bands) {
3741         case 2:
3742                 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
3743                 scan->tx_cmd.rate_n_flags =
3744                                 iwl4965_hw_set_rate_n_flags(IWL_RATE_1M_PLCP,
3745                                 RATE_MCS_ANT_B_MSK|RATE_MCS_CCK_MSK);
3746
3747                 scan->good_CRC_th = 0;
3748                 band = IEEE80211_BAND_2GHZ;
3749                 break;
3750
3751         case 1:
3752                 scan->tx_cmd.rate_n_flags =
3753                                 iwl4965_hw_set_rate_n_flags(IWL_RATE_6M_PLCP,
3754                                 RATE_MCS_ANT_B_MSK);
3755                 scan->good_CRC_th = IWL_GOOD_CRC_TH;
3756                 band = IEEE80211_BAND_5GHZ;
3757                 break;
3758
3759         default:
3760                 IWL_WARNING("Invalid scan band count\n");
3761                 goto done;
3762         }
3763
3764         /* We don't build a direct scan probe request; the uCode will do
3765          * that based on the direct_mask added to each channel entry */
3766         cmd_len = iwl4965_fill_probe_req(priv, band,
3767                                         (struct ieee80211_mgmt *)scan->data,
3768                                         IWL_MAX_SCAN_SIZE - sizeof(*scan), 0);
3769
3770         scan->tx_cmd.len = cpu_to_le16(cmd_len);
3771         /* select Rx chains */
3772
3773         /* Force use of chains B and C (0x6) for scan Rx.
3774          * Avoid A (0x1) because of its off-channel reception on A-band.
3775          * MIMO is not used here, but value is required to make uCode happy. */
3776         scan->rx_chain = RXON_RX_CHAIN_DRIVER_FORCE_MSK |
3777                         cpu_to_le16((0x7 << RXON_RX_CHAIN_VALID_POS) |
3778                         (0x6 << RXON_RX_CHAIN_FORCE_SEL_POS) |
3779                         (0x7 << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS));
3780
3781         if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
3782                 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
3783
3784         if (direct_mask)
3785                 scan->channel_count =
3786                         iwl4965_get_channels_for_scan(
3787                                 priv, band, 1, /* active */
3788                                 direct_mask,
3789                                 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
3790         else
3791                 scan->channel_count =
3792                         iwl4965_get_channels_for_scan(
3793                                 priv, band, 0, /* passive */
3794                                 direct_mask,
3795                                 (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
3796
3797         scan->filter_flags |= (RXON_FILTER_ACCEPT_GRP_MSK |
3798                                RXON_FILTER_BCON_AWARE_MSK);
3799         cmd.len += le16_to_cpu(scan->tx_cmd.len) +
3800             scan->channel_count * sizeof(struct iwl4965_scan_channel);
3801         cmd.data = scan;
3802         scan->len = cpu_to_le16(cmd.len);
3803
3804         set_bit(STATUS_SCAN_HW, &priv->status);
3805         ret = iwl_send_cmd_sync(priv, &cmd);
3806         if (ret)
3807                 goto done;
3808
3809         queue_delayed_work(priv->workqueue, &priv->scan_check,
3810                            IWL_SCAN_CHECK_WATCHDOG);
3811
3812         mutex_unlock(&priv->mutex);
3813         return;
3814
3815  done:
3816         /* inform mac80211 scan aborted */
3817         queue_work(priv->workqueue, &priv->scan_completed);
3818         mutex_unlock(&priv->mutex);
3819 }
3820
3821 static void iwl4965_bg_up(struct work_struct *data)
3822 {
3823         struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
3824
3825         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3826                 return;
3827
3828         mutex_lock(&priv->mutex);
3829         __iwl4965_up(priv);
3830         mutex_unlock(&priv->mutex);
3831 }
3832
3833 static void iwl4965_bg_restart(struct work_struct *data)
3834 {
3835         struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
3836
3837         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3838                 return;
3839
3840         iwl4965_down(priv);
3841         queue_work(priv->workqueue, &priv->up);
3842 }
3843
3844 static void iwl4965_bg_rx_replenish(struct work_struct *data)
3845 {
3846         struct iwl_priv *priv =
3847             container_of(data, struct iwl_priv, rx_replenish);
3848
3849         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3850                 return;
3851
3852         mutex_lock(&priv->mutex);
3853         iwl_rx_replenish(priv);
3854         mutex_unlock(&priv->mutex);
3855 }
3856
3857 #define IWL_DELAY_NEXT_SCAN (HZ*2)
3858
3859 static void iwl4965_post_associate(struct iwl_priv *priv)
3860 {
3861         struct ieee80211_conf *conf = NULL;
3862         int ret = 0;
3863         DECLARE_MAC_BUF(mac);
3864
3865         if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
3866                 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
3867                 return;
3868         }
3869
3870         IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
3871                         priv->assoc_id,
3872                         print_mac(mac, priv->active_rxon.bssid_addr));
3873
3874
3875         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3876                 return;
3877
3878
3879         if (!priv->vif || !priv->is_open)
3880                 return;
3881
3882         iwl4965_scan_cancel_timeout(priv, 200);
3883
3884         conf = ieee80211_get_hw_conf(priv->hw);
3885
3886         priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3887         iwl4965_commit_rxon(priv);
3888
3889         memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
3890         iwl4965_setup_rxon_timing(priv);
3891         ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3892                               sizeof(priv->rxon_timing), &priv->rxon_timing);
3893         if (ret)
3894                 IWL_WARNING("REPLY_RXON_TIMING failed - "
3895                             "Attempting to continue.\n");
3896
3897         priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
3898
3899 #ifdef CONFIG_IWL4965_HT
3900         if (priv->current_ht_config.is_ht)
3901                 iwl_set_rxon_ht(priv, &priv->current_ht_config);
3902 #endif /* CONFIG_IWL4965_HT*/
3903         iwl_set_rxon_chain(priv);
3904         priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3905
3906         IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
3907                         priv->assoc_id, priv->beacon_int);
3908
3909         if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3910                 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3911         else
3912                 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3913
3914         if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3915                 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
3916                         priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
3917                 else
3918                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3919
3920                 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3921                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3922
3923         }
3924
3925         iwl4965_commit_rxon(priv);
3926
3927         switch (priv->iw_mode) {
3928         case IEEE80211_IF_TYPE_STA:
3929                 iwl4965_rate_scale_init(priv->hw, IWL_AP_ID);
3930                 break;
3931
3932         case IEEE80211_IF_TYPE_IBSS:
3933
3934                 /* clear out the station table */
3935                 iwlcore_clear_stations_table(priv);
3936
3937                 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
3938                 iwl_rxon_add_station(priv, priv->bssid, 0);
3939                 iwl4965_rate_scale_init(priv->hw, IWL_STA_ID);
3940                 iwl4965_send_beacon_cmd(priv);
3941
3942                 break;
3943
3944         default:
3945                 IWL_ERROR("%s Should not be called in %d mode\n",
3946                                 __FUNCTION__, priv->iw_mode);
3947                 break;
3948         }
3949
3950         iwl4965_sequence_reset(priv);
3951
3952         /* Enable Rx differential gain and sensitivity calibrations */
3953         iwl_chain_noise_reset(priv);
3954         priv->start_calib = 1;
3955
3956         if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
3957                 priv->assoc_station_added = 1;
3958
3959         iwl4965_activate_qos(priv, 0);
3960
3961         iwl_power_update_mode(priv, 0);
3962         /* we have just associated, don't start scan too early */
3963         priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
3964 }
3965
3966
3967 static void iwl4965_bg_post_associate(struct work_struct *data)
3968 {
3969         struct iwl_priv *priv = container_of(data, struct iwl_priv,
3970                                              post_associate.work);
3971
3972         mutex_lock(&priv->mutex);
3973         iwl4965_post_associate(priv);
3974         mutex_unlock(&priv->mutex);
3975
3976 }
3977
3978 static void iwl4965_bg_abort_scan(struct work_struct *work)
3979 {
3980         struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
3981
3982         if (!iwl_is_ready(priv))
3983                 return;
3984
3985         mutex_lock(&priv->mutex);
3986
3987         set_bit(STATUS_SCAN_ABORTING, &priv->status);
3988         iwl4965_send_scan_abort(priv);
3989
3990         mutex_unlock(&priv->mutex);
3991 }
3992
3993 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
3994
3995 static void iwl4965_bg_scan_completed(struct work_struct *work)
3996 {
3997         struct iwl_priv *priv =
3998             container_of(work, struct iwl_priv, scan_completed);
3999
4000         IWL_DEBUG(IWL_DL_SCAN, "SCAN complete scan\n");
4001
4002         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4003                 return;
4004
4005         if (test_bit(STATUS_CONF_PENDING, &priv->status))
4006                 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
4007
4008         ieee80211_scan_completed(priv->hw);
4009
4010         /* Since setting the TXPOWER may have been deferred while
4011          * performing the scan, fire one off */
4012         mutex_lock(&priv->mutex);
4013         iwl4965_hw_reg_send_txpower(priv);
4014         mutex_unlock(&priv->mutex);
4015 }
4016
4017 /*****************************************************************************
4018  *
4019  * mac80211 entry point functions
4020  *
4021  *****************************************************************************/
4022
4023 #define UCODE_READY_TIMEOUT     (2 * HZ)
4024
4025 static int iwl4965_mac_start(struct ieee80211_hw *hw)
4026 {
4027         struct iwl_priv *priv = hw->priv;
4028         int ret;
4029
4030         IWL_DEBUG_MAC80211("enter\n");
4031
4032         if (pci_enable_device(priv->pci_dev)) {
4033                 IWL_ERROR("Fail to pci_enable_device\n");
4034                 return -ENODEV;
4035         }
4036         pci_restore_state(priv->pci_dev);
4037         pci_enable_msi(priv->pci_dev);
4038
4039         ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
4040                           DRV_NAME, priv);
4041         if (ret) {
4042                 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
4043                 goto out_disable_msi;
4044         }
4045
4046         /* we should be verifying the device is ready to be opened */
4047         mutex_lock(&priv->mutex);
4048
4049         memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
4050         /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
4051          * ucode filename and max sizes are card-specific. */
4052
4053         if (!priv->ucode_code.len) {
4054                 ret = iwl4965_read_ucode(priv);
4055                 if (ret) {
4056                         IWL_ERROR("Could not read microcode: %d\n", ret);
4057                         mutex_unlock(&priv->mutex);
4058                         goto out_release_irq;
4059                 }
4060         }
4061
4062         ret = __iwl4965_up(priv);
4063
4064         mutex_unlock(&priv->mutex);
4065
4066         if (ret)
4067                 goto out_release_irq;
4068
4069         IWL_DEBUG_INFO("Start UP work done.\n");
4070
4071         if (test_bit(STATUS_IN_SUSPEND, &priv->status))
4072                 return 0;
4073
4074         /* Wait for START_ALIVE from ucode. Otherwise callbacks from
4075          * mac80211 will not be run successfully. */
4076         ret = wait_event_interruptible_timeout(priv->wait_command_queue,
4077                         test_bit(STATUS_READY, &priv->status),
4078                         UCODE_READY_TIMEOUT);
4079         if (!ret) {
4080                 if (!test_bit(STATUS_READY, &priv->status)) {
4081                         IWL_ERROR("Wait for START_ALIVE timeout after %dms.\n",
4082                                   jiffies_to_msecs(UCODE_READY_TIMEOUT));
4083                         ret = -ETIMEDOUT;
4084                         goto out_release_irq;
4085                 }
4086         }
4087
4088         priv->is_open = 1;
4089         IWL_DEBUG_MAC80211("leave\n");
4090         return 0;
4091
4092 out_release_irq:
4093         free_irq(priv->pci_dev->irq, priv);
4094 out_disable_msi:
4095         pci_disable_msi(priv->pci_dev);
4096         pci_disable_device(priv->pci_dev);
4097         priv->is_open = 0;
4098         IWL_DEBUG_MAC80211("leave - failed\n");
4099         return ret;
4100 }
4101
4102 static void iwl4965_mac_stop(struct ieee80211_hw *hw)
4103 {
4104         struct iwl_priv *priv = hw->priv;
4105
4106         IWL_DEBUG_MAC80211("enter\n");
4107
4108         if (!priv->is_open) {
4109                 IWL_DEBUG_MAC80211("leave - skip\n");
4110                 return;
4111         }
4112
4113         priv->is_open = 0;
4114
4115         if (iwl_is_ready_rf(priv)) {
4116                 /* stop mac, cancel any scan request and clear
4117                  * RXON_FILTER_ASSOC_MSK BIT
4118                  */
4119                 mutex_lock(&priv->mutex);
4120                 iwl4965_scan_cancel_timeout(priv, 100);
4121                 cancel_delayed_work(&priv->post_associate);
4122                 mutex_unlock(&priv->mutex);
4123         }
4124
4125         iwl4965_down(priv);
4126
4127         flush_workqueue(priv->workqueue);
4128         free_irq(priv->pci_dev->irq, priv);
4129         pci_disable_msi(priv->pci_dev);
4130         pci_save_state(priv->pci_dev);
4131         pci_disable_device(priv->pci_dev);
4132
4133         IWL_DEBUG_MAC80211("leave\n");
4134 }
4135
4136 static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
4137 {
4138         struct iwl_priv *priv = hw->priv;
4139
4140         IWL_DEBUG_MAC80211("enter\n");
4141
4142         if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
4143                 IWL_DEBUG_MAC80211("leave - monitor\n");
4144                 return -1;
4145         }
4146
4147         IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
4148                      ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
4149
4150         if (iwl_tx_skb(priv, skb))
4151                 dev_kfree_skb_any(skb);
4152
4153         IWL_DEBUG_MAC80211("leave\n");
4154         return 0;
4155 }
4156
4157 static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
4158                                  struct ieee80211_if_init_conf *conf)
4159 {
4160         struct iwl_priv *priv = hw->priv;
4161         unsigned long flags;
4162         DECLARE_MAC_BUF(mac);
4163
4164         IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
4165
4166         if (priv->vif) {
4167                 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
4168                 return -EOPNOTSUPP;
4169         }
4170
4171         spin_lock_irqsave(&priv->lock, flags);
4172         priv->vif = conf->vif;
4173
4174         spin_unlock_irqrestore(&priv->lock, flags);
4175
4176         mutex_lock(&priv->mutex);
4177
4178         if (conf->mac_addr) {
4179                 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
4180                 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
4181         }
4182
4183         if (iwl_is_ready(priv))
4184                 iwl4965_set_mode(priv, conf->type);
4185
4186         mutex_unlock(&priv->mutex);
4187
4188         IWL_DEBUG_MAC80211("leave\n");
4189         return 0;
4190 }
4191
4192 /**
4193  * iwl4965_mac_config - mac80211 config callback
4194  *
4195  * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
4196  * be set inappropriately and the driver currently sets the hardware up to
4197  * use it whenever needed.
4198  */
4199 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
4200 {
4201         struct iwl_priv *priv = hw->priv;
4202         const struct iwl_channel_info *ch_info;
4203         unsigned long flags;
4204         int ret = 0;
4205
4206         mutex_lock(&priv->mutex);
4207         IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
4208
4209         priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
4210
4211         if (!iwl_is_ready(priv)) {
4212                 IWL_DEBUG_MAC80211("leave - not ready\n");
4213                 ret = -EIO;
4214                 goto out;
4215         }
4216
4217         if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
4218                      test_bit(STATUS_SCANNING, &priv->status))) {
4219                 IWL_DEBUG_MAC80211("leave - scanning\n");
4220                 set_bit(STATUS_CONF_PENDING, &priv->status);
4221                 mutex_unlock(&priv->mutex);
4222                 return 0;
4223         }
4224
4225         spin_lock_irqsave(&priv->lock, flags);
4226
4227         ch_info = iwl_get_channel_info(priv, conf->channel->band,
4228                         ieee80211_frequency_to_channel(conf->channel->center_freq));
4229         if (!is_channel_valid(ch_info)) {
4230                 IWL_DEBUG_MAC80211("leave - invalid channel\n");
4231                 spin_unlock_irqrestore(&priv->lock, flags);
4232                 ret = -EINVAL;
4233                 goto out;
4234         }
4235
4236 #ifdef CONFIG_IWL4965_HT
4237         /* if we are switching from ht to 2.4 clear flags
4238          * from any ht related info since 2.4 does not
4239          * support ht */
4240         if ((le16_to_cpu(priv->staging_rxon.channel) != conf->channel->hw_value)
4241 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
4242             && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
4243 #endif
4244         )
4245                 priv->staging_rxon.flags = 0;
4246 #endif /* CONFIG_IWL4965_HT */
4247
4248         iwl_set_rxon_channel(priv, conf->channel->band,
4249                 ieee80211_frequency_to_channel(conf->channel->center_freq));
4250
4251         iwl4965_set_flags_for_phymode(priv, conf->channel->band);
4252
4253         /* The list of supported rates and rate mask can be different
4254          * for each band; since the band may have changed, reset
4255          * the rate mask to what mac80211 lists */
4256         iwl4965_set_rate(priv);
4257
4258         spin_unlock_irqrestore(&priv->lock, flags);
4259
4260 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
4261         if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
4262                 iwl4965_hw_channel_switch(priv, conf->channel);
4263                 goto out;
4264         }
4265 #endif
4266
4267         if (priv->cfg->ops->lib->radio_kill_sw)
4268                 priv->cfg->ops->lib->radio_kill_sw(priv, !conf->radio_enabled);
4269
4270         if (!conf->radio_enabled) {
4271                 IWL_DEBUG_MAC80211("leave - radio disabled\n");
4272                 goto out;
4273         }
4274
4275         if (iwl_is_rfkill(priv)) {
4276                 IWL_DEBUG_MAC80211("leave - RF kill\n");
4277                 ret = -EIO;
4278                 goto out;
4279         }
4280
4281         iwl4965_set_rate(priv);
4282
4283         if (memcmp(&priv->active_rxon,
4284                    &priv->staging_rxon, sizeof(priv->staging_rxon)))
4285                 iwl4965_commit_rxon(priv);
4286         else
4287                 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
4288
4289         IWL_DEBUG_MAC80211("leave\n");
4290
4291 out:
4292         clear_bit(STATUS_CONF_PENDING, &priv->status);
4293         mutex_unlock(&priv->mutex);
4294         return ret;
4295 }
4296
4297 static void iwl4965_config_ap(struct iwl_priv *priv)
4298 {
4299         int ret = 0;
4300
4301         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
4302                 return;
4303
4304         /* The following should be done only at AP bring up */
4305         if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
4306
4307                 /* RXON - unassoc (to set timing command) */
4308                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4309                 iwl4965_commit_rxon(priv);
4310
4311                 /* RXON Timing */
4312                 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
4313                 iwl4965_setup_rxon_timing(priv);
4314                 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
4315                                 sizeof(priv->rxon_timing), &priv->rxon_timing);
4316                 if (ret)
4317                         IWL_WARNING("REPLY_RXON_TIMING failed - "
4318                                         "Attempting to continue.\n");
4319
4320                 iwl_set_rxon_chain(priv);
4321
4322                 /* FIXME: what should be the assoc_id for AP? */
4323                 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
4324                 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
4325                         priv->staging_rxon.flags |=
4326                                 RXON_FLG_SHORT_PREAMBLE_MSK;
4327                 else
4328                         priv->staging_rxon.flags &=
4329                                 ~RXON_FLG_SHORT_PREAMBLE_MSK;
4330
4331                 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
4332                         if (priv->assoc_capability &
4333                                 WLAN_CAPABILITY_SHORT_SLOT_TIME)
4334                                 priv->staging_rxon.flags |=
4335                                         RXON_FLG_SHORT_SLOT_MSK;
4336                         else
4337                                 priv->staging_rxon.flags &=
4338                                         ~RXON_FLG_SHORT_SLOT_MSK;
4339
4340                         if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
4341                                 priv->staging_rxon.flags &=
4342                                         ~RXON_FLG_SHORT_SLOT_MSK;
4343                 }
4344                 /* restore RXON assoc */
4345                 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
4346                 iwl4965_commit_rxon(priv);
4347                 iwl4965_activate_qos(priv, 1);
4348                 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
4349         }
4350         iwl4965_send_beacon_cmd(priv);
4351
4352         /* FIXME - we need to add code here to detect a totally new
4353          * configuration, reset the AP, unassoc, rxon timing, assoc,
4354          * clear sta table, add BCAST sta... */
4355 }
4356
4357 static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
4358                                         struct ieee80211_vif *vif,
4359                                     struct ieee80211_if_conf *conf)
4360 {
4361         struct iwl_priv *priv = hw->priv;
4362         DECLARE_MAC_BUF(mac);
4363         unsigned long flags;
4364         int rc;
4365
4366         if (conf == NULL)
4367                 return -EIO;
4368
4369         if (priv->vif != vif) {
4370                 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
4371                 return 0;
4372         }
4373
4374         if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
4375             (!conf->beacon || !conf->ssid_len)) {
4376                 IWL_DEBUG_MAC80211
4377                     ("Leaving in AP mode because HostAPD is not ready.\n");
4378                 return 0;
4379         }
4380
4381         if (!iwl_is_alive(priv))
4382                 return -EAGAIN;
4383
4384         mutex_lock(&priv->mutex);
4385
4386         if (conf->bssid)
4387                 IWL_DEBUG_MAC80211("bssid: %s\n",
4388                                    print_mac(mac, conf->bssid));
4389
4390 /*
4391  * very dubious code was here; the probe filtering flag is never set:
4392  *
4393         if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
4394             !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
4395  */
4396
4397         if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
4398                 if (!conf->bssid) {
4399                         conf->bssid = priv->mac_addr;
4400                         memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
4401                         IWL_DEBUG_MAC80211("bssid was set to: %s\n",
4402                                            print_mac(mac, conf->bssid));
4403                 }
4404                 if (priv->ibss_beacon)
4405                         dev_kfree_skb(priv->ibss_beacon);
4406
4407                 priv->ibss_beacon = conf->beacon;
4408         }
4409
4410         if (iwl_is_rfkill(priv))
4411                 goto done;
4412
4413         if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
4414             !is_multicast_ether_addr(conf->bssid)) {
4415                 /* If there is currently a HW scan going on in the background
4416                  * then we need to cancel it else the RXON below will fail. */
4417                 if (iwl4965_scan_cancel_timeout(priv, 100)) {
4418                         IWL_WARNING("Aborted scan still in progress "
4419                                     "after 100ms\n");
4420                         IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
4421                         mutex_unlock(&priv->mutex);
4422                         return -EAGAIN;
4423                 }
4424                 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
4425
4426                 /* TODO: Audit driver for usage of these members and see
4427                  * if mac80211 deprecates them (priv->bssid looks like it
4428                  * shouldn't be there, but I haven't scanned the IBSS code
4429                  * to verify) - jpk */
4430                 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
4431
4432                 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
4433                         iwl4965_config_ap(priv);
4434                 else {
4435                         rc = iwl4965_commit_rxon(priv);
4436                         if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
4437                                 iwl_rxon_add_station(
4438                                         priv, priv->active_rxon.bssid_addr, 1);
4439                 }
4440
4441         } else {
4442                 iwl4965_scan_cancel_timeout(priv, 100);
4443                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4444                 iwl4965_commit_rxon(priv);
4445         }
4446
4447  done:
4448         spin_lock_irqsave(&priv->lock, flags);
4449         if (!conf->ssid_len)
4450                 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4451         else
4452                 memcpy(priv->essid, conf->ssid, conf->ssid_len);
4453
4454         priv->essid_len = conf->ssid_len;
4455         spin_unlock_irqrestore(&priv->lock, flags);
4456
4457         IWL_DEBUG_MAC80211("leave\n");
4458         mutex_unlock(&priv->mutex);
4459
4460         return 0;
4461 }
4462
4463 static void iwl4965_configure_filter(struct ieee80211_hw *hw,
4464                                  unsigned int changed_flags,
4465                                  unsigned int *total_flags,
4466                                  int mc_count, struct dev_addr_list *mc_list)
4467 {
4468         /*
4469          * XXX: dummy
4470          * see also iwl4965_connection_init_rx_config
4471          */
4472         struct iwl_priv *priv = hw->priv;
4473         int new_flags = 0;
4474         if (changed_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4475                 if (*total_flags & (FIF_PROMISC_IN_BSS | FIF_OTHER_BSS)) {
4476                         IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
4477                                            IEEE80211_IF_TYPE_MNTR,
4478                                            changed_flags, *total_flags);
4479                         /* queue work 'cuz mac80211 is holding a lock which
4480                          * prevents us from issuing (synchronous) f/w cmds */
4481                         queue_work(priv->workqueue, &priv->set_monitor);
4482                         new_flags &= FIF_PROMISC_IN_BSS |
4483                                      FIF_OTHER_BSS |
4484                                      FIF_ALLMULTI;
4485                 }
4486         }
4487         *total_flags = new_flags;
4488 }
4489
4490 static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
4491                                      struct ieee80211_if_init_conf *conf)
4492 {
4493         struct iwl_priv *priv = hw->priv;
4494
4495         IWL_DEBUG_MAC80211("enter\n");
4496
4497         mutex_lock(&priv->mutex);
4498
4499         if (iwl_is_ready_rf(priv)) {
4500                 iwl4965_scan_cancel_timeout(priv, 100);
4501                 cancel_delayed_work(&priv->post_associate);
4502                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4503                 iwl4965_commit_rxon(priv);
4504         }
4505         if (priv->vif == conf->vif) {
4506                 priv->vif = NULL;
4507                 memset(priv->bssid, 0, ETH_ALEN);
4508                 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
4509                 priv->essid_len = 0;
4510         }
4511         mutex_unlock(&priv->mutex);
4512
4513         IWL_DEBUG_MAC80211("leave\n");
4514
4515 }
4516
4517 #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
4518 static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
4519                                      struct ieee80211_vif *vif,
4520                                      struct ieee80211_bss_conf *bss_conf,
4521                                      u32 changes)
4522 {
4523         struct iwl_priv *priv = hw->priv;
4524
4525         IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
4526
4527         if (changes & BSS_CHANGED_ERP_PREAMBLE) {
4528                 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
4529                                    bss_conf->use_short_preamble);
4530                 if (bss_conf->use_short_preamble)
4531                         priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4532                 else
4533                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
4534         }
4535
4536         if (changes & BSS_CHANGED_ERP_CTS_PROT) {
4537                 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
4538                 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
4539                         priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
4540                 else
4541                         priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
4542         }
4543
4544         if (changes & BSS_CHANGED_HT) {
4545                 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
4546                 iwl4965_ht_conf(priv, bss_conf);
4547                 iwl_set_rxon_chain(priv);
4548         }
4549
4550         if (changes & BSS_CHANGED_ASSOC) {
4551                 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
4552                 /* This should never happen as this function should
4553                  * never be called from interrupt context. */
4554                 if (WARN_ON_ONCE(in_interrupt()))
4555                         return;
4556                 if (bss_conf->assoc) {
4557                         priv->assoc_id = bss_conf->aid;
4558                         priv->beacon_int = bss_conf->beacon_int;
4559                         priv->timestamp = bss_conf->timestamp;
4560                         priv->assoc_capability = bss_conf->assoc_capability;
4561                         priv->next_scan_jiffies = jiffies +
4562                                         IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
4563                         mutex_lock(&priv->mutex);
4564                         iwl4965_post_associate(priv);
4565                         mutex_unlock(&priv->mutex);
4566                 } else {
4567                         priv->assoc_id = 0;
4568                         IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
4569                 }
4570         } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
4571                         IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
4572                         iwl_send_rxon_assoc(priv);
4573         }
4574
4575 }
4576
4577 static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
4578 {
4579         int rc = 0;
4580         unsigned long flags;
4581         struct iwl_priv *priv = hw->priv;
4582
4583         IWL_DEBUG_MAC80211("enter\n");
4584
4585         mutex_lock(&priv->mutex);
4586         spin_lock_irqsave(&priv->lock, flags);
4587
4588         if (!iwl_is_ready_rf(priv)) {
4589                 rc = -EIO;
4590                 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
4591                 goto out_unlock;
4592         }
4593
4594         if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {    /* APs don't scan */
4595                 rc = -EIO;
4596                 IWL_ERROR("ERROR: APs don't scan\n");
4597                 goto out_unlock;
4598         }
4599
4600         /* we don't schedule scan within next_scan_jiffies period */
4601         if (priv->next_scan_jiffies &&
4602                         time_after(priv->next_scan_jiffies, jiffies)) {
4603                 rc = -EAGAIN;
4604                 goto out_unlock;
4605         }
4606         /* if we just finished scan ask for delay */
4607         if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
4608                                 IWL_DELAY_NEXT_SCAN, jiffies)) {
4609                 rc = -EAGAIN;
4610                 goto out_unlock;
4611         }
4612         if (len) {
4613                 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
4614                                iwl4965_escape_essid(ssid, len), (int)len);
4615
4616                 priv->one_direct_scan = 1;
4617                 priv->direct_ssid_len = (u8)
4618                     min((u8) len, (u8) IW_ESSID_MAX_SIZE);
4619                 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
4620         } else
4621                 priv->one_direct_scan = 0;
4622
4623         rc = iwl4965_scan_initiate(priv);
4624
4625         IWL_DEBUG_MAC80211("leave\n");
4626
4627 out_unlock:
4628         spin_unlock_irqrestore(&priv->lock, flags);
4629         mutex_unlock(&priv->mutex);
4630
4631         return rc;
4632 }
4633
4634 static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
4635                         struct ieee80211_key_conf *keyconf, const u8 *addr,
4636                         u32 iv32, u16 *phase1key)
4637 {
4638         struct iwl_priv *priv = hw->priv;
4639         u8 sta_id = IWL_INVALID_STATION;
4640         unsigned long flags;
4641         __le16 key_flags = 0;
4642         int i;
4643         DECLARE_MAC_BUF(mac);
4644
4645         IWL_DEBUG_MAC80211("enter\n");
4646
4647         sta_id = iwl_find_station(priv, addr);
4648         if (sta_id == IWL_INVALID_STATION) {
4649                 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4650                                    print_mac(mac, addr));
4651                 return;
4652         }
4653
4654         iwl4965_scan_cancel_timeout(priv, 100);
4655
4656         key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
4657         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
4658         key_flags &= ~STA_KEY_FLG_INVALID;
4659
4660         if (sta_id == priv->hw_params.bcast_sta_id)
4661                 key_flags |= STA_KEY_MULTICAST_MSK;
4662
4663         spin_lock_irqsave(&priv->sta_lock, flags);
4664
4665         priv->stations[sta_id].sta.key.key_flags = key_flags;
4666         priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
4667
4668         for (i = 0; i < 5; i++)
4669                 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
4670                         cpu_to_le16(phase1key[i]);
4671
4672         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
4673         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
4674
4675         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
4676
4677         spin_unlock_irqrestore(&priv->sta_lock, flags);
4678
4679         IWL_DEBUG_MAC80211("leave\n");
4680 }
4681
4682 static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4683                            const u8 *local_addr, const u8 *addr,
4684                            struct ieee80211_key_conf *key)
4685 {
4686         struct iwl_priv *priv = hw->priv;
4687         DECLARE_MAC_BUF(mac);
4688         int ret = 0;
4689         u8 sta_id = IWL_INVALID_STATION;
4690         u8 is_default_wep_key = 0;
4691
4692         IWL_DEBUG_MAC80211("enter\n");
4693
4694         if (priv->hw_params.sw_crypto) {
4695                 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
4696                 return -EOPNOTSUPP;
4697         }
4698
4699         if (is_zero_ether_addr(addr))
4700                 /* only support pairwise keys */
4701                 return -EOPNOTSUPP;
4702
4703         sta_id = iwl_find_station(priv, addr);
4704         if (sta_id == IWL_INVALID_STATION) {
4705                 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
4706                                    print_mac(mac, addr));
4707                 return -EINVAL;
4708
4709         }
4710
4711         mutex_lock(&priv->mutex);
4712         iwl4965_scan_cancel_timeout(priv, 100);
4713         mutex_unlock(&priv->mutex);
4714
4715         /* If we are getting WEP group key and we didn't receive any key mapping
4716          * so far, we are in legacy wep mode (group key only), otherwise we are
4717          * in 1X mode.
4718          * In legacy wep mode, we use another host command to the uCode */
4719         if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
4720                 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
4721                 if (cmd == SET_KEY)
4722                         is_default_wep_key = !priv->key_mapping_key;
4723                 else
4724                         is_default_wep_key =
4725                                         (key->hw_key_idx == HW_KEY_DEFAULT);
4726         }
4727
4728         switch (cmd) {
4729         case SET_KEY:
4730                 if (is_default_wep_key)
4731                         ret = iwl_set_default_wep_key(priv, key);
4732                 else
4733                         ret = iwl_set_dynamic_key(priv, key, sta_id);
4734
4735                 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
4736                 break;
4737         case DISABLE_KEY:
4738                 if (is_default_wep_key)
4739                         ret = iwl_remove_default_wep_key(priv, key);
4740                 else
4741                         ret = iwl_remove_dynamic_key(priv, key, sta_id);
4742
4743                 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
4744                 break;
4745         default:
4746                 ret = -EINVAL;
4747         }
4748
4749         IWL_DEBUG_MAC80211("leave\n");
4750
4751         return ret;
4752 }
4753
4754 static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
4755                            const struct ieee80211_tx_queue_params *params)
4756 {
4757         struct iwl_priv *priv = hw->priv;
4758         unsigned long flags;
4759         int q;
4760
4761         IWL_DEBUG_MAC80211("enter\n");
4762
4763         if (!iwl_is_ready_rf(priv)) {
4764                 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4765                 return -EIO;
4766         }
4767
4768         if (queue >= AC_NUM) {
4769                 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
4770                 return 0;
4771         }
4772
4773         if (!priv->qos_data.qos_enable) {
4774                 priv->qos_data.qos_active = 0;
4775                 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
4776                 return 0;
4777         }
4778         q = AC_NUM - 1 - queue;
4779
4780         spin_lock_irqsave(&priv->lock, flags);
4781
4782         priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
4783         priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
4784         priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
4785         priv->qos_data.def_qos_parm.ac[q].edca_txop =
4786                         cpu_to_le16((params->txop * 32));
4787
4788         priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
4789         priv->qos_data.qos_active = 1;
4790
4791         spin_unlock_irqrestore(&priv->lock, flags);
4792
4793         mutex_lock(&priv->mutex);
4794         if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
4795                 iwl4965_activate_qos(priv, 1);
4796         else if (priv->assoc_id && iwl_is_associated(priv))
4797                 iwl4965_activate_qos(priv, 0);
4798
4799         mutex_unlock(&priv->mutex);
4800
4801         IWL_DEBUG_MAC80211("leave\n");
4802         return 0;
4803 }
4804
4805 static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
4806                                 struct ieee80211_tx_queue_stats *stats)
4807 {
4808         struct iwl_priv *priv = hw->priv;
4809         int i, avail;
4810         struct iwl_tx_queue *txq;
4811         struct iwl_queue *q;
4812         unsigned long flags;
4813
4814         IWL_DEBUG_MAC80211("enter\n");
4815
4816         if (!iwl_is_ready_rf(priv)) {
4817                 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4818                 return -EIO;
4819         }
4820
4821         spin_lock_irqsave(&priv->lock, flags);
4822
4823         for (i = 0; i < AC_NUM; i++) {
4824                 txq = &priv->txq[i];
4825                 q = &txq->q;
4826                 avail = iwl_queue_space(q);
4827
4828                 stats[i].len = q->n_window - avail;
4829                 stats[i].limit = q->n_window - q->high_mark;
4830                 stats[i].count = q->n_window;
4831
4832         }
4833         spin_unlock_irqrestore(&priv->lock, flags);
4834
4835         IWL_DEBUG_MAC80211("leave\n");
4836
4837         return 0;
4838 }
4839
4840 static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
4841                              struct ieee80211_low_level_stats *stats)
4842 {
4843         struct iwl_priv *priv = hw->priv;
4844
4845         priv = hw->priv;
4846         IWL_DEBUG_MAC80211("enter\n");
4847         IWL_DEBUG_MAC80211("leave\n");
4848
4849         return 0;
4850 }
4851
4852 static u64 iwl4965_mac_get_tsf(struct ieee80211_hw *hw)
4853 {
4854         struct iwl_priv *priv;
4855
4856         priv = hw->priv;
4857         IWL_DEBUG_MAC80211("enter\n");
4858         IWL_DEBUG_MAC80211("leave\n");
4859
4860         return 0;
4861 }
4862
4863 static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
4864 {
4865         struct iwl_priv *priv = hw->priv;
4866         unsigned long flags;
4867
4868         mutex_lock(&priv->mutex);
4869         IWL_DEBUG_MAC80211("enter\n");
4870
4871         priv->lq_mngr.lq_ready = 0;
4872 #ifdef CONFIG_IWL4965_HT
4873         spin_lock_irqsave(&priv->lock, flags);
4874         memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
4875         spin_unlock_irqrestore(&priv->lock, flags);
4876 #endif /* CONFIG_IWL4965_HT */
4877
4878         iwl_reset_qos(priv);
4879
4880         cancel_delayed_work(&priv->post_associate);
4881
4882         spin_lock_irqsave(&priv->lock, flags);
4883         priv->assoc_id = 0;
4884         priv->assoc_capability = 0;
4885         priv->assoc_station_added = 0;
4886
4887         /* new association get rid of ibss beacon skb */
4888         if (priv->ibss_beacon)
4889                 dev_kfree_skb(priv->ibss_beacon);
4890
4891         priv->ibss_beacon = NULL;
4892
4893         priv->beacon_int = priv->hw->conf.beacon_int;
4894         priv->timestamp = 0;
4895         if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
4896                 priv->beacon_int = 0;
4897
4898         spin_unlock_irqrestore(&priv->lock, flags);
4899
4900         if (!iwl_is_ready_rf(priv)) {
4901                 IWL_DEBUG_MAC80211("leave - not ready\n");
4902                 mutex_unlock(&priv->mutex);
4903                 return;
4904         }
4905
4906         /* we are restarting association process
4907          * clear RXON_FILTER_ASSOC_MSK bit
4908          */
4909         if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
4910                 iwl4965_scan_cancel_timeout(priv, 100);
4911                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4912                 iwl4965_commit_rxon(priv);
4913         }
4914
4915         iwl_power_update_mode(priv, 0);
4916
4917         /* Per mac80211.h: This is only used in IBSS mode... */
4918         if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
4919
4920                 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
4921                 mutex_unlock(&priv->mutex);
4922                 return;
4923         }
4924
4925         iwl4965_set_rate(priv);
4926
4927         mutex_unlock(&priv->mutex);
4928
4929         IWL_DEBUG_MAC80211("leave\n");
4930 }
4931
4932 static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
4933 {
4934         struct iwl_priv *priv = hw->priv;
4935         unsigned long flags;
4936
4937         mutex_lock(&priv->mutex);
4938         IWL_DEBUG_MAC80211("enter\n");
4939
4940         if (!iwl_is_ready_rf(priv)) {
4941                 IWL_DEBUG_MAC80211("leave - RF not ready\n");
4942                 mutex_unlock(&priv->mutex);
4943                 return -EIO;
4944         }
4945
4946         if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
4947                 IWL_DEBUG_MAC80211("leave - not IBSS\n");
4948                 mutex_unlock(&priv->mutex);
4949                 return -EIO;
4950         }
4951
4952         spin_lock_irqsave(&priv->lock, flags);
4953
4954         if (priv->ibss_beacon)
4955                 dev_kfree_skb(priv->ibss_beacon);
4956
4957         priv->ibss_beacon = skb;
4958
4959         priv->assoc_id = 0;
4960
4961         IWL_DEBUG_MAC80211("leave\n");
4962         spin_unlock_irqrestore(&priv->lock, flags);
4963
4964         iwl_reset_qos(priv);
4965
4966         queue_work(priv->workqueue, &priv->post_associate.work);
4967
4968         mutex_unlock(&priv->mutex);
4969
4970         return 0;
4971 }
4972
4973 /*****************************************************************************
4974  *
4975  * sysfs attributes
4976  *
4977  *****************************************************************************/
4978
4979 #ifdef CONFIG_IWLWIFI_DEBUG
4980
4981 /*
4982  * The following adds a new attribute to the sysfs representation
4983  * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
4984  * used for controlling the debug level.
4985  *
4986  * See the level definitions in iwl for details.
4987  */
4988
4989 static ssize_t show_debug_level(struct device *d,
4990                                 struct device_attribute *attr, char *buf)
4991 {
4992         struct iwl_priv *priv = d->driver_data;
4993
4994         return sprintf(buf, "0x%08X\n", priv->debug_level);
4995 }
4996 static ssize_t store_debug_level(struct device *d,
4997                                 struct device_attribute *attr,
4998                                  const char *buf, size_t count)
4999 {
5000         struct iwl_priv *priv = d->driver_data;
5001         char *p = (char *)buf;
5002         u32 val;
5003
5004         val = simple_strtoul(p, &p, 0);
5005         if (p == buf)
5006                 printk(KERN_INFO DRV_NAME
5007                        ": %s is not in hex or decimal form.\n", buf);
5008         else
5009                 priv->debug_level = val;
5010
5011         return strnlen(buf, count);
5012 }
5013
5014 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
5015                         show_debug_level, store_debug_level);
5016
5017
5018 #endif /* CONFIG_IWLWIFI_DEBUG */
5019
5020
5021 static ssize_t show_version(struct device *d,
5022                                 struct device_attribute *attr, char *buf)
5023 {
5024         struct iwl_priv *priv = d->driver_data;
5025         struct iwl_alive_resp *palive = &priv->card_alive;
5026
5027         if (palive->is_valid)
5028                 return sprintf(buf, "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
5029                                     "fw type: 0x%01X 0x%01X\n",
5030                                 palive->ucode_major, palive->ucode_minor,
5031                                 palive->sw_rev[0], palive->sw_rev[1],
5032                                 palive->ver_type, palive->ver_subtype);
5033
5034         else
5035                 return sprintf(buf, "fw not loaded\n");
5036 }
5037
5038 static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
5039
5040 static ssize_t show_temperature(struct device *d,
5041                                 struct device_attribute *attr, char *buf)
5042 {
5043         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5044
5045         if (!iwl_is_alive(priv))
5046                 return -EAGAIN;
5047
5048         return sprintf(buf, "%d\n", iwl4965_hw_get_temperature(priv));
5049 }
5050
5051 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
5052
5053 static ssize_t show_rs_window(struct device *d,
5054                               struct device_attribute *attr,
5055                               char *buf)
5056 {
5057         struct iwl_priv *priv = d->driver_data;
5058         return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
5059 }
5060 static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
5061
5062 static ssize_t show_tx_power(struct device *d,
5063                              struct device_attribute *attr, char *buf)
5064 {
5065         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5066         return sprintf(buf, "%d\n", priv->user_txpower_limit);
5067 }
5068
5069 static ssize_t store_tx_power(struct device *d,
5070                               struct device_attribute *attr,
5071                               const char *buf, size_t count)
5072 {
5073         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5074         char *p = (char *)buf;
5075         u32 val;
5076
5077         val = simple_strtoul(p, &p, 10);
5078         if (p == buf)
5079                 printk(KERN_INFO DRV_NAME
5080                        ": %s is not in decimal form.\n", buf);
5081         else
5082                 iwl4965_hw_reg_set_txpower(priv, val);
5083
5084         return count;
5085 }
5086
5087 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
5088
5089 static ssize_t show_flags(struct device *d,
5090                           struct device_attribute *attr, char *buf)
5091 {
5092         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5093
5094         return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
5095 }
5096
5097 static ssize_t store_flags(struct device *d,
5098                            struct device_attribute *attr,
5099                            const char *buf, size_t count)
5100 {
5101         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5102         u32 flags = simple_strtoul(buf, NULL, 0);
5103
5104         mutex_lock(&priv->mutex);
5105         if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
5106                 /* Cancel any currently running scans... */
5107                 if (iwl4965_scan_cancel_timeout(priv, 100))
5108                         IWL_WARNING("Could not cancel scan.\n");
5109                 else {
5110                         IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
5111                                        flags);
5112                         priv->staging_rxon.flags = cpu_to_le32(flags);
5113                         iwl4965_commit_rxon(priv);
5114                 }
5115         }
5116         mutex_unlock(&priv->mutex);
5117
5118         return count;
5119 }
5120
5121 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
5122
5123 static ssize_t show_filter_flags(struct device *d,
5124                                  struct device_attribute *attr, char *buf)
5125 {
5126         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5127
5128         return sprintf(buf, "0x%04X\n",
5129                 le32_to_cpu(priv->active_rxon.filter_flags));
5130 }
5131
5132 static ssize_t store_filter_flags(struct device *d,
5133                                   struct device_attribute *attr,
5134                                   const char *buf, size_t count)
5135 {
5136         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5137         u32 filter_flags = simple_strtoul(buf, NULL, 0);
5138
5139         mutex_lock(&priv->mutex);
5140         if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
5141                 /* Cancel any currently running scans... */
5142                 if (iwl4965_scan_cancel_timeout(priv, 100))
5143                         IWL_WARNING("Could not cancel scan.\n");
5144                 else {
5145                         IWL_DEBUG_INFO("Committing rxon.filter_flags = "
5146                                        "0x%04X\n", filter_flags);
5147                         priv->staging_rxon.filter_flags =
5148                                 cpu_to_le32(filter_flags);
5149                         iwl4965_commit_rxon(priv);
5150                 }
5151         }
5152         mutex_unlock(&priv->mutex);
5153
5154         return count;
5155 }
5156
5157 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
5158                    store_filter_flags);
5159
5160 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
5161
5162 static ssize_t show_measurement(struct device *d,
5163                                 struct device_attribute *attr, char *buf)
5164 {
5165         struct iwl_priv *priv = dev_get_drvdata(d);
5166         struct iwl4965_spectrum_notification measure_report;
5167         u32 size = sizeof(measure_report), len = 0, ofs = 0;
5168         u8 *data = (u8 *) & measure_report;
5169         unsigned long flags;
5170
5171         spin_lock_irqsave(&priv->lock, flags);
5172         if (!(priv->measurement_status & MEASUREMENT_READY)) {
5173                 spin_unlock_irqrestore(&priv->lock, flags);
5174                 return 0;
5175         }
5176         memcpy(&measure_report, &priv->measure_report, size);
5177         priv->measurement_status = 0;
5178         spin_unlock_irqrestore(&priv->lock, flags);
5179
5180         while (size && (PAGE_SIZE - len)) {
5181                 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5182                                    PAGE_SIZE - len, 1);
5183                 len = strlen(buf);
5184                 if (PAGE_SIZE - len)
5185                         buf[len++] = '\n';
5186
5187                 ofs += 16;
5188                 size -= min(size, 16U);
5189         }
5190
5191         return len;
5192 }
5193
5194 static ssize_t store_measurement(struct device *d,
5195                                  struct device_attribute *attr,
5196                                  const char *buf, size_t count)
5197 {
5198         struct iwl_priv *priv = dev_get_drvdata(d);
5199         struct ieee80211_measurement_params params = {
5200                 .channel = le16_to_cpu(priv->active_rxon.channel),
5201                 .start_time = cpu_to_le64(priv->last_tsf),
5202                 .duration = cpu_to_le16(1),
5203         };
5204         u8 type = IWL_MEASURE_BASIC;
5205         u8 buffer[32];
5206         u8 channel;
5207
5208         if (count) {
5209                 char *p = buffer;
5210                 strncpy(buffer, buf, min(sizeof(buffer), count));
5211                 channel = simple_strtoul(p, NULL, 0);
5212                 if (channel)
5213                         params.channel = channel;
5214
5215                 p = buffer;
5216                 while (*p && *p != ' ')
5217                         p++;
5218                 if (*p)
5219                         type = simple_strtoul(p + 1, NULL, 0);
5220         }
5221
5222         IWL_DEBUG_INFO("Invoking measurement of type %d on "
5223                        "channel %d (for '%s')\n", type, params.channel, buf);
5224         iwl4965_get_measurement(priv, &params, type);
5225
5226         return count;
5227 }
5228
5229 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
5230                    show_measurement, store_measurement);
5231 #endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
5232
5233 static ssize_t store_retry_rate(struct device *d,
5234                                 struct device_attribute *attr,
5235                                 const char *buf, size_t count)
5236 {
5237         struct iwl_priv *priv = dev_get_drvdata(d);
5238
5239         priv->retry_rate = simple_strtoul(buf, NULL, 0);
5240         if (priv->retry_rate <= 0)
5241                 priv->retry_rate = 1;
5242
5243         return count;
5244 }
5245
5246 static ssize_t show_retry_rate(struct device *d,
5247                                struct device_attribute *attr, char *buf)
5248 {
5249         struct iwl_priv *priv = dev_get_drvdata(d);
5250         return sprintf(buf, "%d", priv->retry_rate);
5251 }
5252
5253 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
5254                    store_retry_rate);
5255
5256 static ssize_t store_power_level(struct device *d,
5257                                  struct device_attribute *attr,
5258                                  const char *buf, size_t count)
5259 {
5260         struct iwl_priv *priv = dev_get_drvdata(d);
5261         int rc;
5262         int mode;
5263
5264         mode = simple_strtoul(buf, NULL, 0);
5265         mutex_lock(&priv->mutex);
5266
5267         if (!iwl_is_ready(priv)) {
5268                 rc = -EAGAIN;
5269                 goto out;
5270         }
5271
5272         rc = iwl_power_set_user_mode(priv, mode);
5273         if (rc) {
5274                 IWL_DEBUG_MAC80211("failed setting power mode.\n");
5275                 goto out;
5276         }
5277         rc = count;
5278
5279  out:
5280         mutex_unlock(&priv->mutex);
5281         return rc;
5282 }
5283
5284 #define MAX_WX_STRING 80
5285
5286 /* Values are in microsecond */
5287 static const s32 timeout_duration[] = {
5288         350000,
5289         250000,
5290         75000,
5291         37000,
5292         25000,
5293 };
5294 static const s32 period_duration[] = {
5295         400000,
5296         700000,
5297         1000000,
5298         1000000,
5299         1000000
5300 };
5301
5302 static ssize_t show_power_level(struct device *d,
5303                                 struct device_attribute *attr, char *buf)
5304 {
5305         struct iwl_priv *priv = dev_get_drvdata(d);
5306         int level = priv->power_data.power_mode;
5307         char *p = buf;
5308
5309         p += sprintf(p, "%d ", level);
5310         switch (level) {
5311         case IWL_POWER_MODE_CAM:
5312         case IWL_POWER_AC:
5313                 p += sprintf(p, "(AC)");
5314                 break;
5315         case IWL_POWER_BATTERY:
5316                 p += sprintf(p, "(BATTERY)");
5317                 break;
5318         default:
5319                 p += sprintf(p,
5320                              "(Timeout %dms, Period %dms)",
5321                              timeout_duration[level - 1] / 1000,
5322                              period_duration[level - 1] / 1000);
5323         }
5324 /*
5325         if (!(priv->power_mode & IWL_POWER_ENABLED))
5326                 p += sprintf(p, " OFF\n");
5327         else
5328                 p += sprintf(p, " \n");
5329 */
5330         p += sprintf(p, " \n");
5331         return (p - buf + 1);
5332 }
5333
5334 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
5335                    store_power_level);
5336
5337 static ssize_t show_channels(struct device *d,
5338                              struct device_attribute *attr, char *buf)
5339 {
5340         /* all this shit doesn't belong into sysfs anyway */
5341         return 0;
5342 }
5343
5344 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
5345
5346 static ssize_t show_statistics(struct device *d,
5347                                struct device_attribute *attr, char *buf)
5348 {
5349         struct iwl_priv *priv = dev_get_drvdata(d);
5350         u32 size = sizeof(struct iwl4965_notif_statistics);
5351         u32 len = 0, ofs = 0;
5352         u8 *data = (u8 *) & priv->statistics;
5353         int rc = 0;
5354
5355         if (!iwl_is_alive(priv))
5356                 return -EAGAIN;
5357
5358         mutex_lock(&priv->mutex);
5359         rc = iwl_send_statistics_request(priv, 0);
5360         mutex_unlock(&priv->mutex);
5361
5362         if (rc) {
5363                 len = sprintf(buf,
5364                               "Error sending statistics request: 0x%08X\n", rc);
5365                 return len;
5366         }
5367
5368         while (size && (PAGE_SIZE - len)) {
5369                 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
5370                                    PAGE_SIZE - len, 1);
5371                 len = strlen(buf);
5372                 if (PAGE_SIZE - len)
5373                         buf[len++] = '\n';
5374
5375                 ofs += 16;
5376                 size -= min(size, 16U);
5377         }
5378
5379         return len;
5380 }
5381
5382 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
5383
5384 static ssize_t show_status(struct device *d,
5385                            struct device_attribute *attr, char *buf)
5386 {
5387         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
5388         if (!iwl_is_alive(priv))
5389                 return -EAGAIN;
5390         return sprintf(buf, "0x%08x\n", (int)priv->status);
5391 }
5392
5393 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
5394
5395 /*****************************************************************************
5396  *
5397  * driver setup and teardown
5398  *
5399  *****************************************************************************/
5400
5401 static void iwl4965_setup_deferred_work(struct iwl_priv *priv)
5402 {
5403         priv->workqueue = create_workqueue(DRV_NAME);
5404
5405         init_waitqueue_head(&priv->wait_command_queue);
5406
5407         INIT_WORK(&priv->up, iwl4965_bg_up);
5408         INIT_WORK(&priv->restart, iwl4965_bg_restart);
5409         INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
5410         INIT_WORK(&priv->scan_completed, iwl4965_bg_scan_completed);
5411         INIT_WORK(&priv->request_scan, iwl4965_bg_request_scan);
5412         INIT_WORK(&priv->abort_scan, iwl4965_bg_abort_scan);
5413         INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
5414         INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
5415         INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
5416         INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
5417         INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
5418         INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
5419         INIT_DELAYED_WORK(&priv->scan_check, iwl4965_bg_scan_check);
5420
5421         iwl4965_hw_setup_deferred_work(priv);
5422
5423         tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
5424                      iwl4965_irq_tasklet, (unsigned long)priv);
5425 }
5426
5427 static void iwl4965_cancel_deferred_work(struct iwl_priv *priv)
5428 {
5429         iwl4965_hw_cancel_deferred_work(priv);
5430
5431         cancel_delayed_work_sync(&priv->init_alive_start);
5432         cancel_delayed_work(&priv->scan_check);
5433         cancel_delayed_work(&priv->alive_start);
5434         cancel_delayed_work(&priv->post_associate);
5435         cancel_work_sync(&priv->beacon_update);
5436 }
5437
5438 static struct attribute *iwl4965_sysfs_entries[] = {
5439         &dev_attr_channels.attr,
5440         &dev_attr_flags.attr,
5441         &dev_attr_filter_flags.attr,
5442 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
5443         &dev_attr_measurement.attr,
5444 #endif
5445         &dev_attr_power_level.attr,
5446         &dev_attr_retry_rate.attr,
5447         &dev_attr_rs_window.attr,
5448         &dev_attr_statistics.attr,
5449         &dev_attr_status.attr,
5450         &dev_attr_temperature.attr,
5451         &dev_attr_tx_power.attr,
5452 #ifdef CONFIG_IWLWIFI_DEBUG
5453         &dev_attr_debug_level.attr,
5454 #endif
5455         &dev_attr_version.attr,
5456
5457         NULL
5458 };
5459
5460 static struct attribute_group iwl4965_attribute_group = {
5461         .name = NULL,           /* put in device directory */
5462         .attrs = iwl4965_sysfs_entries,
5463 };
5464
5465 static struct ieee80211_ops iwl4965_hw_ops = {
5466         .tx = iwl4965_mac_tx,
5467         .start = iwl4965_mac_start,
5468         .stop = iwl4965_mac_stop,
5469         .add_interface = iwl4965_mac_add_interface,
5470         .remove_interface = iwl4965_mac_remove_interface,
5471         .config = iwl4965_mac_config,
5472         .config_interface = iwl4965_mac_config_interface,
5473         .configure_filter = iwl4965_configure_filter,
5474         .set_key = iwl4965_mac_set_key,
5475         .update_tkip_key = iwl4965_mac_update_tkip_key,
5476         .get_stats = iwl4965_mac_get_stats,
5477         .get_tx_stats = iwl4965_mac_get_tx_stats,
5478         .conf_tx = iwl4965_mac_conf_tx,
5479         .get_tsf = iwl4965_mac_get_tsf,
5480         .reset_tsf = iwl4965_mac_reset_tsf,
5481         .beacon_update = iwl4965_mac_beacon_update,
5482         .bss_info_changed = iwl4965_bss_info_changed,
5483 #ifdef CONFIG_IWL4965_HT
5484         .ampdu_action = iwl4965_mac_ampdu_action,
5485 #endif  /* CONFIG_IWL4965_HT */
5486         .hw_scan = iwl4965_mac_hw_scan
5487 };
5488
5489 static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
5490 {
5491         int err = 0;
5492         struct iwl_priv *priv;
5493         struct ieee80211_hw *hw;
5494         struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
5495         unsigned long flags;
5496         DECLARE_MAC_BUF(mac);
5497
5498         /************************
5499          * 1. Allocating HW data
5500          ************************/
5501
5502         /* Disabling hardware scan means that mac80211 will perform scans
5503          * "the hard way", rather than using device's scan. */
5504         if (cfg->mod_params->disable_hw_scan) {
5505                 if (cfg->mod_params->debug & IWL_DL_INFO)
5506                         dev_printk(KERN_DEBUG, &(pdev->dev),
5507                                    "Disabling hw_scan\n");
5508                 iwl4965_hw_ops.hw_scan = NULL;
5509         }
5510
5511         hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
5512         if (!hw) {
5513                 err = -ENOMEM;
5514                 goto out;
5515         }
5516         priv = hw->priv;
5517         /* At this point both hw and priv are allocated. */
5518
5519         SET_IEEE80211_DEV(hw, &pdev->dev);
5520
5521         IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
5522         priv->cfg = cfg;
5523         priv->pci_dev = pdev;
5524
5525 #ifdef CONFIG_IWLWIFI_DEBUG
5526         priv->debug_level = priv->cfg->mod_params->debug;
5527         atomic_set(&priv->restrict_refcnt, 0);
5528 #endif
5529
5530         /**************************
5531          * 2. Initializing PCI bus
5532          **************************/
5533         if (pci_enable_device(pdev)) {
5534                 err = -ENODEV;
5535                 goto out_ieee80211_free_hw;
5536         }
5537
5538         pci_set_master(pdev);
5539
5540         err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
5541         if (!err)
5542                 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
5543         if (err) {
5544                 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
5545                 if (!err)
5546                         err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
5547                 /* both attempts failed: */
5548                 if (err) {
5549                         printk(KERN_WARNING "%s: No suitable DMA available.\n",
5550                                 DRV_NAME);
5551                         goto out_pci_disable_device;
5552                 }
5553         }
5554
5555         err = pci_request_regions(pdev, DRV_NAME);
5556         if (err)
5557                 goto out_pci_disable_device;
5558
5559         pci_set_drvdata(pdev, priv);
5560
5561         /* We disable the RETRY_TIMEOUT register (0x41) to keep
5562          * PCI Tx retries from interfering with C3 CPU state */
5563         pci_write_config_byte(pdev, 0x41, 0x00);
5564
5565         /***********************
5566          * 3. Read REV register
5567          ***********************/
5568         priv->hw_base = pci_iomap(pdev, 0, 0);
5569         if (!priv->hw_base) {
5570                 err = -ENODEV;
5571                 goto out_pci_release_regions;
5572         }
5573
5574         IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
5575                 (unsigned long long) pci_resource_len(pdev, 0));
5576         IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
5577
5578         iwl_hw_detect(priv);
5579         printk(KERN_INFO DRV_NAME
5580                 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
5581                 priv->cfg->name, priv->hw_rev);
5582
5583         /* amp init */
5584         err = priv->cfg->ops->lib->apm_ops.init(priv);
5585         if (err < 0) {
5586                 IWL_DEBUG_INFO("Failed to init APMG\n");
5587                 goto out_iounmap;
5588         }
5589         /*****************
5590          * 4. Read EEPROM
5591          *****************/
5592         /* Read the EEPROM */
5593         err = iwl_eeprom_init(priv);
5594         if (err) {
5595                 IWL_ERROR("Unable to init EEPROM\n");
5596                 goto out_iounmap;
5597         }
5598         err = iwl_eeprom_check_version(priv);
5599         if (err)
5600                 goto out_iounmap;
5601
5602         /* extract MAC Address */
5603         iwl_eeprom_get_mac(priv, priv->mac_addr);
5604         IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
5605         SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
5606
5607         /************************
5608          * 5. Setup HW constants
5609          ************************/
5610         /* Device-specific setup */
5611         if (priv->cfg->ops->lib->set_hw_params(priv)) {
5612                 IWL_ERROR("failed to set hw parameters\n");
5613                 goto out_free_eeprom;
5614         }
5615
5616         /*******************
5617          * 6. Setup priv
5618          *******************/
5619
5620         err = iwl_init_drv(priv);
5621         if (err)
5622                 goto out_free_eeprom;
5623         /* At this point both hw and priv are initialized. */
5624
5625         /**********************************
5626          * 7. Initialize module parameters
5627          **********************************/
5628
5629         /* Disable radio (SW RF KILL) via parameter when loading driver */
5630         if (priv->cfg->mod_params->disable) {
5631                 set_bit(STATUS_RF_KILL_SW, &priv->status);
5632                 IWL_DEBUG_INFO("Radio disabled.\n");
5633         }
5634
5635         /********************
5636          * 8. Setup services
5637          ********************/
5638         spin_lock_irqsave(&priv->lock, flags);
5639         iwl4965_disable_interrupts(priv);
5640         spin_unlock_irqrestore(&priv->lock, flags);
5641
5642         err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5643         if (err) {
5644                 IWL_ERROR("failed to create sysfs device attributes\n");
5645                 goto out_uninit_drv;
5646         }
5647
5648
5649         iwl4965_setup_deferred_work(priv);
5650         iwl4965_setup_rx_handlers(priv);
5651
5652         /********************
5653          * 9. Conclude
5654          ********************/
5655         pci_save_state(pdev);
5656         pci_disable_device(pdev);
5657
5658         /**********************************
5659          * 10. Setup and register mac80211
5660          **********************************/
5661
5662         err = iwl_setup_mac(priv);
5663         if (err)
5664                 goto out_remove_sysfs;
5665
5666         err = iwl_dbgfs_register(priv, DRV_NAME);
5667         if (err)
5668                 IWL_ERROR("failed to create debugfs files\n");
5669
5670         /* notify iwlcore to init */
5671         iwlcore_low_level_notify(priv, IWLCORE_INIT_EVT);
5672         return 0;
5673
5674  out_remove_sysfs:
5675         sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5676  out_uninit_drv:
5677         iwl_uninit_drv(priv);
5678  out_free_eeprom:
5679         iwl_eeprom_free(priv);
5680  out_iounmap:
5681         pci_iounmap(pdev, priv->hw_base);
5682  out_pci_release_regions:
5683         pci_release_regions(pdev);
5684         pci_set_drvdata(pdev, NULL);
5685  out_pci_disable_device:
5686         pci_disable_device(pdev);
5687  out_ieee80211_free_hw:
5688         ieee80211_free_hw(priv->hw);
5689  out:
5690         return err;
5691 }
5692
5693 static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
5694 {
5695         struct iwl_priv *priv = pci_get_drvdata(pdev);
5696         struct list_head *p, *q;
5697         int i;
5698         unsigned long flags;
5699
5700         if (!priv)
5701                 return;
5702
5703         IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
5704
5705         if (priv->mac80211_registered) {
5706                 ieee80211_unregister_hw(priv->hw);
5707                 priv->mac80211_registered = 0;
5708         }
5709
5710         set_bit(STATUS_EXIT_PENDING, &priv->status);
5711
5712         iwl4965_down(priv);
5713
5714         /* make sure we flush any pending irq or
5715          * tasklet for the driver
5716          */
5717         spin_lock_irqsave(&priv->lock, flags);
5718         iwl4965_disable_interrupts(priv);
5719         spin_unlock_irqrestore(&priv->lock, flags);
5720
5721         iwl_synchronize_irq(priv);
5722
5723         /* Free MAC hash list for ADHOC */
5724         for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
5725                 list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
5726                         list_del(p);
5727                         kfree(list_entry(p, struct iwl4965_ibss_seq, list));
5728                 }
5729         }
5730
5731         iwlcore_low_level_notify(priv, IWLCORE_REMOVE_EVT);
5732         iwl_dbgfs_unregister(priv);
5733         sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
5734
5735         iwl4965_dealloc_ucode_pci(priv);
5736
5737         if (priv->rxq.bd)
5738                 iwl_rx_queue_free(priv, &priv->rxq);
5739         iwl_hw_txq_ctx_free(priv);
5740
5741         iwlcore_clear_stations_table(priv);
5742         iwl_eeprom_free(priv);
5743
5744
5745         /*netif_stop_queue(dev); */
5746         flush_workqueue(priv->workqueue);
5747
5748         /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
5749          * priv->workqueue... so we can't take down the workqueue
5750          * until now... */
5751         destroy_workqueue(priv->workqueue);
5752         priv->workqueue = NULL;
5753
5754         pci_iounmap(pdev, priv->hw_base);
5755         pci_release_regions(pdev);
5756         pci_disable_device(pdev);
5757         pci_set_drvdata(pdev, NULL);
5758
5759         iwl_uninit_drv(priv);
5760
5761         if (priv->ibss_beacon)
5762                 dev_kfree_skb(priv->ibss_beacon);
5763
5764         ieee80211_free_hw(priv->hw);
5765 }
5766
5767 #ifdef CONFIG_PM
5768
5769 static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
5770 {
5771         struct iwl_priv *priv = pci_get_drvdata(pdev);
5772
5773         if (priv->is_open) {
5774                 set_bit(STATUS_IN_SUSPEND, &priv->status);
5775                 iwl4965_mac_stop(priv->hw);
5776                 priv->is_open = 1;
5777         }
5778
5779         pci_set_power_state(pdev, PCI_D3hot);
5780
5781         return 0;
5782 }
5783
5784 static int iwl4965_pci_resume(struct pci_dev *pdev)
5785 {
5786         struct iwl_priv *priv = pci_get_drvdata(pdev);
5787
5788         pci_set_power_state(pdev, PCI_D0);
5789
5790         if (priv->is_open)
5791                 iwl4965_mac_start(priv->hw);
5792
5793         clear_bit(STATUS_IN_SUSPEND, &priv->status);
5794         return 0;
5795 }
5796
5797 #endif /* CONFIG_PM */
5798
5799 /*****************************************************************************
5800  *
5801  * driver and module entry point
5802  *
5803  *****************************************************************************/
5804
5805 /* Hardware specific file defines the PCI IDs table for that hardware module */
5806 static struct pci_device_id iwl_hw_card_ids[] = {
5807         {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
5808         {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
5809 #ifdef CONFIG_IWL5000
5810         {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
5811         {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
5812         {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
5813 #endif /* CONFIG_IWL5000 */
5814         {0}
5815 };
5816 MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
5817
5818 static struct pci_driver iwl_driver = {
5819         .name = DRV_NAME,
5820         .id_table = iwl_hw_card_ids,
5821         .probe = iwl4965_pci_probe,
5822         .remove = __devexit_p(iwl4965_pci_remove),
5823 #ifdef CONFIG_PM
5824         .suspend = iwl4965_pci_suspend,
5825         .resume = iwl4965_pci_resume,
5826 #endif
5827 };
5828
5829 static int __init iwl4965_init(void)
5830 {
5831
5832         int ret;
5833         printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
5834         printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
5835
5836         ret = iwl4965_rate_control_register();
5837         if (ret) {
5838                 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
5839                 return ret;
5840         }
5841
5842         ret = pci_register_driver(&iwl_driver);
5843         if (ret) {
5844                 IWL_ERROR("Unable to initialize PCI module\n");
5845                 goto error_register;
5846         }
5847
5848         return ret;
5849
5850 error_register:
5851         iwl4965_rate_control_unregister();
5852         return ret;
5853 }
5854
5855 static void __exit iwl4965_exit(void)
5856 {
5857         pci_unregister_driver(&iwl_driver);
5858         iwl4965_rate_control_unregister();
5859 }
5860
5861 module_exit(iwl4965_exit);
5862 module_init(iwl4965_init);