7cc560bc4f9569658e9cc9287466c7782eec6976
[linux-2.6.git] / drivers / net / wireless / iwlegacy / iwl-core.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 - 2011 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"
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-legacy: common functions for 3945 and 4965");
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 bool bt_coex_active = true;
68 EXPORT_SYMBOL_GPL(bt_coex_active);
69 module_param(bt_coex_active, bool, S_IRUGO);
70 MODULE_PARM_DESC(bt_coex_active, "enable wifi/bluetooth co-exist");
71
72 u32 iwl_debug_level;
73 EXPORT_SYMBOL(iwl_debug_level);
74
75 const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
76 EXPORT_SYMBOL(iwl_bcast_addr);
77
78
79 /* This function both allocates and initializes hw and priv. */
80 struct ieee80211_hw *iwl_legacy_alloc_all(struct iwl_cfg *cfg)
81 {
82         struct iwl_priv *priv;
83         /* mac80211 allocates memory for this device instance, including
84          *   space for this driver's private structure */
85         struct ieee80211_hw *hw;
86
87         hw = ieee80211_alloc_hw(sizeof(struct iwl_priv),
88                                 cfg->ops->ieee80211_ops);
89         if (hw == NULL) {
90                 pr_err("%s: Can not allocate network device\n",
91                        cfg->name);
92                 goto out;
93         }
94
95         priv = hw->priv;
96         priv->hw = hw;
97
98 out:
99         return hw;
100 }
101 EXPORT_SYMBOL(iwl_legacy_alloc_all);
102
103 #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
104 #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
105 static void iwl_legacy_init_ht_hw_capab(const struct iwl_priv *priv,
106                               struct ieee80211_sta_ht_cap *ht_info,
107                               enum ieee80211_band band)
108 {
109         u16 max_bit_rate = 0;
110         u8 rx_chains_num = priv->hw_params.rx_chains_num;
111         u8 tx_chains_num = priv->hw_params.tx_chains_num;
112
113         ht_info->cap = 0;
114         memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
115
116         ht_info->ht_supported = true;
117
118         ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
119         max_bit_rate = MAX_BIT_RATE_20_MHZ;
120         if (priv->hw_params.ht40_channel & BIT(band)) {
121                 ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
122                 ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
123                 ht_info->mcs.rx_mask[4] = 0x01;
124                 max_bit_rate = MAX_BIT_RATE_40_MHZ;
125         }
126
127         if (priv->cfg->mod_params->amsdu_size_8K)
128                 ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
129
130         ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
131         ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
132
133         ht_info->mcs.rx_mask[0] = 0xFF;
134         if (rx_chains_num >= 2)
135                 ht_info->mcs.rx_mask[1] = 0xFF;
136         if (rx_chains_num >= 3)
137                 ht_info->mcs.rx_mask[2] = 0xFF;
138
139         /* Highest supported Rx data rate */
140         max_bit_rate *= rx_chains_num;
141         WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
142         ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
143
144         /* Tx MCS capabilities */
145         ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
146         if (tx_chains_num != rx_chains_num) {
147                 ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
148                 ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
149                                 IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
150         }
151 }
152
153 /**
154  * iwl_legacy_init_geos - Initialize mac80211's geo/channel info based from eeprom
155  */
156 int iwl_legacy_init_geos(struct iwl_priv *priv)
157 {
158         struct iwl_channel_info *ch;
159         struct ieee80211_supported_band *sband;
160         struct ieee80211_channel *channels;
161         struct ieee80211_channel *geo_ch;
162         struct ieee80211_rate *rates;
163         int i = 0;
164
165         if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
166             priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
167                 IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
168                 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
169                 return 0;
170         }
171
172         channels = kzalloc(sizeof(struct ieee80211_channel) *
173                            priv->channel_count, GFP_KERNEL);
174         if (!channels)
175                 return -ENOMEM;
176
177         rates = kzalloc((sizeof(struct ieee80211_rate) * IWL_RATE_COUNT_LEGACY),
178                         GFP_KERNEL);
179         if (!rates) {
180                 kfree(channels);
181                 return -ENOMEM;
182         }
183
184         /* 5.2GHz channels start after the 2.4GHz channels */
185         sband = &priv->bands[IEEE80211_BAND_5GHZ];
186         sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
187         /* just OFDM */
188         sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
189         sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
190
191         if (priv->cfg->sku & IWL_SKU_N)
192                 iwl_legacy_init_ht_hw_capab(priv, &sband->ht_cap,
193                                          IEEE80211_BAND_5GHZ);
194
195         sband = &priv->bands[IEEE80211_BAND_2GHZ];
196         sband->channels = channels;
197         /* OFDM & CCK */
198         sband->bitrates = rates;
199         sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
200
201         if (priv->cfg->sku & IWL_SKU_N)
202                 iwl_legacy_init_ht_hw_capab(priv, &sband->ht_cap,
203                                          IEEE80211_BAND_2GHZ);
204
205         priv->ieee_channels = channels;
206         priv->ieee_rates = rates;
207
208         for (i = 0;  i < priv->channel_count; i++) {
209                 ch = &priv->channel_info[i];
210
211                 if (!iwl_legacy_is_channel_valid(ch))
212                         continue;
213
214                 if (iwl_legacy_is_channel_a_band(ch))
215                         sband =  &priv->bands[IEEE80211_BAND_5GHZ];
216                 else
217                         sband =  &priv->bands[IEEE80211_BAND_2GHZ];
218
219                 geo_ch = &sband->channels[sband->n_channels++];
220
221                 geo_ch->center_freq =
222                         ieee80211_channel_to_frequency(ch->channel, ch->band);
223                 geo_ch->max_power = ch->max_power_avg;
224                 geo_ch->max_antenna_gain = 0xff;
225                 geo_ch->hw_value = ch->channel;
226
227                 if (iwl_legacy_is_channel_valid(ch)) {
228                         if (!(ch->flags & EEPROM_CHANNEL_IBSS))
229                                 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
230
231                         if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
232                                 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
233
234                         if (ch->flags & EEPROM_CHANNEL_RADAR)
235                                 geo_ch->flags |= IEEE80211_CHAN_RADAR;
236
237                         geo_ch->flags |= ch->ht40_extension_channel;
238
239                         if (ch->max_power_avg > priv->tx_power_device_lmt)
240                                 priv->tx_power_device_lmt = ch->max_power_avg;
241                 } else {
242                         geo_ch->flags |= IEEE80211_CHAN_DISABLED;
243                 }
244
245                 IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
246                                 ch->channel, geo_ch->center_freq,
247                                 iwl_legacy_is_channel_a_band(ch) ?  "5.2" : "2.4",
248                                 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
249                                 "restricted" : "valid",
250                                  geo_ch->flags);
251         }
252
253         if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
254              priv->cfg->sku & IWL_SKU_A) {
255                 IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
256                         "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
257                            priv->pci_dev->device,
258                            priv->pci_dev->subsystem_device);
259                 priv->cfg->sku &= ~IWL_SKU_A;
260         }
261
262         IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
263                    priv->bands[IEEE80211_BAND_2GHZ].n_channels,
264                    priv->bands[IEEE80211_BAND_5GHZ].n_channels);
265
266         set_bit(STATUS_GEO_CONFIGURED, &priv->status);
267
268         return 0;
269 }
270 EXPORT_SYMBOL(iwl_legacy_init_geos);
271
272 /*
273  * iwl_legacy_free_geos - undo allocations in iwl_legacy_init_geos
274  */
275 void iwl_legacy_free_geos(struct iwl_priv *priv)
276 {
277         kfree(priv->ieee_channels);
278         kfree(priv->ieee_rates);
279         clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
280 }
281 EXPORT_SYMBOL(iwl_legacy_free_geos);
282
283 static bool iwl_legacy_is_channel_extension(struct iwl_priv *priv,
284                                      enum ieee80211_band band,
285                                      u16 channel, u8 extension_chan_offset)
286 {
287         const struct iwl_channel_info *ch_info;
288
289         ch_info = iwl_legacy_get_channel_info(priv, band, channel);
290         if (!iwl_legacy_is_channel_valid(ch_info))
291                 return false;
292
293         if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
294                 return !(ch_info->ht40_extension_channel &
295                                         IEEE80211_CHAN_NO_HT40PLUS);
296         else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
297                 return !(ch_info->ht40_extension_channel &
298                                         IEEE80211_CHAN_NO_HT40MINUS);
299
300         return false;
301 }
302
303 bool iwl_legacy_is_ht40_tx_allowed(struct iwl_priv *priv,
304                             struct iwl_rxon_context *ctx,
305                             struct ieee80211_sta_ht_cap *ht_cap)
306 {
307         if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
308                 return false;
309
310         /*
311          * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
312          * the bit will not set if it is pure 40MHz case
313          */
314         if (ht_cap && !ht_cap->ht_supported)
315                 return false;
316
317 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUGFS
318         if (priv->disable_ht40)
319                 return false;
320 #endif
321
322         return iwl_legacy_is_channel_extension(priv, priv->band,
323                         le16_to_cpu(ctx->staging.channel),
324                         ctx->ht.extension_chan_offset);
325 }
326 EXPORT_SYMBOL(iwl_legacy_is_ht40_tx_allowed);
327
328 static u16 iwl_legacy_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
329 {
330         u16 new_val;
331         u16 beacon_factor;
332
333         /*
334          * If mac80211 hasn't given us a beacon interval, program
335          * the default into the device.
336          */
337         if (!beacon_val)
338                 return DEFAULT_BEACON_INTERVAL;
339
340         /*
341          * If the beacon interval we obtained from the peer
342          * is too large, we'll have to wake up more often
343          * (and in IBSS case, we'll beacon too much)
344          *
345          * For example, if max_beacon_val is 4096, and the
346          * requested beacon interval is 7000, we'll have to
347          * use 3500 to be able to wake up on the beacons.
348          *
349          * This could badly influence beacon detection stats.
350          */
351
352         beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
353         new_val = beacon_val / beacon_factor;
354
355         if (!new_val)
356                 new_val = max_beacon_val;
357
358         return new_val;
359 }
360
361 int
362 iwl_legacy_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
363 {
364         u64 tsf;
365         s32 interval_tm, rem;
366         struct ieee80211_conf *conf = NULL;
367         u16 beacon_int;
368         struct ieee80211_vif *vif = ctx->vif;
369
370         conf = iwl_legacy_ieee80211_get_hw_conf(priv->hw);
371
372         lockdep_assert_held(&priv->mutex);
373
374         memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
375
376         ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
377         ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
378
379         beacon_int = vif ? vif->bss_conf.beacon_int : 0;
380
381         /*
382          * TODO: For IBSS we need to get atim_window from mac80211,
383          *       for now just always use 0
384          */
385         ctx->timing.atim_window = 0;
386
387         beacon_int = iwl_legacy_adjust_beacon_interval(beacon_int,
388                         priv->hw_params.max_beacon_itrvl * TIME_UNIT);
389         ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
390
391         tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
392         interval_tm = beacon_int * TIME_UNIT;
393         rem = do_div(tsf, interval_tm);
394         ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
395
396         ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
397
398         IWL_DEBUG_ASSOC(priv,
399                         "beacon interval %d beacon timer %d beacon tim %d\n",
400                         le16_to_cpu(ctx->timing.beacon_interval),
401                         le32_to_cpu(ctx->timing.beacon_init_val),
402                         le16_to_cpu(ctx->timing.atim_window));
403
404         return iwl_legacy_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
405                                 sizeof(ctx->timing), &ctx->timing);
406 }
407 EXPORT_SYMBOL(iwl_legacy_send_rxon_timing);
408
409 void
410 iwl_legacy_set_rxon_hwcrypto(struct iwl_priv *priv,
411                                 struct iwl_rxon_context *ctx,
412                                 int hw_decrypt)
413 {
414         struct iwl_legacy_rxon_cmd *rxon = &ctx->staging;
415
416         if (hw_decrypt)
417                 rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
418         else
419                 rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
420
421 }
422 EXPORT_SYMBOL(iwl_legacy_set_rxon_hwcrypto);
423
424 /* validate RXON structure is valid */
425 int
426 iwl_legacy_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
427 {
428         struct iwl_legacy_rxon_cmd *rxon = &ctx->staging;
429         bool error = false;
430
431         if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
432                 if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
433                         IWL_WARN(priv, "check 2.4G: wrong narrow\n");
434                         error = true;
435                 }
436                 if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
437                         IWL_WARN(priv, "check 2.4G: wrong radar\n");
438                         error = true;
439                 }
440         } else {
441                 if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
442                         IWL_WARN(priv, "check 5.2G: not short slot!\n");
443                         error = true;
444                 }
445                 if (rxon->flags & RXON_FLG_CCK_MSK) {
446                         IWL_WARN(priv, "check 5.2G: CCK!\n");
447                         error = true;
448                 }
449         }
450         if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
451                 IWL_WARN(priv, "mac/bssid mcast!\n");
452                 error = true;
453         }
454
455         /* make sure basic rates 6Mbps and 1Mbps are supported */
456         if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
457             (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
458                 IWL_WARN(priv, "neither 1 nor 6 are basic\n");
459                 error = true;
460         }
461
462         if (le16_to_cpu(rxon->assoc_id) > 2007) {
463                 IWL_WARN(priv, "aid > 2007\n");
464                 error = true;
465         }
466
467         if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
468                         == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
469                 IWL_WARN(priv, "CCK and short slot\n");
470                 error = true;
471         }
472
473         if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
474                         == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
475                 IWL_WARN(priv, "CCK and auto detect");
476                 error = true;
477         }
478
479         if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
480                             RXON_FLG_TGG_PROTECT_MSK)) ==
481                             RXON_FLG_TGG_PROTECT_MSK) {
482                 IWL_WARN(priv, "TGg but no auto-detect\n");
483                 error = true;
484         }
485
486         if (error)
487                 IWL_WARN(priv, "Tuning to channel %d\n",
488                             le16_to_cpu(rxon->channel));
489
490         if (error) {
491                 IWL_ERR(priv, "Invalid RXON\n");
492                 return -EINVAL;
493         }
494         return 0;
495 }
496 EXPORT_SYMBOL(iwl_legacy_check_rxon_cmd);
497
498 /**
499  * iwl_legacy_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
500  * @priv: staging_rxon is compared to active_rxon
501  *
502  * If the RXON structure is changing enough to require a new tune,
503  * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
504  * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
505  */
506 int iwl_legacy_full_rxon_required(struct iwl_priv *priv,
507                            struct iwl_rxon_context *ctx)
508 {
509         const struct iwl_legacy_rxon_cmd *staging = &ctx->staging;
510         const struct iwl_legacy_rxon_cmd *active = &ctx->active;
511
512 #define CHK(cond)                                                       \
513         if ((cond)) {                                                   \
514                 IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n");   \
515                 return 1;                                               \
516         }
517
518 #define CHK_NEQ(c1, c2)                                         \
519         if ((c1) != (c2)) {                                     \
520                 IWL_DEBUG_INFO(priv, "need full RXON - "        \
521                                #c1 " != " #c2 " - %d != %d\n",  \
522                                (c1), (c2));                     \
523                 return 1;                                       \
524         }
525
526         /* These items are only settable from the full RXON command */
527         CHK(!iwl_legacy_is_associated_ctx(ctx));
528         CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
529         CHK(compare_ether_addr(staging->node_addr, active->node_addr));
530         CHK(compare_ether_addr(staging->wlap_bssid_addr,
531                                 active->wlap_bssid_addr));
532         CHK_NEQ(staging->dev_type, active->dev_type);
533         CHK_NEQ(staging->channel, active->channel);
534         CHK_NEQ(staging->air_propagation, active->air_propagation);
535         CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
536                 active->ofdm_ht_single_stream_basic_rates);
537         CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
538                 active->ofdm_ht_dual_stream_basic_rates);
539         CHK_NEQ(staging->assoc_id, active->assoc_id);
540
541         /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
542          * be updated with the RXON_ASSOC command -- however only some
543          * flag transitions are allowed using RXON_ASSOC */
544
545         /* Check if we are not switching bands */
546         CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
547                 active->flags & RXON_FLG_BAND_24G_MSK);
548
549         /* Check if we are switching association toggle */
550         CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
551                 active->filter_flags & RXON_FILTER_ASSOC_MSK);
552
553 #undef CHK
554 #undef CHK_NEQ
555
556         return 0;
557 }
558 EXPORT_SYMBOL(iwl_legacy_full_rxon_required);
559
560 u8 iwl_legacy_get_lowest_plcp(struct iwl_priv *priv,
561                             struct iwl_rxon_context *ctx)
562 {
563         /*
564          * Assign the lowest rate -- should really get this from
565          * the beacon skb from mac80211.
566          */
567         if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK)
568                 return IWL_RATE_1M_PLCP;
569         else
570                 return IWL_RATE_6M_PLCP;
571 }
572 EXPORT_SYMBOL(iwl_legacy_get_lowest_plcp);
573
574 static void _iwl_legacy_set_rxon_ht(struct iwl_priv *priv,
575                              struct iwl_ht_config *ht_conf,
576                              struct iwl_rxon_context *ctx)
577 {
578         struct iwl_legacy_rxon_cmd *rxon = &ctx->staging;
579
580         if (!ctx->ht.enabled) {
581                 rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
582                         RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
583                         RXON_FLG_HT40_PROT_MSK |
584                         RXON_FLG_HT_PROT_MSK);
585                 return;
586         }
587
588         rxon->flags |= cpu_to_le32(ctx->ht.protection <<
589                                         RXON_FLG_HT_OPERATING_MODE_POS);
590
591         /* Set up channel bandwidth:
592          * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
593         /* clear the HT channel mode before set the mode */
594         rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
595                          RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
596         if (iwl_legacy_is_ht40_tx_allowed(priv, ctx, NULL)) {
597                 /* pure ht40 */
598                 if (ctx->ht.protection ==
599                                 IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
600                         rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
601                         /* Note: control channel is opposite of extension channel */
602                         switch (ctx->ht.extension_chan_offset) {
603                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
604                                 rxon->flags &=
605                                         ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
606                                 break;
607                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
608                                 rxon->flags |=
609                                         RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
610                                 break;
611                         }
612                 } else {
613                         /* Note: control channel is opposite of extension channel */
614                         switch (ctx->ht.extension_chan_offset) {
615                         case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
616                                 rxon->flags &=
617                                         ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
618                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
619                                 break;
620                         case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
621                                 rxon->flags |=
622                                         RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
623                                 rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
624                                 break;
625                         case IEEE80211_HT_PARAM_CHA_SEC_NONE:
626                         default:
627                                 /* channel location only valid if in Mixed mode */
628                                 IWL_ERR(priv,
629                                         "invalid extension channel offset\n");
630                                 break;
631                         }
632                 }
633         } else {
634                 rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
635         }
636
637         if (priv->cfg->ops->hcmd->set_rxon_chain)
638                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
639
640         IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
641                         "extension channel offset 0x%x\n",
642                         le32_to_cpu(rxon->flags), ctx->ht.protection,
643                         ctx->ht.extension_chan_offset);
644 }
645
646 void iwl_legacy_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
647 {
648         struct iwl_rxon_context *ctx;
649
650         for_each_context(priv, ctx)
651                 _iwl_legacy_set_rxon_ht(priv, ht_conf, ctx);
652 }
653 EXPORT_SYMBOL(iwl_legacy_set_rxon_ht);
654
655 /* Return valid, unused, channel for a passive scan to reset the RF */
656 u8 iwl_legacy_get_single_channel_number(struct iwl_priv *priv,
657                                  enum ieee80211_band band)
658 {
659         const struct iwl_channel_info *ch_info;
660         int i;
661         u8 channel = 0;
662         u8 min, max;
663         struct iwl_rxon_context *ctx;
664
665         if (band == IEEE80211_BAND_5GHZ) {
666                 min = 14;
667                 max = priv->channel_count;
668         } else {
669                 min = 0;
670                 max = 14;
671         }
672
673         for (i = min; i < max; i++) {
674                 bool busy = false;
675
676                 for_each_context(priv, ctx) {
677                         busy = priv->channel_info[i].channel ==
678                                 le16_to_cpu(ctx->staging.channel);
679                         if (busy)
680                                 break;
681                 }
682
683                 if (busy)
684                         continue;
685
686                 channel = priv->channel_info[i].channel;
687                 ch_info = iwl_legacy_get_channel_info(priv, band, channel);
688                 if (iwl_legacy_is_channel_valid(ch_info))
689                         break;
690         }
691
692         return channel;
693 }
694 EXPORT_SYMBOL(iwl_legacy_get_single_channel_number);
695
696 /**
697  * iwl_legacy_set_rxon_channel - Set the band and channel values in staging RXON
698  * @ch: requested channel as a pointer to struct ieee80211_channel
699
700  * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
701  * in the staging RXON flag structure based on the ch->band
702  */
703 int
704 iwl_legacy_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
705                          struct iwl_rxon_context *ctx)
706 {
707         enum ieee80211_band band = ch->band;
708         u16 channel = ch->hw_value;
709
710         if ((le16_to_cpu(ctx->staging.channel) == channel) &&
711             (priv->band == band))
712                 return 0;
713
714         ctx->staging.channel = cpu_to_le16(channel);
715         if (band == IEEE80211_BAND_5GHZ)
716                 ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
717         else
718                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
719
720         priv->band = band;
721
722         IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
723
724         return 0;
725 }
726 EXPORT_SYMBOL(iwl_legacy_set_rxon_channel);
727
728 void iwl_legacy_set_flags_for_band(struct iwl_priv *priv,
729                             struct iwl_rxon_context *ctx,
730                             enum ieee80211_band band,
731                             struct ieee80211_vif *vif)
732 {
733         if (band == IEEE80211_BAND_5GHZ) {
734                 ctx->staging.flags &=
735                     ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
736                       | RXON_FLG_CCK_MSK);
737                 ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
738         } else {
739                 /* Copied from iwl_post_associate() */
740                 if (vif && vif->bss_conf.use_short_slot)
741                         ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
742                 else
743                         ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
744
745                 ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
746                 ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
747                 ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
748         }
749 }
750 EXPORT_SYMBOL(iwl_legacy_set_flags_for_band);
751
752 /*
753  * initialize rxon structure with default values from eeprom
754  */
755 void iwl_legacy_connection_init_rx_config(struct iwl_priv *priv,
756                                    struct iwl_rxon_context *ctx)
757 {
758         const struct iwl_channel_info *ch_info;
759
760         memset(&ctx->staging, 0, sizeof(ctx->staging));
761
762         if (!ctx->vif) {
763                 ctx->staging.dev_type = ctx->unused_devtype;
764         } else
765         switch (ctx->vif->type) {
766
767         case NL80211_IFTYPE_STATION:
768                 ctx->staging.dev_type = ctx->station_devtype;
769                 ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
770                 break;
771
772         case NL80211_IFTYPE_ADHOC:
773                 ctx->staging.dev_type = ctx->ibss_devtype;
774                 ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
775                 ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
776                                                   RXON_FILTER_ACCEPT_GRP_MSK;
777                 break;
778
779         default:
780                 IWL_ERR(priv, "Unsupported interface type %d\n",
781                         ctx->vif->type);
782                 break;
783         }
784
785 #if 0
786         /* TODO:  Figure out when short_preamble would be set and cache from
787          * that */
788         if (!hw_to_local(priv->hw)->short_preamble)
789                 ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
790         else
791                 ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
792 #endif
793
794         ch_info = iwl_legacy_get_channel_info(priv, priv->band,
795                                        le16_to_cpu(ctx->active.channel));
796
797         if (!ch_info)
798                 ch_info = &priv->channel_info[0];
799
800         ctx->staging.channel = cpu_to_le16(ch_info->channel);
801         priv->band = ch_info->band;
802
803         iwl_legacy_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
804
805         ctx->staging.ofdm_basic_rates =
806             (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
807         ctx->staging.cck_basic_rates =
808             (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
809
810         /* clear both MIX and PURE40 mode flag */
811         ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
812                                         RXON_FLG_CHANNEL_MODE_PURE_40);
813         if (ctx->vif)
814                 memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
815
816         ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
817         ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
818 }
819 EXPORT_SYMBOL(iwl_legacy_connection_init_rx_config);
820
821 void iwl_legacy_set_rate(struct iwl_priv *priv)
822 {
823         const struct ieee80211_supported_band *hw = NULL;
824         struct ieee80211_rate *rate;
825         struct iwl_rxon_context *ctx;
826         int i;
827
828         hw = iwl_get_hw_mode(priv, priv->band);
829         if (!hw) {
830                 IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
831                 return;
832         }
833
834         priv->active_rate = 0;
835
836         for (i = 0; i < hw->n_bitrates; i++) {
837                 rate = &(hw->bitrates[i]);
838                 if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
839                         priv->active_rate |= (1 << rate->hw_value);
840         }
841
842         IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
843
844         for_each_context(priv, ctx) {
845                 ctx->staging.cck_basic_rates =
846                     (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
847
848                 ctx->staging.ofdm_basic_rates =
849                    (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
850         }
851 }
852 EXPORT_SYMBOL(iwl_legacy_set_rate);
853
854 void iwl_legacy_chswitch_done(struct iwl_priv *priv, bool is_success)
855 {
856         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
857
858         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
859                 return;
860
861         if (priv->switch_rxon.switch_in_progress) {
862                 ieee80211_chswitch_done(ctx->vif, is_success);
863                 mutex_lock(&priv->mutex);
864                 priv->switch_rxon.switch_in_progress = false;
865                 mutex_unlock(&priv->mutex);
866         }
867 }
868 EXPORT_SYMBOL(iwl_legacy_chswitch_done);
869
870 void iwl_legacy_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
871 {
872         struct iwl_rx_packet *pkt = rxb_addr(rxb);
873         struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
874
875         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
876         struct iwl_legacy_rxon_cmd *rxon = (void *)&ctx->active;
877
878         if (priv->switch_rxon.switch_in_progress) {
879                 if (!le32_to_cpu(csa->status) &&
880                     (csa->channel == priv->switch_rxon.channel)) {
881                         rxon->channel = csa->channel;
882                         ctx->staging.channel = csa->channel;
883                         IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
884                               le16_to_cpu(csa->channel));
885                         iwl_legacy_chswitch_done(priv, true);
886                 } else {
887                         IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
888                               le16_to_cpu(csa->channel));
889                         iwl_legacy_chswitch_done(priv, false);
890                 }
891         }
892 }
893 EXPORT_SYMBOL(iwl_legacy_rx_csa);
894
895 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
896 void iwl_legacy_print_rx_config_cmd(struct iwl_priv *priv,
897                              struct iwl_rxon_context *ctx)
898 {
899         struct iwl_legacy_rxon_cmd *rxon = &ctx->staging;
900
901         IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
902         iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
903         IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n",
904                                 le16_to_cpu(rxon->channel));
905         IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
906         IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
907                                 le32_to_cpu(rxon->filter_flags));
908         IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
909         IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
910                         rxon->ofdm_basic_rates);
911         IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n",
912                                 rxon->cck_basic_rates);
913         IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
914         IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
915         IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n",
916                                 le16_to_cpu(rxon->assoc_id));
917 }
918 EXPORT_SYMBOL(iwl_legacy_print_rx_config_cmd);
919 #endif
920 /**
921  * iwl_legacy_irq_handle_error - called for HW or SW error interrupt from card
922  */
923 void iwl_legacy_irq_handle_error(struct iwl_priv *priv)
924 {
925         /* Set the FW error flag -- cleared on iwl_down */
926         set_bit(STATUS_FW_ERROR, &priv->status);
927
928         /* Cancel currently queued command. */
929         clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
930
931         IWL_ERR(priv, "Loaded firmware version: %s\n",
932                 priv->hw->wiphy->fw_version);
933
934         priv->cfg->ops->lib->dump_nic_error_log(priv);
935         if (priv->cfg->ops->lib->dump_fh)
936                 priv->cfg->ops->lib->dump_fh(priv, NULL, false);
937         priv->cfg->ops->lib->dump_nic_event_log(priv, false, NULL, false);
938 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
939         if (iwl_legacy_get_debug_level(priv) & IWL_DL_FW_ERRORS)
940                 iwl_legacy_print_rx_config_cmd(priv,
941                                         &priv->contexts[IWL_RXON_CTX_BSS]);
942 #endif
943
944         wake_up_interruptible(&priv->wait_command_queue);
945
946         /* Keep the restart process from trying to send host
947          * commands by clearing the INIT status bit */
948         clear_bit(STATUS_READY, &priv->status);
949
950         if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
951                 IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
952                           "Restarting adapter due to uCode error.\n");
953
954                 if (priv->cfg->mod_params->restart_fw)
955                         queue_work(priv->workqueue, &priv->restart);
956         }
957 }
958 EXPORT_SYMBOL(iwl_legacy_irq_handle_error);
959
960 static int iwl_legacy_apm_stop_master(struct iwl_priv *priv)
961 {
962         int ret = 0;
963
964         /* stop device's busmaster DMA activity */
965         iwl_legacy_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
966
967         ret = iwl_poll_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
968                         CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
969         if (ret)
970                 IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
971
972         IWL_DEBUG_INFO(priv, "stop master\n");
973
974         return ret;
975 }
976
977 void iwl_legacy_apm_stop(struct iwl_priv *priv)
978 {
979         IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
980
981         /* Stop device's DMA activity */
982         iwl_legacy_apm_stop_master(priv);
983
984         /* Reset the entire device */
985         iwl_legacy_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
986
987         udelay(10);
988
989         /*
990          * Clear "initialization complete" bit to move adapter from
991          * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
992          */
993         iwl_legacy_clear_bit(priv, CSR_GP_CNTRL,
994                                 CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
995 }
996 EXPORT_SYMBOL(iwl_legacy_apm_stop);
997
998
999 /*
1000  * Start up NIC's basic functionality after it has been reset
1001  * (e.g. after platform boot, or shutdown via iwl_legacy_apm_stop())
1002  * NOTE:  This does not load uCode nor start the embedded processor
1003  */
1004 int iwl_legacy_apm_init(struct iwl_priv *priv)
1005 {
1006         int ret = 0;
1007         u16 lctl;
1008
1009         IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
1010
1011         /*
1012          * Use "set_bit" below rather than "write", to preserve any hardware
1013          * bits already set by default after reset.
1014          */
1015
1016         /* Disable L0S exit timer (platform NMI Work/Around) */
1017         iwl_legacy_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1018                           CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
1019
1020         /*
1021          * Disable L0s without affecting L1;
1022          *  don't wait for ICH L0s (ICH bug W/A)
1023          */
1024         iwl_legacy_set_bit(priv, CSR_GIO_CHICKEN_BITS,
1025                           CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
1026
1027         /* Set FH wait threshold to maximum (HW error during stress W/A) */
1028         iwl_legacy_set_bit(priv, CSR_DBG_HPET_MEM_REG,
1029                                         CSR_DBG_HPET_MEM_REG_VAL);
1030
1031         /*
1032          * Enable HAP INTA (interrupt from management bus) to
1033          * wake device's PCI Express link L1a -> L0s
1034          * NOTE:  This is no-op for 3945 (non-existant bit)
1035          */
1036         iwl_legacy_set_bit(priv, CSR_HW_IF_CONFIG_REG,
1037                                     CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
1038
1039         /*
1040          * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
1041          * Check if BIOS (or OS) enabled L1-ASPM on this device.
1042          * If so (likely), disable L0S, so device moves directly L0->L1;
1043          *    costs negligible amount of power savings.
1044          * If not (unlikely), enable L0S, so there is at least some
1045          *    power savings, even without L1.
1046          */
1047         if (priv->cfg->base_params->set_l0s) {
1048                 lctl = iwl_legacy_pcie_link_ctl(priv);
1049                 if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
1050                                         PCI_CFG_LINK_CTRL_VAL_L1_EN) {
1051                         /* L1-ASPM enabled; disable(!) L0S  */
1052                         iwl_legacy_set_bit(priv, CSR_GIO_REG,
1053                                         CSR_GIO_REG_VAL_L0S_ENABLED);
1054                         IWL_DEBUG_POWER(priv, "L1 Enabled; Disabling L0S\n");
1055                 } else {
1056                         /* L1-ASPM disabled; enable(!) L0S */
1057                         iwl_legacy_clear_bit(priv, CSR_GIO_REG,
1058                                         CSR_GIO_REG_VAL_L0S_ENABLED);
1059                         IWL_DEBUG_POWER(priv, "L1 Disabled; Enabling L0S\n");
1060                 }
1061         }
1062
1063         /* Configure analog phase-lock-loop before activating to D0A */
1064         if (priv->cfg->base_params->pll_cfg_val)
1065                 iwl_legacy_set_bit(priv, CSR_ANA_PLL_CFG,
1066                             priv->cfg->base_params->pll_cfg_val);
1067
1068         /*
1069          * Set "initialization complete" bit to move adapter from
1070          * D0U* --> D0A* (powered-up active) state.
1071          */
1072         iwl_legacy_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
1073
1074         /*
1075          * Wait for clock stabilization; once stabilized, access to
1076          * device-internal resources is supported, e.g. iwl_legacy_write_prph()
1077          * and accesses to uCode SRAM.
1078          */
1079         ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
1080                         CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
1081                         CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
1082         if (ret < 0) {
1083                 IWL_DEBUG_INFO(priv, "Failed to init the card\n");
1084                 goto out;
1085         }
1086
1087         /*
1088          * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
1089          * BSM (Boostrap State Machine) is only in 3945 and 4965.
1090          *
1091          * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
1092          * do not disable clocks.  This preserves any hardware bits already
1093          * set by default in "CLK_CTRL_REG" after reset.
1094          */
1095         if (priv->cfg->base_params->use_bsm)
1096                 iwl_legacy_write_prph(priv, APMG_CLK_EN_REG,
1097                         APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
1098         else
1099                 iwl_legacy_write_prph(priv, APMG_CLK_EN_REG,
1100                         APMG_CLK_VAL_DMA_CLK_RQT);
1101         udelay(20);
1102
1103         /* Disable L1-Active */
1104         iwl_legacy_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
1105                           APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
1106
1107 out:
1108         return ret;
1109 }
1110 EXPORT_SYMBOL(iwl_legacy_apm_init);
1111
1112
1113 int iwl_legacy_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
1114 {
1115         int ret;
1116         s8 prev_tx_power;
1117         bool defer;
1118         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
1119
1120         lockdep_assert_held(&priv->mutex);
1121
1122         if (priv->tx_power_user_lmt == tx_power && !force)
1123                 return 0;
1124
1125         if (!priv->cfg->ops->lib->send_tx_power)
1126                 return -EOPNOTSUPP;
1127
1128         if (tx_power < IWL4965_TX_POWER_TARGET_POWER_MIN) {
1129                 IWL_WARN(priv,
1130                          "Requested user TXPOWER %d below lower limit %d.\n",
1131                          tx_power,
1132                          IWL4965_TX_POWER_TARGET_POWER_MIN);
1133                 return -EINVAL;
1134         }
1135
1136         if (tx_power > priv->tx_power_device_lmt) {
1137                 IWL_WARN(priv,
1138                         "Requested user TXPOWER %d above upper limit %d.\n",
1139                          tx_power, priv->tx_power_device_lmt);
1140                 return -EINVAL;
1141         }
1142
1143         if (!iwl_legacy_is_ready_rf(priv))
1144                 return -EIO;
1145
1146         /* scan complete and commit_rxon use tx_power_next value,
1147          * it always need to be updated for newest request */
1148         priv->tx_power_next = tx_power;
1149
1150         /* do not set tx power when scanning or channel changing */
1151         defer = test_bit(STATUS_SCANNING, &priv->status) ||
1152                 memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
1153         if (defer && !force) {
1154                 IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
1155                 return 0;
1156         }
1157
1158         prev_tx_power = priv->tx_power_user_lmt;
1159         priv->tx_power_user_lmt = tx_power;
1160
1161         ret = priv->cfg->ops->lib->send_tx_power(priv);
1162
1163         /* if fail to set tx_power, restore the orig. tx power */
1164         if (ret) {
1165                 priv->tx_power_user_lmt = prev_tx_power;
1166                 priv->tx_power_next = prev_tx_power;
1167         }
1168         return ret;
1169 }
1170 EXPORT_SYMBOL(iwl_legacy_set_tx_power);
1171
1172 void iwl_legacy_send_bt_config(struct iwl_priv *priv)
1173 {
1174         struct iwl_bt_cmd bt_cmd = {
1175                 .lead_time = BT_LEAD_TIME_DEF,
1176                 .max_kill = BT_MAX_KILL_DEF,
1177                 .kill_ack_mask = 0,
1178                 .kill_cts_mask = 0,
1179         };
1180
1181         if (!bt_coex_active)
1182                 bt_cmd.flags = BT_COEX_DISABLE;
1183         else
1184                 bt_cmd.flags = BT_COEX_ENABLE;
1185
1186         IWL_DEBUG_INFO(priv, "BT coex %s\n",
1187                 (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
1188
1189         if (iwl_legacy_send_cmd_pdu(priv, REPLY_BT_CONFIG,
1190                              sizeof(struct iwl_bt_cmd), &bt_cmd))
1191                 IWL_ERR(priv, "failed to send BT Coex Config\n");
1192 }
1193 EXPORT_SYMBOL(iwl_legacy_send_bt_config);
1194
1195 int iwl_legacy_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
1196 {
1197         struct iwl_statistics_cmd statistics_cmd = {
1198                 .configuration_flags =
1199                         clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
1200         };
1201
1202         if (flags & CMD_ASYNC)
1203                 return iwl_legacy_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
1204                                         sizeof(struct iwl_statistics_cmd),
1205                                         &statistics_cmd, NULL);
1206         else
1207                 return iwl_legacy_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
1208                                         sizeof(struct iwl_statistics_cmd),
1209                                         &statistics_cmd);
1210 }
1211 EXPORT_SYMBOL(iwl_legacy_send_statistics_request);
1212
1213 void iwl_legacy_rx_pm_sleep_notif(struct iwl_priv *priv,
1214                            struct iwl_rx_mem_buffer *rxb)
1215 {
1216 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
1217         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1218         struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
1219         IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
1220                      sleep->pm_sleep_mode, sleep->pm_wakeup_src);
1221 #endif
1222 }
1223 EXPORT_SYMBOL(iwl_legacy_rx_pm_sleep_notif);
1224
1225 void iwl_legacy_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
1226                                       struct iwl_rx_mem_buffer *rxb)
1227 {
1228         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1229         u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
1230         IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
1231                         "notification for %s:\n", len,
1232                         iwl_legacy_get_cmd_string(pkt->hdr.cmd));
1233         iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
1234 }
1235 EXPORT_SYMBOL(iwl_legacy_rx_pm_debug_statistics_notif);
1236
1237 void iwl_legacy_rx_reply_error(struct iwl_priv *priv,
1238                         struct iwl_rx_mem_buffer *rxb)
1239 {
1240         struct iwl_rx_packet *pkt = rxb_addr(rxb);
1241
1242         IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
1243                 "seq 0x%04X ser 0x%08X\n",
1244                 le32_to_cpu(pkt->u.err_resp.error_type),
1245                 iwl_legacy_get_cmd_string(pkt->u.err_resp.cmd_id),
1246                 pkt->u.err_resp.cmd_id,
1247                 le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
1248                 le32_to_cpu(pkt->u.err_resp.error_info));
1249 }
1250 EXPORT_SYMBOL(iwl_legacy_rx_reply_error);
1251
1252 void iwl_legacy_clear_isr_stats(struct iwl_priv *priv)
1253 {
1254         memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
1255 }
1256
1257 int iwl_legacy_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
1258                            const struct ieee80211_tx_queue_params *params)
1259 {
1260         struct iwl_priv *priv = hw->priv;
1261         struct iwl_rxon_context *ctx;
1262         unsigned long flags;
1263         int q;
1264
1265         IWL_DEBUG_MAC80211(priv, "enter\n");
1266
1267         if (!iwl_legacy_is_ready_rf(priv)) {
1268                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
1269                 return -EIO;
1270         }
1271
1272         if (queue >= AC_NUM) {
1273                 IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
1274                 return 0;
1275         }
1276
1277         q = AC_NUM - 1 - queue;
1278
1279         spin_lock_irqsave(&priv->lock, flags);
1280
1281         for_each_context(priv, ctx) {
1282                 ctx->qos_data.def_qos_parm.ac[q].cw_min =
1283                         cpu_to_le16(params->cw_min);
1284                 ctx->qos_data.def_qos_parm.ac[q].cw_max =
1285                         cpu_to_le16(params->cw_max);
1286                 ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
1287                 ctx->qos_data.def_qos_parm.ac[q].edca_txop =
1288                                 cpu_to_le16((params->txop * 32));
1289
1290                 ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
1291         }
1292
1293         spin_unlock_irqrestore(&priv->lock, flags);
1294
1295         IWL_DEBUG_MAC80211(priv, "leave\n");
1296         return 0;
1297 }
1298 EXPORT_SYMBOL(iwl_legacy_mac_conf_tx);
1299
1300 int iwl_legacy_mac_tx_last_beacon(struct ieee80211_hw *hw)
1301 {
1302         struct iwl_priv *priv = hw->priv;
1303
1304         return priv->ibss_manager == IWL_IBSS_MANAGER;
1305 }
1306 EXPORT_SYMBOL_GPL(iwl_legacy_mac_tx_last_beacon);
1307
1308 static int
1309 iwl_legacy_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
1310 {
1311         iwl_legacy_connection_init_rx_config(priv, ctx);
1312
1313         if (priv->cfg->ops->hcmd->set_rxon_chain)
1314                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
1315
1316         return iwl_legacy_commit_rxon(priv, ctx);
1317 }
1318
1319 static int iwl_legacy_setup_interface(struct iwl_priv *priv,
1320                                struct iwl_rxon_context *ctx)
1321 {
1322         struct ieee80211_vif *vif = ctx->vif;
1323         int err;
1324
1325         lockdep_assert_held(&priv->mutex);
1326
1327         /*
1328          * This variable will be correct only when there's just
1329          * a single context, but all code using it is for hardware
1330          * that supports only one context.
1331          */
1332         priv->iw_mode = vif->type;
1333
1334         ctx->is_active = true;
1335
1336         err = iwl_legacy_set_mode(priv, ctx);
1337         if (err) {
1338                 if (!ctx->always_active)
1339                         ctx->is_active = false;
1340                 return err;
1341         }
1342
1343         return 0;
1344 }
1345
1346 int
1347 iwl_legacy_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
1348 {
1349         struct iwl_priv *priv = hw->priv;
1350         struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1351         struct iwl_rxon_context *tmp, *ctx = NULL;
1352         int err;
1353
1354         IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
1355                            vif->type, vif->addr);
1356
1357         mutex_lock(&priv->mutex);
1358
1359         if (!iwl_legacy_is_ready_rf(priv)) {
1360                 IWL_WARN(priv, "Try to add interface when device not ready\n");
1361                 err = -EINVAL;
1362                 goto out;
1363         }
1364
1365         for_each_context(priv, tmp) {
1366                 u32 possible_modes =
1367                         tmp->interface_modes | tmp->exclusive_interface_modes;
1368
1369                 if (tmp->vif) {
1370                         /* check if this busy context is exclusive */
1371                         if (tmp->exclusive_interface_modes &
1372                                                 BIT(tmp->vif->type)) {
1373                                 err = -EINVAL;
1374                                 goto out;
1375                         }
1376                         continue;
1377                 }
1378
1379                 if (!(possible_modes & BIT(vif->type)))
1380                         continue;
1381
1382                 /* have maybe usable context w/o interface */
1383                 ctx = tmp;
1384                 break;
1385         }
1386
1387         if (!ctx) {
1388                 err = -EOPNOTSUPP;
1389                 goto out;
1390         }
1391
1392         vif_priv->ctx = ctx;
1393         ctx->vif = vif;
1394
1395         err = iwl_legacy_setup_interface(priv, ctx);
1396         if (!err)
1397                 goto out;
1398
1399         ctx->vif = NULL;
1400         priv->iw_mode = NL80211_IFTYPE_STATION;
1401  out:
1402         mutex_unlock(&priv->mutex);
1403
1404         IWL_DEBUG_MAC80211(priv, "leave\n");
1405         return err;
1406 }
1407 EXPORT_SYMBOL(iwl_legacy_mac_add_interface);
1408
1409 static void iwl_legacy_teardown_interface(struct iwl_priv *priv,
1410                                    struct ieee80211_vif *vif,
1411                                    bool mode_change)
1412 {
1413         struct iwl_rxon_context *ctx = iwl_legacy_rxon_ctx_from_vif(vif);
1414
1415         lockdep_assert_held(&priv->mutex);
1416
1417         if (priv->scan_vif == vif) {
1418                 iwl_legacy_scan_cancel_timeout(priv, 200);
1419                 iwl_legacy_force_scan_end(priv);
1420         }
1421
1422         if (!mode_change) {
1423                 iwl_legacy_set_mode(priv, ctx);
1424                 if (!ctx->always_active)
1425                         ctx->is_active = false;
1426         }
1427 }
1428
1429 void iwl_legacy_mac_remove_interface(struct ieee80211_hw *hw,
1430                               struct ieee80211_vif *vif)
1431 {
1432         struct iwl_priv *priv = hw->priv;
1433         struct iwl_rxon_context *ctx = iwl_legacy_rxon_ctx_from_vif(vif);
1434
1435         IWL_DEBUG_MAC80211(priv, "enter\n");
1436
1437         mutex_lock(&priv->mutex);
1438
1439         WARN_ON(ctx->vif != vif);
1440         ctx->vif = NULL;
1441
1442         iwl_legacy_teardown_interface(priv, vif, false);
1443
1444         memset(priv->bssid, 0, ETH_ALEN);
1445         mutex_unlock(&priv->mutex);
1446
1447         IWL_DEBUG_MAC80211(priv, "leave\n");
1448
1449 }
1450 EXPORT_SYMBOL(iwl_legacy_mac_remove_interface);
1451
1452 int iwl_legacy_alloc_txq_mem(struct iwl_priv *priv)
1453 {
1454         if (!priv->txq)
1455                 priv->txq = kzalloc(
1456                         sizeof(struct iwl_tx_queue) *
1457                                 priv->cfg->base_params->num_of_queues,
1458                         GFP_KERNEL);
1459         if (!priv->txq) {
1460                 IWL_ERR(priv, "Not enough memory for txq\n");
1461                 return -ENOMEM;
1462         }
1463         return 0;
1464 }
1465 EXPORT_SYMBOL(iwl_legacy_alloc_txq_mem);
1466
1467 void iwl_legacy_txq_mem(struct iwl_priv *priv)
1468 {
1469         kfree(priv->txq);
1470         priv->txq = NULL;
1471 }
1472 EXPORT_SYMBOL(iwl_legacy_txq_mem);
1473
1474 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUGFS
1475
1476 #define IWL_TRAFFIC_DUMP_SIZE   (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
1477
1478 void iwl_legacy_reset_traffic_log(struct iwl_priv *priv)
1479 {
1480         priv->tx_traffic_idx = 0;
1481         priv->rx_traffic_idx = 0;
1482         if (priv->tx_traffic)
1483                 memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1484         if (priv->rx_traffic)
1485                 memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
1486 }
1487
1488 int iwl_legacy_alloc_traffic_mem(struct iwl_priv *priv)
1489 {
1490         u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
1491
1492         if (iwl_debug_level & IWL_DL_TX) {
1493                 if (!priv->tx_traffic) {
1494                         priv->tx_traffic =
1495                                 kzalloc(traffic_size, GFP_KERNEL);
1496                         if (!priv->tx_traffic)
1497                                 return -ENOMEM;
1498                 }
1499         }
1500         if (iwl_debug_level & IWL_DL_RX) {
1501                 if (!priv->rx_traffic) {
1502                         priv->rx_traffic =
1503                                 kzalloc(traffic_size, GFP_KERNEL);
1504                         if (!priv->rx_traffic)
1505                                 return -ENOMEM;
1506                 }
1507         }
1508         iwl_legacy_reset_traffic_log(priv);
1509         return 0;
1510 }
1511 EXPORT_SYMBOL(iwl_legacy_alloc_traffic_mem);
1512
1513 void iwl_legacy_free_traffic_mem(struct iwl_priv *priv)
1514 {
1515         kfree(priv->tx_traffic);
1516         priv->tx_traffic = NULL;
1517
1518         kfree(priv->rx_traffic);
1519         priv->rx_traffic = NULL;
1520 }
1521 EXPORT_SYMBOL(iwl_legacy_free_traffic_mem);
1522
1523 void iwl_legacy_dbg_log_tx_data_frame(struct iwl_priv *priv,
1524                       u16 length, struct ieee80211_hdr *header)
1525 {
1526         __le16 fc;
1527         u16 len;
1528
1529         if (likely(!(iwl_debug_level & IWL_DL_TX)))
1530                 return;
1531
1532         if (!priv->tx_traffic)
1533                 return;
1534
1535         fc = header->frame_control;
1536         if (ieee80211_is_data(fc)) {
1537                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1538                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
1539                 memcpy((priv->tx_traffic +
1540                        (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1541                        header, len);
1542                 priv->tx_traffic_idx =
1543                         (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1544         }
1545 }
1546 EXPORT_SYMBOL(iwl_legacy_dbg_log_tx_data_frame);
1547
1548 void iwl_legacy_dbg_log_rx_data_frame(struct iwl_priv *priv,
1549                       u16 length, struct ieee80211_hdr *header)
1550 {
1551         __le16 fc;
1552         u16 len;
1553
1554         if (likely(!(iwl_debug_level & IWL_DL_RX)))
1555                 return;
1556
1557         if (!priv->rx_traffic)
1558                 return;
1559
1560         fc = header->frame_control;
1561         if (ieee80211_is_data(fc)) {
1562                 len = (length > IWL_TRAFFIC_ENTRY_SIZE)
1563                        ? IWL_TRAFFIC_ENTRY_SIZE : length;
1564                 memcpy((priv->rx_traffic +
1565                        (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
1566                        header, len);
1567                 priv->rx_traffic_idx =
1568                         (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
1569         }
1570 }
1571 EXPORT_SYMBOL(iwl_legacy_dbg_log_rx_data_frame);
1572
1573 const char *iwl_legacy_get_mgmt_string(int cmd)
1574 {
1575         switch (cmd) {
1576                 IWL_CMD(MANAGEMENT_ASSOC_REQ);
1577                 IWL_CMD(MANAGEMENT_ASSOC_RESP);
1578                 IWL_CMD(MANAGEMENT_REASSOC_REQ);
1579                 IWL_CMD(MANAGEMENT_REASSOC_RESP);
1580                 IWL_CMD(MANAGEMENT_PROBE_REQ);
1581                 IWL_CMD(MANAGEMENT_PROBE_RESP);
1582                 IWL_CMD(MANAGEMENT_BEACON);
1583                 IWL_CMD(MANAGEMENT_ATIM);
1584                 IWL_CMD(MANAGEMENT_DISASSOC);
1585                 IWL_CMD(MANAGEMENT_AUTH);
1586                 IWL_CMD(MANAGEMENT_DEAUTH);
1587                 IWL_CMD(MANAGEMENT_ACTION);
1588         default:
1589                 return "UNKNOWN";
1590
1591         }
1592 }
1593
1594 const char *iwl_legacy_get_ctrl_string(int cmd)
1595 {
1596         switch (cmd) {
1597                 IWL_CMD(CONTROL_BACK_REQ);
1598                 IWL_CMD(CONTROL_BACK);
1599                 IWL_CMD(CONTROL_PSPOLL);
1600                 IWL_CMD(CONTROL_RTS);
1601                 IWL_CMD(CONTROL_CTS);
1602                 IWL_CMD(CONTROL_ACK);
1603                 IWL_CMD(CONTROL_CFEND);
1604                 IWL_CMD(CONTROL_CFENDACK);
1605         default:
1606                 return "UNKNOWN";
1607
1608         }
1609 }
1610
1611 void iwl_legacy_clear_traffic_stats(struct iwl_priv *priv)
1612 {
1613         memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
1614         memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
1615 }
1616
1617 /*
1618  * if CONFIG_IWLWIFI_LEGACY_DEBUGFS defined,
1619  * iwl_legacy_update_stats function will
1620  * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass
1621  * Use debugFs to display the rx/rx_statistics
1622  * if CONFIG_IWLWIFI_LEGACY_DEBUGFS not being defined, then no MGMT and CTRL
1623  * information will be recorded, but DATA pkt still will be recorded
1624  * for the reason of iwl_led.c need to control the led blinking based on
1625  * number of tx and rx data.
1626  *
1627  */
1628 void
1629 iwl_legacy_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
1630 {
1631         struct traffic_stats    *stats;
1632
1633         if (is_tx)
1634                 stats = &priv->tx_stats;
1635         else
1636                 stats = &priv->rx_stats;
1637
1638         if (ieee80211_is_mgmt(fc)) {
1639                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1640                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
1641                         stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
1642                         break;
1643                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
1644                         stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
1645                         break;
1646                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
1647                         stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
1648                         break;
1649                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
1650                         stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
1651                         break;
1652                 case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
1653                         stats->mgmt[MANAGEMENT_PROBE_REQ]++;
1654                         break;
1655                 case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
1656                         stats->mgmt[MANAGEMENT_PROBE_RESP]++;
1657                         break;
1658                 case cpu_to_le16(IEEE80211_STYPE_BEACON):
1659                         stats->mgmt[MANAGEMENT_BEACON]++;
1660                         break;
1661                 case cpu_to_le16(IEEE80211_STYPE_ATIM):
1662                         stats->mgmt[MANAGEMENT_ATIM]++;
1663                         break;
1664                 case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
1665                         stats->mgmt[MANAGEMENT_DISASSOC]++;
1666                         break;
1667                 case cpu_to_le16(IEEE80211_STYPE_AUTH):
1668                         stats->mgmt[MANAGEMENT_AUTH]++;
1669                         break;
1670                 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
1671                         stats->mgmt[MANAGEMENT_DEAUTH]++;
1672                         break;
1673                 case cpu_to_le16(IEEE80211_STYPE_ACTION):
1674                         stats->mgmt[MANAGEMENT_ACTION]++;
1675                         break;
1676                 }
1677         } else if (ieee80211_is_ctl(fc)) {
1678                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
1679                 case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
1680                         stats->ctrl[CONTROL_BACK_REQ]++;
1681                         break;
1682                 case cpu_to_le16(IEEE80211_STYPE_BACK):
1683                         stats->ctrl[CONTROL_BACK]++;
1684                         break;
1685                 case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
1686                         stats->ctrl[CONTROL_PSPOLL]++;
1687                         break;
1688                 case cpu_to_le16(IEEE80211_STYPE_RTS):
1689                         stats->ctrl[CONTROL_RTS]++;
1690                         break;
1691                 case cpu_to_le16(IEEE80211_STYPE_CTS):
1692                         stats->ctrl[CONTROL_CTS]++;
1693                         break;
1694                 case cpu_to_le16(IEEE80211_STYPE_ACK):
1695                         stats->ctrl[CONTROL_ACK]++;
1696                         break;
1697                 case cpu_to_le16(IEEE80211_STYPE_CFEND):
1698                         stats->ctrl[CONTROL_CFEND]++;
1699                         break;
1700                 case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
1701                         stats->ctrl[CONTROL_CFENDACK]++;
1702                         break;
1703                 }
1704         } else {
1705                 /* data */
1706                 stats->data_cnt++;
1707                 stats->data_bytes += len;
1708         }
1709 }
1710 EXPORT_SYMBOL(iwl_legacy_update_stats);
1711 #endif
1712
1713 static void _iwl_legacy_force_rf_reset(struct iwl_priv *priv)
1714 {
1715         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1716                 return;
1717
1718         if (!iwl_legacy_is_any_associated(priv)) {
1719                 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
1720                 return;
1721         }
1722         /*
1723          * There is no easy and better way to force reset the radio,
1724          * the only known method is switching channel which will force to
1725          * reset and tune the radio.
1726          * Use internal short scan (single channel) operation to should
1727          * achieve this objective.
1728          * Driver should reset the radio when number of consecutive missed
1729          * beacon, or any other uCode error condition detected.
1730          */
1731         IWL_DEBUG_INFO(priv, "perform radio reset.\n");
1732         iwl_legacy_internal_short_hw_scan(priv);
1733 }
1734
1735
1736 int iwl_legacy_force_reset(struct iwl_priv *priv, int mode, bool external)
1737 {
1738         struct iwl_force_reset *force_reset;
1739
1740         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1741                 return -EINVAL;
1742
1743         if (mode >= IWL_MAX_FORCE_RESET) {
1744                 IWL_DEBUG_INFO(priv, "invalid reset request.\n");
1745                 return -EINVAL;
1746         }
1747         force_reset = &priv->force_reset[mode];
1748         force_reset->reset_request_count++;
1749         if (!external) {
1750                 if (force_reset->last_force_reset_jiffies &&
1751                     time_after(force_reset->last_force_reset_jiffies +
1752                     force_reset->reset_duration, jiffies)) {
1753                         IWL_DEBUG_INFO(priv, "force reset rejected\n");
1754                         force_reset->reset_reject_count++;
1755                         return -EAGAIN;
1756                 }
1757         }
1758         force_reset->reset_success_count++;
1759         force_reset->last_force_reset_jiffies = jiffies;
1760         IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
1761         switch (mode) {
1762         case IWL_RF_RESET:
1763                 _iwl_legacy_force_rf_reset(priv);
1764                 break;
1765         case IWL_FW_RESET:
1766                 /*
1767                  * if the request is from external(ex: debugfs),
1768                  * then always perform the request in regardless the module
1769                  * parameter setting
1770                  * if the request is from internal (uCode error or driver
1771                  * detect failure), then fw_restart module parameter
1772                  * need to be check before performing firmware reload
1773                  */
1774                 if (!external && !priv->cfg->mod_params->restart_fw) {
1775                         IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
1776                                        "module parameter setting\n");
1777                         break;
1778                 }
1779                 IWL_ERR(priv, "On demand firmware reload\n");
1780                 /* Set the FW error flag -- cleared on iwl_down */
1781                 set_bit(STATUS_FW_ERROR, &priv->status);
1782                 wake_up_interruptible(&priv->wait_command_queue);
1783                 /*
1784                  * Keep the restart process from trying to send host
1785                  * commands by clearing the INIT status bit
1786                  */
1787                 clear_bit(STATUS_READY, &priv->status);
1788                 queue_work(priv->workqueue, &priv->restart);
1789                 break;
1790         }
1791         return 0;
1792 }
1793
1794 int
1795 iwl_legacy_mac_change_interface(struct ieee80211_hw *hw,
1796                         struct ieee80211_vif *vif,
1797                         enum nl80211_iftype newtype, bool newp2p)
1798 {
1799         struct iwl_priv *priv = hw->priv;
1800         struct iwl_rxon_context *ctx = iwl_legacy_rxon_ctx_from_vif(vif);
1801         struct iwl_rxon_context *tmp;
1802         u32 interface_modes;
1803         int err;
1804
1805         newtype = ieee80211_iftype_p2p(newtype, newp2p);
1806
1807         mutex_lock(&priv->mutex);
1808
1809         interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
1810
1811         if (!(interface_modes & BIT(newtype))) {
1812                 err = -EBUSY;
1813                 goto out;
1814         }
1815
1816         if (ctx->exclusive_interface_modes & BIT(newtype)) {
1817                 for_each_context(priv, tmp) {
1818                         if (ctx == tmp)
1819                                 continue;
1820
1821                         if (!tmp->vif)
1822                                 continue;
1823
1824                         /*
1825                          * The current mode switch would be exclusive, but
1826                          * another context is active ... refuse the switch.
1827                          */
1828                         err = -EBUSY;
1829                         goto out;
1830                 }
1831         }
1832
1833         /* success */
1834         iwl_legacy_teardown_interface(priv, vif, true);
1835         vif->type = newtype;
1836         err = iwl_legacy_setup_interface(priv, ctx);
1837         WARN_ON(err);
1838         /*
1839          * We've switched internally, but submitting to the
1840          * device may have failed for some reason. Mask this
1841          * error, because otherwise mac80211 will not switch
1842          * (and set the interface type back) and we'll be
1843          * out of sync with it.
1844          */
1845         err = 0;
1846
1847  out:
1848         mutex_unlock(&priv->mutex);
1849         return err;
1850 }
1851 EXPORT_SYMBOL(iwl_legacy_mac_change_interface);
1852
1853 /*
1854  * On every watchdog tick we check (latest) time stamp. If it does not
1855  * change during timeout period and queue is not empty we reset firmware.
1856  */
1857 static int iwl_legacy_check_stuck_queue(struct iwl_priv *priv, int cnt)
1858 {
1859         struct iwl_tx_queue *txq = &priv->txq[cnt];
1860         struct iwl_queue *q = &txq->q;
1861         unsigned long timeout;
1862         int ret;
1863
1864         if (q->read_ptr == q->write_ptr) {
1865                 txq->time_stamp = jiffies;
1866                 return 0;
1867         }
1868
1869         timeout = txq->time_stamp +
1870                   msecs_to_jiffies(priv->cfg->base_params->wd_timeout);
1871
1872         if (time_after(jiffies, timeout)) {
1873                 IWL_ERR(priv, "Queue %d stuck for %u ms.\n",
1874                                 q->id, priv->cfg->base_params->wd_timeout);
1875                 ret = iwl_legacy_force_reset(priv, IWL_FW_RESET, false);
1876                 return (ret == -EAGAIN) ? 0 : 1;
1877         }
1878
1879         return 0;
1880 }
1881
1882 /*
1883  * Making watchdog tick be a quarter of timeout assure we will
1884  * discover the queue hung between timeout and 1.25*timeout
1885  */
1886 #define IWL_WD_TICK(timeout) ((timeout) / 4)
1887
1888 /*
1889  * Watchdog timer callback, we check each tx queue for stuck, if if hung
1890  * we reset the firmware. If everything is fine just rearm the timer.
1891  */
1892 void iwl_legacy_bg_watchdog(unsigned long data)
1893 {
1894         struct iwl_priv *priv = (struct iwl_priv *)data;
1895         int cnt;
1896         unsigned long timeout;
1897
1898         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
1899                 return;
1900
1901         timeout = priv->cfg->base_params->wd_timeout;
1902         if (timeout == 0)
1903                 return;
1904
1905         /* monitor and check for stuck cmd queue */
1906         if (iwl_legacy_check_stuck_queue(priv, priv->cmd_queue))
1907                 return;
1908
1909         /* monitor and check for other stuck queues */
1910         if (iwl_legacy_is_any_associated(priv)) {
1911                 for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
1912                         /* skip as we already checked the command queue */
1913                         if (cnt == priv->cmd_queue)
1914                                 continue;
1915                         if (iwl_legacy_check_stuck_queue(priv, cnt))
1916                                 return;
1917                 }
1918         }
1919
1920         mod_timer(&priv->watchdog, jiffies +
1921                   msecs_to_jiffies(IWL_WD_TICK(timeout)));
1922 }
1923 EXPORT_SYMBOL(iwl_legacy_bg_watchdog);
1924
1925 void iwl_legacy_setup_watchdog(struct iwl_priv *priv)
1926 {
1927         unsigned int timeout = priv->cfg->base_params->wd_timeout;
1928
1929         if (timeout)
1930                 mod_timer(&priv->watchdog,
1931                           jiffies + msecs_to_jiffies(IWL_WD_TICK(timeout)));
1932         else
1933                 del_timer(&priv->watchdog);
1934 }
1935 EXPORT_SYMBOL(iwl_legacy_setup_watchdog);
1936
1937 /*
1938  * extended beacon time format
1939  * time in usec will be changed into a 32-bit value in extended:internal format
1940  * the extended part is the beacon counts
1941  * the internal part is the time in usec within one beacon interval
1942  */
1943 u32
1944 iwl_legacy_usecs_to_beacons(struct iwl_priv *priv,
1945                                         u32 usec, u32 beacon_interval)
1946 {
1947         u32 quot;
1948         u32 rem;
1949         u32 interval = beacon_interval * TIME_UNIT;
1950
1951         if (!interval || !usec)
1952                 return 0;
1953
1954         quot = (usec / interval) &
1955                 (iwl_legacy_beacon_time_mask_high(priv,
1956                 priv->hw_params.beacon_time_tsf_bits) >>
1957                 priv->hw_params.beacon_time_tsf_bits);
1958         rem = (usec % interval) & iwl_legacy_beacon_time_mask_low(priv,
1959                                    priv->hw_params.beacon_time_tsf_bits);
1960
1961         return (quot << priv->hw_params.beacon_time_tsf_bits) + rem;
1962 }
1963 EXPORT_SYMBOL(iwl_legacy_usecs_to_beacons);
1964
1965 /* base is usually what we get from ucode with each received frame,
1966  * the same as HW timer counter counting down
1967  */
1968 __le32 iwl_legacy_add_beacon_time(struct iwl_priv *priv, u32 base,
1969                            u32 addon, u32 beacon_interval)
1970 {
1971         u32 base_low = base & iwl_legacy_beacon_time_mask_low(priv,
1972                                         priv->hw_params.beacon_time_tsf_bits);
1973         u32 addon_low = addon & iwl_legacy_beacon_time_mask_low(priv,
1974                                         priv->hw_params.beacon_time_tsf_bits);
1975         u32 interval = beacon_interval * TIME_UNIT;
1976         u32 res = (base & iwl_legacy_beacon_time_mask_high(priv,
1977                                 priv->hw_params.beacon_time_tsf_bits)) +
1978                                 (addon & iwl_legacy_beacon_time_mask_high(priv,
1979                                 priv->hw_params.beacon_time_tsf_bits));
1980
1981         if (base_low > addon_low)
1982                 res += base_low - addon_low;
1983         else if (base_low < addon_low) {
1984                 res += interval + base_low - addon_low;
1985                 res += (1 << priv->hw_params.beacon_time_tsf_bits);
1986         } else
1987                 res += (1 << priv->hw_params.beacon_time_tsf_bits);
1988
1989         return cpu_to_le32(res);
1990 }
1991 EXPORT_SYMBOL(iwl_legacy_add_beacon_time);
1992
1993 #ifdef CONFIG_PM
1994
1995 int iwl_legacy_pci_suspend(struct device *device)
1996 {
1997         struct pci_dev *pdev = to_pci_dev(device);
1998         struct iwl_priv *priv = pci_get_drvdata(pdev);
1999
2000         /*
2001          * This function is called when system goes into suspend state
2002          * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
2003          * first but since iwl_mac_stop() has no knowledge of who the caller is,
2004          * it will not call apm_ops.stop() to stop the DMA operation.
2005          * Calling apm_ops.stop here to make sure we stop the DMA.
2006          */
2007         iwl_legacy_apm_stop(priv);
2008
2009         return 0;
2010 }
2011 EXPORT_SYMBOL(iwl_legacy_pci_suspend);
2012
2013 int iwl_legacy_pci_resume(struct device *device)
2014 {
2015         struct pci_dev *pdev = to_pci_dev(device);
2016         struct iwl_priv *priv = pci_get_drvdata(pdev);
2017         bool hw_rfkill = false;
2018
2019         /*
2020          * We disable the RETRY_TIMEOUT register (0x41) to keep
2021          * PCI Tx retries from interfering with C3 CPU state.
2022          */
2023         pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
2024
2025         iwl_legacy_enable_interrupts(priv);
2026
2027         if (!(iwl_read32(priv, CSR_GP_CNTRL) &
2028                                 CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
2029                 hw_rfkill = true;
2030
2031         if (hw_rfkill)
2032                 set_bit(STATUS_RF_KILL_HW, &priv->status);
2033         else
2034                 clear_bit(STATUS_RF_KILL_HW, &priv->status);
2035
2036         wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rfkill);
2037
2038         return 0;
2039 }
2040 EXPORT_SYMBOL(iwl_legacy_pci_resume);
2041
2042 const struct dev_pm_ops iwl_legacy_pm_ops = {
2043         .suspend = iwl_legacy_pci_suspend,
2044         .resume = iwl_legacy_pci_resume,
2045         .freeze = iwl_legacy_pci_suspend,
2046         .thaw = iwl_legacy_pci_resume,
2047         .poweroff = iwl_legacy_pci_suspend,
2048         .restore = iwl_legacy_pci_resume,
2049 };
2050 EXPORT_SYMBOL(iwl_legacy_pm_ops);
2051
2052 #endif /* CONFIG_PM */
2053
2054 static void
2055 iwl_legacy_update_qos(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
2056 {
2057         if (test_bit(STATUS_EXIT_PENDING, &priv->status))
2058                 return;
2059
2060         if (!ctx->is_active)
2061                 return;
2062
2063         ctx->qos_data.def_qos_parm.qos_flags = 0;
2064
2065         if (ctx->qos_data.qos_active)
2066                 ctx->qos_data.def_qos_parm.qos_flags |=
2067                         QOS_PARAM_FLG_UPDATE_EDCA_MSK;
2068
2069         if (ctx->ht.enabled)
2070                 ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
2071
2072         IWL_DEBUG_QOS(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
2073                       ctx->qos_data.qos_active,
2074                       ctx->qos_data.def_qos_parm.qos_flags);
2075
2076         iwl_legacy_send_cmd_pdu_async(priv, ctx->qos_cmd,
2077                                sizeof(struct iwl_qosparam_cmd),
2078                                &ctx->qos_data.def_qos_parm, NULL);
2079 }
2080
2081 /**
2082  * iwl_legacy_mac_config - mac80211 config callback
2083  */
2084 int iwl_legacy_mac_config(struct ieee80211_hw *hw, u32 changed)
2085 {
2086         struct iwl_priv *priv = hw->priv;
2087         const struct iwl_channel_info *ch_info;
2088         struct ieee80211_conf *conf = &hw->conf;
2089         struct ieee80211_channel *channel = conf->channel;
2090         struct iwl_ht_config *ht_conf = &priv->current_ht_config;
2091         struct iwl_rxon_context *ctx;
2092         unsigned long flags = 0;
2093         int ret = 0;
2094         u16 ch;
2095         int scan_active = 0;
2096         bool ht_changed[NUM_IWL_RXON_CTX] = {};
2097
2098         if (WARN_ON(!priv->cfg->ops->legacy))
2099                 return -EOPNOTSUPP;
2100
2101         mutex_lock(&priv->mutex);
2102
2103         IWL_DEBUG_MAC80211(priv, "enter to channel %d changed 0x%X\n",
2104                                         channel->hw_value, changed);
2105
2106         if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
2107                         test_bit(STATUS_SCANNING, &priv->status))) {
2108                 scan_active = 1;
2109                 IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
2110         }
2111
2112         if (changed & (IEEE80211_CONF_CHANGE_SMPS |
2113                        IEEE80211_CONF_CHANGE_CHANNEL)) {
2114                 /* mac80211 uses static for non-HT which is what we want */
2115                 priv->current_ht_config.smps = conf->smps_mode;
2116
2117                 /*
2118                  * Recalculate chain counts.
2119                  *
2120                  * If monitor mode is enabled then mac80211 will
2121                  * set up the SM PS mode to OFF if an HT channel is
2122                  * configured.
2123                  */
2124                 if (priv->cfg->ops->hcmd->set_rxon_chain)
2125                         for_each_context(priv, ctx)
2126                                 priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
2127         }
2128
2129         /* during scanning mac80211 will delay channel setting until
2130          * scan finish with changed = 0
2131          */
2132         if (!changed || (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
2133                 if (scan_active)
2134                         goto set_ch_out;
2135
2136                 ch = channel->hw_value;
2137                 ch_info = iwl_legacy_get_channel_info(priv, channel->band, ch);
2138                 if (!iwl_legacy_is_channel_valid(ch_info)) {
2139                         IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
2140                         ret = -EINVAL;
2141                         goto set_ch_out;
2142                 }
2143
2144                 spin_lock_irqsave(&priv->lock, flags);
2145
2146                 for_each_context(priv, ctx) {
2147                         /* Configure HT40 channels */
2148                         if (ctx->ht.enabled != conf_is_ht(conf)) {
2149                                 ctx->ht.enabled = conf_is_ht(conf);
2150                                 ht_changed[ctx->ctxid] = true;
2151                         }
2152                         if (ctx->ht.enabled) {
2153                                 if (conf_is_ht40_minus(conf)) {
2154                                         ctx->ht.extension_chan_offset =
2155                                         IEEE80211_HT_PARAM_CHA_SEC_BELOW;
2156                                         ctx->ht.is_40mhz = true;
2157                                 } else if (conf_is_ht40_plus(conf)) {
2158                                         ctx->ht.extension_chan_offset =
2159                                         IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
2160                                         ctx->ht.is_40mhz = true;
2161                                 } else {
2162                                         ctx->ht.extension_chan_offset =
2163                                         IEEE80211_HT_PARAM_CHA_SEC_NONE;
2164                                         ctx->ht.is_40mhz = false;
2165                                 }
2166                         } else
2167                                 ctx->ht.is_40mhz = false;
2168
2169                         /*
2170                          * Default to no protection. Protection mode will
2171                          * later be set from BSS config in iwl_ht_conf
2172                          */
2173                         ctx->ht.protection =
2174                                         IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2175
2176                         /* if we are switching from ht to 2.4 clear flags
2177                          * from any ht related info since 2.4 does not
2178                          * support ht */
2179                         if ((le16_to_cpu(ctx->staging.channel) != ch))
2180                                 ctx->staging.flags = 0;
2181
2182                         iwl_legacy_set_rxon_channel(priv, channel, ctx);
2183                         iwl_legacy_set_rxon_ht(priv, ht_conf);
2184
2185                         iwl_legacy_set_flags_for_band(priv, ctx, channel->band,
2186                                                ctx->vif);
2187                 }
2188
2189                 spin_unlock_irqrestore(&priv->lock, flags);
2190
2191                 if (priv->cfg->ops->legacy->update_bcast_stations)
2192                         ret =
2193                         priv->cfg->ops->legacy->update_bcast_stations(priv);
2194
2195  set_ch_out:
2196                 /* The list of supported rates and rate mask can be different
2197                  * for each band; since the band may have changed, reset
2198                  * the rate mask to what mac80211 lists */
2199                 iwl_legacy_set_rate(priv);
2200         }
2201
2202         if (changed & (IEEE80211_CONF_CHANGE_PS |
2203                         IEEE80211_CONF_CHANGE_IDLE)) {
2204                 ret = iwl_legacy_power_update_mode(priv, false);
2205                 if (ret)
2206                         IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
2207         }
2208
2209         if (changed & IEEE80211_CONF_CHANGE_POWER) {
2210                 IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
2211                         priv->tx_power_user_lmt, conf->power_level);
2212
2213                 iwl_legacy_set_tx_power(priv, conf->power_level, false);
2214         }
2215
2216         if (!iwl_legacy_is_ready(priv)) {
2217                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
2218                 goto out;
2219         }
2220
2221         if (scan_active)
2222                 goto out;
2223
2224         for_each_context(priv, ctx) {
2225                 if (memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging)))
2226                         iwl_legacy_commit_rxon(priv, ctx);
2227                 else
2228                         IWL_DEBUG_INFO(priv,
2229                                 "Not re-sending same RXON configuration.\n");
2230                 if (ht_changed[ctx->ctxid])
2231                         iwl_legacy_update_qos(priv, ctx);
2232         }
2233
2234 out:
2235         IWL_DEBUG_MAC80211(priv, "leave\n");
2236         mutex_unlock(&priv->mutex);
2237         return ret;
2238 }
2239 EXPORT_SYMBOL(iwl_legacy_mac_config);
2240
2241 void iwl_legacy_mac_reset_tsf(struct ieee80211_hw *hw)
2242 {
2243         struct iwl_priv *priv = hw->priv;
2244         unsigned long flags;
2245         /* IBSS can only be the IWL_RXON_CTX_BSS context */
2246         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
2247
2248         if (WARN_ON(!priv->cfg->ops->legacy))
2249                 return;
2250
2251         mutex_lock(&priv->mutex);
2252         IWL_DEBUG_MAC80211(priv, "enter\n");
2253
2254         spin_lock_irqsave(&priv->lock, flags);
2255         memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_config));
2256         spin_unlock_irqrestore(&priv->lock, flags);
2257
2258         spin_lock_irqsave(&priv->lock, flags);
2259
2260         /* new association get rid of ibss beacon skb */
2261         if (priv->beacon_skb)
2262                 dev_kfree_skb(priv->beacon_skb);
2263
2264         priv->beacon_skb = NULL;
2265
2266         priv->timestamp = 0;
2267
2268         spin_unlock_irqrestore(&priv->lock, flags);
2269
2270         iwl_legacy_scan_cancel_timeout(priv, 100);
2271         if (!iwl_legacy_is_ready_rf(priv)) {
2272                 IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
2273                 mutex_unlock(&priv->mutex);
2274                 return;
2275         }
2276
2277         /* we are restarting association process
2278          * clear RXON_FILTER_ASSOC_MSK bit
2279          */
2280         ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2281         iwl_legacy_commit_rxon(priv, ctx);
2282
2283         iwl_legacy_set_rate(priv);
2284
2285         mutex_unlock(&priv->mutex);
2286
2287         IWL_DEBUG_MAC80211(priv, "leave\n");
2288 }
2289 EXPORT_SYMBOL(iwl_legacy_mac_reset_tsf);
2290
2291 static void iwl_legacy_ht_conf(struct iwl_priv *priv,
2292                         struct ieee80211_vif *vif)
2293 {
2294         struct iwl_ht_config *ht_conf = &priv->current_ht_config;
2295         struct ieee80211_sta *sta;
2296         struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
2297         struct iwl_rxon_context *ctx = iwl_legacy_rxon_ctx_from_vif(vif);
2298
2299         IWL_DEBUG_ASSOC(priv, "enter:\n");
2300
2301         if (!ctx->ht.enabled)
2302                 return;
2303
2304         ctx->ht.protection =
2305                 bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_PROTECTION;
2306         ctx->ht.non_gf_sta_present =
2307                 !!(bss_conf->ht_operation_mode &
2308                                 IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
2309
2310         ht_conf->single_chain_sufficient = false;
2311
2312         switch (vif->type) {
2313         case NL80211_IFTYPE_STATION:
2314                 rcu_read_lock();
2315                 sta = ieee80211_find_sta(vif, bss_conf->bssid);
2316                 if (sta) {
2317                         struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
2318                         int maxstreams;
2319
2320                         maxstreams = (ht_cap->mcs.tx_params &
2321                               IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
2322                                 >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
2323                         maxstreams += 1;
2324
2325                         if ((ht_cap->mcs.rx_mask[1] == 0) &&
2326                             (ht_cap->mcs.rx_mask[2] == 0))
2327                                 ht_conf->single_chain_sufficient = true;
2328                         if (maxstreams <= 1)
2329                                 ht_conf->single_chain_sufficient = true;
2330                 } else {
2331                         /*
2332                          * If at all, this can only happen through a race
2333                          * when the AP disconnects us while we're still
2334                          * setting up the connection, in that case mac80211
2335                          * will soon tell us about that.
2336                          */
2337                         ht_conf->single_chain_sufficient = true;
2338                 }
2339                 rcu_read_unlock();
2340                 break;
2341         case NL80211_IFTYPE_ADHOC:
2342                 ht_conf->single_chain_sufficient = true;
2343                 break;
2344         default:
2345                 break;
2346         }
2347
2348         IWL_DEBUG_ASSOC(priv, "leave\n");
2349 }
2350
2351 static inline void iwl_legacy_set_no_assoc(struct iwl_priv *priv,
2352                                     struct ieee80211_vif *vif)
2353 {
2354         struct iwl_rxon_context *ctx = iwl_legacy_rxon_ctx_from_vif(vif);
2355
2356         /*
2357          * inform the ucode that there is no longer an
2358          * association and that no more packets should be
2359          * sent
2360          */
2361         ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
2362         ctx->staging.assoc_id = 0;
2363         iwl_legacy_commit_rxon(priv, ctx);
2364 }
2365
2366 static void iwl_legacy_beacon_update(struct ieee80211_hw *hw,
2367                                   struct ieee80211_vif *vif)
2368 {
2369         struct iwl_priv *priv = hw->priv;
2370         unsigned long flags;
2371         __le64 timestamp;
2372         struct sk_buff *skb = ieee80211_beacon_get(hw, vif);
2373
2374         if (!skb)
2375                 return;
2376
2377         IWL_DEBUG_MAC80211(priv, "enter\n");
2378
2379         lockdep_assert_held(&priv->mutex);
2380
2381         if (!priv->beacon_ctx) {
2382                 IWL_ERR(priv, "update beacon but no beacon context!\n");
2383                 dev_kfree_skb(skb);
2384                 return;
2385         }
2386
2387         spin_lock_irqsave(&priv->lock, flags);
2388
2389         if (priv->beacon_skb)
2390                 dev_kfree_skb(priv->beacon_skb);
2391
2392         priv->beacon_skb = skb;
2393
2394         timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
2395         priv->timestamp = le64_to_cpu(timestamp);
2396
2397         IWL_DEBUG_MAC80211(priv, "leave\n");
2398         spin_unlock_irqrestore(&priv->lock, flags);
2399
2400         if (!iwl_legacy_is_ready_rf(priv)) {
2401                 IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
2402                 return;
2403         }
2404
2405         priv->cfg->ops->legacy->post_associate(priv);
2406 }
2407
2408 void iwl_legacy_mac_bss_info_changed(struct ieee80211_hw *hw,
2409                                      struct ieee80211_vif *vif,
2410                                      struct ieee80211_bss_conf *bss_conf,
2411                                      u32 changes)
2412 {
2413         struct iwl_priv *priv = hw->priv;
2414         struct iwl_rxon_context *ctx = iwl_legacy_rxon_ctx_from_vif(vif);
2415         int ret;
2416
2417         if (WARN_ON(!priv->cfg->ops->legacy))
2418                 return;
2419
2420         IWL_DEBUG_MAC80211(priv, "changes = 0x%X\n", changes);
2421
2422         if (!iwl_legacy_is_alive(priv))
2423                 return;
2424
2425         mutex_lock(&priv->mutex);
2426
2427         if (changes & BSS_CHANGED_QOS) {
2428                 unsigned long flags;
2429
2430                 spin_lock_irqsave(&priv->lock, flags);
2431                 ctx->qos_data.qos_active = bss_conf->qos;
2432                 iwl_legacy_update_qos(priv, ctx);
2433                 spin_unlock_irqrestore(&priv->lock, flags);
2434         }
2435
2436         if (changes & BSS_CHANGED_BEACON_ENABLED) {
2437                 /*
2438                  * the add_interface code must make sure we only ever
2439                  * have a single interface that could be beaconing at
2440                  * any time.
2441                  */
2442                 if (vif->bss_conf.enable_beacon)
2443                         priv->beacon_ctx = ctx;
2444                 else
2445                         priv->beacon_ctx = NULL;
2446         }
2447
2448         if (changes & BSS_CHANGED_BSSID) {
2449                 IWL_DEBUG_MAC80211(priv, "BSSID %pM\n", bss_conf->bssid);
2450
2451                 /*
2452                  * If there is currently a HW scan going on in the
2453                  * background then we need to cancel it else the RXON
2454                  * below/in post_associate will fail.
2455                  */
2456                 if (iwl_legacy_scan_cancel_timeout(priv, 100)) {
2457                         IWL_WARN(priv,
2458                                 "Aborted scan still in progress after 100ms\n");
2459                         IWL_DEBUG_MAC80211(priv,
2460                                 "leaving - scan abort failed.\n");
2461                         mutex_unlock(&priv->mutex);
2462                         return;
2463                 }
2464
2465                 /* mac80211 only sets assoc when in STATION mode */
2466                 if (vif->type == NL80211_IFTYPE_ADHOC || bss_conf->assoc) {
2467                         memcpy(ctx->staging.bssid_addr,
2468                                bss_conf->bssid, ETH_ALEN);
2469
2470                         /* currently needed in a few places */
2471                         memcpy(priv->bssid, bss_conf->bssid, ETH_ALEN);
2472                 } else {
2473                         ctx->staging.filter_flags &=
2474                                 ~RXON_FILTER_ASSOC_MSK;
2475                 }
2476
2477         }
2478
2479         /*
2480          * This needs to be after setting the BSSID in case
2481          * mac80211 decides to do both changes at once because
2482          * it will invoke post_associate.
2483          */
2484         if (vif->type == NL80211_IFTYPE_ADHOC && changes & BSS_CHANGED_BEACON)
2485                 iwl_legacy_beacon_update(hw, vif);
2486
2487         if (changes & BSS_CHANGED_ERP_PREAMBLE) {
2488                 IWL_DEBUG_MAC80211(priv, "ERP_PREAMBLE %d\n",
2489                                    bss_conf->use_short_preamble);
2490                 if (bss_conf->use_short_preamble)
2491                         ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
2492                 else
2493                         ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
2494         }
2495
2496         if (changes & BSS_CHANGED_ERP_CTS_PROT) {
2497                 IWL_DEBUG_MAC80211(priv,
2498                         "ERP_CTS %d\n", bss_conf->use_cts_prot);
2499                 if (bss_conf->use_cts_prot &&
2500                         (priv->band != IEEE80211_BAND_5GHZ))
2501                         ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
2502                 else
2503                         ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
2504                 if (bss_conf->use_cts_prot)
2505                         ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
2506                 else
2507                         ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
2508         }
2509
2510         if (changes & BSS_CHANGED_BASIC_RATES) {
2511                 /* XXX use this information
2512                  *
2513                  * To do that, remove code from iwl_legacy_set_rate() and put something
2514                  * like this here:
2515                  *
2516                 if (A-band)
2517                         ctx->staging.ofdm_basic_rates =
2518                                 bss_conf->basic_rates;
2519                 else
2520                         ctx->staging.ofdm_basic_rates =
2521                                 bss_conf->basic_rates >> 4;
2522                         ctx->staging.cck_basic_rates =
2523                                 bss_conf->basic_rates & 0xF;
2524                  */
2525         }
2526
2527         if (changes & BSS_CHANGED_HT) {
2528                 iwl_legacy_ht_conf(priv, vif);
2529
2530                 if (priv->cfg->ops->hcmd->set_rxon_chain)
2531                         priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
2532         }
2533
2534         if (changes & BSS_CHANGED_ASSOC) {
2535                 IWL_DEBUG_MAC80211(priv, "ASSOC %d\n", bss_conf->assoc);
2536                 if (bss_conf->assoc) {
2537                         priv->timestamp = bss_conf->timestamp;
2538
2539                         if (!iwl_legacy_is_rfkill(priv))
2540                                 priv->cfg->ops->legacy->post_associate(priv);
2541                 } else
2542                         iwl_legacy_set_no_assoc(priv, vif);
2543         }
2544
2545         if (changes && iwl_legacy_is_associated_ctx(ctx) && bss_conf->aid) {
2546                 IWL_DEBUG_MAC80211(priv, "Changes (%#x) while associated\n",
2547                                    changes);
2548                 ret = iwl_legacy_send_rxon_assoc(priv, ctx);
2549                 if (!ret) {
2550                         /* Sync active_rxon with latest change. */
2551                         memcpy((void *)&ctx->active,
2552                                 &ctx->staging,
2553                                 sizeof(struct iwl_legacy_rxon_cmd));
2554                 }
2555         }
2556
2557         if (changes & BSS_CHANGED_BEACON_ENABLED) {
2558                 if (vif->bss_conf.enable_beacon) {
2559                         memcpy(ctx->staging.bssid_addr,
2560                                bss_conf->bssid, ETH_ALEN);
2561                         memcpy(priv->bssid, bss_conf->bssid, ETH_ALEN);
2562                         priv->cfg->ops->legacy->config_ap(priv);
2563                 } else
2564                         iwl_legacy_set_no_assoc(priv, vif);
2565         }
2566
2567         if (changes & BSS_CHANGED_IBSS) {
2568                 ret = priv->cfg->ops->legacy->manage_ibss_station(priv, vif,
2569                                                         bss_conf->ibss_joined);
2570                 if (ret)
2571                         IWL_ERR(priv, "failed to %s IBSS station %pM\n",
2572                                 bss_conf->ibss_joined ? "add" : "remove",
2573                                 bss_conf->bssid);
2574         }
2575
2576         mutex_unlock(&priv->mutex);
2577
2578         IWL_DEBUG_MAC80211(priv, "leave\n");
2579 }
2580 EXPORT_SYMBOL(iwl_legacy_mac_bss_info_changed);
2581
2582 irqreturn_t iwl_legacy_isr(int irq, void *data)
2583 {
2584         struct iwl_priv *priv = data;
2585         u32 inta, inta_mask;
2586         u32 inta_fh;
2587         unsigned long flags;
2588         if (!priv)
2589                 return IRQ_NONE;
2590
2591         spin_lock_irqsave(&priv->lock, flags);
2592
2593         /* Disable (but don't clear!) interrupts here to avoid
2594          *    back-to-back ISRs and sporadic interrupts from our NIC.
2595          * If we have something to service, the tasklet will re-enable ints.
2596          * If we *don't* have something, we'll re-enable before leaving here. */
2597         inta_mask = iwl_read32(priv, CSR_INT_MASK);  /* just for debug */
2598         iwl_write32(priv, CSR_INT_MASK, 0x00000000);
2599
2600         /* Discover which interrupts are active/pending */
2601         inta = iwl_read32(priv, CSR_INT);
2602         inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
2603
2604         /* Ignore interrupt if there's nothing in NIC to service.
2605          * This may be due to IRQ shared with another device,
2606          * or due to sporadic interrupts thrown from our NIC. */
2607         if (!inta && !inta_fh) {
2608                 IWL_DEBUG_ISR(priv,
2609                         "Ignore interrupt, inta == 0, inta_fh == 0\n");
2610                 goto none;
2611         }
2612
2613         if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
2614                 /* Hardware disappeared. It might have already raised
2615                  * an interrupt */
2616                 IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
2617                 goto unplugged;
2618         }
2619
2620         IWL_DEBUG_ISR(priv, "ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
2621                       inta, inta_mask, inta_fh);
2622
2623         inta &= ~CSR_INT_BIT_SCD;
2624
2625         /* iwl_irq_tasklet() will service interrupts and re-enable them */
2626         if (likely(inta || inta_fh))
2627                 tasklet_schedule(&priv->irq_tasklet);
2628
2629 unplugged:
2630         spin_unlock_irqrestore(&priv->lock, flags);
2631         return IRQ_HANDLED;
2632
2633 none:
2634         /* re-enable interrupts here since we don't have anything to service. */
2635         /* only Re-enable if diabled by irq */
2636         if (test_bit(STATUS_INT_ENABLED, &priv->status))
2637                 iwl_legacy_enable_interrupts(priv);
2638         spin_unlock_irqrestore(&priv->lock, flags);
2639         return IRQ_NONE;
2640 }
2641 EXPORT_SYMBOL(iwl_legacy_isr);
2642
2643 /*
2644  *  iwl_legacy_tx_cmd_protection: Set rts/cts. 3945 and 4965 only share this
2645  *  function.
2646  */
2647 void iwl_legacy_tx_cmd_protection(struct iwl_priv *priv,
2648                                struct ieee80211_tx_info *info,
2649                                __le16 fc, __le32 *tx_flags)
2650 {
2651         if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
2652                 *tx_flags |= TX_CMD_FLG_RTS_MSK;
2653                 *tx_flags &= ~TX_CMD_FLG_CTS_MSK;
2654                 *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2655
2656                 if (!ieee80211_is_mgmt(fc))
2657                         return;
2658
2659                 switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
2660                 case cpu_to_le16(IEEE80211_STYPE_AUTH):
2661                 case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
2662                 case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
2663                 case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
2664                         *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2665                         *tx_flags |= TX_CMD_FLG_CTS_MSK;
2666                         break;
2667                 }
2668         } else if (info->control.rates[0].flags &
2669                    IEEE80211_TX_RC_USE_CTS_PROTECT) {
2670                 *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
2671                 *tx_flags |= TX_CMD_FLG_CTS_MSK;
2672                 *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
2673         }
2674 }
2675 EXPORT_SYMBOL(iwl_legacy_tx_cmd_protection);