]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/net/wireless/iwlwifi/iwl-core.c
c4b5c1a100f29c0de1c1e963869f54a5a0b7b648
[linux-2.6-omap-h63xx.git] / drivers / net / wireless / iwlwifi / iwl-core.c
1 /******************************************************************************
2  *
3  * GPL LICENSE SUMMARY
4  *
5  * Copyright(c) 2008 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  * Tomas Winkler <tomas.winkler@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/version.h>
32 #include <net/mac80211.h>
33
34 struct iwl_priv; /* FIXME: remove */
35 #include "iwl-debug.h"
36 #include "iwl-eeprom.h"
37 #include "iwl-dev.h" /* FIXME: remove */
38 #include "iwl-core.h"
39 #include "iwl-io.h"
40 #include "iwl-rfkill.h"
41 #include "iwl-power.h"
42
43
44 MODULE_DESCRIPTION("iwl core");
45 MODULE_VERSION(IWLWIFI_VERSION);
46 MODULE_AUTHOR(DRV_COPYRIGHT);
47 MODULE_LICENSE("GPL");
48
49 #ifdef CONFIG_IWLWIFI_DEBUG
50 u32 iwl_debug_level;
51 EXPORT_SYMBOL(iwl_debug_level);
52 #endif
53
54 #define IWL_DECLARE_RATE_INFO(r, s, ip, in, rp, rn, pp, np)    \
55         [IWL_RATE_##r##M_INDEX] = { IWL_RATE_##r##M_PLCP,      \
56                                     IWL_RATE_SISO_##s##M_PLCP, \
57                                     IWL_RATE_MIMO2_##s##M_PLCP,\
58                                     IWL_RATE_MIMO3_##s##M_PLCP,\
59                                     IWL_RATE_##r##M_IEEE,      \
60                                     IWL_RATE_##ip##M_INDEX,    \
61                                     IWL_RATE_##in##M_INDEX,    \
62                                     IWL_RATE_##rp##M_INDEX,    \
63                                     IWL_RATE_##rn##M_INDEX,    \
64                                     IWL_RATE_##pp##M_INDEX,    \
65                                     IWL_RATE_##np##M_INDEX }
66
67 /*
68  * Parameter order:
69  *   rate, ht rate, prev rate, next rate, prev tgg rate, next tgg rate
70  *
71  * If there isn't a valid next or previous rate then INV is used which
72  * maps to IWL_RATE_INVALID
73  *
74  */
75 const struct iwl4965_rate_info iwl4965_rates[IWL_RATE_COUNT] = {
76         IWL_DECLARE_RATE_INFO(1, INV, INV, 2, INV, 2, INV, 2),    /*  1mbps */
77         IWL_DECLARE_RATE_INFO(2, INV, 1, 5, 1, 5, 1, 5),          /*  2mbps */
78         IWL_DECLARE_RATE_INFO(5, INV, 2, 6, 2, 11, 2, 11),        /*5.5mbps */
79         IWL_DECLARE_RATE_INFO(11, INV, 9, 12, 9, 12, 5, 18),      /* 11mbps */
80         IWL_DECLARE_RATE_INFO(6, 6, 5, 9, 5, 11, 5, 11),        /*  6mbps */
81         IWL_DECLARE_RATE_INFO(9, 6, 6, 11, 6, 11, 5, 11),       /*  9mbps */
82         IWL_DECLARE_RATE_INFO(12, 12, 11, 18, 11, 18, 11, 18),   /* 12mbps */
83         IWL_DECLARE_RATE_INFO(18, 18, 12, 24, 12, 24, 11, 24),   /* 18mbps */
84         IWL_DECLARE_RATE_INFO(24, 24, 18, 36, 18, 36, 18, 36),   /* 24mbps */
85         IWL_DECLARE_RATE_INFO(36, 36, 24, 48, 24, 48, 24, 48),   /* 36mbps */
86         IWL_DECLARE_RATE_INFO(48, 48, 36, 54, 36, 54, 36, 54),   /* 48mbps */
87         IWL_DECLARE_RATE_INFO(54, 54, 48, INV, 48, INV, 48, INV),/* 54mbps */
88         IWL_DECLARE_RATE_INFO(60, 60, 48, INV, 48, INV, 48, INV),/* 60mbps */
89         /* FIXME:RS:          ^^    should be INV (legacy) */
90 };
91 EXPORT_SYMBOL(iwl4965_rates);
92
93 /* This function both allocates and initializes hw and priv. */
94 struct ieee80211_hw *iwl_alloc_all(struct iwl_cfg *cfg,
95                 struct ieee80211_ops *hw_ops)
96 {
97         struct iwl_priv *priv;
98
99         /* mac80211 allocates memory for this device instance, including
100          *   space for this driver's private structure */
101         struct ieee80211_hw *hw =
102                 ieee80211_alloc_hw(sizeof(struct iwl_priv), hw_ops);
103         if (hw == NULL) {
104                 IWL_ERROR("Can not allocate network device\n");
105                 goto out;
106         }
107
108         priv = hw->priv;
109         priv->hw = hw;
110
111 out:
112         return hw;
113 }
114 EXPORT_SYMBOL(iwl_alloc_all);
115
116 void iwl_hw_detect(struct iwl_priv *priv)
117 {
118         priv->hw_rev = _iwl_read32(priv, CSR_HW_REV);
119         priv->hw_wa_rev = _iwl_read32(priv, CSR_HW_REV_WA_REG);
120         pci_read_config_byte(priv->pci_dev, PCI_REVISION_ID, &priv->rev_id);
121 }
122 EXPORT_SYMBOL(iwl_hw_detect);
123
124 /**
125  * iwlcore_clear_stations_table - Clear the driver's station table
126  *
127  * NOTE:  This does not clear or otherwise alter the device's station table.
128  */
129 void iwlcore_clear_stations_table(struct iwl_priv *priv)
130 {
131         unsigned long flags;
132
133         spin_lock_irqsave(&priv->sta_lock, flags);
134
135         priv->num_stations = 0;
136         memset(priv->stations, 0, sizeof(priv->stations));
137
138         spin_unlock_irqrestore(&priv->sta_lock, flags);
139 }
140 EXPORT_SYMBOL(iwlcore_clear_stations_table);
141
142 void iwl_reset_qos(struct iwl_priv *priv)
143 {
144         u16 cw_min = 15;
145         u16 cw_max = 1023;
146         u8 aifs = 2;
147         u8 is_legacy = 0;
148         unsigned long flags;
149         int i;
150
151         spin_lock_irqsave(&priv->lock, flags);
152         priv->qos_data.qos_active = 0;
153
154         if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
155                 if (priv->qos_data.qos_enable)
156                         priv->qos_data.qos_active = 1;
157                 if (!(priv->active_rate & 0xfff0)) {
158                         cw_min = 31;
159                         is_legacy = 1;
160                 }
161         } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
162                 if (priv->qos_data.qos_enable)
163                         priv->qos_data.qos_active = 1;
164         } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
165                 cw_min = 31;
166                 is_legacy = 1;
167         }
168
169         if (priv->qos_data.qos_active)
170                 aifs = 3;
171
172         priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
173         priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
174         priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
175         priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
176         priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
177
178         if (priv->qos_data.qos_active) {
179                 i = 1;
180                 priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
181                 priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
182                 priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
183                 priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
184                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
185
186                 i = 2;
187                 priv->qos_data.def_qos_parm.ac[i].cw_min =
188                         cpu_to_le16((cw_min + 1) / 2 - 1);
189                 priv->qos_data.def_qos_parm.ac[i].cw_max =
190                         cpu_to_le16(cw_max);
191                 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
192                 if (is_legacy)
193                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
194                                 cpu_to_le16(6016);
195                 else
196                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
197                                 cpu_to_le16(3008);
198                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
199
200                 i = 3;
201                 priv->qos_data.def_qos_parm.ac[i].cw_min =
202                         cpu_to_le16((cw_min + 1) / 4 - 1);
203                 priv->qos_data.def_qos_parm.ac[i].cw_max =
204                         cpu_to_le16((cw_max + 1) / 2 - 1);
205                 priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
206                 priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
207                 if (is_legacy)
208                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
209                                 cpu_to_le16(3264);
210                 else
211                         priv->qos_data.def_qos_parm.ac[i].edca_txop =
212                                 cpu_to_le16(1504);
213         } else {
214                 for (i = 1; i < 4; i++) {
215                         priv->qos_data.def_qos_parm.ac[i].cw_min =
216                                 cpu_to_le16(cw_min);
217                         priv->qos_data.def_qos_parm.ac[i].cw_max =
218                                 cpu_to_le16(cw_max);
219                         priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
220                         priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
221                         priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
222                 }
223         }
224         IWL_DEBUG_QOS("set QoS to default \n");
225
226         spin_unlock_irqrestore(&priv->lock, flags);
227 }
228 EXPORT_SYMBOL(iwl_reset_qos);
229
230 #ifdef CONFIG_IWL4965_HT
231 static void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
232                               struct ieee80211_ht_info *ht_info,
233                               enum ieee80211_band band)
234 {
235         ht_info->cap = 0;
236         memset(ht_info->supp_mcs_set, 0, 16);
237
238         ht_info->ht_supported = 1;
239
240         if (priv->hw_params.fat_channel & BIT(band)) {
241                 ht_info->cap |= (u16)IEEE80211_HT_CAP_SUP_WIDTH;
242                 ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_40;
243                 ht_info->supp_mcs_set[4] = 0x01;
244         }
245         ht_info->cap |= (u16)IEEE80211_HT_CAP_GRN_FLD;
246         ht_info->cap |= (u16)IEEE80211_HT_CAP_SGI_20;
247         ht_info->cap |= (u16)(IEEE80211_HT_CAP_MIMO_PS &
248                              (IWL_MIMO_PS_NONE << 2));
249
250         if (priv->cfg->mod_params->amsdu_size_8K)
251                 ht_info->cap |= (u16)IEEE80211_HT_CAP_MAX_AMSDU;
252
253         ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
254         ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
255
256         ht_info->supp_mcs_set[0] = 0xFF;
257         if (priv->hw_params.tx_chains_num >= 2)
258                 ht_info->supp_mcs_set[1] = 0xFF;
259         if (priv->hw_params.tx_chains_num >= 3)
260                 ht_info->supp_mcs_set[2] = 0xFF;
261 }
262 #endif /* CONFIG_IWL4965_HT */
263
264 static void iwlcore_init_hw_rates(struct iwl_priv *priv,
265                               struct ieee80211_rate *rates)
266 {
267         int i;
268
269         for (i = 0; i < IWL_RATE_COUNT; i++) {
270                 rates[i].bitrate = iwl4965_rates[i].ieee * 5;
271                 rates[i].hw_value = i; /* Rate scaling will work on indexes */
272                 rates[i].hw_value_short = i;
273                 rates[i].flags = 0;
274                 if ((i > IWL_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
275                         /*
276                          * If CCK != 1M then set short preamble rate flag.
277                          */
278                         rates[i].flags |=
279                                 (iwl4965_rates[i].plcp == IWL_RATE_1M_PLCP) ?
280                                         0 : IEEE80211_RATE_SHORT_PREAMBLE;
281                 }
282         }
283 }
284
285 /**
286  * iwlcore_init_geos - Initialize mac80211's geo/channel info based from eeprom
287  */
288 static int iwlcore_init_geos(struct iwl_priv *priv)
289 {
290         struct iwl_channel_info *ch;
291         struct ieee80211_supported_band *sband;
292         struct ieee80211_channel *channels;
293         struct ieee80211_channel *geo_ch;
294         struct ieee80211_rate *rates;
295         int i = 0;
296
297         if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
298             priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
299                 IWL_DEBUG_INFO("Geography modes already initialized.\n");
300                 set_bit(STATUS_GEO_CONFIGURED, &priv->status);
301                 return 0;
302         }
303
304         channels = kzalloc(sizeof(struct ieee80211_channel) *
305                            priv->channel_count, GFP_KERNEL);
306         if (!channels)
307                 return -ENOMEM;
308
309         rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
310                         GFP_KERNEL);
311         if (!rates) {
312                 kfree(channels);
313                 return -ENOMEM;
314         }
315
316         /* 5.2GHz channels start after the 2.4GHz channels */
317         sband = &priv->bands[IEEE80211_BAND_5GHZ];
318         sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
319         /* just OFDM */
320         sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
321         sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
322
323         iwlcore_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_5GHZ);
324
325         sband = &priv->bands[IEEE80211_BAND_2GHZ];
326         sband->channels = channels;
327         /* OFDM & CCK */
328         sband->bitrates = rates;
329         sband->n_bitrates = IWL_RATE_COUNT;
330
331         iwlcore_init_ht_hw_capab(priv, &sband->ht_info, IEEE80211_BAND_2GHZ);
332
333         priv->ieee_channels = channels;
334         priv->ieee_rates = rates;
335
336         iwlcore_init_hw_rates(priv, rates);
337
338         for (i = 0;  i < priv->channel_count; i++) {
339                 ch = &priv->channel_info[i];
340
341                 /* FIXME: might be removed if scan is OK */
342                 if (!is_channel_valid(ch))
343                         continue;
344
345                 if (is_channel_a_band(ch))
346                         sband =  &priv->bands[IEEE80211_BAND_5GHZ];
347                 else
348                         sband =  &priv->bands[IEEE80211_BAND_2GHZ];
349
350                 geo_ch = &sband->channels[sband->n_channels++];
351
352                 geo_ch->center_freq =
353                                 ieee80211_channel_to_frequency(ch->channel);
354                 geo_ch->max_power = ch->max_power_avg;
355                 geo_ch->max_antenna_gain = 0xff;
356                 geo_ch->hw_value = ch->channel;
357
358                 if (is_channel_valid(ch)) {
359                         if (!(ch->flags & EEPROM_CHANNEL_IBSS))
360                                 geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
361
362                         if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
363                                 geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
364
365                         if (ch->flags & EEPROM_CHANNEL_RADAR)
366                                 geo_ch->flags |= IEEE80211_CHAN_RADAR;
367
368                         if (ch->max_power_avg > priv->max_channel_txpower_limit)
369                                 priv->max_channel_txpower_limit =
370                                     ch->max_power_avg;
371                 } else {
372                         geo_ch->flags |= IEEE80211_CHAN_DISABLED;
373                 }
374
375                 /* Save flags for reg domain usage */
376                 geo_ch->orig_flags = geo_ch->flags;
377
378                 IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
379                                 ch->channel, geo_ch->center_freq,
380                                 is_channel_a_band(ch) ?  "5.2" : "2.4",
381                                 geo_ch->flags & IEEE80211_CHAN_DISABLED ?
382                                 "restricted" : "valid",
383                                  geo_ch->flags);
384         }
385
386         if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
387              priv->cfg->sku & IWL_SKU_A) {
388                 printk(KERN_INFO DRV_NAME
389                        ": Incorrectly detected BG card as ABG.  Please send "
390                        "your PCI ID 0x%04X:0x%04X to maintainer.\n",
391                        priv->pci_dev->device, priv->pci_dev->subsystem_device);
392                 priv->cfg->sku &= ~IWL_SKU_A;
393         }
394
395         printk(KERN_INFO DRV_NAME
396                ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
397                priv->bands[IEEE80211_BAND_2GHZ].n_channels,
398                priv->bands[IEEE80211_BAND_5GHZ].n_channels);
399
400         if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
401                 priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
402                         &priv->bands[IEEE80211_BAND_2GHZ];
403         if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
404                 priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
405                         &priv->bands[IEEE80211_BAND_5GHZ];
406
407         set_bit(STATUS_GEO_CONFIGURED, &priv->status);
408
409         return 0;
410 }
411
412 /*
413  * iwlcore_free_geos - undo allocations in iwlcore_init_geos
414  */
415 void iwlcore_free_geos(struct iwl_priv *priv)
416 {
417         kfree(priv->ieee_channels);
418         kfree(priv->ieee_rates);
419         clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
420 }
421 EXPORT_SYMBOL(iwlcore_free_geos);
422
423 #ifdef CONFIG_IWL4965_HT
424 static u8 is_single_rx_stream(struct iwl_priv *priv)
425 {
426         return !priv->current_ht_config.is_ht ||
427                ((priv->current_ht_config.supp_mcs_set[1] == 0) &&
428                 (priv->current_ht_config.supp_mcs_set[2] == 0)) ||
429                priv->ps_mode == IWL_MIMO_PS_STATIC;
430 }
431 #else
432 static inline u8 is_single_rx_stream(struct iwl_priv *priv)
433 {
434         return 1;
435 }
436 #endif  /*CONFIG_IWL4965_HT */
437
438 /*
439  * Determine how many receiver/antenna chains to use.
440  * More provides better reception via diversity.  Fewer saves power.
441  * MIMO (dual stream) requires at least 2, but works better with 3.
442  * This does not determine *which* chains to use, just how many.
443  */
444 static int iwlcore_get_rx_chain_counter(struct iwl_priv *priv,
445                                         u8 *idle_state, u8 *rx_state)
446 {
447         u8 is_single = is_single_rx_stream(priv);
448         u8 is_cam = test_bit(STATUS_POWER_PMI, &priv->status) ? 0 : 1;
449
450         /* # of Rx chains to use when expecting MIMO. */
451         if (is_single || (!is_cam && (priv->ps_mode == IWL_MIMO_PS_STATIC)))
452                 *rx_state = 2;
453         else
454                 *rx_state = 3;
455
456         /* # Rx chains when idling and maybe trying to save power */
457         switch (priv->ps_mode) {
458         case IWL_MIMO_PS_STATIC:
459         case IWL_MIMO_PS_DYNAMIC:
460                 *idle_state = (is_cam) ? 2 : 1;
461                 break;
462         case IWL_MIMO_PS_NONE:
463                 *idle_state = (is_cam) ? *rx_state : 1;
464                 break;
465         default:
466                 *idle_state = 1;
467                 break;
468         }
469
470         return 0;
471 }
472
473 /**
474  * iwl_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
475  *
476  * Selects how many and which Rx receivers/antennas/chains to use.
477  * This should not be used for scan command ... it puts data in wrong place.
478  */
479 void iwl_set_rxon_chain(struct iwl_priv *priv)
480 {
481         u8 is_single = is_single_rx_stream(priv);
482         u8 idle_state, rx_state;
483
484         priv->staging_rxon.rx_chain = 0;
485         rx_state = idle_state = 3;
486
487         /* Tell uCode which antennas are actually connected.
488          * Before first association, we assume all antennas are connected.
489          * Just after first association, iwl_chain_noise_calibration()
490          *    checks which antennas actually *are* connected. */
491         priv->staging_rxon.rx_chain |=
492                     cpu_to_le16(priv->hw_params.valid_rx_ant <<
493                                                  RXON_RX_CHAIN_VALID_POS);
494
495         /* How many receivers should we use? */
496         iwlcore_get_rx_chain_counter(priv, &idle_state, &rx_state);
497         priv->staging_rxon.rx_chain |=
498                 cpu_to_le16(rx_state << RXON_RX_CHAIN_MIMO_CNT_POS);
499         priv->staging_rxon.rx_chain |=
500                 cpu_to_le16(idle_state << RXON_RX_CHAIN_CNT_POS);
501
502         if (!is_single && (rx_state >= 2) &&
503             !test_bit(STATUS_POWER_PMI, &priv->status))
504                 priv->staging_rxon.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
505         else
506                 priv->staging_rxon.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
507
508         IWL_DEBUG_ASSOC("rx chain %X\n", priv->staging_rxon.rx_chain);
509 }
510 EXPORT_SYMBOL(iwl_set_rxon_chain);
511
512 /**
513  * iwlcore_set_rxon_channel - Set the phymode and channel values in staging RXON
514  * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
515  * @channel: Any channel valid for the requested phymode
516
517  * In addition to setting the staging RXON, priv->phymode is also set.
518  *
519  * NOTE:  Does not commit to the hardware; it sets appropriate bit fields
520  * in the staging RXON flag structure based on the phymode
521  */
522 int iwl_set_rxon_channel(struct iwl_priv *priv,
523                                 enum ieee80211_band band,
524                                 u16 channel)
525 {
526         if (!iwl_get_channel_info(priv, band, channel)) {
527                 IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
528                                channel, band);
529                 return -EINVAL;
530         }
531
532         if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
533             (priv->band == band))
534                 return 0;
535
536         priv->staging_rxon.channel = cpu_to_le16(channel);
537         if (band == IEEE80211_BAND_5GHZ)
538                 priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
539         else
540                 priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
541
542         priv->band = band;
543
544         IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
545
546         return 0;
547 }
548 EXPORT_SYMBOL(iwl_set_rxon_channel);
549
550 static void iwlcore_init_hw(struct iwl_priv *priv)
551 {
552         struct ieee80211_hw *hw = priv->hw;
553         hw->rate_control_algorithm = "iwl-4965-rs";
554
555         /* Tell mac80211 and its clients (e.g. Wireless Extensions)
556          *       the range of signal quality values that we'll provide.
557          * Negative values for level/noise indicate that we'll provide dBm.
558          * For WE, at least, non-0 values here *enable* display of values
559          *       in app (iwconfig). */
560         hw->max_rssi = -20; /* signal level, negative indicates dBm */
561         hw->max_noise = -20;    /* noise level, negative indicates dBm */
562         hw->max_signal = 100;   /* link quality indication (%) */
563
564         /* Tell mac80211 our Tx characteristics */
565         hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
566
567         /* Default value; 4 EDCA QOS priorities */
568         hw->queues = 4;
569 #ifdef CONFIG_IWL4965_HT
570         /* Enhanced value; more queues, to support 11n aggregation */
571         hw->ampdu_queues = 12;
572 #endif /* CONFIG_IWL4965_HT */
573 }
574
575 static int iwlcore_init_drv(struct iwl_priv *priv)
576 {
577         int ret;
578         int i;
579
580         priv->retry_rate = 1;
581         priv->ibss_beacon = NULL;
582
583         spin_lock_init(&priv->lock);
584         spin_lock_init(&priv->power_data.lock);
585         spin_lock_init(&priv->sta_lock);
586         spin_lock_init(&priv->hcmd_lock);
587         spin_lock_init(&priv->lq_mngr.lock);
588
589         for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
590                 INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
591
592         INIT_LIST_HEAD(&priv->free_frames);
593
594         mutex_init(&priv->mutex);
595
596         /* Clear the driver's (not device's) station table */
597         iwlcore_clear_stations_table(priv);
598
599         priv->data_retry_limit = -1;
600         priv->ieee_channels = NULL;
601         priv->ieee_rates = NULL;
602         priv->band = IEEE80211_BAND_2GHZ;
603
604         priv->iw_mode = IEEE80211_IF_TYPE_STA;
605
606         priv->use_ant_b_for_management_frame = 1; /* start with ant B */
607         priv->ps_mode = IWL_MIMO_PS_NONE;
608
609         /* Choose which receivers/antennas to use */
610         iwl_set_rxon_chain(priv);
611
612         iwl_reset_qos(priv);
613
614         priv->qos_data.qos_active = 0;
615         priv->qos_data.qos_cap.val = 0;
616
617         iwl_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
618
619         priv->rates_mask = IWL_RATES_MASK;
620         /* If power management is turned on, default to AC mode */
621         priv->power_mode = IWL_POWER_AC;
622         priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
623
624         ret = iwl_init_channel_map(priv);
625         if (ret) {
626                 IWL_ERROR("initializing regulatory failed: %d\n", ret);
627                 goto err;
628         }
629
630         ret = iwlcore_init_geos(priv);
631         if (ret) {
632                 IWL_ERROR("initializing geos failed: %d\n", ret);
633                 goto err_free_channel_map;
634         }
635
636         ret = ieee80211_register_hw(priv->hw);
637         if (ret) {
638                 IWL_ERROR("Failed to register network device (error %d)\n",
639                                 ret);
640                 goto err_free_geos;
641         }
642
643         priv->hw->conf.beacon_int = 100;
644         priv->mac80211_registered = 1;
645
646         return 0;
647
648 err_free_geos:
649         iwlcore_free_geos(priv);
650 err_free_channel_map:
651         iwl_free_channel_map(priv);
652 err:
653         return ret;
654 }
655
656 int iwl_setup(struct iwl_priv *priv)
657 {
658         int ret = 0;
659         iwlcore_init_hw(priv);
660         ret = iwlcore_init_drv(priv);
661         return ret;
662 }
663 EXPORT_SYMBOL(iwl_setup);
664
665 /* Low level driver call this function to update iwlcore with
666  * driver status.
667  */
668 int iwlcore_low_level_notify(struct iwl_priv *priv,
669                               enum iwlcore_card_notify notify)
670 {
671         int ret;
672         switch (notify) {
673         case IWLCORE_INIT_EVT:
674                 ret = iwl_rfkill_init(priv);
675                 if (ret)
676                         IWL_ERROR("Unable to initialize RFKILL system. "
677                                   "Ignoring error: %d\n", ret);
678                 iwl_power_initialize(priv);
679                 break;
680         case IWLCORE_START_EVT:
681                 iwl_power_update_mode(priv, 1);
682                 break;
683         case IWLCORE_STOP_EVT:
684                 break;
685         case IWLCORE_REMOVE_EVT:
686                 iwl_rfkill_unregister(priv);
687                 break;
688         }
689
690         return 0;
691 }
692 EXPORT_SYMBOL(iwlcore_low_level_notify);
693
694 int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags)
695 {
696         u32 stat_flags = 0;
697         struct iwl_host_cmd cmd = {
698                 .id = REPLY_STATISTICS_CMD,
699                 .meta.flags = flags,
700                 .len = sizeof(stat_flags),
701                 .data = (u8 *) &stat_flags,
702         };
703         return iwl_send_cmd(priv, &cmd);
704 }
705 EXPORT_SYMBOL(iwl_send_statistics_request);
706
707 /**
708  * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
709  *   using sample data 100 bytes apart.  If these sample points are good,
710  *   it's a pretty good bet that everything between them is good, too.
711  */
712 static int iwlcore_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
713 {
714         u32 val;
715         int ret = 0;
716         u32 errcnt = 0;
717         u32 i;
718
719         IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
720
721         ret = iwl_grab_nic_access(priv);
722         if (ret)
723                 return ret;
724
725         for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
726                 /* read data comes through single port, auto-incr addr */
727                 /* NOTE: Use the debugless read so we don't flood kernel log
728                  * if IWL_DL_IO is set */
729                 iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
730                         i + RTC_INST_LOWER_BOUND);
731                 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
732                 if (val != le32_to_cpu(*image)) {
733                         ret = -EIO;
734                         errcnt++;
735                         if (errcnt >= 3)
736                                 break;
737                 }
738         }
739
740         iwl_release_nic_access(priv);
741
742         return ret;
743 }
744
745 /**
746  * iwlcore_verify_inst_full - verify runtime uCode image in card vs. host,
747  *     looking at all data.
748  */
749 static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 *image,
750                                  u32 len)
751 {
752         u32 val;
753         u32 save_len = len;
754         int ret = 0;
755         u32 errcnt;
756
757         IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
758
759         ret = iwl_grab_nic_access(priv);
760         if (ret)
761                 return ret;
762
763         iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
764
765         errcnt = 0;
766         for (; len > 0; len -= sizeof(u32), image++) {
767                 /* read data comes through single port, auto-incr addr */
768                 /* NOTE: Use the debugless read so we don't flood kernel log
769                  * if IWL_DL_IO is set */
770                 val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
771                 if (val != le32_to_cpu(*image)) {
772                         IWL_ERROR("uCode INST section is invalid at "
773                                   "offset 0x%x, is 0x%x, s/b 0x%x\n",
774                                   save_len - len, val, le32_to_cpu(*image));
775                         ret = -EIO;
776                         errcnt++;
777                         if (errcnt >= 20)
778                                 break;
779                 }
780         }
781
782         iwl_release_nic_access(priv);
783
784         if (!errcnt)
785                 IWL_DEBUG_INFO
786                     ("ucode image in INSTRUCTION memory is good\n");
787
788         return ret;
789 }
790
791 /**
792  * iwl_verify_ucode - determine which instruction image is in SRAM,
793  *    and verify its contents
794  */
795 int iwl_verify_ucode(struct iwl_priv *priv)
796 {
797         __le32 *image;
798         u32 len;
799         int ret;
800
801         /* Try bootstrap */
802         image = (__le32 *)priv->ucode_boot.v_addr;
803         len = priv->ucode_boot.len;
804         ret = iwlcore_verify_inst_sparse(priv, image, len);
805         if (!ret) {
806                 IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
807                 return 0;
808         }
809
810         /* Try initialize */
811         image = (__le32 *)priv->ucode_init.v_addr;
812         len = priv->ucode_init.len;
813         ret = iwlcore_verify_inst_sparse(priv, image, len);
814         if (!ret) {
815                 IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
816                 return 0;
817         }
818
819         /* Try runtime/protocol */
820         image = (__le32 *)priv->ucode_code.v_addr;
821         len = priv->ucode_code.len;
822         ret = iwlcore_verify_inst_sparse(priv, image, len);
823         if (!ret) {
824                 IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
825                 return 0;
826         }
827
828         IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
829
830         /* Since nothing seems to match, show first several data entries in
831          * instruction SRAM, so maybe visual inspection will give a clue.
832          * Selection of bootstrap image (vs. other images) is arbitrary. */
833         image = (__le32 *)priv->ucode_boot.v_addr;
834         len = priv->ucode_boot.len;
835         ret = iwl_verify_inst_full(priv, image, len);
836
837         return ret;
838 }
839 EXPORT_SYMBOL(iwl_verify_ucode);
840