iwlagn: sensitivity and chain noise done by driver
[pandora-kernel.git] / drivers / net / wireless / iwlwifi / iwl-2000.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  * The full GNU General Public License is included in this distribution in the
19  * file called LICENSE.
20  *
21  * Contact Information:
22  *  Intel Linux Wireless <ilw@linux.intel.com>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  *****************************************************************************/
26
27 #include <linux/kernel.h>
28 #include <linux/module.h>
29 #include <linux/init.h>
30 #include <linux/pci.h>
31 #include <linux/dma-mapping.h>
32 #include <linux/delay.h>
33 #include <linux/skbuff.h>
34 #include <linux/netdevice.h>
35 #include <linux/wireless.h>
36 #include <net/mac80211.h>
37 #include <linux/etherdevice.h>
38 #include <asm/unaligned.h>
39
40 #include "iwl-eeprom.h"
41 #include "iwl-dev.h"
42 #include "iwl-core.h"
43 #include "iwl-io.h"
44 #include "iwl-sta.h"
45 #include "iwl-agn.h"
46 #include "iwl-helpers.h"
47 #include "iwl-agn-hw.h"
48 #include "iwl-6000-hw.h"
49 #include "iwl-agn-led.h"
50 #include "iwl-agn-debugfs.h"
51
52 /* Highest firmware API version supported */
53 #define IWL2030_UCODE_API_MAX 5
54 #define IWL2000_UCODE_API_MAX 5
55 #define IWL200_UCODE_API_MAX 5
56
57 /* Lowest firmware API version supported */
58 #define IWL2030_UCODE_API_MIN 5
59 #define IWL2000_UCODE_API_MIN 5
60 #define IWL200_UCODE_API_MIN 5
61
62 #define IWL2030_FW_PRE "iwlwifi-2030-"
63 #define IWL2030_MODULE_FIRMWARE(api) IWL2030_FW_PRE #api ".ucode"
64
65 #define IWL2000_FW_PRE "iwlwifi-2000-"
66 #define IWL2000_MODULE_FIRMWARE(api) IWL2000_FW_PRE #api ".ucode"
67
68 #define IWL200_FW_PRE "iwlwifi-200-"
69 #define IWL200_MODULE_FIRMWARE(api) IWL200_FW_PRE #api ".ucode"
70
71 static void iwl2000_set_ct_threshold(struct iwl_priv *priv)
72 {
73         /* want Celsius */
74         priv->hw_params.ct_kill_threshold = CT_KILL_THRESHOLD;
75         priv->hw_params.ct_kill_exit_threshold = CT_KILL_EXIT_THRESHOLD;
76 }
77
78 /* NIC configuration for 2000 series */
79 static void iwl2000_nic_config(struct iwl_priv *priv)
80 {
81         u16 radio_cfg;
82
83         radio_cfg = iwl_eeprom_query16(priv, EEPROM_RADIO_CONFIG);
84
85         /* write radio config values to register */
86         if (EEPROM_RF_CFG_TYPE_MSK(radio_cfg) <= EEPROM_RF_CONFIG_TYPE_MAX)
87         iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
88                         EEPROM_RF_CFG_TYPE_MSK(radio_cfg) |
89                         EEPROM_RF_CFG_STEP_MSK(radio_cfg) |
90                         EEPROM_RF_CFG_DASH_MSK(radio_cfg));
91
92         /* set CSR_HW_CONFIG_REG for uCode use */
93         iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
94                     CSR_HW_IF_CONFIG_REG_BIT_RADIO_SI |
95                     CSR_HW_IF_CONFIG_REG_BIT_MAC_SI);
96
97         if (priv->cfg->iq_invert)
98                 iwl_set_bit(priv, CSR_GP_DRIVER_REG,
99                             CSR_GP_DRIVER_REG_BIT_RADIO_IQ_INVER);
100
101         if (priv->cfg->disable_otp_refresh)
102                 iwl_write_prph(priv, APMG_ANALOG_SVR_REG, 0x80000010);
103 }
104
105 static struct iwl_sensitivity_ranges iwl2000_sensitivity = {
106         .min_nrg_cck = 97,
107         .max_nrg_cck = 0, /* not used, set to 0 */
108         .auto_corr_min_ofdm = 80,
109         .auto_corr_min_ofdm_mrc = 128,
110         .auto_corr_min_ofdm_x1 = 105,
111         .auto_corr_min_ofdm_mrc_x1 = 192,
112
113         .auto_corr_max_ofdm = 145,
114         .auto_corr_max_ofdm_mrc = 232,
115         .auto_corr_max_ofdm_x1 = 110,
116         .auto_corr_max_ofdm_mrc_x1 = 232,
117
118         .auto_corr_min_cck = 125,
119         .auto_corr_max_cck = 175,
120         .auto_corr_min_cck_mrc = 160,
121         .auto_corr_max_cck_mrc = 310,
122         .nrg_th_cck = 97,
123         .nrg_th_ofdm = 100,
124
125         .barker_corr_th_min = 190,
126         .barker_corr_th_min_mrc = 390,
127         .nrg_th_cca = 62,
128 };
129
130 static int iwl2000_hw_set_hw_params(struct iwl_priv *priv)
131 {
132         if (priv->cfg->mod_params->num_of_queues >= IWL_MIN_NUM_QUEUES &&
133             priv->cfg->mod_params->num_of_queues <= IWLAGN_NUM_QUEUES)
134                 priv->cfg->base_params->num_of_queues =
135                         priv->cfg->mod_params->num_of_queues;
136
137         priv->hw_params.max_txq_num = priv->cfg->base_params->num_of_queues;
138         priv->hw_params.dma_chnl_num = FH50_TCSR_CHNL_NUM;
139         priv->hw_params.scd_bc_tbls_size =
140                 priv->cfg->base_params->num_of_queues *
141                 sizeof(struct iwlagn_scd_bc_tbl);
142         priv->hw_params.tfd_size = sizeof(struct iwl_tfd);
143         priv->hw_params.max_stations = IWLAGN_STATION_COUNT;
144         priv->contexts[IWL_RXON_CTX_BSS].bcast_sta_id = IWLAGN_BROADCAST_ID;
145
146         priv->hw_params.max_data_size = IWL60_RTC_DATA_SIZE;
147         priv->hw_params.max_inst_size = IWL60_RTC_INST_SIZE;
148
149         priv->hw_params.ht40_channel =  BIT(IEEE80211_BAND_2GHZ) |
150                                         BIT(IEEE80211_BAND_5GHZ);
151         priv->hw_params.rx_wrt_ptr_reg = FH_RSCSR_CHNL0_WPTR;
152
153         priv->hw_params.tx_chains_num = num_of_ant(priv->cfg->valid_tx_ant);
154         if (priv->cfg->rx_with_siso_diversity)
155                 priv->hw_params.rx_chains_num = 1;
156         else
157                 priv->hw_params.rx_chains_num =
158                         num_of_ant(priv->cfg->valid_rx_ant);
159         priv->hw_params.valid_tx_ant = priv->cfg->valid_tx_ant;
160         priv->hw_params.valid_rx_ant = priv->cfg->valid_rx_ant;
161
162         iwl2000_set_ct_threshold(priv);
163
164         /* Set initial sensitivity parameters */
165         /* Set initial calibration set */
166         priv->hw_params.sens = &iwl2000_sensitivity;
167         priv->hw_params.calib_init_cfg =
168                 BIT(IWL_CALIB_XTAL)             |
169                 BIT(IWL_CALIB_LO)               |
170                 BIT(IWL_CALIB_TX_IQ)            |
171                 BIT(IWL_CALIB_BASE_BAND);
172         if (priv->cfg->need_dc_calib)
173                 priv->hw_params.calib_rt_cfg |= BIT(IWL_CALIB_CFG_DC_IDX);
174         if (priv->cfg->need_temp_offset_calib)
175                 priv->hw_params.calib_init_cfg |= BIT(IWL_CALIB_TEMP_OFFSET);
176
177         priv->hw_params.beacon_time_tsf_bits = IWLAGN_EXT_BEACON_TIME_POS;
178
179         return 0;
180 }
181
182 static int iwl2030_hw_channel_switch(struct iwl_priv *priv,
183                                     struct ieee80211_channel_switch *ch_switch)
184 {
185         /*
186          * MULTI-FIXME
187          * See iwl_mac_channel_switch.
188          */
189         struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
190         struct iwl6000_channel_switch_cmd cmd;
191         const struct iwl_channel_info *ch_info;
192         u32 switch_time_in_usec, ucode_switch_time;
193         u16 ch;
194         u32 tsf_low;
195         u8 switch_count;
196         u16 beacon_interval = le16_to_cpu(ctx->timing.beacon_interval);
197         struct ieee80211_vif *vif = ctx->vif;
198         struct iwl_host_cmd hcmd = {
199                 .id = REPLY_CHANNEL_SWITCH,
200                 .len = sizeof(cmd),
201                 .flags = CMD_SYNC,
202                 .data = &cmd,
203         };
204
205         cmd.band = priv->band == IEEE80211_BAND_2GHZ;
206         ch = ch_switch->channel->hw_value;
207         IWL_DEBUG_11H(priv, "channel switch from %u to %u\n",
208                 ctx->active.channel, ch);
209         cmd.channel = cpu_to_le16(ch);
210         cmd.rxon_flags = ctx->staging.flags;
211         cmd.rxon_filter_flags = ctx->staging.filter_flags;
212         switch_count = ch_switch->count;
213         tsf_low = ch_switch->timestamp & 0x0ffffffff;
214         /*
215          * calculate the ucode channel switch time
216          * adding TSF as one of the factor for when to switch
217          */
218         if ((priv->ucode_beacon_time > tsf_low) && beacon_interval) {
219                 if (switch_count > ((priv->ucode_beacon_time - tsf_low) /
220                     beacon_interval)) {
221                         switch_count -= (priv->ucode_beacon_time -
222                                 tsf_low) / beacon_interval;
223                 } else
224                         switch_count = 0;
225         }
226         if (switch_count <= 1)
227                 cmd.switch_time = cpu_to_le32(priv->ucode_beacon_time);
228         else {
229                 switch_time_in_usec =
230                         vif->bss_conf.beacon_int * switch_count * TIME_UNIT;
231                 ucode_switch_time = iwl_usecs_to_beacons(priv,
232                                                 switch_time_in_usec,
233                                                 beacon_interval);
234                 cmd.switch_time = iwl_add_beacon_time(priv,
235                                                 priv->ucode_beacon_time,
236                                                 ucode_switch_time,
237                                                 beacon_interval);
238         }
239         IWL_DEBUG_11H(priv, "uCode time for the switch is 0x%x\n",
240                       cmd.switch_time);
241         ch_info = iwl_get_channel_info(priv, priv->band, ch);
242         if (ch_info)
243                 cmd.expect_beacon = is_channel_radar(ch_info);
244         else {
245                 IWL_ERR(priv, "invalid channel switch from %u to %u\n",
246                         ctx->active.channel, ch);
247                 return -EFAULT;
248         }
249         priv->switch_rxon.channel = cmd.channel;
250         priv->switch_rxon.switch_in_progress = true;
251
252         return iwl_send_cmd_sync(priv, &hcmd);
253 }
254
255 static struct iwl_lib_ops iwl2000_lib = {
256         .set_hw_params = iwl2000_hw_set_hw_params,
257         .txq_update_byte_cnt_tbl = iwlagn_txq_update_byte_cnt_tbl,
258         .txq_inval_byte_cnt_tbl = iwlagn_txq_inval_byte_cnt_tbl,
259         .txq_set_sched = iwlagn_txq_set_sched,
260         .txq_attach_buf_to_tfd = iwl_hw_txq_attach_buf_to_tfd,
261         .txq_free_tfd = iwl_hw_txq_free_tfd,
262         .txq_init = iwl_hw_tx_queue_init,
263         .rx_handler_setup = iwlagn_rx_handler_setup,
264         .setup_deferred_work = iwlagn_bt_setup_deferred_work,
265         .cancel_deferred_work = iwlagn_bt_cancel_deferred_work,
266         .is_valid_rtc_data_addr = iwlagn_hw_valid_rtc_data_addr,
267         .send_tx_power = iwlagn_send_tx_power,
268         .update_chain_flags = iwl_update_chain_flags,
269         .set_channel_switch = iwl2030_hw_channel_switch,
270         .apm_ops = {
271                 .init = iwl_apm_init,
272                 .config = iwl2000_nic_config,
273         },
274         .eeprom_ops = {
275                 .regulatory_bands = {
276                         EEPROM_REG_BAND_1_CHANNELS,
277                         EEPROM_REG_BAND_2_CHANNELS,
278                         EEPROM_REG_BAND_3_CHANNELS,
279                         EEPROM_REG_BAND_4_CHANNELS,
280                         EEPROM_REG_BAND_5_CHANNELS,
281                         EEPROM_6000_REG_BAND_24_HT40_CHANNELS,
282                         EEPROM_REG_BAND_52_HT40_CHANNELS
283                 },
284                 .acquire_semaphore = iwlcore_eeprom_acquire_semaphore,
285                 .release_semaphore = iwlcore_eeprom_release_semaphore,
286                 .calib_version  = iwlagn_eeprom_calib_version,
287                 .query_addr = iwlagn_eeprom_query_addr,
288                 .update_enhanced_txpower = iwlcore_eeprom_enhanced_txpower,
289         },
290         .temp_ops = {
291                 .temperature = iwlagn_temperature,
292         },
293         .debugfs_ops = {
294                 .rx_stats_read = iwl_ucode_rx_stats_read,
295                 .tx_stats_read = iwl_ucode_tx_stats_read,
296                 .general_stats_read = iwl_ucode_general_stats_read,
297                 .bt_stats_read = iwl_ucode_bt_stats_read,
298                 .reply_tx_error = iwl_reply_tx_error_read,
299         },
300         .txfifo_flush = iwlagn_txfifo_flush,
301         .dev_txfifo_flush = iwlagn_dev_txfifo_flush,
302         .tt_ops = {
303                 .lower_power_detection = iwl_tt_is_low_power_state,
304                 .tt_power_mode = iwl_tt_current_power_mode,
305                 .ct_kill_check = iwl_check_for_ct_kill,
306         }
307 };
308
309 static const struct iwl_ops iwl2000_ops = {
310         .lib = &iwl2000_lib,
311         .hcmd = &iwlagn_hcmd,
312         .utils = &iwlagn_hcmd_utils,
313         .led = &iwlagn_led_ops,
314         .ieee80211_ops = &iwlagn_hw_ops,
315 };
316
317 static const struct iwl_ops iwl2030_ops = {
318         .lib = &iwl2000_lib,
319         .hcmd = &iwlagn_bt_hcmd,
320         .utils = &iwlagn_hcmd_utils,
321         .led = &iwlagn_led_ops,
322         .ieee80211_ops = &iwlagn_hw_ops,
323 };
324
325 static const struct iwl_ops iwl200_ops = {
326         .lib = &iwl2000_lib,
327         .hcmd = &iwlagn_hcmd,
328         .utils = &iwlagn_hcmd_utils,
329         .led = &iwlagn_led_ops,
330         .ieee80211_ops = &iwlagn_hw_ops,
331 };
332
333 static const struct iwl_ops iwl230_ops = {
334         .lib = &iwl2000_lib,
335         .hcmd = &iwlagn_bt_hcmd,
336         .utils = &iwlagn_hcmd_utils,
337         .led = &iwlagn_led_ops,
338         .ieee80211_ops = &iwlagn_hw_ops,
339 };
340
341 static struct iwl_base_params iwl2000_base_params = {
342         .eeprom_size = OTP_LOW_IMAGE_SIZE,
343         .num_of_queues = IWLAGN_NUM_QUEUES,
344         .num_of_ampdu_queues = IWLAGN_NUM_AMPDU_QUEUES,
345         .pll_cfg_val = 0,
346         .max_ll_items = OTP_MAX_LL_ITEMS_2x00,
347         .shadow_ram_support = true,
348         .led_compensation = 51,
349         .chain_noise_num_beacons = IWL_CAL_NUM_BEACONS,
350         .adv_thermal_throttle = true,
351         .support_ct_kill_exit = true,
352         .plcp_delta_threshold = IWL_MAX_PLCP_ERR_THRESHOLD_DEF,
353         .chain_noise_scale = 1000,
354         .wd_timeout = IWL_DEF_WD_TIMEOUT,
355         .max_event_log_size = 512,
356         .ucode_tracing = true,
357         .shadow_reg_enable = true,
358 };
359
360
361 static struct iwl_base_params iwl2030_base_params = {
362         .eeprom_size = OTP_LOW_IMAGE_SIZE,
363         .num_of_queues = IWLAGN_NUM_QUEUES,
364         .num_of_ampdu_queues = IWLAGN_NUM_AMPDU_QUEUES,
365         .pll_cfg_val = 0,
366         .max_ll_items = OTP_MAX_LL_ITEMS_2x00,
367         .shadow_ram_support = true,
368         .led_compensation = 57,
369         .chain_noise_num_beacons = IWL_CAL_NUM_BEACONS,
370         .adv_thermal_throttle = true,
371         .support_ct_kill_exit = true,
372         .plcp_delta_threshold = IWL_MAX_PLCP_ERR_THRESHOLD_DEF,
373         .chain_noise_scale = 1000,
374         .wd_timeout = IWL_LONG_WD_TIMEOUT,
375         .max_event_log_size = 512,
376         .ucode_tracing = true,
377         .shadow_reg_enable = true,
378 };
379
380 static struct iwl_ht_params iwl2000_ht_params = {
381         .ht_greenfield_support = true,
382         .use_rts_for_aggregation = true, /* use rts/cts protection */
383 };
384
385 static struct iwl_bt_params iwl2030_bt_params = {
386         .bt_statistics = true,
387         /* Due to bluetooth, we transmit 2.4 GHz probes only on antenna A */
388         .advanced_bt_coexist = true,
389         .agg_time_limit = BT_AGG_THRESHOLD_DEF,
390         .bt_init_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_NONE,
391         .bt_prio_boost = IWLAGN_BT_PRIO_BOOST_DEFAULT,
392         .bt_sco_disable = true,
393         .bt_session_2 = true,
394 };
395
396 #define IWL_DEVICE_2000                                         \
397         .fw_name_pre = IWL2000_FW_PRE,                          \
398         .ucode_api_max = IWL2000_UCODE_API_MAX,                 \
399         .ucode_api_min = IWL2000_UCODE_API_MIN,                 \
400         .eeprom_ver = EEPROM_2000_EEPROM_VERSION,               \
401         .eeprom_calib_ver = EEPROM_2000_TX_POWER_VERSION,       \
402         .ops = &iwl2000_ops,                                    \
403         .mod_params = &iwlagn_mod_params,                       \
404         .base_params = &iwl2000_base_params,                    \
405         .need_dc_calib = true,                                  \
406         .need_temp_offset_calib = true,                         \
407         .led_mode = IWL_LED_RF_STATE,                           \
408         .iq_invert = true,                                      \
409         .disable_otp_refresh = true                             \
410
411 struct iwl_cfg iwl2000_2bgn_cfg = {
412         .name = "2000 Series 2x2 BGN",
413         IWL_DEVICE_2000,
414         .ht_params = &iwl2000_ht_params,
415 };
416
417 struct iwl_cfg iwl2000_2bg_cfg = {
418         .name = "2000 Series 2x2 BG",
419         IWL_DEVICE_2000,
420 };
421
422 #define IWL_DEVICE_2030                                         \
423         .fw_name_pre = IWL2030_FW_PRE,                          \
424         .ucode_api_max = IWL2030_UCODE_API_MAX,                 \
425         .ucode_api_min = IWL2030_UCODE_API_MIN,                 \
426         .eeprom_ver = EEPROM_2000_EEPROM_VERSION,               \
427         .eeprom_calib_ver = EEPROM_2000_TX_POWER_VERSION,       \
428         .ops = &iwl2030_ops,                                    \
429         .mod_params = &iwlagn_mod_params,                       \
430         .base_params = &iwl2030_base_params,                    \
431         .bt_params = &iwl2030_bt_params,                        \
432         .need_dc_calib = true,                                  \
433         .need_temp_offset_calib = true,                         \
434         .led_mode = IWL_LED_RF_STATE,                           \
435         .adv_pm = true,                                         \
436         .iq_invert = true                                       \
437
438 struct iwl_cfg iwl2030_2bgn_cfg = {
439         .name = "2000 Series 2x2 BGN/BT",
440         IWL_DEVICE_2030,
441         .ht_params = &iwl2000_ht_params,
442 };
443
444 struct iwl_cfg iwl2030_2bg_cfg = {
445         .name = "2000 Series 2x2 BG/BT",
446         IWL_DEVICE_2030,
447 };
448
449 #define IWL_DEVICE_200                                          \
450         .fw_name_pre = IWL200_FW_PRE,                           \
451         .ucode_api_max = IWL200_UCODE_API_MAX,                  \
452         .ucode_api_min = IWL200_UCODE_API_MIN,                  \
453         .eeprom_ver = EEPROM_2000_EEPROM_VERSION,               \
454         .eeprom_calib_ver = EEPROM_2000_TX_POWER_VERSION,       \
455         .ops = &iwl200_ops,                                     \
456         .mod_params = &iwlagn_mod_params,                       \
457         .base_params = &iwl2000_base_params,                    \
458         .need_dc_calib = true,                                  \
459         .need_temp_offset_calib = true,                         \
460         .led_mode = IWL_LED_RF_STATE,                           \
461         .adv_pm = true,                                         \
462         .rx_with_siso_diversity = true                          \
463
464 struct iwl_cfg iwl200_bg_cfg = {
465         .name = "200 Series 1x1 BG",
466         IWL_DEVICE_200,
467 };
468
469 struct iwl_cfg iwl200_bgn_cfg = {
470         .name = "200 Series 1x1 BGN",
471         IWL_DEVICE_200,
472         .ht_params = &iwl2000_ht_params,
473 };
474
475 #define IWL_DEVICE_230                                          \
476         .fw_name_pre = IWL200_FW_PRE,                           \
477         .ucode_api_max = IWL200_UCODE_API_MAX,                  \
478         .ucode_api_min = IWL200_UCODE_API_MIN,                  \
479         .eeprom_ver = EEPROM_2000_EEPROM_VERSION,               \
480         .eeprom_calib_ver = EEPROM_2000_TX_POWER_VERSION,       \
481         .ops = &iwl230_ops,                                     \
482         .mod_params = &iwlagn_mod_params,                       \
483         .base_params = &iwl2030_base_params,                    \
484         .bt_params = &iwl2030_bt_params,                        \
485         .need_dc_calib = true,                                  \
486         .need_temp_offset_calib = true,                         \
487         .led_mode = IWL_LED_RF_STATE,                           \
488         .adv_pm = true,                                         \
489         .rx_with_siso_diversity = true                          \
490
491 struct iwl_cfg iwl230_bg_cfg = {
492         .name = "200 Series 1x1 BG/BT",
493         IWL_DEVICE_230,
494 };
495
496 struct iwl_cfg iwl230_bgn_cfg = {
497         .name = "200 Series 1x1 BGN/BT",
498         IWL_DEVICE_230,
499         .ht_params = &iwl2000_ht_params,
500 };
501
502 MODULE_FIRMWARE(IWL2000_MODULE_FIRMWARE(IWL2000_UCODE_API_MAX));
503 MODULE_FIRMWARE(IWL2030_MODULE_FIRMWARE(IWL2030_UCODE_API_MAX));
504 MODULE_FIRMWARE(IWL200_MODULE_FIRMWARE(IWL200_UCODE_API_MAX));