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