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