]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/net/wireless/iwlwifi/iwl4965-base.c
iwlwifi: fix incorrect monitor mode operation
[mv-sheeva.git] / 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 /*************** STATION TABLE MANAGEMENT ****
91  * mac80211 should be examined to determine if sta_info is duplicating
92  * the functionality provided here
93  */
94
95 /**************************************************************/
96
97
98
99 static void iwl4965_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
100 {
101         struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
102
103         if (hw_decrypt)
104                 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
105         else
106                 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
107
108 }
109
110 /**
111  * iwl4965_check_rxon_cmd - validate RXON structure is valid
112  *
113  * NOTE:  This is really only useful during development and can eventually
114  * be #ifdef'd out once the driver is stable and folks aren't actively
115  * making changes
116  */
117 static int iwl4965_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
118 {
119         int error = 0;
120         int counter = 1;
121
122         if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
123                 error |= le32_to_cpu(rxon->flags &
124                                 (RXON_FLG_TGJ_NARROW_BAND_MSK |
125                                  RXON_FLG_RADAR_DETECT_MSK));
126                 if (error)
127                         IWL_WARNING("check 24G fields %d | %d\n",
128                                     counter++, error);
129         } else {
130                 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
131                                 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
132                 if (error)
133                         IWL_WARNING("check 52 fields %d | %d\n",
134                                     counter++, error);
135                 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
136                 if (error)
137                         IWL_WARNING("check 52 CCK %d | %d\n",
138                                     counter++, error);
139         }
140         error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
141         if (error)
142                 IWL_WARNING("check mac addr %d | %d\n", counter++, error);
143
144         /* make sure basic rates 6Mbps and 1Mbps are supported */
145         error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
146                   ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
147         if (error)
148                 IWL_WARNING("check basic rate %d | %d\n", counter++, error);
149
150         error |= (le16_to_cpu(rxon->assoc_id) > 2007);
151         if (error)
152                 IWL_WARNING("check assoc id %d | %d\n", counter++, error);
153
154         error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
155                         == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
156         if (error)
157                 IWL_WARNING("check CCK and short slot %d | %d\n",
158                             counter++, error);
159
160         error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
161                         == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
162         if (error)
163                 IWL_WARNING("check CCK & auto detect %d | %d\n",
164                             counter++, error);
165
166         error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
167                         RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
168         if (error)
169                 IWL_WARNING("check TGG and auto detect %d | %d\n",
170                             counter++, error);
171
172         if (error)
173                 IWL_WARNING("Tuning to channel %d\n",
174                             le16_to_cpu(rxon->channel));
175
176         if (error) {
177                 IWL_ERROR("Not a valid iwl4965_rxon_assoc_cmd field values\n");
178                 return -1;
179         }
180         return 0;
181 }
182
183 /**
184  * iwl4965_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
185  * @priv: staging_rxon is compared to active_rxon
186  *
187  * If the RXON structure is changing enough to require a new tune,
188  * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
189  * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
190  */
191 static int iwl4965_full_rxon_required(struct iwl_priv *priv)
192 {
193
194         /* These items are only settable from the full RXON command */
195         if (!(iwl_is_associated(priv)) ||
196             compare_ether_addr(priv->staging_rxon.bssid_addr,
197                                priv->active_rxon.bssid_addr) ||
198             compare_ether_addr(priv->staging_rxon.node_addr,
199                                priv->active_rxon.node_addr) ||
200             compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
201                                priv->active_rxon.wlap_bssid_addr) ||
202             (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
203             (priv->staging_rxon.channel != priv->active_rxon.channel) ||
204             (priv->staging_rxon.air_propagation !=
205              priv->active_rxon.air_propagation) ||
206             (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
207              priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
208             (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
209              priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
210             (priv->staging_rxon.rx_chain != priv->active_rxon.rx_chain) ||
211             (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
212                 return 1;
213
214         /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
215          * be updated with the RXON_ASSOC command -- however only some
216          * flag transitions are allowed using RXON_ASSOC */
217
218         /* Check if we are not switching bands */
219         if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
220             (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
221                 return 1;
222
223         /* Check if we are switching association toggle */
224         if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
225                 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
226                 return 1;
227
228         return 0;
229 }
230
231 /**
232  * iwl4965_commit_rxon - commit staging_rxon to hardware
233  *
234  * The RXON command in staging_rxon is committed to the hardware and
235  * the active_rxon structure is updated with the new data.  This
236  * function correctly transitions out of the RXON_ASSOC_MSK state if
237  * a HW tune is required based on the RXON structure changes.
238  */
239 static int iwl4965_commit_rxon(struct iwl_priv *priv)
240 {
241         /* cast away the const for active_rxon in this function */
242         struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
243         DECLARE_MAC_BUF(mac);
244         int ret;
245         bool new_assoc =
246                 !!(priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK);
247
248         if (!iwl_is_alive(priv))
249                 return -EBUSY;
250
251         /* always get timestamp with Rx frame */
252         priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
253
254         ret = iwl4965_check_rxon_cmd(&priv->staging_rxon);
255         if (ret) {
256                 IWL_ERROR("Invalid RXON configuration.  Not committing.\n");
257                 return -EINVAL;
258         }
259
260         /* If we don't need to send a full RXON, we can use
261          * iwl4965_rxon_assoc_cmd which is used to reconfigure filter
262          * and other flags for the current radio configuration. */
263         if (!iwl4965_full_rxon_required(priv)) {
264                 ret = iwl_send_rxon_assoc(priv);
265                 if (ret) {
266                         IWL_ERROR("Error setting RXON_ASSOC (%d)\n", ret);
267                         return ret;
268                 }
269
270                 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
271                 return 0;
272         }
273
274         /* station table will be cleared */
275         priv->assoc_station_added = 0;
276
277         /* If we are currently associated and the new config requires
278          * an RXON_ASSOC and the new config wants the associated mask enabled,
279          * we must clear the associated from the active configuration
280          * before we apply the new config */
281         if (iwl_is_associated(priv) && new_assoc) {
282                 IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
283                 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
284
285                 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
286                                       sizeof(struct iwl_rxon_cmd),
287                                       &priv->active_rxon);
288
289                 /* If the mask clearing failed then we set
290                  * active_rxon back to what it was previously */
291                 if (ret) {
292                         active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
293                         IWL_ERROR("Error clearing ASSOC_MSK (%d)\n", ret);
294                         return ret;
295                 }
296         }
297
298         IWL_DEBUG_INFO("Sending RXON\n"
299                        "* with%s RXON_FILTER_ASSOC_MSK\n"
300                        "* channel = %d\n"
301                        "* bssid = %s\n",
302                        (new_assoc ? "" : "out"),
303                        le16_to_cpu(priv->staging_rxon.channel),
304                        print_mac(mac, priv->staging_rxon.bssid_addr));
305
306         iwl4965_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
307
308         /* Apply the new configuration
309          * RXON unassoc clears the station table in uCode, send it before
310          * we add the bcast station. If assoc bit is set, we will send RXON
311          * after having added the bcast and bssid station.
312          */
313         if (!new_assoc) {
314                 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
315                               sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
316                 if (ret) {
317                         IWL_ERROR("Error setting new RXON (%d)\n", ret);
318                         return ret;
319                 }
320                 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
321         }
322
323         iwl_clear_stations_table(priv);
324
325         if (!priv->error_recovering)
326                 priv->start_calib = 0;
327
328         iwl_init_sensitivity(priv);
329
330         /* If we issue a new RXON command which required a tune then we must
331          * send a new TXPOWER command or we won't be able to Tx any frames */
332         ret = iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
333         if (ret) {
334                 IWL_ERROR("Error sending TX power (%d)\n", ret);
335                 return ret;
336         }
337
338         /* Add the broadcast address so we can send broadcast frames */
339         if (iwl_rxon_add_station(priv, iwl_bcast_addr, 0) ==
340                                                 IWL_INVALID_STATION) {
341                 IWL_ERROR("Error adding BROADCAST address for transmit.\n");
342                 return -EIO;
343         }
344
345         /* If we have set the ASSOC_MSK and we are in BSS mode then
346          * add the IWL_AP_ID to the station rate table */
347         if (new_assoc) {
348                 if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
349                         ret = iwl_rxon_add_station(priv,
350                                            priv->active_rxon.bssid_addr, 1);
351                         if (ret == IWL_INVALID_STATION) {
352                                 IWL_ERROR("Error adding AP address for TX.\n");
353                                 return -EIO;
354                         }
355                         priv->assoc_station_added = 1;
356                         if (priv->default_wep_key &&
357                             iwl_send_static_wepkey_cmd(priv, 0))
358                                 IWL_ERROR("Could not send WEP static key.\n");
359                 }
360
361                 /* Apply the new configuration
362                  * RXON assoc doesn't clear the station table in uCode,
363                  */
364                 ret = iwl_send_cmd_pdu(priv, REPLY_RXON,
365                               sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
366                 if (ret) {
367                         IWL_ERROR("Error setting new RXON (%d)\n", ret);
368                         return ret;
369                 }
370                 memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
371         }
372
373         return 0;
374 }
375
376 void iwl4965_update_chain_flags(struct iwl_priv *priv)
377 {
378
379         iwl_set_rxon_chain(priv);
380         iwl4965_commit_rxon(priv);
381 }
382
383 static int iwl4965_send_bt_config(struct iwl_priv *priv)
384 {
385         struct iwl4965_bt_cmd bt_cmd = {
386                 .flags = 3,
387                 .lead_time = 0xAA,
388                 .max_kill = 1,
389                 .kill_ack_mask = 0,
390                 .kill_cts_mask = 0,
391         };
392
393         return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
394                                 sizeof(struct iwl4965_bt_cmd), &bt_cmd);
395 }
396
397 static void iwl_clear_free_frames(struct iwl_priv *priv)
398 {
399         struct list_head *element;
400
401         IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
402                        priv->frames_count);
403
404         while (!list_empty(&priv->free_frames)) {
405                 element = priv->free_frames.next;
406                 list_del(element);
407                 kfree(list_entry(element, struct iwl_frame, list));
408                 priv->frames_count--;
409         }
410
411         if (priv->frames_count) {
412                 IWL_WARNING("%d frames still in use.  Did we lose one?\n",
413                             priv->frames_count);
414                 priv->frames_count = 0;
415         }
416 }
417
418 static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
419 {
420         struct iwl_frame *frame;
421         struct list_head *element;
422         if (list_empty(&priv->free_frames)) {
423                 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
424                 if (!frame) {
425                         IWL_ERROR("Could not allocate frame!\n");
426                         return NULL;
427                 }
428
429                 priv->frames_count++;
430                 return frame;
431         }
432
433         element = priv->free_frames.next;
434         list_del(element);
435         return list_entry(element, struct iwl_frame, list);
436 }
437
438 static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
439 {
440         memset(frame, 0, sizeof(*frame));
441         list_add(&frame->list, &priv->free_frames);
442 }
443
444 unsigned int iwl4965_fill_beacon_frame(struct iwl_priv *priv,
445                                 struct ieee80211_hdr *hdr,
446                                 const u8 *dest, int left)
447 {
448
449         if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
450             ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
451              (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
452                 return 0;
453
454         if (priv->ibss_beacon->len > left)
455                 return 0;
456
457         memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
458
459         return priv->ibss_beacon->len;
460 }
461
462 static u8 iwl4965_rate_get_lowest_plcp(struct iwl_priv *priv)
463 {
464         int i;
465         int rate_mask;
466
467         /* Set rate mask*/
468         if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
469                 rate_mask = priv->active_rate_basic & 0xF;
470         else
471                 rate_mask = priv->active_rate_basic & 0xFF0;
472
473         /* Find lowest valid rate */
474         for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
475                                         i = iwl_rates[i].next_ieee) {
476                 if (rate_mask & (1 << i))
477                         return iwl_rates[i].plcp;
478         }
479
480         /* No valid rate was found. Assign the lowest one */
481         if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
482                 return IWL_RATE_1M_PLCP;
483         else
484                 return IWL_RATE_6M_PLCP;
485 }
486
487 static int iwl4965_send_beacon_cmd(struct iwl_priv *priv)
488 {
489         struct iwl_frame *frame;
490         unsigned int frame_size;
491         int rc;
492         u8 rate;
493
494         frame = iwl_get_free_frame(priv);
495
496         if (!frame) {
497                 IWL_ERROR("Could not obtain free frame buffer for beacon "
498                           "command.\n");
499                 return -ENOMEM;
500         }
501
502         rate = iwl4965_rate_get_lowest_plcp(priv);
503
504         frame_size = iwl4965_hw_get_beacon_cmd(priv, frame, rate);
505
506         rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
507                               &frame->u.cmd[0]);
508
509         iwl_free_frame(priv, frame);
510
511         return rc;
512 }
513
514 /******************************************************************************
515  *
516  * Misc. internal state and helper functions
517  *
518  ******************************************************************************/
519
520 static void iwl4965_ht_conf(struct iwl_priv *priv,
521                             struct ieee80211_bss_conf *bss_conf)
522 {
523         struct ieee80211_ht_info *ht_conf = bss_conf->ht_conf;
524         struct ieee80211_ht_bss_info *ht_bss_conf = bss_conf->ht_bss_conf;
525         struct iwl_ht_info *iwl_conf = &priv->current_ht_config;
526
527         IWL_DEBUG_MAC80211("enter: \n");
528
529         iwl_conf->is_ht = bss_conf->assoc_ht;
530
531         if (!iwl_conf->is_ht)
532                 return;
533
534         priv->ps_mode = (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
535
536         if (ht_conf->cap & IEEE80211_HT_CAP_SGI_20)
537                 iwl_conf->sgf |= HT_SHORT_GI_20MHZ;
538         if (ht_conf->cap & IEEE80211_HT_CAP_SGI_40)
539                 iwl_conf->sgf |= HT_SHORT_GI_40MHZ;
540
541         iwl_conf->is_green_field = !!(ht_conf->cap & IEEE80211_HT_CAP_GRN_FLD);
542         iwl_conf->max_amsdu_size =
543                 !!(ht_conf->cap & IEEE80211_HT_CAP_MAX_AMSDU);
544
545         iwl_conf->supported_chan_width =
546                 !!(ht_conf->cap & IEEE80211_HT_CAP_SUP_WIDTH);
547         iwl_conf->extension_chan_offset =
548                 ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_SEC_OFFSET;
549         /* If no above or below channel supplied disable FAT channel */
550         if (iwl_conf->extension_chan_offset != IEEE80211_HT_IE_CHA_SEC_ABOVE &&
551             iwl_conf->extension_chan_offset != IEEE80211_HT_IE_CHA_SEC_BELOW) {
552                 iwl_conf->extension_chan_offset = IEEE80211_HT_IE_CHA_SEC_NONE;
553                 iwl_conf->supported_chan_width = 0;
554         }
555
556         iwl_conf->tx_mimo_ps_mode =
557                 (u8)((ht_conf->cap & IEEE80211_HT_CAP_MIMO_PS) >> 2);
558         memcpy(iwl_conf->supp_mcs_set, ht_conf->supp_mcs_set, 16);
559
560         iwl_conf->control_channel = ht_bss_conf->primary_channel;
561         iwl_conf->tx_chan_width =
562                 !!(ht_bss_conf->bss_cap & IEEE80211_HT_IE_CHA_WIDTH);
563         iwl_conf->ht_protection =
564                 ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_HT_PROTECTION;
565         iwl_conf->non_GF_STA_present =
566                 !!(ht_bss_conf->bss_op_mode & IEEE80211_HT_IE_NON_GF_STA_PRSNT);
567
568         IWL_DEBUG_MAC80211("control channel %d\n", iwl_conf->control_channel);
569         IWL_DEBUG_MAC80211("leave\n");
570 }
571
572 /*
573  * QoS  support
574 */
575 static int iwl4965_send_qos_params_command(struct iwl_priv *priv,
576                                        struct iwl4965_qosparam_cmd *qos)
577 {
578
579         return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
580                                 sizeof(struct iwl4965_qosparam_cmd), qos);
581 }
582
583 static void iwl4965_activate_qos(struct iwl_priv *priv, u8 force)
584 {
585         unsigned long flags;
586
587         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
588                 return;
589
590         if (!priv->qos_data.qos_enable)
591                 return;
592
593         spin_lock_irqsave(&priv->lock, flags);
594         priv->qos_data.def_qos_parm.qos_flags = 0;
595
596         if (priv->qos_data.qos_cap.q_AP.queue_request &&
597             !priv->qos_data.qos_cap.q_AP.txop_request)
598                 priv->qos_data.def_qos_parm.qos_flags |=
599                         QOS_PARAM_FLG_TXOP_TYPE_MSK;
600         if (priv->qos_data.qos_active)
601                 priv->qos_data.def_qos_parm.qos_flags |=
602                         QOS_PARAM_FLG_UPDATE_EDCA_MSK;
603
604         if (priv->current_ht_config.is_ht)
605                 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
606
607         spin_unlock_irqrestore(&priv->lock, flags);
608
609         if (force || iwl_is_associated(priv)) {
610                 IWL_DEBUG_QOS("send QoS cmd with Qos active=%d FLAGS=0x%X\n",
611                                 priv->qos_data.qos_active,
612                                 priv->qos_data.def_qos_parm.qos_flags);
613
614                 iwl4965_send_qos_params_command(priv,
615                                 &(priv->qos_data.def_qos_parm));
616         }
617 }
618
619 #define MAX_UCODE_BEACON_INTERVAL       4096
620 #define INTEL_CONN_LISTEN_INTERVAL      __constant_cpu_to_le16(0xA)
621
622 static __le16 iwl4965_adjust_beacon_interval(u16 beacon_val)
623 {
624         u16 new_val = 0;
625         u16 beacon_factor = 0;
626
627         beacon_factor =
628             (beacon_val + MAX_UCODE_BEACON_INTERVAL)
629                 / MAX_UCODE_BEACON_INTERVAL;
630         new_val = beacon_val / beacon_factor;
631
632         return cpu_to_le16(new_val);
633 }
634
635 static void iwl4965_setup_rxon_timing(struct iwl_priv *priv)
636 {
637         u64 interval_tm_unit;
638         u64 tsf, result;
639         unsigned long flags;
640         struct ieee80211_conf *conf = NULL;
641         u16 beacon_int = 0;
642
643         conf = ieee80211_get_hw_conf(priv->hw);
644
645         spin_lock_irqsave(&priv->lock, flags);
646         priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp >> 32);
647         priv->rxon_timing.timestamp.dw[0] =
648                                 cpu_to_le32(priv->timestamp & 0xFFFFFFFF);
649
650         priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
651
652         tsf = priv->timestamp;
653
654         beacon_int = priv->beacon_int;
655         spin_unlock_irqrestore(&priv->lock, flags);
656
657         if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
658                 if (beacon_int == 0) {
659                         priv->rxon_timing.beacon_interval = cpu_to_le16(100);
660                         priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
661                 } else {
662                         priv->rxon_timing.beacon_interval =
663                                 cpu_to_le16(beacon_int);
664                         priv->rxon_timing.beacon_interval =
665                             iwl4965_adjust_beacon_interval(
666                                 le16_to_cpu(priv->rxon_timing.beacon_interval));
667                 }
668
669                 priv->rxon_timing.atim_window = 0;
670         } else {
671                 priv->rxon_timing.beacon_interval =
672                         iwl4965_adjust_beacon_interval(conf->beacon_int);
673                 /* TODO: we need to get atim_window from upper stack
674                  * for now we set to 0 */
675                 priv->rxon_timing.atim_window = 0;
676         }
677
678         interval_tm_unit =
679                 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
680         result = do_div(tsf, interval_tm_unit);
681         priv->rxon_timing.beacon_init_val =
682             cpu_to_le32((u32) ((u64) interval_tm_unit - result));
683
684         IWL_DEBUG_ASSOC
685             ("beacon interval %d beacon timer %d beacon tim %d\n",
686                 le16_to_cpu(priv->rxon_timing.beacon_interval),
687                 le32_to_cpu(priv->rxon_timing.beacon_init_val),
688                 le16_to_cpu(priv->rxon_timing.atim_window));
689 }
690
691 static void iwl_set_flags_for_band(struct iwl_priv *priv,
692                                    enum ieee80211_band band)
693 {
694         if (band == IEEE80211_BAND_5GHZ) {
695                 priv->staging_rxon.flags &=
696                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
697                       | RXON_FLG_CCK_MSK);
698                 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
699         } else {
700                 /* Copied from iwl4965_post_associate() */
701                 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
702                         priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
703                 else
704                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
705
706                 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
707                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
708
709                 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
710                 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
711                 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
712         }
713 }
714
715 /*
716  * initialize rxon structure with default values from eeprom
717  */
718 static void iwl4965_connection_init_rx_config(struct iwl_priv *priv)
719 {
720         const struct iwl_channel_info *ch_info;
721
722         memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
723
724         switch (priv->iw_mode) {
725         case IEEE80211_IF_TYPE_AP:
726                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
727                 break;
728
729         case IEEE80211_IF_TYPE_STA:
730                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
731                 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
732                 break;
733
734         case IEEE80211_IF_TYPE_IBSS:
735                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
736                 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
737                 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
738                                                   RXON_FILTER_ACCEPT_GRP_MSK;
739                 break;
740
741         case IEEE80211_IF_TYPE_MNTR:
742                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
743                 priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
744                     RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
745                 break;
746         default:
747                 IWL_ERROR("Unsupported interface type %d\n", priv->iw_mode);
748                 break;
749         }
750
751 #if 0
752         /* TODO:  Figure out when short_preamble would be set and cache from
753          * that */
754         if (!hw_to_local(priv->hw)->short_preamble)
755                 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
756         else
757                 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
758 #endif
759
760         ch_info = iwl_get_channel_info(priv, priv->band,
761                                        le16_to_cpu(priv->active_rxon.channel));
762
763         if (!ch_info)
764                 ch_info = &priv->channel_info[0];
765
766         /*
767          * in some case A channels are all non IBSS
768          * in this case force B/G channel
769          */
770         if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
771             !(is_channel_ibss(ch_info)))
772                 ch_info = &priv->channel_info[0];
773
774         priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
775         priv->band = ch_info->band;
776
777         iwl_set_flags_for_band(priv, priv->band);
778
779         priv->staging_rxon.ofdm_basic_rates =
780             (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
781         priv->staging_rxon.cck_basic_rates =
782             (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
783
784         priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED_MSK |
785                                         RXON_FLG_CHANNEL_MODE_PURE_40_MSK);
786         memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
787         memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
788         priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
789         priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
790         iwl_set_rxon_chain(priv);
791 }
792
793 static int iwl4965_set_mode(struct iwl_priv *priv, int mode)
794 {
795         priv->iw_mode = mode;
796
797         iwl4965_connection_init_rx_config(priv);
798         memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
799
800         iwl_clear_stations_table(priv);
801
802         /* dont commit rxon if rf-kill is on*/
803         if (!iwl_is_ready_rf(priv))
804                 return -EAGAIN;
805
806         cancel_delayed_work(&priv->scan_check);
807         if (iwl_scan_cancel_timeout(priv, 100)) {
808                 IWL_WARNING("Aborted scan still in progress after 100ms\n");
809                 IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
810                 return -EAGAIN;
811         }
812
813         iwl4965_commit_rxon(priv);
814
815         return 0;
816 }
817
818 static void iwl4965_set_rate(struct iwl_priv *priv)
819 {
820         const struct ieee80211_supported_band *hw = NULL;
821         struct ieee80211_rate *rate;
822         int i;
823
824         hw = iwl_get_hw_mode(priv, priv->band);
825         if (!hw) {
826                 IWL_ERROR("Failed to set rate: unable to get hw mode\n");
827                 return;
828         }
829
830         priv->active_rate = 0;
831         priv->active_rate_basic = 0;
832
833         for (i = 0; i < hw->n_bitrates; i++) {
834                 rate = &(hw->bitrates[i]);
835                 if (rate->hw_value < IWL_RATE_COUNT)
836                         priv->active_rate |= (1 << rate->hw_value);
837         }
838
839         IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
840                        priv->active_rate, priv->active_rate_basic);
841
842         /*
843          * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
844          * otherwise set it to the default of all CCK rates and 6, 12, 24 for
845          * OFDM
846          */
847         if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
848                 priv->staging_rxon.cck_basic_rates =
849                     ((priv->active_rate_basic &
850                       IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
851         else
852                 priv->staging_rxon.cck_basic_rates =
853                     (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
854
855         if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
856                 priv->staging_rxon.ofdm_basic_rates =
857                     ((priv->active_rate_basic &
858                       (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
859                       IWL_FIRST_OFDM_RATE) & 0xFF;
860         else
861                 priv->staging_rxon.ofdm_basic_rates =
862                    (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
863 }
864
865 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
866
867 #include "iwl-spectrum.h"
868
869 #define BEACON_TIME_MASK_LOW    0x00FFFFFF
870 #define BEACON_TIME_MASK_HIGH   0xFF000000
871 #define TIME_UNIT               1024
872
873 /*
874  * extended beacon time format
875  * time in usec will be changed into a 32-bit value in 8:24 format
876  * the high 1 byte is the beacon counts
877  * the lower 3 bytes is the time in usec within one beacon interval
878  */
879
880 static u32 iwl4965_usecs_to_beacons(u32 usec, u32 beacon_interval)
881 {
882         u32 quot;
883         u32 rem;
884         u32 interval = beacon_interval * 1024;
885
886         if (!interval || !usec)
887                 return 0;
888
889         quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
890         rem = (usec % interval) & BEACON_TIME_MASK_LOW;
891
892         return (quot << 24) + rem;
893 }
894
895 /* base is usually what we get from ucode with each received frame,
896  * the same as HW timer counter counting down
897  */
898
899 static __le32 iwl4965_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
900 {
901         u32 base_low = base & BEACON_TIME_MASK_LOW;
902         u32 addon_low = addon & BEACON_TIME_MASK_LOW;
903         u32 interval = beacon_interval * TIME_UNIT;
904         u32 res = (base & BEACON_TIME_MASK_HIGH) +
905             (addon & BEACON_TIME_MASK_HIGH);
906
907         if (base_low > addon_low)
908                 res += base_low - addon_low;
909         else if (base_low < addon_low) {
910                 res += interval + base_low - addon_low;
911                 res += (1 << 24);
912         } else
913                 res += (1 << 24);
914
915         return cpu_to_le32(res);
916 }
917
918 static int iwl4965_get_measurement(struct iwl_priv *priv,
919                                struct ieee80211_measurement_params *params,
920                                u8 type)
921 {
922         struct iwl4965_spectrum_cmd spectrum;
923         struct iwl_rx_packet *res;
924         struct iwl_host_cmd cmd = {
925                 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
926                 .data = (void *)&spectrum,
927                 .meta.flags = CMD_WANT_SKB,
928         };
929         u32 add_time = le64_to_cpu(params->start_time);
930         int rc;
931         int spectrum_resp_status;
932         int duration = le16_to_cpu(params->duration);
933
934         if (iwl_is_associated(priv))
935                 add_time =
936                     iwl4965_usecs_to_beacons(
937                         le64_to_cpu(params->start_time) - priv->last_tsf,
938                         le16_to_cpu(priv->rxon_timing.beacon_interval));
939
940         memset(&spectrum, 0, sizeof(spectrum));
941
942         spectrum.channel_count = cpu_to_le16(1);
943         spectrum.flags =
944             RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
945         spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
946         cmd.len = sizeof(spectrum);
947         spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
948
949         if (iwl_is_associated(priv))
950                 spectrum.start_time =
951                     iwl4965_add_beacon_time(priv->last_beacon_time,
952                                 add_time,
953                                 le16_to_cpu(priv->rxon_timing.beacon_interval));
954         else
955                 spectrum.start_time = 0;
956
957         spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
958         spectrum.channels[0].channel = params->channel;
959         spectrum.channels[0].type = type;
960         if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
961                 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
962                     RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
963
964         rc = iwl_send_cmd_sync(priv, &cmd);
965         if (rc)
966                 return rc;
967
968         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
969         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
970                 IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
971                 rc = -EIO;
972         }
973
974         spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
975         switch (spectrum_resp_status) {
976         case 0:         /* Command will be handled */
977                 if (res->u.spectrum.id != 0xff) {
978                         IWL_DEBUG_INFO
979                             ("Replaced existing measurement: %d\n",
980                              res->u.spectrum.id);
981                         priv->measurement_status &= ~MEASUREMENT_READY;
982                 }
983                 priv->measurement_status |= MEASUREMENT_ACTIVE;
984                 rc = 0;
985                 break;
986
987         case 1:         /* Command will not be handled */
988                 rc = -EAGAIN;
989                 break;
990         }
991
992         dev_kfree_skb_any(cmd.meta.u.skb);
993
994         return rc;
995 }
996 #endif
997
998 /******************************************************************************
999  *
1000  * Generic RX handler implementations
1001  *
1002  ******************************************************************************/
1003 static void iwl_rx_reply_alive(struct iwl_priv *priv,
1004                                 struct iwl_rx_mem_buffer *rxb)
1005 {
1006         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1007         struct iwl_alive_resp *palive;
1008         struct delayed_work *pwork;
1009
1010         palive = &pkt->u.alive_frame;
1011
1012         IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
1013                        "0x%01X 0x%01X\n",
1014                        palive->is_valid, palive->ver_type,
1015                        palive->ver_subtype);
1016
1017         if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1018                 IWL_DEBUG_INFO("Initialization Alive received.\n");
1019                 memcpy(&priv->card_alive_init,
1020                        &pkt->u.alive_frame,
1021                        sizeof(struct iwl_init_alive_resp));
1022                 pwork = &priv->init_alive_start;
1023         } else {
1024                 IWL_DEBUG_INFO("Runtime Alive received.\n");
1025                 memcpy(&priv->card_alive, &pkt->u.alive_frame,
1026                        sizeof(struct iwl_alive_resp));
1027                 pwork = &priv->alive_start;
1028         }
1029
1030         /* We delay the ALIVE response by 5ms to
1031          * give the HW RF Kill time to activate... */
1032         if (palive->is_valid == UCODE_VALID_OK)
1033                 queue_delayed_work(priv->workqueue, pwork,
1034                                    msecs_to_jiffies(5));
1035         else
1036                 IWL_WARNING("uCode did not respond OK.\n");
1037 }
1038
1039 static void iwl4965_rx_reply_error(struct iwl_priv *priv,
1040                                    struct iwl_rx_mem_buffer *rxb)
1041 {
1042         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1043
1044         IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
1045                 "seq 0x%04X ser 0x%08X\n",
1046                 le32_to_cpu(pkt->u.err_resp.error_type),
1047                 get_cmd_string(pkt->u.err_resp.cmd_id),
1048                 pkt->u.err_resp.cmd_id,
1049                 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1050                 le32_to_cpu(pkt->u.err_resp.error_info));
1051 }
1052
1053 #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
1054
1055 static void iwl4965_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
1056 {
1057         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1058         struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
1059         struct iwl4965_csa_notification *csa = &(pkt->u.csa_notif);
1060         IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
1061                       le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
1062         rxon->channel = csa->channel;
1063         priv->staging_rxon.channel = csa->channel;
1064 }
1065
1066 static void iwl4965_rx_spectrum_measure_notif(struct iwl_priv *priv,
1067                                           struct iwl_rx_mem_buffer *rxb)
1068 {
1069 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
1070         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1071         struct iwl4965_spectrum_notification *report = &(pkt->u.spectrum_notif);
1072
1073         if (!report->state) {
1074                 IWL_DEBUG(IWL_DL_11H,
1075                         "Spectrum Measure Notification: Start\n");
1076                 return;
1077         }
1078
1079         memcpy(&priv->measure_report, report, sizeof(*report));
1080         priv->measurement_status |= MEASUREMENT_READY;
1081 #endif
1082 }
1083
1084 static void iwl4965_rx_pm_sleep_notif(struct iwl_priv *priv,
1085                                       struct iwl_rx_mem_buffer *rxb)
1086 {
1087 #ifdef CONFIG_IWLWIFI_DEBUG
1088         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1089         struct iwl4965_sleep_notification *sleep = &(pkt->u.sleep_notif);
1090         IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
1091                      sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1092 #endif
1093 }
1094
1095 static void iwl4965_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1096                                              struct iwl_rx_mem_buffer *rxb)
1097 {
1098         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1099         IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
1100                         "notification for %s:\n",
1101                         le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
1102         iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
1103 }
1104
1105 static void iwl4965_bg_beacon_update(struct work_struct *work)
1106 {
1107         struct iwl_priv *priv =
1108                 container_of(work, struct iwl_priv, beacon_update);
1109         struct sk_buff *beacon;
1110
1111         /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
1112         beacon = ieee80211_beacon_get(priv->hw, priv->vif);
1113
1114         if (!beacon) {
1115                 IWL_ERROR("update beacon failed\n");
1116                 return;
1117         }
1118
1119         mutex_lock(&priv->mutex);
1120         /* new beacon skb is allocated every time; dispose previous.*/
1121         if (priv->ibss_beacon)
1122                 dev_kfree_skb(priv->ibss_beacon);
1123
1124         priv->ibss_beacon = beacon;
1125         mutex_unlock(&priv->mutex);
1126
1127         iwl4965_send_beacon_cmd(priv);
1128 }
1129
1130 /**
1131  * iwl4965_bg_statistics_periodic - Timer callback to queue statistics
1132  *
1133  * This callback is provided in order to send a statistics request.
1134  *
1135  * This timer function is continually reset to execute within
1136  * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
1137  * was received.  We need to ensure we receive the statistics in order
1138  * to update the temperature used for calibrating the TXPOWER.
1139  */
1140 static void iwl4965_bg_statistics_periodic(unsigned long data)
1141 {
1142         struct iwl_priv *priv = (struct iwl_priv *)data;
1143
1144         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1145                 return;
1146
1147         iwl_send_statistics_request(priv, CMD_ASYNC);
1148 }
1149
1150 static void iwl4965_rx_beacon_notif(struct iwl_priv *priv,
1151                                 struct iwl_rx_mem_buffer *rxb)
1152 {
1153 #ifdef CONFIG_IWLWIFI_DEBUG
1154         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1155         struct iwl4965_beacon_notif *beacon = &(pkt->u.beacon_status);
1156         u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
1157
1158         IWL_DEBUG_RX("beacon status %x retries %d iss %d "
1159                 "tsf %d %d rate %d\n",
1160                 le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
1161                 beacon->beacon_notify_hdr.failure_frame,
1162                 le32_to_cpu(beacon->ibss_mgr_status),
1163                 le32_to_cpu(beacon->high_tsf),
1164                 le32_to_cpu(beacon->low_tsf), rate);
1165 #endif
1166
1167         if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
1168             (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1169                 queue_work(priv->workqueue, &priv->beacon_update);
1170 }
1171
1172 /* Handle notification from uCode that card's power state is changing
1173  * due to software, hardware, or critical temperature RFKILL */
1174 static void iwl4965_rx_card_state_notif(struct iwl_priv *priv,
1175                                     struct iwl_rx_mem_buffer *rxb)
1176 {
1177         struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
1178         u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1179         unsigned long status = priv->status;
1180
1181         IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
1182                           (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1183                           (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1184
1185         if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
1186                      RF_CARD_DISABLED)) {
1187
1188                 iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1189                             CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1190
1191                 if (!iwl_grab_nic_access(priv)) {
1192                         iwl_write_direct32(
1193                                 priv, HBUS_TARG_MBX_C,
1194                                 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1195
1196                         iwl_release_nic_access(priv);
1197                 }
1198
1199                 if (!(flags & RXON_CARD_DISABLED)) {
1200                         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
1201                                     CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1202                         if (!iwl_grab_nic_access(priv)) {
1203                                 iwl_write_direct32(
1204                                         priv, HBUS_TARG_MBX_C,
1205                                         HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
1206
1207                                 iwl_release_nic_access(priv);
1208                         }
1209                 }
1210
1211                 if (flags & RF_CARD_DISABLED) {
1212                         iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1213                                     CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
1214                         iwl_read32(priv, CSR_UCODE_DRV_GP1);
1215                         if (!iwl_grab_nic_access(priv))
1216                                 iwl_release_nic_access(priv);
1217                 }
1218         }
1219
1220         if (flags & HW_CARD_DISABLED)
1221                 set_bit(STATUS_RF_KILL_HW, &priv->status);
1222         else
1223                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1224
1225
1226         if (flags & SW_CARD_DISABLED)
1227                 set_bit(STATUS_RF_KILL_SW, &priv->status);
1228         else
1229                 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1230
1231         if (!(flags & RXON_CARD_DISABLED))
1232                 iwl_scan_cancel(priv);
1233
1234         if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1235              test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1236             (test_bit(STATUS_RF_KILL_SW, &status) !=
1237              test_bit(STATUS_RF_KILL_SW, &priv->status)))
1238                 queue_work(priv->workqueue, &priv->rf_kill);
1239         else
1240                 wake_up_interruptible(&priv->wait_command_queue);
1241 }
1242
1243 /**
1244  * iwl4965_setup_rx_handlers - Initialize Rx handler callbacks
1245  *
1246  * Setup the RX handlers for each of the reply types sent from the uCode
1247  * to the host.
1248  *
1249  * This function chains into the hardware specific files for them to setup
1250  * any hardware specific handlers as well.
1251  */
1252 static void iwl_setup_rx_handlers(struct iwl_priv *priv)
1253 {
1254         priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
1255         priv->rx_handlers[REPLY_ERROR] = iwl4965_rx_reply_error;
1256         priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl4965_rx_csa;
1257         priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
1258             iwl4965_rx_spectrum_measure_notif;
1259         priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl4965_rx_pm_sleep_notif;
1260         priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
1261             iwl4965_rx_pm_debug_statistics_notif;
1262         priv->rx_handlers[BEACON_NOTIFICATION] = iwl4965_rx_beacon_notif;
1263
1264         /*
1265          * The same handler is used for both the REPLY to a discrete
1266          * statistics request from the host as well as for the periodic
1267          * statistics notifications (after received beacons) from the uCode.
1268          */
1269         priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_rx_statistics;
1270         priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_rx_statistics;
1271
1272         iwl_setup_rx_scan_handlers(priv);
1273
1274         /* status change handler */
1275         priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl4965_rx_card_state_notif;
1276
1277         priv->rx_handlers[MISSED_BEACONS_NOTIFICATION] =
1278             iwl_rx_missed_beacon_notif;
1279         /* Rx handlers */
1280         priv->rx_handlers[REPLY_RX_PHY_CMD] = iwl_rx_reply_rx_phy;
1281         priv->rx_handlers[REPLY_RX_MPDU_CMD] = iwl_rx_reply_rx;
1282         /* block ack */
1283         priv->rx_handlers[REPLY_COMPRESSED_BA] = iwl_rx_reply_compressed_ba;
1284         /* Set up hardware specific Rx handlers */
1285         priv->cfg->ops->lib->rx_handler_setup(priv);
1286 }
1287
1288 /*
1289  * this should be called while priv->lock is locked
1290 */
1291 static void __iwl_rx_replenish(struct iwl_priv *priv)
1292 {
1293         iwl_rx_allocate(priv);
1294         iwl_rx_queue_restock(priv);
1295 }
1296
1297
1298 /**
1299  * iwl_rx_handle - Main entry function for receiving responses from uCode
1300  *
1301  * Uses the priv->rx_handlers callback function array to invoke
1302  * the appropriate handlers, including command responses,
1303  * frame-received notifications, and other notifications.
1304  */
1305 void iwl_rx_handle(struct iwl_priv *priv)
1306 {
1307         struct iwl_rx_mem_buffer *rxb;
1308         struct iwl_rx_packet *pkt;
1309         struct iwl_rx_queue *rxq = &priv->rxq;
1310         u32 r, i;
1311         int reclaim;
1312         unsigned long flags;
1313         u8 fill_rx = 0;
1314         u32 count = 8;
1315
1316         /* uCode's read index (stored in shared DRAM) indicates the last Rx
1317          * buffer that the driver may process (last buffer filled by ucode). */
1318         r = priv->cfg->ops->lib->shared_mem_rx_idx(priv);
1319         i = rxq->read;
1320
1321         /* Rx interrupt, but nothing sent from uCode */
1322         if (i == r)
1323                 IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d\n", r, i);
1324
1325         if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
1326                 fill_rx = 1;
1327
1328         while (i != r) {
1329                 rxb = rxq->queue[i];
1330
1331                 /* If an RXB doesn't have a Rx queue slot associated with it,
1332                  * then a bug has been introduced in the queue refilling
1333                  * routines -- catch it here */
1334                 BUG_ON(rxb == NULL);
1335
1336                 rxq->queue[i] = NULL;
1337
1338                 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
1339                                             priv->hw_params.rx_buf_size,
1340                                             PCI_DMA_FROMDEVICE);
1341                 pkt = (struct iwl_rx_packet *)rxb->skb->data;
1342
1343                 /* Reclaim a command buffer only if this packet is a response
1344                  *   to a (driver-originated) command.
1345                  * If the packet (e.g. Rx frame) originated from uCode,
1346                  *   there is no command buffer to reclaim.
1347                  * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1348                  *   but apparently a few don't get set; catch them here. */
1349                 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1350                         (pkt->hdr.cmd != REPLY_RX_PHY_CMD) &&
1351                         (pkt->hdr.cmd != REPLY_RX) &&
1352                         (pkt->hdr.cmd != REPLY_COMPRESSED_BA) &&
1353                         (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1354                         (pkt->hdr.cmd != REPLY_TX);
1355
1356                 /* Based on type of command response or notification,
1357                  *   handle those that need handling via function in
1358                  *   rx_handlers table.  See iwl4965_setup_rx_handlers() */
1359                 if (priv->rx_handlers[pkt->hdr.cmd]) {
1360                         IWL_DEBUG(IWL_DL_RX, "r = %d, i = %d, %s, 0x%02x\n", r,
1361                                 i, get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1362                         priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1363                 } else {
1364                         /* No handling needed */
1365                         IWL_DEBUG(IWL_DL_RX,
1366                                 "r %d i %d No handler needed for %s, 0x%02x\n",
1367                                 r, i, get_cmd_string(pkt->hdr.cmd),
1368                                 pkt->hdr.cmd);
1369                 }
1370
1371                 if (reclaim) {
1372                         /* Invoke any callbacks, transfer the skb to caller, and
1373                          * fire off the (possibly) blocking iwl_send_cmd()
1374                          * as we reclaim the driver command queue */
1375                         if (rxb && rxb->skb)
1376                                 iwl_tx_cmd_complete(priv, rxb);
1377                         else
1378                                 IWL_WARNING("Claim null rxb?\n");
1379                 }
1380
1381                 /* For now we just don't re-use anything.  We can tweak this
1382                  * later to try and re-use notification packets and SKBs that
1383                  * fail to Rx correctly */
1384                 if (rxb->skb != NULL) {
1385                         priv->alloc_rxb_skb--;
1386                         dev_kfree_skb_any(rxb->skb);
1387                         rxb->skb = NULL;
1388                 }
1389
1390                 pci_unmap_single(priv->pci_dev, rxb->dma_addr,
1391                                  priv->hw_params.rx_buf_size,
1392                                  PCI_DMA_FROMDEVICE);
1393                 spin_lock_irqsave(&rxq->lock, flags);
1394                 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1395                 spin_unlock_irqrestore(&rxq->lock, flags);
1396                 i = (i + 1) & RX_QUEUE_MASK;
1397                 /* If there are a lot of unused frames,
1398                  * restock the Rx queue so ucode wont assert. */
1399                 if (fill_rx) {
1400                         count++;
1401                         if (count >= 8) {
1402                                 priv->rxq.read = i;
1403                                 __iwl_rx_replenish(priv);
1404                                 count = 0;
1405                         }
1406                 }
1407         }
1408
1409         /* Backtrack one entry */
1410         priv->rxq.read = i;
1411         iwl_rx_queue_restock(priv);
1412 }
1413
1414 #ifdef CONFIG_IWLWIFI_DEBUG
1415 static void iwl4965_print_rx_config_cmd(struct iwl_priv *priv)
1416 {
1417         struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
1418         DECLARE_MAC_BUF(mac);
1419
1420         IWL_DEBUG_RADIO("RX CONFIG:\n");
1421         iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
1422         IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
1423         IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
1424         IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
1425                         le32_to_cpu(rxon->filter_flags));
1426         IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
1427         IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
1428                         rxon->ofdm_basic_rates);
1429         IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
1430         IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
1431                         print_mac(mac, rxon->node_addr));
1432         IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
1433                         print_mac(mac, rxon->bssid_addr));
1434         IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
1435 }
1436 #endif
1437
1438 static void iwl4965_enable_interrupts(struct iwl_priv *priv)
1439 {
1440         IWL_DEBUG_ISR("Enabling interrupts\n");
1441         set_bit(STATUS_INT_ENABLED, &priv->status);
1442         iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
1443 }
1444
1445 /* call this function to flush any scheduled tasklet */
1446 static inline void iwl_synchronize_irq(struct iwl_priv *priv)
1447 {
1448         /* wait to make sure we flush pedding tasklet*/
1449         synchronize_irq(priv->pci_dev->irq);
1450         tasklet_kill(&priv->irq_tasklet);
1451 }
1452
1453 static inline void iwl4965_disable_interrupts(struct iwl_priv *priv)
1454 {
1455         clear_bit(STATUS_INT_ENABLED, &priv->status);
1456
1457         /* disable interrupts from uCode/NIC to host */
1458         iwl_write32(priv, CSR_INT_MASK, 0x00000000);
1459
1460         /* acknowledge/clear/reset any interrupts still pending
1461          * from uCode or flow handler (Rx/Tx DMA) */
1462         iwl_write32(priv, CSR_INT, 0xffffffff);
1463         iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
1464         IWL_DEBUG_ISR("Disabled interrupts\n");
1465 }
1466
1467
1468 /**
1469  * iwl4965_irq_handle_error - called for HW or SW error interrupt from card
1470  */
1471 static void iwl4965_irq_handle_error(struct iwl_priv *priv)
1472 {
1473         /* Set the FW error flag -- cleared on iwl4965_down */
1474         set_bit(STATUS_FW_ERROR, &priv->status);
1475
1476         /* Cancel currently queued command. */
1477         clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
1478
1479 #ifdef CONFIG_IWLWIFI_DEBUG
1480         if (priv->debug_level & IWL_DL_FW_ERRORS) {
1481                 iwl_dump_nic_error_log(priv);
1482                 iwl_dump_nic_event_log(priv);
1483                 iwl4965_print_rx_config_cmd(priv);
1484         }
1485 #endif
1486
1487         wake_up_interruptible(&priv->wait_command_queue);
1488
1489         /* Keep the restart process from trying to send host
1490          * commands by clearing the INIT status bit */
1491         clear_bit(STATUS_READY, &priv->status);
1492
1493         if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1494                 IWL_DEBUG(IWL_DL_FW_ERRORS,
1495                           "Restarting adapter due to uCode error.\n");
1496
1497                 if (iwl_is_associated(priv)) {
1498                         memcpy(&priv->recovery_rxon, &priv->active_rxon,
1499                                sizeof(priv->recovery_rxon));
1500                         priv->error_recovering = 1;
1501                 }
1502                 if (priv->cfg->mod_params->restart_fw)
1503                         queue_work(priv->workqueue, &priv->restart);
1504         }
1505 }
1506
1507 static void iwl4965_error_recovery(struct iwl_priv *priv)
1508 {
1509         unsigned long flags;
1510
1511         memcpy(&priv->staging_rxon, &priv->recovery_rxon,
1512                sizeof(priv->staging_rxon));
1513         priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
1514         iwl4965_commit_rxon(priv);
1515
1516         iwl_rxon_add_station(priv, priv->bssid, 1);
1517
1518         spin_lock_irqsave(&priv->lock, flags);
1519         priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
1520         priv->error_recovering = 0;
1521         spin_unlock_irqrestore(&priv->lock, flags);
1522 }
1523
1524 static void iwl4965_irq_tasklet(struct iwl_priv *priv)
1525 {
1526         u32 inta, handled = 0;
1527         u32 inta_fh;
1528         unsigned long flags;
1529 #ifdef CONFIG_IWLWIFI_DEBUG
1530         u32 inta_mask;
1531 #endif
1532
1533         spin_lock_irqsave(&priv->lock, flags);
1534
1535         /* Ack/clear/reset pending uCode interrupts.
1536          * Note:  Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
1537          *  and will clear only when CSR_FH_INT_STATUS gets cleared. */
1538         inta = iwl_read32(priv, CSR_INT);
1539         iwl_write32(priv, CSR_INT, inta);
1540
1541         /* Ack/clear/reset pending flow-handler (DMA) interrupts.
1542          * Any new interrupts that happen after this, either while we're
1543          * in this tasklet, or later, will show up in next ISR/tasklet. */
1544         inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1545         iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
1546
1547 #ifdef CONFIG_IWLWIFI_DEBUG
1548         if (priv->debug_level & IWL_DL_ISR) {
1549                 /* just for debug */
1550                 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1551                 IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1552                               inta, inta_mask, inta_fh);
1553         }
1554 #endif
1555
1556         /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
1557          * atomic, make sure that inta covers all the interrupts that
1558          * we've discovered, even if FH interrupt came in just after
1559          * reading CSR_INT. */
1560         if (inta_fh & CSR49_FH_INT_RX_MASK)
1561                 inta |= CSR_INT_BIT_FH_RX;
1562         if (inta_fh & CSR49_FH_INT_TX_MASK)
1563                 inta |= CSR_INT_BIT_FH_TX;
1564
1565         /* Now service all interrupt bits discovered above. */
1566         if (inta & CSR_INT_BIT_HW_ERR) {
1567                 IWL_ERROR("Microcode HW error detected.  Restarting.\n");
1568
1569                 /* Tell the device to stop sending interrupts */
1570                 iwl4965_disable_interrupts(priv);
1571
1572                 iwl4965_irq_handle_error(priv);
1573
1574                 handled |= CSR_INT_BIT_HW_ERR;
1575
1576                 spin_unlock_irqrestore(&priv->lock, flags);
1577
1578                 return;
1579         }
1580
1581 #ifdef CONFIG_IWLWIFI_DEBUG
1582         if (priv->debug_level & (IWL_DL_ISR)) {
1583                 /* NIC fires this, but we don't use it, redundant with WAKEUP */
1584                 if (inta & CSR_INT_BIT_SCD)
1585                         IWL_DEBUG_ISR("Scheduler finished to transmit "
1586                                       "the frame/frames.\n");
1587
1588                 /* Alive notification via Rx interrupt will do the real work */
1589                 if (inta & CSR_INT_BIT_ALIVE)
1590                         IWL_DEBUG_ISR("Alive interrupt\n");
1591         }
1592 #endif
1593         /* Safely ignore these bits for debug checks below */
1594         inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
1595
1596         /* HW RF KILL switch toggled */
1597         if (inta & CSR_INT_BIT_RF_KILL) {
1598                 int hw_rf_kill = 0;
1599                 if (!(iwl_read32(priv, CSR_GP_CNTRL) &
1600                                 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
1601                         hw_rf_kill = 1;
1602
1603                 IWL_DEBUG(IWL_DL_RF_KILL, "RF_KILL bit toggled to %s.\n",
1604                                 hw_rf_kill ? "disable radio":"enable radio");
1605
1606                 /* driver only loads ucode once setting the interface up.
1607                  * the driver as well won't allow loading if RFKILL is set
1608                  * therefore no need to restart the driver from this handler
1609                  */
1610                 if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status))
1611                         clear_bit(STATUS_RF_KILL_HW, &priv->status);
1612
1613                 handled |= CSR_INT_BIT_RF_KILL;
1614         }
1615
1616         /* Chip got too hot and stopped itself */
1617         if (inta & CSR_INT_BIT_CT_KILL) {
1618                 IWL_ERROR("Microcode CT kill error detected.\n");
1619                 handled |= CSR_INT_BIT_CT_KILL;
1620         }
1621
1622         /* Error detected by uCode */
1623         if (inta & CSR_INT_BIT_SW_ERR) {
1624                 IWL_ERROR("Microcode SW error detected.  Restarting 0x%X.\n",
1625                           inta);
1626                 iwl4965_irq_handle_error(priv);
1627                 handled |= CSR_INT_BIT_SW_ERR;
1628         }
1629
1630         /* uCode wakes up after power-down sleep */
1631         if (inta & CSR_INT_BIT_WAKEUP) {
1632                 IWL_DEBUG_ISR("Wakeup interrupt\n");
1633                 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
1634                 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
1635                 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
1636                 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
1637                 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
1638                 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
1639                 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
1640
1641                 handled |= CSR_INT_BIT_WAKEUP;
1642         }
1643
1644         /* All uCode command responses, including Tx command responses,
1645          * Rx "responses" (frame-received notification), and other
1646          * notifications from uCode come through here*/
1647         if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
1648                 iwl_rx_handle(priv);
1649                 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
1650         }
1651
1652         if (inta & CSR_INT_BIT_FH_TX) {
1653                 IWL_DEBUG_ISR("Tx interrupt\n");
1654                 handled |= CSR_INT_BIT_FH_TX;
1655                 /* FH finished to write, send event */
1656                 priv->ucode_write_complete = 1;
1657                 wake_up_interruptible(&priv->wait_command_queue);
1658         }
1659
1660         if (inta & ~handled)
1661                 IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
1662
1663         if (inta & ~CSR_INI_SET_MASK) {
1664                 IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
1665                          inta & ~CSR_INI_SET_MASK);
1666                 IWL_WARNING("   with FH_INT = 0x%08x\n", inta_fh);
1667         }
1668
1669         /* Re-enable all interrupts */
1670         /* only Re-enable if diabled by irq */
1671         if (test_bit(STATUS_INT_ENABLED, &priv->status))
1672                 iwl4965_enable_interrupts(priv);
1673
1674 #ifdef CONFIG_IWLWIFI_DEBUG
1675         if (priv->debug_level & (IWL_DL_ISR)) {
1676                 inta = iwl_read32(priv, CSR_INT);
1677                 inta_mask = iwl_read32(priv, CSR_INT_MASK);
1678                 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1679                 IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
1680                         "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
1681         }
1682 #endif
1683         spin_unlock_irqrestore(&priv->lock, flags);
1684 }
1685
1686 static irqreturn_t iwl4965_isr(int irq, void *data)
1687 {
1688         struct iwl_priv *priv = data;
1689         u32 inta, inta_mask;
1690         u32 inta_fh;
1691         if (!priv)
1692                 return IRQ_NONE;
1693
1694         spin_lock(&priv->lock);
1695
1696         /* Disable (but don't clear!) interrupts here to avoid
1697          *    back-to-back ISRs and sporadic interrupts from our NIC.
1698          * If we have something to service, the tasklet will re-enable ints.
1699          * If we *don't* have something, we'll re-enable before leaving here. */
1700         inta_mask = iwl_read32(priv, CSR_INT_MASK);  /* just for debug */
1701         iwl_write32(priv, CSR_INT_MASK, 0x00000000);
1702
1703         /* Discover which interrupts are active/pending */
1704         inta = iwl_read32(priv, CSR_INT);
1705         inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1706
1707         /* Ignore interrupt if there's nothing in NIC to service.
1708          * This may be due to IRQ shared with another device,
1709          * or due to sporadic interrupts thrown from our NIC. */
1710         if (!inta && !inta_fh) {
1711                 IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
1712                 goto none;
1713         }
1714
1715         if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1716                 /* Hardware disappeared. It might have already raised
1717                  * an interrupt */
1718                 IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
1719                 goto unplugged;
1720         }
1721
1722         IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1723                       inta, inta_mask, inta_fh);
1724
1725         inta &= ~CSR_INT_BIT_SCD;
1726
1727         /* iwl4965_irq_tasklet() will service interrupts and re-enable them */
1728         if (likely(inta || inta_fh))
1729                 tasklet_schedule(&priv->irq_tasklet);
1730
1731  unplugged:
1732         spin_unlock(&priv->lock);
1733         return IRQ_HANDLED;
1734
1735  none:
1736         /* re-enable interrupts here since we don't have anything to service. */
1737         /* only Re-enable if diabled by irq */
1738         if (test_bit(STATUS_INT_ENABLED, &priv->status))
1739                 iwl4965_enable_interrupts(priv);
1740         spin_unlock(&priv->lock);
1741         return IRQ_NONE;
1742 }
1743
1744 /******************************************************************************
1745  *
1746  * uCode download functions
1747  *
1748  ******************************************************************************/
1749
1750 static void iwl4965_dealloc_ucode_pci(struct iwl_priv *priv)
1751 {
1752         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
1753         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
1754         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1755         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
1756         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1757         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
1758 }
1759
1760 static void iwl4965_nic_start(struct iwl_priv *priv)
1761 {
1762         /* Remove all resets to allow NIC to operate */
1763         iwl_write32(priv, CSR_RESET, 0);
1764 }
1765
1766
1767 /**
1768  * iwl4965_read_ucode - Read uCode images from disk file.
1769  *
1770  * Copy into buffers for card to fetch via bus-mastering
1771  */
1772 static int iwl4965_read_ucode(struct iwl_priv *priv)
1773 {
1774         struct iwl_ucode *ucode;
1775         int ret;
1776         const struct firmware *ucode_raw;
1777         const char *name = priv->cfg->fw_name;
1778         u8 *src;
1779         size_t len;
1780         u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
1781
1782         /* Ask kernel firmware_class module to get the boot firmware off disk.
1783          * request_firmware() is synchronous, file is in memory on return. */
1784         ret = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
1785         if (ret < 0) {
1786                 IWL_ERROR("%s firmware file req failed: Reason %d\n",
1787                                         name, ret);
1788                 goto error;
1789         }
1790
1791         IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
1792                        name, ucode_raw->size);
1793
1794         /* Make sure that we got at least our header! */
1795         if (ucode_raw->size < sizeof(*ucode)) {
1796                 IWL_ERROR("File size way too small!\n");
1797                 ret = -EINVAL;
1798                 goto err_release;
1799         }
1800
1801         /* Data from ucode file:  header followed by uCode images */
1802         ucode = (void *)ucode_raw->data;
1803
1804         ver = le32_to_cpu(ucode->ver);
1805         inst_size = le32_to_cpu(ucode->inst_size);
1806         data_size = le32_to_cpu(ucode->data_size);
1807         init_size = le32_to_cpu(ucode->init_size);
1808         init_data_size = le32_to_cpu(ucode->init_data_size);
1809         boot_size = le32_to_cpu(ucode->boot_size);
1810
1811         IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
1812         IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n",
1813                        inst_size);
1814         IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n",
1815                        data_size);
1816         IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n",
1817                        init_size);
1818         IWL_DEBUG_INFO("f/w package hdr init data size = %u\n",
1819                        init_data_size);
1820         IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n",
1821                        boot_size);
1822
1823         /* Verify size of file vs. image size info in file's header */
1824         if (ucode_raw->size < sizeof(*ucode) +
1825                 inst_size + data_size + init_size +
1826                 init_data_size + boot_size) {
1827
1828                 IWL_DEBUG_INFO("uCode file size %d too small\n",
1829                                (int)ucode_raw->size);
1830                 ret = -EINVAL;
1831                 goto err_release;
1832         }
1833
1834         /* Verify that uCode images will fit in card's SRAM */
1835         if (inst_size > priv->hw_params.max_inst_size) {
1836                 IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
1837                                inst_size);
1838                 ret = -EINVAL;
1839                 goto err_release;
1840         }
1841
1842         if (data_size > priv->hw_params.max_data_size) {
1843                 IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
1844                                 data_size);
1845                 ret = -EINVAL;
1846                 goto err_release;
1847         }
1848         if (init_size > priv->hw_params.max_inst_size) {
1849                 IWL_DEBUG_INFO
1850                     ("uCode init instr len %d too large to fit in\n",
1851                       init_size);
1852                 ret = -EINVAL;
1853                 goto err_release;
1854         }
1855         if (init_data_size > priv->hw_params.max_data_size) {
1856                 IWL_DEBUG_INFO
1857                     ("uCode init data len %d too large to fit in\n",
1858                       init_data_size);
1859                 ret = -EINVAL;
1860                 goto err_release;
1861         }
1862         if (boot_size > priv->hw_params.max_bsm_size) {
1863                 IWL_DEBUG_INFO
1864                     ("uCode boot instr len %d too large to fit in\n",
1865                       boot_size);
1866                 ret = -EINVAL;
1867                 goto err_release;
1868         }
1869
1870         /* Allocate ucode buffers for card's bus-master loading ... */
1871
1872         /* Runtime instructions and 2 copies of data:
1873          * 1) unmodified from disk
1874          * 2) backup cache for save/restore during power-downs */
1875         priv->ucode_code.len = inst_size;
1876         iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
1877
1878         priv->ucode_data.len = data_size;
1879         iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
1880
1881         priv->ucode_data_backup.len = data_size;
1882         iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
1883
1884         /* Initialization instructions and data */
1885         if (init_size && init_data_size) {
1886                 priv->ucode_init.len = init_size;
1887                 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
1888
1889                 priv->ucode_init_data.len = init_data_size;
1890                 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
1891
1892                 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
1893                         goto err_pci_alloc;
1894         }
1895
1896         /* Bootstrap (instructions only, no data) */
1897         if (boot_size) {
1898                 priv->ucode_boot.len = boot_size;
1899                 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
1900
1901                 if (!priv->ucode_boot.v_addr)
1902                         goto err_pci_alloc;
1903         }
1904
1905         /* Copy images into buffers for card's bus-master reads ... */
1906
1907         /* Runtime instructions (first block of data in file) */
1908         src = &ucode->data[0];
1909         len = priv->ucode_code.len;
1910         IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
1911         memcpy(priv->ucode_code.v_addr, src, len);
1912         IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
1913                 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
1914
1915         /* Runtime data (2nd block)
1916          * NOTE:  Copy into backup buffer will be done in iwl4965_up()  */
1917         src = &ucode->data[inst_size];
1918         len = priv->ucode_data.len;
1919         IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
1920         memcpy(priv->ucode_data.v_addr, src, len);
1921         memcpy(priv->ucode_data_backup.v_addr, src, len);
1922
1923         /* Initialization instructions (3rd block) */
1924         if (init_size) {
1925                 src = &ucode->data[inst_size + data_size];
1926                 len = priv->ucode_init.len;
1927                 IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
1928                                 len);
1929                 memcpy(priv->ucode_init.v_addr, src, len);
1930         }
1931
1932         /* Initialization data (4th block) */
1933         if (init_data_size) {
1934                 src = &ucode->data[inst_size + data_size + init_size];
1935                 len = priv->ucode_init_data.len;
1936                 IWL_DEBUG_INFO("Copying (but not loading) init data len %Zd\n",
1937                                len);
1938                 memcpy(priv->ucode_init_data.v_addr, src, len);
1939         }
1940
1941         /* Bootstrap instructions (5th block) */
1942         src = &ucode->data[inst_size + data_size + init_size + init_data_size];
1943         len = priv->ucode_boot.len;
1944         IWL_DEBUG_INFO("Copying (but not loading) boot instr len %Zd\n", len);
1945         memcpy(priv->ucode_boot.v_addr, src, len);
1946
1947         /* We have our copies now, allow OS release its copies */
1948         release_firmware(ucode_raw);
1949         return 0;
1950
1951  err_pci_alloc:
1952         IWL_ERROR("failed to allocate pci memory\n");
1953         ret = -ENOMEM;
1954         iwl4965_dealloc_ucode_pci(priv);
1955
1956  err_release:
1957         release_firmware(ucode_raw);
1958
1959  error:
1960         return ret;
1961 }
1962
1963 /**
1964  * iwl_alive_start - called after REPLY_ALIVE notification received
1965  *                   from protocol/runtime uCode (initialization uCode's
1966  *                   Alive gets handled by iwl_init_alive_start()).
1967  */
1968 static void iwl_alive_start(struct iwl_priv *priv)
1969 {
1970         int ret = 0;
1971
1972         IWL_DEBUG_INFO("Runtime Alive received.\n");
1973
1974         if (priv->card_alive.is_valid != UCODE_VALID_OK) {
1975                 /* We had an error bringing up the hardware, so take it
1976                  * all the way back down so we can try again */
1977                 IWL_DEBUG_INFO("Alive failed.\n");
1978                 goto restart;
1979         }
1980
1981         /* Initialize uCode has loaded Runtime uCode ... verify inst image.
1982          * This is a paranoid check, because we would not have gotten the
1983          * "runtime" alive if code weren't properly loaded.  */
1984         if (iwl_verify_ucode(priv)) {
1985                 /* Runtime instruction load was bad;
1986                  * take it all the way back down so we can try again */
1987                 IWL_DEBUG_INFO("Bad runtime uCode load.\n");
1988                 goto restart;
1989         }
1990
1991         iwl_clear_stations_table(priv);
1992         ret = priv->cfg->ops->lib->alive_notify(priv);
1993         if (ret) {
1994                 IWL_WARNING("Could not complete ALIVE transition [ntf]: %d\n",
1995                             ret);
1996                 goto restart;
1997         }
1998
1999         /* After the ALIVE response, we can send host commands to 4965 uCode */
2000         set_bit(STATUS_ALIVE, &priv->status);
2001
2002         if (iwl_is_rfkill(priv))
2003                 return;
2004
2005         ieee80211_wake_queues(priv->hw);
2006
2007         priv->active_rate = priv->rates_mask;
2008         priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2009
2010         if (iwl_is_associated(priv)) {
2011                 struct iwl_rxon_cmd *active_rxon =
2012                                 (struct iwl_rxon_cmd *)&priv->active_rxon;
2013
2014                 memcpy(&priv->staging_rxon, &priv->active_rxon,
2015                        sizeof(priv->staging_rxon));
2016                 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2017         } else {
2018                 /* Initialize our rx_config data */
2019                 iwl4965_connection_init_rx_config(priv);
2020                 memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2021         }
2022
2023         /* Configure Bluetooth device coexistence support */
2024         iwl4965_send_bt_config(priv);
2025
2026         iwl_reset_run_time_calib(priv);
2027
2028         /* Configure the adapter for unassociated operation */
2029         iwl4965_commit_rxon(priv);
2030
2031         /* At this point, the NIC is initialized and operational */
2032         iwl_rf_kill_ct_config(priv);
2033
2034         iwl_leds_register(priv);
2035
2036         IWL_DEBUG_INFO("ALIVE processing complete.\n");
2037         set_bit(STATUS_READY, &priv->status);
2038         wake_up_interruptible(&priv->wait_command_queue);
2039
2040         if (priv->error_recovering)
2041                 iwl4965_error_recovery(priv);
2042
2043         iwl_power_update_mode(priv, 1);
2044         ieee80211_notify_mac(priv->hw, IEEE80211_NOTIFY_RE_ASSOC);
2045
2046         if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
2047                 iwl4965_set_mode(priv, priv->iw_mode);
2048
2049         return;
2050
2051  restart:
2052         queue_work(priv->workqueue, &priv->restart);
2053 }
2054
2055 static void iwl_cancel_deferred_work(struct iwl_priv *priv);
2056
2057 static void __iwl4965_down(struct iwl_priv *priv)
2058 {
2059         unsigned long flags;
2060         int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2061
2062         IWL_DEBUG_INFO(DRV_NAME " is going down\n");
2063
2064         if (!exit_pending)
2065                 set_bit(STATUS_EXIT_PENDING, &priv->status);
2066
2067         iwl_leds_unregister(priv);
2068
2069         iwl_clear_stations_table(priv);
2070
2071         /* Unblock any waiting calls */
2072         wake_up_interruptible_all(&priv->wait_command_queue);
2073
2074         /* Wipe out the EXIT_PENDING status bit if we are not actually
2075          * exiting the module */
2076         if (!exit_pending)
2077                 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2078
2079         /* stop and reset the on-board processor */
2080         iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
2081
2082         /* tell the device to stop sending interrupts */
2083         spin_lock_irqsave(&priv->lock, flags);
2084         iwl4965_disable_interrupts(priv);
2085         spin_unlock_irqrestore(&priv->lock, flags);
2086         iwl_synchronize_irq(priv);
2087
2088         if (priv->mac80211_registered)
2089                 ieee80211_stop_queues(priv->hw);
2090
2091         /* If we have not previously called iwl4965_init() then
2092          * clear all bits but the RF Kill and SUSPEND bits and return */
2093         if (!iwl_is_init(priv)) {
2094                 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2095                                         STATUS_RF_KILL_HW |
2096                                test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2097                                         STATUS_RF_KILL_SW |
2098                                test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2099                                         STATUS_GEO_CONFIGURED |
2100                                test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2101                                         STATUS_IN_SUSPEND |
2102                                test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2103                                         STATUS_EXIT_PENDING;
2104                 goto exit;
2105         }
2106
2107         /* ...otherwise clear out all the status bits but the RF Kill and
2108          * SUSPEND bits and continue taking the NIC down. */
2109         priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
2110                                 STATUS_RF_KILL_HW |
2111                         test_bit(STATUS_RF_KILL_SW, &priv->status) <<
2112                                 STATUS_RF_KILL_SW |
2113                         test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
2114                                 STATUS_GEO_CONFIGURED |
2115                         test_bit(STATUS_IN_SUSPEND, &priv->status) <<
2116                                 STATUS_IN_SUSPEND |
2117                         test_bit(STATUS_FW_ERROR, &priv->status) <<
2118                                 STATUS_FW_ERROR |
2119                        test_bit(STATUS_EXIT_PENDING, &priv->status) <<
2120                                 STATUS_EXIT_PENDING;
2121
2122         spin_lock_irqsave(&priv->lock, flags);
2123         iwl_clear_bit(priv, CSR_GP_CNTRL,
2124                          CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
2125         spin_unlock_irqrestore(&priv->lock, flags);
2126
2127         iwl_txq_ctx_stop(priv);
2128         iwl_rxq_stop(priv);
2129
2130         spin_lock_irqsave(&priv->lock, flags);
2131         if (!iwl_grab_nic_access(priv)) {
2132                 iwl_write_prph(priv, APMG_CLK_DIS_REG,
2133                                          APMG_CLK_VAL_DMA_CLK_RQT);
2134                 iwl_release_nic_access(priv);
2135         }
2136         spin_unlock_irqrestore(&priv->lock, flags);
2137
2138         udelay(5);
2139
2140         /* FIXME: apm_ops.suspend(priv) */
2141         priv->cfg->ops->lib->apm_ops.reset(priv);
2142         priv->cfg->ops->lib->free_shared_mem(priv);
2143
2144  exit:
2145         memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
2146
2147         if (priv->ibss_beacon)
2148                 dev_kfree_skb(priv->ibss_beacon);
2149         priv->ibss_beacon = NULL;
2150
2151         /* clear out any free frames */
2152         iwl_clear_free_frames(priv);
2153 }
2154
2155 static void iwl4965_down(struct iwl_priv *priv)
2156 {
2157         mutex_lock(&priv->mutex);
2158         __iwl4965_down(priv);
2159         mutex_unlock(&priv->mutex);
2160
2161         iwl_cancel_deferred_work(priv);
2162 }
2163
2164 #define MAX_HW_RESTARTS 5
2165
2166 static int __iwl4965_up(struct iwl_priv *priv)
2167 {
2168         int i;
2169         int ret;
2170
2171         if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
2172                 IWL_WARNING("Exit pending; will not bring the NIC up\n");
2173                 return -EIO;
2174         }
2175
2176         if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
2177                 IWL_ERROR("ucode not available for device bringup\n");
2178                 return -EIO;
2179         }
2180
2181         /* If platform's RF_KILL switch is NOT set to KILL */
2182         if (iwl_read32(priv, CSR_GP_CNTRL) &
2183                                 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
2184                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2185         else
2186                 set_bit(STATUS_RF_KILL_HW, &priv->status);
2187
2188         if (!test_bit(STATUS_IN_SUSPEND, &priv->status) &&
2189             iwl_is_rfkill(priv)) {
2190                 iwl_rfkill_set_hw_state(priv);
2191                 IWL_WARNING("Radio disabled by %s RF Kill switch\n",
2192                     test_bit(STATUS_RF_KILL_HW, &priv->status) ? "HW" : "SW");
2193                 return -ENODEV;
2194         }
2195
2196         iwl_rfkill_set_hw_state(priv);
2197         iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
2198
2199         ret = priv->cfg->ops->lib->alloc_shared_mem(priv);
2200         if (ret) {
2201                 IWL_ERROR("Unable to allocate shared memory\n");
2202                 return ret;
2203         }
2204
2205         ret = iwl_hw_nic_init(priv);
2206         if (ret) {
2207                 IWL_ERROR("Unable to init nic\n");
2208                 return ret;
2209         }
2210
2211         /* make sure rfkill handshake bits are cleared */
2212         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2213         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
2214                     CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
2215
2216         /* clear (again), then enable host interrupts */
2217         iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
2218         iwl4965_enable_interrupts(priv);
2219
2220         /* really make sure rfkill handshake bits are cleared */
2221         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2222         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
2223
2224         /* Copy original ucode data image from disk into backup cache.
2225          * This will be used to initialize the on-board processor's
2226          * data SRAM for a clean start when the runtime program first loads. */
2227         memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
2228                priv->ucode_data.len);
2229
2230         /* We return success when we resume from suspend and rf_kill is on. */
2231         if (test_bit(STATUS_RF_KILL_HW, &priv->status) ||
2232             test_bit(STATUS_RF_KILL_SW, &priv->status))
2233                 return 0;
2234
2235         for (i = 0; i < MAX_HW_RESTARTS; i++) {
2236
2237                 iwl_clear_stations_table(priv);
2238
2239                 /* load bootstrap state machine,
2240                  * load bootstrap program into processor's memory,
2241                  * prepare to load the "initialize" uCode */
2242                 ret = priv->cfg->ops->lib->load_ucode(priv);
2243
2244                 if (ret) {
2245                         IWL_ERROR("Unable to set up bootstrap uCode: %d\n", ret);
2246                         continue;
2247                 }
2248
2249                 /* Clear out the uCode error bit if it is set */
2250                 clear_bit(STATUS_FW_ERROR, &priv->status);
2251
2252                 /* start card; "initialize" will load runtime ucode */
2253                 iwl4965_nic_start(priv);
2254
2255                 IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
2256
2257                 return 0;
2258         }
2259
2260         set_bit(STATUS_EXIT_PENDING, &priv->status);
2261         __iwl4965_down(priv);
2262         clear_bit(STATUS_EXIT_PENDING, &priv->status);
2263
2264         /* tried to restart and config the device for as long as our
2265          * patience could withstand */
2266         IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
2267         return -EIO;
2268 }
2269
2270
2271 /*****************************************************************************
2272  *
2273  * Workqueue callbacks
2274  *
2275  *****************************************************************************/
2276
2277 static void iwl_bg_init_alive_start(struct work_struct *data)
2278 {
2279         struct iwl_priv *priv =
2280             container_of(data, struct iwl_priv, init_alive_start.work);
2281
2282         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2283                 return;
2284
2285         mutex_lock(&priv->mutex);
2286         priv->cfg->ops->lib->init_alive_start(priv);
2287         mutex_unlock(&priv->mutex);
2288 }
2289
2290 static void iwl_bg_alive_start(struct work_struct *data)
2291 {
2292         struct iwl_priv *priv =
2293             container_of(data, struct iwl_priv, alive_start.work);
2294
2295         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2296                 return;
2297
2298         mutex_lock(&priv->mutex);
2299         iwl_alive_start(priv);
2300         mutex_unlock(&priv->mutex);
2301 }
2302
2303 static void iwl4965_bg_rf_kill(struct work_struct *work)
2304 {
2305         struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
2306
2307         wake_up_interruptible(&priv->wait_command_queue);
2308
2309         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2310                 return;
2311
2312         mutex_lock(&priv->mutex);
2313
2314         if (!iwl_is_rfkill(priv)) {
2315                 IWL_DEBUG(IWL_DL_RF_KILL,
2316                           "HW and/or SW RF Kill no longer active, restarting "
2317                           "device\n");
2318                 if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
2319                         queue_work(priv->workqueue, &priv->restart);
2320         } else {
2321                 /* make sure mac80211 stop sending Tx frame */
2322                 if (priv->mac80211_registered)
2323                         ieee80211_stop_queues(priv->hw);
2324
2325                 if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
2326                         IWL_DEBUG_RF_KILL("Can not turn radio back on - "
2327                                           "disabled by SW switch\n");
2328                 else
2329                         IWL_WARNING("Radio Frequency Kill Switch is On:\n"
2330                                     "Kill switch must be turned off for "
2331                                     "wireless networking to work.\n");
2332         }
2333         iwl_rfkill_set_hw_state(priv);
2334
2335         mutex_unlock(&priv->mutex);
2336 }
2337
2338 static void iwl4965_bg_set_monitor(struct work_struct *work)
2339 {
2340         struct iwl_priv *priv = container_of(work,
2341                                 struct iwl_priv, set_monitor);
2342         int ret;
2343
2344         IWL_DEBUG(IWL_DL_STATE, "setting monitor mode\n");
2345
2346         mutex_lock(&priv->mutex);
2347
2348         ret = iwl4965_set_mode(priv, IEEE80211_IF_TYPE_MNTR);
2349
2350         if (ret) {
2351                 if (ret == -EAGAIN)
2352                         IWL_DEBUG(IWL_DL_STATE, "leave - not ready\n");
2353                 else
2354                         IWL_ERROR("iwl4965_set_mode() failed ret = %d\n", ret);
2355         }
2356
2357         mutex_unlock(&priv->mutex);
2358 }
2359
2360 static void iwl_bg_run_time_calib_work(struct work_struct *work)
2361 {
2362         struct iwl_priv *priv = container_of(work, struct iwl_priv,
2363                         run_time_calib_work);
2364
2365         mutex_lock(&priv->mutex);
2366
2367         if (test_bit(STATUS_EXIT_PENDING, &priv->status) ||
2368             test_bit(STATUS_SCANNING, &priv->status)) {
2369                 mutex_unlock(&priv->mutex);
2370                 return;
2371         }
2372
2373         if (priv->start_calib) {
2374                 iwl_chain_noise_calibration(priv, &priv->statistics);
2375
2376                 iwl_sensitivity_calibration(priv, &priv->statistics);
2377         }
2378
2379         mutex_unlock(&priv->mutex);
2380         return;
2381 }
2382
2383 static void iwl4965_bg_up(struct work_struct *data)
2384 {
2385         struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
2386
2387         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2388                 return;
2389
2390         mutex_lock(&priv->mutex);
2391         __iwl4965_up(priv);
2392         mutex_unlock(&priv->mutex);
2393 }
2394
2395 static void iwl4965_bg_restart(struct work_struct *data)
2396 {
2397         struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
2398
2399         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2400                 return;
2401
2402         iwl4965_down(priv);
2403         queue_work(priv->workqueue, &priv->up);
2404 }
2405
2406 static void iwl4965_bg_rx_replenish(struct work_struct *data)
2407 {
2408         struct iwl_priv *priv =
2409             container_of(data, struct iwl_priv, rx_replenish);
2410
2411         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2412                 return;
2413
2414         mutex_lock(&priv->mutex);
2415         iwl_rx_replenish(priv);
2416         mutex_unlock(&priv->mutex);
2417 }
2418
2419 #define IWL_DELAY_NEXT_SCAN (HZ*2)
2420
2421 static void iwl4965_post_associate(struct iwl_priv *priv)
2422 {
2423         struct ieee80211_conf *conf = NULL;
2424         int ret = 0;
2425         DECLARE_MAC_BUF(mac);
2426
2427         if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2428                 IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
2429                 return;
2430         }
2431
2432         IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
2433                         priv->assoc_id,
2434                         print_mac(mac, priv->active_rxon.bssid_addr));
2435
2436
2437         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2438                 return;
2439
2440
2441         if (!priv->vif || !priv->is_open)
2442                 return;
2443
2444         iwl_scan_cancel_timeout(priv, 200);
2445
2446         conf = ieee80211_get_hw_conf(priv->hw);
2447
2448         priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2449         iwl4965_commit_rxon(priv);
2450
2451         memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
2452         iwl4965_setup_rxon_timing(priv);
2453         ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
2454                               sizeof(priv->rxon_timing), &priv->rxon_timing);
2455         if (ret)
2456                 IWL_WARNING("REPLY_RXON_TIMING failed - "
2457                             "Attempting to continue.\n");
2458
2459         priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
2460
2461         if (priv->current_ht_config.is_ht)
2462                 iwl_set_rxon_ht(priv, &priv->current_ht_config);
2463
2464         iwl_set_rxon_chain(priv);
2465         priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2466
2467         IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
2468                         priv->assoc_id, priv->beacon_int);
2469
2470         if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2471                 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2472         else
2473                 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2474
2475         if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2476                 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
2477                         priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
2478                 else
2479                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2480
2481                 if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2482                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
2483
2484         }
2485
2486         iwl4965_commit_rxon(priv);
2487
2488         switch (priv->iw_mode) {
2489         case IEEE80211_IF_TYPE_STA:
2490                 break;
2491
2492         case IEEE80211_IF_TYPE_IBSS:
2493
2494                 /* assume default assoc id */
2495                 priv->assoc_id = 1;
2496
2497                 iwl_rxon_add_station(priv, priv->bssid, 0);
2498                 iwl4965_send_beacon_cmd(priv);
2499
2500                 break;
2501
2502         default:
2503                 IWL_ERROR("%s Should not be called in %d mode\n",
2504                                 __FUNCTION__, priv->iw_mode);
2505                 break;
2506         }
2507
2508         /* Enable Rx differential gain and sensitivity calibrations */
2509         iwl_chain_noise_reset(priv);
2510         priv->start_calib = 1;
2511
2512         if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2513                 priv->assoc_station_added = 1;
2514
2515         iwl4965_activate_qos(priv, 0);
2516
2517         iwl_power_update_mode(priv, 0);
2518         /* we have just associated, don't start scan too early */
2519         priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
2520 }
2521
2522
2523 static void iwl4965_bg_post_associate(struct work_struct *data)
2524 {
2525         struct iwl_priv *priv = container_of(data, struct iwl_priv,
2526                                              post_associate.work);
2527
2528         mutex_lock(&priv->mutex);
2529         iwl4965_post_associate(priv);
2530         mutex_unlock(&priv->mutex);
2531
2532 }
2533
2534 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf);
2535
2536 static void iwl_bg_scan_completed(struct work_struct *work)
2537 {
2538         struct iwl_priv *priv =
2539             container_of(work, struct iwl_priv, scan_completed);
2540
2541         IWL_DEBUG_SCAN("SCAN complete scan\n");
2542
2543         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2544                 return;
2545
2546         if (test_bit(STATUS_CONF_PENDING, &priv->status))
2547                 iwl4965_mac_config(priv->hw, ieee80211_get_hw_conf(priv->hw));
2548
2549         ieee80211_scan_completed(priv->hw);
2550
2551         /* Since setting the TXPOWER may have been deferred while
2552          * performing the scan, fire one off */
2553         mutex_lock(&priv->mutex);
2554         iwl_set_tx_power(priv, priv->tx_power_user_lmt, true);
2555         mutex_unlock(&priv->mutex);
2556 }
2557
2558 /*****************************************************************************
2559  *
2560  * mac80211 entry point functions
2561  *
2562  *****************************************************************************/
2563
2564 #define UCODE_READY_TIMEOUT     (4 * HZ)
2565
2566 static int iwl4965_mac_start(struct ieee80211_hw *hw)
2567 {
2568         struct iwl_priv *priv = hw->priv;
2569         int ret;
2570
2571         IWL_DEBUG_MAC80211("enter\n");
2572
2573         if (pci_enable_device(priv->pci_dev)) {
2574                 IWL_ERROR("Fail to pci_enable_device\n");
2575                 return -ENODEV;
2576         }
2577         pci_restore_state(priv->pci_dev);
2578         pci_enable_msi(priv->pci_dev);
2579
2580         ret = request_irq(priv->pci_dev->irq, iwl4965_isr, IRQF_SHARED,
2581                           DRV_NAME, priv);
2582         if (ret) {
2583                 IWL_ERROR("Error allocating IRQ %d\n", priv->pci_dev->irq);
2584                 goto out_disable_msi;
2585         }
2586
2587         /* we should be verifying the device is ready to be opened */
2588         mutex_lock(&priv->mutex);
2589
2590         memset(&priv->staging_rxon, 0, sizeof(struct iwl_rxon_cmd));
2591         /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
2592          * ucode filename and max sizes are card-specific. */
2593
2594         if (!priv->ucode_code.len) {
2595                 ret = iwl4965_read_ucode(priv);
2596                 if (ret) {
2597                         IWL_ERROR("Could not read microcode: %d\n", ret);
2598                         mutex_unlock(&priv->mutex);
2599                         goto out_release_irq;
2600                 }
2601         }
2602
2603         ret = __iwl4965_up(priv);
2604
2605         mutex_unlock(&priv->mutex);
2606
2607         if (ret)
2608                 goto out_release_irq;
2609
2610         IWL_DEBUG_INFO("Start UP work done.\n");
2611
2612         if (test_bit(STATUS_IN_SUSPEND, &priv->status))
2613                 return 0;
2614
2615         /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
2616          * mac80211 will not be run successfully. */
2617         ret = wait_event_interruptible_timeout(priv->wait_command_queue,
2618                         test_bit(STATUS_READY, &priv->status),
2619                         UCODE_READY_TIMEOUT);
2620         if (!ret) {
2621                 if (!test_bit(STATUS_READY, &priv->status)) {
2622                         IWL_ERROR("START_ALIVE timeout after %dms.\n",
2623                                 jiffies_to_msecs(UCODE_READY_TIMEOUT));
2624                         ret = -ETIMEDOUT;
2625                         goto out_release_irq;
2626                 }
2627         }
2628
2629         priv->is_open = 1;
2630         IWL_DEBUG_MAC80211("leave\n");
2631         return 0;
2632
2633 out_release_irq:
2634         free_irq(priv->pci_dev->irq, priv);
2635 out_disable_msi:
2636         pci_disable_msi(priv->pci_dev);
2637         pci_disable_device(priv->pci_dev);
2638         priv->is_open = 0;
2639         IWL_DEBUG_MAC80211("leave - failed\n");
2640         return ret;
2641 }
2642
2643 static void iwl4965_mac_stop(struct ieee80211_hw *hw)
2644 {
2645         struct iwl_priv *priv = hw->priv;
2646
2647         IWL_DEBUG_MAC80211("enter\n");
2648
2649         if (!priv->is_open) {
2650                 IWL_DEBUG_MAC80211("leave - skip\n");
2651                 return;
2652         }
2653
2654         priv->is_open = 0;
2655
2656         if (iwl_is_ready_rf(priv)) {
2657                 /* stop mac, cancel any scan request and clear
2658                  * RXON_FILTER_ASSOC_MSK BIT
2659                  */
2660                 mutex_lock(&priv->mutex);
2661                 iwl_scan_cancel_timeout(priv, 100);
2662                 cancel_delayed_work(&priv->post_associate);
2663                 mutex_unlock(&priv->mutex);
2664         }
2665
2666         iwl4965_down(priv);
2667
2668         flush_workqueue(priv->workqueue);
2669         free_irq(priv->pci_dev->irq, priv);
2670         pci_disable_msi(priv->pci_dev);
2671         pci_save_state(priv->pci_dev);
2672         pci_disable_device(priv->pci_dev);
2673
2674         IWL_DEBUG_MAC80211("leave\n");
2675 }
2676
2677 static int iwl4965_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
2678 {
2679         struct iwl_priv *priv = hw->priv;
2680
2681         IWL_DEBUG_MAC80211("enter\n");
2682
2683         if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
2684                 IWL_DEBUG_MAC80211("leave - monitor\n");
2685                 return -1;
2686         }
2687
2688         IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
2689                      ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
2690
2691         if (iwl_tx_skb(priv, skb))
2692                 dev_kfree_skb_any(skb);
2693
2694         IWL_DEBUG_MAC80211("leave\n");
2695         return 0;
2696 }
2697
2698 static int iwl4965_mac_add_interface(struct ieee80211_hw *hw,
2699                                  struct ieee80211_if_init_conf *conf)
2700 {
2701         struct iwl_priv *priv = hw->priv;
2702         unsigned long flags;
2703         DECLARE_MAC_BUF(mac);
2704
2705         IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
2706
2707         if (priv->vif) {
2708                 IWL_DEBUG_MAC80211("leave - vif != NULL\n");
2709                 return -EOPNOTSUPP;
2710         }
2711
2712         spin_lock_irqsave(&priv->lock, flags);
2713         priv->vif = conf->vif;
2714
2715         spin_unlock_irqrestore(&priv->lock, flags);
2716
2717         mutex_lock(&priv->mutex);
2718
2719         if (conf->mac_addr) {
2720                 IWL_DEBUG_MAC80211("Set %s\n", print_mac(mac, conf->mac_addr));
2721                 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
2722         }
2723
2724         if (iwl4965_set_mode(priv, conf->type) == -EAGAIN)
2725                 /* we are not ready, will run again when ready */
2726                 set_bit(STATUS_MODE_PENDING, &priv->status);
2727
2728         mutex_unlock(&priv->mutex);
2729
2730         IWL_DEBUG_MAC80211("leave\n");
2731         return 0;
2732 }
2733
2734 /**
2735  * iwl4965_mac_config - mac80211 config callback
2736  *
2737  * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
2738  * be set inappropriately and the driver currently sets the hardware up to
2739  * use it whenever needed.
2740  */
2741 static int iwl4965_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
2742 {
2743         struct iwl_priv *priv = hw->priv;
2744         const struct iwl_channel_info *ch_info;
2745         unsigned long flags;
2746         int ret = 0;
2747         u16 channel;
2748
2749         mutex_lock(&priv->mutex);
2750         IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
2751
2752         priv->add_radiotap = !!(conf->flags & IEEE80211_CONF_RADIOTAP);
2753
2754         if (conf->radio_enabled && iwl_radio_kill_sw_enable_radio(priv)) {
2755                 IWL_DEBUG_MAC80211("leave - RF-KILL - waiting for uCode\n");
2756                 goto out;
2757         }
2758
2759         if (!conf->radio_enabled)
2760                 iwl_radio_kill_sw_disable_radio(priv);
2761
2762         if (!iwl_is_ready(priv)) {
2763                 IWL_DEBUG_MAC80211("leave - not ready\n");
2764                 ret = -EIO;
2765                 goto out;
2766         }
2767
2768         if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
2769                      test_bit(STATUS_SCANNING, &priv->status))) {
2770                 IWL_DEBUG_MAC80211("leave - scanning\n");
2771                 set_bit(STATUS_CONF_PENDING, &priv->status);
2772                 mutex_unlock(&priv->mutex);
2773                 return 0;
2774         }
2775
2776         channel = ieee80211_frequency_to_channel(conf->channel->center_freq);
2777         ch_info = iwl_get_channel_info(priv, conf->channel->band, channel);
2778         if (!is_channel_valid(ch_info)) {
2779                 IWL_DEBUG_MAC80211("leave - invalid channel\n");
2780                 ret = -EINVAL;
2781                 goto out;
2782         }
2783
2784         if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS &&
2785             !is_channel_ibss(ch_info)) {
2786                 IWL_ERROR("channel %d in band %d not IBSS channel\n",
2787                         conf->channel->hw_value, conf->channel->band);
2788                 ret = -EINVAL;
2789                 goto out;
2790         }
2791
2792         spin_lock_irqsave(&priv->lock, flags);
2793
2794         /* if we are switching from ht to 2.4 clear flags
2795          * from any ht related info since 2.4 does not
2796          * support ht */
2797         if ((le16_to_cpu(priv->staging_rxon.channel) != channel)
2798 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
2799             && !(conf->flags & IEEE80211_CONF_CHANNEL_SWITCH)
2800 #endif
2801         )
2802                 priv->staging_rxon.flags = 0;
2803
2804         iwl_set_rxon_channel(priv, conf->channel->band, channel);
2805
2806         iwl_set_flags_for_band(priv, conf->channel->band);
2807
2808         /* The list of supported rates and rate mask can be different
2809          * for each band; since the band may have changed, reset
2810          * the rate mask to what mac80211 lists */
2811         iwl4965_set_rate(priv);
2812
2813         spin_unlock_irqrestore(&priv->lock, flags);
2814
2815 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
2816         if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
2817                 iwl4965_hw_channel_switch(priv, conf->channel);
2818                 goto out;
2819         }
2820 #endif
2821
2822         if (!conf->radio_enabled) {
2823                 IWL_DEBUG_MAC80211("leave - radio disabled\n");
2824                 goto out;
2825         }
2826
2827         if (iwl_is_rfkill(priv)) {
2828                 IWL_DEBUG_MAC80211("leave - RF kill\n");
2829                 ret = -EIO;
2830                 goto out;
2831         }
2832
2833         IWL_DEBUG_MAC80211("TX Power old=%d new=%d\n",
2834                            priv->tx_power_user_lmt, conf->power_level);
2835
2836         iwl_set_tx_power(priv, conf->power_level, false);
2837
2838         iwl4965_set_rate(priv);
2839
2840         if (memcmp(&priv->active_rxon,
2841                    &priv->staging_rxon, sizeof(priv->staging_rxon)))
2842                 iwl4965_commit_rxon(priv);
2843         else
2844                 IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
2845
2846         IWL_DEBUG_MAC80211("leave\n");
2847
2848 out:
2849         clear_bit(STATUS_CONF_PENDING, &priv->status);
2850         mutex_unlock(&priv->mutex);
2851         return ret;
2852 }
2853
2854 static void iwl4965_config_ap(struct iwl_priv *priv)
2855 {
2856         int ret = 0;
2857
2858         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2859                 return;
2860
2861         /* The following should be done only at AP bring up */
2862         if (!(iwl_is_associated(priv))) {
2863
2864                 /* RXON - unassoc (to set timing command) */
2865                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2866                 iwl4965_commit_rxon(priv);
2867
2868                 /* RXON Timing */
2869                 memset(&priv->rxon_timing, 0, sizeof(struct iwl4965_rxon_time_cmd));
2870                 iwl4965_setup_rxon_timing(priv);
2871                 ret = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
2872                                 sizeof(priv->rxon_timing), &priv->rxon_timing);
2873                 if (ret)
2874                         IWL_WARNING("REPLY_RXON_TIMING failed - "
2875                                         "Attempting to continue.\n");
2876
2877                 iwl_set_rxon_chain(priv);
2878
2879                 /* FIXME: what should be the assoc_id for AP? */
2880                 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
2881                 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
2882                         priv->staging_rxon.flags |=
2883                                 RXON_FLG_SHORT_PREAMBLE_MSK;
2884                 else
2885                         priv->staging_rxon.flags &=
2886                                 ~RXON_FLG_SHORT_PREAMBLE_MSK;
2887
2888                 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
2889                         if (priv->assoc_capability &
2890                                 WLAN_CAPABILITY_SHORT_SLOT_TIME)
2891                                 priv->staging_rxon.flags |=
2892                                         RXON_FLG_SHORT_SLOT_MSK;
2893                         else
2894                                 priv->staging_rxon.flags &=
2895                                         ~RXON_FLG_SHORT_SLOT_MSK;
2896
2897                         if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
2898                                 priv->staging_rxon.flags &=
2899                                         ~RXON_FLG_SHORT_SLOT_MSK;
2900                 }
2901                 /* restore RXON assoc */
2902                 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
2903                 iwl4965_commit_rxon(priv);
2904                 iwl4965_activate_qos(priv, 1);
2905                 iwl_rxon_add_station(priv, iwl_bcast_addr, 0);
2906         }
2907         iwl4965_send_beacon_cmd(priv);
2908
2909         /* FIXME - we need to add code here to detect a totally new
2910          * configuration, reset the AP, unassoc, rxon timing, assoc,
2911          * clear sta table, add BCAST sta... */
2912 }
2913
2914 static int iwl4965_mac_config_interface(struct ieee80211_hw *hw,
2915                                         struct ieee80211_vif *vif,
2916                                     struct ieee80211_if_conf *conf)
2917 {
2918         struct iwl_priv *priv = hw->priv;
2919         DECLARE_MAC_BUF(mac);
2920         unsigned long flags;
2921         int rc;
2922
2923         if (conf == NULL)
2924                 return -EIO;
2925
2926         if (priv->vif != vif) {
2927                 IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
2928                 return 0;
2929         }
2930
2931         if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
2932             (!conf->beacon || !conf->ssid_len)) {
2933                 IWL_DEBUG_MAC80211
2934                     ("Leaving in AP mode because HostAPD is not ready.\n");
2935                 return 0;
2936         }
2937
2938         if (!iwl_is_alive(priv))
2939                 return -EAGAIN;
2940
2941         mutex_lock(&priv->mutex);
2942
2943         if (conf->bssid)
2944                 IWL_DEBUG_MAC80211("bssid: %s\n",
2945                                    print_mac(mac, conf->bssid));
2946
2947 /*
2948  * very dubious code was here; the probe filtering flag is never set:
2949  *
2950         if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
2951             !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
2952  */
2953
2954         if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
2955                 if (!conf->bssid) {
2956                         conf->bssid = priv->mac_addr;
2957                         memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
2958                         IWL_DEBUG_MAC80211("bssid was set to: %s\n",
2959                                            print_mac(mac, conf->bssid));
2960                 }
2961                 if (priv->ibss_beacon)
2962                         dev_kfree_skb(priv->ibss_beacon);
2963
2964                 priv->ibss_beacon = conf->beacon;
2965         }
2966
2967         if (iwl_is_rfkill(priv))
2968                 goto done;
2969
2970         if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
2971             !is_multicast_ether_addr(conf->bssid)) {
2972                 /* If there is currently a HW scan going on in the background
2973                  * then we need to cancel it else the RXON below will fail. */
2974                 if (iwl_scan_cancel_timeout(priv, 100)) {
2975                         IWL_WARNING("Aborted scan still in progress "
2976                                     "after 100ms\n");
2977                         IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
2978                         mutex_unlock(&priv->mutex);
2979                         return -EAGAIN;
2980                 }
2981                 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
2982
2983                 /* TODO: Audit driver for usage of these members and see
2984                  * if mac80211 deprecates them (priv->bssid looks like it
2985                  * shouldn't be there, but I haven't scanned the IBSS code
2986                  * to verify) - jpk */
2987                 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
2988
2989                 if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
2990                         iwl4965_config_ap(priv);
2991                 else {
2992                         rc = iwl4965_commit_rxon(priv);
2993                         if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
2994                                 iwl_rxon_add_station(
2995                                         priv, priv->active_rxon.bssid_addr, 1);
2996                 }
2997
2998         } else {
2999                 iwl_scan_cancel_timeout(priv, 100);
3000                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3001                 iwl4965_commit_rxon(priv);
3002         }
3003
3004  done:
3005         spin_lock_irqsave(&priv->lock, flags);
3006         if (!conf->ssid_len)
3007                 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
3008         else
3009                 memcpy(priv->essid, conf->ssid, conf->ssid_len);
3010
3011         priv->essid_len = conf->ssid_len;
3012         spin_unlock_irqrestore(&priv->lock, flags);
3013
3014         IWL_DEBUG_MAC80211("leave\n");
3015         mutex_unlock(&priv->mutex);
3016
3017         return 0;
3018 }
3019
3020 static void iwl4965_configure_filter(struct ieee80211_hw *hw,
3021                                  unsigned int changed_flags,
3022                                  unsigned int *total_flags,
3023                                  int mc_count, struct dev_addr_list *mc_list)
3024 {
3025         struct iwl_priv *priv = hw->priv;
3026
3027         if (changed_flags & (*total_flags) & FIF_OTHER_BSS) {
3028                 IWL_DEBUG_MAC80211("Enter: type %d (0x%x, 0x%x)\n",
3029                                    IEEE80211_IF_TYPE_MNTR,
3030                                    changed_flags, *total_flags);
3031                 /* queue work 'cuz mac80211 is holding a lock which
3032                  * prevents us from issuing (synchronous) f/w cmds */
3033                 queue_work(priv->workqueue, &priv->set_monitor);
3034         }
3035         *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI |
3036                         FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
3037 }
3038
3039 static void iwl4965_mac_remove_interface(struct ieee80211_hw *hw,
3040                                      struct ieee80211_if_init_conf *conf)
3041 {
3042         struct iwl_priv *priv = hw->priv;
3043
3044         IWL_DEBUG_MAC80211("enter\n");
3045
3046         mutex_lock(&priv->mutex);
3047
3048         if (iwl_is_ready_rf(priv)) {
3049                 iwl_scan_cancel_timeout(priv, 100);
3050                 cancel_delayed_work(&priv->post_associate);
3051                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3052                 iwl4965_commit_rxon(priv);
3053         }
3054         if (priv->vif == conf->vif) {
3055                 priv->vif = NULL;
3056                 memset(priv->bssid, 0, ETH_ALEN);
3057                 memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
3058                 priv->essid_len = 0;
3059         }
3060         mutex_unlock(&priv->mutex);
3061
3062         IWL_DEBUG_MAC80211("leave\n");
3063
3064 }
3065
3066 #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
3067 static void iwl4965_bss_info_changed(struct ieee80211_hw *hw,
3068                                      struct ieee80211_vif *vif,
3069                                      struct ieee80211_bss_conf *bss_conf,
3070                                      u32 changes)
3071 {
3072         struct iwl_priv *priv = hw->priv;
3073
3074         IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
3075
3076         if (changes & BSS_CHANGED_ERP_PREAMBLE) {
3077                 IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
3078                                    bss_conf->use_short_preamble);
3079                 if (bss_conf->use_short_preamble)
3080                         priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3081                 else
3082                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3083         }
3084
3085         if (changes & BSS_CHANGED_ERP_CTS_PROT) {
3086                 IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
3087                 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
3088                         priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
3089                 else
3090                         priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
3091         }
3092
3093         if (changes & BSS_CHANGED_HT) {
3094                 IWL_DEBUG_MAC80211("HT %d\n", bss_conf->assoc_ht);
3095                 iwl4965_ht_conf(priv, bss_conf);
3096                 iwl_set_rxon_chain(priv);
3097         }
3098
3099         if (changes & BSS_CHANGED_ASSOC) {
3100                 IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
3101                 /* This should never happen as this function should
3102                  * never be called from interrupt context. */
3103                 if (WARN_ON_ONCE(in_interrupt()))
3104                         return;
3105                 if (bss_conf->assoc) {
3106                         priv->assoc_id = bss_conf->aid;
3107                         priv->beacon_int = bss_conf->beacon_int;
3108                         priv->timestamp = bss_conf->timestamp;
3109                         priv->assoc_capability = bss_conf->assoc_capability;
3110                         priv->next_scan_jiffies = jiffies +
3111                                         IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
3112                         mutex_lock(&priv->mutex);
3113                         iwl4965_post_associate(priv);
3114                         mutex_unlock(&priv->mutex);
3115                 } else {
3116                         priv->assoc_id = 0;
3117                         IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
3118                 }
3119         } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
3120                         IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
3121                         iwl_send_rxon_assoc(priv);
3122         }
3123
3124 }
3125
3126 static int iwl4965_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
3127 {
3128         int rc = 0;
3129         unsigned long flags;
3130         struct iwl_priv *priv = hw->priv;
3131
3132         IWL_DEBUG_MAC80211("enter\n");
3133
3134         mutex_lock(&priv->mutex);
3135         spin_lock_irqsave(&priv->lock, flags);
3136
3137         if (!iwl_is_ready_rf(priv)) {
3138                 rc = -EIO;
3139                 IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
3140                 goto out_unlock;
3141         }
3142
3143         if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {    /* APs don't scan */
3144                 rc = -EIO;
3145                 IWL_ERROR("ERROR: APs don't scan\n");
3146                 goto out_unlock;
3147         }
3148
3149         /* we don't schedule scan within next_scan_jiffies period */
3150         if (priv->next_scan_jiffies &&
3151                         time_after(priv->next_scan_jiffies, jiffies)) {
3152                 rc = -EAGAIN;
3153                 goto out_unlock;
3154         }
3155         /* if we just finished scan ask for delay */
3156         if (priv->last_scan_jiffies && time_after(priv->last_scan_jiffies +
3157                                 IWL_DELAY_NEXT_SCAN, jiffies)) {
3158                 rc = -EAGAIN;
3159                 goto out_unlock;
3160         }
3161         if (len) {
3162                 IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
3163                                iwl_escape_essid(ssid, len), (int)len);
3164
3165                 priv->one_direct_scan = 1;
3166                 priv->direct_ssid_len = (u8)
3167                     min((u8) len, (u8) IW_ESSID_MAX_SIZE);
3168                 memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
3169         } else
3170                 priv->one_direct_scan = 0;
3171
3172         rc = iwl_scan_initiate(priv);
3173
3174         IWL_DEBUG_MAC80211("leave\n");
3175
3176 out_unlock:
3177         spin_unlock_irqrestore(&priv->lock, flags);
3178         mutex_unlock(&priv->mutex);
3179
3180         return rc;
3181 }
3182
3183 static void iwl4965_mac_update_tkip_key(struct ieee80211_hw *hw,
3184                         struct ieee80211_key_conf *keyconf, const u8 *addr,
3185                         u32 iv32, u16 *phase1key)
3186 {
3187         struct iwl_priv *priv = hw->priv;
3188         u8 sta_id = IWL_INVALID_STATION;
3189         unsigned long flags;
3190         __le16 key_flags = 0;
3191         int i;
3192         DECLARE_MAC_BUF(mac);
3193
3194         IWL_DEBUG_MAC80211("enter\n");
3195
3196         sta_id = iwl_find_station(priv, addr);
3197         if (sta_id == IWL_INVALID_STATION) {
3198                 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
3199                                    print_mac(mac, addr));
3200                 return;
3201         }
3202
3203         iwl_scan_cancel_timeout(priv, 100);
3204
3205         key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
3206         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
3207         key_flags &= ~STA_KEY_FLG_INVALID;
3208
3209         if (sta_id == priv->hw_params.bcast_sta_id)
3210                 key_flags |= STA_KEY_MULTICAST_MSK;
3211
3212         spin_lock_irqsave(&priv->sta_lock, flags);
3213
3214         priv->stations[sta_id].sta.key.key_flags = key_flags;
3215         priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
3216
3217         for (i = 0; i < 5; i++)
3218                 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
3219                         cpu_to_le16(phase1key[i]);
3220
3221         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
3222         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
3223
3224         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
3225
3226         spin_unlock_irqrestore(&priv->sta_lock, flags);
3227
3228         IWL_DEBUG_MAC80211("leave\n");
3229 }
3230
3231 static int iwl4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
3232                            const u8 *local_addr, const u8 *addr,
3233                            struct ieee80211_key_conf *key)
3234 {
3235         struct iwl_priv *priv = hw->priv;
3236         DECLARE_MAC_BUF(mac);
3237         int ret = 0;
3238         u8 sta_id = IWL_INVALID_STATION;
3239         u8 is_default_wep_key = 0;
3240
3241         IWL_DEBUG_MAC80211("enter\n");
3242
3243         if (priv->hw_params.sw_crypto) {
3244                 IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
3245                 return -EOPNOTSUPP;
3246         }
3247
3248         if (is_zero_ether_addr(addr))
3249                 /* only support pairwise keys */
3250                 return -EOPNOTSUPP;
3251
3252         sta_id = iwl_find_station(priv, addr);
3253         if (sta_id == IWL_INVALID_STATION) {
3254                 IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
3255                                    print_mac(mac, addr));
3256                 return -EINVAL;
3257
3258         }
3259
3260         mutex_lock(&priv->mutex);
3261         iwl_scan_cancel_timeout(priv, 100);
3262         mutex_unlock(&priv->mutex);
3263
3264         /* If we are getting WEP group key and we didn't receive any key mapping
3265          * so far, we are in legacy wep mode (group key only), otherwise we are
3266          * in 1X mode.
3267          * In legacy wep mode, we use another host command to the uCode */
3268         if (key->alg == ALG_WEP && sta_id == priv->hw_params.bcast_sta_id &&
3269                 priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3270                 if (cmd == SET_KEY)
3271                         is_default_wep_key = !priv->key_mapping_key;
3272                 else
3273                         is_default_wep_key =
3274                                         (key->hw_key_idx == HW_KEY_DEFAULT);
3275         }
3276
3277         switch (cmd) {
3278         case SET_KEY:
3279                 if (is_default_wep_key)
3280                         ret = iwl_set_default_wep_key(priv, key);
3281                 else
3282                         ret = iwl_set_dynamic_key(priv, key, sta_id);
3283
3284                 IWL_DEBUG_MAC80211("enable hwcrypto key\n");
3285                 break;
3286         case DISABLE_KEY:
3287                 if (is_default_wep_key)
3288                         ret = iwl_remove_default_wep_key(priv, key);
3289                 else
3290                         ret = iwl_remove_dynamic_key(priv, key, sta_id);
3291
3292                 IWL_DEBUG_MAC80211("disable hwcrypto key\n");
3293                 break;
3294         default:
3295                 ret = -EINVAL;
3296         }
3297
3298         IWL_DEBUG_MAC80211("leave\n");
3299
3300         return ret;
3301 }
3302
3303 static int iwl4965_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
3304                            const struct ieee80211_tx_queue_params *params)
3305 {
3306         struct iwl_priv *priv = hw->priv;
3307         unsigned long flags;
3308         int q;
3309
3310         IWL_DEBUG_MAC80211("enter\n");
3311
3312         if (!iwl_is_ready_rf(priv)) {
3313                 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3314                 return -EIO;
3315         }
3316
3317         if (queue >= AC_NUM) {
3318                 IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
3319                 return 0;
3320         }
3321
3322         if (!priv->qos_data.qos_enable) {
3323                 priv->qos_data.qos_active = 0;
3324                 IWL_DEBUG_MAC80211("leave - qos not enabled\n");
3325                 return 0;
3326         }
3327         q = AC_NUM - 1 - queue;
3328
3329         spin_lock_irqsave(&priv->lock, flags);
3330
3331         priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
3332         priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
3333         priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
3334         priv->qos_data.def_qos_parm.ac[q].edca_txop =
3335                         cpu_to_le16((params->txop * 32));
3336
3337         priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
3338         priv->qos_data.qos_active = 1;
3339
3340         spin_unlock_irqrestore(&priv->lock, flags);
3341
3342         mutex_lock(&priv->mutex);
3343         if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
3344                 iwl4965_activate_qos(priv, 1);
3345         else if (priv->assoc_id && iwl_is_associated(priv))
3346                 iwl4965_activate_qos(priv, 0);
3347
3348         mutex_unlock(&priv->mutex);
3349
3350         IWL_DEBUG_MAC80211("leave\n");
3351         return 0;
3352 }
3353
3354 static int iwl4965_mac_get_tx_stats(struct ieee80211_hw *hw,
3355                                 struct ieee80211_tx_queue_stats *stats)
3356 {
3357         struct iwl_priv *priv = hw->priv;
3358         int i, avail;
3359         struct iwl_tx_queue *txq;
3360         struct iwl_queue *q;
3361         unsigned long flags;
3362
3363         IWL_DEBUG_MAC80211("enter\n");
3364
3365         if (!iwl_is_ready_rf(priv)) {
3366                 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3367                 return -EIO;
3368         }
3369
3370         spin_lock_irqsave(&priv->lock, flags);
3371
3372         for (i = 0; i < AC_NUM; i++) {
3373                 txq = &priv->txq[i];
3374                 q = &txq->q;
3375                 avail = iwl_queue_space(q);
3376
3377                 stats[i].len = q->n_window - avail;
3378                 stats[i].limit = q->n_window - q->high_mark;
3379                 stats[i].count = q->n_window;
3380
3381         }
3382         spin_unlock_irqrestore(&priv->lock, flags);
3383
3384         IWL_DEBUG_MAC80211("leave\n");
3385
3386         return 0;
3387 }
3388
3389 static int iwl4965_mac_get_stats(struct ieee80211_hw *hw,
3390                              struct ieee80211_low_level_stats *stats)
3391 {
3392         struct iwl_priv *priv = hw->priv;
3393
3394         priv = hw->priv;
3395         IWL_DEBUG_MAC80211("enter\n");
3396         IWL_DEBUG_MAC80211("leave\n");
3397
3398         return 0;
3399 }
3400
3401 static void iwl4965_mac_reset_tsf(struct ieee80211_hw *hw)
3402 {
3403         struct iwl_priv *priv = hw->priv;
3404         unsigned long flags;
3405
3406         mutex_lock(&priv->mutex);
3407         IWL_DEBUG_MAC80211("enter\n");
3408
3409         spin_lock_irqsave(&priv->lock, flags);
3410         memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_info));
3411         spin_unlock_irqrestore(&priv->lock, flags);
3412
3413         iwl_reset_qos(priv);
3414
3415         cancel_delayed_work(&priv->post_associate);
3416
3417         spin_lock_irqsave(&priv->lock, flags);
3418         priv->assoc_id = 0;
3419         priv->assoc_capability = 0;
3420         priv->assoc_station_added = 0;
3421
3422         /* new association get rid of ibss beacon skb */
3423         if (priv->ibss_beacon)
3424                 dev_kfree_skb(priv->ibss_beacon);
3425
3426         priv->ibss_beacon = NULL;
3427
3428         priv->beacon_int = priv->hw->conf.beacon_int;
3429         priv->timestamp = 0;
3430         if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
3431                 priv->beacon_int = 0;
3432
3433         spin_unlock_irqrestore(&priv->lock, flags);
3434
3435         if (!iwl_is_ready_rf(priv)) {
3436                 IWL_DEBUG_MAC80211("leave - not ready\n");
3437                 mutex_unlock(&priv->mutex);
3438                 return;
3439         }
3440
3441         /* we are restarting association process
3442          * clear RXON_FILTER_ASSOC_MSK bit
3443          */
3444         if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
3445                 iwl_scan_cancel_timeout(priv, 100);
3446                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3447                 iwl4965_commit_rxon(priv);
3448         }
3449
3450         iwl_power_update_mode(priv, 0);
3451
3452         /* Per mac80211.h: This is only used in IBSS mode... */
3453         if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
3454
3455                 IWL_DEBUG_MAC80211("leave - not in IBSS\n");
3456                 mutex_unlock(&priv->mutex);
3457                 return;
3458         }
3459
3460         iwl4965_set_rate(priv);
3461
3462         mutex_unlock(&priv->mutex);
3463
3464         IWL_DEBUG_MAC80211("leave\n");
3465 }
3466
3467 static int iwl4965_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
3468 {
3469         struct iwl_priv *priv = hw->priv;
3470         unsigned long flags;
3471         __le64 timestamp;
3472
3473         mutex_lock(&priv->mutex);
3474         IWL_DEBUG_MAC80211("enter\n");
3475
3476         if (!iwl_is_ready_rf(priv)) {
3477                 IWL_DEBUG_MAC80211("leave - RF not ready\n");
3478                 mutex_unlock(&priv->mutex);
3479                 return -EIO;
3480         }
3481
3482         if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
3483                 IWL_DEBUG_MAC80211("leave - not IBSS\n");
3484                 mutex_unlock(&priv->mutex);
3485                 return -EIO;
3486         }
3487
3488         spin_lock_irqsave(&priv->lock, flags);
3489
3490         if (priv->ibss_beacon)
3491                 dev_kfree_skb(priv->ibss_beacon);
3492
3493         priv->ibss_beacon = skb;
3494
3495         priv->assoc_id = 0;
3496         timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
3497         priv->timestamp = le64_to_cpu(timestamp) +  (priv->beacon_int * 1000);
3498
3499         IWL_DEBUG_MAC80211("leave\n");
3500         spin_unlock_irqrestore(&priv->lock, flags);
3501
3502         iwl_reset_qos(priv);
3503
3504         iwl4965_post_associate(priv);
3505
3506         mutex_unlock(&priv->mutex);
3507
3508         return 0;
3509 }
3510
3511 /*****************************************************************************
3512  *
3513  * sysfs attributes
3514  *
3515  *****************************************************************************/
3516
3517 #ifdef CONFIG_IWLWIFI_DEBUG
3518
3519 /*
3520  * The following adds a new attribute to the sysfs representation
3521  * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
3522  * used for controlling the debug level.
3523  *
3524  * See the level definitions in iwl for details.
3525  */
3526
3527 static ssize_t show_debug_level(struct device *d,
3528                                 struct device_attribute *attr, char *buf)
3529 {
3530         struct iwl_priv *priv = d->driver_data;
3531
3532         return sprintf(buf, "0x%08X\n", priv->debug_level);
3533 }
3534 static ssize_t store_debug_level(struct device *d,
3535                                 struct device_attribute *attr,
3536                                  const char *buf, size_t count)
3537 {
3538         struct iwl_priv *priv = d->driver_data;
3539         char *p = (char *)buf;
3540         u32 val;
3541
3542         val = simple_strtoul(p, &p, 0);
3543         if (p == buf)
3544                 printk(KERN_INFO DRV_NAME
3545                        ": %s is not in hex or decimal form.\n", buf);
3546         else
3547                 priv->debug_level = val;
3548
3549         return strnlen(buf, count);
3550 }
3551
3552 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
3553                         show_debug_level, store_debug_level);
3554
3555
3556 #endif /* CONFIG_IWLWIFI_DEBUG */
3557
3558
3559 static ssize_t show_version(struct device *d,
3560                                 struct device_attribute *attr, char *buf)
3561 {
3562         struct iwl_priv *priv = d->driver_data;
3563         struct iwl_alive_resp *palive = &priv->card_alive;
3564         ssize_t pos = 0;
3565         u16 eeprom_ver;
3566
3567         if (palive->is_valid)
3568                 pos += sprintf(buf + pos,
3569                                 "fw version: 0x%01X.0x%01X.0x%01X.0x%01X\n"
3570                                 "fw type: 0x%01X 0x%01X\n",
3571                                 palive->ucode_major, palive->ucode_minor,
3572                                 palive->sw_rev[0], palive->sw_rev[1],
3573                                 palive->ver_type, palive->ver_subtype);
3574         else
3575                 pos += sprintf(buf + pos, "fw not loaded\n");
3576
3577         if (priv->eeprom) {
3578                 eeprom_ver = iwl_eeprom_query16(priv, EEPROM_VERSION);
3579                 pos += sprintf(buf + pos, "EEPROM version: 0x%x\n",
3580                                  eeprom_ver);
3581         } else {
3582                 pos += sprintf(buf + pos, "EEPROM not initialzed\n");
3583         }
3584
3585         return pos;
3586 }
3587
3588 static DEVICE_ATTR(version, S_IWUSR | S_IRUGO, show_version, NULL);
3589
3590 static ssize_t show_temperature(struct device *d,
3591                                 struct device_attribute *attr, char *buf)
3592 {
3593         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3594
3595         if (!iwl_is_alive(priv))
3596                 return -EAGAIN;
3597
3598         return sprintf(buf, "%d\n", priv->temperature);
3599 }
3600
3601 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
3602
3603 static ssize_t show_rs_window(struct device *d,
3604                               struct device_attribute *attr,
3605                               char *buf)
3606 {
3607         struct iwl_priv *priv = d->driver_data;
3608         return iwl4965_fill_rs_info(priv->hw, buf, IWL_AP_ID);
3609 }
3610 static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
3611
3612 static ssize_t show_tx_power(struct device *d,
3613                              struct device_attribute *attr, char *buf)
3614 {
3615         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3616         return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
3617 }
3618
3619 static ssize_t store_tx_power(struct device *d,
3620                               struct device_attribute *attr,
3621                               const char *buf, size_t count)
3622 {
3623         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3624         char *p = (char *)buf;
3625         u32 val;
3626
3627         val = simple_strtoul(p, &p, 10);
3628         if (p == buf)
3629                 printk(KERN_INFO DRV_NAME
3630                        ": %s is not in decimal form.\n", buf);
3631         else
3632                 iwl_set_tx_power(priv, val, false);
3633
3634         return count;
3635 }
3636
3637 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
3638
3639 static ssize_t show_flags(struct device *d,
3640                           struct device_attribute *attr, char *buf)
3641 {
3642         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3643
3644         return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
3645 }
3646
3647 static ssize_t store_flags(struct device *d,
3648                            struct device_attribute *attr,
3649                            const char *buf, size_t count)
3650 {
3651         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3652         u32 flags = simple_strtoul(buf, NULL, 0);
3653
3654         mutex_lock(&priv->mutex);
3655         if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
3656                 /* Cancel any currently running scans... */
3657                 if (iwl_scan_cancel_timeout(priv, 100))
3658                         IWL_WARNING("Could not cancel scan.\n");
3659                 else {
3660                         IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
3661                                        flags);
3662                         priv->staging_rxon.flags = cpu_to_le32(flags);
3663                         iwl4965_commit_rxon(priv);
3664                 }
3665         }
3666         mutex_unlock(&priv->mutex);
3667
3668         return count;
3669 }
3670
3671 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
3672
3673 static ssize_t show_filter_flags(struct device *d,
3674                                  struct device_attribute *attr, char *buf)
3675 {
3676         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3677
3678         return sprintf(buf, "0x%04X\n",
3679                 le32_to_cpu(priv->active_rxon.filter_flags));
3680 }
3681
3682 static ssize_t store_filter_flags(struct device *d,
3683                                   struct device_attribute *attr,
3684                                   const char *buf, size_t count)
3685 {
3686         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3687         u32 filter_flags = simple_strtoul(buf, NULL, 0);
3688
3689         mutex_lock(&priv->mutex);
3690         if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
3691                 /* Cancel any currently running scans... */
3692                 if (iwl_scan_cancel_timeout(priv, 100))
3693                         IWL_WARNING("Could not cancel scan.\n");
3694                 else {
3695                         IWL_DEBUG_INFO("Committing rxon.filter_flags = "
3696                                        "0x%04X\n", filter_flags);
3697                         priv->staging_rxon.filter_flags =
3698                                 cpu_to_le32(filter_flags);
3699                         iwl4965_commit_rxon(priv);
3700                 }
3701         }
3702         mutex_unlock(&priv->mutex);
3703
3704         return count;
3705 }
3706
3707 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
3708                    store_filter_flags);
3709
3710 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
3711
3712 static ssize_t show_measurement(struct device *d,
3713                                 struct device_attribute *attr, char *buf)
3714 {
3715         struct iwl_priv *priv = dev_get_drvdata(d);
3716         struct iwl4965_spectrum_notification measure_report;
3717         u32 size = sizeof(measure_report), len = 0, ofs = 0;
3718         u8 *data = (u8 *) & measure_report;
3719         unsigned long flags;
3720
3721         spin_lock_irqsave(&priv->lock, flags);
3722         if (!(priv->measurement_status & MEASUREMENT_READY)) {
3723                 spin_unlock_irqrestore(&priv->lock, flags);
3724                 return 0;
3725         }
3726         memcpy(&measure_report, &priv->measure_report, size);
3727         priv->measurement_status = 0;
3728         spin_unlock_irqrestore(&priv->lock, flags);
3729
3730         while (size && (PAGE_SIZE - len)) {
3731                 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3732                                    PAGE_SIZE - len, 1);
3733                 len = strlen(buf);
3734                 if (PAGE_SIZE - len)
3735                         buf[len++] = '\n';
3736
3737                 ofs += 16;
3738                 size -= min(size, 16U);
3739         }
3740
3741         return len;
3742 }
3743
3744 static ssize_t store_measurement(struct device *d,
3745                                  struct device_attribute *attr,
3746                                  const char *buf, size_t count)
3747 {
3748         struct iwl_priv *priv = dev_get_drvdata(d);
3749         struct ieee80211_measurement_params params = {
3750                 .channel = le16_to_cpu(priv->active_rxon.channel),
3751                 .start_time = cpu_to_le64(priv->last_tsf),
3752                 .duration = cpu_to_le16(1),
3753         };
3754         u8 type = IWL_MEASURE_BASIC;
3755         u8 buffer[32];
3756         u8 channel;
3757
3758         if (count) {
3759                 char *p = buffer;
3760                 strncpy(buffer, buf, min(sizeof(buffer), count));
3761                 channel = simple_strtoul(p, NULL, 0);
3762                 if (channel)
3763                         params.channel = channel;
3764
3765                 p = buffer;
3766                 while (*p && *p != ' ')
3767                         p++;
3768                 if (*p)
3769                         type = simple_strtoul(p + 1, NULL, 0);
3770         }
3771
3772         IWL_DEBUG_INFO("Invoking measurement of type %d on "
3773                        "channel %d (for '%s')\n", type, params.channel, buf);
3774         iwl4965_get_measurement(priv, &params, type);
3775
3776         return count;
3777 }
3778
3779 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
3780                    show_measurement, store_measurement);
3781 #endif /* CONFIG_IWL4965_SPECTRUM_MEASUREMENT */
3782
3783 static ssize_t store_retry_rate(struct device *d,
3784                                 struct device_attribute *attr,
3785                                 const char *buf, size_t count)
3786 {
3787         struct iwl_priv *priv = dev_get_drvdata(d);
3788
3789         priv->retry_rate = simple_strtoul(buf, NULL, 0);
3790         if (priv->retry_rate <= 0)
3791                 priv->retry_rate = 1;
3792
3793         return count;
3794 }
3795
3796 static ssize_t show_retry_rate(struct device *d,
3797                                struct device_attribute *attr, char *buf)
3798 {
3799         struct iwl_priv *priv = dev_get_drvdata(d);
3800         return sprintf(buf, "%d", priv->retry_rate);
3801 }
3802
3803 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
3804                    store_retry_rate);
3805
3806 static ssize_t store_power_level(struct device *d,
3807                                  struct device_attribute *attr,
3808                                  const char *buf, size_t count)
3809 {
3810         struct iwl_priv *priv = dev_get_drvdata(d);
3811         int rc;
3812         int mode;
3813
3814         mode = simple_strtoul(buf, NULL, 0);
3815         mutex_lock(&priv->mutex);
3816
3817         if (!iwl_is_ready(priv)) {
3818                 rc = -EAGAIN;
3819                 goto out;
3820         }
3821
3822         rc = iwl_power_set_user_mode(priv, mode);
3823         if (rc) {
3824                 IWL_DEBUG_MAC80211("failed setting power mode.\n");
3825                 goto out;
3826         }
3827         rc = count;
3828
3829  out:
3830         mutex_unlock(&priv->mutex);
3831         return rc;
3832 }
3833
3834 #define MAX_WX_STRING 80
3835
3836 /* Values are in microsecond */
3837 static const s32 timeout_duration[] = {
3838         350000,
3839         250000,
3840         75000,
3841         37000,
3842         25000,
3843 };
3844 static const s32 period_duration[] = {
3845         400000,
3846         700000,
3847         1000000,
3848         1000000,
3849         1000000
3850 };
3851
3852 static ssize_t show_power_level(struct device *d,
3853                                 struct device_attribute *attr, char *buf)
3854 {
3855         struct iwl_priv *priv = dev_get_drvdata(d);
3856         int level = priv->power_data.power_mode;
3857         char *p = buf;
3858
3859         p += sprintf(p, "%d ", level);
3860         switch (level) {
3861         case IWL_POWER_MODE_CAM:
3862         case IWL_POWER_AC:
3863                 p += sprintf(p, "(AC)");
3864                 break;
3865         case IWL_POWER_BATTERY:
3866                 p += sprintf(p, "(BATTERY)");
3867                 break;
3868         default:
3869                 p += sprintf(p,
3870                              "(Timeout %dms, Period %dms)",
3871                              timeout_duration[level - 1] / 1000,
3872                              period_duration[level - 1] / 1000);
3873         }
3874 /*
3875         if (!(priv->power_mode & IWL_POWER_ENABLED))
3876                 p += sprintf(p, " OFF\n");
3877         else
3878                 p += sprintf(p, " \n");
3879 */
3880         p += sprintf(p, " \n");
3881         return (p - buf + 1);
3882 }
3883
3884 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
3885                    store_power_level);
3886
3887 static ssize_t show_channels(struct device *d,
3888                              struct device_attribute *attr, char *buf)
3889 {
3890
3891         struct iwl_priv *priv = dev_get_drvdata(d);
3892         struct ieee80211_channel *channels = NULL;
3893         const struct ieee80211_supported_band *supp_band = NULL;
3894         int len = 0, i;
3895         int count = 0;
3896
3897         if (!test_bit(STATUS_GEO_CONFIGURED, &priv->status))
3898                 return -EAGAIN;
3899
3900         supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_2GHZ);
3901         channels = supp_band->channels;
3902         count = supp_band->n_channels;
3903
3904         len += sprintf(&buf[len],
3905                         "Displaying %d channels in 2.4GHz band "
3906                         "(802.11bg):\n", count);
3907
3908         for (i = 0; i < count; i++)
3909                 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
3910                                 ieee80211_frequency_to_channel(
3911                                 channels[i].center_freq),
3912                                 channels[i].max_power,
3913                                 channels[i].flags & IEEE80211_CHAN_RADAR ?
3914                                 " (IEEE 802.11h required)" : "",
3915                                 (!(channels[i].flags & IEEE80211_CHAN_NO_IBSS)
3916                                 || (channels[i].flags &
3917                                 IEEE80211_CHAN_RADAR)) ? "" :
3918                                 ", IBSS",
3919                                 channels[i].flags &
3920                                 IEEE80211_CHAN_PASSIVE_SCAN ?
3921                                 "passive only" : "active/passive");
3922
3923         supp_band = iwl_get_hw_mode(priv, IEEE80211_BAND_5GHZ);
3924         channels = supp_band->channels;
3925         count = supp_band->n_channels;
3926
3927         len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
3928                         "(802.11a):\n", count);
3929
3930         for (i = 0; i < count; i++)
3931                 len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
3932                                 ieee80211_frequency_to_channel(
3933                                 channels[i].center_freq),
3934                                 channels[i].max_power,
3935                                 channels[i].flags & IEEE80211_CHAN_RADAR ?
3936                                 " (IEEE 802.11h required)" : "",
3937                                 ((channels[i].flags & IEEE80211_CHAN_NO_IBSS)
3938                                 || (channels[i].flags &
3939                                 IEEE80211_CHAN_RADAR)) ? "" :
3940                                 ", IBSS",
3941                                 channels[i].flags &
3942                                 IEEE80211_CHAN_PASSIVE_SCAN ?
3943                                 "passive only" : "active/passive");
3944
3945         return len;
3946 }
3947
3948 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
3949
3950 static ssize_t show_statistics(struct device *d,
3951                                struct device_attribute *attr, char *buf)
3952 {
3953         struct iwl_priv *priv = dev_get_drvdata(d);
3954         u32 size = sizeof(struct iwl_notif_statistics);
3955         u32 len = 0, ofs = 0;
3956         u8 *data = (u8 *) & priv->statistics;
3957         int rc = 0;
3958
3959         if (!iwl_is_alive(priv))
3960                 return -EAGAIN;
3961
3962         mutex_lock(&priv->mutex);
3963         rc = iwl_send_statistics_request(priv, 0);
3964         mutex_unlock(&priv->mutex);
3965
3966         if (rc) {
3967                 len = sprintf(buf,
3968                               "Error sending statistics request: 0x%08X\n", rc);
3969                 return len;
3970         }
3971
3972         while (size && (PAGE_SIZE - len)) {
3973                 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
3974                                    PAGE_SIZE - len, 1);
3975                 len = strlen(buf);
3976                 if (PAGE_SIZE - len)
3977                         buf[len++] = '\n';
3978
3979                 ofs += 16;
3980                 size -= min(size, 16U);
3981         }
3982
3983         return len;
3984 }
3985
3986 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
3987
3988 static ssize_t show_status(struct device *d,
3989                            struct device_attribute *attr, char *buf)
3990 {
3991         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
3992         if (!iwl_is_alive(priv))
3993                 return -EAGAIN;
3994         return sprintf(buf, "0x%08x\n", (int)priv->status);
3995 }
3996
3997 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
3998
3999 /*****************************************************************************
4000  *
4001  * driver setup and teardown
4002  *
4003  *****************************************************************************/
4004
4005 static void iwl_setup_deferred_work(struct iwl_priv *priv)
4006 {
4007         priv->workqueue = create_workqueue(DRV_NAME);
4008
4009         init_waitqueue_head(&priv->wait_command_queue);
4010
4011         INIT_WORK(&priv->up, iwl4965_bg_up);
4012         INIT_WORK(&priv->restart, iwl4965_bg_restart);
4013         INIT_WORK(&priv->rx_replenish, iwl4965_bg_rx_replenish);
4014         INIT_WORK(&priv->rf_kill, iwl4965_bg_rf_kill);
4015         INIT_WORK(&priv->beacon_update, iwl4965_bg_beacon_update);
4016         INIT_WORK(&priv->set_monitor, iwl4965_bg_set_monitor);
4017         INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
4018         INIT_DELAYED_WORK(&priv->post_associate, iwl4965_bg_post_associate);
4019         INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
4020         INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
4021
4022         /* FIXME : remove when resolved PENDING */
4023         INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
4024         iwl_setup_scan_deferred_work(priv);
4025
4026         if (priv->cfg->ops->lib->setup_deferred_work)
4027                 priv->cfg->ops->lib->setup_deferred_work(priv);
4028
4029         init_timer(&priv->statistics_periodic);
4030         priv->statistics_periodic.data = (unsigned long)priv;
4031         priv->statistics_periodic.function = iwl4965_bg_statistics_periodic;
4032
4033         tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
4034                      iwl4965_irq_tasklet, (unsigned long)priv);
4035 }
4036
4037 static void iwl_cancel_deferred_work(struct iwl_priv *priv)
4038 {
4039         if (priv->cfg->ops->lib->cancel_deferred_work)
4040                 priv->cfg->ops->lib->cancel_deferred_work(priv);
4041
4042         cancel_delayed_work_sync(&priv->init_alive_start);
4043         cancel_delayed_work(&priv->scan_check);
4044         cancel_delayed_work(&priv->alive_start);
4045         cancel_delayed_work(&priv->post_associate);
4046         cancel_work_sync(&priv->beacon_update);
4047         del_timer_sync(&priv->statistics_periodic);
4048 }
4049
4050 static struct attribute *iwl4965_sysfs_entries[] = {
4051         &dev_attr_channels.attr,
4052         &dev_attr_flags.attr,
4053         &dev_attr_filter_flags.attr,
4054 #ifdef CONFIG_IWL4965_SPECTRUM_MEASUREMENT
4055         &dev_attr_measurement.attr,
4056 #endif
4057         &dev_attr_power_level.attr,
4058         &dev_attr_retry_rate.attr,
4059         &dev_attr_rs_window.attr,
4060         &dev_attr_statistics.attr,
4061         &dev_attr_status.attr,
4062         &dev_attr_temperature.attr,
4063         &dev_attr_tx_power.attr,
4064 #ifdef CONFIG_IWLWIFI_DEBUG
4065         &dev_attr_debug_level.attr,
4066 #endif
4067         &dev_attr_version.attr,
4068
4069         NULL
4070 };
4071
4072 static struct attribute_group iwl4965_attribute_group = {
4073         .name = NULL,           /* put in device directory */
4074         .attrs = iwl4965_sysfs_entries,
4075 };
4076
4077 static struct ieee80211_ops iwl4965_hw_ops = {
4078         .tx = iwl4965_mac_tx,
4079         .start = iwl4965_mac_start,
4080         .stop = iwl4965_mac_stop,
4081         .add_interface = iwl4965_mac_add_interface,
4082         .remove_interface = iwl4965_mac_remove_interface,
4083         .config = iwl4965_mac_config,
4084         .config_interface = iwl4965_mac_config_interface,
4085         .configure_filter = iwl4965_configure_filter,
4086         .set_key = iwl4965_mac_set_key,
4087         .update_tkip_key = iwl4965_mac_update_tkip_key,
4088         .get_stats = iwl4965_mac_get_stats,
4089         .get_tx_stats = iwl4965_mac_get_tx_stats,
4090         .conf_tx = iwl4965_mac_conf_tx,
4091         .reset_tsf = iwl4965_mac_reset_tsf,
4092         .beacon_update = iwl4965_mac_beacon_update,
4093         .bss_info_changed = iwl4965_bss_info_changed,
4094         .ampdu_action = iwl4965_mac_ampdu_action,
4095         .hw_scan = iwl4965_mac_hw_scan
4096 };
4097
4098 static int iwl4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4099 {
4100         int err = 0;
4101         struct iwl_priv *priv;
4102         struct ieee80211_hw *hw;
4103         struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
4104         unsigned long flags;
4105         DECLARE_MAC_BUF(mac);
4106
4107         /************************
4108          * 1. Allocating HW data
4109          ************************/
4110
4111         /* Disabling hardware scan means that mac80211 will perform scans
4112          * "the hard way", rather than using device's scan. */
4113         if (cfg->mod_params->disable_hw_scan) {
4114                 if (cfg->mod_params->debug & IWL_DL_INFO)
4115                         dev_printk(KERN_DEBUG, &(pdev->dev),
4116                                    "Disabling hw_scan\n");
4117                 iwl4965_hw_ops.hw_scan = NULL;
4118         }
4119
4120         hw = iwl_alloc_all(cfg, &iwl4965_hw_ops);
4121         if (!hw) {
4122                 err = -ENOMEM;
4123                 goto out;
4124         }
4125         priv = hw->priv;
4126         /* At this point both hw and priv are allocated. */
4127
4128         SET_IEEE80211_DEV(hw, &pdev->dev);
4129
4130         IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
4131         priv->cfg = cfg;
4132         priv->pci_dev = pdev;
4133
4134 #ifdef CONFIG_IWLWIFI_DEBUG
4135         priv->debug_level = priv->cfg->mod_params->debug;
4136         atomic_set(&priv->restrict_refcnt, 0);
4137 #endif
4138
4139         /**************************
4140          * 2. Initializing PCI bus
4141          **************************/
4142         if (pci_enable_device(pdev)) {
4143                 err = -ENODEV;
4144                 goto out_ieee80211_free_hw;
4145         }
4146
4147         pci_set_master(pdev);
4148
4149         err = pci_set_dma_mask(pdev, DMA_64BIT_MASK);
4150         if (!err)
4151                 err = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
4152         if (err) {
4153                 err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
4154                 if (!err)
4155                         err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
4156                 /* both attempts failed: */
4157                 if (err) {
4158                         printk(KERN_WARNING "%s: No suitable DMA available.\n",
4159                                 DRV_NAME);
4160                         goto out_pci_disable_device;
4161                 }
4162         }
4163
4164         err = pci_request_regions(pdev, DRV_NAME);
4165         if (err)
4166                 goto out_pci_disable_device;
4167
4168         pci_set_drvdata(pdev, priv);
4169
4170         /* We disable the RETRY_TIMEOUT register (0x41) to keep
4171          * PCI Tx retries from interfering with C3 CPU state */
4172         pci_write_config_byte(pdev, 0x41, 0x00);
4173
4174         /***********************
4175          * 3. Read REV register
4176          ***********************/
4177         priv->hw_base = pci_iomap(pdev, 0, 0);
4178         if (!priv->hw_base) {
4179                 err = -ENODEV;
4180                 goto out_pci_release_regions;
4181         }
4182
4183         IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
4184                 (unsigned long long) pci_resource_len(pdev, 0));
4185         IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
4186
4187         iwl_hw_detect(priv);
4188         printk(KERN_INFO DRV_NAME
4189                 ": Detected Intel Wireless WiFi Link %s REV=0x%X\n",
4190                 priv->cfg->name, priv->hw_rev);
4191
4192         /* amp init */
4193         err = priv->cfg->ops->lib->apm_ops.init(priv);
4194         if (err < 0) {
4195                 IWL_DEBUG_INFO("Failed to init APMG\n");
4196                 goto out_iounmap;
4197         }
4198         /*****************
4199          * 4. Read EEPROM
4200          *****************/
4201         /* Read the EEPROM */
4202         err = iwl_eeprom_init(priv);
4203         if (err) {
4204                 IWL_ERROR("Unable to init EEPROM\n");
4205                 goto out_iounmap;
4206         }
4207         err = iwl_eeprom_check_version(priv);
4208         if (err)
4209                 goto out_iounmap;
4210
4211         /* extract MAC Address */
4212         iwl_eeprom_get_mac(priv, priv->mac_addr);
4213         IWL_DEBUG_INFO("MAC address: %s\n", print_mac(mac, priv->mac_addr));
4214         SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
4215
4216         /************************
4217          * 5. Setup HW constants
4218          ************************/
4219         if (iwl_set_hw_params(priv)) {
4220                 IWL_ERROR("failed to set hw parameters\n");
4221                 goto out_free_eeprom;
4222         }
4223
4224         /*******************
4225          * 6. Setup priv
4226          *******************/
4227
4228         err = iwl_init_drv(priv);
4229         if (err)
4230                 goto out_free_eeprom;
4231         /* At this point both hw and priv are initialized. */
4232
4233         /**********************************
4234          * 7. Initialize module parameters
4235          **********************************/
4236
4237         /* Disable radio (SW RF KILL) via parameter when loading driver */
4238         if (priv->cfg->mod_params->disable) {
4239                 set_bit(STATUS_RF_KILL_SW, &priv->status);
4240                 IWL_DEBUG_INFO("Radio disabled.\n");
4241         }
4242
4243         /********************
4244          * 8. Setup services
4245          ********************/
4246         spin_lock_irqsave(&priv->lock, flags);
4247         iwl4965_disable_interrupts(priv);
4248         spin_unlock_irqrestore(&priv->lock, flags);
4249
4250         err = sysfs_create_group(&pdev->dev.kobj, &iwl4965_attribute_group);
4251         if (err) {
4252                 IWL_ERROR("failed to create sysfs device attributes\n");
4253                 goto out_uninit_drv;
4254         }
4255
4256
4257         iwl_setup_deferred_work(priv);
4258         iwl_setup_rx_handlers(priv);
4259
4260         /********************
4261          * 9. Conclude
4262          ********************/
4263         pci_save_state(pdev);
4264         pci_disable_device(pdev);
4265
4266         /**********************************
4267          * 10. Setup and register mac80211
4268          **********************************/
4269
4270         err = iwl_setup_mac(priv);
4271         if (err)
4272                 goto out_remove_sysfs;
4273
4274         err = iwl_dbgfs_register(priv, DRV_NAME);
4275         if (err)
4276                 IWL_ERROR("failed to create debugfs files\n");
4277
4278         err = iwl_rfkill_init(priv);
4279         if (err)
4280                 IWL_ERROR("Unable to initialize RFKILL system. "
4281                                   "Ignoring error: %d\n", err);
4282         iwl_power_initialize(priv);
4283         return 0;
4284
4285  out_remove_sysfs:
4286         sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
4287  out_uninit_drv:
4288         iwl_uninit_drv(priv);
4289  out_free_eeprom:
4290         iwl_eeprom_free(priv);
4291  out_iounmap:
4292         pci_iounmap(pdev, priv->hw_base);
4293  out_pci_release_regions:
4294         pci_release_regions(pdev);
4295         pci_set_drvdata(pdev, NULL);
4296  out_pci_disable_device:
4297         pci_disable_device(pdev);
4298  out_ieee80211_free_hw:
4299         ieee80211_free_hw(priv->hw);
4300  out:
4301         return err;
4302 }
4303
4304 static void __devexit iwl4965_pci_remove(struct pci_dev *pdev)
4305 {
4306         struct iwl_priv *priv = pci_get_drvdata(pdev);
4307         unsigned long flags;
4308
4309         if (!priv)
4310                 return;
4311
4312         IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
4313
4314         iwl_dbgfs_unregister(priv);
4315         sysfs_remove_group(&pdev->dev.kobj, &iwl4965_attribute_group);
4316
4317         if (priv->mac80211_registered) {
4318                 ieee80211_unregister_hw(priv->hw);
4319                 priv->mac80211_registered = 0;
4320         }
4321
4322         set_bit(STATUS_EXIT_PENDING, &priv->status);
4323
4324         iwl4965_down(priv);
4325
4326         /* make sure we flush any pending irq or
4327          * tasklet for the driver
4328          */
4329         spin_lock_irqsave(&priv->lock, flags);
4330         iwl4965_disable_interrupts(priv);
4331         spin_unlock_irqrestore(&priv->lock, flags);
4332
4333         iwl_synchronize_irq(priv);
4334
4335         iwl_rfkill_unregister(priv);
4336         iwl4965_dealloc_ucode_pci(priv);
4337
4338         if (priv->rxq.bd)
4339                 iwl_rx_queue_free(priv, &priv->rxq);
4340         iwl_hw_txq_ctx_free(priv);
4341
4342         iwl_clear_stations_table(priv);
4343         iwl_eeprom_free(priv);
4344
4345
4346         /*netif_stop_queue(dev); */
4347         flush_workqueue(priv->workqueue);
4348
4349         /* ieee80211_unregister_hw calls iwl4965_mac_stop, which flushes
4350          * priv->workqueue... so we can't take down the workqueue
4351          * until now... */
4352         destroy_workqueue(priv->workqueue);
4353         priv->workqueue = NULL;
4354
4355         pci_iounmap(pdev, priv->hw_base);
4356         pci_release_regions(pdev);
4357         pci_disable_device(pdev);
4358         pci_set_drvdata(pdev, NULL);
4359
4360         iwl_uninit_drv(priv);
4361
4362         if (priv->ibss_beacon)
4363                 dev_kfree_skb(priv->ibss_beacon);
4364
4365         ieee80211_free_hw(priv->hw);
4366 }
4367
4368 #ifdef CONFIG_PM
4369
4370 static int iwl4965_pci_suspend(struct pci_dev *pdev, pm_message_t state)
4371 {
4372         struct iwl_priv *priv = pci_get_drvdata(pdev);
4373
4374         if (priv->is_open) {
4375                 set_bit(STATUS_IN_SUSPEND, &priv->status);
4376                 iwl4965_mac_stop(priv->hw);
4377                 priv->is_open = 1;
4378         }
4379
4380         pci_set_power_state(pdev, PCI_D3hot);
4381
4382         return 0;
4383 }
4384
4385 static int iwl4965_pci_resume(struct pci_dev *pdev)
4386 {
4387         struct iwl_priv *priv = pci_get_drvdata(pdev);
4388
4389         pci_set_power_state(pdev, PCI_D0);
4390
4391         if (priv->is_open)
4392                 iwl4965_mac_start(priv->hw);
4393
4394         clear_bit(STATUS_IN_SUSPEND, &priv->status);
4395         return 0;
4396 }
4397
4398 #endif /* CONFIG_PM */
4399
4400 /*****************************************************************************
4401  *
4402  * driver and module entry point
4403  *
4404  *****************************************************************************/
4405
4406 /* Hardware specific file defines the PCI IDs table for that hardware module */
4407 static struct pci_device_id iwl_hw_card_ids[] = {
4408         {IWL_PCI_DEVICE(0x4229, PCI_ANY_ID, iwl4965_agn_cfg)},
4409         {IWL_PCI_DEVICE(0x4230, PCI_ANY_ID, iwl4965_agn_cfg)},
4410 #ifdef CONFIG_IWL5000
4411         {IWL_PCI_DEVICE(0x4235, PCI_ANY_ID, iwl5300_agn_cfg)},
4412         {IWL_PCI_DEVICE(0x4232, PCI_ANY_ID, iwl5100_agn_cfg)},
4413         {IWL_PCI_DEVICE(0x423A, PCI_ANY_ID, iwl5350_agn_cfg)},
4414 #endif /* CONFIG_IWL5000 */
4415         {0}
4416 };
4417 MODULE_DEVICE_TABLE(pci, iwl_hw_card_ids);
4418
4419 static struct pci_driver iwl_driver = {
4420         .name = DRV_NAME,
4421         .id_table = iwl_hw_card_ids,
4422         .probe = iwl4965_pci_probe,
4423         .remove = __devexit_p(iwl4965_pci_remove),
4424 #ifdef CONFIG_PM
4425         .suspend = iwl4965_pci_suspend,
4426         .resume = iwl4965_pci_resume,
4427 #endif
4428 };
4429
4430 static int __init iwl4965_init(void)
4431 {
4432
4433         int ret;
4434         printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
4435         printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
4436
4437         ret = iwl4965_rate_control_register();
4438         if (ret) {
4439                 IWL_ERROR("Unable to register rate control algorithm: %d\n", ret);
4440                 return ret;
4441         }
4442
4443         ret = pci_register_driver(&iwl_driver);
4444         if (ret) {
4445                 IWL_ERROR("Unable to initialize PCI module\n");
4446                 goto error_register;
4447         }
4448
4449         return ret;
4450
4451 error_register:
4452         iwl4965_rate_control_unregister();
4453         return ret;
4454 }
4455
4456 static void __exit iwl4965_exit(void)
4457 {
4458         pci_unregister_driver(&iwl_driver);
4459         iwl4965_rate_control_unregister();
4460 }
4461
4462 module_exit(iwl4965_exit);
4463 module_init(iwl4965_init);