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