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