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