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