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