]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/net/wireless/iwlwifi/iwl-core.c
iwlwifi: move agn only tx functions from iwlcore to iwlagn
[karo-tx-linux.git] / drivers / net / wireless / iwlwifi / iwl-core.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of version 2 of the GNU General Public License as
9  * published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
19  * USA
20  *
21  * The full GNU General Public License is included in this distribution
22  * in the file called LICENSE.GPL.
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 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/etherdevice.h>
32 #include <linux/sched.h>
33 #include <net/mac80211.h>
34
35 #include "iwl-eeprom.h"
36 #include "iwl-dev.h" /* FIXME: remove */
37 #include "iwl-debug.h"
38 #include "iwl-core.h"
39 #include "iwl-io.h"
40 #include "iwl-power.h"
41 #include "iwl-sta.h"
42 #include "iwl-helpers.h"
43
44
45 MODULE_DESCRIPTION("iwl core");
46 MODULE_VERSION(IWLWIFI_VERSION);
47 MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
48 MODULE_LICENSE("GPL");
49
50 /*
51  * set bt_coex_active to true, uCode will do kill/defer
52  * every time the priority line is asserted (BT is sending signals on the
53  * priority line in the PCIx).
54  * set bt_coex_active to false, uCode will ignore the BT activity and
55  * perform the normal operation
56  *
57  * User might experience transmit issue on some platform due to WiFi/BT
58  * co-exist problem. The possible behaviors are:
59  *   Able to scan and finding all the available AP
60  *   Not able to associate with any AP
61  * On those platforms, WiFi communication can be restored by set
62  * "bt_coex_active" module parameter to "false"
63  *
64  * default: bt_coex_active = true (BT_COEX_ENABLE)
65  */
66 static bool bt_coex_active = true;
67 module_param(bt_coex_active, bool, S_IRUGO);
68 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bluetooth co-exist\n");
69
70 static struct iwl_wimax_coex_event_entry cu_priorities[COEX_NUM_OF_EVENTS] = {
71         {COEX_CU_UNASSOC_IDLE_RP, COEX_CU_UNASSOC_IDLE_WP,
72          0, COEX_UNASSOC_IDLE_FLAGS},
73         {COEX_CU_UNASSOC_MANUAL_SCAN_RP, COEX_CU_UNASSOC_MANUAL_SCAN_WP,
74          0, COEX_UNASSOC_MANUAL_SCAN_FLAGS},
75         {COEX_CU_UNASSOC_AUTO_SCAN_RP, COEX_CU_UNASSOC_AUTO_SCAN_WP,
76          0, COEX_UNASSOC_AUTO_SCAN_FLAGS},
77         {COEX_CU_CALIBRATION_RP, COEX_CU_CALIBRATION_WP,
78          0, COEX_CALIBRATION_FLAGS},
79         {COEX_CU_PERIODIC_CALIBRATION_RP, COEX_CU_PERIODIC_CALIBRATION_WP,
80          0, COEX_PERIODIC_CALIBRATION_FLAGS},
81         {COEX_CU_CONNECTION_ESTAB_RP, COEX_CU_CONNECTION_ESTAB_WP,
82          0, COEX_CONNECTION_ESTAB_FLAGS},
83         {COEX_CU_ASSOCIATED_IDLE_RP, COEX_CU_ASSOCIATED_IDLE_WP,
84          0, COEX_ASSOCIATED_IDLE_FLAGS},
85         {COEX_CU_ASSOC_MANUAL_SCAN_RP, COEX_CU_ASSOC_MANUAL_SCAN_WP,
86          0, COEX_ASSOC_MANUAL_SCAN_FLAGS},
87         {COEX_CU_ASSOC_AUTO_SCAN_RP, COEX_CU_ASSOC_AUTO_SCAN_WP,
88          0, COEX_ASSOC_AUTO_SCAN_FLAGS},
89         {COEX_CU_ASSOC_ACTIVE_LEVEL_RP, COEX_CU_ASSOC_ACTIVE_LEVEL_WP,
90          0, COEX_ASSOC_ACTIVE_LEVEL_FLAGS},
91         {COEX_CU_RF_ON_RP, COEX_CU_RF_ON_WP, 0, COEX_CU_RF_ON_FLAGS},
92         {COEX_CU_RF_OFF_RP, COEX_CU_RF_OFF_WP, 0, COEX_RF_OFF_FLAGS},
93         {COEX_CU_STAND_ALONE_DEBUG_RP, COEX_CU_STAND_ALONE_DEBUG_WP,
94          0, COEX_STAND_ALONE_DEBUG_FLAGS},
95         {COEX_CU_IPAN_ASSOC_LEVEL_RP, COEX_CU_IPAN_ASSOC_LEVEL_WP,
96          0, COEX_IPAN_ASSOC_LEVEL_FLAGS},
97         {COEX_CU_RSRVD1_RP, COEX_CU_RSRVD1_WP, 0, COEX_RSRVD1_FLAGS},
98         {COEX_CU_RSRVD2_RP, COEX_CU_RSRVD2_WP, 0, COEX_RSRVD2_FLAGS}
99 };
100
101 #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np)    \
102         [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP,      \
103                                     IWL_RATE_SISO_##s##M_PLCP, \
104                                     IWL_RATE_MIMO2_##s##M_PLCP,\
105                                     IWL_RATE_MIMO3_##s##M_PLCP,\
106                                     IWL_RATE_##r##M_IEEE,      \
107                                     IWL_RATE_##ip##M_INDEX,    \
108                                     IWL_RATE_##in##M_INDEX,    \
109                                     IWL_RATE_##rp##M_INDEX,    \
110                                     IWL_RATE_##rn##M_INDEX,    \
111                                     IWL_RATE_##pp##M_INDEX,    \
112                                     IWL_RATE_##np##M_INDEX }
113
114 u32 iwl_debug_level;
115 EXPORT_SYMBOL(iwl_debug_level);
116
117 /*
118  * Parameter order:
119  *   rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
120  *
121  * If there isn't a valid next or previous rate then INV is used which
122  * maps to IWL_RATE_INVALID
123  *
124  */
125 const struct iwl_rate_info iwl_rates[IWL_RATE_COUNT] = {
126         IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2),    /*  1mbps */
127         IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5),          /*  2mbps */
128         IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11),        /*5.5mbps */
129         IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18),      /* 11mbps */
130         IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11),        /*  6mbps */
131         IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11),       /*  9mbps */
132         IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18),   /* 12mbps */
133         IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24),   /* 18mbps */
134         IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36),   /* 24mbps */
135         IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48),   /* 36mbps */
136         IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54),   /* 48mbps */
137         IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
138         IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
139         /* FIXME:RS:          ^^    should be INV (legacy) */
140 };
141 EXPORT_SYMBOL(iwl_rates);
142
143 /**
144  * translate ucode response to mac80211 tx status control values
145  */
146 void iwl_hwrate_to_tx_control(struct iwl_priv *priv, u32 rate_n_flags,
147                                   struct ieee80211_tx_info *info)
148 {
149         struct ieee80211_tx_rate *r = &info->control.rates[0];
150
151         info->antenna_sel_tx =
152                 ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
153         if (rate_n_flags & RATE_MCS_HT_MSK)
154                 r->flags |= IEEE80211_TX_RC_MCS;
155         if (rate_n_flags & RATE_MCS_GF_MSK)
156                 r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
157         if (rate_n_flags & RATE_MCS_HT40_MSK)
158                 r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
159         if (rate_n_flags & RATE_MCS_DUP_MSK)
160                 r->flags |= IEEE80211_TX_RC_DUP_DATA;
161         if (rate_n_flags & RATE_MCS_SGI_MSK)
162                 r->flags |= IEEE80211_TX_RC_SHORT_GI;
163         r->idx = iwl_hwrate_to_mac80211_idx(rate_n_flags, info->band);
164 }
165 EXPORT_SYMBOL(iwl_hwrate_to_tx_control);
166
167 int iwl_hwrate_to_plcp_idx(u32 rate_n_flags)
168 {
169         int idx = 0;
170
171         /* HT rate format */
172         if (rate_n_flags & RATE_MCS_HT_MSK) {
173                 idx = (rate_n_flags & 0xff);
174
175                 if (idx >= IWL_RATE_MIMO3_6M_PLCP)
176                         idx = idx - IWL_RATE_MIMO3_6M_PLCP;
177                 else if (idx >= IWL_RATE_MIMO2_6M_PLCP)
178                         idx = idx - IWL_RATE_MIMO2_6M_PLCP;
179
180                 idx += IWL_FIRST_OFDM_RATE;
181                 /* skip 9M not supported in ht*/
182                 if (idx >= IWL_RATE_9M_INDEX)
183                         idx += 1;
184                 if ((idx >= IWL_FIRST_OFDM_RATE) && (idx <= IWL_LAST_OFDM_RATE))
185                         return idx;
186
187         /* legacy rate format, search for match in table */
188         } else {
189                 for (idx = 0; idx < ARRAY_SIZE(iwl_rates); idx++)
190                         if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
191                                 return idx;
192         }
193
194         return -1;
195 }
196 EXPORT_SYMBOL(iwl_hwrate_to_plcp_idx);
197
198 int iwl_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
199 {
200         int idx = 0;
201         int band_offset = 0;
202
203         /* HT rate format: mac80211 wants an MCS number, which is just LSB */
204         if (rate_n_flags & RATE_MCS_HT_MSK) {
205                 idx = (rate_n_flags & 0xff);
206                 return idx;
207         /* Legacy rate format, search for match in table */
208         } else {
209                 if (band == IEEE80211_BAND_5GHZ)
210                         band_offset = IWL_FIRST_OFDM_RATE;
211                 for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
212                         if (iwl_rates[idx].plcp == (rate_n_flags & 0xFF))
213                                 return idx - band_offset;
214         }
215
216         return -1;
217 }
218
219 u8 iwl_toggle_tx_ant(struct iwl_priv *priv, u8 ant)
220 {
221         int i;
222         u8 ind = ant;
223         for (i = 0; i < RATE_ANT_NUM - 1; i++) {
224                 ind = (ind + 1) < RATE_ANT_NUM ?  ind + 1 : 0;
225                 if (priv->hw_params.valid_tx_ant & BIT(ind))
226                         return ind;
227         }
228         return ant;
229 }
230 EXPORT_SYMBOL(iwl_toggle_tx_ant);
231
232 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
233 EXPORT_SYMBOL(iwl_bcast_addr);
234
235
236 /* This function both allocates and initializes hw and priv. */
237 struct ieee80211_hw *iwl_alloc_all(struct iwl_cfg *cfg,
238                 struct ieee80211_ops *hw_ops)
239 {
240         struct iwl_priv *priv;
241
242         /* mac80211 allocates memory for this device instance, including
243          *   space for this driver's private structure */
244         struct ieee80211_hw *hw =
245                 ieee80211_alloc_hw(sizeof(struct iwl_priv), hw_ops);
246         if (hw == NULL) {
247                 printk(KERN_ERR "%s: Can not allocate network device\n",
248                        cfg->name);
249                 goto out;
250         }
251
252         priv = hw->priv;
253         priv->hw = hw;
254
255 out:
256         return hw;
257 }
258 EXPORT_SYMBOL(iwl_alloc_all);
259
260 void iwl_hw_detect(struct iwl_priv *priv)
261 {
262         priv->hw_rev = _iwl_read32(priv, CSR_HW_REV);
263         priv->hw_wa_rev = _iwl_read32(priv, CSR_HW_REV_WA_REG);
264         pci_read_config_byte(priv->pci_dev, PCI_REVISION_ID, &priv->rev_id);
265 }
266 EXPORT_SYMBOL(iwl_hw_detect);
267
268 /*
269  * QoS  support
270 */
271 void iwl_activate_qos(struct iwl_priv *priv, u8 force)
272 {
273         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
274                 return;
275
276         priv->qos_data.def_qos_parm.qos_flags = 0;
277
278         if (priv->qos_data.qos_cap.q_AP.queue_request &&
279             !priv->qos_data.qos_cap.q_AP.txop_request)
280                 priv->qos_data.def_qos_parm.qos_flags |=
281                         QOS_PARAM_FLG_TXOP_TYPE_MSK;
282         if (priv->qos_data.qos_active)
283                 priv->qos_data.def_qos_parm.qos_flags |=
284                         QOS_PARAM_FLG_UPDATE_EDCA_MSK;
285
286         if (priv->current_ht_config.is_ht)
287                 priv->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
288
289         if (force || iwl_is_associated(priv)) {
290                 IWL_DEBUG_QOS(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
291                                 priv->qos_data.qos_active,
292                                 priv->qos_data.def_qos_parm.qos_flags);
293
294                 iwl_send_cmd_pdu_async(priv, REPLY_QOS_PARAM,
295                                        sizeof(struct iwl_qosparam_cmd),
296                                        &priv->qos_data.def_qos_parm, NULL);
297         }
298 }
299 EXPORT_SYMBOL(iwl_activate_qos);
300
301 /*
302  * AC        CWmin         CW max      AIFSN      TXOP Limit    TXOP Limit
303  *                                              (802.11b)      (802.11a/g)
304  * AC_BK      15            1023        7           0               0
305  * AC_BE      15            1023        3           0               0
306  * AC_VI       7              15        2          6.016ms       3.008ms
307  * AC_VO       3               7        2          3.264ms       1.504ms
308  */
309 void iwl_reset_qos(struct iwl_priv *priv)
310 {
311         u16 cw_min = 15;
312         u16 cw_max = 1023;
313         u8 aifs = 2;
314         bool is_legacy = false;
315         unsigned long flags;
316         int i;
317
318         spin_lock_irqsave(&priv->lock, flags);
319         /* QoS always active in AP and ADHOC mode
320          * In STA mode wait for association
321          */
322         if (priv->iw_mode == NL80211_IFTYPE_ADHOC ||
323             priv->iw_mode == NL80211_IFTYPE_AP)
324                 priv->qos_data.qos_active = 1;
325         else
326                 priv->qos_data.qos_active = 0;
327
328         /* check for legacy mode */
329         if ((priv->iw_mode == NL80211_IFTYPE_ADHOC &&
330             (priv->active_rate & IWL_OFDM_RATES_MASK) == 0) ||
331             (priv->iw_mode == NL80211_IFTYPE_STATION &&
332             (priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK) == 0)) {
333                 cw_min = 31;
334                 is_legacy = 1;
335         }
336
337         if (priv->qos_data.qos_active)
338                 aifs = 3;
339
340         /* AC_BE */
341         priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
342         priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
343         priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
344         priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
345         priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
346
347         if (priv->qos_data.qos_active) {
348                 /* AC_BK */
349                 i = 1;
350                 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
351                 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
352                 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
353                 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
354                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
355
356                 /* AC_VI */
357                 i = 2;
358                 priv->qos_data.def_qos_parm.ac[i].cw_min =
359                         cpu_to_le16((cw_min + 1) / 2 - 1);
360                 priv->qos_data.def_qos_parm.ac[i].cw_max =
361                         cpu_to_le16(cw_min);
362                 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
363                 if (is_legacy)
364                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
365                                 cpu_to_le16(6016);
366                 else
367                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
368                                 cpu_to_le16(3008);
369                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
370
371                 /* AC_VO */
372                 i = 3;
373                 priv->qos_data.def_qos_parm.ac[i].cw_min =
374                         cpu_to_le16((cw_min + 1) / 4 - 1);
375                 priv->qos_data.def_qos_parm.ac[i].cw_max =
376                         cpu_to_le16((cw_min + 1) / 2 - 1);
377                 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
378                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
379                 if (is_legacy)
380                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
381                                 cpu_to_le16(3264);
382                 else
383                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
384                                 cpu_to_le16(1504);
385         } else {
386                 for (i = 1; i < 4; i++) {
387                         priv->qos_data.def_qos_parm.ac[i].cw_min =
388                                 cpu_to_le16(cw_min);
389                         priv->qos_data.def_qos_parm.ac[i].cw_max =
390                                 cpu_to_le16(cw_max);
391                         priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
392                         priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
393                         priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
394                 }
395         }
396         IWL_DEBUG_QOS(priv, "set QoS to default \n");
397
398         spin_unlock_irqrestore(&priv->lock, flags);
399 }
400 EXPORT_SYMBOL(iwl_reset_qos);
401
402 #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
403 #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
404 static void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
405                               struct ieee80211_sta_ht_cap *ht_info,
406                               enum ieee80211_band band)
407 {
408         u16 max_bit_rate = 0;
409         u8 rx_chains_num = priv->hw_params.rx_chains_num;
410         u8 tx_chains_num = priv->hw_params.tx_chains_num;
411
412         ht_info->cap = 0;
413         memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
414
415         ht_info->ht_supported = true;
416
417         if (priv->cfg->ht_greenfield_support)
418                 ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
419         ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
420         max_bit_rate = MAX_BIT_RATE_20_MHZ;
421         if (priv->hw_params.ht40_channel & BIT(band)) {
422                 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
423                 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
424                 ht_info->mcs.rx_mask[4] = 0x01;
425                 max_bit_rate = MAX_BIT_RATE_40_MHZ;
426         }
427
428         if (priv->cfg->mod_params->amsdu_size_8K)
429                 ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
430
431         ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
432         ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
433
434         ht_info->mcs.rx_mask[0] = 0xFF;
435         if (rx_chains_num >= 2)
436                 ht_info->mcs.rx_mask[1] = 0xFF;
437         if (rx_chains_num >= 3)
438                 ht_info->mcs.rx_mask[2] = 0xFF;
439
440         /* Highest supported Rx data rate */
441         max_bit_rate *= rx_chains_num;
442         WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
443         ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
444
445         /* Tx MCS capabilities */
446         ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
447         if (tx_chains_num != rx_chains_num) {
448                 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
449                 ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
450                                 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
451         }
452 }
453
454 /**
455  * iwlcore_init_geos - Initialize mac80211's geo/channel info based from eeprom
456  */
457 int iwlcore_init_geos(struct iwl_priv *priv)
458 {
459         struct iwl_channel_info *ch;
460         struct ieee80211_supported_band *sband;
461         struct ieee80211_channel *channels;
462         struct ieee80211_channel *geo_ch;
463         struct ieee80211_rate *rates;
464         int i = 0;
465
466         if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
467             priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
468                 IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
469                 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
470                 return 0;
471         }
472
473         channels = kzalloc(sizeof(struct ieee80211_channel) *
474                            priv->channel_count, GFP_KERNEL);
475         if (!channels)
476                 return -ENOMEM;
477
478         rates = kzalloc((sizeof(struct ieee80211_rate) * IWL_RATE_COUNT_LEGACY),
479                         GFP_KERNEL);
480         if (!rates) {
481                 kfree(channels);
482                 return -ENOMEM;
483         }
484
485         /* 5.2GHz channels start after the 2.4GHz channels */
486         sband = &priv->bands[IEEE80211_BAND_5GHZ];
487         sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
488         /* just OFDM */
489         sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
490         sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
491
492         if (priv->cfg->sku & IWL_SKU_N)
493                 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
494                                          IEEE80211_BAND_5GHZ);
495
496         sband = &priv->bands[IEEE80211_BAND_2GHZ];
497         sband->channels = channels;
498         /* OFDM & CCK */
499         sband->bitrates = rates;
500         sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
501
502         if (priv->cfg->sku & IWL_SKU_N)
503                 iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
504                                          IEEE80211_BAND_2GHZ);
505
506         priv->ieee_channels = channels;
507         priv->ieee_rates = rates;
508
509         for (i = 0;  i < priv->channel_count; i++) {
510                 ch = &priv->channel_info[i];
511
512                 /* FIXME: might be removed if scan is OK */
513                 if (!is_channel_valid(ch))
514                         continue;
515
516                 if (is_channel_a_band(ch))
517                         sband =  &priv->bands[IEEE80211_BAND_5GHZ];
518                 else
519                         sband =  &priv->bands[IEEE80211_BAND_2GHZ];
520
521                 geo_ch = &sband->channels[sband->n_channels++];
522
523                 geo_ch->center_freq =
524                                 ieee80211_channel_to_frequency(ch->channel);
525                 geo_ch->max_power = ch->max_power_avg;
526                 geo_ch->max_antenna_gain = 0xff;
527                 geo_ch->hw_value = ch->channel;
528
529                 if (is_channel_valid(ch)) {
530                         if (!(ch->flags & EEPROM_CHANNEL_IBSS))
531                                 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
532
533                         if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
534                                 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
535
536                         if (ch->flags & EEPROM_CHANNEL_RADAR)
537                                 geo_ch->flags |= IEEE80211_CHAN_RADAR;
538
539                         geo_ch->flags |= ch->ht40_extension_channel;
540
541                         if (ch->max_power_avg > priv->tx_power_device_lmt)
542                                 priv->tx_power_device_lmt = ch->max_power_avg;
543                 } else {
544                         geo_ch->flags |= IEEE80211_CHAN_DISABLED;
545                 }
546
547                 IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
548                                 ch->channel, geo_ch->center_freq,
549                                 is_channel_a_band(ch) ?  "5.2" : "2.4",
550                                 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
551                                 "restricted" : "valid",
552                                  geo_ch->flags);
553         }
554
555         if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
556              priv->cfg->sku & IWL_SKU_A) {
557                 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
558                         "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
559                            priv->pci_dev->device,
560                            priv->pci_dev->subsystem_device);
561                 priv->cfg->sku &= ~IWL_SKU_A;
562         }
563
564         IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
565                    priv->bands[IEEE80211_BAND_2GHZ].n_channels,
566                    priv->bands[IEEE80211_BAND_5GHZ].n_channels);
567
568         set_bit(STATUS_GEO_CONFIGURED, &priv->status);
569
570         return 0;
571 }
572 EXPORT_SYMBOL(iwlcore_init_geos);
573
574 /*
575  * iwlcore_free_geos - undo allocations in iwlcore_init_geos
576  */
577 void iwlcore_free_geos(struct iwl_priv *priv)
578 {
579         kfree(priv->ieee_channels);
580         kfree(priv->ieee_rates);
581         clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
582 }
583 EXPORT_SYMBOL(iwlcore_free_geos);
584
585 /*
586  *  iwlcore_rts_tx_cmd_flag: Set rts/cts. 3945 and 4965 only share this
587  *  function.
588  */
589 void iwlcore_rts_tx_cmd_flag(struct ieee80211_tx_info *info,
590                                 __le32 *tx_flags)
591 {
592         if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
593                 *tx_flags |= TX_CMD_FLG_RTS_MSK;
594                 *tx_flags &= ~TX_CMD_FLG_CTS_MSK;
595         } else if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
596                 *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
597                 *tx_flags |= TX_CMD_FLG_CTS_MSK;
598         }
599 }
600 EXPORT_SYMBOL(iwlcore_rts_tx_cmd_flag);
601
602 static bool is_single_rx_stream(struct iwl_priv *priv)
603 {
604         return priv->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
605                priv->current_ht_config.single_chain_sufficient;
606 }
607
608 static u8 iwl_is_channel_extension(struct iwl_priv *priv,
609                                    enum ieee80211_band band,
610                                    u16 channel, u8 extension_chan_offset)
611 {
612         const struct iwl_channel_info *ch_info;
613
614         ch_info = iwl_get_channel_info(priv, band, channel);
615         if (!is_channel_valid(ch_info))
616                 return 0;
617
618         if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
619                 return !(ch_info->ht40_extension_channel &
620                                         IEEE80211_CHAN_NO_HT40PLUS);
621         else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
622                 return !(ch_info->ht40_extension_channel &
623                                         IEEE80211_CHAN_NO_HT40MINUS);
624
625         return 0;
626 }
627
628 u8 iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
629                          struct ieee80211_sta_ht_cap *sta_ht_inf)
630 {
631         struct iwl_ht_config *ht_conf = &priv->current_ht_config;
632
633         if (!ht_conf->is_ht || !ht_conf->is_40mhz)
634                 return 0;
635
636         /* We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
637          * the bit will not set if it is pure 40MHz case
638          */
639         if (sta_ht_inf) {
640                 if (!sta_ht_inf->ht_supported)
641                         return 0;
642         }
643 #ifdef CONFIG_IWLWIFI_DEBUG
644         if (priv->disable_ht40)
645                 return 0;
646 #endif
647         return iwl_is_channel_extension(priv, priv->band,
648                         le16_to_cpu(priv->staging_rxon.channel),
649                         ht_conf->extension_chan_offset);
650 }
651 EXPORT_SYMBOL(iwl_is_ht40_tx_allowed);
652
653 static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
654 {
655         u16 new_val = 0;
656         u16 beacon_factor = 0;
657
658         beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
659         new_val = beacon_val / beacon_factor;
660
661         if (!new_val)
662                 new_val = max_beacon_val;
663
664         return new_val;
665 }
666
667 void iwl_setup_rxon_timing(struct iwl_priv *priv)
668 {
669         u64 tsf;
670         s32 interval_tm, rem;
671         unsigned long flags;
672         struct ieee80211_conf *conf = NULL;
673         u16 beacon_int;
674
675         conf = ieee80211_get_hw_conf(priv->hw);
676
677         spin_lock_irqsave(&priv->lock, flags);
678         priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
679         priv->rxon_timing.listen_interval = cpu_to_le16(conf->listen_interval);
680
681         if (priv->iw_mode == NL80211_IFTYPE_STATION) {
682                 beacon_int = priv->beacon_int;
683                 priv->rxon_timing.atim_window = 0;
684         } else {
685                 beacon_int = priv->vif->bss_conf.beacon_int;
686
687                 /* TODO: we need to get atim_window from upper stack
688                  * for now we set to 0 */
689                 priv->rxon_timing.atim_window = 0;
690         }
691
692         beacon_int = iwl_adjust_beacon_interval(beacon_int,
693                                 priv->hw_params.max_beacon_itrvl * 1024);
694         priv->rxon_timing.beacon_interval = cpu_to_le16(beacon_int);
695
696         tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
697         interval_tm = beacon_int * 1024;
698         rem = do_div(tsf, interval_tm);
699         priv->rxon_timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
700
701         spin_unlock_irqrestore(&priv->lock, flags);
702         IWL_DEBUG_ASSOC(priv,
703                         "beacon interval %d beacon timer %d beacon tim %d\n",
704                         le16_to_cpu(priv->rxon_timing.beacon_interval),
705                         le32_to_cpu(priv->rxon_timing.beacon_init_val),
706                         le16_to_cpu(priv->rxon_timing.atim_window));
707 }
708 EXPORT_SYMBOL(iwl_setup_rxon_timing);
709
710 void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
711 {
712         struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
713
714         if (hw_decrypt)
715                 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
716         else
717                 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
718
719 }
720 EXPORT_SYMBOL(iwl_set_rxon_hwcrypto);
721
722 /**
723  * iwl_check_rxon_cmd - validate RXON structure is valid
724  *
725  * NOTE:  This is really only useful during development and can eventually
726  * be #ifdef'd out once the driver is stable and folks aren't actively
727  * making changes
728  */
729 int iwl_check_rxon_cmd(struct iwl_priv *priv)
730 {
731         int error = 0;
732         int counter = 1;
733         struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
734
735         if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
736                 error |= le32_to_cpu(rxon->flags &
737                                 (RXON_FLG_TGJ_NARROW_BAND_MSK |
738                                  RXON_FLG_RADAR_DETECT_MSK));
739                 if (error)
740                         IWL_WARN(priv, "check 24G fields %d | %d\n",
741                                     counter++, error);
742         } else {
743                 error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
744                                 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
745                 if (error)
746                         IWL_WARN(priv, "check 52 fields %d | %d\n",
747                                     counter++, error);
748                 error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
749                 if (error)
750                         IWL_WARN(priv, "check 52 CCK %d | %d\n",
751                                     counter++, error);
752         }
753         error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
754         if (error)
755                 IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
756
757         /* make sure basic rates 6Mbps and 1Mbps are supported */
758         error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
759                   ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
760         if (error)
761                 IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
762
763         error |= (le16_to_cpu(rxon->assoc_id) > 2007);
764         if (error)
765                 IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
766
767         error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
768                         == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
769         if (error)
770                 IWL_WARN(priv, "check CCK and short slot %d | %d\n",
771                             counter++, error);
772
773         error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
774                         == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
775         if (error)
776                 IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
777                             counter++, error);
778
779         error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
780                         RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
781         if (error)
782                 IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
783                             counter++, error);
784
785         if (error)
786                 IWL_WARN(priv, "Tuning to channel %d\n",
787                             le16_to_cpu(rxon->channel));
788
789         if (error) {
790                 IWL_ERR(priv, "Not a valid iwl_rxon_assoc_cmd field values\n");
791                 return -1;
792         }
793         return 0;
794 }
795 EXPORT_SYMBOL(iwl_check_rxon_cmd);
796
797 /**
798  * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
799  * @priv: staging_rxon is compared to active_rxon
800  *
801  * If the RXON structure is changing enough to require a new tune,
802  * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
803  * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
804  */
805 int iwl_full_rxon_required(struct iwl_priv *priv)
806 {
807
808         /* These items are only settable from the full RXON command */
809         if (!(iwl_is_associated(priv)) ||
810             compare_ether_addr(priv->staging_rxon.bssid_addr,
811                                priv->active_rxon.bssid_addr) ||
812             compare_ether_addr(priv->staging_rxon.node_addr,
813                                priv->active_rxon.node_addr) ||
814             compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
815                                priv->active_rxon.wlap_bssid_addr) ||
816             (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
817             (priv->staging_rxon.channel != priv->active_rxon.channel) ||
818             (priv->staging_rxon.air_propagation !=
819              priv->active_rxon.air_propagation) ||
820             (priv->staging_rxon.ofdm_ht_single_stream_basic_rates !=
821              priv->active_rxon.ofdm_ht_single_stream_basic_rates) ||
822             (priv->staging_rxon.ofdm_ht_dual_stream_basic_rates !=
823              priv->active_rxon.ofdm_ht_dual_stream_basic_rates) ||
824             (priv->staging_rxon.ofdm_ht_triple_stream_basic_rates !=
825              priv->active_rxon.ofdm_ht_triple_stream_basic_rates) ||
826             (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
827                 return 1;
828
829         /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
830          * be updated with the RXON_ASSOC command -- however only some
831          * flag transitions are allowed using RXON_ASSOC */
832
833         /* Check if we are not switching bands */
834         if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
835             (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
836                 return 1;
837
838         /* Check if we are switching association toggle */
839         if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
840                 (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
841                 return 1;
842
843         return 0;
844 }
845 EXPORT_SYMBOL(iwl_full_rxon_required);
846
847 u8 iwl_rate_get_lowest_plcp(struct iwl_priv *priv)
848 {
849         /*
850          * Assign the lowest rate -- should really get this from
851          * the beacon skb from mac80211.
852          */
853         if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)
854                 return IWL_RATE_1M_PLCP;
855         else
856                 return IWL_RATE_6M_PLCP;
857 }
858 EXPORT_SYMBOL(iwl_rate_get_lowest_plcp);
859
860 void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
861 {
862         struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
863
864         if (!ht_conf->is_ht) {
865                 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
866                         RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
867                         RXON_FLG_HT40_PROT_MSK |
868                         RXON_FLG_HT_PROT_MSK);
869                 return;
870         }
871
872         /* FIXME: if the definition of ht_protection changed, the "translation"
873          * will be needed for rxon->flags
874          */
875         rxon->flags |= cpu_to_le32(ht_conf->ht_protection << RXON_FLG_HT_OPERATING_MODE_POS);
876
877         /* Set up channel bandwidth:
878          * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
879         /* clear the HT channel mode before set the mode */
880         rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
881                          RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
882         if (iwl_is_ht40_tx_allowed(priv, NULL)) {
883                 /* pure ht40 */
884                 if (ht_conf->ht_protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
885                         rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
886                         /* Note: control channel is opposite of extension channel */
887                         switch (ht_conf->extension_chan_offset) {
888                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
889                                 rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
890                                 break;
891                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
892                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
893                                 break;
894                         }
895                 } else {
896                         /* Note: control channel is opposite of extension channel */
897                         switch (ht_conf->extension_chan_offset) {
898                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
899                                 rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
900                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
901                                 break;
902                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
903                                 rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
904                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
905                                 break;
906                         case IEEE80211_HT_PARAM_CHA_SEC_NONE:
907                         default:
908                                 /* channel location only valid if in Mixed mode */
909                                 IWL_ERR(priv, "invalid extension channel offset\n");
910                                 break;
911                         }
912                 }
913         } else {
914                 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
915         }
916
917         if (priv->cfg->ops->hcmd->set_rxon_chain)
918                 priv->cfg->ops->hcmd->set_rxon_chain(priv);
919
920         IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
921                         "extension channel offset 0x%x\n",
922                         le32_to_cpu(rxon->flags), ht_conf->ht_protection,
923                         ht_conf->extension_chan_offset);
924         return;
925 }
926 EXPORT_SYMBOL(iwl_set_rxon_ht);
927
928 #define IWL_NUM_RX_CHAINS_MULTIPLE      3
929 #define IWL_NUM_RX_CHAINS_SINGLE        2
930 #define IWL_NUM_IDLE_CHAINS_DUAL        2
931 #define IWL_NUM_IDLE_CHAINS_SINGLE      1
932
933 /*
934  * Determine how many receiver/antenna chains to use.
935  *
936  * More provides better reception via diversity.  Fewer saves power
937  * at the expense of throughput, but only when not in powersave to
938  * start with.
939  *
940  * MIMO (dual stream) requires at least 2, but works better with 3.
941  * This does not determine *which* chains to use, just how many.
942  */
943 static int iwl_get_active_rx_chain_count(struct iwl_priv *priv)
944 {
945         /* # of Rx chains to use when expecting MIMO. */
946         if (is_single_rx_stream(priv))
947                 return IWL_NUM_RX_CHAINS_SINGLE;
948         else
949                 return IWL_NUM_RX_CHAINS_MULTIPLE;
950 }
951
952 /*
953  * When we are in power saving mode, unless device support spatial
954  * multiplexing power save, use the active count for rx chain count.
955  */
956 static int iwl_get_idle_rx_chain_count(struct iwl_priv *priv, int active_cnt)
957 {
958         /* # Rx chains when idling, depending on SMPS mode */
959         switch (priv->current_ht_config.smps) {
960         case IEEE80211_SMPS_STATIC:
961         case IEEE80211_SMPS_DYNAMIC:
962                 return IWL_NUM_IDLE_CHAINS_SINGLE;
963         case IEEE80211_SMPS_OFF:
964                 return active_cnt;
965         default:
966                 WARN(1, "invalid SMPS mode %d",
967                      priv->current_ht_config.smps);
968                 return active_cnt;
969         }
970 }
971
972 /* up to 4 chains */
973 static u8 iwl_count_chain_bitmap(u32 chain_bitmap)
974 {
975         u8 res;
976         res = (chain_bitmap & BIT(0)) >> 0;
977         res += (chain_bitmap & BIT(1)) >> 1;
978         res += (chain_bitmap & BIT(2)) >> 2;
979         res += (chain_bitmap & BIT(3)) >> 3;
980         return res;
981 }
982
983 /**
984  * iwl_is_monitor_mode - Determine if interface in monitor mode
985  *
986  * priv->iw_mode is set in add_interface, but add_interface is
987  * never called for monitor mode. The only way mac80211 informs us about
988  * monitor mode is through configuring filters (call to configure_filter).
989  */
990 bool iwl_is_monitor_mode(struct iwl_priv *priv)
991 {
992         return !!(priv->staging_rxon.filter_flags & RXON_FILTER_PROMISC_MSK);
993 }
994 EXPORT_SYMBOL(iwl_is_monitor_mode);
995
996 /**
997  * iwl_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
998  *
999  * Selects how many and which Rx receivers/antennas/chains to use.
1000  * This should not be used for scan command ... it puts data in wrong place.
1001  */
1002 void iwl_set_rxon_chain(struct iwl_priv *priv)
1003 {
1004         bool is_single = is_single_rx_stream(priv);
1005         bool is_cam = !test_bit(STATUS_POWER_PMI, &priv->status);
1006         u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
1007         u32 active_chains;
1008         u16 rx_chain;
1009
1010         /* Tell uCode which antennas are actually connected.
1011          * Before first association, we assume all antennas are connected.
1012          * Just after first association, iwl_chain_noise_calibration()
1013          *    checks which antennas actually *are* connected. */
1014          if (priv->chain_noise_data.active_chains)
1015                 active_chains = priv->chain_noise_data.active_chains;
1016         else
1017                 active_chains = priv->hw_params.valid_rx_ant;
1018
1019         rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
1020
1021         /* How many receivers should we use? */
1022         active_rx_cnt = iwl_get_active_rx_chain_count(priv);
1023         idle_rx_cnt = iwl_get_idle_rx_chain_count(priv, active_rx_cnt);
1024
1025
1026         /* correct rx chain count according hw settings
1027          * and chain noise calibration
1028          */
1029         valid_rx_cnt = iwl_count_chain_bitmap(active_chains);
1030         if (valid_rx_cnt < active_rx_cnt)
1031                 active_rx_cnt = valid_rx_cnt;
1032
1033         if (valid_rx_cnt < idle_rx_cnt)
1034                 idle_rx_cnt = valid_rx_cnt;
1035
1036         rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
1037         rx_chain |= idle_rx_cnt  << RXON_RX_CHAIN_CNT_POS;
1038
1039         /* copied from 'iwl_bg_request_scan()' */
1040         /* Force use of chains B and C (0x6) for Rx for 4965
1041          * Avoid A (0x1) because of its off-channel reception on A-band.
1042          * MIMO is not used here, but value is required */
1043         if (iwl_is_monitor_mode(priv) &&
1044             !(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) &&
1045             ((priv->hw_rev & CSR_HW_REV_TYPE_MSK) == CSR_HW_REV_TYPE_4965)) {
1046                 rx_chain = ANT_ABC << RXON_RX_CHAIN_VALID_POS;
1047                 rx_chain |= ANT_BC << RXON_RX_CHAIN_FORCE_SEL_POS;
1048                 rx_chain |= ANT_ABC << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
1049                 rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
1050         }
1051
1052         priv->staging_rxon.rx_chain = cpu_to_le16(rx_chain);
1053
1054         if (!is_single && (active_rx_cnt >= IWL_NUM_RX_CHAINS_SINGLE) && is_cam)
1055                 priv->staging_rxon.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
1056         else
1057                 priv->staging_rxon.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
1058
1059         IWL_DEBUG_ASSOC(priv, "rx_chain=0x%X active=%d idle=%d\n",
1060                         priv->staging_rxon.rx_chain,
1061                         active_rx_cnt, idle_rx_cnt);
1062
1063         WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
1064                 active_rx_cnt < idle_rx_cnt);
1065 }
1066 EXPORT_SYMBOL(iwl_set_rxon_chain);
1067
1068 /**
1069  * iwl_set_rxon_channel - Set the phymode and channel values in staging RXON
1070  * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
1071  * @channel: Any channel valid for the requested phymode
1072
1073  * In addition to setting the staging RXON, priv->phymode is also set.
1074  *
1075  * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
1076  * in the staging RXON flag structure based on the phymode
1077  */
1078 int iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch)
1079 {
1080         enum ieee80211_band band = ch->band;
1081         u16 channel = ieee80211_frequency_to_channel(ch->center_freq);
1082
1083         if (!iwl_get_channel_info(priv, band, channel)) {
1084                 IWL_DEBUG_INFO(priv, "Could not set channel to %d [%d]\n",
1085                                channel, band);
1086                 return -EINVAL;
1087         }
1088
1089         if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
1090             (priv->band == band))
1091                 return 0;
1092
1093         priv->staging_rxon.channel = cpu_to_le16(channel);
1094         if (band == IEEE80211_BAND_5GHZ)
1095                 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
1096         else
1097                 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1098
1099         priv->band = band;
1100
1101         IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
1102
1103         return 0;
1104 }
1105 EXPORT_SYMBOL(iwl_set_rxon_channel);
1106
1107 void iwl_set_flags_for_band(struct iwl_priv *priv,
1108                             enum ieee80211_band band)
1109 {
1110         if (band == IEEE80211_BAND_5GHZ) {
1111                 priv->staging_rxon.flags &=
1112                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
1113                       | RXON_FLG_CCK_MSK);
1114                 priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1115         } else {
1116                 /* Copied from iwl_post_associate() */
1117                 if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
1118                         priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
1119                 else
1120                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1121
1122                 if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
1123                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
1124
1125                 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
1126                 priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
1127                 priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
1128         }
1129 }
1130
1131 /*
1132  * initialize rxon structure with default values from eeprom
1133  */
1134 void iwl_connection_init_rx_config(struct iwl_priv *priv, int mode)
1135 {
1136         const struct iwl_channel_info *ch_info;
1137
1138         memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
1139
1140         switch (mode) {
1141         case NL80211_IFTYPE_AP:
1142                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
1143                 break;
1144
1145         case NL80211_IFTYPE_STATION:
1146                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
1147                 priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
1148                 break;
1149
1150         case NL80211_IFTYPE_ADHOC:
1151                 priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
1152                 priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
1153                 priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
1154                                                   RXON_FILTER_ACCEPT_GRP_MSK;
1155                 break;
1156
1157         default:
1158                 IWL_ERR(priv, "Unsupported interface type %d\n", mode);
1159                 break;
1160         }
1161
1162 #if 0
1163         /* TODO:  Figure out when short_preamble would be set and cache from
1164          * that */
1165         if (!hw_to_local(priv->hw)->short_preamble)
1166                 priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
1167         else
1168                 priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
1169 #endif
1170
1171         ch_info = iwl_get_channel_info(priv, priv->band,
1172                                        le16_to_cpu(priv->active_rxon.channel));
1173
1174         if (!ch_info)
1175                 ch_info = &priv->channel_info[0];
1176
1177         priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
1178         priv->band = ch_info->band;
1179
1180         iwl_set_flags_for_band(priv, priv->band);
1181
1182         priv->staging_rxon.ofdm_basic_rates =
1183             (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1184         priv->staging_rxon.cck_basic_rates =
1185             (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1186
1187         /* clear both MIX and PURE40 mode flag */
1188         priv->staging_rxon.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
1189                                         RXON_FLG_CHANNEL_MODE_PURE_40);
1190         memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
1191         memcpy(priv->staging_rxon.wlap_bssid_addr, priv->mac_addr, ETH_ALEN);
1192         priv->staging_rxon.ofdm_ht_single_stream_basic_rates = 0xff;
1193         priv->staging_rxon.ofdm_ht_dual_stream_basic_rates = 0xff;
1194         priv->staging_rxon.ofdm_ht_triple_stream_basic_rates = 0xff;
1195 }
1196 EXPORT_SYMBOL(iwl_connection_init_rx_config);
1197
1198 static void iwl_set_rate(struct iwl_priv *priv)
1199 {
1200         const struct ieee80211_supported_band *hw = NULL;
1201         struct ieee80211_rate *rate;
1202         int i;
1203
1204         hw = iwl_get_hw_mode(priv, priv->band);
1205         if (!hw) {
1206                 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
1207                 return;
1208         }
1209
1210         priv->active_rate = 0;
1211
1212         for (i = 0; i < hw->n_bitrates; i++) {
1213                 rate = &(hw->bitrates[i]);
1214                 if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
1215                         priv->active_rate |= (1 << rate->hw_value);
1216         }
1217
1218         IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
1219
1220         priv->staging_rxon.cck_basic_rates =
1221             (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
1222
1223         priv->staging_rxon.ofdm_basic_rates =
1224            (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
1225 }
1226
1227 void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
1228 {
1229         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1230         struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
1231         struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
1232
1233         if (priv->switch_rxon.switch_in_progress) {
1234                 if (!le32_to_cpu(csa->status) &&
1235                     (csa->channel == priv->switch_rxon.channel)) {
1236                         rxon->channel = csa->channel;
1237                         priv->staging_rxon.channel = csa->channel;
1238                         IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
1239                               le16_to_cpu(csa->channel));
1240                 } else
1241                         IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
1242                               le16_to_cpu(csa->channel));
1243
1244                 priv->switch_rxon.switch_in_progress = false;
1245         }
1246 }
1247 EXPORT_SYMBOL(iwl_rx_csa);
1248
1249 #ifdef CONFIG_IWLWIFI_DEBUG
1250 void iwl_print_rx_config_cmd(struct iwl_priv *priv)
1251 {
1252         struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
1253
1254         IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
1255         iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
1256         IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
1257         IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
1258         IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
1259                         le32_to_cpu(rxon->filter_flags));
1260         IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
1261         IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
1262                         rxon->ofdm_basic_rates);
1263         IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
1264         IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
1265         IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
1266         IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
1267 }
1268 EXPORT_SYMBOL(iwl_print_rx_config_cmd);
1269 #endif
1270 /**
1271  * iwl_irq_handle_error - called for HW or SW error interrupt from card
1272  */
1273 void iwl_irq_handle_error(struct iwl_priv *priv)
1274 {
1275         /* Set the FW error flag -- cleared on iwl_down */
1276         set_bit(STATUS_FW_ERROR, &priv->status);
1277
1278         /* Cancel currently queued command. */
1279         clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
1280
1281         priv->cfg->ops->lib->dump_nic_error_log(priv);
1282         if (priv->cfg->ops->lib->dump_csr)
1283                 priv->cfg->ops->lib->dump_csr(priv);
1284         if (priv->cfg->ops->lib->dump_fh)
1285                 priv->cfg->ops->lib->dump_fh(priv, NULL, false);
1286         priv->cfg->ops->lib->dump_nic_event_log(priv, false, NULL, false);
1287 #ifdef CONFIG_IWLWIFI_DEBUG
1288         if (iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS)
1289                 iwl_print_rx_config_cmd(priv);
1290 #endif
1291
1292         wake_up_interruptible(&priv->wait_command_queue);
1293
1294         /* Keep the restart process from trying to send host
1295          * commands by clearing the INIT status bit */
1296         clear_bit(STATUS_READY, &priv->status);
1297
1298         if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
1299                 IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
1300                           "Restarting adapter due to uCode error.\n");
1301
1302                 if (priv->cfg->mod_params->restart_fw)
1303                         queue_work(priv->workqueue, &priv->restart);
1304         }
1305 }
1306 EXPORT_SYMBOL(iwl_irq_handle_error);
1307
1308 static int iwl_apm_stop_master(struct iwl_priv *priv)
1309 {
1310         int ret = 0;
1311
1312         /* stop device's busmaster DMA activity */
1313         iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
1314
1315         ret = iwl_poll_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
1316                         CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
1317         if (ret)
1318                 IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
1319
1320         IWL_DEBUG_INFO(priv, "stop master\n");
1321
1322         return ret;
1323 }
1324
1325 void iwl_apm_stop(struct iwl_priv *priv)
1326 {
1327         IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
1328
1329         /* Stop device's DMA activity */
1330         iwl_apm_stop_master(priv);
1331
1332         /* Reset the entire device */
1333         iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
1334
1335         udelay(10);
1336
1337         /*
1338          * Clear "initialization complete" bit to move adapter from
1339          * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
1340          */
1341         iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1342 }
1343 EXPORT_SYMBOL(iwl_apm_stop);
1344
1345
1346 /*
1347  * Start up NIC's basic functionality after it has been reset
1348  * (e.g. after platform boot, or shutdown via iwl_apm_stop())
1349  * NOTE:  This does not load uCode nor start the embedded processor
1350  */
1351 int iwl_apm_init(struct iwl_priv *priv)
1352 {
1353         int ret = 0;
1354         u16 lctl;
1355
1356         IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
1357
1358         /*
1359          * Use "set_bit" below rather than "write", to preserve any hardware
1360          * bits already set by default after reset.
1361          */
1362
1363         /* Disable L0S exit timer (platform NMI Work/Around) */
1364         iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1365                           CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
1366
1367         /*
1368          * Disable L0s without affecting L1;
1369          *  don't wait for ICH L0s (ICH bug W/A)
1370          */
1371         iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1372                           CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
1373
1374         /* Set FH wait threshold to maximum (HW error during stress W/A) */
1375         iwl_set_bit(priv, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
1376
1377         /*
1378          * Enable HAP INTA (interrupt from management bus) to
1379          * wake device's PCI Express link L1a -> L0s
1380          * NOTE:  This is no-op for 3945 (non-existant bit)
1381          */
1382         iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
1383                                     CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
1384
1385         /*
1386          * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
1387          * Check if BIOS (or OS) enabled L1-ASPM on this device.
1388          * If so (likely), disable L0S, so device moves directly L0->L1;
1389          *    costs negligible amount of power savings.
1390          * If not (unlikely), enable L0S, so there is at least some
1391          *    power savings, even without L1.
1392          */
1393         if (priv->cfg->set_l0s) {
1394                 lctl = iwl_pcie_link_ctl(priv);
1395                 if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
1396                                         PCI_CFG_LINK_CTRL_VAL_L1_EN) {
1397                         /* L1-ASPM enabled; disable(!) L0S  */
1398                         iwl_set_bit(priv, CSR_GIO_REG,
1399                                         CSR_GIO_REG_VAL_L0S_ENABLED);
1400                         IWL_DEBUG_POWER(priv, "L1 Enabled; Disabling L0S\n");
1401                 } else {
1402                         /* L1-ASPM disabled; enable(!) L0S */
1403                         iwl_clear_bit(priv, CSR_GIO_REG,
1404                                         CSR_GIO_REG_VAL_L0S_ENABLED);
1405                         IWL_DEBUG_POWER(priv, "L1 Disabled; Enabling L0S\n");
1406                 }
1407         }
1408
1409         /* Configure analog phase-lock-loop before activating to D0A */
1410         if (priv->cfg->pll_cfg_val)
1411                 iwl_set_bit(priv, CSR_ANA_PLL_CFG, priv->cfg->pll_cfg_val);
1412
1413         /*
1414          * Set "initialization complete" bit to move adapter from
1415          * D0U* --> D0A* (powered-up active) state.
1416          */
1417         iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1418
1419         /*
1420          * Wait for clock stabilization; once stabilized, access to
1421          * device-internal resources is supported, e.g. iwl_write_prph()
1422          * and accesses to uCode SRAM.
1423          */
1424         ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
1425                         CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
1426                         CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
1427         if (ret < 0) {
1428                 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
1429                 goto out;
1430         }
1431
1432         /*
1433          * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
1434          * BSM (Boostrap State Machine) is only in 3945 and 4965;
1435          * later devices (i.e. 5000 and later) have non-volatile SRAM,
1436          * and don't need BSM to restore data after power-saving sleep.
1437          *
1438          * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
1439          * do not disable clocks.  This preserves any hardware bits already
1440          * set by default in "CLK_CTRL_REG" after reset.
1441          */
1442         if (priv->cfg->use_bsm)
1443                 iwl_write_prph(priv, APMG_CLK_EN_REG,
1444                         APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
1445         else
1446                 iwl_write_prph(priv, APMG_CLK_EN_REG,
1447                         APMG_CLK_VAL_DMA_CLK_RQT);
1448         udelay(20);
1449
1450         /* Disable L1-Active */
1451         iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
1452                           APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
1453
1454 out:
1455         return ret;
1456 }
1457 EXPORT_SYMBOL(iwl_apm_init);
1458
1459
1460
1461 void iwl_configure_filter(struct ieee80211_hw *hw,
1462                           unsigned int changed_flags,
1463                           unsigned int *total_flags,
1464                           u64 multicast)
1465 {
1466         struct iwl_priv *priv = hw->priv;
1467         __le32 *filter_flags = &priv->staging_rxon.filter_flags;
1468
1469         IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
1470                         changed_flags, *total_flags);
1471
1472         if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
1473                 if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
1474                         *filter_flags |= RXON_FILTER_PROMISC_MSK;
1475                 else
1476                         *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
1477         }
1478         if (changed_flags & FIF_ALLMULTI) {
1479                 if (*total_flags & FIF_ALLMULTI)
1480                         *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
1481                 else
1482                         *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
1483         }
1484         if (changed_flags & FIF_CONTROL) {
1485                 if (*total_flags & FIF_CONTROL)
1486                         *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
1487                 else
1488                         *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
1489         }
1490         if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
1491                 if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
1492                         *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
1493                 else
1494                         *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
1495         }
1496
1497         /* We avoid iwl_commit_rxon here to commit the new filter flags
1498          * since mac80211 will call ieee80211_hw_config immediately.
1499          * (mc_list is not supported at this time). Otherwise, we need to
1500          * queue a background iwl_commit_rxon work.
1501          */
1502
1503         *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
1504                         FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
1505 }
1506 EXPORT_SYMBOL(iwl_configure_filter);
1507
1508 int iwl_set_hw_params(struct iwl_priv *priv)
1509 {
1510         priv->hw_params.max_rxq_size = RX_QUEUE_SIZE;
1511         priv->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
1512         if (priv->cfg->mod_params->amsdu_size_8K)
1513                 priv->hw_params.rx_page_order = get_order(IWL_RX_BUF_SIZE_8K);
1514         else
1515                 priv->hw_params.rx_page_order = get_order(IWL_RX_BUF_SIZE_4K);
1516
1517         priv->hw_params.max_beacon_itrvl = IWL_MAX_UCODE_BEACON_INTERVAL;
1518
1519         if (priv->cfg->mod_params->disable_11n)
1520                 priv->cfg->sku &= ~IWL_SKU_N;
1521
1522         /* Device-specific setup */
1523         return priv->cfg->ops->lib->set_hw_params(priv);
1524 }
1525 EXPORT_SYMBOL(iwl_set_hw_params);
1526
1527 int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
1528 {
1529         int ret = 0;
1530         s8 prev_tx_power = priv->tx_power_user_lmt;
1531
1532         if (tx_power < IWL_TX_POWER_TARGET_POWER_MIN) {
1533                 IWL_WARN(priv, "Requested user TXPOWER %d below lower limit %d.\n",
1534                          tx_power,
1535                          IWL_TX_POWER_TARGET_POWER_MIN);
1536                 return -EINVAL;
1537         }
1538
1539         if (tx_power > priv->tx_power_device_lmt) {
1540                 IWL_WARN(priv,
1541                         "Requested user TXPOWER %d above upper limit %d.\n",
1542                          tx_power, priv->tx_power_device_lmt);
1543                 return -EINVAL;
1544         }
1545
1546         if (priv->tx_power_user_lmt != tx_power)
1547                 force = true;
1548
1549         /* if nic is not up don't send command */
1550         if (iwl_is_ready_rf(priv)) {
1551                 priv->tx_power_user_lmt = tx_power;
1552                 if (force && priv->cfg->ops->lib->send_tx_power)
1553                         ret = priv->cfg->ops->lib->send_tx_power(priv);
1554                 else if (!priv->cfg->ops->lib->send_tx_power)
1555                         ret = -EOPNOTSUPP;
1556                 /*
1557                  * if fail to set tx_power, restore the orig. tx power
1558                  */
1559                 if (ret)
1560                         priv->tx_power_user_lmt = prev_tx_power;
1561         }
1562
1563         /*
1564          * Even this is an async host command, the command
1565          * will always report success from uCode
1566          * So once driver can placing the command into the queue
1567          * successfully, driver can use priv->tx_power_user_lmt
1568          * to reflect the current tx power
1569          */
1570         return ret;
1571 }
1572 EXPORT_SYMBOL(iwl_set_tx_power);
1573
1574 irqreturn_t iwl_isr_legacy(int irq, void *data)
1575 {
1576         struct iwl_priv *priv = data;
1577         u32 inta, inta_mask;
1578         u32 inta_fh;
1579         if (!priv)
1580                 return IRQ_NONE;
1581
1582         spin_lock(&priv->lock);
1583
1584         /* Disable (but don't clear!) interrupts here to avoid
1585          *    back-to-back ISRs and sporadic interrupts from our NIC.
1586          * If we have something to service, the tasklet will re-enable ints.
1587          * If we *don't* have something, we'll re-enable before leaving here. */
1588         inta_mask = iwl_read32(priv, CSR_INT_MASK);  /* just for debug */
1589         iwl_write32(priv, CSR_INT_MASK, 0x00000000);
1590
1591         /* Discover which interrupts are active/pending */
1592         inta = iwl_read32(priv, CSR_INT);
1593         inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
1594
1595         /* Ignore interrupt if there's nothing in NIC to service.
1596          * This may be due to IRQ shared with another device,
1597          * or due to sporadic interrupts thrown from our NIC. */
1598         if (!inta && !inta_fh) {
1599                 IWL_DEBUG_ISR(priv, "Ignore interrupt, inta == 0, inta_fh == 0\n");
1600                 goto none;
1601         }
1602
1603         if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
1604                 /* Hardware disappeared. It might have already raised
1605                  * an interrupt */
1606                 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
1607                 goto unplugged;
1608         }
1609
1610         IWL_DEBUG_ISR(priv, "ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
1611                       inta, inta_mask, inta_fh);
1612
1613         inta &= ~CSR_INT_BIT_SCD;
1614
1615         /* iwl_irq_tasklet() will service interrupts and re-enable them */
1616         if (likely(inta || inta_fh))
1617                 tasklet_schedule(&priv->irq_tasklet);
1618
1619  unplugged:
1620         spin_unlock(&priv->lock);
1621         return IRQ_HANDLED;
1622
1623  none:
1624         /* re-enable interrupts here since we don't have anything to service. */
1625         /* only Re-enable if diabled by irq */
1626         if (test_bit(STATUS_INT_ENABLED, &priv->status))
1627                 iwl_enable_interrupts(priv);
1628         spin_unlock(&priv->lock);
1629         return IRQ_NONE;
1630 }
1631 EXPORT_SYMBOL(iwl_isr_legacy);
1632
1633 int iwl_send_bt_config(struct iwl_priv *priv)
1634 {
1635         struct iwl_bt_cmd bt_cmd = {
1636                 .lead_time = BT_LEAD_TIME_DEF,
1637                 .max_kill = BT_MAX_KILL_DEF,
1638                 .kill_ack_mask = 0,
1639                 .kill_cts_mask = 0,
1640         };
1641
1642         if (!bt_coex_active)
1643                 bt_cmd.flags = BT_COEX_DISABLE;
1644         else
1645                 bt_cmd.flags = BT_COEX_ENABLE;
1646
1647         IWL_DEBUG_INFO(priv, "BT coex %s\n",
1648                 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
1649
1650         return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1651                                 sizeof(struct iwl_bt_cmd), &bt_cmd);
1652 }
1653 EXPORT_SYMBOL(iwl_send_bt_config);
1654
1655 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
1656 {
1657         struct iwl_statistics_cmd statistics_cmd = {
1658                 .configuration_flags =
1659                         clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
1660         };
1661
1662         if (flags & CMD_ASYNC)
1663                 return iwl_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
1664                                                sizeof(struct iwl_statistics_cmd),
1665                                                &statistics_cmd, NULL);
1666         else
1667                 return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
1668                                         sizeof(struct iwl_statistics_cmd),
1669                                         &statistics_cmd);
1670 }
1671 EXPORT_SYMBOL(iwl_send_statistics_request);
1672
1673 /**
1674  * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
1675  *   using sample data 100 bytes apart.  If these sample points are good,
1676  *   it's a pretty good bet that everything between them is good, too.
1677  */
1678 static int iwlcore_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
1679 {
1680         u32 val;
1681         int ret = 0;
1682         u32 errcnt = 0;
1683         u32 i;
1684
1685         IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
1686
1687         for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
1688                 /* read data comes through single port, auto-incr addr */
1689                 /* NOTE: Use the debugless read so we don't flood kernel log
1690                  * if IWL_DL_IO is set */
1691                 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
1692                         i + IWL49_RTC_INST_LOWER_BOUND);
1693                 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1694                 if (val != le32_to_cpu(*image)) {
1695                         ret = -EIO;
1696                         errcnt++;
1697                         if (errcnt >= 3)
1698                                 break;
1699                 }
1700         }
1701
1702         return ret;
1703 }
1704
1705 /**
1706  * iwlcore_verify_inst_full - verify runtime uCode image in card vs. host,
1707  *     looking at all data.
1708  */
1709 static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 *image,
1710                                  u32 len)
1711 {
1712         u32 val;
1713         u32 save_len = len;
1714         int ret = 0;
1715         u32 errcnt;
1716
1717         IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
1718
1719         iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
1720                            IWL49_RTC_INST_LOWER_BOUND);
1721
1722         errcnt = 0;
1723         for (; len > 0; len -= sizeof(u32), image++) {
1724                 /* read data comes through single port, auto-incr addr */
1725                 /* NOTE: Use the debugless read so we don't flood kernel log
1726                  * if IWL_DL_IO is set */
1727                 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
1728                 if (val != le32_to_cpu(*image)) {
1729                         IWL_ERR(priv, "uCode INST section is invalid at "
1730                                   "offset 0x%x, is 0x%x, s/b 0x%x\n",
1731                                   save_len - len, val, le32_to_cpu(*image));
1732                         ret = -EIO;
1733                         errcnt++;
1734                         if (errcnt >= 20)
1735                                 break;
1736                 }
1737         }
1738
1739         if (!errcnt)
1740                 IWL_DEBUG_INFO(priv,
1741                     "ucode image in INSTRUCTION memory is good\n");
1742
1743         return ret;
1744 }
1745
1746 /**
1747  * iwl_verify_ucode - determine which instruction image is in SRAM,
1748  *    and verify its contents
1749  */
1750 int iwl_verify_ucode(struct iwl_priv *priv)
1751 {
1752         __le32 *image;
1753         u32 len;
1754         int ret;
1755
1756         /* Try bootstrap */
1757         image = (__le32 *)priv->ucode_boot.v_addr;
1758         len = priv->ucode_boot.len;
1759         ret = iwlcore_verify_inst_sparse(priv, image, len);
1760         if (!ret) {
1761                 IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
1762                 return 0;
1763         }
1764
1765         /* Try initialize */
1766         image = (__le32 *)priv->ucode_init.v_addr;
1767         len = priv->ucode_init.len;
1768         ret = iwlcore_verify_inst_sparse(priv, image, len);
1769         if (!ret) {
1770                 IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
1771                 return 0;
1772         }
1773
1774         /* Try runtime/protocol */
1775         image = (__le32 *)priv->ucode_code.v_addr;
1776         len = priv->ucode_code.len;
1777         ret = iwlcore_verify_inst_sparse(priv, image, len);
1778         if (!ret) {
1779                 IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
1780                 return 0;
1781         }
1782
1783         IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
1784
1785         /* Since nothing seems to match, show first several data entries in
1786          * instruction SRAM, so maybe visual inspection will give a clue.
1787          * Selection of bootstrap image (vs. other images) is arbitrary. */
1788         image = (__le32 *)priv->ucode_boot.v_addr;
1789         len = priv->ucode_boot.len;
1790         ret = iwl_verify_inst_full(priv, image, len);
1791
1792         return ret;
1793 }
1794 EXPORT_SYMBOL(iwl_verify_ucode);
1795
1796
1797 void iwl_rf_kill_ct_config(struct iwl_priv *priv)
1798 {
1799         struct iwl_ct_kill_config cmd;
1800         struct iwl_ct_kill_throttling_config adv_cmd;
1801         unsigned long flags;
1802         int ret = 0;
1803
1804         spin_lock_irqsave(&priv->lock, flags);
1805         iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
1806                     CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
1807         spin_unlock_irqrestore(&priv->lock, flags);
1808         priv->thermal_throttle.ct_kill_toggle = false;
1809
1810         if (priv->cfg->support_ct_kill_exit) {
1811                 adv_cmd.critical_temperature_enter =
1812                         cpu_to_le32(priv->hw_params.ct_kill_threshold);
1813                 adv_cmd.critical_temperature_exit =
1814                         cpu_to_le32(priv->hw_params.ct_kill_exit_threshold);
1815
1816                 ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
1817                                        sizeof(adv_cmd), &adv_cmd);
1818                 if (ret)
1819                         IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
1820                 else
1821                         IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
1822                                         "succeeded, "
1823                                         "critical temperature enter is %d,"
1824                                         "exit is %d\n",
1825                                        priv->hw_params.ct_kill_threshold,
1826                                        priv->hw_params.ct_kill_exit_threshold);
1827         } else {
1828                 cmd.critical_temperature_R =
1829                         cpu_to_le32(priv->hw_params.ct_kill_threshold);
1830
1831                 ret = iwl_send_cmd_pdu(priv, REPLY_CT_KILL_CONFIG_CMD,
1832                                        sizeof(cmd), &cmd);
1833                 if (ret)
1834                         IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
1835                 else
1836                         IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
1837                                         "succeeded, "
1838                                         "critical temperature is %d\n",
1839                                         priv->hw_params.ct_kill_threshold);
1840         }
1841 }
1842 EXPORT_SYMBOL(iwl_rf_kill_ct_config);
1843
1844
1845 /*
1846  * CARD_STATE_CMD
1847  *
1848  * Use: Sets the device's internal card state to enable, disable, or halt
1849  *
1850  * When in the 'enable' state the card operates as normal.
1851  * When in the 'disable' state, the card enters into a low power mode.
1852  * When in the 'halt' state, the card is shut down and must be fully
1853  * restarted to come back on.
1854  */
1855 int iwl_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
1856 {
1857         struct iwl_host_cmd cmd = {
1858                 .id = REPLY_CARD_STATE_CMD,
1859                 .len = sizeof(u32),
1860                 .data = &flags,
1861                 .flags = meta_flag,
1862         };
1863
1864         return iwl_send_cmd(priv, &cmd);
1865 }
1866
1867 void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
1868                            struct iwl_rx_mem_buffer *rxb)
1869 {
1870 #ifdef CONFIG_IWLWIFI_DEBUG
1871         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1872         struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
1873         IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
1874                      sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1875 #endif
1876 }
1877 EXPORT_SYMBOL(iwl_rx_pm_sleep_notif);
1878
1879 void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1880                                       struct iwl_rx_mem_buffer *rxb)
1881 {
1882         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1883         u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1884         IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
1885                         "notification for %s:\n", len,
1886                         get_cmd_string(pkt->hdr.cmd));
1887         iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
1888 }
1889 EXPORT_SYMBOL(iwl_rx_pm_debug_statistics_notif);
1890
1891 void iwl_rx_reply_error(struct iwl_priv *priv,
1892                         struct iwl_rx_mem_buffer *rxb)
1893 {
1894         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1895
1896         IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
1897                 "seq 0x%04X ser 0x%08X\n",
1898                 le32_to_cpu(pkt->u.err_resp.error_type),
1899                 get_cmd_string(pkt->u.err_resp.cmd_id),
1900                 pkt->u.err_resp.cmd_id,
1901                 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1902                 le32_to_cpu(pkt->u.err_resp.error_info));
1903 }
1904 EXPORT_SYMBOL(iwl_rx_reply_error);
1905
1906 void iwl_clear_isr_stats(struct iwl_priv *priv)
1907 {
1908         memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
1909 }
1910
1911 int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
1912                            const struct ieee80211_tx_queue_params *params)
1913 {
1914         struct iwl_priv *priv = hw->priv;
1915         unsigned long flags;
1916         int q;
1917
1918         IWL_DEBUG_MAC80211(priv, "enter\n");
1919
1920         if (!iwl_is_ready_rf(priv)) {
1921                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1922                 return -EIO;
1923         }
1924
1925         if (queue >= AC_NUM) {
1926                 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1927                 return 0;
1928         }
1929
1930         q = AC_NUM - 1 - queue;
1931
1932         spin_lock_irqsave(&priv->lock, flags);
1933
1934         priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
1935         priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
1936         priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1937         priv->qos_data.def_qos_parm.ac[q].edca_txop =
1938                         cpu_to_le16((params->txop * 32));
1939
1940         priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1941         priv->qos_data.qos_active = 1;
1942
1943         if (priv->iw_mode == NL80211_IFTYPE_AP)
1944                 iwl_activate_qos(priv, 1);
1945         else if (priv->assoc_id && iwl_is_associated(priv))
1946                 iwl_activate_qos(priv, 0);
1947
1948         spin_unlock_irqrestore(&priv->lock, flags);
1949
1950         IWL_DEBUG_MAC80211(priv, "leave\n");
1951         return 0;
1952 }
1953 EXPORT_SYMBOL(iwl_mac_conf_tx);
1954
1955 static void iwl_ht_conf(struct iwl_priv *priv,
1956                         struct ieee80211_bss_conf *bss_conf)
1957 {
1958         struct iwl_ht_config *ht_conf = &priv->current_ht_config;
1959         struct ieee80211_sta *sta;
1960
1961         IWL_DEBUG_MAC80211(priv, "enter: \n");
1962
1963         if (!ht_conf->is_ht)
1964                 return;
1965
1966         ht_conf->ht_protection =
1967                 bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_PROTECTION;
1968         ht_conf->non_GF_STA_present =
1969                 !!(bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
1970
1971         ht_conf->single_chain_sufficient = false;
1972
1973         switch (priv->iw_mode) {
1974         case NL80211_IFTYPE_STATION:
1975                 rcu_read_lock();
1976                 sta = ieee80211_find_sta(priv->vif, priv->bssid);
1977                 if (sta) {
1978                         struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
1979                         int maxstreams;
1980
1981                         maxstreams = (ht_cap->mcs.tx_params &
1982                                       IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
1983                                         >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
1984                         maxstreams += 1;
1985
1986                         if ((ht_cap->mcs.rx_mask[1] == 0) &&
1987                             (ht_cap->mcs.rx_mask[2] == 0))
1988                                 ht_conf->single_chain_sufficient = true;
1989                         if (maxstreams <= 1)
1990                                 ht_conf->single_chain_sufficient = true;
1991                 } else {
1992                         /*
1993                          * If at all, this can only happen through a race
1994                          * when the AP disconnects us while we're still
1995                          * setting up the connection, in that case mac80211
1996                          * will soon tell us about that.
1997                          */
1998                         ht_conf->single_chain_sufficient = true;
1999                 }
2000                 rcu_read_unlock();
2001                 break;
2002         case NL80211_IFTYPE_ADHOC:
2003                 ht_conf->single_chain_sufficient = true;
2004                 break;
2005         default:
2006                 break;
2007         }
2008
2009         IWL_DEBUG_MAC80211(priv, "leave\n");
2010 }
2011
2012 static inline void iwl_set_no_assoc(struct iwl_priv *priv)
2013 {
2014         priv->assoc_id = 0;
2015         iwl_led_disassociate(priv);
2016         /*
2017          * inform the ucode that there is no longer an
2018          * association and that no more packets should be
2019          * sent
2020          */
2021         priv->staging_rxon.filter_flags &=
2022                 ~RXON_FILTER_ASSOC_MSK;
2023         priv->staging_rxon.assoc_id = 0;
2024         iwlcore_commit_rxon(priv);
2025 }
2026
2027 #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
2028 void iwl_bss_info_changed(struct ieee80211_hw *hw,
2029                           struct ieee80211_vif *vif,
2030                           struct ieee80211_bss_conf *bss_conf,
2031                           u32 changes)
2032 {
2033         struct iwl_priv *priv = hw->priv;
2034         int ret;
2035
2036         IWL_DEBUG_MAC80211(priv, "changes = 0x%X\n", changes);
2037
2038         if (!iwl_is_alive(priv))
2039                 return;
2040
2041         mutex_lock(&priv->mutex);
2042
2043         if (changes & BSS_CHANGED_BEACON &&
2044             priv->iw_mode == NL80211_IFTYPE_AP) {
2045                 dev_kfree_skb(priv->ibss_beacon);
2046                 priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
2047         }
2048
2049         if (changes & BSS_CHANGED_BEACON_INT) {
2050                 priv->beacon_int = bss_conf->beacon_int;
2051                 /* TODO: in AP mode, do something to make this take effect */
2052         }
2053
2054         if (changes & BSS_CHANGED_BSSID) {
2055                 IWL_DEBUG_MAC80211(priv, "BSSID %pM\n", bss_conf->bssid);
2056
2057                 /*
2058                  * If there is currently a HW scan going on in the
2059                  * background then we need to cancel it else the RXON
2060                  * below/in post_associate will fail.
2061                  */
2062                 if (iwl_scan_cancel_timeout(priv, 100)) {
2063                         IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
2064                         IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
2065                         mutex_unlock(&priv->mutex);
2066                         return;
2067                 }
2068
2069                 /* mac80211 only sets assoc when in STATION mode */
2070                 if (priv->iw_mode == NL80211_IFTYPE_ADHOC ||
2071                     bss_conf->assoc) {
2072                         memcpy(priv->staging_rxon.bssid_addr,
2073                                bss_conf->bssid, ETH_ALEN);
2074
2075                         /* currently needed in a few places */
2076                         memcpy(priv->bssid, bss_conf->bssid, ETH_ALEN);
2077                 } else {
2078                         priv->staging_rxon.filter_flags &=
2079                                 ~RXON_FILTER_ASSOC_MSK;
2080                 }
2081
2082         }
2083
2084         /*
2085          * This needs to be after setting the BSSID in case
2086          * mac80211 decides to do both changes at once because
2087          * it will invoke post_associate.
2088          */
2089         if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
2090             changes & BSS_CHANGED_BEACON) {
2091                 struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
2092
2093                 if (beacon)
2094                         iwl_mac_beacon_update(hw, beacon);
2095         }
2096
2097         if (changes & BSS_CHANGED_ERP_PREAMBLE) {
2098                 IWL_DEBUG_MAC80211(priv, "ERP_PREAMBLE %d\n",
2099                                    bss_conf->use_short_preamble);
2100                 if (bss_conf->use_short_preamble)
2101                         priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2102                 else
2103                         priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2104         }
2105
2106         if (changes & BSS_CHANGED_ERP_CTS_PROT) {
2107                 IWL_DEBUG_MAC80211(priv, "ERP_CTS %d\n", bss_conf->use_cts_prot);
2108                 if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
2109                         priv->staging_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
2110                 else
2111                         priv->staging_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
2112         }
2113
2114         if (changes & BSS_CHANGED_BASIC_RATES) {
2115                 /* XXX use this information
2116                  *
2117                  * To do that, remove code from iwl_set_rate() and put something
2118                  * like this here:
2119                  *
2120                 if (A-band)
2121                         priv->staging_rxon.ofdm_basic_rates =
2122                                 bss_conf->basic_rates;
2123                 else
2124                         priv->staging_rxon.ofdm_basic_rates =
2125                                 bss_conf->basic_rates >> 4;
2126                         priv->staging_rxon.cck_basic_rates =
2127                                 bss_conf->basic_rates & 0xF;
2128                  */
2129         }
2130
2131         if (changes & BSS_CHANGED_HT) {
2132                 iwl_ht_conf(priv, bss_conf);
2133
2134                 if (priv->cfg->ops->hcmd->set_rxon_chain)
2135                         priv->cfg->ops->hcmd->set_rxon_chain(priv);
2136         }
2137
2138         if (changes & BSS_CHANGED_ASSOC) {
2139                 IWL_DEBUG_MAC80211(priv, "ASSOC %d\n", bss_conf->assoc);
2140                 if (bss_conf->assoc) {
2141                         priv->assoc_id = bss_conf->aid;
2142                         priv->beacon_int = bss_conf->beacon_int;
2143                         priv->timestamp = bss_conf->timestamp;
2144                         priv->assoc_capability = bss_conf->assoc_capability;
2145
2146                         iwl_led_associate(priv);
2147
2148                         /*
2149                          * We have just associated, don't start scan too early
2150                          * leave time for EAPOL exchange to complete.
2151                          *
2152                          * XXX: do this in mac80211
2153                          */
2154                         priv->next_scan_jiffies = jiffies +
2155                                         IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
2156                         if (!iwl_is_rfkill(priv))
2157                                 priv->cfg->ops->lib->post_associate(priv);
2158                 } else
2159                         iwl_set_no_assoc(priv);
2160         }
2161
2162         if (changes && iwl_is_associated(priv) && priv->assoc_id) {
2163                 IWL_DEBUG_MAC80211(priv, "Changes (%#x) while associated\n",
2164                                    changes);
2165                 ret = iwl_send_rxon_assoc(priv);
2166                 if (!ret) {
2167                         /* Sync active_rxon with latest change. */
2168                         memcpy((void *)&priv->active_rxon,
2169                                 &priv->staging_rxon,
2170                                 sizeof(struct iwl_rxon_cmd));
2171                 }
2172         }
2173
2174         if (changes & BSS_CHANGED_BEACON_ENABLED) {
2175                 if (vif->bss_conf.enable_beacon) {
2176                         memcpy(priv->staging_rxon.bssid_addr,
2177                                bss_conf->bssid, ETH_ALEN);
2178                         memcpy(priv->bssid, bss_conf->bssid, ETH_ALEN);
2179                         iwlcore_config_ap(priv);
2180                 } else
2181                         iwl_set_no_assoc(priv);
2182         }
2183
2184         mutex_unlock(&priv->mutex);
2185
2186         IWL_DEBUG_MAC80211(priv, "leave\n");
2187 }
2188 EXPORT_SYMBOL(iwl_bss_info_changed);
2189
2190 int iwl_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
2191 {
2192         struct iwl_priv *priv = hw->priv;
2193         unsigned long flags;
2194         __le64 timestamp;
2195
2196         IWL_DEBUG_MAC80211(priv, "enter\n");
2197
2198         if (!iwl_is_ready_rf(priv)) {
2199                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
2200                 return -EIO;
2201         }
2202
2203         spin_lock_irqsave(&priv->lock, flags);
2204
2205         if (priv->ibss_beacon)
2206                 dev_kfree_skb(priv->ibss_beacon);
2207
2208         priv->ibss_beacon = skb;
2209
2210         priv->assoc_id = 0;
2211         timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
2212         priv->timestamp = le64_to_cpu(timestamp);
2213
2214         IWL_DEBUG_MAC80211(priv, "leave\n");
2215         spin_unlock_irqrestore(&priv->lock, flags);
2216
2217         iwl_reset_qos(priv);
2218
2219         priv->cfg->ops->lib->post_associate(priv);
2220
2221
2222         return 0;
2223 }
2224 EXPORT_SYMBOL(iwl_mac_beacon_update);
2225
2226 static int iwl_set_mode(struct iwl_priv *priv, struct ieee80211_vif *vif)
2227 {
2228         iwl_connection_init_rx_config(priv, vif->type);
2229
2230         if (priv->cfg->ops->hcmd->set_rxon_chain)
2231                 priv->cfg->ops->hcmd->set_rxon_chain(priv);
2232
2233         memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
2234
2235         return iwlcore_commit_rxon(priv);
2236 }
2237
2238 int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2239 {
2240         struct iwl_priv *priv = hw->priv;
2241         int err = 0;
2242
2243         IWL_DEBUG_MAC80211(priv, "enter: type %d\n", vif->type);
2244
2245         mutex_lock(&priv->mutex);
2246
2247         if (WARN_ON(!iwl_is_ready_rf(priv))) {
2248                 err = -EINVAL;
2249                 goto out;
2250         }
2251
2252         if (priv->vif) {
2253                 IWL_DEBUG_MAC80211(priv, "leave - vif != NULL\n");
2254                 err = -EOPNOTSUPP;
2255                 goto out;
2256         }
2257
2258         priv->vif = vif;
2259         priv->iw_mode = vif->type;
2260
2261         IWL_DEBUG_MAC80211(priv, "Set %pM\n", vif->addr);
2262         memcpy(priv->mac_addr, vif->addr, ETH_ALEN);
2263
2264         err = iwl_set_mode(priv, vif);
2265         if (err)
2266                 goto out_err;
2267
2268         /* Add the broadcast address so we can send broadcast frames */
2269         priv->cfg->ops->lib->add_bcast_station(priv);
2270
2271         goto out;
2272
2273  out_err:
2274         priv->vif = NULL;
2275         priv->iw_mode = NL80211_IFTYPE_STATION;
2276  out:
2277         mutex_unlock(&priv->mutex);
2278
2279         IWL_DEBUG_MAC80211(priv, "leave\n");
2280         return err;
2281 }
2282 EXPORT_SYMBOL(iwl_mac_add_interface);
2283
2284 void iwl_mac_remove_interface(struct ieee80211_hw *hw,
2285                               struct ieee80211_vif *vif)
2286 {
2287         struct iwl_priv *priv = hw->priv;
2288
2289         IWL_DEBUG_MAC80211(priv, "enter\n");
2290
2291         mutex_lock(&priv->mutex);
2292
2293         iwl_clear_ucode_stations(priv, true);
2294
2295         if (iwl_is_ready_rf(priv)) {
2296                 iwl_scan_cancel_timeout(priv, 100);
2297                 priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2298                 iwlcore_commit_rxon(priv);
2299         }
2300         if (priv->vif == vif) {
2301                 priv->vif = NULL;
2302                 memset(priv->bssid, 0, ETH_ALEN);
2303         }
2304         mutex_unlock(&priv->mutex);
2305
2306         IWL_DEBUG_MAC80211(priv, "leave\n");
2307
2308 }
2309 EXPORT_SYMBOL(iwl_mac_remove_interface);
2310
2311 /**
2312  * iwl_mac_config - mac80211 config callback
2313  *
2314  * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
2315  * be set inappropriately and the driver currently sets the hardware up to
2316  * use it whenever needed.
2317  */
2318 int iwl_mac_config(struct ieee80211_hw *hw, u32 changed)
2319 {
2320         struct iwl_priv *priv = hw->priv;
2321         const struct iwl_channel_info *ch_info;
2322         struct ieee80211_conf *conf = &hw->conf;
2323         struct iwl_ht_config *ht_conf = &priv->current_ht_config;
2324         unsigned long flags = 0;
2325         int ret = 0;
2326         u16 ch;
2327         int scan_active = 0;
2328
2329         mutex_lock(&priv->mutex);
2330
2331         IWL_DEBUG_MAC80211(priv, "enter to channel %d changed 0x%X\n",
2332                                         conf->channel->hw_value, changed);
2333
2334         if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
2335                         test_bit(STATUS_SCANNING, &priv->status))) {
2336                 scan_active = 1;
2337                 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
2338         }
2339
2340         if (changed & (IEEE80211_CONF_CHANGE_SMPS |
2341                        IEEE80211_CONF_CHANGE_CHANNEL)) {
2342                 /* mac80211 uses static for non-HT which is what we want */
2343                 priv->current_ht_config.smps = conf->smps_mode;
2344
2345                 /*
2346                  * Recalculate chain counts.
2347                  *
2348                  * If monitor mode is enabled then mac80211 will
2349                  * set up the SM PS mode to OFF if an HT channel is
2350                  * configured.
2351                  */
2352                 if (priv->cfg->ops->hcmd->set_rxon_chain)
2353                         priv->cfg->ops->hcmd->set_rxon_chain(priv);
2354         }
2355
2356         /* during scanning mac80211 will delay channel setting until
2357          * scan finish with changed = 0
2358          */
2359         if (!changed || (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
2360                 if (scan_active)
2361                         goto set_ch_out;
2362
2363                 ch = ieee80211_frequency_to_channel(conf->channel->center_freq);
2364                 ch_info = iwl_get_channel_info(priv, conf->channel->band, ch);
2365                 if (!is_channel_valid(ch_info)) {
2366                         IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
2367                         ret = -EINVAL;
2368                         goto set_ch_out;
2369                 }
2370
2371                 spin_lock_irqsave(&priv->lock, flags);
2372
2373                 /* Configure HT40 channels */
2374                 ht_conf->is_ht = conf_is_ht(conf);
2375                 if (ht_conf->is_ht) {
2376                         if (conf_is_ht40_minus(conf)) {
2377                                 ht_conf->extension_chan_offset =
2378                                         IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2379                                 ht_conf->is_40mhz = true;
2380                         } else if (conf_is_ht40_plus(conf)) {
2381                                 ht_conf->extension_chan_offset =
2382                                         IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2383                                 ht_conf->is_40mhz = true;
2384                         } else {
2385                                 ht_conf->extension_chan_offset =
2386                                         IEEE80211_HT_PARAM_CHA_SEC_NONE;
2387                                 ht_conf->is_40mhz = false;
2388                         }
2389                 } else
2390                         ht_conf->is_40mhz = false;
2391                 /* Default to no protection. Protection mode will later be set
2392                  * from BSS config in iwl_ht_conf */
2393                 ht_conf->ht_protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2394
2395                 /* if we are switching from ht to 2.4 clear flags
2396                  * from any ht related info since 2.4 does not
2397                  * support ht */
2398                 if ((le16_to_cpu(priv->staging_rxon.channel) != ch))
2399                         priv->staging_rxon.flags = 0;
2400
2401                 iwl_set_rxon_channel(priv, conf->channel);
2402                 iwl_set_rxon_ht(priv, ht_conf);
2403
2404                 iwl_set_flags_for_band(priv, conf->channel->band);
2405                 spin_unlock_irqrestore(&priv->lock, flags);
2406                 if (iwl_is_associated(priv) &&
2407                     (le16_to_cpu(priv->active_rxon.channel) != ch) &&
2408                     priv->cfg->ops->lib->set_channel_switch) {
2409                         iwl_set_rate(priv);
2410                         /*
2411                          * at this point, staging_rxon has the
2412                          * configuration for channel switch
2413                          */
2414                         ret = priv->cfg->ops->lib->set_channel_switch(priv,
2415                                 ch);
2416                         if (!ret) {
2417                                 iwl_print_rx_config_cmd(priv);
2418                                 goto out;
2419                         }
2420                         priv->switch_rxon.switch_in_progress = false;
2421                 }
2422  set_ch_out:
2423                 /* The list of supported rates and rate mask can be different
2424                  * for each band; since the band may have changed, reset
2425                  * the rate mask to what mac80211 lists */
2426                 iwl_set_rate(priv);
2427         }
2428
2429         if (changed & (IEEE80211_CONF_CHANGE_PS |
2430                         IEEE80211_CONF_CHANGE_IDLE)) {
2431                 ret = iwl_power_update_mode(priv, false);
2432                 if (ret)
2433                         IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
2434         }
2435
2436         if (changed & IEEE80211_CONF_CHANGE_POWER) {
2437                 IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
2438                         priv->tx_power_user_lmt, conf->power_level);
2439
2440                 iwl_set_tx_power(priv, conf->power_level, false);
2441         }
2442
2443         if (!iwl_is_ready(priv)) {
2444                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
2445                 goto out;
2446         }
2447
2448         if (scan_active)
2449                 goto out;
2450
2451         if (memcmp(&priv->active_rxon,
2452                    &priv->staging_rxon, sizeof(priv->staging_rxon)))
2453                 iwlcore_commit_rxon(priv);
2454         else
2455                 IWL_DEBUG_INFO(priv, "Not re-sending same RXON configuration.\n");
2456
2457
2458 out:
2459         IWL_DEBUG_MAC80211(priv, "leave\n");
2460         mutex_unlock(&priv->mutex);
2461         return ret;
2462 }
2463 EXPORT_SYMBOL(iwl_mac_config);
2464
2465 void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
2466 {
2467         struct iwl_priv *priv = hw->priv;
2468         unsigned long flags;
2469
2470         mutex_lock(&priv->mutex);
2471         IWL_DEBUG_MAC80211(priv, "enter\n");
2472
2473         spin_lock_irqsave(&priv->lock, flags);
2474         memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_config));
2475         spin_unlock_irqrestore(&priv->lock, flags);
2476
2477         iwl_reset_qos(priv);
2478
2479         spin_lock_irqsave(&priv->lock, flags);
2480         priv->assoc_id = 0;
2481         priv->assoc_capability = 0;
2482
2483         /* new association get rid of ibss beacon skb */
2484         if (priv->ibss_beacon)
2485                 dev_kfree_skb(priv->ibss_beacon);
2486
2487         priv->ibss_beacon = NULL;
2488
2489         priv->beacon_int = priv->vif->bss_conf.beacon_int;
2490         priv->timestamp = 0;
2491
2492         spin_unlock_irqrestore(&priv->lock, flags);
2493
2494         if (!iwl_is_ready_rf(priv)) {
2495                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
2496                 mutex_unlock(&priv->mutex);
2497                 return;
2498         }
2499
2500         /* we are restarting association process
2501          * clear RXON_FILTER_ASSOC_MSK bit
2502          */
2503         iwl_scan_cancel_timeout(priv, 100);
2504         priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2505         iwlcore_commit_rxon(priv);
2506
2507         iwl_set_rate(priv);
2508
2509         mutex_unlock(&priv->mutex);
2510
2511         IWL_DEBUG_MAC80211(priv, "leave\n");
2512 }
2513 EXPORT_SYMBOL(iwl_mac_reset_tsf);
2514
2515 int iwl_alloc_txq_mem(struct iwl_priv *priv)
2516 {
2517         if (!priv->txq)
2518                 priv->txq = kzalloc(
2519                         sizeof(struct iwl_tx_queue) * priv->cfg->num_of_queues,
2520                         GFP_KERNEL);
2521         if (!priv->txq) {
2522                 IWL_ERR(priv, "Not enough memory for txq \n");
2523                 return -ENOMEM;
2524         }
2525         return 0;
2526 }
2527 EXPORT_SYMBOL(iwl_alloc_txq_mem);
2528
2529 void iwl_free_txq_mem(struct iwl_priv *priv)
2530 {
2531         kfree(priv->txq);
2532         priv->txq = NULL;
2533 }
2534 EXPORT_SYMBOL(iwl_free_txq_mem);
2535
2536 int iwl_send_wimax_coex(struct iwl_priv *priv)
2537 {
2538         struct iwl_wimax_coex_cmd uninitialized_var(coex_cmd);
2539
2540         if (priv->cfg->support_wimax_coexist) {
2541                 /* UnMask wake up src at associated sleep */
2542                 coex_cmd.flags |= COEX_FLAGS_ASSOC_WA_UNMASK_MSK;
2543
2544                 /* UnMask wake up src at unassociated sleep */
2545                 coex_cmd.flags |= COEX_FLAGS_UNASSOC_WA_UNMASK_MSK;
2546                 memcpy(coex_cmd.sta_prio, cu_priorities,
2547                         sizeof(struct iwl_wimax_coex_event_entry) *
2548                          COEX_NUM_OF_EVENTS);
2549
2550                 /* enabling the coexistence feature */
2551                 coex_cmd.flags |= COEX_FLAGS_COEX_ENABLE_MSK;
2552
2553                 /* enabling the priorities tables */
2554                 coex_cmd.flags |= COEX_FLAGS_STA_TABLE_VALID_MSK;
2555         } else {
2556                 /* coexistence is disabled */
2557                 memset(&coex_cmd, 0, sizeof(coex_cmd));
2558         }
2559         return iwl_send_cmd_pdu(priv, COEX_PRIORITY_TABLE_CMD,
2560                                 sizeof(coex_cmd), &coex_cmd);
2561 }
2562 EXPORT_SYMBOL(iwl_send_wimax_coex);
2563
2564 #ifdef CONFIG_IWLWIFI_DEBUGFS
2565
2566 #define IWL_TRAFFIC_DUMP_SIZE   (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
2567
2568 void iwl_reset_traffic_log(struct iwl_priv *priv)
2569 {
2570         priv->tx_traffic_idx = 0;
2571         priv->rx_traffic_idx = 0;
2572         if (priv->tx_traffic)
2573                 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
2574         if (priv->rx_traffic)
2575                 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
2576 }
2577
2578 int iwl_alloc_traffic_mem(struct iwl_priv *priv)
2579 {
2580         u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
2581
2582         if (iwl_debug_level & IWL_DL_TX) {
2583                 if (!priv->tx_traffic) {
2584                         priv->tx_traffic =
2585                                 kzalloc(traffic_size, GFP_KERNEL);
2586                         if (!priv->tx_traffic)
2587                                 return -ENOMEM;
2588                 }
2589         }
2590         if (iwl_debug_level & IWL_DL_RX) {
2591                 if (!priv->rx_traffic) {
2592                         priv->rx_traffic =
2593                                 kzalloc(traffic_size, GFP_KERNEL);
2594                         if (!priv->rx_traffic)
2595                                 return -ENOMEM;
2596                 }
2597         }
2598         iwl_reset_traffic_log(priv);
2599         return 0;
2600 }
2601 EXPORT_SYMBOL(iwl_alloc_traffic_mem);
2602
2603 void iwl_free_traffic_mem(struct iwl_priv *priv)
2604 {
2605         kfree(priv->tx_traffic);
2606         priv->tx_traffic = NULL;
2607
2608         kfree(priv->rx_traffic);
2609         priv->rx_traffic = NULL;
2610 }
2611 EXPORT_SYMBOL(iwl_free_traffic_mem);
2612
2613 void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
2614                       u16 length, struct ieee80211_hdr *header)
2615 {
2616         __le16 fc;
2617         u16 len;
2618
2619         if (likely(!(iwl_debug_level & IWL_DL_TX)))
2620                 return;
2621
2622         if (!priv->tx_traffic)
2623                 return;
2624
2625         fc = header->frame_control;
2626         if (ieee80211_is_data(fc)) {
2627                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
2628                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
2629                 memcpy((priv->tx_traffic +
2630                        (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
2631                        header, len);
2632                 priv->tx_traffic_idx =
2633                         (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
2634         }
2635 }
2636 EXPORT_SYMBOL(iwl_dbg_log_tx_data_frame);
2637
2638 void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
2639                       u16 length, struct ieee80211_hdr *header)
2640 {
2641         __le16 fc;
2642         u16 len;
2643
2644         if (likely(!(iwl_debug_level & IWL_DL_RX)))
2645                 return;
2646
2647         if (!priv->rx_traffic)
2648                 return;
2649
2650         fc = header->frame_control;
2651         if (ieee80211_is_data(fc)) {
2652                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
2653                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
2654                 memcpy((priv->rx_traffic +
2655                        (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
2656                        header, len);
2657                 priv->rx_traffic_idx =
2658                         (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
2659         }
2660 }
2661 EXPORT_SYMBOL(iwl_dbg_log_rx_data_frame);
2662
2663 const char *get_mgmt_string(int cmd)
2664 {
2665         switch (cmd) {
2666                 IWL_CMD(MANAGEMENT_ASSOC_REQ);
2667                 IWL_CMD(MANAGEMENT_ASSOC_RESP);
2668                 IWL_CMD(MANAGEMENT_REASSOC_REQ);
2669                 IWL_CMD(MANAGEMENT_REASSOC_RESP);
2670                 IWL_CMD(MANAGEMENT_PROBE_REQ);
2671                 IWL_CMD(MANAGEMENT_PROBE_RESP);
2672                 IWL_CMD(MANAGEMENT_BEACON);
2673                 IWL_CMD(MANAGEMENT_ATIM);
2674                 IWL_CMD(MANAGEMENT_DISASSOC);
2675                 IWL_CMD(MANAGEMENT_AUTH);
2676                 IWL_CMD(MANAGEMENT_DEAUTH);
2677                 IWL_CMD(MANAGEMENT_ACTION);
2678         default:
2679                 return "UNKNOWN";
2680
2681         }
2682 }
2683
2684 const char *get_ctrl_string(int cmd)
2685 {
2686         switch (cmd) {
2687                 IWL_CMD(CONTROL_BACK_REQ);
2688                 IWL_CMD(CONTROL_BACK);
2689                 IWL_CMD(CONTROL_PSPOLL);
2690                 IWL_CMD(CONTROL_RTS);
2691                 IWL_CMD(CONTROL_CTS);
2692                 IWL_CMD(CONTROL_ACK);
2693                 IWL_CMD(CONTROL_CFEND);
2694                 IWL_CMD(CONTROL_CFENDACK);
2695         default:
2696                 return "UNKNOWN";
2697
2698         }
2699 }
2700
2701 void iwl_clear_traffic_stats(struct iwl_priv *priv)
2702 {
2703         memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
2704         memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
2705         priv->led_tpt = 0;
2706 }
2707
2708 /*
2709  * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
2710  * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
2711  * Use debugFs to display the rx/rx_statistics
2712  * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
2713  * information will be recorded, but DATA pkt still will be recorded
2714  * for the reason of iwl_led.c need to control the led blinking based on
2715  * number of tx and rx data.
2716  *
2717  */
2718 void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
2719 {
2720         struct traffic_stats    *stats;
2721
2722         if (is_tx)
2723                 stats = &priv->tx_stats;
2724         else
2725                 stats = &priv->rx_stats;
2726
2727         if (ieee80211_is_mgmt(fc)) {
2728                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
2729                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
2730                         stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
2731                         break;
2732                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
2733                         stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
2734                         break;
2735                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
2736                         stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
2737                         break;
2738                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
2739                         stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
2740                         break;
2741                 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
2742                         stats->mgmt[MANAGEMENT_PROBE_REQ]++;
2743                         break;
2744                 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
2745                         stats->mgmt[MANAGEMENT_PROBE_RESP]++;
2746                         break;
2747                 case cpu_to_le16(IEEE80211_STYPE_BEACON):
2748                         stats->mgmt[MANAGEMENT_BEACON]++;
2749                         break;
2750                 case cpu_to_le16(IEEE80211_STYPE_ATIM):
2751                         stats->mgmt[MANAGEMENT_ATIM]++;
2752                         break;
2753                 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
2754                         stats->mgmt[MANAGEMENT_DISASSOC]++;
2755                         break;
2756                 case cpu_to_le16(IEEE80211_STYPE_AUTH):
2757                         stats->mgmt[MANAGEMENT_AUTH]++;
2758                         break;
2759                 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
2760                         stats->mgmt[MANAGEMENT_DEAUTH]++;
2761                         break;
2762                 case cpu_to_le16(IEEE80211_STYPE_ACTION):
2763                         stats->mgmt[MANAGEMENT_ACTION]++;
2764                         break;
2765                 }
2766         } else if (ieee80211_is_ctl(fc)) {
2767                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
2768                 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
2769                         stats->ctrl[CONTROL_BACK_REQ]++;
2770                         break;
2771                 case cpu_to_le16(IEEE80211_STYPE_BACK):
2772                         stats->ctrl[CONTROL_BACK]++;
2773                         break;
2774                 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
2775                         stats->ctrl[CONTROL_PSPOLL]++;
2776                         break;
2777                 case cpu_to_le16(IEEE80211_STYPE_RTS):
2778                         stats->ctrl[CONTROL_RTS]++;
2779                         break;
2780                 case cpu_to_le16(IEEE80211_STYPE_CTS):
2781                         stats->ctrl[CONTROL_CTS]++;
2782                         break;
2783                 case cpu_to_le16(IEEE80211_STYPE_ACK):
2784                         stats->ctrl[CONTROL_ACK]++;
2785                         break;
2786                 case cpu_to_le16(IEEE80211_STYPE_CFEND):
2787                         stats->ctrl[CONTROL_CFEND]++;
2788                         break;
2789                 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
2790                         stats->ctrl[CONTROL_CFENDACK]++;
2791                         break;
2792                 }
2793         } else {
2794                 /* data */
2795                 stats->data_cnt++;
2796                 stats->data_bytes += len;
2797         }
2798         iwl_leds_background(priv);
2799 }
2800 EXPORT_SYMBOL(iwl_update_stats);
2801 #endif
2802
2803 const static char *get_csr_string(int cmd)
2804 {
2805         switch (cmd) {
2806                 IWL_CMD(CSR_HW_IF_CONFIG_REG);
2807                 IWL_CMD(CSR_INT_COALESCING);
2808                 IWL_CMD(CSR_INT);
2809                 IWL_CMD(CSR_INT_MASK);
2810                 IWL_CMD(CSR_FH_INT_STATUS);
2811                 IWL_CMD(CSR_GPIO_IN);
2812                 IWL_CMD(CSR_RESET);
2813                 IWL_CMD(CSR_GP_CNTRL);
2814                 IWL_CMD(CSR_HW_REV);
2815                 IWL_CMD(CSR_EEPROM_REG);
2816                 IWL_CMD(CSR_EEPROM_GP);
2817                 IWL_CMD(CSR_OTP_GP_REG);
2818                 IWL_CMD(CSR_GIO_REG);
2819                 IWL_CMD(CSR_GP_UCODE_REG);
2820                 IWL_CMD(CSR_GP_DRIVER_REG);
2821                 IWL_CMD(CSR_UCODE_DRV_GP1);
2822                 IWL_CMD(CSR_UCODE_DRV_GP2);
2823                 IWL_CMD(CSR_LED_REG);
2824                 IWL_CMD(CSR_DRAM_INT_TBL_REG);
2825                 IWL_CMD(CSR_GIO_CHICKEN_BITS);
2826                 IWL_CMD(CSR_ANA_PLL_CFG);
2827                 IWL_CMD(CSR_HW_REV_WA_REG);
2828                 IWL_CMD(CSR_DBG_HPET_MEM_REG);
2829         default:
2830                 return "UNKNOWN";
2831
2832         }
2833 }
2834
2835 void iwl_dump_csr(struct iwl_priv *priv)
2836 {
2837         int i;
2838         u32 csr_tbl[] = {
2839                 CSR_HW_IF_CONFIG_REG,
2840                 CSR_INT_COALESCING,
2841                 CSR_INT,
2842                 CSR_INT_MASK,
2843                 CSR_FH_INT_STATUS,
2844                 CSR_GPIO_IN,
2845                 CSR_RESET,
2846                 CSR_GP_CNTRL,
2847                 CSR_HW_REV,
2848                 CSR_EEPROM_REG,
2849                 CSR_EEPROM_GP,
2850                 CSR_OTP_GP_REG,
2851                 CSR_GIO_REG,
2852                 CSR_GP_UCODE_REG,
2853                 CSR_GP_DRIVER_REG,
2854                 CSR_UCODE_DRV_GP1,
2855                 CSR_UCODE_DRV_GP2,
2856                 CSR_LED_REG,
2857                 CSR_DRAM_INT_TBL_REG,
2858                 CSR_GIO_CHICKEN_BITS,
2859                 CSR_ANA_PLL_CFG,
2860                 CSR_HW_REV_WA_REG,
2861                 CSR_DBG_HPET_MEM_REG
2862         };
2863         IWL_ERR(priv, "CSR values:\n");
2864         IWL_ERR(priv, "(2nd byte of CSR_INT_COALESCING is "
2865                 "CSR_INT_PERIODIC_REG)\n");
2866         for (i = 0; i <  ARRAY_SIZE(csr_tbl); i++) {
2867                 IWL_ERR(priv, "  %25s: 0X%08x\n",
2868                         get_csr_string(csr_tbl[i]),
2869                         iwl_read32(priv, csr_tbl[i]));
2870         }
2871 }
2872 EXPORT_SYMBOL(iwl_dump_csr);
2873
2874 const static char *get_fh_string(int cmd)
2875 {
2876         switch (cmd) {
2877                 IWL_CMD(FH_RSCSR_CHNL0_STTS_WPTR_REG);
2878                 IWL_CMD(FH_RSCSR_CHNL0_RBDCB_BASE_REG);
2879                 IWL_CMD(FH_RSCSR_CHNL0_WPTR);
2880                 IWL_CMD(FH_MEM_RCSR_CHNL0_CONFIG_REG);
2881                 IWL_CMD(FH_MEM_RSSR_SHARED_CTRL_REG);
2882                 IWL_CMD(FH_MEM_RSSR_RX_STATUS_REG);
2883                 IWL_CMD(FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
2884                 IWL_CMD(FH_TSSR_TX_STATUS_REG);
2885                 IWL_CMD(FH_TSSR_TX_ERROR_REG);
2886         default:
2887                 return "UNKNOWN";
2888
2889         }
2890 }
2891
2892 int iwl_dump_fh(struct iwl_priv *priv, char **buf, bool display)
2893 {
2894         int i;
2895 #ifdef CONFIG_IWLWIFI_DEBUG
2896         int pos = 0;
2897         size_t bufsz = 0;
2898 #endif
2899         u32 fh_tbl[] = {
2900                 FH_RSCSR_CHNL0_STTS_WPTR_REG,
2901                 FH_RSCSR_CHNL0_RBDCB_BASE_REG,
2902                 FH_RSCSR_CHNL0_WPTR,
2903                 FH_MEM_RCSR_CHNL0_CONFIG_REG,
2904                 FH_MEM_RSSR_SHARED_CTRL_REG,
2905                 FH_MEM_RSSR_RX_STATUS_REG,
2906                 FH_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
2907                 FH_TSSR_TX_STATUS_REG,
2908                 FH_TSSR_TX_ERROR_REG
2909         };
2910 #ifdef CONFIG_IWLWIFI_DEBUG
2911         if (display) {
2912                 bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
2913                 *buf = kmalloc(bufsz, GFP_KERNEL);
2914                 if (!*buf)
2915                         return -ENOMEM;
2916                 pos += scnprintf(*buf + pos, bufsz - pos,
2917                                 "FH register values:\n");
2918                 for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
2919                         pos += scnprintf(*buf + pos, bufsz - pos,
2920                                 "  %34s: 0X%08x\n",
2921                                 get_fh_string(fh_tbl[i]),
2922                                 iwl_read_direct32(priv, fh_tbl[i]));
2923                 }
2924                 return pos;
2925         }
2926 #endif
2927         IWL_ERR(priv, "FH register values:\n");
2928         for (i = 0; i <  ARRAY_SIZE(fh_tbl); i++) {
2929                 IWL_ERR(priv, "  %34s: 0X%08x\n",
2930                         get_fh_string(fh_tbl[i]),
2931                         iwl_read_direct32(priv, fh_tbl[i]));
2932         }
2933         return 0;
2934 }
2935 EXPORT_SYMBOL(iwl_dump_fh);
2936
2937 static void iwl_force_rf_reset(struct iwl_priv *priv)
2938 {
2939         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2940                 return;
2941
2942         if (!iwl_is_associated(priv)) {
2943                 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
2944                 return;
2945         }
2946         /*
2947          * There is no easy and better way to force reset the radio,
2948          * the only known method is switching channel which will force to
2949          * reset and tune the radio.
2950          * Use internal short scan (single channel) operation to should
2951          * achieve this objective.
2952          * Driver should reset the radio when number of consecutive missed
2953          * beacon, or any other uCode error condition detected.
2954          */
2955         IWL_DEBUG_INFO(priv, "perform radio reset.\n");
2956         iwl_internal_short_hw_scan(priv);
2957         return;
2958 }
2959
2960
2961 int iwl_force_reset(struct iwl_priv *priv, int mode)
2962 {
2963         struct iwl_force_reset *force_reset;
2964
2965         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2966                 return -EINVAL;
2967
2968         if (mode >= IWL_MAX_FORCE_RESET) {
2969                 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
2970                 return -EINVAL;
2971         }
2972         force_reset = &priv->force_reset[mode];
2973         force_reset->reset_request_count++;
2974         if (force_reset->last_force_reset_jiffies &&
2975             time_after(force_reset->last_force_reset_jiffies +
2976             force_reset->reset_duration, jiffies)) {
2977                 IWL_DEBUG_INFO(priv, "force reset rejected\n");
2978                 force_reset->reset_reject_count++;
2979                 return -EAGAIN;
2980         }
2981         force_reset->reset_success_count++;
2982         force_reset->last_force_reset_jiffies = jiffies;
2983         IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
2984         switch (mode) {
2985         case IWL_RF_RESET:
2986                 iwl_force_rf_reset(priv);
2987                 break;
2988         case IWL_FW_RESET:
2989                 IWL_ERR(priv, "On demand firmware reload\n");
2990                 /* Set the FW error flag -- cleared on iwl_down */
2991                 set_bit(STATUS_FW_ERROR, &priv->status);
2992                 wake_up_interruptible(&priv->wait_command_queue);
2993                 /*
2994                  * Keep the restart process from trying to send host
2995                  * commands by clearing the INIT status bit
2996                  */
2997                 clear_bit(STATUS_READY, &priv->status);
2998                 queue_work(priv->workqueue, &priv->restart);
2999                 break;
3000         }
3001         return 0;
3002 }
3003 EXPORT_SYMBOL(iwl_force_reset);
3004
3005 /**
3006  * iwl_bg_monitor_recover - Timer callback to check for stuck queue and recover
3007  *
3008  * During normal condition (no queue is stuck), the timer is continually set to
3009  * execute every monitor_recover_period milliseconds after the last timer
3010  * expired.  When the queue read_ptr is at the same place, the timer is
3011  * shorten to 100mSecs.  This is
3012  *      1) to reduce the chance that the read_ptr may wrap around (not stuck)
3013  *      2) to detect the stuck queues quicker before the station and AP can
3014  *      disassociate each other.
3015  *
3016  * This function monitors all the tx queues and recover from it if any
3017  * of the queues are stuck.
3018  * 1. It first check the cmd queue for stuck conditions.  If it is stuck,
3019  *      it will recover by resetting the firmware and return.
3020  * 2. Then, it checks for station association.  If it associates it will check
3021  *      other queues.  If any queue is stuck, it will recover by resetting
3022  *      the firmware.
3023  * Note: It the number of times the queue read_ptr to be at the same place to
3024  *      be MAX_REPEAT+1 in order to consider to be stuck.
3025  */
3026 /*
3027  * The maximum number of times the read pointer of the tx queue at the
3028  * same place without considering to be stuck.
3029  */
3030 #define MAX_REPEAT      (2)
3031 static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
3032 {
3033         struct iwl_tx_queue *txq;
3034         struct iwl_queue *q;
3035
3036         txq = &priv->txq[cnt];
3037         q = &txq->q;
3038         /* queue is empty, skip */
3039         if (q->read_ptr != q->write_ptr) {
3040                 if (q->read_ptr == q->last_read_ptr) {
3041                         /* a queue has not been read from last time */
3042                         if (q->repeat_same_read_ptr > MAX_REPEAT) {
3043                                 IWL_ERR(priv,
3044                                         "queue %d stuck %d time. Fw reload.\n",
3045                                         q->id, q->repeat_same_read_ptr);
3046                                 q->repeat_same_read_ptr = 0;
3047                                 iwl_force_reset(priv, IWL_FW_RESET);
3048                         } else {
3049                                 q->repeat_same_read_ptr++;
3050                                 IWL_DEBUG_RADIO(priv,
3051                                                 "queue %d, not read %d time\n",
3052                                                 q->id,
3053                                                 q->repeat_same_read_ptr);
3054                                 mod_timer(&priv->monitor_recover, jiffies +
3055                                         msecs_to_jiffies(IWL_ONE_HUNDRED_MSECS));
3056                         }
3057                         return 1;
3058                 } else {
3059                         q->last_read_ptr = q->read_ptr;
3060                         q->repeat_same_read_ptr = 0;
3061                 }
3062         }
3063         return 0;
3064 }
3065
3066 void iwl_bg_monitor_recover(unsigned long data)
3067 {
3068         struct iwl_priv *priv = (struct iwl_priv *)data;
3069         int cnt;
3070
3071         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
3072                 return;
3073
3074         /* monitor and check for stuck cmd queue */
3075         if (iwl_check_stuck_queue(priv, IWL_CMD_QUEUE_NUM))
3076                 return;
3077
3078         /* monitor and check for other stuck queues */
3079         if (iwl_is_associated(priv)) {
3080                 for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
3081                         /* skip as we already checked the command queue */
3082                         if (cnt == IWL_CMD_QUEUE_NUM)
3083                                 continue;
3084                         if (iwl_check_stuck_queue(priv, cnt))
3085                                 return;
3086                 }
3087         }
3088         /*
3089          * Reschedule the timer to occur in
3090          * priv->cfg->monitor_recover_period
3091          */
3092         mod_timer(&priv->monitor_recover,
3093                 jiffies + msecs_to_jiffies(priv->cfg->monitor_recover_period));
3094 }
3095 EXPORT_SYMBOL(iwl_bg_monitor_recover);
3096
3097 #ifdef CONFIG_PM
3098
3099 int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
3100 {
3101         struct iwl_priv *priv = pci_get_drvdata(pdev);
3102
3103         /*
3104          * This function is called when system goes into suspend state
3105          * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
3106          * first but since iwl_mac_stop() has no knowledge of who the caller is,
3107          * it will not call apm_ops.stop() to stop the DMA operation.
3108          * Calling apm_ops.stop here to make sure we stop the DMA.
3109          */
3110         priv->cfg->ops->lib->apm_ops.stop(priv);
3111
3112         pci_save_state(pdev);
3113         pci_disable_device(pdev);
3114         pci_set_power_state(pdev, PCI_D3hot);
3115
3116         return 0;
3117 }
3118 EXPORT_SYMBOL(iwl_pci_suspend);
3119
3120 int iwl_pci_resume(struct pci_dev *pdev)
3121 {
3122         struct iwl_priv *priv = pci_get_drvdata(pdev);
3123         int ret;
3124
3125         pci_set_power_state(pdev, PCI_D0);
3126         ret = pci_enable_device(pdev);
3127         if (ret)
3128                 return ret;
3129         pci_restore_state(pdev);
3130         iwl_enable_interrupts(priv);
3131
3132         return 0;
3133 }
3134 EXPORT_SYMBOL(iwl_pci_resume);
3135
3136 #endif /* CONFIG_PM */