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