iwlwifi: add debugging for TX path
[safe/jmp/linux-2.6] / drivers / net / wireless / iwlwifi / iwl3945-base.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2009 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  *  Intel Linux Wireless <ilw@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/init.h>
33 #include <linux/pci.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/delay.h>
36 #include <linux/skbuff.h>
37 #include <linux/netdevice.h>
38 #include <linux/wireless.h>
39 #include <linux/firmware.h>
40 #include <linux/etherdevice.h>
41 #include <linux/if_arp.h>
42
43 #include <net/ieee80211_radiotap.h>
44 #include <net/lib80211.h>
45 #include <net/mac80211.h>
46
47 #include <asm/div64.h>
48
49 #define DRV_NAME        "iwl3945"
50
51 #include "iwl-fh.h"
52 #include "iwl-3945-fh.h"
53 #include "iwl-commands.h"
54 #include "iwl-sta.h"
55 #include "iwl-3945.h"
56 #include "iwl-helpers.h"
57 #include "iwl-core.h"
58 #include "iwl-dev.h"
59
60 /*
61  * module name, copyright, version, etc.
62  */
63
64 #define DRV_DESCRIPTION \
65 "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
66
67 #ifdef CONFIG_IWLWIFI_DEBUG
68 #define VD "d"
69 #else
70 #define VD
71 #endif
72
73 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
74 #define VS "s"
75 #else
76 #define VS
77 #endif
78
79 #define IWL39_VERSION "1.2.26k" VD VS
80 #define DRV_COPYRIGHT   "Copyright(c) 2003-2009 Intel Corporation"
81 #define DRV_AUTHOR     "<ilw@linux.intel.com>"
82 #define DRV_VERSION     IWL39_VERSION
83
84
85 MODULE_DESCRIPTION(DRV_DESCRIPTION);
86 MODULE_VERSION(DRV_VERSION);
87 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
88 MODULE_LICENSE("GPL");
89
90  /* module parameters */
91 struct iwl_mod_params iwl3945_mod_params = {
92         .num_of_queues = IWL39_MAX_NUM_QUEUES,
93         .sw_crypto = 1,
94         .restart_fw = 1,
95         /* the rest are 0 by default */
96 };
97
98 /*************** STATION TABLE MANAGEMENT ****
99  * mac80211 should be examined to determine if sta_info is duplicating
100  * the functionality provided here
101  */
102
103 /**************************************************************/
104 #if 0 /* temporary disable till we add real remove station */
105 /**
106  * iwl3945_remove_station - Remove driver's knowledge of station.
107  *
108  * NOTE:  This does not remove station from device's station table.
109  */
110 static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
111 {
112         int index = IWL_INVALID_STATION;
113         int i;
114         unsigned long flags;
115
116         spin_lock_irqsave(&priv->sta_lock, flags);
117
118         if (is_ap)
119                 index = IWL_AP_ID;
120         else if (is_broadcast_ether_addr(addr))
121                 index = priv->hw_params.bcast_sta_id;
122         else
123                 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
124                         if (priv->stations_39[i].used &&
125                             !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
126                                                 addr)) {
127                                 index = i;
128                                 break;
129                         }
130
131         if (unlikely(index == IWL_INVALID_STATION))
132                 goto out;
133
134         if (priv->stations_39[index].used) {
135                 priv->stations_39[index].used = 0;
136                 priv->num_stations--;
137         }
138
139         BUG_ON(priv->num_stations < 0);
140
141 out:
142         spin_unlock_irqrestore(&priv->sta_lock, flags);
143         return 0;
144 }
145 #endif
146
147 /**
148  * iwl3945_clear_stations_table - Clear the driver's station table
149  *
150  * NOTE:  This does not clear or otherwise alter the device's station table.
151  */
152 static void iwl3945_clear_stations_table(struct iwl_priv *priv)
153 {
154         unsigned long flags;
155
156         spin_lock_irqsave(&priv->sta_lock, flags);
157
158         priv->num_stations = 0;
159         memset(priv->stations_39, 0, sizeof(priv->stations_39));
160
161         spin_unlock_irqrestore(&priv->sta_lock, flags);
162 }
163
164 /**
165  * iwl3945_add_station - Add station to station tables in driver and device
166  */
167 u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
168 {
169         int i;
170         int index = IWL_INVALID_STATION;
171         struct iwl3945_station_entry *station;
172         unsigned long flags_spin;
173         u8 rate;
174
175         spin_lock_irqsave(&priv->sta_lock, flags_spin);
176         if (is_ap)
177                 index = IWL_AP_ID;
178         else if (is_broadcast_ether_addr(addr))
179                 index = priv->hw_params.bcast_sta_id;
180         else
181                 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
182                         if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
183                                                 addr)) {
184                                 index = i;
185                                 break;
186                         }
187
188                         if (!priv->stations_39[i].used &&
189                             index == IWL_INVALID_STATION)
190                                 index = i;
191                 }
192
193         /* These two conditions has the same outcome but keep them separate
194           since they have different meaning */
195         if (unlikely(index == IWL_INVALID_STATION)) {
196                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
197                 return index;
198         }
199
200         if (priv->stations_39[index].used &&
201            !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
202                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
203                 return index;
204         }
205
206         IWL_DEBUG_ASSOC(priv, "Add STA ID %d: %pM\n", index, addr);
207         station = &priv->stations_39[index];
208         station->used = 1;
209         priv->num_stations++;
210
211         /* Set up the REPLY_ADD_STA command to send to device */
212         memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
213         memcpy(station->sta.sta.addr, addr, ETH_ALEN);
214         station->sta.mode = 0;
215         station->sta.sta.sta_id = index;
216         station->sta.station_flags = 0;
217
218         if (priv->band == IEEE80211_BAND_5GHZ)
219                 rate = IWL_RATE_6M_PLCP;
220         else
221                 rate =  IWL_RATE_1M_PLCP;
222
223         /* Turn on both antennas for the station... */
224         station->sta.rate_n_flags =
225                         iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
226
227         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
228
229         /* Add station to device's station table */
230         iwl_send_add_sta(priv,
231                          (struct iwl_addsta_cmd *)&station->sta, flags);
232         return index;
233
234 }
235
236 static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
237 {
238         int rc = 0;
239         struct iwl_rx_packet *res = NULL;
240         struct iwl3945_rxon_assoc_cmd rxon_assoc;
241         struct iwl_host_cmd cmd = {
242                 .id = REPLY_RXON_ASSOC,
243                 .len = sizeof(rxon_assoc),
244                 .meta.flags = CMD_WANT_SKB,
245                 .data = &rxon_assoc,
246         };
247         const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
248         const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
249
250         if ((rxon1->flags == rxon2->flags) &&
251             (rxon1->filter_flags == rxon2->filter_flags) &&
252             (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
253             (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
254                 IWL_DEBUG_INFO(priv, "Using current RXON_ASSOC.  Not resending.\n");
255                 return 0;
256         }
257
258         rxon_assoc.flags = priv->staging_rxon.flags;
259         rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
260         rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
261         rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
262         rxon_assoc.reserved = 0;
263
264         rc = iwl_send_cmd_sync(priv, &cmd);
265         if (rc)
266                 return rc;
267
268         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
269         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
270                 IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
271                 rc = -EIO;
272         }
273
274         priv->alloc_rxb_skb--;
275         dev_kfree_skb_any(cmd.meta.u.skb);
276
277         return rc;
278 }
279
280 /**
281  * iwl3945_get_antenna_flags - Get antenna flags for RXON command
282  * @priv: eeprom and antenna fields are used to determine antenna flags
283  *
284  * priv->eeprom39  is used to determine if antenna AUX/MAIN are reversed
285  * iwl3945_mod_params.antenna specifies the antenna diversity mode:
286  *
287  * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
288  * IWL_ANTENNA_MAIN      - Force MAIN antenna
289  * IWL_ANTENNA_AUX       - Force AUX antenna
290  */
291 __le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
292 {
293         struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
294
295         switch (iwl3945_mod_params.antenna) {
296         case IWL_ANTENNA_DIVERSITY:
297                 return 0;
298
299         case IWL_ANTENNA_MAIN:
300                 if (eeprom->antenna_switch_type)
301                         return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
302                 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
303
304         case IWL_ANTENNA_AUX:
305                 if (eeprom->antenna_switch_type)
306                         return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
307                 return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
308         }
309
310         /* bad antenna selector value */
311         IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
312                 iwl3945_mod_params.antenna);
313
314         return 0;               /* "diversity" is default if error */
315 }
316
317 /**
318  * iwl3945_commit_rxon - commit staging_rxon to hardware
319  *
320  * The RXON command in staging_rxon is committed to the hardware and
321  * the active_rxon structure is updated with the new data.  This
322  * function correctly transitions out of the RXON_ASSOC_MSK state if
323  * a HW tune is required based on the RXON structure changes.
324  */
325 static int iwl3945_commit_rxon(struct iwl_priv *priv)
326 {
327         /* cast away the const for active_rxon in this function */
328         struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
329         struct iwl3945_rxon_cmd *staging_rxon = (void *)&priv->staging_rxon;
330         int rc = 0;
331         bool new_assoc =
332                 !!(priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK);
333
334         if (!iwl_is_alive(priv))
335                 return -1;
336
337         /* always get timestamp with Rx frame */
338         staging_rxon->flags |= RXON_FLG_TSF2HOST_MSK;
339
340         /* select antenna */
341         staging_rxon->flags &=
342             ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
343         staging_rxon->flags |= iwl3945_get_antenna_flags(priv);
344
345         rc = iwl_check_rxon_cmd(priv);
346         if (rc) {
347                 IWL_ERR(priv, "Invalid RXON configuration.  Not committing.\n");
348                 return -EINVAL;
349         }
350
351         /* If we don't need to send a full RXON, we can use
352          * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
353          * and other flags for the current radio configuration. */
354         if (!iwl_full_rxon_required(priv)) {
355                 rc = iwl3945_send_rxon_assoc(priv);
356                 if (rc) {
357                         IWL_ERR(priv, "Error setting RXON_ASSOC "
358                                   "configuration (%d).\n", rc);
359                         return rc;
360                 }
361
362                 memcpy(active_rxon, staging_rxon, sizeof(*active_rxon));
363
364                 return 0;
365         }
366
367         /* If we are currently associated and the new config requires
368          * an RXON_ASSOC and the new config wants the associated mask enabled,
369          * we must clear the associated from the active configuration
370          * before we apply the new config */
371         if (iwl_is_associated(priv) && new_assoc) {
372                 IWL_DEBUG_INFO(priv, "Toggling associated bit on current RXON\n");
373                 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
374
375                 /*
376                  * reserved4 and 5 could have been filled by the iwlcore code.
377                  * Let's clear them before pushing to the 3945.
378                  */
379                 active_rxon->reserved4 = 0;
380                 active_rxon->reserved5 = 0;
381                 rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
382                                       sizeof(struct iwl3945_rxon_cmd),
383                                       &priv->active_rxon);
384
385                 /* If the mask clearing failed then we set
386                  * active_rxon back to what it was previously */
387                 if (rc) {
388                         active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
389                         IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
390                                   "configuration (%d).\n", rc);
391                         return rc;
392                 }
393         }
394
395         IWL_DEBUG_INFO(priv, "Sending RXON\n"
396                        "* with%s RXON_FILTER_ASSOC_MSK\n"
397                        "* channel = %d\n"
398                        "* bssid = %pM\n",
399                        (new_assoc ? "" : "out"),
400                        le16_to_cpu(staging_rxon->channel),
401                        staging_rxon->bssid_addr);
402
403         /*
404          * reserved4 and 5 could have been filled by the iwlcore code.
405          * Let's clear them before pushing to the 3945.
406          */
407         staging_rxon->reserved4 = 0;
408         staging_rxon->reserved5 = 0;
409
410         iwl_set_rxon_hwcrypto(priv, !priv->hw_params.sw_crypto);
411
412         /* Apply the new configuration */
413         rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
414                               sizeof(struct iwl3945_rxon_cmd),
415                               staging_rxon);
416         if (rc) {
417                 IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
418                 return rc;
419         }
420
421         memcpy(active_rxon, staging_rxon, sizeof(*active_rxon));
422
423         iwl3945_clear_stations_table(priv);
424
425         /* If we issue a new RXON command which required a tune then we must
426          * send a new TXPOWER command or we won't be able to Tx any frames */
427         rc = priv->cfg->ops->lib->send_tx_power(priv);
428         if (rc) {
429                 IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
430                 return rc;
431         }
432
433         /* Add the broadcast address so we can send broadcast frames */
434         if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
435             IWL_INVALID_STATION) {
436                 IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
437                 return -EIO;
438         }
439
440         /* If we have set the ASSOC_MSK and we are in BSS mode then
441          * add the IWL_AP_ID to the station rate table */
442         if (iwl_is_associated(priv) &&
443             (priv->iw_mode == NL80211_IFTYPE_STATION))
444                 if (iwl3945_add_station(priv, priv->active_rxon.bssid_addr,
445                                         1, 0)
446                     == IWL_INVALID_STATION) {
447                         IWL_ERR(priv, "Error adding AP address for transmit\n");
448                         return -EIO;
449                 }
450
451         /* Init the hardware's rate fallback order based on the band */
452         rc = iwl3945_init_hw_rate_table(priv);
453         if (rc) {
454                 IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
455                 return -EIO;
456         }
457
458         return 0;
459 }
460
461 static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
462                                    struct ieee80211_key_conf *keyconf,
463                                    u8 sta_id)
464 {
465         unsigned long flags;
466         __le16 key_flags = 0;
467         int ret;
468
469         key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
470         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
471
472         if (sta_id == priv->hw_params.bcast_sta_id)
473                 key_flags |= STA_KEY_MULTICAST_MSK;
474
475         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
476         keyconf->hw_key_idx = keyconf->keyidx;
477         key_flags &= ~STA_KEY_FLG_INVALID;
478
479         spin_lock_irqsave(&priv->sta_lock, flags);
480         priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
481         priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
482         memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
483                keyconf->keylen);
484
485         memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
486                keyconf->keylen);
487
488         if ((priv->stations_39[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
489                         == STA_KEY_FLG_NO_ENC)
490                 priv->stations_39[sta_id].sta.key.key_offset =
491                                  iwl_get_free_ucode_key_index(priv);
492         /* else, we are overriding an existing key => no need to allocated room
493         * in uCode. */
494
495         WARN(priv->stations_39[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
496                 "no space for a new key");
497
498         priv->stations_39[sta_id].sta.key.key_flags = key_flags;
499         priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
500         priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
501
502         IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
503
504         ret = iwl_send_add_sta(priv,
505                 (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, CMD_ASYNC);
506
507         spin_unlock_irqrestore(&priv->sta_lock, flags);
508
509         return ret;
510 }
511
512 static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
513                                   struct ieee80211_key_conf *keyconf,
514                                   u8 sta_id)
515 {
516         return -EOPNOTSUPP;
517 }
518
519 static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
520                                   struct ieee80211_key_conf *keyconf,
521                                   u8 sta_id)
522 {
523         return -EOPNOTSUPP;
524 }
525
526 static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
527 {
528         unsigned long flags;
529
530         spin_lock_irqsave(&priv->sta_lock, flags);
531         memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
532         memset(&priv->stations_39[sta_id].sta.key, 0,
533                 sizeof(struct iwl4965_keyinfo));
534         priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
535         priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
536         priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
537         spin_unlock_irqrestore(&priv->sta_lock, flags);
538
539         IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
540         iwl_send_add_sta(priv,
541                 (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, 0);
542         return 0;
543 }
544
545 static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
546                         struct ieee80211_key_conf *keyconf, u8 sta_id)
547 {
548         int ret = 0;
549
550         keyconf->hw_key_idx = HW_KEY_DYNAMIC;
551
552         switch (keyconf->alg) {
553         case ALG_CCMP:
554                 ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
555                 break;
556         case ALG_TKIP:
557                 ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
558                 break;
559         case ALG_WEP:
560                 ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
561                 break;
562         default:
563                 IWL_ERR(priv, "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
564                 ret = -EINVAL;
565         }
566
567         IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
568                       keyconf->alg, keyconf->keylen, keyconf->keyidx,
569                       sta_id, ret);
570
571         return ret;
572 }
573
574 static int iwl3945_remove_static_key(struct iwl_priv *priv)
575 {
576         int ret = -EOPNOTSUPP;
577
578         return ret;
579 }
580
581 static int iwl3945_set_static_key(struct iwl_priv *priv,
582                                 struct ieee80211_key_conf *key)
583 {
584         if (key->alg == ALG_WEP)
585                 return -EOPNOTSUPP;
586
587         IWL_ERR(priv, "Static key invalid: alg %d\n", key->alg);
588         return -EINVAL;
589 }
590
591 static void iwl3945_clear_free_frames(struct iwl_priv *priv)
592 {
593         struct list_head *element;
594
595         IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
596                        priv->frames_count);
597
598         while (!list_empty(&priv->free_frames)) {
599                 element = priv->free_frames.next;
600                 list_del(element);
601                 kfree(list_entry(element, struct iwl3945_frame, list));
602                 priv->frames_count--;
603         }
604
605         if (priv->frames_count) {
606                 IWL_WARN(priv, "%d frames still in use.  Did we lose one?\n",
607                             priv->frames_count);
608                 priv->frames_count = 0;
609         }
610 }
611
612 static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
613 {
614         struct iwl3945_frame *frame;
615         struct list_head *element;
616         if (list_empty(&priv->free_frames)) {
617                 frame = kzalloc(sizeof(*frame), GFP_KERNEL);
618                 if (!frame) {
619                         IWL_ERR(priv, "Could not allocate frame!\n");
620                         return NULL;
621                 }
622
623                 priv->frames_count++;
624                 return frame;
625         }
626
627         element = priv->free_frames.next;
628         list_del(element);
629         return list_entry(element, struct iwl3945_frame, list);
630 }
631
632 static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
633 {
634         memset(frame, 0, sizeof(*frame));
635         list_add(&frame->list, &priv->free_frames);
636 }
637
638 unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
639                                 struct ieee80211_hdr *hdr,
640                                 int left)
641 {
642
643         if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
644             ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
645              (priv->iw_mode != NL80211_IFTYPE_AP)))
646                 return 0;
647
648         if (priv->ibss_beacon->len > left)
649                 return 0;
650
651         memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
652
653         return priv->ibss_beacon->len;
654 }
655
656 static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
657 {
658         struct iwl3945_frame *frame;
659         unsigned int frame_size;
660         int rc;
661         u8 rate;
662
663         frame = iwl3945_get_free_frame(priv);
664
665         if (!frame) {
666                 IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
667                           "command.\n");
668                 return -ENOMEM;
669         }
670
671         rate = iwl_rate_get_lowest_plcp(priv);
672
673         frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
674
675         rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
676                               &frame->u.cmd[0]);
677
678         iwl3945_free_frame(priv, frame);
679
680         return rc;
681 }
682
683 static void iwl3945_unset_hw_params(struct iwl_priv *priv)
684 {
685         if (priv->shared_virt)
686                 pci_free_consistent(priv->pci_dev,
687                                     sizeof(struct iwl3945_shared),
688                                     priv->shared_virt,
689                                     priv->shared_phys);
690 }
691
692 #define MAX_UCODE_BEACON_INTERVAL       1024
693 #define INTEL_CONN_LISTEN_INTERVAL      cpu_to_le16(0xA)
694
695 static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
696 {
697         u16 new_val = 0;
698         u16 beacon_factor = 0;
699
700         beacon_factor =
701             (beacon_val + MAX_UCODE_BEACON_INTERVAL)
702                 / MAX_UCODE_BEACON_INTERVAL;
703         new_val = beacon_val / beacon_factor;
704
705         return cpu_to_le16(new_val);
706 }
707
708 static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
709 {
710         u64 interval_tm_unit;
711         u64 tsf, result;
712         unsigned long flags;
713         struct ieee80211_conf *conf = NULL;
714         u16 beacon_int = 0;
715
716         conf = ieee80211_get_hw_conf(priv->hw);
717
718         spin_lock_irqsave(&priv->lock, flags);
719         priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
720         priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
721
722         tsf = priv->timestamp;
723
724         beacon_int = priv->beacon_int;
725         spin_unlock_irqrestore(&priv->lock, flags);
726
727         if (priv->iw_mode == NL80211_IFTYPE_STATION) {
728                 if (beacon_int == 0) {
729                         priv->rxon_timing.beacon_interval = cpu_to_le16(100);
730                         priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
731                 } else {
732                         priv->rxon_timing.beacon_interval =
733                                 cpu_to_le16(beacon_int);
734                         priv->rxon_timing.beacon_interval =
735                             iwl3945_adjust_beacon_interval(
736                                 le16_to_cpu(priv->rxon_timing.beacon_interval));
737                 }
738
739                 priv->rxon_timing.atim_window = 0;
740         } else {
741                 priv->rxon_timing.beacon_interval =
742                         iwl3945_adjust_beacon_interval(conf->beacon_int);
743                 /* TODO: we need to get atim_window from upper stack
744                  * for now we set to 0 */
745                 priv->rxon_timing.atim_window = 0;
746         }
747
748         interval_tm_unit =
749                 (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
750         result = do_div(tsf, interval_tm_unit);
751         priv->rxon_timing.beacon_init_val =
752             cpu_to_le32((u32) ((u64) interval_tm_unit - result));
753
754         IWL_DEBUG_ASSOC(priv,
755                 "beacon interval %d beacon timer %d beacon tim %d\n",
756                 le16_to_cpu(priv->rxon_timing.beacon_interval),
757                 le32_to_cpu(priv->rxon_timing.beacon_init_val),
758                 le16_to_cpu(priv->rxon_timing.atim_window));
759 }
760
761 static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
762 {
763         if (mode == NL80211_IFTYPE_ADHOC) {
764                 const struct iwl_channel_info *ch_info;
765
766                 ch_info = iwl_get_channel_info(priv,
767                         priv->band,
768                         le16_to_cpu(priv->staging_rxon.channel));
769
770                 if (!ch_info || !is_channel_ibss(ch_info)) {
771                         IWL_ERR(priv, "channel %d not IBSS channel\n",
772                                   le16_to_cpu(priv->staging_rxon.channel));
773                         return -EINVAL;
774                 }
775         }
776
777         iwl_connection_init_rx_config(priv, mode);
778
779         iwl3945_clear_stations_table(priv);
780
781         /* don't commit rxon if rf-kill is on*/
782         if (!iwl_is_ready_rf(priv))
783                 return -EAGAIN;
784
785         cancel_delayed_work(&priv->scan_check);
786         if (iwl_scan_cancel_timeout(priv, 100)) {
787                 IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
788                 IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
789                 return -EAGAIN;
790         }
791
792         iwl3945_commit_rxon(priv);
793
794         return 0;
795 }
796
797 static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
798                                       struct ieee80211_tx_info *info,
799                                       struct iwl_cmd *cmd,
800                                       struct sk_buff *skb_frag,
801                                       int sta_id)
802 {
803         struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
804         struct iwl3945_hw_key *keyinfo =
805             &priv->stations_39[sta_id].keyinfo;
806
807         switch (keyinfo->alg) {
808         case ALG_CCMP:
809                 tx->sec_ctl = TX_CMD_SEC_CCM;
810                 memcpy(tx->key, keyinfo->key, keyinfo->keylen);
811                 IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
812                 break;
813
814         case ALG_TKIP:
815                 break;
816
817         case ALG_WEP:
818                 tx->sec_ctl = TX_CMD_SEC_WEP |
819                     (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
820
821                 if (keyinfo->keylen == 13)
822                         tx->sec_ctl |= TX_CMD_SEC_KEY128;
823
824                 memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
825
826                 IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
827                              "with key %d\n", info->control.hw_key->hw_key_idx);
828                 break;
829
830         default:
831                 IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
832                 break;
833         }
834 }
835
836 /*
837  * handle build REPLY_TX command notification.
838  */
839 static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
840                                   struct iwl_cmd *cmd,
841                                   struct ieee80211_tx_info *info,
842                                   struct ieee80211_hdr *hdr, u8 std_id)
843 {
844         struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
845         __le32 tx_flags = tx->tx_flags;
846         __le16 fc = hdr->frame_control;
847         u8 rc_flags = info->control.rates[0].flags;
848
849         tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
850         if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
851                 tx_flags |= TX_CMD_FLG_ACK_MSK;
852                 if (ieee80211_is_mgmt(fc))
853                         tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
854                 if (ieee80211_is_probe_resp(fc) &&
855                     !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
856                         tx_flags |= TX_CMD_FLG_TSF_MSK;
857         } else {
858                 tx_flags &= (~TX_CMD_FLG_ACK_MSK);
859                 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
860         }
861
862         tx->sta_id = std_id;
863         if (ieee80211_has_morefrags(fc))
864                 tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
865
866         if (ieee80211_is_data_qos(fc)) {
867                 u8 *qc = ieee80211_get_qos_ctl(hdr);
868                 tx->tid_tspec = qc[0] & 0xf;
869                 tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
870         } else {
871                 tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
872         }
873
874         if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
875                 tx_flags |= TX_CMD_FLG_RTS_MSK;
876                 tx_flags &= ~TX_CMD_FLG_CTS_MSK;
877         } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
878                 tx_flags &= ~TX_CMD_FLG_RTS_MSK;
879                 tx_flags |= TX_CMD_FLG_CTS_MSK;
880         }
881
882         if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
883                 tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
884
885         tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
886         if (ieee80211_is_mgmt(fc)) {
887                 if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
888                         tx->timeout.pm_frame_timeout = cpu_to_le16(3);
889                 else
890                         tx->timeout.pm_frame_timeout = cpu_to_le16(2);
891         } else {
892                 tx->timeout.pm_frame_timeout = 0;
893 #ifdef CONFIG_IWLWIFI_LEDS
894                 priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
895 #endif
896         }
897
898         tx->driver_txop = 0;
899         tx->tx_flags = tx_flags;
900         tx->next_frame_len = 0;
901 }
902
903 /**
904  * iwl3945_get_sta_id - Find station's index within station table
905  */
906 static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
907 {
908         int sta_id;
909         u16 fc = le16_to_cpu(hdr->frame_control);
910
911         /* If this frame is broadcast or management, use broadcast station id */
912         if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
913             is_multicast_ether_addr(hdr->addr1))
914                 return priv->hw_params.bcast_sta_id;
915
916         switch (priv->iw_mode) {
917
918         /* If we are a client station in a BSS network, use the special
919          * AP station entry (that's the only station we communicate with) */
920         case NL80211_IFTYPE_STATION:
921                 return IWL_AP_ID;
922
923         /* If we are an AP, then find the station, or use BCAST */
924         case NL80211_IFTYPE_AP:
925                 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
926                 if (sta_id != IWL_INVALID_STATION)
927                         return sta_id;
928                 return priv->hw_params.bcast_sta_id;
929
930         /* If this frame is going out to an IBSS network, find the station,
931          * or create a new station table entry */
932         case NL80211_IFTYPE_ADHOC: {
933                 /* Create new station table entry */
934                 sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
935                 if (sta_id != IWL_INVALID_STATION)
936                         return sta_id;
937
938                 sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
939
940                 if (sta_id != IWL_INVALID_STATION)
941                         return sta_id;
942
943                 IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
944                                "Defaulting to broadcast...\n",
945                                hdr->addr1);
946                 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
947                 return priv->hw_params.bcast_sta_id;
948         }
949         /* If we are in monitor mode, use BCAST. This is required for
950          * packet injection. */
951         case NL80211_IFTYPE_MONITOR:
952                 return priv->hw_params.bcast_sta_id;
953
954         default:
955                 IWL_WARN(priv, "Unknown mode of operation: %d\n",
956                         priv->iw_mode);
957                 return priv->hw_params.bcast_sta_id;
958         }
959 }
960
961 /*
962  * start REPLY_TX command process
963  */
964 static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
965 {
966         struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
967         struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
968         struct iwl3945_tx_cmd *tx;
969         struct iwl_tx_queue *txq = NULL;
970         struct iwl_queue *q = NULL;
971         struct iwl_cmd *out_cmd = NULL;
972         dma_addr_t phys_addr;
973         dma_addr_t txcmd_phys;
974         int txq_id = skb_get_queue_mapping(skb);
975         u16 len, idx, len_org, hdr_len;
976         u8 id;
977         u8 unicast;
978         u8 sta_id;
979         u8 tid = 0;
980         u16 seq_number = 0;
981         __le16 fc;
982         u8 wait_write_ptr = 0;
983         u8 *qc = NULL;
984         unsigned long flags;
985         int rc;
986
987         spin_lock_irqsave(&priv->lock, flags);
988         if (iwl_is_rfkill(priv)) {
989                 IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
990                 goto drop_unlock;
991         }
992
993         if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
994                 IWL_ERR(priv, "ERROR: No TX rate available.\n");
995                 goto drop_unlock;
996         }
997
998         unicast = !is_multicast_ether_addr(hdr->addr1);
999         id = 0;
1000
1001         fc = hdr->frame_control;
1002
1003 #ifdef CONFIG_IWLWIFI_DEBUG
1004         if (ieee80211_is_auth(fc))
1005                 IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
1006         else if (ieee80211_is_assoc_req(fc))
1007                 IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
1008         else if (ieee80211_is_reassoc_req(fc))
1009                 IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
1010 #endif
1011
1012         /* drop all data frame if we are not associated */
1013         if (ieee80211_is_data(fc) &&
1014             (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
1015             (!iwl_is_associated(priv) ||
1016              ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
1017                 IWL_DEBUG_DROP(priv, "Dropping - !iwl_is_associated\n");
1018                 goto drop_unlock;
1019         }
1020
1021         spin_unlock_irqrestore(&priv->lock, flags);
1022
1023         hdr_len = ieee80211_hdrlen(fc);
1024
1025         /* Find (or create) index into station table for destination station */
1026         sta_id = iwl3945_get_sta_id(priv, hdr);
1027         if (sta_id == IWL_INVALID_STATION) {
1028                 IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
1029                                hdr->addr1);
1030                 goto drop;
1031         }
1032
1033         IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
1034
1035         if (ieee80211_is_data_qos(fc)) {
1036                 qc = ieee80211_get_qos_ctl(hdr);
1037                 tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
1038                 seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
1039                                 IEEE80211_SCTL_SEQ;
1040                 hdr->seq_ctrl = cpu_to_le16(seq_number) |
1041                         (hdr->seq_ctrl &
1042                                 cpu_to_le16(IEEE80211_SCTL_FRAG));
1043                 seq_number += 0x10;
1044         }
1045
1046         /* Descriptor for chosen Tx queue */
1047         txq = &priv->txq[txq_id];
1048         q = &txq->q;
1049
1050         spin_lock_irqsave(&priv->lock, flags);
1051
1052         idx = get_cmd_index(q, q->write_ptr, 0);
1053
1054         /* Set up driver data for this TFD */
1055         memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
1056         txq->txb[q->write_ptr].skb[0] = skb;
1057
1058         /* Init first empty entry in queue's array of Tx/cmd buffers */
1059         out_cmd = txq->cmd[idx];
1060         tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
1061         memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
1062         memset(tx, 0, sizeof(*tx));
1063
1064         /*
1065          * Set up the Tx-command (not MAC!) header.
1066          * Store the chosen Tx queue and TFD index within the sequence field;
1067          * after Tx, uCode's Tx response will return this value so driver can
1068          * locate the frame within the tx queue and do post-tx processing.
1069          */
1070         out_cmd->hdr.cmd = REPLY_TX;
1071         out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
1072                                 INDEX_TO_SEQ(q->write_ptr)));
1073
1074         /* Copy MAC header from skb into command buffer */
1075         memcpy(tx->hdr, hdr, hdr_len);
1076
1077         /*
1078          * Use the first empty entry in this queue's command buffer array
1079          * to contain the Tx command and MAC header concatenated together
1080          * (payload data will be in another buffer).
1081          * Size of this varies, due to varying MAC header length.
1082          * If end is not dword aligned, we'll have 2 extra bytes at the end
1083          * of the MAC header (device reads on dword boundaries).
1084          * We'll tell device about this padding later.
1085          */
1086         len = sizeof(struct iwl3945_tx_cmd) +
1087                         sizeof(struct iwl_cmd_header) + hdr_len;
1088
1089         len_org = len;
1090         len = (len + 3) & ~3;
1091
1092         if (len_org != len)
1093                 len_org = 1;
1094         else
1095                 len_org = 0;
1096
1097         /* Physical address of this Tx command's header (not MAC header!),
1098          * within command buffer array. */
1099         txcmd_phys = pci_map_single(priv->pci_dev,
1100                                     out_cmd, sizeof(struct iwl_cmd),
1101                                     PCI_DMA_TODEVICE);
1102         pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
1103         pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
1104         /* Add buffer containing Tx command and MAC(!) header to TFD's
1105          * first entry */
1106         txcmd_phys += offsetof(struct iwl_cmd, hdr);
1107
1108         /* Add buffer containing Tx command and MAC(!) header to TFD's
1109          * first entry */
1110         priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
1111                                                    txcmd_phys, len, 1, 0);
1112
1113         if (info->control.hw_key)
1114                 iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
1115
1116         /* Set up TFD's 2nd entry to point directly to remainder of skb,
1117          * if any (802.11 null frames have no payload). */
1118         len = skb->len - hdr_len;
1119         if (len) {
1120                 phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
1121                                            len, PCI_DMA_TODEVICE);
1122                 priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
1123                                                            phys_addr, len,
1124                                                            0, U32_PAD(len));
1125         }
1126
1127         /* Total # bytes to be transmitted */
1128         len = (u16)skb->len;
1129         tx->len = cpu_to_le16(len);
1130
1131         /* TODO need this for burst mode later on */
1132         iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
1133
1134         /* set is_hcca to 0; it probably will never be implemented */
1135         iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
1136
1137         tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
1138         tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
1139
1140         if (!ieee80211_has_morefrags(hdr->frame_control)) {
1141                 txq->need_update = 1;
1142                 if (qc)
1143                         priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
1144         } else {
1145                 wait_write_ptr = 1;
1146                 txq->need_update = 0;
1147         }
1148
1149         IWL_DEBUG_TX(priv, "sequence nr = 0X%x \n",
1150                      le16_to_cpu(out_cmd->hdr.sequence));
1151         IWL_DEBUG_TX(priv, "tx_flags = 0X%x \n", le32_to_cpu(tx->tx_flags));
1152         iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
1153         iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
1154                            ieee80211_hdrlen(fc));
1155
1156         /* Tell device the write index *just past* this latest filled TFD */
1157         q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
1158         rc = iwl_txq_update_write_ptr(priv, txq);
1159         spin_unlock_irqrestore(&priv->lock, flags);
1160
1161         if (rc)
1162                 return rc;
1163
1164         if ((iwl_queue_space(q) < q->high_mark)
1165             && priv->mac80211_registered) {
1166                 if (wait_write_ptr) {
1167                         spin_lock_irqsave(&priv->lock, flags);
1168                         txq->need_update = 1;
1169                         iwl_txq_update_write_ptr(priv, txq);
1170                         spin_unlock_irqrestore(&priv->lock, flags);
1171                 }
1172
1173                 iwl_stop_queue(priv, skb_get_queue_mapping(skb));
1174         }
1175
1176         return 0;
1177
1178 drop_unlock:
1179         spin_unlock_irqrestore(&priv->lock, flags);
1180 drop:
1181         return -1;
1182 }
1183
1184 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
1185
1186 #include "iwl-spectrum.h"
1187
1188 #define BEACON_TIME_MASK_LOW    0x00FFFFFF
1189 #define BEACON_TIME_MASK_HIGH   0xFF000000
1190 #define TIME_UNIT               1024
1191
1192 /*
1193  * extended beacon time format
1194  * time in usec will be changed into a 32-bit value in 8:24 format
1195  * the high 1 byte is the beacon counts
1196  * the lower 3 bytes is the time in usec within one beacon interval
1197  */
1198
1199 static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
1200 {
1201         u32 quot;
1202         u32 rem;
1203         u32 interval = beacon_interval * 1024;
1204
1205         if (!interval || !usec)
1206                 return 0;
1207
1208         quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
1209         rem = (usec % interval) & BEACON_TIME_MASK_LOW;
1210
1211         return (quot << 24) + rem;
1212 }
1213
1214 /* base is usually what we get from ucode with each received frame,
1215  * the same as HW timer counter counting down
1216  */
1217
1218 static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
1219 {
1220         u32 base_low = base & BEACON_TIME_MASK_LOW;
1221         u32 addon_low = addon & BEACON_TIME_MASK_LOW;
1222         u32 interval = beacon_interval * TIME_UNIT;
1223         u32 res = (base & BEACON_TIME_MASK_HIGH) +
1224             (addon & BEACON_TIME_MASK_HIGH);
1225
1226         if (base_low > addon_low)
1227                 res += base_low - addon_low;
1228         else if (base_low < addon_low) {
1229                 res += interval + base_low - addon_low;
1230                 res += (1 << 24);
1231         } else
1232                 res += (1 << 24);
1233
1234         return cpu_to_le32(res);
1235 }
1236
1237 static int iwl3945_get_measurement(struct iwl_priv *priv,
1238                                struct ieee80211_measurement_params *params,
1239                                u8 type)
1240 {
1241         struct iwl_spectrum_cmd spectrum;
1242         struct iwl_rx_packet *res;
1243         struct iwl_host_cmd cmd = {
1244                 .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
1245                 .data = (void *)&spectrum,
1246                 .meta.flags = CMD_WANT_SKB,
1247         };
1248         u32 add_time = le64_to_cpu(params->start_time);
1249         int rc;
1250         int spectrum_resp_status;
1251         int duration = le16_to_cpu(params->duration);
1252
1253         if (iwl_is_associated(priv))
1254                 add_time =
1255                     iwl3945_usecs_to_beacons(
1256                         le64_to_cpu(params->start_time) - priv->last_tsf,
1257                         le16_to_cpu(priv->rxon_timing.beacon_interval));
1258
1259         memset(&spectrum, 0, sizeof(spectrum));
1260
1261         spectrum.channel_count = cpu_to_le16(1);
1262         spectrum.flags =
1263             RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
1264         spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
1265         cmd.len = sizeof(spectrum);
1266         spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
1267
1268         if (iwl_is_associated(priv))
1269                 spectrum.start_time =
1270                     iwl3945_add_beacon_time(priv->last_beacon_time,
1271                                 add_time,
1272                                 le16_to_cpu(priv->rxon_timing.beacon_interval));
1273         else
1274                 spectrum.start_time = 0;
1275
1276         spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
1277         spectrum.channels[0].channel = params->channel;
1278         spectrum.channels[0].type = type;
1279         if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
1280                 spectrum.flags |= RXON_FLG_BAND_24G_MSK |
1281                     RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
1282
1283         rc = iwl_send_cmd_sync(priv, &cmd);
1284         if (rc)
1285                 return rc;
1286
1287         res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
1288         if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
1289                 IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
1290                 rc = -EIO;
1291         }
1292
1293         spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
1294         switch (spectrum_resp_status) {
1295         case 0:         /* Command will be handled */
1296                 if (res->u.spectrum.id != 0xff) {
1297                         IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
1298                                                 res->u.spectrum.id);
1299                         priv->measurement_status &= ~MEASUREMENT_READY;
1300                 }
1301                 priv->measurement_status |= MEASUREMENT_ACTIVE;
1302                 rc = 0;
1303                 break;
1304
1305         case 1:         /* Command will not be handled */
1306                 rc = -EAGAIN;
1307                 break;
1308         }
1309
1310         dev_kfree_skb_any(cmd.meta.u.skb);
1311
1312         return rc;
1313 }
1314 #endif
1315
1316 static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
1317                                struct iwl_rx_mem_buffer *rxb)
1318 {
1319         struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1320         struct iwl_alive_resp *palive;
1321         struct delayed_work *pwork;
1322
1323         palive = &pkt->u.alive_frame;
1324
1325         IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
1326                        "0x%01X 0x%01X\n",
1327                        palive->is_valid, palive->ver_type,
1328                        palive->ver_subtype);
1329
1330         if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
1331                 IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
1332                 memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
1333                        sizeof(struct iwl_alive_resp));
1334                 pwork = &priv->init_alive_start;
1335         } else {
1336                 IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
1337                 memcpy(&priv->card_alive, &pkt->u.alive_frame,
1338                        sizeof(struct iwl_alive_resp));
1339                 pwork = &priv->alive_start;
1340                 iwl3945_disable_events(priv);
1341         }
1342
1343         /* We delay the ALIVE response by 5ms to
1344          * give the HW RF Kill time to activate... */
1345         if (palive->is_valid == UCODE_VALID_OK)
1346                 queue_delayed_work(priv->workqueue, pwork,
1347                                    msecs_to_jiffies(5));
1348         else
1349                 IWL_WARN(priv, "uCode did not respond OK.\n");
1350 }
1351
1352 static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
1353                                  struct iwl_rx_mem_buffer *rxb)
1354 {
1355 #ifdef CONFIG_IWLWIFI_DEBUG
1356         struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1357 #endif
1358
1359         IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
1360         return;
1361 }
1362
1363 static void iwl3945_bg_beacon_update(struct work_struct *work)
1364 {
1365         struct iwl_priv *priv =
1366                 container_of(work, struct iwl_priv, beacon_update);
1367         struct sk_buff *beacon;
1368
1369         /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
1370         beacon = ieee80211_beacon_get(priv->hw, priv->vif);
1371
1372         if (!beacon) {
1373                 IWL_ERR(priv, "update beacon failed\n");
1374                 return;
1375         }
1376
1377         mutex_lock(&priv->mutex);
1378         /* new beacon skb is allocated every time; dispose previous.*/
1379         if (priv->ibss_beacon)
1380                 dev_kfree_skb(priv->ibss_beacon);
1381
1382         priv->ibss_beacon = beacon;
1383         mutex_unlock(&priv->mutex);
1384
1385         iwl3945_send_beacon_cmd(priv);
1386 }
1387
1388 static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
1389                                 struct iwl_rx_mem_buffer *rxb)
1390 {
1391 #ifdef CONFIG_IWLWIFI_DEBUG
1392         struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1393         struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
1394         u8 rate = beacon->beacon_notify_hdr.rate;
1395
1396         IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
1397                 "tsf %d %d rate %d\n",
1398                 le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
1399                 beacon->beacon_notify_hdr.failure_frame,
1400                 le32_to_cpu(beacon->ibss_mgr_status),
1401                 le32_to_cpu(beacon->high_tsf),
1402                 le32_to_cpu(beacon->low_tsf), rate);
1403 #endif
1404
1405         if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
1406             (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
1407                 queue_work(priv->workqueue, &priv->beacon_update);
1408 }
1409
1410 /* Handle notification from uCode that card's power state is changing
1411  * due to software, hardware, or critical temperature RFKILL */
1412 static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
1413                                     struct iwl_rx_mem_buffer *rxb)
1414 {
1415         struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
1416         u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
1417         unsigned long status = priv->status;
1418
1419         IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s\n",
1420                           (flags & HW_CARD_DISABLED) ? "Kill" : "On",
1421                           (flags & SW_CARD_DISABLED) ? "Kill" : "On");
1422
1423         iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
1424                     CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
1425
1426         if (flags & HW_CARD_DISABLED)
1427                 set_bit(STATUS_RF_KILL_HW, &priv->status);
1428         else
1429                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
1430
1431
1432         if (flags & SW_CARD_DISABLED)
1433                 set_bit(STATUS_RF_KILL_SW, &priv->status);
1434         else
1435                 clear_bit(STATUS_RF_KILL_SW, &priv->status);
1436
1437         iwl_scan_cancel(priv);
1438
1439         if ((test_bit(STATUS_RF_KILL_HW, &status) !=
1440              test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
1441             (test_bit(STATUS_RF_KILL_SW, &status) !=
1442              test_bit(STATUS_RF_KILL_SW, &priv->status)))
1443                 queue_work(priv->workqueue, &priv->rf_kill);
1444         else
1445                 wake_up_interruptible(&priv->wait_command_queue);
1446 }
1447
1448 /**
1449  * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
1450  *
1451  * Setup the RX handlers for each of the reply types sent from the uCode
1452  * to the host.
1453  *
1454  * This function chains into the hardware specific files for them to setup
1455  * any hardware specific handlers as well.
1456  */
1457 static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
1458 {
1459         priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
1460         priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
1461         priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
1462         priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
1463         priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
1464         priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
1465             iwl_rx_pm_debug_statistics_notif;
1466         priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
1467
1468         /*
1469          * The same handler is used for both the REPLY to a discrete
1470          * statistics request from the host as well as for the periodic
1471          * statistics notifications (after received beacons) from the uCode.
1472          */
1473         priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
1474         priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
1475
1476         iwl_setup_spectrum_handlers(priv);
1477         iwl_setup_rx_scan_handlers(priv);
1478         priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
1479
1480         /* Set up hardware specific Rx handlers */
1481         iwl3945_hw_rx_handler_setup(priv);
1482 }
1483
1484 /************************** RX-FUNCTIONS ****************************/
1485 /*
1486  * Rx theory of operation
1487  *
1488  * The host allocates 32 DMA target addresses and passes the host address
1489  * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
1490  * 0 to 31
1491  *
1492  * Rx Queue Indexes
1493  * The host/firmware share two index registers for managing the Rx buffers.
1494  *
1495  * The READ index maps to the first position that the firmware may be writing
1496  * to -- the driver can read up to (but not including) this position and get
1497  * good data.
1498  * The READ index is managed by the firmware once the card is enabled.
1499  *
1500  * The WRITE index maps to the last position the driver has read from -- the
1501  * position preceding WRITE is the last slot the firmware can place a packet.
1502  *
1503  * The queue is empty (no good data) if WRITE = READ - 1, and is full if
1504  * WRITE = READ.
1505  *
1506  * During initialization, the host sets up the READ queue position to the first
1507  * INDEX position, and WRITE to the last (READ - 1 wrapped)
1508  *
1509  * When the firmware places a packet in a buffer, it will advance the READ index
1510  * and fire the RX interrupt.  The driver can then query the READ index and
1511  * process as many packets as possible, moving the WRITE index forward as it
1512  * resets the Rx queue buffers with new memory.
1513  *
1514  * The management in the driver is as follows:
1515  * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free.  When
1516  *   iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
1517  *   to replenish the iwl->rxq->rx_free.
1518  * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
1519  *   iwl->rxq is replenished and the READ INDEX is updated (updating the
1520  *   'processed' and 'read' driver indexes as well)
1521  * + A received packet is processed and handed to the kernel network stack,
1522  *   detached from the iwl->rxq.  The driver 'processed' index is updated.
1523  * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
1524  *   list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
1525  *   INDEX is not incremented and iwl->status(RX_STALLED) is set.  If there
1526  *   were enough free buffers and RX_STALLED is set it is cleared.
1527  *
1528  *
1529  * Driver sequence:
1530  *
1531  * iwl3945_rx_replenish()     Replenishes rx_free list from rx_used, and calls
1532  *                            iwl3945_rx_queue_restock
1533  * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
1534  *                            queue, updates firmware pointers, and updates
1535  *                            the WRITE index.  If insufficient rx_free buffers
1536  *                            are available, schedules iwl3945_rx_replenish
1537  *
1538  * -- enable interrupts --
1539  * ISR - iwl3945_rx()         Detach iwl_rx_mem_buffers from pool up to the
1540  *                            READ INDEX, detaching the SKB from the pool.
1541  *                            Moves the packet buffer from queue to rx_used.
1542  *                            Calls iwl3945_rx_queue_restock to refill any empty
1543  *                            slots.
1544  * ...
1545  *
1546  */
1547
1548 /**
1549  * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
1550  */
1551 static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
1552                                           dma_addr_t dma_addr)
1553 {
1554         return cpu_to_le32((u32)dma_addr);
1555 }
1556
1557 /**
1558  * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
1559  *
1560  * If there are slots in the RX queue that need to be restocked,
1561  * and we have free pre-allocated buffers, fill the ranks as much
1562  * as we can, pulling from rx_free.
1563  *
1564  * This moves the 'write' index forward to catch up with 'processed', and
1565  * also updates the memory address in the firmware to reference the new
1566  * target buffer.
1567  */
1568 static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
1569 {
1570         struct iwl_rx_queue *rxq = &priv->rxq;
1571         struct list_head *element;
1572         struct iwl_rx_mem_buffer *rxb;
1573         unsigned long flags;
1574         int write, rc;
1575
1576         spin_lock_irqsave(&rxq->lock, flags);
1577         write = rxq->write & ~0x7;
1578         while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
1579                 /* Get next free Rx buffer, remove from free list */
1580                 element = rxq->rx_free.next;
1581                 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1582                 list_del(element);
1583
1584                 /* Point to Rx buffer via next RBD in circular buffer */
1585                 rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
1586                 rxq->queue[rxq->write] = rxb;
1587                 rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
1588                 rxq->free_count--;
1589         }
1590         spin_unlock_irqrestore(&rxq->lock, flags);
1591         /* If the pre-allocated buffer pool is dropping low, schedule to
1592          * refill it */
1593         if (rxq->free_count <= RX_LOW_WATERMARK)
1594                 queue_work(priv->workqueue, &priv->rx_replenish);
1595
1596
1597         /* If we've added more space for the firmware to place data, tell it.
1598          * Increment device's write pointer in multiples of 8. */
1599         if ((write != (rxq->write & ~0x7))
1600             || (abs(rxq->write - rxq->read) > 7)) {
1601                 spin_lock_irqsave(&rxq->lock, flags);
1602                 rxq->need_update = 1;
1603                 spin_unlock_irqrestore(&rxq->lock, flags);
1604                 rc = iwl_rx_queue_update_write_ptr(priv, rxq);
1605                 if (rc)
1606                         return rc;
1607         }
1608
1609         return 0;
1610 }
1611
1612 /**
1613  * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
1614  *
1615  * When moving to rx_free an SKB is allocated for the slot.
1616  *
1617  * Also restock the Rx queue via iwl3945_rx_queue_restock.
1618  * This is called as a scheduled work item (except for during initialization)
1619  */
1620 static void iwl3945_rx_allocate(struct iwl_priv *priv)
1621 {
1622         struct iwl_rx_queue *rxq = &priv->rxq;
1623         struct list_head *element;
1624         struct iwl_rx_mem_buffer *rxb;
1625         unsigned long flags;
1626         spin_lock_irqsave(&rxq->lock, flags);
1627         while (!list_empty(&rxq->rx_used)) {
1628                 element = rxq->rx_used.next;
1629                 rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
1630
1631                 /* Alloc a new receive buffer */
1632                 rxb->skb =
1633                     alloc_skb(priv->hw_params.rx_buf_size,
1634                                 __GFP_NOWARN | GFP_ATOMIC);
1635                 if (!rxb->skb) {
1636                         if (net_ratelimit())
1637                                 IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
1638                         /* We don't reschedule replenish work here -- we will
1639                          * call the restock method and if it still needs
1640                          * more buffers it will schedule replenish */
1641                         break;
1642                 }
1643
1644                 /* If radiotap head is required, reserve some headroom here.
1645                  * The physical head count is a variable rx_stats->phy_count.
1646                  * We reserve 4 bytes here. Plus these extra bytes, the
1647                  * headroom of the physical head should be enough for the
1648                  * radiotap head that iwl3945 supported. See iwl3945_rt.
1649                  */
1650                 skb_reserve(rxb->skb, 4);
1651
1652                 priv->alloc_rxb_skb++;
1653                 list_del(element);
1654
1655                 /* Get physical address of RB/SKB */
1656                 rxb->real_dma_addr = pci_map_single(priv->pci_dev,
1657                                                 rxb->skb->data,
1658                                                 priv->hw_params.rx_buf_size,
1659                                                 PCI_DMA_FROMDEVICE);
1660                 list_add_tail(&rxb->list, &rxq->rx_free);
1661                 rxq->free_count++;
1662         }
1663         spin_unlock_irqrestore(&rxq->lock, flags);
1664 }
1665
1666 /*
1667  * this should be called while priv->lock is locked
1668  */
1669 static void __iwl3945_rx_replenish(void *data)
1670 {
1671         struct iwl_priv *priv = data;
1672
1673         iwl3945_rx_allocate(priv);
1674         iwl3945_rx_queue_restock(priv);
1675 }
1676
1677
1678 void iwl3945_rx_replenish(void *data)
1679 {
1680         struct iwl_priv *priv = data;
1681         unsigned long flags;
1682
1683         iwl3945_rx_allocate(priv);
1684
1685         spin_lock_irqsave(&priv->lock, flags);
1686         iwl3945_rx_queue_restock(priv);
1687         spin_unlock_irqrestore(&priv->lock, flags);
1688 }
1689
1690 /* Convert linear signal-to-noise ratio into dB */
1691 static u8 ratio2dB[100] = {
1692 /*       0   1   2   3   4   5   6   7   8   9 */
1693          0,  0,  6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
1694         20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
1695         26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
1696         29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
1697         32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
1698         34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
1699         36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
1700         37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
1701         38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
1702         39, 39, 39, 39, 39, 40, 40, 40, 40, 40  /* 90 - 99 */
1703 };
1704
1705 /* Calculates a relative dB value from a ratio of linear
1706  *   (i.e. not dB) signal levels.
1707  * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
1708 int iwl3945_calc_db_from_ratio(int sig_ratio)
1709 {
1710         /* 1000:1 or higher just report as 60 dB */
1711         if (sig_ratio >= 1000)
1712                 return 60;
1713
1714         /* 100:1 or higher, divide by 10 and use table,
1715          *   add 20 dB to make up for divide by 10 */
1716         if (sig_ratio >= 100)
1717                 return 20 + (int)ratio2dB[sig_ratio/10];
1718
1719         /* We shouldn't see this */
1720         if (sig_ratio < 1)
1721                 return 0;
1722
1723         /* Use table for ratios 1:1 - 99:1 */
1724         return (int)ratio2dB[sig_ratio];
1725 }
1726
1727 #define PERFECT_RSSI (-20) /* dBm */
1728 #define WORST_RSSI (-95)   /* dBm */
1729 #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
1730
1731 /* Calculate an indication of rx signal quality (a percentage, not dBm!).
1732  * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
1733  *   about formulas used below. */
1734 int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
1735 {
1736         int sig_qual;
1737         int degradation = PERFECT_RSSI - rssi_dbm;
1738
1739         /* If we get a noise measurement, use signal-to-noise ratio (SNR)
1740          * as indicator; formula is (signal dbm - noise dbm).
1741          * SNR at or above 40 is a great signal (100%).
1742          * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
1743          * Weakest usable signal is usually 10 - 15 dB SNR. */
1744         if (noise_dbm) {
1745                 if (rssi_dbm - noise_dbm >= 40)
1746                         return 100;
1747                 else if (rssi_dbm < noise_dbm)
1748                         return 0;
1749                 sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
1750
1751         /* Else use just the signal level.
1752          * This formula is a least squares fit of data points collected and
1753          *   compared with a reference system that had a percentage (%) display
1754          *   for signal quality. */
1755         } else
1756                 sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
1757                             (15 * RSSI_RANGE + 62 * degradation)) /
1758                            (RSSI_RANGE * RSSI_RANGE);
1759
1760         if (sig_qual > 100)
1761                 sig_qual = 100;
1762         else if (sig_qual < 1)
1763                 sig_qual = 0;
1764
1765         return sig_qual;
1766 }
1767
1768 /**
1769  * iwl3945_rx_handle - Main entry function for receiving responses from uCode
1770  *
1771  * Uses the priv->rx_handlers callback function array to invoke
1772  * the appropriate handlers, including command responses,
1773  * frame-received notifications, and other notifications.
1774  */
1775 static void iwl3945_rx_handle(struct iwl_priv *priv)
1776 {
1777         struct iwl_rx_mem_buffer *rxb;
1778         struct iwl_rx_packet *pkt;
1779         struct iwl_rx_queue *rxq = &priv->rxq;
1780         u32 r, i;
1781         int reclaim;
1782         unsigned long flags;
1783         u8 fill_rx = 0;
1784         u32 count = 8;
1785
1786         /* uCode's read index (stored in shared DRAM) indicates the last Rx
1787          * buffer that the driver may process (last buffer filled by ucode). */
1788         r = le16_to_cpu(rxq->rb_stts->closed_rb_num) &  0x0FFF;
1789         i = rxq->read;
1790
1791         if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
1792                 fill_rx = 1;
1793         /* Rx interrupt, but nothing sent from uCode */
1794         if (i == r)
1795                 IWL_DEBUG(priv, IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
1796
1797         while (i != r) {
1798                 rxb = rxq->queue[i];
1799
1800                 /* If an RXB doesn't have a Rx queue slot associated with it,
1801                  * then a bug has been introduced in the queue refilling
1802                  * routines -- catch it here */
1803                 BUG_ON(rxb == NULL);
1804
1805                 rxq->queue[i] = NULL;
1806
1807                 pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
1808                                             priv->hw_params.rx_buf_size,
1809                                             PCI_DMA_FROMDEVICE);
1810                 pkt = (struct iwl_rx_packet *)rxb->skb->data;
1811
1812                 /* Reclaim a command buffer only if this packet is a response
1813                  *   to a (driver-originated) command.
1814                  * If the packet (e.g. Rx frame) originated from uCode,
1815                  *   there is no command buffer to reclaim.
1816                  * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
1817                  *   but apparently a few don't get set; catch them here. */
1818                 reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
1819                         (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
1820                         (pkt->hdr.cmd != REPLY_TX);
1821
1822                 /* Based on type of command response or notification,
1823                  *   handle those that need handling via function in
1824                  *   rx_handlers table.  See iwl3945_setup_rx_handlers() */
1825                 if (priv->rx_handlers[pkt->hdr.cmd]) {
1826                         IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
1827                                 "r = %d, i = %d, %s, 0x%02x\n", r, i,
1828                                 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1829                         priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
1830                 } else {
1831                         /* No handling needed */
1832                         IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
1833                                 "r %d i %d No handler needed for %s, 0x%02x\n",
1834                                 r, i, get_cmd_string(pkt->hdr.cmd),
1835                                 pkt->hdr.cmd);
1836                 }
1837
1838                 if (reclaim) {
1839                         /* Invoke any callbacks, transfer the skb to caller, and
1840                          * fire off the (possibly) blocking iwl_send_cmd()
1841                          * as we reclaim the driver command queue */
1842                         if (rxb && rxb->skb)
1843                                 iwl_tx_cmd_complete(priv, rxb);
1844                         else
1845                                 IWL_WARN(priv, "Claim null rxb?\n");
1846                 }
1847
1848                 /* For now we just don't re-use anything.  We can tweak this
1849                  * later to try and re-use notification packets and SKBs that
1850                  * fail to Rx correctly */
1851                 if (rxb->skb != NULL) {
1852                         priv->alloc_rxb_skb--;
1853                         dev_kfree_skb_any(rxb->skb);
1854                         rxb->skb = NULL;
1855                 }
1856
1857                 pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
1858                                 priv->hw_params.rx_buf_size,
1859                                 PCI_DMA_FROMDEVICE);
1860                 spin_lock_irqsave(&rxq->lock, flags);
1861                 list_add_tail(&rxb->list, &priv->rxq.rx_used);
1862                 spin_unlock_irqrestore(&rxq->lock, flags);
1863                 i = (i + 1) & RX_QUEUE_MASK;
1864                 /* If there are a lot of unused frames,
1865                  * restock the Rx queue so ucode won't assert. */
1866                 if (fill_rx) {
1867                         count++;
1868                         if (count >= 8) {
1869                                 priv->rxq.read = i;
1870                                 __iwl3945_rx_replenish(priv);
1871                                 count = 0;
1872                         }
1873                 }
1874         }
1875
1876         /* Backtrack one entry */
1877         priv->rxq.read = i;
1878         iwl3945_rx_queue_restock(priv);
1879 }
1880
1881 /* call this function to flush any scheduled tasklet */
1882 static inline void iwl_synchronize_irq(struct iwl_priv *priv)
1883 {
1884         /* wait to make sure we flush pending tasklet*/
1885         synchronize_irq(priv->pci_dev->irq);
1886         tasklet_kill(&priv->irq_tasklet);
1887 }
1888
1889 static const char *desc_lookup(int i)
1890 {
1891         switch (i) {
1892         case 1:
1893                 return "FAIL";
1894         case 2:
1895                 return "BAD_PARAM";
1896         case 3:
1897                 return "BAD_CHECKSUM";
1898         case 4:
1899                 return "NMI_INTERRUPT";
1900         case 5:
1901                 return "SYSASSERT";
1902         case 6:
1903                 return "FATAL_ERROR";
1904         }
1905
1906         return "UNKNOWN";
1907 }
1908
1909 #define ERROR_START_OFFSET  (1 * sizeof(u32))
1910 #define ERROR_ELEM_SIZE     (7 * sizeof(u32))
1911
1912 static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
1913 {
1914         u32 i;
1915         u32 desc, time, count, base, data1;
1916         u32 blink1, blink2, ilink1, ilink2;
1917         int rc;
1918
1919         base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
1920
1921         if (!iwl3945_hw_valid_rtc_data_addr(base)) {
1922                 IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
1923                 return;
1924         }
1925
1926         rc = iwl_grab_nic_access(priv);
1927         if (rc) {
1928                 IWL_WARN(priv, "Can not read from adapter at this time.\n");
1929                 return;
1930         }
1931
1932         count = iwl_read_targ_mem(priv, base);
1933
1934         if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
1935                 IWL_ERR(priv, "Start IWL Error Log Dump:\n");
1936                 IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
1937                         priv->status, count);
1938         }
1939
1940         IWL_ERR(priv, "Desc       Time       asrtPC  blink2 "
1941                   "ilink1  nmiPC   Line\n");
1942         for (i = ERROR_START_OFFSET;
1943              i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
1944              i += ERROR_ELEM_SIZE) {
1945                 desc = iwl_read_targ_mem(priv, base + i);
1946                 time =
1947                     iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
1948                 blink1 =
1949                     iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
1950                 blink2 =
1951                     iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
1952                 ilink1 =
1953                     iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
1954                 ilink2 =
1955                     iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
1956                 data1 =
1957                     iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
1958
1959                 IWL_ERR(priv,
1960                         "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
1961                         desc_lookup(desc), desc, time, blink1, blink2,
1962                         ilink1, ilink2, data1);
1963         }
1964
1965         iwl_release_nic_access(priv);
1966
1967 }
1968
1969 #define EVENT_START_OFFSET  (6 * sizeof(u32))
1970
1971 /**
1972  * iwl3945_print_event_log - Dump error event log to syslog
1973  *
1974  * NOTE: Must be called with iwl_grab_nic_access() already obtained!
1975  */
1976 static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
1977                                 u32 num_events, u32 mode)
1978 {
1979         u32 i;
1980         u32 base;       /* SRAM byte address of event log header */
1981         u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
1982         u32 ptr;        /* SRAM byte address of log data */
1983         u32 ev, time, data; /* event log data */
1984
1985         if (num_events == 0)
1986                 return;
1987
1988         base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
1989
1990         if (mode == 0)
1991                 event_size = 2 * sizeof(u32);
1992         else
1993                 event_size = 3 * sizeof(u32);
1994
1995         ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
1996
1997         /* "time" is actually "data" for mode 0 (no timestamp).
1998          * place event id # at far right for easier visual parsing. */
1999         for (i = 0; i < num_events; i++) {
2000                 ev = iwl_read_targ_mem(priv, ptr);
2001                 ptr += sizeof(u32);
2002                 time = iwl_read_targ_mem(priv, ptr);
2003                 ptr += sizeof(u32);
2004                 if (mode == 0) {
2005                         /* data, ev */
2006                         IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
2007                 } else {
2008                         data = iwl_read_targ_mem(priv, ptr);
2009                         ptr += sizeof(u32);
2010                         IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
2011                 }
2012         }
2013 }
2014
2015 static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
2016 {
2017         int rc;
2018         u32 base;       /* SRAM byte address of event log header */
2019         u32 capacity;   /* event log capacity in # entries */
2020         u32 mode;       /* 0 - no timestamp, 1 - timestamp recorded */
2021         u32 num_wraps;  /* # times uCode wrapped to top of log */
2022         u32 next_entry; /* index of next entry to be written by uCode */
2023         u32 size;       /* # entries that we'll print */
2024
2025         base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
2026         if (!iwl3945_hw_valid_rtc_data_addr(base)) {
2027                 IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
2028                 return;
2029         }
2030
2031         rc = iwl_grab_nic_access(priv);
2032         if (rc) {
2033                 IWL_WARN(priv, "Can not read from adapter at this time.\n");
2034                 return;
2035         }
2036
2037         /* event log header */
2038         capacity = iwl_read_targ_mem(priv, base);
2039         mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
2040         num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
2041         next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
2042
2043         size = num_wraps ? capacity : next_entry;
2044
2045         /* bail out if nothing in log */
2046         if (size == 0) {
2047                 IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
2048                 iwl_release_nic_access(priv);
2049                 return;
2050         }
2051
2052         IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
2053                   size, num_wraps);
2054
2055         /* if uCode has wrapped back to top of log, start at the oldest entry,
2056          * i.e the next one that uCode would fill. */
2057         if (num_wraps)
2058                 iwl3945_print_event_log(priv, next_entry,
2059                                     capacity - next_entry, mode);
2060
2061         /* (then/else) start at top of log */
2062         iwl3945_print_event_log(priv, 0, next_entry, mode);
2063
2064         iwl_release_nic_access(priv);
2065 }
2066
2067 static void iwl3945_error_recovery(struct iwl_priv *priv)
2068 {
2069         unsigned long flags;
2070
2071         memcpy(&priv->staging_rxon, &priv->recovery_rxon,
2072                sizeof(priv->staging_rxon));
2073         priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2074         iwl3945_commit_rxon(priv);
2075
2076         iwl3945_add_station(priv, priv->bssid, 1, 0);
2077
2078         spin_lock_irqsave(&priv->lock, flags);
2079         priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
2080         priv->error_recovering = 0;
2081         spin_unlock_irqrestore(&priv->lock, flags);
2082 }
2083
2084 static void iwl3945_irq_tasklet(struct iwl_priv *priv)
2085 {
2086         u32 inta, handled = 0;
2087         u32 inta_fh;
2088         unsigned long flags;
2089 #ifdef CONFIG_IWLWIFI_DEBUG
2090         u32 inta_mask;
2091 #endif
2092
2093         spin_lock_irqsave(&priv->lock, flags);
2094
2095         /* Ack/clear/reset pending uCode interrupts.
2096          * Note:  Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
2097          *  and will clear only when CSR_FH_INT_STATUS gets cleared. */
2098         inta = iwl_read32(priv, CSR_INT);
2099         iwl_write32(priv, CSR_INT, inta);
2100
2101         /* Ack/clear/reset pending flow-handler (DMA) interrupts.
2102          * Any new interrupts that happen after this, either while we're
2103          * in this tasklet, or later, will show up in next ISR/tasklet. */
2104         inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2105         iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
2106
2107 #ifdef CONFIG_IWLWIFI_DEBUG
2108         if (priv->debug_level & IWL_DL_ISR) {
2109                 /* just for debug */
2110                 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2111                 IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2112                               inta, inta_mask, inta_fh);
2113         }
2114 #endif
2115
2116         /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
2117          * atomic, make sure that inta covers all the interrupts that
2118          * we've discovered, even if FH interrupt came in just after
2119          * reading CSR_INT. */
2120         if (inta_fh & CSR39_FH_INT_RX_MASK)
2121                 inta |= CSR_INT_BIT_FH_RX;
2122         if (inta_fh & CSR39_FH_INT_TX_MASK)
2123                 inta |= CSR_INT_BIT_FH_TX;
2124
2125         /* Now service all interrupt bits discovered above. */
2126         if (inta & CSR_INT_BIT_HW_ERR) {
2127                 IWL_ERR(priv, "Microcode HW error detected.  Restarting.\n");
2128
2129                 /* Tell the device to stop sending interrupts */
2130                 iwl_disable_interrupts(priv);
2131
2132                 iwl_irq_handle_error(priv);
2133
2134                 handled |= CSR_INT_BIT_HW_ERR;
2135
2136                 spin_unlock_irqrestore(&priv->lock, flags);
2137
2138                 return;
2139         }
2140
2141 #ifdef CONFIG_IWLWIFI_DEBUG
2142         if (priv->debug_level & (IWL_DL_ISR)) {
2143                 /* NIC fires this, but we don't use it, redundant with WAKEUP */
2144                 if (inta & CSR_INT_BIT_SCD)
2145                         IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
2146                                       "the frame/frames.\n");
2147
2148                 /* Alive notification via Rx interrupt will do the real work */
2149                 if (inta & CSR_INT_BIT_ALIVE)
2150                         IWL_DEBUG_ISR(priv, "Alive interrupt\n");
2151         }
2152 #endif
2153         /* Safely ignore these bits for debug checks below */
2154         inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
2155
2156         /* Error detected by uCode */
2157         if (inta & CSR_INT_BIT_SW_ERR) {
2158                 IWL_ERR(priv, "Microcode SW error detected. "
2159                         "Restarting 0x%X.\n", inta);
2160                 iwl_irq_handle_error(priv);
2161                 handled |= CSR_INT_BIT_SW_ERR;
2162         }
2163
2164         /* uCode wakes up after power-down sleep */
2165         if (inta & CSR_INT_BIT_WAKEUP) {
2166                 IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
2167                 iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
2168                 iwl_txq_update_write_ptr(priv, &priv->txq[0]);
2169                 iwl_txq_update_write_ptr(priv, &priv->txq[1]);
2170                 iwl_txq_update_write_ptr(priv, &priv->txq[2]);
2171                 iwl_txq_update_write_ptr(priv, &priv->txq[3]);
2172                 iwl_txq_update_write_ptr(priv, &priv->txq[4]);
2173                 iwl_txq_update_write_ptr(priv, &priv->txq[5]);
2174
2175                 handled |= CSR_INT_BIT_WAKEUP;
2176         }
2177
2178         /* All uCode command responses, including Tx command responses,
2179          * Rx "responses" (frame-received notification), and other
2180          * notifications from uCode come through here*/
2181         if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
2182                 iwl3945_rx_handle(priv);
2183                 handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
2184         }
2185
2186         if (inta & CSR_INT_BIT_FH_TX) {
2187                 IWL_DEBUG_ISR(priv, "Tx interrupt\n");
2188
2189                 iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
2190                 if (!iwl_grab_nic_access(priv)) {
2191                         iwl_write_direct32(priv, FH39_TCSR_CREDIT
2192                                              (FH39_SRVC_CHNL), 0x0);
2193                         iwl_release_nic_access(priv);
2194                 }
2195                 handled |= CSR_INT_BIT_FH_TX;
2196         }
2197
2198         if (inta & ~handled)
2199                 IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
2200
2201         if (inta & ~CSR_INI_SET_MASK) {
2202                 IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
2203                          inta & ~CSR_INI_SET_MASK);
2204                 IWL_WARN(priv, "   with FH_INT = 0x%08x\n", inta_fh);
2205         }
2206
2207         /* Re-enable all interrupts */
2208         /* only Re-enable if disabled by irq */
2209         if (test_bit(STATUS_INT_ENABLED, &priv->status))
2210                 iwl_enable_interrupts(priv);
2211
2212 #ifdef CONFIG_IWLWIFI_DEBUG
2213         if (priv->debug_level & (IWL_DL_ISR)) {
2214                 inta = iwl_read32(priv, CSR_INT);
2215                 inta_mask = iwl_read32(priv, CSR_INT_MASK);
2216                 inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2217                 IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
2218                         "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
2219         }
2220 #endif
2221         spin_unlock_irqrestore(&priv->lock, flags);
2222 }
2223
2224 static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
2225                                          enum ieee80211_band band,
2226                                      u8 is_active, u8 n_probes,
2227                                      struct iwl3945_scan_channel *scan_ch)
2228 {
2229         const struct ieee80211_channel *channels = NULL;
2230         const struct ieee80211_supported_band *sband;
2231         const struct iwl_channel_info *ch_info;
2232         u16 passive_dwell = 0;
2233         u16 active_dwell = 0;
2234         int added, i;
2235
2236         sband = iwl_get_hw_mode(priv, band);
2237         if (!sband)
2238                 return 0;
2239
2240         channels = sband->channels;
2241
2242         active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
2243         passive_dwell = iwl_get_passive_dwell_time(priv, band);
2244
2245         if (passive_dwell <= active_dwell)
2246                 passive_dwell = active_dwell + 1;
2247
2248         for (i = 0, added = 0; i < sband->n_channels; i++) {
2249                 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
2250                         continue;
2251
2252                 scan_ch->channel = channels[i].hw_value;
2253
2254                 ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
2255                 if (!is_channel_valid(ch_info)) {
2256                         IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
2257                                        scan_ch->channel);
2258                         continue;
2259                 }
2260
2261                 scan_ch->active_dwell = cpu_to_le16(active_dwell);
2262                 scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
2263                 /* If passive , set up for auto-switch
2264                  *  and use long active_dwell time.
2265                  */
2266                 if (!is_active || is_channel_passive(ch_info) ||
2267                     (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
2268                         scan_ch->type = 0;      /* passive */
2269                         if (IWL_UCODE_API(priv->ucode_ver) == 1)
2270                                 scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
2271                 } else {
2272                         scan_ch->type = 1;      /* active */
2273                 }
2274
2275                 /* Set direct probe bits. These may be used both for active
2276                  * scan channels (probes gets sent right away),
2277                  * or for passive channels (probes get se sent only after
2278                  * hearing clear Rx packet).*/
2279                 if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
2280                         if (n_probes)
2281                                 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
2282                 } else {
2283                         /* uCode v1 does not allow setting direct probe bits on
2284                          * passive channel. */
2285                         if ((scan_ch->type & 1) && n_probes)
2286                                 scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
2287                 }
2288
2289                 /* Set txpower levels to defaults */
2290                 scan_ch->tpc.dsp_atten = 110;
2291                 /* scan_pwr_info->tpc.dsp_atten; */
2292
2293                 /*scan_pwr_info->tpc.tx_gain; */
2294                 if (band == IEEE80211_BAND_5GHZ)
2295                         scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
2296                 else {
2297                         scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
2298                         /* NOTE: if we were doing 6Mb OFDM for scans we'd use
2299                          * power level:
2300                          * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
2301                          */
2302                 }
2303
2304                 IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
2305                                scan_ch->channel,
2306                                (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
2307                                (scan_ch->type & 1) ?
2308                                active_dwell : passive_dwell);
2309
2310                 scan_ch++;
2311                 added++;
2312         }
2313
2314         IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
2315         return added;
2316 }
2317
2318 static void iwl3945_init_hw_rates(struct iwl_priv *priv,
2319                               struct ieee80211_rate *rates)
2320 {
2321         int i;
2322
2323         for (i = 0; i < IWL_RATE_COUNT; i++) {
2324                 rates[i].bitrate = iwl3945_rates[i].ieee * 5;
2325                 rates[i].hw_value = i; /* Rate scaling will work on indexes */
2326                 rates[i].hw_value_short = i;
2327                 rates[i].flags = 0;
2328                 if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
2329                         /*
2330                          * If CCK != 1M then set short preamble rate flag.
2331                          */
2332                         rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
2333                                 0 : IEEE80211_RATE_SHORT_PREAMBLE;
2334                 }
2335         }
2336 }
2337
2338 /******************************************************************************
2339  *
2340  * uCode download functions
2341  *
2342  ******************************************************************************/
2343
2344 static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
2345 {
2346         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
2347         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
2348         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2349         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
2350         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2351         iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
2352 }
2353
2354 /**
2355  * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
2356  *     looking at all data.
2357  */
2358 static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
2359 {
2360         u32 val;
2361         u32 save_len = len;
2362         int rc = 0;
2363         u32 errcnt;
2364
2365         IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
2366
2367         rc = iwl_grab_nic_access(priv);
2368         if (rc)
2369                 return rc;
2370
2371         iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
2372                                IWL39_RTC_INST_LOWER_BOUND);
2373
2374         errcnt = 0;
2375         for (; len > 0; len -= sizeof(u32), image++) {
2376                 /* read data comes through single port, auto-incr addr */
2377                 /* NOTE: Use the debugless read so we don't flood kernel log
2378                  * if IWL_DL_IO is set */
2379                 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
2380                 if (val != le32_to_cpu(*image)) {
2381                         IWL_ERR(priv, "uCode INST section is invalid at "
2382                                   "offset 0x%x, is 0x%x, s/b 0x%x\n",
2383                                   save_len - len, val, le32_to_cpu(*image));
2384                         rc = -EIO;
2385                         errcnt++;
2386                         if (errcnt >= 20)
2387                                 break;
2388                 }
2389         }
2390
2391         iwl_release_nic_access(priv);
2392
2393         if (!errcnt)
2394                 IWL_DEBUG_INFO(priv,
2395                         "ucode image in INSTRUCTION memory is good\n");
2396
2397         return rc;
2398 }
2399
2400
2401 /**
2402  * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
2403  *   using sample data 100 bytes apart.  If these sample points are good,
2404  *   it's a pretty good bet that everything between them is good, too.
2405  */
2406 static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
2407 {
2408         u32 val;
2409         int rc = 0;
2410         u32 errcnt = 0;
2411         u32 i;
2412
2413         IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
2414
2415         rc = iwl_grab_nic_access(priv);
2416         if (rc)
2417                 return rc;
2418
2419         for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
2420                 /* read data comes through single port, auto-incr addr */
2421                 /* NOTE: Use the debugless read so we don't flood kernel log
2422                  * if IWL_DL_IO is set */
2423                 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
2424                         i + IWL39_RTC_INST_LOWER_BOUND);
2425                 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
2426                 if (val != le32_to_cpu(*image)) {
2427 #if 0 /* Enable this if you want to see details */
2428                         IWL_ERR(priv, "uCode INST section is invalid at "
2429                                   "offset 0x%x, is 0x%x, s/b 0x%x\n",
2430                                   i, val, *image);
2431 #endif
2432                         rc = -EIO;
2433                         errcnt++;
2434                         if (errcnt >= 3)
2435                                 break;
2436                 }
2437         }
2438
2439         iwl_release_nic_access(priv);
2440
2441         return rc;
2442 }
2443
2444
2445 /**
2446  * iwl3945_verify_ucode - determine which instruction image is in SRAM,
2447  *    and verify its contents
2448  */
2449 static int iwl3945_verify_ucode(struct iwl_priv *priv)
2450 {
2451         __le32 *image;
2452         u32 len;
2453         int rc = 0;
2454
2455         /* Try bootstrap */
2456         image = (__le32 *)priv->ucode_boot.v_addr;
2457         len = priv->ucode_boot.len;
2458         rc = iwl3945_verify_inst_sparse(priv, image, len);
2459         if (rc == 0) {
2460                 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
2461                 return 0;
2462         }
2463
2464         /* Try initialize */
2465         image = (__le32 *)priv->ucode_init.v_addr;
2466         len = priv->ucode_init.len;
2467         rc = iwl3945_verify_inst_sparse(priv, image, len);
2468         if (rc == 0) {
2469                 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
2470                 return 0;
2471         }
2472
2473         /* Try runtime/protocol */
2474         image = (__le32 *)priv->ucode_code.v_addr;
2475         len = priv->ucode_code.len;
2476         rc = iwl3945_verify_inst_sparse(priv, image, len);
2477         if (rc == 0) {
2478                 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
2479                 return 0;
2480         }
2481
2482         IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
2483
2484         /* Since nothing seems to match, show first several data entries in
2485          * instruction SRAM, so maybe visual inspection will give a clue.
2486          * Selection of bootstrap image (vs. other images) is arbitrary. */
2487         image = (__le32 *)priv->ucode_boot.v_addr;
2488         len = priv->ucode_boot.len;
2489         rc = iwl3945_verify_inst_full(priv, image, len);
2490
2491         return rc;
2492 }
2493
2494 static void iwl3945_nic_start(struct iwl_priv *priv)
2495 {
2496         /* Remove all resets to allow NIC to operate */
2497         iwl_write32(priv, CSR_RESET, 0);
2498 }
2499
2500 /**
2501  * iwl3945_read_ucode - Read uCode images from disk file.
2502  *
2503  * Copy into buffers for card to fetch via bus-mastering
2504  */
2505 static int iwl3945_read_ucode(struct iwl_priv *priv)
2506 {
2507         struct iwl_ucode *ucode;
2508         int ret = -EINVAL, index;
2509         const struct firmware *ucode_raw;
2510         /* firmware file name contains uCode/driver compatibility version */
2511         const char *name_pre = priv->cfg->fw_name_pre;
2512         const unsigned int api_max = priv->cfg->ucode_api_max;
2513         const unsigned int api_min = priv->cfg->ucode_api_min;
2514         char buf[25];
2515         u8 *src;
2516         size_t len;
2517         u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
2518
2519         /* Ask kernel firmware_class module to get the boot firmware off disk.
2520          * request_firmware() is synchronous, file is in memory on return. */
2521         for (index = api_max; index >= api_min; index--) {
2522                 sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
2523                 ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
2524                 if (ret < 0) {
2525                         IWL_ERR(priv, "%s firmware file req failed: %d\n",
2526                                   buf, ret);
2527                         if (ret == -ENOENT)
2528                                 continue;
2529                         else
2530                                 goto error;
2531                 } else {
2532                         if (index < api_max)
2533                                 IWL_ERR(priv, "Loaded firmware %s, "
2534                                         "which is deprecated. "
2535                                         " Please use API v%u instead.\n",
2536                                           buf, api_max);
2537                         IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
2538                                        "(%zd bytes) from disk\n",
2539                                        buf, ucode_raw->size);
2540                         break;
2541                 }
2542         }
2543
2544         if (ret < 0)
2545                 goto error;
2546
2547         /* Make sure that we got at least our header! */
2548         if (ucode_raw->size < sizeof(*ucode)) {
2549                 IWL_ERR(priv, "File size way too small!\n");
2550                 ret = -EINVAL;
2551                 goto err_release;
2552         }
2553
2554         /* Data from ucode file:  header followed by uCode images */
2555         ucode = (void *)ucode_raw->data;
2556
2557         priv->ucode_ver = le32_to_cpu(ucode->ver);
2558         api_ver = IWL_UCODE_API(priv->ucode_ver);
2559         inst_size = le32_to_cpu(ucode->inst_size);
2560         data_size = le32_to_cpu(ucode->data_size);
2561         init_size = le32_to_cpu(ucode->init_size);
2562         init_data_size = le32_to_cpu(ucode->init_data_size);
2563         boot_size = le32_to_cpu(ucode->boot_size);
2564
2565         /* api_ver should match the api version forming part of the
2566          * firmware filename ... but we don't check for that and only rely
2567          * on the API version read from firmware header from here on forward */
2568
2569         if (api_ver < api_min || api_ver > api_max) {
2570                 IWL_ERR(priv, "Driver unable to support your firmware API. "
2571                           "Driver supports v%u, firmware is v%u.\n",
2572                           api_max, api_ver);
2573                 priv->ucode_ver = 0;
2574                 ret = -EINVAL;
2575                 goto err_release;
2576         }
2577         if (api_ver != api_max)
2578                 IWL_ERR(priv, "Firmware has old API version. Expected %u, "
2579                           "got %u. New firmware can be obtained "
2580                           "from http://www.intellinuxwireless.org.\n",
2581                           api_max, api_ver);
2582
2583         IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
2584                 IWL_UCODE_MAJOR(priv->ucode_ver),
2585                 IWL_UCODE_MINOR(priv->ucode_ver),
2586                 IWL_UCODE_API(priv->ucode_ver),
2587                 IWL_UCODE_SERIAL(priv->ucode_ver));
2588
2589         IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
2590                        priv->ucode_ver);
2591         IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
2592                        inst_size);
2593         IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
2594                        data_size);
2595         IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
2596                        init_size);
2597         IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
2598                        init_data_size);
2599         IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
2600                        boot_size);
2601
2602
2603         /* Verify size of file vs. image size info in file's header */
2604         if (ucode_raw->size < sizeof(*ucode) +
2605                 inst_size + data_size + init_size +
2606                 init_data_size + boot_size) {
2607
2608                 IWL_DEBUG_INFO(priv, "uCode file size %zd too small\n",
2609                                ucode_raw->size);
2610                 ret = -EINVAL;
2611                 goto err_release;
2612         }
2613
2614         /* Verify that uCode images will fit in card's SRAM */
2615         if (inst_size > IWL39_MAX_INST_SIZE) {
2616                 IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
2617                                inst_size);
2618                 ret = -EINVAL;
2619                 goto err_release;
2620         }
2621
2622         if (data_size > IWL39_MAX_DATA_SIZE) {
2623                 IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
2624                                data_size);
2625                 ret = -EINVAL;
2626                 goto err_release;
2627         }
2628         if (init_size > IWL39_MAX_INST_SIZE) {
2629                 IWL_DEBUG_INFO(priv,
2630                                 "uCode init instr len %d too large to fit in\n",
2631                                 init_size);
2632                 ret = -EINVAL;
2633                 goto err_release;
2634         }
2635         if (init_data_size > IWL39_MAX_DATA_SIZE) {
2636                 IWL_DEBUG_INFO(priv,
2637                                 "uCode init data len %d too large to fit in\n",
2638                                 init_data_size);
2639                 ret = -EINVAL;
2640                 goto err_release;
2641         }
2642         if (boot_size > IWL39_MAX_BSM_SIZE) {
2643                 IWL_DEBUG_INFO(priv,
2644                                 "uCode boot instr len %d too large to fit in\n",
2645                                 boot_size);
2646                 ret = -EINVAL;
2647                 goto err_release;
2648         }
2649
2650         /* Allocate ucode buffers for card's bus-master loading ... */
2651
2652         /* Runtime instructions and 2 copies of data:
2653          * 1) unmodified from disk
2654          * 2) backup cache for save/restore during power-downs */
2655         priv->ucode_code.len = inst_size;
2656         iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
2657
2658         priv->ucode_data.len = data_size;
2659         iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
2660
2661         priv->ucode_data_backup.len = data_size;
2662         iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
2663
2664         if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
2665             !priv->ucode_data_backup.v_addr)
2666                 goto err_pci_alloc;
2667
2668         /* Initialization instructions and data */
2669         if (init_size && init_data_size) {
2670                 priv->ucode_init.len = init_size;
2671                 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
2672
2673                 priv->ucode_init_data.len = init_data_size;
2674                 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
2675
2676                 if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
2677                         goto err_pci_alloc;
2678         }
2679
2680         /* Bootstrap (instructions only, no data) */
2681         if (boot_size) {
2682                 priv->ucode_boot.len = boot_size;
2683                 iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
2684
2685                 if (!priv->ucode_boot.v_addr)
2686                         goto err_pci_alloc;
2687         }
2688
2689         /* Copy images into buffers for card's bus-master reads ... */
2690
2691         /* Runtime instructions (first block of data in file) */
2692         src = &ucode->data[0];
2693         len = priv->ucode_code.len;
2694         IWL_DEBUG_INFO(priv,
2695                 "Copying (but not loading) uCode instr len %zd\n", len);
2696         memcpy(priv->ucode_code.v_addr, src, len);
2697         IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
2698                 priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
2699
2700         /* Runtime data (2nd block)
2701          * NOTE:  Copy into backup buffer will be done in iwl3945_up()  */
2702         src = &ucode->data[inst_size];
2703         len = priv->ucode_data.len;
2704         IWL_DEBUG_INFO(priv,
2705                 "Copying (but not loading) uCode data len %zd\n", len);
2706         memcpy(priv->ucode_data.v_addr, src, len);
2707         memcpy(priv->ucode_data_backup.v_addr, src, len);
2708
2709         /* Initialization instructions (3rd block) */
2710         if (init_size) {
2711                 src = &ucode->data[inst_size + data_size];
2712                 len = priv->ucode_init.len;
2713                 IWL_DEBUG_INFO(priv,
2714                         "Copying (but not loading) init instr len %zd\n", len);
2715                 memcpy(priv->ucode_init.v_addr, src, len);
2716         }
2717
2718         /* Initialization data (4th block) */
2719         if (init_data_size) {
2720                 src = &ucode->data[inst_size + data_size + init_size];
2721                 len = priv->ucode_init_data.len;
2722                 IWL_DEBUG_INFO(priv,
2723                         "Copying (but not loading) init data len %zd\n", len);
2724                 memcpy(priv->ucode_init_data.v_addr, src, len);
2725         }
2726
2727         /* Bootstrap instructions (5th block) */
2728         src = &ucode->data[inst_size + data_size + init_size + init_data_size];
2729         len = priv->ucode_boot.len;
2730         IWL_DEBUG_INFO(priv,
2731                 "Copying (but not loading) boot instr len %zd\n", len);
2732         memcpy(priv->ucode_boot.v_addr, src, len);
2733
2734         /* We have our copies now, allow OS release its copies */
2735         release_firmware(ucode_raw);
2736         return 0;
2737
2738  err_pci_alloc:
2739         IWL_ERR(priv, "failed to allocate pci memory\n");
2740         ret = -ENOMEM;
2741         iwl3945_dealloc_ucode_pci(priv);
2742
2743  err_release:
2744         release_firmware(ucode_raw);
2745
2746  error:
2747         return ret;
2748 }
2749
2750
2751 /**
2752  * iwl3945_set_ucode_ptrs - Set uCode address location
2753  *
2754  * Tell initialization uCode where to find runtime uCode.
2755  *
2756  * BSM registers initially contain pointers to initialization uCode.
2757  * We need to replace them to load runtime uCode inst and data,
2758  * and to save runtime data when powering down.
2759  */
2760 static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
2761 {
2762         dma_addr_t pinst;
2763         dma_addr_t pdata;
2764         int rc = 0;
2765         unsigned long flags;
2766
2767         /* bits 31:0 for 3945 */
2768         pinst = priv->ucode_code.p_addr;
2769         pdata = priv->ucode_data_backup.p_addr;
2770
2771         spin_lock_irqsave(&priv->lock, flags);
2772         rc = iwl_grab_nic_access(priv);
2773         if (rc) {
2774                 spin_unlock_irqrestore(&priv->lock, flags);
2775                 return rc;
2776         }
2777
2778         /* Tell bootstrap uCode where to find image to load */
2779         iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
2780         iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
2781         iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
2782                                  priv->ucode_data.len);
2783
2784         /* Inst byte count must be last to set up, bit 31 signals uCode
2785          *   that all new ptr/size info is in place */
2786         iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
2787                                  priv->ucode_code.len | BSM_DRAM_INST_LOAD);
2788
2789         iwl_release_nic_access(priv);
2790
2791         spin_unlock_irqrestore(&priv->lock, flags);
2792
2793         IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
2794
2795         return rc;
2796 }
2797
2798 /**
2799  * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
2800  *
2801  * Called after REPLY_ALIVE notification received from "initialize" uCode.
2802  *
2803  * Tell "initialize" uCode to go ahead and load the runtime uCode.
2804  */
2805 static void iwl3945_init_alive_start(struct iwl_priv *priv)
2806 {
2807         /* Check alive response for "valid" sign from uCode */
2808         if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
2809                 /* We had an error bringing up the hardware, so take it
2810                  * all the way back down so we can try again */
2811                 IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
2812                 goto restart;
2813         }
2814
2815         /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
2816          * This is a paranoid check, because we would not have gotten the
2817          * "initialize" alive if code weren't properly loaded.  */
2818         if (iwl3945_verify_ucode(priv)) {
2819                 /* Runtime instruction load was bad;
2820                  * take it all the way back down so we can try again */
2821                 IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
2822                 goto restart;
2823         }
2824
2825         /* Send pointers to protocol/runtime uCode image ... init code will
2826          * load and launch runtime uCode, which will send us another "Alive"
2827          * notification. */
2828         IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
2829         if (iwl3945_set_ucode_ptrs(priv)) {
2830                 /* Runtime instruction load won't happen;
2831                  * take it all the way back down so we can try again */
2832                 IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
2833                 goto restart;
2834         }
2835         return;
2836
2837  restart:
2838         queue_work(priv->workqueue, &priv->restart);
2839 }
2840
2841
2842 /* temporary */
2843 static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
2844                                      struct sk_buff *skb);
2845
2846 /**
2847  * iwl3945_alive_start - called after REPLY_ALIVE notification received
2848  *                   from protocol/runtime uCode (initialization uCode's
2849  *                   Alive gets handled by iwl3945_init_alive_start()).
2850  */
2851 static void iwl3945_alive_start(struct iwl_priv *priv)
2852 {
2853         int rc = 0;
2854         int thermal_spin = 0;
2855         u32 rfkill;
2856
2857         IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
2858
2859         if (priv->card_alive.is_valid != UCODE_VALID_OK) {
2860                 /* We had an error bringing up the hardware, so take it
2861                  * all the way back down so we can try again */
2862                 IWL_DEBUG_INFO(priv, "Alive failed.\n");
2863                 goto restart;
2864         }
2865
2866         /* Initialize uCode has loaded Runtime uCode ... verify inst image.
2867          * This is a paranoid check, because we would not have gotten the
2868          * "runtime" alive if code weren't properly loaded.  */
2869         if (iwl3945_verify_ucode(priv)) {
2870                 /* Runtime instruction load was bad;
2871                  * take it all the way back down so we can try again */
2872                 IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
2873                 goto restart;
2874         }
2875
2876         iwl3945_clear_stations_table(priv);
2877
2878         rc = iwl_grab_nic_access(priv);
2879         if (rc) {
2880                 IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
2881                 return;
2882         }
2883
2884         rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
2885         IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
2886         iwl_release_nic_access(priv);
2887
2888         if (rfkill & 0x1) {
2889                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2890                 /* if RFKILL is not on, then wait for thermal
2891                  * sensor in adapter to kick in */
2892                 while (iwl3945_hw_get_temperature(priv) == 0) {
2893                         thermal_spin++;
2894                         udelay(10);
2895                 }
2896
2897                 if (thermal_spin)
2898                         IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
2899                                        thermal_spin * 10);
2900         } else
2901                 set_bit(STATUS_RF_KILL_HW, &priv->status);
2902
2903         /* After the ALIVE response, we can send commands to 3945 uCode */
2904         set_bit(STATUS_ALIVE, &priv->status);
2905
2906         /* Clear out the uCode error bit if it is set */
2907         clear_bit(STATUS_FW_ERROR, &priv->status);
2908
2909         if (iwl_is_rfkill(priv))
2910                 return;
2911
2912         ieee80211_wake_queues(priv->hw);
2913
2914         priv->active_rate = priv->rates_mask;
2915         priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
2916
2917         iwl_power_update_mode(priv, false);
2918
2919         if (iwl_is_associated(priv)) {
2920                 struct iwl3945_rxon_cmd *active_rxon =
2921                                 (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
2922
2923                 memcpy(&priv->staging_rxon, &priv->active_rxon,
2924                        sizeof(priv->staging_rxon));
2925                 active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2926         } else {
2927                 /* Initialize our rx_config data */
2928                 iwl_connection_init_rx_config(priv, priv->iw_mode);
2929         }
2930
2931         /* Configure Bluetooth device coexistence support */
2932         iwl_send_bt_config(priv);
2933
2934         /* Configure the adapter for unassociated operation */
2935         iwl3945_commit_rxon(priv);
2936
2937         iwl3945_reg_txpower_periodic(priv);
2938
2939         iwl3945_led_register(priv);
2940
2941         IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
2942         set_bit(STATUS_READY, &priv->status);
2943         wake_up_interruptible(&priv->wait_command_queue);
2944
2945         if (priv->error_recovering)
2946                 iwl3945_error_recovery(priv);
2947
2948         /* reassociate for ADHOC mode */
2949         if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
2950                 struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
2951                                                                 priv->vif);
2952                 if (beacon)
2953                         iwl3945_mac_beacon_update(priv->hw, beacon);
2954         }
2955
2956         return;
2957
2958  restart:
2959         queue_work(priv->workqueue, &priv->restart);
2960 }
2961
2962 static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
2963
2964 static void __iwl3945_down(struct iwl_priv *priv)
2965 {
2966         unsigned long flags;
2967         int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
2968         struct ieee80211_conf *conf = NULL;
2969
2970         IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
2971
2972         conf = ieee80211_get_hw_conf(priv->hw);
2973
2974         if (!exit_pending)
2975                 set_bit(STATUS_EXIT_PENDING, &priv->status);
2976
2977         iwl3945_led_unregister(priv);
2978         iwl3945_clear_stations_table(priv);
2979
2980         /* Unblock any waiting calls */
2981         wake_up_interruptible_all(&priv->wait_command_queue);
2982
2983         /* Wipe out the EXIT_PENDING status bit if we are not actually
2984          * exiting the module */
2985         if (!exit_pending)
2986                 clear_bit(STATUS_EXIT_PENDING, &priv->status);
2987
2988         /* stop and reset the on-board processor */
2989         iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
2990
2991         /* tell the device to stop sending interrupts */
2992         spin_lock_irqsave(&priv->lock, flags);
2993         iwl_disable_interrupts(priv);
2994         spin_unlock_irqrestore(&priv->lock, flags);
2995         iwl_synchronize_irq(priv);
2996
2997         if (priv->mac80211_registered)
2998                 ieee80211_stop_queues(priv->hw);
2999
3000         /* If we have not previously called iwl3945_init() then
3001          * clear all bits but the RF Kill and SUSPEND bits and return */
3002         if (!iwl_is_init(priv)) {
3003                 priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
3004                                         STATUS_RF_KILL_HW |
3005                                test_bit(STATUS_RF_KILL_SW, &priv->status) <<
3006                                         STATUS_RF_KILL_SW |
3007                                test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
3008                                         STATUS_GEO_CONFIGURED |
3009                                test_bit(STATUS_IN_SUSPEND, &priv->status) <<
3010                                         STATUS_IN_SUSPEND |
3011                                 test_bit(STATUS_EXIT_PENDING, &priv->status) <<
3012                                         STATUS_EXIT_PENDING;
3013                 goto exit;
3014         }
3015
3016         /* ...otherwise clear out all the status bits but the RF Kill and
3017          * SUSPEND bits and continue taking the NIC down. */
3018         priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
3019                                 STATUS_RF_KILL_HW |
3020                         test_bit(STATUS_RF_KILL_SW, &priv->status) <<
3021                                 STATUS_RF_KILL_SW |
3022                         test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
3023                                 STATUS_GEO_CONFIGURED |
3024                         test_bit(STATUS_IN_SUSPEND, &priv->status) <<
3025                                 STATUS_IN_SUSPEND |
3026                         test_bit(STATUS_FW_ERROR, &priv->status) <<
3027                                 STATUS_FW_ERROR |
3028                         test_bit(STATUS_EXIT_PENDING, &priv->status) <<
3029                                 STATUS_EXIT_PENDING;
3030
3031         priv->cfg->ops->lib->apm_ops.reset(priv);
3032         spin_lock_irqsave(&priv->lock, flags);
3033         iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
3034         spin_unlock_irqrestore(&priv->lock, flags);
3035
3036         iwl3945_hw_txq_ctx_stop(priv);
3037         iwl3945_hw_rxq_stop(priv);
3038
3039         spin_lock_irqsave(&priv->lock, flags);
3040         if (!iwl_grab_nic_access(priv)) {
3041                 iwl_write_prph(priv, APMG_CLK_DIS_REG,
3042                                          APMG_CLK_VAL_DMA_CLK_RQT);
3043                 iwl_release_nic_access(priv);
3044         }
3045         spin_unlock_irqrestore(&priv->lock, flags);
3046
3047         udelay(5);
3048
3049         if (exit_pending || test_bit(STATUS_IN_SUSPEND, &priv->status))
3050                 priv->cfg->ops->lib->apm_ops.stop(priv);
3051         else
3052                 priv->cfg->ops->lib->apm_ops.reset(priv);
3053
3054  exit:
3055         memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
3056
3057         if (priv->ibss_beacon)
3058                 dev_kfree_skb(priv->ibss_beacon);
3059         priv->ibss_beacon = NULL;
3060
3061         /* clear out any free frames */
3062         iwl3945_clear_free_frames(priv);
3063 }
3064
3065 static void iwl3945_down(struct iwl_priv *priv)
3066 {
3067         mutex_lock(&priv->mutex);
3068         __iwl3945_down(priv);
3069         mutex_unlock(&priv->mutex);
3070
3071         iwl3945_cancel_deferred_work(priv);
3072 }
3073
3074 #define MAX_HW_RESTARTS 5
3075
3076 static int __iwl3945_up(struct iwl_priv *priv)
3077 {
3078         int rc, i;
3079
3080         if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3081                 IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
3082                 return -EIO;
3083         }
3084
3085         if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
3086                 IWL_WARN(priv, "Radio disabled by SW RF kill (module "
3087                             "parameter)\n");
3088                 return -ENODEV;
3089         }
3090
3091         if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
3092                 IWL_ERR(priv, "ucode not available for device bring up\n");
3093                 return -EIO;
3094         }
3095
3096         /* If platform's RF_KILL switch is NOT set to KILL */
3097         if (iwl_read32(priv, CSR_GP_CNTRL) &
3098                                 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3099                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3100         else {
3101                 set_bit(STATUS_RF_KILL_HW, &priv->status);
3102                 if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
3103                         IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
3104                         return -ENODEV;
3105                 }
3106         }
3107
3108         iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
3109
3110         rc = iwl3945_hw_nic_init(priv);
3111         if (rc) {
3112                 IWL_ERR(priv, "Unable to int nic\n");
3113                 return rc;
3114         }
3115
3116         /* make sure rfkill handshake bits are cleared */
3117         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3118         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
3119                     CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
3120
3121         /* clear (again), then enable host interrupts */
3122         iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
3123         iwl_enable_interrupts(priv);
3124
3125         /* really make sure rfkill handshake bits are cleared */
3126         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3127         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
3128
3129         /* Copy original ucode data image from disk into backup cache.
3130          * This will be used to initialize the on-board processor's
3131          * data SRAM for a clean start when the runtime program first loads. */
3132         memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
3133                priv->ucode_data.len);
3134
3135         /* We return success when we resume from suspend and rf_kill is on. */
3136         if (test_bit(STATUS_RF_KILL_HW, &priv->status))
3137                 return 0;
3138
3139         for (i = 0; i < MAX_HW_RESTARTS; i++) {
3140
3141                 iwl3945_clear_stations_table(priv);
3142
3143                 /* load bootstrap state machine,
3144                  * load bootstrap program into processor's memory,
3145                  * prepare to load the "initialize" uCode */
3146                 priv->cfg->ops->lib->load_ucode(priv);
3147
3148                 if (rc) {
3149                         IWL_ERR(priv,
3150                                 "Unable to set up bootstrap uCode: %d\n", rc);
3151                         continue;
3152                 }
3153
3154                 /* start card; "initialize" will load runtime ucode */
3155                 iwl3945_nic_start(priv);
3156
3157                 IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
3158
3159                 return 0;
3160         }
3161
3162         set_bit(STATUS_EXIT_PENDING, &priv->status);
3163         __iwl3945_down(priv);
3164         clear_bit(STATUS_EXIT_PENDING, &priv->status);
3165
3166         /* tried to restart and config the device for as long as our
3167          * patience could withstand */
3168         IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
3169         return -EIO;
3170 }
3171
3172
3173 /*****************************************************************************
3174  *
3175  * Workqueue callbacks
3176  *
3177  *****************************************************************************/
3178
3179 static void iwl3945_bg_init_alive_start(struct work_struct *data)
3180 {
3181         struct iwl_priv *priv =
3182             container_of(data, struct iwl_priv, init_alive_start.work);
3183
3184         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3185                 return;
3186
3187         mutex_lock(&priv->mutex);
3188         iwl3945_init_alive_start(priv);
3189         mutex_unlock(&priv->mutex);
3190 }
3191
3192 static void iwl3945_bg_alive_start(struct work_struct *data)
3193 {
3194         struct iwl_priv *priv =
3195             container_of(data, struct iwl_priv, alive_start.work);
3196
3197         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3198                 return;
3199
3200         mutex_lock(&priv->mutex);
3201         iwl3945_alive_start(priv);
3202         mutex_unlock(&priv->mutex);
3203 }
3204
3205 static void iwl3945_rfkill_poll(struct work_struct *data)
3206 {
3207         struct iwl_priv *priv =
3208             container_of(data, struct iwl_priv, rfkill_poll.work);
3209         unsigned long status = priv->status;
3210
3211         if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
3212                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
3213         else
3214                 set_bit(STATUS_RF_KILL_HW, &priv->status);
3215
3216         if (test_bit(STATUS_RF_KILL_HW, &status) != test_bit(STATUS_RF_KILL_HW, &priv->status))
3217                 queue_work(priv->workqueue, &priv->rf_kill);
3218
3219         queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3220                            round_jiffies_relative(2 * HZ));
3221
3222 }
3223
3224 #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
3225 static void iwl3945_bg_request_scan(struct work_struct *data)
3226 {
3227         struct iwl_priv *priv =
3228             container_of(data, struct iwl_priv, request_scan);
3229         struct iwl_host_cmd cmd = {
3230                 .id = REPLY_SCAN_CMD,
3231                 .len = sizeof(struct iwl3945_scan_cmd),
3232                 .meta.flags = CMD_SIZE_HUGE,
3233         };
3234         int rc = 0;
3235         struct iwl3945_scan_cmd *scan;
3236         struct ieee80211_conf *conf = NULL;
3237         u8 n_probes = 2;
3238         enum ieee80211_band band;
3239         DECLARE_SSID_BUF(ssid);
3240
3241         conf = ieee80211_get_hw_conf(priv->hw);
3242
3243         mutex_lock(&priv->mutex);
3244
3245         if (!iwl_is_ready(priv)) {
3246                 IWL_WARN(priv, "request scan called when driver not ready.\n");
3247                 goto done;
3248         }
3249
3250         /* Make sure the scan wasn't canceled before this queued work
3251          * was given the chance to run... */
3252         if (!test_bit(STATUS_SCANNING, &priv->status))
3253                 goto done;
3254
3255         /* This should never be called or scheduled if there is currently
3256          * a scan active in the hardware. */
3257         if (test_bit(STATUS_SCAN_HW, &priv->status)) {
3258                 IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests  "
3259                                 "Ignoring second request.\n");
3260                 rc = -EIO;
3261                 goto done;
3262         }
3263
3264         if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
3265                 IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
3266                 goto done;
3267         }
3268
3269         if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
3270                 IWL_DEBUG_HC(priv,
3271                         "Scan request while abort pending. Queuing.\n");
3272                 goto done;
3273         }
3274
3275         if (iwl_is_rfkill(priv)) {
3276                 IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
3277                 goto done;
3278         }
3279
3280         if (!test_bit(STATUS_READY, &priv->status)) {
3281                 IWL_DEBUG_HC(priv,
3282                         "Scan request while uninitialized. Queuing.\n");
3283                 goto done;
3284         }
3285
3286         if (!priv->scan_bands) {
3287                 IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
3288                 goto done;
3289         }
3290
3291         if (!priv->scan) {
3292                 priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
3293                                      IWL_MAX_SCAN_SIZE, GFP_KERNEL);
3294                 if (!priv->scan) {
3295                         rc = -ENOMEM;
3296                         goto done;
3297                 }
3298         }
3299         scan = priv->scan;
3300         memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
3301
3302         scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
3303         scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
3304
3305         if (iwl_is_associated(priv)) {
3306                 u16 interval = 0;
3307                 u32 extra;
3308                 u32 suspend_time = 100;
3309                 u32 scan_suspend_time = 100;
3310                 unsigned long flags;
3311
3312                 IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
3313
3314                 spin_lock_irqsave(&priv->lock, flags);
3315                 interval = priv->beacon_int;
3316                 spin_unlock_irqrestore(&priv->lock, flags);
3317
3318                 scan->suspend_time = 0;
3319                 scan->max_out_time = cpu_to_le32(200 * 1024);
3320                 if (!interval)
3321                         interval = suspend_time;
3322                 /*
3323                  * suspend time format:
3324                  *  0-19: beacon interval in usec (time before exec.)
3325                  * 20-23: 0
3326                  * 24-31: number of beacons (suspend between channels)
3327                  */
3328
3329                 extra = (suspend_time / interval) << 24;
3330                 scan_suspend_time = 0xFF0FFFFF &
3331                     (extra | ((suspend_time % interval) * 1024));
3332
3333                 scan->suspend_time = cpu_to_le32(scan_suspend_time);
3334                 IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
3335                                scan_suspend_time, interval);
3336         }
3337
3338         /* We should add the ability for user to lock to PASSIVE ONLY */
3339         if (priv->one_direct_scan) {
3340                 IWL_DEBUG_SCAN(priv, "Kicking off one direct scan for '%s'\n",
3341                                 print_ssid(ssid, priv->direct_ssid,
3342                                 priv->direct_ssid_len));
3343                 scan->direct_scan[0].id = WLAN_EID_SSID;
3344                 scan->direct_scan[0].len = priv->direct_ssid_len;
3345                 memcpy(scan->direct_scan[0].ssid,
3346                        priv->direct_ssid, priv->direct_ssid_len);
3347                 n_probes++;
3348         } else
3349                 IWL_DEBUG_SCAN(priv, "Kicking off one indirect scan.\n");
3350
3351         /* We don't build a direct scan probe request; the uCode will do
3352          * that based on the direct_mask added to each channel entry */
3353         scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
3354         scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
3355         scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
3356
3357         /* flags + rate selection */
3358
3359         if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
3360                 scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
3361                 scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
3362                 scan->good_CRC_th = 0;
3363                 band = IEEE80211_BAND_2GHZ;
3364         } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
3365                 scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
3366                 scan->good_CRC_th = IWL_GOOD_CRC_TH;
3367                 band = IEEE80211_BAND_5GHZ;
3368         } else {
3369                 IWL_WARN(priv, "Invalid scan band count\n");
3370                 goto done;
3371         }
3372
3373         scan->tx_cmd.len = cpu_to_le16(
3374                 iwl_fill_probe_req(priv, band,
3375                                    (struct ieee80211_mgmt *)scan->data,
3376                                    IWL_MAX_SCAN_SIZE - sizeof(*scan)));
3377
3378         /* select Rx antennas */
3379         scan->flags |= iwl3945_get_antenna_flags(priv);
3380
3381         if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
3382                 scan->filter_flags = RXON_FILTER_PROMISC_MSK;
3383
3384         scan->channel_count =
3385                 iwl3945_get_channels_for_scan(priv, band, 1, /* active */
3386                                               n_probes,
3387                         (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
3388
3389         if (scan->channel_count == 0) {
3390                 IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
3391                 goto done;
3392         }
3393
3394         cmd.len += le16_to_cpu(scan->tx_cmd.len) +
3395             scan->channel_count * sizeof(struct iwl3945_scan_channel);
3396         cmd.data = scan;
3397         scan->len = cpu_to_le16(cmd.len);
3398
3399         set_bit(STATUS_SCAN_HW, &priv->status);
3400         rc = iwl_send_cmd_sync(priv, &cmd);
3401         if (rc)
3402                 goto done;
3403
3404         queue_delayed_work(priv->workqueue, &priv->scan_check,
3405                            IWL_SCAN_CHECK_WATCHDOG);
3406
3407         mutex_unlock(&priv->mutex);
3408         return;
3409
3410  done:
3411         /* can not perform scan make sure we clear scanning
3412          * bits from status so next scan request can be performed.
3413          * if we dont clear scanning status bit here all next scan
3414          * will fail
3415         */
3416         clear_bit(STATUS_SCAN_HW, &priv->status);
3417         clear_bit(STATUS_SCANNING, &priv->status);
3418
3419         /* inform mac80211 scan aborted */
3420         queue_work(priv->workqueue, &priv->scan_completed);
3421         mutex_unlock(&priv->mutex);
3422 }
3423
3424 static void iwl3945_bg_up(struct work_struct *data)
3425 {
3426         struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
3427
3428         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3429                 return;
3430
3431         mutex_lock(&priv->mutex);
3432         __iwl3945_up(priv);
3433         mutex_unlock(&priv->mutex);
3434         iwl_rfkill_set_hw_state(priv);
3435 }
3436
3437 static void iwl3945_bg_restart(struct work_struct *data)
3438 {
3439         struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
3440
3441         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3442                 return;
3443
3444         iwl3945_down(priv);
3445         queue_work(priv->workqueue, &priv->up);
3446 }
3447
3448 static void iwl3945_bg_rx_replenish(struct work_struct *data)
3449 {
3450         struct iwl_priv *priv =
3451             container_of(data, struct iwl_priv, rx_replenish);
3452
3453         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3454                 return;
3455
3456         mutex_lock(&priv->mutex);
3457         iwl3945_rx_replenish(priv);
3458         mutex_unlock(&priv->mutex);
3459 }
3460
3461 #define IWL_DELAY_NEXT_SCAN (HZ*2)
3462
3463 static void iwl3945_post_associate(struct iwl_priv *priv)
3464 {
3465         int rc = 0;
3466         struct ieee80211_conf *conf = NULL;
3467
3468         if (priv->iw_mode == NL80211_IFTYPE_AP) {
3469                 IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
3470                 return;
3471         }
3472
3473
3474         IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
3475                         priv->assoc_id, priv->active_rxon.bssid_addr);
3476
3477         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3478                 return;
3479
3480         if (!priv->vif || !priv->is_open)
3481                 return;
3482
3483         iwl_scan_cancel_timeout(priv, 200);
3484
3485         conf = ieee80211_get_hw_conf(priv->hw);
3486
3487         priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3488         iwl3945_commit_rxon(priv);
3489
3490         memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
3491         iwl3945_setup_rxon_timing(priv);
3492         rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3493                               sizeof(priv->rxon_timing), &priv->rxon_timing);
3494         if (rc)
3495                 IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
3496                             "Attempting to continue.\n");
3497
3498         priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
3499
3500         priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3501
3502         IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
3503                         priv->assoc_id, priv->beacon_int);
3504
3505         if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3506                 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
3507         else
3508                 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
3509
3510         if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3511                 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
3512                         priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
3513                 else
3514                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3515
3516                 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
3517                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
3518
3519         }
3520
3521         iwl3945_commit_rxon(priv);
3522
3523         switch (priv->iw_mode) {
3524         case NL80211_IFTYPE_STATION:
3525                 iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
3526                 break;
3527
3528         case NL80211_IFTYPE_ADHOC:
3529
3530                 priv->assoc_id = 1;
3531                 iwl3945_add_station(priv, priv->bssid, 0, 0);
3532                 iwl3945_sync_sta(priv, IWL_STA_ID,
3533                                  (priv->band == IEEE80211_BAND_5GHZ) ?
3534                                  IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
3535                                  CMD_ASYNC);
3536                 iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
3537                 iwl3945_send_beacon_cmd(priv);
3538
3539                 break;
3540
3541         default:
3542                  IWL_ERR(priv, "%s Should not be called in %d mode\n",
3543                            __func__, priv->iw_mode);
3544                 break;
3545         }
3546
3547         iwl_activate_qos(priv, 0);
3548
3549         /* we have just associated, don't start scan too early */
3550         priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
3551 }
3552
3553 static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
3554
3555 /*****************************************************************************
3556  *
3557  * mac80211 entry point functions
3558  *
3559  *****************************************************************************/
3560
3561 #define UCODE_READY_TIMEOUT     (2 * HZ)
3562
3563 static int iwl3945_mac_start(struct ieee80211_hw *hw)
3564 {
3565         struct iwl_priv *priv = hw->priv;
3566         int ret;
3567
3568         IWL_DEBUG_MAC80211(priv, "enter\n");
3569
3570         /* we should be verifying the device is ready to be opened */
3571         mutex_lock(&priv->mutex);
3572
3573         memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
3574         /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
3575          * ucode filename and max sizes are card-specific. */
3576
3577         if (!priv->ucode_code.len) {
3578                 ret = iwl3945_read_ucode(priv);
3579                 if (ret) {
3580                         IWL_ERR(priv, "Could not read microcode: %d\n", ret);
3581                         mutex_unlock(&priv->mutex);
3582                         goto out_release_irq;
3583                 }
3584         }
3585
3586         ret = __iwl3945_up(priv);
3587
3588         mutex_unlock(&priv->mutex);
3589
3590         iwl_rfkill_set_hw_state(priv);
3591
3592         if (ret)
3593                 goto out_release_irq;
3594
3595         IWL_DEBUG_INFO(priv, "Start UP work.\n");
3596
3597         if (test_bit(STATUS_IN_SUSPEND, &priv->status))
3598                 return 0;
3599
3600         /* Wait for START_ALIVE from ucode. Otherwise callbacks from
3601          * mac80211 will not be run successfully. */
3602         ret = wait_event_interruptible_timeout(priv->wait_command_queue,
3603                         test_bit(STATUS_READY, &priv->status),
3604                         UCODE_READY_TIMEOUT);
3605         if (!ret) {
3606                 if (!test_bit(STATUS_READY, &priv->status)) {
3607                         IWL_ERR(priv,
3608                                 "Wait for START_ALIVE timeout after %dms.\n",
3609                                 jiffies_to_msecs(UCODE_READY_TIMEOUT));
3610                         ret = -ETIMEDOUT;
3611                         goto out_release_irq;
3612                 }
3613         }
3614
3615         /* ucode is running and will send rfkill notifications,
3616          * no need to poll the killswitch state anymore */
3617         cancel_delayed_work(&priv->rfkill_poll);
3618
3619         priv->is_open = 1;
3620         IWL_DEBUG_MAC80211(priv, "leave\n");
3621         return 0;
3622
3623 out_release_irq:
3624         priv->is_open = 0;
3625         IWL_DEBUG_MAC80211(priv, "leave - failed\n");
3626         return ret;
3627 }
3628
3629 static void iwl3945_mac_stop(struct ieee80211_hw *hw)
3630 {
3631         struct iwl_priv *priv = hw->priv;
3632
3633         IWL_DEBUG_MAC80211(priv, "enter\n");
3634
3635         if (!priv->is_open) {
3636                 IWL_DEBUG_MAC80211(priv, "leave - skip\n");
3637                 return;
3638         }
3639
3640         priv->is_open = 0;
3641
3642         if (iwl_is_ready_rf(priv)) {
3643                 /* stop mac, cancel any scan request and clear
3644                  * RXON_FILTER_ASSOC_MSK BIT
3645                  */
3646                 mutex_lock(&priv->mutex);
3647                 iwl_scan_cancel_timeout(priv, 100);
3648                 mutex_unlock(&priv->mutex);
3649         }
3650
3651         iwl3945_down(priv);
3652
3653         flush_workqueue(priv->workqueue);
3654
3655         /* start polling the killswitch state again */
3656         queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
3657                            round_jiffies_relative(2 * HZ));
3658
3659         IWL_DEBUG_MAC80211(priv, "leave\n");
3660 }
3661
3662 static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
3663 {
3664         struct iwl_priv *priv = hw->priv;
3665
3666         IWL_DEBUG_MAC80211(priv, "enter\n");
3667
3668         IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
3669                      ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
3670
3671         if (iwl3945_tx_skb(priv, skb))
3672                 dev_kfree_skb_any(skb);
3673
3674         IWL_DEBUG_MAC80211(priv, "leave\n");
3675         return NETDEV_TX_OK;
3676 }
3677
3678 static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
3679                                  struct ieee80211_if_init_conf *conf)
3680 {
3681         struct iwl_priv *priv = hw->priv;
3682         unsigned long flags;
3683
3684         IWL_DEBUG_MAC80211(priv, "enter: type %d\n", conf->type);
3685
3686         if (priv->vif) {
3687                 IWL_DEBUG_MAC80211(priv, "leave - vif != NULL\n");
3688                 return -EOPNOTSUPP;
3689         }
3690
3691         spin_lock_irqsave(&priv->lock, flags);
3692         priv->vif = conf->vif;
3693         priv->iw_mode = conf->type;
3694
3695         spin_unlock_irqrestore(&priv->lock, flags);
3696
3697         mutex_lock(&priv->mutex);
3698
3699         if (conf->mac_addr) {
3700                 IWL_DEBUG_MAC80211(priv, "Set: %pM\n", conf->mac_addr);
3701                 memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
3702         }
3703
3704         if (iwl_is_ready(priv))
3705                 iwl3945_set_mode(priv, conf->type);
3706
3707         mutex_unlock(&priv->mutex);
3708
3709         IWL_DEBUG_MAC80211(priv, "leave\n");
3710         return 0;
3711 }
3712
3713 /**
3714  * iwl3945_mac_config - mac80211 config callback
3715  *
3716  * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
3717  * be set inappropriately and the driver currently sets the hardware up to
3718  * use it whenever needed.
3719  */
3720 static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
3721 {
3722         struct iwl_priv *priv = hw->priv;
3723         const struct iwl_channel_info *ch_info;
3724         struct ieee80211_conf *conf = &hw->conf;
3725         unsigned long flags;
3726         int ret = 0;
3727
3728         mutex_lock(&priv->mutex);
3729         IWL_DEBUG_MAC80211(priv, "enter to channel %d\n",
3730                                 conf->channel->hw_value);
3731
3732         if (!iwl_is_ready(priv)) {
3733                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
3734                 ret = -EIO;
3735                 goto out;
3736         }
3737
3738         if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
3739                      test_bit(STATUS_SCANNING, &priv->status))) {
3740                 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
3741                 set_bit(STATUS_CONF_PENDING, &priv->status);
3742                 mutex_unlock(&priv->mutex);
3743                 return 0;
3744         }
3745
3746         spin_lock_irqsave(&priv->lock, flags);
3747
3748         ch_info = iwl_get_channel_info(priv, conf->channel->band,
3749                                        conf->channel->hw_value);
3750         if (!is_channel_valid(ch_info)) {
3751                 IWL_DEBUG_SCAN(priv,
3752                                 "Channel %d [%d] is INVALID for this band.\n",
3753                                 conf->channel->hw_value, conf->channel->band);
3754                 IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
3755                 spin_unlock_irqrestore(&priv->lock, flags);
3756                 ret = -EINVAL;
3757                 goto out;
3758         }
3759
3760         iwl_set_rxon_channel(priv, conf->channel);
3761
3762         iwl_set_flags_for_band(priv, conf->channel->band);
3763
3764         /* The list of supported rates and rate mask can be different
3765          * for each phymode; since the phymode may have changed, reset
3766          * the rate mask to what mac80211 lists */
3767         iwl_set_rate(priv);
3768
3769         spin_unlock_irqrestore(&priv->lock, flags);
3770
3771 #ifdef IEEE80211_CONF_CHANNEL_SWITCH
3772         if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
3773                 iwl3945_hw_channel_switch(priv, conf->channel);
3774                 goto out;
3775         }
3776 #endif
3777
3778         if (changed & IEEE80211_CONF_CHANGE_RADIO_ENABLED) {
3779                 if (conf->radio_enabled &&
3780                     iwl_radio_kill_sw_enable_radio(priv)) {
3781                         IWL_DEBUG_MAC80211(priv, "leave - RF-KILL - "
3782                                                  "waiting for uCode\n");
3783                         goto out;
3784                 }
3785
3786                 if (!conf->radio_enabled) {
3787                         iwl_radio_kill_sw_disable_radio(priv);
3788                         IWL_DEBUG_MAC80211(priv, "leave - radio disabled\n");
3789                         goto out;
3790                 }
3791         }
3792
3793         if (iwl_is_rfkill(priv)) {
3794                 IWL_DEBUG_MAC80211(priv, "leave - RF kill\n");
3795                 ret = -EIO;
3796                 goto out;
3797         }
3798
3799         iwl_set_rate(priv);
3800
3801         if (memcmp(&priv->active_rxon,
3802                    &priv->staging_rxon, sizeof(priv->staging_rxon)))
3803                 iwl3945_commit_rxon(priv);
3804         else
3805                 IWL_DEBUG_INFO(priv, "Not re-sending same RXON configuration\n");
3806
3807         IWL_DEBUG_MAC80211(priv, "leave\n");
3808
3809 out:
3810         clear_bit(STATUS_CONF_PENDING, &priv->status);
3811         mutex_unlock(&priv->mutex);
3812         return ret;
3813 }
3814
3815 static void iwl3945_config_ap(struct iwl_priv *priv)
3816 {
3817         int rc = 0;
3818
3819         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3820                 return;
3821
3822         /* The following should be done only at AP bring up */
3823         if (!(iwl_is_associated(priv))) {
3824
3825                 /* RXON - unassoc (to set timing command) */
3826                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3827                 iwl3945_commit_rxon(priv);
3828
3829                 /* RXON Timing */
3830                 memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
3831                 iwl3945_setup_rxon_timing(priv);
3832                 rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
3833                                       sizeof(priv->rxon_timing),
3834                                       &priv->rxon_timing);
3835                 if (rc)
3836                         IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
3837                                         "Attempting to continue.\n");
3838
3839                 /* FIXME: what should be the assoc_id for AP? */
3840                 priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
3841                 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
3842                         priv->staging_rxon.flags |=
3843                                 RXON_FLG_SHORT_PREAMBLE_MSK;
3844                 else
3845                         priv->staging_rxon.flags &=
3846                                 ~RXON_FLG_SHORT_PREAMBLE_MSK;
3847
3848                 if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
3849                         if (priv->assoc_capability &
3850                                 WLAN_CAPABILITY_SHORT_SLOT_TIME)
3851                                 priv->staging_rxon.flags |=
3852                                         RXON_FLG_SHORT_SLOT_MSK;
3853                         else
3854                                 priv->staging_rxon.flags &=
3855                                         ~RXON_FLG_SHORT_SLOT_MSK;
3856
3857                         if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
3858                                 priv->staging_rxon.flags &=
3859                                         ~RXON_FLG_SHORT_SLOT_MSK;
3860                 }
3861                 /* restore RXON assoc */
3862                 priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
3863                 iwl3945_commit_rxon(priv);
3864                 iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
3865         }
3866         iwl3945_send_beacon_cmd(priv);
3867
3868         /* FIXME - we need to add code here to detect a totally new
3869          * configuration, reset the AP, unassoc, rxon timing, assoc,
3870          * clear sta table, add BCAST sta... */
3871 }
3872
3873 static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
3874                                         struct ieee80211_vif *vif,
3875                                         struct ieee80211_if_conf *conf)
3876 {
3877         struct iwl_priv *priv = hw->priv;
3878         int rc;
3879
3880         if (conf == NULL)
3881                 return -EIO;
3882
3883         if (priv->vif != vif) {
3884                 IWL_DEBUG_MAC80211(priv, "leave - priv->vif != vif\n");
3885                 return 0;
3886         }
3887
3888         /* handle this temporarily here */
3889         if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
3890             conf->changed & IEEE80211_IFCC_BEACON) {
3891                 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
3892                 if (!beacon)
3893                         return -ENOMEM;
3894                 mutex_lock(&priv->mutex);
3895                 rc = iwl3945_mac_beacon_update(hw, beacon);
3896                 mutex_unlock(&priv->mutex);
3897                 if (rc)
3898                         return rc;
3899         }
3900
3901         if (!iwl_is_alive(priv))
3902                 return -EAGAIN;
3903
3904         mutex_lock(&priv->mutex);
3905
3906         if (conf->bssid)
3907                 IWL_DEBUG_MAC80211(priv, "bssid: %pM\n", conf->bssid);
3908
3909 /*
3910  * very dubious code was here; the probe filtering flag is never set:
3911  *
3912         if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
3913             !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
3914  */
3915
3916         if (priv->iw_mode == NL80211_IFTYPE_AP) {
3917                 if (!conf->bssid) {
3918                         conf->bssid = priv->mac_addr;
3919                         memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
3920                         IWL_DEBUG_MAC80211(priv, "bssid was set to: %pM\n",
3921                                            conf->bssid);
3922                 }
3923                 if (priv->ibss_beacon)
3924                         dev_kfree_skb(priv->ibss_beacon);
3925
3926                 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
3927         }
3928
3929         if (iwl_is_rfkill(priv))
3930                 goto done;
3931
3932         if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
3933             !is_multicast_ether_addr(conf->bssid)) {
3934                 /* If there is currently a HW scan going on in the background
3935                  * then we need to cancel it else the RXON below will fail. */
3936                 if (iwl_scan_cancel_timeout(priv, 100)) {
3937                         IWL_WARN(priv, "Aborted scan still in progress "
3938                                     "after 100ms\n");
3939                         IWL_DEBUG_MAC80211(priv, "leaving:scan abort failed\n");
3940                         mutex_unlock(&priv->mutex);
3941                         return -EAGAIN;
3942                 }
3943                 memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
3944
3945                 /* TODO: Audit driver for usage of these members and see
3946                  * if mac80211 deprecates them (priv->bssid looks like it
3947                  * shouldn't be there, but I haven't scanned the IBSS code
3948                  * to verify) - jpk */
3949                 memcpy(priv->bssid, conf->bssid, ETH_ALEN);
3950
3951                 if (priv->iw_mode == NL80211_IFTYPE_AP)
3952                         iwl3945_config_ap(priv);
3953                 else {
3954                         rc = iwl3945_commit_rxon(priv);
3955                         if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
3956                                 iwl3945_add_station(priv,
3957                                         priv->active_rxon.bssid_addr, 1, 0);
3958                 }
3959
3960         } else {
3961                 iwl_scan_cancel_timeout(priv, 100);
3962                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3963                 iwl3945_commit_rxon(priv);
3964         }
3965
3966  done:
3967         IWL_DEBUG_MAC80211(priv, "leave\n");
3968         mutex_unlock(&priv->mutex);
3969
3970         return 0;
3971 }
3972
3973 static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
3974                                      struct ieee80211_if_init_conf *conf)
3975 {
3976         struct iwl_priv *priv = hw->priv;
3977
3978         IWL_DEBUG_MAC80211(priv, "enter\n");
3979
3980         mutex_lock(&priv->mutex);
3981
3982         if (iwl_is_ready_rf(priv)) {
3983                 iwl_scan_cancel_timeout(priv, 100);
3984                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
3985                 iwl3945_commit_rxon(priv);
3986         }
3987         if (priv->vif == conf->vif) {
3988                 priv->vif = NULL;
3989                 memset(priv->bssid, 0, ETH_ALEN);
3990         }
3991         mutex_unlock(&priv->mutex);
3992
3993         IWL_DEBUG_MAC80211(priv, "leave\n");
3994 }
3995
3996 #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
3997
3998 static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
3999                                      struct ieee80211_vif *vif,
4000                                      struct ieee80211_bss_conf *bss_conf,
4001                                      u32 changes)
4002 {
4003         struct iwl_priv *priv = hw->priv;
4004
4005         IWL_DEBUG_MAC80211(priv, "changes = 0x%X\n", changes);
4006
4007         if (changes & BSS_CHANGED_ERP_PREAMBLE) {
4008                 IWL_DEBUG_MAC80211(priv, "ERP_PREAMBLE %d\n",
4009                                    bss_conf->use_short_preamble);
4010                 if (bss_conf->use_short_preamble)
4011                         priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
4012                 else
4013                         priv->staging_rxon.flags &=
4014                                 ~RXON_FLG_SHORT_PREAMBLE_MSK;
4015         }
4016
4017         if (changes & BSS_CHANGED_ERP_CTS_PROT) {
4018                 IWL_DEBUG_MAC80211(priv, "ERP_CTS %d\n",
4019                                    bss_conf->use_cts_prot);
4020                 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
4021                         priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
4022                 else
4023                         priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
4024         }
4025
4026         if (changes & BSS_CHANGED_ASSOC) {
4027                 IWL_DEBUG_MAC80211(priv, "ASSOC %d\n", bss_conf->assoc);
4028                 /* This should never happen as this function should
4029                  * never be called from interrupt context. */
4030                 if (WARN_ON_ONCE(in_interrupt()))
4031                         return;
4032                 if (bss_conf->assoc) {
4033                         priv->assoc_id = bss_conf->aid;
4034                         priv->beacon_int = bss_conf->beacon_int;
4035                         priv->timestamp = bss_conf->timestamp;
4036                         priv->assoc_capability = bss_conf->assoc_capability;
4037                         priv->power_data.dtim_period = bss_conf->dtim_period;
4038                         priv->next_scan_jiffies = jiffies +
4039                                         IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
4040                         mutex_lock(&priv->mutex);
4041                         iwl3945_post_associate(priv);
4042                         mutex_unlock(&priv->mutex);
4043                 } else {
4044                         priv->assoc_id = 0;
4045                         IWL_DEBUG_MAC80211(priv,
4046                                         "DISASSOC %d\n", bss_conf->assoc);
4047                 }
4048         } else if (changes && iwl_is_associated(priv) && priv->assoc_id) {
4049                         IWL_DEBUG_MAC80211(priv,
4050                                         "Associated Changes %d\n", changes);
4051                         iwl3945_send_rxon_assoc(priv);
4052         }
4053
4054 }
4055
4056 static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
4057                                struct ieee80211_vif *vif,
4058                                struct ieee80211_sta *sta,
4059                                struct ieee80211_key_conf *key)
4060 {
4061         struct iwl_priv *priv = hw->priv;
4062         const u8 *addr;
4063         int ret = 0;
4064         u8 sta_id = IWL_INVALID_STATION;
4065         u8 static_key;
4066
4067         IWL_DEBUG_MAC80211(priv, "enter\n");
4068
4069         if (iwl3945_mod_params.sw_crypto) {
4070                 IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
4071                 return -EOPNOTSUPP;
4072         }
4073
4074         addr = sta ? sta->addr : iwl_bcast_addr;
4075         static_key = !iwl_is_associated(priv);
4076
4077         if (!static_key) {
4078                 sta_id = iwl3945_hw_find_station(priv, addr);
4079                 if (sta_id == IWL_INVALID_STATION) {
4080                         IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
4081                                             addr);
4082                         return -EINVAL;
4083                 }
4084         }
4085
4086         mutex_lock(&priv->mutex);
4087         iwl_scan_cancel_timeout(priv, 100);
4088         mutex_unlock(&priv->mutex);
4089
4090         switch (cmd) {
4091         case SET_KEY:
4092                 if (static_key)
4093                         ret = iwl3945_set_static_key(priv, key);
4094                 else
4095                         ret = iwl3945_set_dynamic_key(priv, key, sta_id);
4096                 IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
4097                 break;
4098         case DISABLE_KEY:
4099                 if (static_key)
4100                         ret = iwl3945_remove_static_key(priv);
4101                 else
4102                         ret = iwl3945_clear_sta_key_info(priv, sta_id);
4103                 IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
4104                 break;
4105         default:
4106                 ret = -EINVAL;
4107         }
4108
4109         IWL_DEBUG_MAC80211(priv, "leave\n");
4110
4111         return ret;
4112 }
4113
4114 static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
4115                            const struct ieee80211_tx_queue_params *params)
4116 {
4117         struct iwl_priv *priv = hw->priv;
4118         unsigned long flags;
4119         int q;
4120
4121         IWL_DEBUG_MAC80211(priv, "enter\n");
4122
4123         if (!iwl_is_ready_rf(priv)) {
4124                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
4125                 return -EIO;
4126         }
4127
4128         if (queue >= AC_NUM) {
4129                 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
4130                 return 0;
4131         }
4132
4133         q = AC_NUM - 1 - queue;
4134
4135         spin_lock_irqsave(&priv->lock, flags);
4136
4137         priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
4138         priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
4139         priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
4140         priv->qos_data.def_qos_parm.ac[q].edca_txop =
4141                         cpu_to_le16((params->txop * 32));
4142
4143         priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
4144         priv->qos_data.qos_active = 1;
4145
4146         spin_unlock_irqrestore(&priv->lock, flags);
4147
4148         mutex_lock(&priv->mutex);
4149         if (priv->iw_mode == NL80211_IFTYPE_AP)
4150                 iwl_activate_qos(priv, 1);
4151         else if (priv->assoc_id && iwl_is_associated(priv))
4152                 iwl_activate_qos(priv, 0);
4153
4154         mutex_unlock(&priv->mutex);
4155
4156         IWL_DEBUG_MAC80211(priv, "leave\n");
4157         return 0;
4158 }
4159
4160 static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
4161                                 struct ieee80211_tx_queue_stats *stats)
4162 {
4163         struct iwl_priv *priv = hw->priv;
4164         int i, avail;
4165         struct iwl_tx_queue *txq;
4166         struct iwl_queue *q;
4167         unsigned long flags;
4168
4169         IWL_DEBUG_MAC80211(priv, "enter\n");
4170
4171         if (!iwl_is_ready_rf(priv)) {
4172                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
4173                 return -EIO;
4174         }
4175
4176         spin_lock_irqsave(&priv->lock, flags);
4177
4178         for (i = 0; i < AC_NUM; i++) {
4179                 txq = &priv->txq[i];
4180                 q = &txq->q;
4181                 avail = iwl_queue_space(q);
4182
4183                 stats[i].len = q->n_window - avail;
4184                 stats[i].limit = q->n_window - q->high_mark;
4185                 stats[i].count = q->n_window;
4186
4187         }
4188         spin_unlock_irqrestore(&priv->lock, flags);
4189
4190         IWL_DEBUG_MAC80211(priv, "leave\n");
4191
4192         return 0;
4193 }
4194
4195 static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
4196 {
4197         struct iwl_priv *priv = hw->priv;
4198         unsigned long flags;
4199
4200         mutex_lock(&priv->mutex);
4201         IWL_DEBUG_MAC80211(priv, "enter\n");
4202
4203         iwl_reset_qos(priv);
4204
4205         spin_lock_irqsave(&priv->lock, flags);
4206         priv->assoc_id = 0;
4207         priv->assoc_capability = 0;
4208
4209         /* new association get rid of ibss beacon skb */
4210         if (priv->ibss_beacon)
4211                 dev_kfree_skb(priv->ibss_beacon);
4212
4213         priv->ibss_beacon = NULL;
4214
4215         priv->beacon_int = priv->hw->conf.beacon_int;
4216         priv->timestamp = 0;
4217         if ((priv->iw_mode == NL80211_IFTYPE_STATION))
4218                 priv->beacon_int = 0;
4219
4220         spin_unlock_irqrestore(&priv->lock, flags);
4221
4222         if (!iwl_is_ready_rf(priv)) {
4223                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
4224                 mutex_unlock(&priv->mutex);
4225                 return;
4226         }
4227
4228         /* we are restarting association process
4229          * clear RXON_FILTER_ASSOC_MSK bit
4230         */
4231         if (priv->iw_mode != NL80211_IFTYPE_AP) {
4232                 iwl_scan_cancel_timeout(priv, 100);
4233                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
4234                 iwl3945_commit_rxon(priv);
4235         }
4236
4237         /* Per mac80211.h: This is only used in IBSS mode... */
4238         if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
4239
4240                 IWL_DEBUG_MAC80211(priv, "leave - not in IBSS\n");
4241                 mutex_unlock(&priv->mutex);
4242                 return;
4243         }
4244
4245         iwl_set_rate(priv);
4246
4247         mutex_unlock(&priv->mutex);
4248
4249         IWL_DEBUG_MAC80211(priv, "leave\n");
4250
4251 }
4252
4253 static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
4254 {
4255         struct iwl_priv *priv = hw->priv;
4256         unsigned long flags;
4257         __le64 timestamp;
4258
4259         IWL_DEBUG_MAC80211(priv, "enter\n");
4260
4261         if (!iwl_is_ready_rf(priv)) {
4262                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
4263                 return -EIO;
4264         }
4265
4266         if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
4267                 IWL_DEBUG_MAC80211(priv, "leave - not IBSS\n");
4268                 return -EIO;
4269         }
4270
4271         spin_lock_irqsave(&priv->lock, flags);
4272
4273         if (priv->ibss_beacon)
4274                 dev_kfree_skb(priv->ibss_beacon);
4275
4276         priv->ibss_beacon = skb;
4277
4278         priv->assoc_id = 0;
4279         timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
4280         priv->timestamp = le64_to_cpu(timestamp);
4281
4282         IWL_DEBUG_MAC80211(priv, "leave\n");
4283         spin_unlock_irqrestore(&priv->lock, flags);
4284
4285         iwl_reset_qos(priv);
4286
4287         iwl3945_post_associate(priv);
4288
4289
4290         return 0;
4291 }
4292
4293 /*****************************************************************************
4294  *
4295  * sysfs attributes
4296  *
4297  *****************************************************************************/
4298
4299 #ifdef CONFIG_IWLWIFI_DEBUG
4300
4301 /*
4302  * The following adds a new attribute to the sysfs representation
4303  * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
4304  * used for controlling the debug level.
4305  *
4306  * See the level definitions in iwl for details.
4307  */
4308 static ssize_t show_debug_level(struct device *d,
4309                                 struct device_attribute *attr, char *buf)
4310 {
4311         struct iwl_priv *priv = d->driver_data;
4312
4313         return sprintf(buf, "0x%08X\n", priv->debug_level);
4314 }
4315 static ssize_t store_debug_level(struct device *d,
4316                                 struct device_attribute *attr,
4317                                  const char *buf, size_t count)
4318 {
4319         struct iwl_priv *priv = d->driver_data;
4320         unsigned long val;
4321         int ret;
4322
4323         ret = strict_strtoul(buf, 0, &val);
4324         if (ret)
4325                 IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
4326         else
4327                 priv->debug_level = val;
4328
4329         return strnlen(buf, count);
4330 }
4331
4332 static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
4333                         show_debug_level, store_debug_level);
4334
4335 #endif /* CONFIG_IWLWIFI_DEBUG */
4336
4337 static ssize_t show_temperature(struct device *d,
4338                                 struct device_attribute *attr, char *buf)
4339 {
4340         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4341
4342         if (!iwl_is_alive(priv))
4343                 return -EAGAIN;
4344
4345         return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
4346 }
4347
4348 static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
4349
4350 static ssize_t show_tx_power(struct device *d,
4351                              struct device_attribute *attr, char *buf)
4352 {
4353         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4354         return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
4355 }
4356
4357 static ssize_t store_tx_power(struct device *d,
4358                               struct device_attribute *attr,
4359                               const char *buf, size_t count)
4360 {
4361         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4362         char *p = (char *)buf;
4363         u32 val;
4364
4365         val = simple_strtoul(p, &p, 10);
4366         if (p == buf)
4367                 IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
4368         else
4369                 iwl3945_hw_reg_set_txpower(priv, val);
4370
4371         return count;
4372 }
4373
4374 static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
4375
4376 static ssize_t show_flags(struct device *d,
4377                           struct device_attribute *attr, char *buf)
4378 {
4379         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4380
4381         return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
4382 }
4383
4384 static ssize_t store_flags(struct device *d,
4385                            struct device_attribute *attr,
4386                            const char *buf, size_t count)
4387 {
4388         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4389         u32 flags = simple_strtoul(buf, NULL, 0);
4390
4391         mutex_lock(&priv->mutex);
4392         if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
4393                 /* Cancel any currently running scans... */
4394                 if (iwl_scan_cancel_timeout(priv, 100))
4395                         IWL_WARN(priv, "Could not cancel scan.\n");
4396                 else {
4397                         IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
4398                                        flags);
4399                         priv->staging_rxon.flags = cpu_to_le32(flags);
4400                         iwl3945_commit_rxon(priv);
4401                 }
4402         }
4403         mutex_unlock(&priv->mutex);
4404
4405         return count;
4406 }
4407
4408 static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
4409
4410 static ssize_t show_filter_flags(struct device *d,
4411                                  struct device_attribute *attr, char *buf)
4412 {
4413         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4414
4415         return sprintf(buf, "0x%04X\n",
4416                 le32_to_cpu(priv->active_rxon.filter_flags));
4417 }
4418
4419 static ssize_t store_filter_flags(struct device *d,
4420                                   struct device_attribute *attr,
4421                                   const char *buf, size_t count)
4422 {
4423         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4424         u32 filter_flags = simple_strtoul(buf, NULL, 0);
4425
4426         mutex_lock(&priv->mutex);
4427         if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
4428                 /* Cancel any currently running scans... */
4429                 if (iwl_scan_cancel_timeout(priv, 100))
4430                         IWL_WARN(priv, "Could not cancel scan.\n");
4431                 else {
4432                         IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
4433                                        "0x%04X\n", filter_flags);
4434                         priv->staging_rxon.filter_flags =
4435                                 cpu_to_le32(filter_flags);
4436                         iwl3945_commit_rxon(priv);
4437                 }
4438         }
4439         mutex_unlock(&priv->mutex);
4440
4441         return count;
4442 }
4443
4444 static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
4445                    store_filter_flags);
4446
4447 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
4448
4449 static ssize_t show_measurement(struct device *d,
4450                                 struct device_attribute *attr, char *buf)
4451 {
4452         struct iwl_priv *priv = dev_get_drvdata(d);
4453         struct iwl_spectrum_notification measure_report;
4454         u32 size = sizeof(measure_report), len = 0, ofs = 0;
4455         u8 *data = (u8 *)&measure_report;
4456         unsigned long flags;
4457
4458         spin_lock_irqsave(&priv->lock, flags);
4459         if (!(priv->measurement_status & MEASUREMENT_READY)) {
4460                 spin_unlock_irqrestore(&priv->lock, flags);
4461                 return 0;
4462         }
4463         memcpy(&measure_report, &priv->measure_report, size);
4464         priv->measurement_status = 0;
4465         spin_unlock_irqrestore(&priv->lock, flags);
4466
4467         while (size && (PAGE_SIZE - len)) {
4468                 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4469                                    PAGE_SIZE - len, 1);
4470                 len = strlen(buf);
4471                 if (PAGE_SIZE - len)
4472                         buf[len++] = '\n';
4473
4474                 ofs += 16;
4475                 size -= min(size, 16U);
4476         }
4477
4478         return len;
4479 }
4480
4481 static ssize_t store_measurement(struct device *d,
4482                                  struct device_attribute *attr,
4483                                  const char *buf, size_t count)
4484 {
4485         struct iwl_priv *priv = dev_get_drvdata(d);
4486         struct ieee80211_measurement_params params = {
4487                 .channel = le16_to_cpu(priv->active_rxon.channel),
4488                 .start_time = cpu_to_le64(priv->last_tsf),
4489                 .duration = cpu_to_le16(1),
4490         };
4491         u8 type = IWL_MEASURE_BASIC;
4492         u8 buffer[32];
4493         u8 channel;
4494
4495         if (count) {
4496                 char *p = buffer;
4497                 strncpy(buffer, buf, min(sizeof(buffer), count));
4498                 channel = simple_strtoul(p, NULL, 0);
4499                 if (channel)
4500                         params.channel = channel;
4501
4502                 p = buffer;
4503                 while (*p && *p != ' ')
4504                         p++;
4505                 if (*p)
4506                         type = simple_strtoul(p + 1, NULL, 0);
4507         }
4508
4509         IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
4510                        "channel %d (for '%s')\n", type, params.channel, buf);
4511         iwl3945_get_measurement(priv, &params, type);
4512
4513         return count;
4514 }
4515
4516 static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
4517                    show_measurement, store_measurement);
4518 #endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
4519
4520 static ssize_t store_retry_rate(struct device *d,
4521                                 struct device_attribute *attr,
4522                                 const char *buf, size_t count)
4523 {
4524         struct iwl_priv *priv = dev_get_drvdata(d);
4525
4526         priv->retry_rate = simple_strtoul(buf, NULL, 0);
4527         if (priv->retry_rate <= 0)
4528                 priv->retry_rate = 1;
4529
4530         return count;
4531 }
4532
4533 static ssize_t show_retry_rate(struct device *d,
4534                                struct device_attribute *attr, char *buf)
4535 {
4536         struct iwl_priv *priv = dev_get_drvdata(d);
4537         return sprintf(buf, "%d", priv->retry_rate);
4538 }
4539
4540 static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
4541                    store_retry_rate);
4542
4543
4544 static ssize_t store_power_level(struct device *d,
4545                                  struct device_attribute *attr,
4546                                  const char *buf, size_t count)
4547 {
4548         struct iwl_priv *priv = dev_get_drvdata(d);
4549         int ret;
4550         unsigned long mode;
4551
4552
4553         mutex_lock(&priv->mutex);
4554
4555         ret = strict_strtoul(buf, 10, &mode);
4556         if (ret)
4557                 goto out;
4558
4559         ret = iwl_power_set_user_mode(priv, mode);
4560         if (ret) {
4561                 IWL_DEBUG_MAC80211(priv, "failed setting power mode.\n");
4562                 goto out;
4563         }
4564         ret = count;
4565
4566  out:
4567         mutex_unlock(&priv->mutex);
4568         return ret;
4569 }
4570
4571 static ssize_t show_power_level(struct device *d,
4572                                 struct device_attribute *attr, char *buf)
4573 {
4574         struct iwl_priv *priv = dev_get_drvdata(d);
4575         int mode = priv->power_data.user_power_setting;
4576         int system = priv->power_data.system_power_setting;
4577         int level = priv->power_data.power_mode;
4578         char *p = buf;
4579
4580         switch (system) {
4581         case IWL_POWER_SYS_AUTO:
4582                 p += sprintf(p, "SYSTEM:auto");
4583                 break;
4584         case IWL_POWER_SYS_AC:
4585                 p += sprintf(p, "SYSTEM:ac");
4586                 break;
4587         case IWL_POWER_SYS_BATTERY:
4588                 p += sprintf(p, "SYSTEM:battery");
4589                 break;
4590         }
4591
4592         p += sprintf(p, "\tMODE:%s", (mode < IWL_POWER_AUTO) ?
4593                         "fixed" : "auto");
4594         p += sprintf(p, "\tINDEX:%d", level);
4595         p += sprintf(p, "\n");
4596         return p - buf + 1;
4597 }
4598
4599 static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR,
4600                    show_power_level, store_power_level);
4601
4602 #define MAX_WX_STRING 80
4603
4604 /* Values are in microsecond */
4605 static const s32 timeout_duration[] = {
4606         350000,
4607         250000,
4608         75000,
4609         37000,
4610         25000,
4611 };
4612 static const s32 period_duration[] = {
4613         400000,
4614         700000,
4615         1000000,
4616         1000000,
4617         1000000
4618 };
4619
4620 static ssize_t show_channels(struct device *d,
4621                              struct device_attribute *attr, char *buf)
4622 {
4623         /* all this shit doesn't belong into sysfs anyway */
4624         return 0;
4625 }
4626
4627 static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
4628
4629 static ssize_t show_statistics(struct device *d,
4630                                struct device_attribute *attr, char *buf)
4631 {
4632         struct iwl_priv *priv = dev_get_drvdata(d);
4633         u32 size = sizeof(struct iwl3945_notif_statistics);
4634         u32 len = 0, ofs = 0;
4635         u8 *data = (u8 *)&priv->statistics_39;
4636         int rc = 0;
4637
4638         if (!iwl_is_alive(priv))
4639                 return -EAGAIN;
4640
4641         mutex_lock(&priv->mutex);
4642         rc = iwl_send_statistics_request(priv, 0);
4643         mutex_unlock(&priv->mutex);
4644
4645         if (rc) {
4646                 len = sprintf(buf,
4647                               "Error sending statistics request: 0x%08X\n", rc);
4648                 return len;
4649         }
4650
4651         while (size && (PAGE_SIZE - len)) {
4652                 hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
4653                                    PAGE_SIZE - len, 1);
4654                 len = strlen(buf);
4655                 if (PAGE_SIZE - len)
4656                         buf[len++] = '\n';
4657
4658                 ofs += 16;
4659                 size -= min(size, 16U);
4660         }
4661
4662         return len;
4663 }
4664
4665 static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
4666
4667 static ssize_t show_antenna(struct device *d,
4668                             struct device_attribute *attr, char *buf)
4669 {
4670         struct iwl_priv *priv = dev_get_drvdata(d);
4671
4672         if (!iwl_is_alive(priv))
4673                 return -EAGAIN;
4674
4675         return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
4676 }
4677
4678 static ssize_t store_antenna(struct device *d,
4679                              struct device_attribute *attr,
4680                              const char *buf, size_t count)
4681 {
4682         struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
4683         int ant;
4684
4685         if (count == 0)
4686                 return 0;
4687
4688         if (sscanf(buf, "%1i", &ant) != 1) {
4689                 IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
4690                 return count;
4691         }
4692
4693         if ((ant >= 0) && (ant <= 2)) {
4694                 IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
4695                 iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
4696         } else
4697                 IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
4698
4699
4700         return count;
4701 }
4702
4703 static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
4704
4705 static ssize_t show_status(struct device *d,
4706                            struct device_attribute *attr, char *buf)
4707 {
4708         struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
4709         if (!iwl_is_alive(priv))
4710                 return -EAGAIN;
4711         return sprintf(buf, "0x%08x\n", (int)priv->status);
4712 }
4713
4714 static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
4715
4716 static ssize_t dump_error_log(struct device *d,
4717                               struct device_attribute *attr,
4718                               const char *buf, size_t count)
4719 {
4720         char *p = (char *)buf;
4721
4722         if (p[0] == '1')
4723                 iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
4724
4725         return strnlen(buf, count);
4726 }
4727
4728 static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
4729
4730 static ssize_t dump_event_log(struct device *d,
4731                               struct device_attribute *attr,
4732                               const char *buf, size_t count)
4733 {
4734         char *p = (char *)buf;
4735
4736         if (p[0] == '1')
4737                 iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
4738
4739         return strnlen(buf, count);
4740 }
4741
4742 static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
4743
4744 /*****************************************************************************
4745  *
4746  * driver setup and tear down
4747  *
4748  *****************************************************************************/
4749
4750 static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
4751 {
4752         priv->workqueue = create_singlethread_workqueue(DRV_NAME);
4753
4754         init_waitqueue_head(&priv->wait_command_queue);
4755
4756         INIT_WORK(&priv->up, iwl3945_bg_up);
4757         INIT_WORK(&priv->restart, iwl3945_bg_restart);
4758         INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
4759         INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
4760         INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
4761         INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
4762         INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
4763         INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
4764         INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
4765         INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
4766         INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
4767         INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
4768
4769         iwl3945_hw_setup_deferred_work(priv);
4770
4771         tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
4772                      iwl3945_irq_tasklet, (unsigned long)priv);
4773 }
4774
4775 static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
4776 {
4777         iwl3945_hw_cancel_deferred_work(priv);
4778
4779         cancel_delayed_work_sync(&priv->init_alive_start);
4780         cancel_delayed_work(&priv->scan_check);
4781         cancel_delayed_work(&priv->alive_start);
4782         cancel_work_sync(&priv->beacon_update);
4783 }
4784
4785 static struct attribute *iwl3945_sysfs_entries[] = {
4786         &dev_attr_antenna.attr,
4787         &dev_attr_channels.attr,
4788         &dev_attr_dump_errors.attr,
4789         &dev_attr_dump_events.attr,
4790         &dev_attr_flags.attr,
4791         &dev_attr_filter_flags.attr,
4792 #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
4793         &dev_attr_measurement.attr,
4794 #endif
4795         &dev_attr_power_level.attr,
4796         &dev_attr_retry_rate.attr,
4797         &dev_attr_statistics.attr,
4798         &dev_attr_status.attr,
4799         &dev_attr_temperature.attr,
4800         &dev_attr_tx_power.attr,
4801 #ifdef CONFIG_IWLWIFI_DEBUG
4802         &dev_attr_debug_level.attr,
4803 #endif
4804         NULL
4805 };
4806
4807 static struct attribute_group iwl3945_attribute_group = {
4808         .name = NULL,           /* put in device directory */
4809         .attrs = iwl3945_sysfs_entries,
4810 };
4811
4812 static struct ieee80211_ops iwl3945_hw_ops = {
4813         .tx = iwl3945_mac_tx,
4814         .start = iwl3945_mac_start,
4815         .stop = iwl3945_mac_stop,
4816         .add_interface = iwl3945_mac_add_interface,
4817         .remove_interface = iwl3945_mac_remove_interface,
4818         .config = iwl3945_mac_config,
4819         .config_interface = iwl3945_mac_config_interface,
4820         .configure_filter = iwl_configure_filter,
4821         .set_key = iwl3945_mac_set_key,
4822         .get_tx_stats = iwl3945_mac_get_tx_stats,
4823         .conf_tx = iwl3945_mac_conf_tx,
4824         .reset_tsf = iwl3945_mac_reset_tsf,
4825         .bss_info_changed = iwl3945_bss_info_changed,
4826         .hw_scan = iwl_mac_hw_scan
4827 };
4828
4829 static int iwl3945_init_drv(struct iwl_priv *priv)
4830 {
4831         int ret;
4832         struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
4833
4834         priv->retry_rate = 1;
4835         priv->ibss_beacon = NULL;
4836
4837         spin_lock_init(&priv->lock);
4838         spin_lock_init(&priv->power_data.lock);
4839         spin_lock_init(&priv->sta_lock);
4840         spin_lock_init(&priv->hcmd_lock);
4841
4842         INIT_LIST_HEAD(&priv->free_frames);
4843
4844         mutex_init(&priv->mutex);
4845
4846         /* Clear the driver's (not device's) station table */
4847         iwl3945_clear_stations_table(priv);
4848
4849         priv->data_retry_limit = -1;
4850         priv->ieee_channels = NULL;
4851         priv->ieee_rates = NULL;
4852         priv->band = IEEE80211_BAND_2GHZ;
4853
4854         priv->iw_mode = NL80211_IFTYPE_STATION;
4855
4856         iwl_reset_qos(priv);
4857
4858         priv->qos_data.qos_active = 0;
4859         priv->qos_data.qos_cap.val = 0;
4860
4861         priv->rates_mask = IWL_RATES_MASK;
4862         /* If power management is turned on, default to CAM mode */
4863         priv->power_mode = IWL_POWER_MODE_CAM;
4864         priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
4865
4866         if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
4867                 IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
4868                          eeprom->version);
4869                 ret = -EINVAL;
4870                 goto err;
4871         }
4872         ret = iwl_init_channel_map(priv);
4873         if (ret) {
4874                 IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
4875                 goto err;
4876         }
4877
4878         /* Set up txpower settings in driver for all channels */
4879         if (iwl3945_txpower_set_from_eeprom(priv)) {
4880                 ret = -EIO;
4881                 goto err_free_channel_map;
4882         }
4883
4884         ret = iwlcore_init_geos(priv);
4885         if (ret) {
4886                 IWL_ERR(priv, "initializing geos failed: %d\n", ret);
4887                 goto err_free_channel_map;
4888         }
4889         iwl3945_init_hw_rates(priv, priv->ieee_rates);
4890
4891         return 0;
4892
4893 err_free_channel_map:
4894         iwl_free_channel_map(priv);
4895 err:
4896         return ret;
4897 }
4898
4899 static int iwl3945_setup_mac(struct iwl_priv *priv)
4900 {
4901         int ret;
4902         struct ieee80211_hw *hw = priv->hw;
4903
4904         hw->rate_control_algorithm = "iwl-3945-rs";
4905         hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
4906
4907         /* Tell mac80211 our characteristics */
4908         hw->flags = IEEE80211_HW_SIGNAL_DBM |
4909                     IEEE80211_HW_NOISE_DBM |
4910                     IEEE80211_HW_SPECTRUM_MGMT;
4911
4912         hw->wiphy->interface_modes =
4913                 BIT(NL80211_IFTYPE_STATION) |
4914                 BIT(NL80211_IFTYPE_ADHOC);
4915
4916         hw->wiphy->custom_regulatory = true;
4917
4918         hw->wiphy->max_scan_ssids = 1; /* WILL FIX */
4919
4920         /* Default value; 4 EDCA QOS priorities */
4921         hw->queues = 4;
4922
4923         hw->conf.beacon_int = 100;
4924
4925         if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
4926                 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
4927                         &priv->bands[IEEE80211_BAND_2GHZ];
4928
4929         if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
4930                 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
4931                         &priv->bands[IEEE80211_BAND_5GHZ];
4932
4933         ret = ieee80211_register_hw(priv->hw);
4934         if (ret) {
4935                 IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
4936                 return ret;
4937         }
4938         priv->mac80211_registered = 1;
4939
4940         return 0;
4941 }
4942
4943 static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
4944 {
4945         int err = 0;
4946         struct iwl_priv *priv;
4947         struct ieee80211_hw *hw;
4948         struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
4949         struct iwl3945_eeprom *eeprom;
4950         unsigned long flags;
4951
4952         /***********************
4953          * 1. Allocating HW data
4954          * ********************/
4955
4956         /* mac80211 allocates memory for this device instance, including
4957          *   space for this driver's private structure */
4958         hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
4959         if (hw == NULL) {
4960                 printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
4961                 err = -ENOMEM;
4962                 goto out;
4963         }
4964         priv = hw->priv;
4965         SET_IEEE80211_DEV(hw, &pdev->dev);
4966
4967         if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
4968              (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
4969                 IWL_ERR(priv,
4970                         "invalid queues_num, should be between %d and %d\n",
4971                         IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
4972                 err = -EINVAL;
4973                 goto out_ieee80211_free_hw;
4974         }
4975
4976         /*
4977          * Disabling hardware scan means that mac80211 will perform scans
4978          * "the hard way", rather than using device's scan.
4979          */
4980         if (iwl3945_mod_params.disable_hw_scan) {
4981                 IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
4982                 iwl3945_hw_ops.hw_scan = NULL;
4983         }
4984
4985
4986         IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
4987         priv->cfg = cfg;
4988         priv->pci_dev = pdev;
4989
4990 #ifdef CONFIG_IWLWIFI_DEBUG
4991         priv->debug_level = iwl3945_mod_params.debug;
4992         atomic_set(&priv->restrict_refcnt, 0);
4993 #endif
4994
4995         /***************************
4996          * 2. Initializing PCI bus
4997          * *************************/
4998         if (pci_enable_device(pdev)) {
4999                 err = -ENODEV;
5000                 goto out_ieee80211_free_hw;
5001         }
5002
5003         pci_set_master(pdev);
5004
5005         err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
5006         if (!err)
5007                 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
5008         if (err) {
5009                 IWL_WARN(priv, "No suitable DMA available.\n");
5010                 goto out_pci_disable_device;
5011         }
5012
5013         pci_set_drvdata(pdev, priv);
5014         err = pci_request_regions(pdev, DRV_NAME);
5015         if (err)
5016                 goto out_pci_disable_device;
5017
5018         /***********************
5019          * 3. Read REV Register
5020          * ********************/
5021         priv->hw_base = pci_iomap(pdev, 0, 0);
5022         if (!priv->hw_base) {
5023                 err = -ENODEV;
5024                 goto out_pci_release_regions;
5025         }
5026
5027         IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
5028                         (unsigned long long) pci_resource_len(pdev, 0));
5029         IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
5030
5031         /* We disable the RETRY_TIMEOUT register (0x41) to keep
5032          * PCI Tx retries from interfering with C3 CPU state */
5033         pci_write_config_byte(pdev, 0x41, 0x00);
5034
5035         /* amp init */
5036         err = priv->cfg->ops->lib->apm_ops.init(priv);
5037         if (err < 0) {
5038                 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
5039                 goto out_iounmap;
5040         }
5041
5042         /***********************
5043          * 4. Read EEPROM
5044          * ********************/
5045
5046         /* Read the EEPROM */
5047         err = iwl_eeprom_init(priv);
5048         if (err) {
5049                 IWL_ERR(priv, "Unable to init EEPROM\n");
5050                 goto out_iounmap;
5051         }
5052         /* MAC Address location in EEPROM same for 3945/4965 */
5053         eeprom = (struct iwl3945_eeprom *)priv->eeprom;
5054         memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
5055         IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
5056         SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
5057
5058         /***********************
5059          * 5. Setup HW Constants
5060          * ********************/
5061         /* Device-specific setup */
5062         if (iwl3945_hw_set_hw_params(priv)) {
5063                 IWL_ERR(priv, "failed to set hw settings\n");
5064                 goto out_eeprom_free;
5065         }
5066
5067         /***********************
5068          * 6. Setup priv
5069          * ********************/
5070
5071         err = iwl3945_init_drv(priv);
5072         if (err) {
5073                 IWL_ERR(priv, "initializing driver failed\n");
5074                 goto out_unset_hw_params;
5075         }
5076
5077         IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
5078                 priv->cfg->name);
5079
5080         /***********************************
5081          * 7. Initialize Module Parameters
5082          * **********************************/
5083
5084         /* Initialize module parameter values here */
5085         /* Disable radio (SW RF KILL) via parameter when loading driver */
5086         if (iwl3945_mod_params.disable) {
5087                 set_bit(STATUS_RF_KILL_SW, &priv->status);
5088                 IWL_DEBUG_INFO(priv, "Radio disabled.\n");
5089         }
5090
5091
5092         /***********************
5093          * 8. Setup Services
5094          * ********************/
5095
5096         spin_lock_irqsave(&priv->lock, flags);
5097         iwl_disable_interrupts(priv);
5098         spin_unlock_irqrestore(&priv->lock, flags);
5099
5100         pci_enable_msi(priv->pci_dev);
5101
5102         err = request_irq(priv->pci_dev->irq, iwl_isr, IRQF_SHARED,
5103                           DRV_NAME, priv);
5104         if (err) {
5105                 IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
5106                 goto out_disable_msi;
5107         }
5108
5109         err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
5110         if (err) {
5111                 IWL_ERR(priv, "failed to create sysfs device attributes\n");
5112                 goto out_release_irq;
5113         }
5114
5115         iwl_set_rxon_channel(priv,
5116                              &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
5117         iwl3945_setup_deferred_work(priv);
5118         iwl3945_setup_rx_handlers(priv);
5119
5120         /*********************************
5121          * 9. Setup and Register mac80211
5122          * *******************************/
5123
5124         iwl_enable_interrupts(priv);
5125
5126         err = iwl3945_setup_mac(priv);
5127         if (err)
5128                 goto  out_remove_sysfs;
5129
5130         err = iwl_rfkill_init(priv);
5131         if (err)
5132                 IWL_ERR(priv, "Unable to initialize RFKILL system. "
5133                                   "Ignoring error: %d\n", err);
5134         else
5135                 iwl_rfkill_set_hw_state(priv);
5136
5137         /* Start monitoring the killswitch */
5138         queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
5139                            2 * HZ);
5140
5141         return 0;
5142
5143  out_remove_sysfs:
5144         destroy_workqueue(priv->workqueue);
5145         priv->workqueue = NULL;
5146         sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
5147  out_release_irq:
5148         free_irq(priv->pci_dev->irq, priv);
5149  out_disable_msi:
5150         pci_disable_msi(priv->pci_dev);
5151         iwlcore_free_geos(priv);
5152         iwl_free_channel_map(priv);
5153  out_unset_hw_params:
5154         iwl3945_unset_hw_params(priv);
5155  out_eeprom_free:
5156         iwl_eeprom_free(priv);
5157  out_iounmap:
5158         pci_iounmap(pdev, priv->hw_base);
5159  out_pci_release_regions:
5160         pci_release_regions(pdev);
5161  out_pci_disable_device:
5162         pci_set_drvdata(pdev, NULL);
5163         pci_disable_device(pdev);
5164  out_ieee80211_free_hw:
5165         ieee80211_free_hw(priv->hw);
5166  out:
5167         return err;
5168 }
5169
5170 static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
5171 {
5172         struct iwl_priv *priv = pci_get_drvdata(pdev);
5173         unsigned long flags;
5174
5175         if (!priv)
5176                 return;
5177
5178         IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
5179
5180         set_bit(STATUS_EXIT_PENDING, &priv->status);
5181
5182         if (priv->mac80211_registered) {
5183                 ieee80211_unregister_hw(priv->hw);
5184                 priv->mac80211_registered = 0;
5185         } else {
5186                 iwl3945_down(priv);
5187         }
5188
5189         /* make sure we flush any pending irq or
5190          * tasklet for the driver
5191          */
5192         spin_lock_irqsave(&priv->lock, flags);
5193         iwl_disable_interrupts(priv);
5194         spin_unlock_irqrestore(&priv->lock, flags);
5195
5196         iwl_synchronize_irq(priv);
5197
5198         sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
5199
5200         iwl_rfkill_unregister(priv);
5201         cancel_delayed_work_sync(&priv->rfkill_poll);
5202
5203         iwl3945_dealloc_ucode_pci(priv);
5204
5205         if (priv->rxq.bd)
5206                 iwl_rx_queue_free(priv, &priv->rxq);
5207         iwl3945_hw_txq_ctx_free(priv);
5208
5209         iwl3945_unset_hw_params(priv);
5210         iwl3945_clear_stations_table(priv);
5211
5212         /*netif_stop_queue(dev); */
5213         flush_workqueue(priv->workqueue);
5214
5215         /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
5216          * priv->workqueue... so we can't take down the workqueue
5217          * until now... */
5218         destroy_workqueue(priv->workqueue);
5219         priv->workqueue = NULL;
5220
5221         free_irq(pdev->irq, priv);
5222         pci_disable_msi(pdev);
5223
5224         pci_iounmap(pdev, priv->hw_base);
5225         pci_release_regions(pdev);
5226         pci_disable_device(pdev);
5227         pci_set_drvdata(pdev, NULL);
5228
5229         iwl_free_channel_map(priv);
5230         iwlcore_free_geos(priv);
5231         kfree(priv->scan);
5232         if (priv->ibss_beacon)
5233                 dev_kfree_skb(priv->ibss_beacon);
5234
5235         ieee80211_free_hw(priv->hw);
5236 }
5237
5238 #ifdef CONFIG_PM
5239
5240 static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
5241 {
5242         struct iwl_priv *priv = pci_get_drvdata(pdev);
5243
5244         if (priv->is_open) {
5245                 set_bit(STATUS_IN_SUSPEND, &priv->status);
5246                 iwl3945_mac_stop(priv->hw);
5247                 priv->is_open = 1;
5248         }
5249         pci_save_state(pdev);
5250         pci_disable_device(pdev);
5251         pci_set_power_state(pdev, PCI_D3hot);
5252
5253         return 0;
5254 }
5255
5256 static int iwl3945_pci_resume(struct pci_dev *pdev)
5257 {
5258         struct iwl_priv *priv = pci_get_drvdata(pdev);
5259         int ret;
5260
5261         pci_set_power_state(pdev, PCI_D0);
5262         ret = pci_enable_device(pdev);
5263         if (ret)
5264                 return ret;
5265         pci_restore_state(pdev);
5266
5267         if (priv->is_open)
5268                 iwl3945_mac_start(priv->hw);
5269
5270         clear_bit(STATUS_IN_SUSPEND, &priv->status);
5271         return 0;
5272 }
5273
5274 #endif /* CONFIG_PM */
5275
5276 /*****************************************************************************
5277  *
5278  * driver and module entry point
5279  *
5280  *****************************************************************************/
5281
5282 static struct pci_driver iwl3945_driver = {
5283         .name = DRV_NAME,
5284         .id_table = iwl3945_hw_card_ids,
5285         .probe = iwl3945_pci_probe,
5286         .remove = __devexit_p(iwl3945_pci_remove),
5287 #ifdef CONFIG_PM
5288         .suspend = iwl3945_pci_suspend,
5289         .resume = iwl3945_pci_resume,
5290 #endif
5291 };
5292
5293 static int __init iwl3945_init(void)
5294 {
5295
5296         int ret;
5297         printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
5298         printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
5299
5300         ret = iwl3945_rate_control_register();
5301         if (ret) {
5302                 printk(KERN_ERR DRV_NAME
5303                        "Unable to register rate control algorithm: %d\n", ret);
5304                 return ret;
5305         }
5306
5307         ret = pci_register_driver(&iwl3945_driver);
5308         if (ret) {
5309                 printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
5310                 goto error_register;
5311         }
5312
5313         return ret;
5314
5315 error_register:
5316         iwl3945_rate_control_unregister();
5317         return ret;
5318 }
5319
5320 static void __exit iwl3945_exit(void)
5321 {
5322         pci_unregister_driver(&iwl3945_driver);
5323         iwl3945_rate_control_unregister();
5324 }
5325
5326 MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
5327
5328 module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
5329 MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
5330 module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
5331 MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
5332 module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
5333 MODULE_PARM_DESC(swcrypto,
5334                  "using software crypto (default 1 [software])\n");
5335 module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
5336 MODULE_PARM_DESC(debug, "debug output mask");
5337 module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
5338 MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
5339
5340 module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
5341 MODULE_PARM_DESC(queues_num, "number of hw queues.");
5342
5343 module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, 0444);
5344 MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
5345
5346 module_exit(iwl3945_exit);
5347 module_init(iwl3945_init);