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