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