| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 1 | /****************************************************************************** | 
 | 2 |  * | 
 | 3 |  * This file is provided under a dual BSD/GPLv2 license.  When using or | 
 | 4 |  * redistributing this file, you may do so under either license. | 
 | 5 |  * | 
 | 6 |  * GPL LICENSE SUMMARY | 
 | 7 |  * | 
| Reinette Chatre | 1f44780 | 2010-01-15 13:43:41 -0800 | [diff] [blame] | 8 |  * Copyright(c) 2008 - 2010 Intel Corporation. All rights reserved. | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 9 |  * | 
 | 10 |  * This program is free software; you can redistribute it and/or modify | 
 | 11 |  * it under the terms of version 2 of the GNU General Public License as | 
 | 12 |  * published by the Free Software Foundation. | 
 | 13 |  * | 
 | 14 |  * This program is distributed in the hope that it will be useful, but | 
 | 15 |  * WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 16 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
 | 17 |  * General Public License for more details. | 
 | 18 |  * | 
 | 19 |  * You should have received a copy of the GNU General Public License | 
 | 20 |  * along with this program; if not, write to the Free Software | 
 | 21 |  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, | 
 | 22 |  * USA | 
 | 23 |  * | 
 | 24 |  * The full GNU General Public License is included in this distribution | 
 | 25 |  * in the file called LICENSE.GPL. | 
 | 26 |  * | 
 | 27 |  * Contact Information: | 
| Winkler, Tomas | 759ef89 | 2008-12-09 11:28:58 -0800 | [diff] [blame] | 28 |  *  Intel Linux Wireless <ilw@linux.intel.com> | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 29 |  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497 | 
 | 30 |  * | 
 | 31 |  * BSD LICENSE | 
 | 32 |  * | 
| Reinette Chatre | 1f44780 | 2010-01-15 13:43:41 -0800 | [diff] [blame] | 33 |  * Copyright(c) 2005 - 2010 Intel Corporation. All rights reserved. | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 34 |  * All rights reserved. | 
 | 35 |  * | 
 | 36 |  * Redistribution and use in source and binary forms, with or without | 
 | 37 |  * modification, are permitted provided that the following conditions | 
 | 38 |  * are met: | 
 | 39 |  * | 
 | 40 |  *  * Redistributions of source code must retain the above copyright | 
 | 41 |  *    notice, this list of conditions and the following disclaimer. | 
 | 42 |  *  * Redistributions in binary form must reproduce the above copyright | 
 | 43 |  *    notice, this list of conditions and the following disclaimer in | 
 | 44 |  *    the documentation and/or other materials provided with the | 
 | 45 |  *    distribution. | 
 | 46 |  *  * Neither the name Intel Corporation nor the names of its | 
 | 47 |  *    contributors may be used to endorse or promote products derived | 
 | 48 |  *    from this software without specific prior written permission. | 
 | 49 |  * | 
 | 50 |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 
 | 51 |  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 
 | 52 |  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 
 | 53 |  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 
 | 54 |  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 
 | 55 |  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 
 | 56 |  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 
 | 57 |  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 
 | 58 |  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 
 | 59 |  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
 | 60 |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
 | 61 |  *****************************************************************************/ | 
 | 62 |  | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 63 | #include <linux/slab.h> | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 64 | #include <net/mac80211.h> | 
 | 65 |  | 
| Tomas Winkler | 3e0d4cb | 2008-04-24 11:55:38 -0700 | [diff] [blame] | 66 | #include "iwl-dev.h" | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 67 | #include "iwl-core.h" | 
| Johannes Berg | 0de7673 | 2010-09-22 18:02:11 +0200 | [diff] [blame] | 68 | #include "iwl-agn-calib.h" | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 69 |  | 
| Tomas Winkler | 6e21f2c | 2008-09-03 11:26:37 +0800 | [diff] [blame] | 70 | /***************************************************************************** | 
 | 71 |  * INIT calibrations framework | 
 | 72 |  *****************************************************************************/ | 
 | 73 |  | 
| Winkler, Tomas | 34c22cf | 2008-12-17 16:52:27 +0800 | [diff] [blame] | 74 | struct statistics_general_data { | 
 | 75 | 	u32 beacon_silence_rssi_a; | 
 | 76 | 	u32 beacon_silence_rssi_b; | 
 | 77 | 	u32 beacon_silence_rssi_c; | 
 | 78 | 	u32 beacon_energy_a; | 
 | 79 | 	u32 beacon_energy_b; | 
 | 80 | 	u32 beacon_energy_c; | 
 | 81 | }; | 
 | 82 |  | 
| Tomas Winkler | be5d56e | 2008-10-08 09:37:27 +0800 | [diff] [blame] | 83 | int iwl_send_calib_results(struct iwl_priv *priv) | 
| Tomas Winkler | 6e21f2c | 2008-09-03 11:26:37 +0800 | [diff] [blame] | 84 | { | 
 | 85 | 	int ret = 0; | 
 | 86 | 	int i = 0; | 
 | 87 |  | 
 | 88 | 	struct iwl_host_cmd hcmd = { | 
 | 89 | 		.id = REPLY_PHY_CALIBRATION_CMD, | 
| Johannes Berg | c2acea8 | 2009-07-24 11:13:05 -0700 | [diff] [blame] | 90 | 		.flags = CMD_SIZE_HUGE, | 
| Tomas Winkler | 6e21f2c | 2008-09-03 11:26:37 +0800 | [diff] [blame] | 91 | 	}; | 
 | 92 |  | 
| Tomas Winkler | be5d56e | 2008-10-08 09:37:27 +0800 | [diff] [blame] | 93 | 	for (i = 0; i < IWL_CALIB_MAX; i++) { | 
 | 94 | 		if ((BIT(i) & priv->hw_params.calib_init_cfg) && | 
 | 95 | 		    priv->calib_results[i].buf) { | 
| Tomas Winkler | 6e21f2c | 2008-09-03 11:26:37 +0800 | [diff] [blame] | 96 | 			hcmd.len = priv->calib_results[i].buf_len; | 
 | 97 | 			hcmd.data = priv->calib_results[i].buf; | 
 | 98 | 			ret = iwl_send_cmd_sync(priv, &hcmd); | 
| Wey-Yi Guy | 936e8a7 | 2010-06-14 17:09:56 -0700 | [diff] [blame] | 99 | 			if (ret) { | 
 | 100 | 				IWL_ERR(priv, "Error %d iteration %d\n", | 
 | 101 | 					ret, i); | 
 | 102 | 				break; | 
 | 103 | 			} | 
| Tomas Winkler | 6e21f2c | 2008-09-03 11:26:37 +0800 | [diff] [blame] | 104 | 		} | 
| Tomas Winkler | be5d56e | 2008-10-08 09:37:27 +0800 | [diff] [blame] | 105 | 	} | 
| Tomas Winkler | 6e21f2c | 2008-09-03 11:26:37 +0800 | [diff] [blame] | 106 |  | 
| Tomas Winkler | 6e21f2c | 2008-09-03 11:26:37 +0800 | [diff] [blame] | 107 | 	return ret; | 
 | 108 | } | 
| Tomas Winkler | 6e21f2c | 2008-09-03 11:26:37 +0800 | [diff] [blame] | 109 |  | 
 | 110 | int iwl_calib_set(struct iwl_calib_result *res, const u8 *buf, int len) | 
 | 111 | { | 
 | 112 | 	if (res->buf_len != len) { | 
 | 113 | 		kfree(res->buf); | 
 | 114 | 		res->buf = kzalloc(len, GFP_ATOMIC); | 
 | 115 | 	} | 
 | 116 | 	if (unlikely(res->buf == NULL)) | 
 | 117 | 		return -ENOMEM; | 
 | 118 |  | 
 | 119 | 	res->buf_len = len; | 
 | 120 | 	memcpy(res->buf, buf, len); | 
 | 121 | 	return 0; | 
 | 122 | } | 
| Tomas Winkler | 6e21f2c | 2008-09-03 11:26:37 +0800 | [diff] [blame] | 123 |  | 
 | 124 | void iwl_calib_free_results(struct iwl_priv *priv) | 
 | 125 | { | 
 | 126 | 	int i; | 
 | 127 |  | 
 | 128 | 	for (i = 0; i < IWL_CALIB_MAX; i++) { | 
 | 129 | 		kfree(priv->calib_results[i].buf); | 
 | 130 | 		priv->calib_results[i].buf = NULL; | 
 | 131 | 		priv->calib_results[i].buf_len = 0; | 
 | 132 | 	} | 
 | 133 | } | 
 | 134 |  | 
 | 135 | /***************************************************************************** | 
 | 136 |  * RUNTIME calibrations framework | 
 | 137 |  *****************************************************************************/ | 
 | 138 |  | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 139 | /* "false alarms" are signals that our DSP tries to lock onto, | 
 | 140 |  *   but then determines that they are either noise, or transmissions | 
 | 141 |  *   from a distant wireless network (also "noise", really) that get | 
 | 142 |  *   "stepped on" by stronger transmissions within our own network. | 
 | 143 |  * This algorithm attempts to set a sensitivity level that is high | 
 | 144 |  *   enough to receive all of our own network traffic, but not so | 
 | 145 |  *   high that our DSP gets too busy trying to lock onto non-network | 
 | 146 |  *   activity/noise. */ | 
 | 147 | static int iwl_sens_energy_cck(struct iwl_priv *priv, | 
 | 148 | 				   u32 norm_fa, | 
 | 149 | 				   u32 rx_enable_time, | 
 | 150 | 				   struct statistics_general_data *rx_info) | 
 | 151 | { | 
 | 152 | 	u32 max_nrg_cck = 0; | 
 | 153 | 	int i = 0; | 
 | 154 | 	u8 max_silence_rssi = 0; | 
 | 155 | 	u32 silence_ref = 0; | 
 | 156 | 	u8 silence_rssi_a = 0; | 
 | 157 | 	u8 silence_rssi_b = 0; | 
 | 158 | 	u8 silence_rssi_c = 0; | 
 | 159 | 	u32 val; | 
 | 160 |  | 
 | 161 | 	/* "false_alarms" values below are cross-multiplications to assess the | 
 | 162 | 	 *   numbers of false alarms within the measured period of actual Rx | 
 | 163 | 	 *   (Rx is off when we're txing), vs the min/max expected false alarms | 
 | 164 | 	 *   (some should be expected if rx is sensitive enough) in a | 
 | 165 | 	 *   hypothetical listening period of 200 time units (TU), 204.8 msec: | 
 | 166 | 	 * | 
 | 167 | 	 * MIN_FA/fixed-time < false_alarms/actual-rx-time < MAX_FA/beacon-time | 
 | 168 | 	 * | 
 | 169 | 	 * */ | 
 | 170 | 	u32 false_alarms = norm_fa * 200 * 1024; | 
 | 171 | 	u32 max_false_alarms = MAX_FA_CCK * rx_enable_time; | 
 | 172 | 	u32 min_false_alarms = MIN_FA_CCK * rx_enable_time; | 
 | 173 | 	struct iwl_sensitivity_data *data = NULL; | 
 | 174 | 	const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens; | 
 | 175 |  | 
 | 176 | 	data = &(priv->sensitivity_data); | 
 | 177 |  | 
 | 178 | 	data->nrg_auto_corr_silence_diff = 0; | 
 | 179 |  | 
 | 180 | 	/* Find max silence rssi among all 3 receivers. | 
 | 181 | 	 * This is background noise, which may include transmissions from other | 
 | 182 | 	 *    networks, measured during silence before our network's beacon */ | 
 | 183 | 	silence_rssi_a = (u8)((rx_info->beacon_silence_rssi_a & | 
 | 184 | 			    ALL_BAND_FILTER) >> 8); | 
 | 185 | 	silence_rssi_b = (u8)((rx_info->beacon_silence_rssi_b & | 
 | 186 | 			    ALL_BAND_FILTER) >> 8); | 
 | 187 | 	silence_rssi_c = (u8)((rx_info->beacon_silence_rssi_c & | 
 | 188 | 			    ALL_BAND_FILTER) >> 8); | 
 | 189 |  | 
 | 190 | 	val = max(silence_rssi_b, silence_rssi_c); | 
 | 191 | 	max_silence_rssi = max(silence_rssi_a, (u8) val); | 
 | 192 |  | 
 | 193 | 	/* Store silence rssi in 20-beacon history table */ | 
 | 194 | 	data->nrg_silence_rssi[data->nrg_silence_idx] = max_silence_rssi; | 
 | 195 | 	data->nrg_silence_idx++; | 
 | 196 | 	if (data->nrg_silence_idx >= NRG_NUM_PREV_STAT_L) | 
 | 197 | 		data->nrg_silence_idx = 0; | 
 | 198 |  | 
 | 199 | 	/* Find max silence rssi across 20 beacon history */ | 
 | 200 | 	for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) { | 
 | 201 | 		val = data->nrg_silence_rssi[i]; | 
 | 202 | 		silence_ref = max(silence_ref, val); | 
 | 203 | 	} | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 204 | 	IWL_DEBUG_CALIB(priv, "silence a %u, b %u, c %u, 20-bcn max %u\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 205 | 			silence_rssi_a, silence_rssi_b, silence_rssi_c, | 
 | 206 | 			silence_ref); | 
 | 207 |  | 
 | 208 | 	/* Find max rx energy (min value!) among all 3 receivers, | 
 | 209 | 	 *   measured during beacon frame. | 
 | 210 | 	 * Save it in 10-beacon history table. */ | 
 | 211 | 	i = data->nrg_energy_idx; | 
 | 212 | 	val = min(rx_info->beacon_energy_b, rx_info->beacon_energy_c); | 
 | 213 | 	data->nrg_value[i] = min(rx_info->beacon_energy_a, val); | 
 | 214 |  | 
 | 215 | 	data->nrg_energy_idx++; | 
 | 216 | 	if (data->nrg_energy_idx >= 10) | 
 | 217 | 		data->nrg_energy_idx = 0; | 
 | 218 |  | 
 | 219 | 	/* Find min rx energy (max value) across 10 beacon history. | 
 | 220 | 	 * This is the minimum signal level that we want to receive well. | 
 | 221 | 	 * Add backoff (margin so we don't miss slightly lower energy frames). | 
 | 222 | 	 * This establishes an upper bound (min value) for energy threshold. */ | 
 | 223 | 	max_nrg_cck = data->nrg_value[0]; | 
 | 224 | 	for (i = 1; i < 10; i++) | 
 | 225 | 		max_nrg_cck = (u32) max(max_nrg_cck, (data->nrg_value[i])); | 
 | 226 | 	max_nrg_cck += 6; | 
 | 227 |  | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 228 | 	IWL_DEBUG_CALIB(priv, "rx energy a %u, b %u, c %u, 10-bcn max/min %u\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 229 | 			rx_info->beacon_energy_a, rx_info->beacon_energy_b, | 
 | 230 | 			rx_info->beacon_energy_c, max_nrg_cck - 6); | 
 | 231 |  | 
 | 232 | 	/* Count number of consecutive beacons with fewer-than-desired | 
 | 233 | 	 *   false alarms. */ | 
 | 234 | 	if (false_alarms < min_false_alarms) | 
 | 235 | 		data->num_in_cck_no_fa++; | 
 | 236 | 	else | 
 | 237 | 		data->num_in_cck_no_fa = 0; | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 238 | 	IWL_DEBUG_CALIB(priv, "consecutive bcns with few false alarms = %u\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 239 | 			data->num_in_cck_no_fa); | 
 | 240 |  | 
 | 241 | 	/* If we got too many false alarms this time, reduce sensitivity */ | 
 | 242 | 	if ((false_alarms > max_false_alarms) && | 
 | 243 | 		(data->auto_corr_cck > AUTO_CORR_MAX_TH_CCK)) { | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 244 | 		IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 245 | 		     false_alarms, max_false_alarms); | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 246 | 		IWL_DEBUG_CALIB(priv, "... reducing sensitivity\n"); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 247 | 		data->nrg_curr_state = IWL_FA_TOO_MANY; | 
 | 248 | 		/* Store for "fewer than desired" on later beacon */ | 
 | 249 | 		data->nrg_silence_ref = silence_ref; | 
 | 250 |  | 
 | 251 | 		/* increase energy threshold (reduce nrg value) | 
 | 252 | 		 *   to decrease sensitivity */ | 
| Wey-Yi Guy | fe6efb4 | 2009-06-12 13:22:54 -0700 | [diff] [blame] | 253 | 		data->nrg_th_cck = data->nrg_th_cck - NRG_STEP_CCK; | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 254 | 	/* Else if we got fewer than desired, increase sensitivity */ | 
 | 255 | 	} else if (false_alarms < min_false_alarms) { | 
 | 256 | 		data->nrg_curr_state = IWL_FA_TOO_FEW; | 
 | 257 |  | 
 | 258 | 		/* Compare silence level with silence level for most recent | 
 | 259 | 		 *   healthy number or too many false alarms */ | 
 | 260 | 		data->nrg_auto_corr_silence_diff = (s32)data->nrg_silence_ref - | 
 | 261 | 						   (s32)silence_ref; | 
 | 262 |  | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 263 | 		IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u, silence diff %d\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 264 | 			 false_alarms, min_false_alarms, | 
 | 265 | 			 data->nrg_auto_corr_silence_diff); | 
 | 266 |  | 
 | 267 | 		/* Increase value to increase sensitivity, but only if: | 
 | 268 | 		 * 1a) previous beacon did *not* have *too many* false alarms | 
 | 269 | 		 * 1b) AND there's a significant difference in Rx levels | 
 | 270 | 		 *      from a previous beacon with too many, or healthy # FAs | 
 | 271 | 		 * OR 2) We've seen a lot of beacons (100) with too few | 
 | 272 | 		 *       false alarms */ | 
 | 273 | 		if ((data->nrg_prev_state != IWL_FA_TOO_MANY) && | 
 | 274 | 			((data->nrg_auto_corr_silence_diff > NRG_DIFF) || | 
 | 275 | 			(data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) { | 
 | 276 |  | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 277 | 			IWL_DEBUG_CALIB(priv, "... increasing sensitivity\n"); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 278 | 			/* Increase nrg value to increase sensitivity */ | 
 | 279 | 			val = data->nrg_th_cck + NRG_STEP_CCK; | 
 | 280 | 			data->nrg_th_cck = min((u32)ranges->min_nrg_cck, val); | 
 | 281 | 		} else { | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 282 | 			IWL_DEBUG_CALIB(priv, "... but not changing sensitivity\n"); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 283 | 		} | 
 | 284 |  | 
 | 285 | 	/* Else we got a healthy number of false alarms, keep status quo */ | 
 | 286 | 	} else { | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 287 | 		IWL_DEBUG_CALIB(priv, " FA in safe zone\n"); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 288 | 		data->nrg_curr_state = IWL_FA_GOOD_RANGE; | 
 | 289 |  | 
 | 290 | 		/* Store for use in "fewer than desired" with later beacon */ | 
 | 291 | 		data->nrg_silence_ref = silence_ref; | 
 | 292 |  | 
 | 293 | 		/* If previous beacon had too many false alarms, | 
 | 294 | 		 *   give it some extra margin by reducing sensitivity again | 
 | 295 | 		 *   (but don't go below measured energy of desired Rx) */ | 
 | 296 | 		if (IWL_FA_TOO_MANY == data->nrg_prev_state) { | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 297 | 			IWL_DEBUG_CALIB(priv, "... increasing margin\n"); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 298 | 			if (data->nrg_th_cck > (max_nrg_cck + NRG_MARGIN)) | 
 | 299 | 				data->nrg_th_cck -= NRG_MARGIN; | 
 | 300 | 			else | 
 | 301 | 				data->nrg_th_cck = max_nrg_cck; | 
 | 302 | 		} | 
 | 303 | 	} | 
 | 304 |  | 
 | 305 | 	/* Make sure the energy threshold does not go above the measured | 
 | 306 | 	 * energy of the desired Rx signals (reduced by backoff margin), | 
 | 307 | 	 * or else we might start missing Rx frames. | 
 | 308 | 	 * Lower value is higher energy, so we use max()! | 
 | 309 | 	 */ | 
 | 310 | 	data->nrg_th_cck = max(max_nrg_cck, data->nrg_th_cck); | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 311 | 	IWL_DEBUG_CALIB(priv, "new nrg_th_cck %u\n", data->nrg_th_cck); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 312 |  | 
 | 313 | 	data->nrg_prev_state = data->nrg_curr_state; | 
 | 314 |  | 
 | 315 | 	/* Auto-correlation CCK algorithm */ | 
 | 316 | 	if (false_alarms > min_false_alarms) { | 
 | 317 |  | 
 | 318 | 		/* increase auto_corr values to decrease sensitivity | 
 | 319 | 		 * so the DSP won't be disturbed by the noise | 
 | 320 | 		 */ | 
 | 321 | 		if (data->auto_corr_cck < AUTO_CORR_MAX_TH_CCK) | 
 | 322 | 			data->auto_corr_cck = AUTO_CORR_MAX_TH_CCK + 1; | 
 | 323 | 		else { | 
 | 324 | 			val = data->auto_corr_cck + AUTO_CORR_STEP_CCK; | 
 | 325 | 			data->auto_corr_cck = | 
 | 326 | 				min((u32)ranges->auto_corr_max_cck, val); | 
 | 327 | 		} | 
 | 328 | 		val = data->auto_corr_cck_mrc + AUTO_CORR_STEP_CCK; | 
 | 329 | 		data->auto_corr_cck_mrc = | 
 | 330 | 			min((u32)ranges->auto_corr_max_cck_mrc, val); | 
 | 331 | 	} else if ((false_alarms < min_false_alarms) && | 
 | 332 | 	   ((data->nrg_auto_corr_silence_diff > NRG_DIFF) || | 
 | 333 | 	   (data->num_in_cck_no_fa > MAX_NUMBER_CCK_NO_FA))) { | 
 | 334 |  | 
 | 335 | 		/* Decrease auto_corr values to increase sensitivity */ | 
 | 336 | 		val = data->auto_corr_cck - AUTO_CORR_STEP_CCK; | 
 | 337 | 		data->auto_corr_cck = | 
 | 338 | 			max((u32)ranges->auto_corr_min_cck, val); | 
 | 339 | 		val = data->auto_corr_cck_mrc - AUTO_CORR_STEP_CCK; | 
 | 340 | 		data->auto_corr_cck_mrc = | 
 | 341 | 			max((u32)ranges->auto_corr_min_cck_mrc, val); | 
 | 342 | 	} | 
 | 343 |  | 
 | 344 | 	return 0; | 
 | 345 | } | 
 | 346 |  | 
 | 347 |  | 
 | 348 | static int iwl_sens_auto_corr_ofdm(struct iwl_priv *priv, | 
 | 349 | 				       u32 norm_fa, | 
 | 350 | 				       u32 rx_enable_time) | 
 | 351 | { | 
 | 352 | 	u32 val; | 
 | 353 | 	u32 false_alarms = norm_fa * 200 * 1024; | 
 | 354 | 	u32 max_false_alarms = MAX_FA_OFDM * rx_enable_time; | 
 | 355 | 	u32 min_false_alarms = MIN_FA_OFDM * rx_enable_time; | 
 | 356 | 	struct iwl_sensitivity_data *data = NULL; | 
 | 357 | 	const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens; | 
 | 358 |  | 
 | 359 | 	data = &(priv->sensitivity_data); | 
 | 360 |  | 
 | 361 | 	/* If we got too many false alarms this time, reduce sensitivity */ | 
 | 362 | 	if (false_alarms > max_false_alarms) { | 
 | 363 |  | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 364 | 		IWL_DEBUG_CALIB(priv, "norm FA %u > max FA %u)\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 365 | 			     false_alarms, max_false_alarms); | 
 | 366 |  | 
 | 367 | 		val = data->auto_corr_ofdm + AUTO_CORR_STEP_OFDM; | 
 | 368 | 		data->auto_corr_ofdm = | 
 | 369 | 			min((u32)ranges->auto_corr_max_ofdm, val); | 
 | 370 |  | 
 | 371 | 		val = data->auto_corr_ofdm_mrc + AUTO_CORR_STEP_OFDM; | 
 | 372 | 		data->auto_corr_ofdm_mrc = | 
 | 373 | 			min((u32)ranges->auto_corr_max_ofdm_mrc, val); | 
 | 374 |  | 
 | 375 | 		val = data->auto_corr_ofdm_x1 + AUTO_CORR_STEP_OFDM; | 
 | 376 | 		data->auto_corr_ofdm_x1 = | 
 | 377 | 			min((u32)ranges->auto_corr_max_ofdm_x1, val); | 
 | 378 |  | 
 | 379 | 		val = data->auto_corr_ofdm_mrc_x1 + AUTO_CORR_STEP_OFDM; | 
 | 380 | 		data->auto_corr_ofdm_mrc_x1 = | 
 | 381 | 			min((u32)ranges->auto_corr_max_ofdm_mrc_x1, val); | 
 | 382 | 	} | 
 | 383 |  | 
 | 384 | 	/* Else if we got fewer than desired, increase sensitivity */ | 
 | 385 | 	else if (false_alarms < min_false_alarms) { | 
 | 386 |  | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 387 | 		IWL_DEBUG_CALIB(priv, "norm FA %u < min FA %u\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 388 | 			     false_alarms, min_false_alarms); | 
 | 389 |  | 
 | 390 | 		val = data->auto_corr_ofdm - AUTO_CORR_STEP_OFDM; | 
 | 391 | 		data->auto_corr_ofdm = | 
 | 392 | 			max((u32)ranges->auto_corr_min_ofdm, val); | 
 | 393 |  | 
 | 394 | 		val = data->auto_corr_ofdm_mrc - AUTO_CORR_STEP_OFDM; | 
 | 395 | 		data->auto_corr_ofdm_mrc = | 
 | 396 | 			max((u32)ranges->auto_corr_min_ofdm_mrc, val); | 
 | 397 |  | 
 | 398 | 		val = data->auto_corr_ofdm_x1 - AUTO_CORR_STEP_OFDM; | 
 | 399 | 		data->auto_corr_ofdm_x1 = | 
 | 400 | 			max((u32)ranges->auto_corr_min_ofdm_x1, val); | 
 | 401 |  | 
 | 402 | 		val = data->auto_corr_ofdm_mrc_x1 - AUTO_CORR_STEP_OFDM; | 
 | 403 | 		data->auto_corr_ofdm_mrc_x1 = | 
 | 404 | 			max((u32)ranges->auto_corr_min_ofdm_mrc_x1, val); | 
 | 405 | 	} else { | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 406 | 		IWL_DEBUG_CALIB(priv, "min FA %u < norm FA %u < max FA %u OK\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 407 | 			 min_false_alarms, false_alarms, max_false_alarms); | 
 | 408 | 	} | 
 | 409 | 	return 0; | 
 | 410 | } | 
 | 411 |  | 
| Wey-Yi Guy | c8312fa | 2010-06-28 13:05:17 -0700 | [diff] [blame] | 412 | static void iwl_prepare_legacy_sensitivity_tbl(struct iwl_priv *priv, | 
 | 413 | 				struct iwl_sensitivity_data *data, | 
 | 414 | 				__le16 *tbl) | 
 | 415 | { | 
 | 416 | 	tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_INDEX] = | 
 | 417 | 				cpu_to_le16((u16)data->auto_corr_ofdm); | 
 | 418 | 	tbl[HD_AUTO_CORR32_X4_TH_ADD_MIN_MRC_INDEX] = | 
 | 419 | 				cpu_to_le16((u16)data->auto_corr_ofdm_mrc); | 
 | 420 | 	tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_INDEX] = | 
 | 421 | 				cpu_to_le16((u16)data->auto_corr_ofdm_x1); | 
 | 422 | 	tbl[HD_AUTO_CORR32_X1_TH_ADD_MIN_MRC_INDEX] = | 
 | 423 | 				cpu_to_le16((u16)data->auto_corr_ofdm_mrc_x1); | 
 | 424 |  | 
 | 425 | 	tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_INDEX] = | 
 | 426 | 				cpu_to_le16((u16)data->auto_corr_cck); | 
 | 427 | 	tbl[HD_AUTO_CORR40_X4_TH_ADD_MIN_MRC_INDEX] = | 
 | 428 | 				cpu_to_le16((u16)data->auto_corr_cck_mrc); | 
 | 429 |  | 
 | 430 | 	tbl[HD_MIN_ENERGY_CCK_DET_INDEX] = | 
 | 431 | 				cpu_to_le16((u16)data->nrg_th_cck); | 
 | 432 | 	tbl[HD_MIN_ENERGY_OFDM_DET_INDEX] = | 
 | 433 | 				cpu_to_le16((u16)data->nrg_th_ofdm); | 
 | 434 |  | 
 | 435 | 	tbl[HD_BARKER_CORR_TH_ADD_MIN_INDEX] = | 
 | 436 | 				cpu_to_le16(data->barker_corr_th_min); | 
 | 437 | 	tbl[HD_BARKER_CORR_TH_ADD_MIN_MRC_INDEX] = | 
 | 438 | 				cpu_to_le16(data->barker_corr_th_min_mrc); | 
 | 439 | 	tbl[HD_OFDM_ENERGY_TH_IN_INDEX] = | 
 | 440 | 				cpu_to_le16(data->nrg_th_cca); | 
 | 441 |  | 
 | 442 | 	IWL_DEBUG_CALIB(priv, "ofdm: ac %u mrc %u x1 %u mrc_x1 %u thresh %u\n", | 
 | 443 | 			data->auto_corr_ofdm, data->auto_corr_ofdm_mrc, | 
 | 444 | 			data->auto_corr_ofdm_x1, data->auto_corr_ofdm_mrc_x1, | 
 | 445 | 			data->nrg_th_ofdm); | 
 | 446 |  | 
 | 447 | 	IWL_DEBUG_CALIB(priv, "cck: ac %u mrc %u thresh %u\n", | 
 | 448 | 			data->auto_corr_cck, data->auto_corr_cck_mrc, | 
 | 449 | 			data->nrg_th_cck); | 
 | 450 | } | 
 | 451 |  | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 452 | /* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */ | 
 | 453 | static int iwl_sensitivity_write(struct iwl_priv *priv) | 
 | 454 | { | 
| Wey-Yi Guy | c8312fa | 2010-06-28 13:05:17 -0700 | [diff] [blame] | 455 | 	struct iwl_sensitivity_cmd cmd; | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 456 | 	struct iwl_sensitivity_data *data = NULL; | 
 | 457 | 	struct iwl_host_cmd cmd_out = { | 
 | 458 | 		.id = SENSITIVITY_CMD, | 
 | 459 | 		.len = sizeof(struct iwl_sensitivity_cmd), | 
| Johannes Berg | c2acea8 | 2009-07-24 11:13:05 -0700 | [diff] [blame] | 460 | 		.flags = CMD_ASYNC, | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 461 | 		.data = &cmd, | 
 | 462 | 	}; | 
 | 463 |  | 
 | 464 | 	data = &(priv->sensitivity_data); | 
 | 465 |  | 
 | 466 | 	memset(&cmd, 0, sizeof(cmd)); | 
 | 467 |  | 
| Wey-Yi Guy | c8312fa | 2010-06-28 13:05:17 -0700 | [diff] [blame] | 468 | 	iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.table[0]); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 469 |  | 
 | 470 | 	/* Update uCode's "work" table, and copy it to DSP */ | 
 | 471 | 	cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE; | 
 | 472 |  | 
 | 473 | 	/* Don't send command to uCode if nothing has changed */ | 
 | 474 | 	if (!memcmp(&cmd.table[0], &(priv->sensitivity_tbl[0]), | 
 | 475 | 		    sizeof(u16)*HD_TABLE_SIZE)) { | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 476 | 		IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n"); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 477 | 		return 0; | 
 | 478 | 	} | 
 | 479 |  | 
 | 480 | 	/* Copy table for comparison next time */ | 
 | 481 | 	memcpy(&(priv->sensitivity_tbl[0]), &(cmd.table[0]), | 
 | 482 | 	       sizeof(u16)*HD_TABLE_SIZE); | 
 | 483 |  | 
| Wey-Yi Guy | 4309af2 | 2009-12-14 14:12:21 -0800 | [diff] [blame] | 484 | 	return iwl_send_cmd(priv, &cmd_out); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 485 | } | 
 | 486 |  | 
| Wey-Yi Guy | c8312fa | 2010-06-28 13:05:17 -0700 | [diff] [blame] | 487 | /* Prepare a SENSITIVITY_CMD, send to uCode if values have changed */ | 
 | 488 | static int iwl_enhance_sensitivity_write(struct iwl_priv *priv) | 
 | 489 | { | 
 | 490 | 	struct iwl_enhance_sensitivity_cmd cmd; | 
 | 491 | 	struct iwl_sensitivity_data *data = NULL; | 
 | 492 | 	struct iwl_host_cmd cmd_out = { | 
 | 493 | 		.id = SENSITIVITY_CMD, | 
 | 494 | 		.len = sizeof(struct iwl_enhance_sensitivity_cmd), | 
 | 495 | 		.flags = CMD_ASYNC, | 
 | 496 | 		.data = &cmd, | 
 | 497 | 	}; | 
 | 498 |  | 
 | 499 | 	data = &(priv->sensitivity_data); | 
 | 500 |  | 
 | 501 | 	memset(&cmd, 0, sizeof(cmd)); | 
 | 502 |  | 
 | 503 | 	iwl_prepare_legacy_sensitivity_tbl(priv, data, &cmd.enhance_table[0]); | 
 | 504 |  | 
 | 505 | 	cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX] = | 
 | 506 | 		HD_INA_NON_SQUARE_DET_OFDM_DATA; | 
 | 507 | 	cmd.enhance_table[HD_INA_NON_SQUARE_DET_CCK_INDEX] = | 
 | 508 | 		HD_INA_NON_SQUARE_DET_CCK_DATA; | 
 | 509 | 	cmd.enhance_table[HD_CORR_11_INSTEAD_OF_CORR_9_EN_INDEX] = | 
 | 510 | 		HD_CORR_11_INSTEAD_OF_CORR_9_EN_DATA; | 
 | 511 | 	cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_INDEX] = | 
 | 512 | 		HD_OFDM_NON_SQUARE_DET_SLOPE_MRC_DATA; | 
 | 513 | 	cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] = | 
 | 514 | 		HD_OFDM_NON_SQUARE_DET_INTERCEPT_MRC_DATA; | 
 | 515 | 	cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_SLOPE_INDEX] = | 
 | 516 | 		HD_OFDM_NON_SQUARE_DET_SLOPE_DATA; | 
 | 517 | 	cmd.enhance_table[HD_OFDM_NON_SQUARE_DET_INTERCEPT_INDEX] = | 
 | 518 | 		HD_OFDM_NON_SQUARE_DET_INTERCEPT_DATA; | 
 | 519 | 	cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_MRC_INDEX] = | 
 | 520 | 		HD_CCK_NON_SQUARE_DET_SLOPE_MRC_DATA; | 
 | 521 | 	cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_INDEX] = | 
 | 522 | 		HD_CCK_NON_SQUARE_DET_INTERCEPT_MRC_DATA; | 
 | 523 | 	cmd.enhance_table[HD_CCK_NON_SQUARE_DET_SLOPE_INDEX] = | 
 | 524 | 		HD_CCK_NON_SQUARE_DET_SLOPE_DATA; | 
 | 525 | 	cmd.enhance_table[HD_CCK_NON_SQUARE_DET_INTERCEPT_INDEX] = | 
 | 526 | 		HD_CCK_NON_SQUARE_DET_INTERCEPT_DATA; | 
 | 527 |  | 
 | 528 | 	/* Update uCode's "work" table, and copy it to DSP */ | 
 | 529 | 	cmd.control = SENSITIVITY_CMD_CONTROL_WORK_TABLE; | 
 | 530 |  | 
 | 531 | 	/* Don't send command to uCode if nothing has changed */ | 
 | 532 | 	if (!memcmp(&cmd.enhance_table[0], &(priv->sensitivity_tbl[0]), | 
 | 533 | 		    sizeof(u16)*HD_TABLE_SIZE) && | 
 | 534 | 	    !memcmp(&cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX], | 
 | 535 | 		    &(priv->enhance_sensitivity_tbl[0]), | 
 | 536 | 		    sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES)) { | 
 | 537 | 		IWL_DEBUG_CALIB(priv, "No change in SENSITIVITY_CMD\n"); | 
 | 538 | 		return 0; | 
 | 539 | 	} | 
 | 540 |  | 
 | 541 | 	/* Copy table for comparison next time */ | 
 | 542 | 	memcpy(&(priv->sensitivity_tbl[0]), &(cmd.enhance_table[0]), | 
 | 543 | 	       sizeof(u16)*HD_TABLE_SIZE); | 
 | 544 | 	memcpy(&(priv->enhance_sensitivity_tbl[0]), | 
 | 545 | 	       &(cmd.enhance_table[HD_INA_NON_SQUARE_DET_OFDM_INDEX]), | 
 | 546 | 	       sizeof(u16)*ENHANCE_HD_TABLE_ENTRIES); | 
 | 547 |  | 
 | 548 | 	return iwl_send_cmd(priv, &cmd_out); | 
 | 549 | } | 
 | 550 |  | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 551 | void iwl_init_sensitivity(struct iwl_priv *priv) | 
 | 552 | { | 
 | 553 | 	int ret = 0; | 
 | 554 | 	int i; | 
 | 555 | 	struct iwl_sensitivity_data *data = NULL; | 
 | 556 | 	const struct iwl_sensitivity_ranges *ranges = priv->hw_params.sens; | 
 | 557 |  | 
| Tomas Winkler | 445c2df | 2008-05-15 13:54:16 +0800 | [diff] [blame] | 558 | 	if (priv->disable_sens_cal) | 
 | 559 | 		return; | 
 | 560 |  | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 561 | 	IWL_DEBUG_CALIB(priv, "Start iwl_init_sensitivity\n"); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 562 |  | 
 | 563 | 	/* Clear driver's sensitivity algo data */ | 
 | 564 | 	data = &(priv->sensitivity_data); | 
 | 565 |  | 
 | 566 | 	if (ranges == NULL) | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 567 | 		return; | 
 | 568 |  | 
 | 569 | 	memset(data, 0, sizeof(struct iwl_sensitivity_data)); | 
 | 570 |  | 
 | 571 | 	data->num_in_cck_no_fa = 0; | 
 | 572 | 	data->nrg_curr_state = IWL_FA_TOO_MANY; | 
 | 573 | 	data->nrg_prev_state = IWL_FA_TOO_MANY; | 
 | 574 | 	data->nrg_silence_ref = 0; | 
 | 575 | 	data->nrg_silence_idx = 0; | 
 | 576 | 	data->nrg_energy_idx = 0; | 
 | 577 |  | 
 | 578 | 	for (i = 0; i < 10; i++) | 
 | 579 | 		data->nrg_value[i] = 0; | 
 | 580 |  | 
 | 581 | 	for (i = 0; i < NRG_NUM_PREV_STAT_L; i++) | 
 | 582 | 		data->nrg_silence_rssi[i] = 0; | 
 | 583 |  | 
| Wey-Yi Guy | f3a2a42 | 2009-09-11 10:38:11 -0700 | [diff] [blame] | 584 | 	data->auto_corr_ofdm =  ranges->auto_corr_min_ofdm; | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 585 | 	data->auto_corr_ofdm_mrc = ranges->auto_corr_min_ofdm_mrc; | 
 | 586 | 	data->auto_corr_ofdm_x1  = ranges->auto_corr_min_ofdm_x1; | 
 | 587 | 	data->auto_corr_ofdm_mrc_x1 = ranges->auto_corr_min_ofdm_mrc_x1; | 
 | 588 | 	data->auto_corr_cck = AUTO_CORR_CCK_MIN_VAL_DEF; | 
 | 589 | 	data->auto_corr_cck_mrc = ranges->auto_corr_min_cck_mrc; | 
 | 590 | 	data->nrg_th_cck = ranges->nrg_th_cck; | 
 | 591 | 	data->nrg_th_ofdm = ranges->nrg_th_ofdm; | 
| Wey-Yi Guy | 55036d6 | 2009-10-09 13:20:24 -0700 | [diff] [blame] | 592 | 	data->barker_corr_th_min = ranges->barker_corr_th_min; | 
 | 593 | 	data->barker_corr_th_min_mrc = ranges->barker_corr_th_min_mrc; | 
 | 594 | 	data->nrg_th_cca = ranges->nrg_th_cca; | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 595 |  | 
 | 596 | 	data->last_bad_plcp_cnt_ofdm = 0; | 
 | 597 | 	data->last_fa_cnt_ofdm = 0; | 
 | 598 | 	data->last_bad_plcp_cnt_cck = 0; | 
 | 599 | 	data->last_fa_cnt_cck = 0; | 
 | 600 |  | 
| Wey-Yi Guy | c8312fa | 2010-06-28 13:05:17 -0700 | [diff] [blame] | 601 | 	if (priv->enhance_sensitivity_table) | 
 | 602 | 		ret |= iwl_enhance_sensitivity_write(priv); | 
 | 603 | 	else | 
 | 604 | 		ret |= iwl_sensitivity_write(priv); | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 605 | 	IWL_DEBUG_CALIB(priv, "<<return 0x%X\n", ret); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 606 | } | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 607 |  | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 608 | void iwl_sensitivity_calibration(struct iwl_priv *priv, void *resp) | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 609 | { | 
 | 610 | 	u32 rx_enable_time; | 
 | 611 | 	u32 fa_cck; | 
 | 612 | 	u32 fa_ofdm; | 
 | 613 | 	u32 bad_plcp_cck; | 
 | 614 | 	u32 bad_plcp_ofdm; | 
 | 615 | 	u32 norm_fa_ofdm; | 
 | 616 | 	u32 norm_fa_cck; | 
 | 617 | 	struct iwl_sensitivity_data *data = NULL; | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 618 | 	struct statistics_rx_non_phy *rx_info; | 
 | 619 | 	struct statistics_rx_phy *ofdm, *cck; | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 620 | 	unsigned long flags; | 
 | 621 | 	struct statistics_general_data statis; | 
 | 622 |  | 
| Tomas Winkler | 445c2df | 2008-05-15 13:54:16 +0800 | [diff] [blame] | 623 | 	if (priv->disable_sens_cal) | 
 | 624 | 		return; | 
 | 625 |  | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 626 | 	data = &(priv->sensitivity_data); | 
 | 627 |  | 
| Johannes Berg | 246ed35 | 2010-08-23 10:46:32 +0200 | [diff] [blame] | 628 | 	if (!iwl_is_any_associated(priv)) { | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 629 | 		IWL_DEBUG_CALIB(priv, "<< - not associated\n"); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 630 | 		return; | 
 | 631 | 	} | 
 | 632 |  | 
 | 633 | 	spin_lock_irqsave(&priv->lock, flags); | 
| Stanislaw Gruszka | 9f60e7e | 2011-01-28 16:47:51 +0100 | [diff] [blame] | 634 | 	if (iwl_bt_statistics(priv)) { | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 635 | 		rx_info = &(((struct iwl_bt_notif_statistics *)resp)-> | 
 | 636 | 			      rx.general.common); | 
 | 637 | 		ofdm = &(((struct iwl_bt_notif_statistics *)resp)->rx.ofdm); | 
 | 638 | 		cck = &(((struct iwl_bt_notif_statistics *)resp)->rx.cck); | 
 | 639 | 	} else { | 
 | 640 | 		rx_info = &(((struct iwl_notif_statistics *)resp)->rx.general); | 
 | 641 | 		ofdm = &(((struct iwl_notif_statistics *)resp)->rx.ofdm); | 
 | 642 | 		cck = &(((struct iwl_notif_statistics *)resp)->rx.cck); | 
 | 643 | 	} | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 644 | 	if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) { | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 645 | 		IWL_DEBUG_CALIB(priv, "<< invalid data.\n"); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 646 | 		spin_unlock_irqrestore(&priv->lock, flags); | 
 | 647 | 		return; | 
 | 648 | 	} | 
 | 649 |  | 
 | 650 | 	/* Extract Statistics: */ | 
 | 651 | 	rx_enable_time = le32_to_cpu(rx_info->channel_load); | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 652 | 	fa_cck = le32_to_cpu(cck->false_alarm_cnt); | 
 | 653 | 	fa_ofdm = le32_to_cpu(ofdm->false_alarm_cnt); | 
 | 654 | 	bad_plcp_cck = le32_to_cpu(cck->plcp_err); | 
 | 655 | 	bad_plcp_ofdm = le32_to_cpu(ofdm->plcp_err); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 656 |  | 
 | 657 | 	statis.beacon_silence_rssi_a = | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 658 | 			le32_to_cpu(rx_info->beacon_silence_rssi_a); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 659 | 	statis.beacon_silence_rssi_b = | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 660 | 			le32_to_cpu(rx_info->beacon_silence_rssi_b); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 661 | 	statis.beacon_silence_rssi_c = | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 662 | 			le32_to_cpu(rx_info->beacon_silence_rssi_c); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 663 | 	statis.beacon_energy_a = | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 664 | 			le32_to_cpu(rx_info->beacon_energy_a); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 665 | 	statis.beacon_energy_b = | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 666 | 			le32_to_cpu(rx_info->beacon_energy_b); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 667 | 	statis.beacon_energy_c = | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 668 | 			le32_to_cpu(rx_info->beacon_energy_c); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 669 |  | 
 | 670 | 	spin_unlock_irqrestore(&priv->lock, flags); | 
 | 671 |  | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 672 | 	IWL_DEBUG_CALIB(priv, "rx_enable_time = %u usecs\n", rx_enable_time); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 673 |  | 
 | 674 | 	if (!rx_enable_time) { | 
| Frans Pop | 91dd6c2 | 2010-03-24 14:19:58 -0700 | [diff] [blame] | 675 | 		IWL_DEBUG_CALIB(priv, "<< RX Enable Time == 0!\n"); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 676 | 		return; | 
 | 677 | 	} | 
 | 678 |  | 
 | 679 | 	/* These statistics increase monotonically, and do not reset | 
 | 680 | 	 *   at each beacon.  Calculate difference from last value, or just | 
 | 681 | 	 *   use the new statistics value if it has reset or wrapped around. */ | 
 | 682 | 	if (data->last_bad_plcp_cnt_cck > bad_plcp_cck) | 
 | 683 | 		data->last_bad_plcp_cnt_cck = bad_plcp_cck; | 
 | 684 | 	else { | 
 | 685 | 		bad_plcp_cck -= data->last_bad_plcp_cnt_cck; | 
 | 686 | 		data->last_bad_plcp_cnt_cck += bad_plcp_cck; | 
 | 687 | 	} | 
 | 688 |  | 
 | 689 | 	if (data->last_bad_plcp_cnt_ofdm > bad_plcp_ofdm) | 
 | 690 | 		data->last_bad_plcp_cnt_ofdm = bad_plcp_ofdm; | 
 | 691 | 	else { | 
 | 692 | 		bad_plcp_ofdm -= data->last_bad_plcp_cnt_ofdm; | 
 | 693 | 		data->last_bad_plcp_cnt_ofdm += bad_plcp_ofdm; | 
 | 694 | 	} | 
 | 695 |  | 
 | 696 | 	if (data->last_fa_cnt_ofdm > fa_ofdm) | 
 | 697 | 		data->last_fa_cnt_ofdm = fa_ofdm; | 
 | 698 | 	else { | 
 | 699 | 		fa_ofdm -= data->last_fa_cnt_ofdm; | 
 | 700 | 		data->last_fa_cnt_ofdm += fa_ofdm; | 
 | 701 | 	} | 
 | 702 |  | 
 | 703 | 	if (data->last_fa_cnt_cck > fa_cck) | 
 | 704 | 		data->last_fa_cnt_cck = fa_cck; | 
 | 705 | 	else { | 
 | 706 | 		fa_cck -= data->last_fa_cnt_cck; | 
 | 707 | 		data->last_fa_cnt_cck += fa_cck; | 
 | 708 | 	} | 
 | 709 |  | 
 | 710 | 	/* Total aborted signal locks */ | 
 | 711 | 	norm_fa_ofdm = fa_ofdm + bad_plcp_ofdm; | 
 | 712 | 	norm_fa_cck = fa_cck + bad_plcp_cck; | 
 | 713 |  | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 714 | 	IWL_DEBUG_CALIB(priv, "cck: fa %u badp %u  ofdm: fa %u badp %u\n", fa_cck, | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 715 | 			bad_plcp_cck, fa_ofdm, bad_plcp_ofdm); | 
 | 716 |  | 
 | 717 | 	iwl_sens_auto_corr_ofdm(priv, norm_fa_ofdm, rx_enable_time); | 
 | 718 | 	iwl_sens_energy_cck(priv, norm_fa_cck, rx_enable_time, &statis); | 
| Wey-Yi Guy | c8312fa | 2010-06-28 13:05:17 -0700 | [diff] [blame] | 719 | 	if (priv->enhance_sensitivity_table) | 
 | 720 | 		iwl_enhance_sensitivity_write(priv); | 
 | 721 | 	else | 
 | 722 | 		iwl_sensitivity_write(priv); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 723 | } | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 724 |  | 
| Wey-Yi Guy | d8c07e7 | 2009-09-25 14:24:26 -0700 | [diff] [blame] | 725 | static inline u8 find_first_chain(u8 mask) | 
 | 726 | { | 
 | 727 | 	if (mask & ANT_A) | 
 | 728 | 		return CHAIN_A; | 
 | 729 | 	if (mask & ANT_B) | 
 | 730 | 		return CHAIN_B; | 
 | 731 | 	return CHAIN_C; | 
 | 732 | } | 
 | 733 |  | 
| Shanyu Zhao | 3031242 | 2010-11-10 09:56:48 -0800 | [diff] [blame] | 734 | /** | 
 | 735 |  * Run disconnected antenna algorithm to find out which antennas are | 
 | 736 |  * disconnected. | 
 | 737 |  */ | 
 | 738 | static void iwl_find_disconn_antenna(struct iwl_priv *priv, u32* average_sig, | 
 | 739 | 				     struct iwl_chain_noise_data *data) | 
 | 740 | { | 
 | 741 | 	u32 active_chains = 0; | 
 | 742 | 	u32 max_average_sig; | 
 | 743 | 	u16 max_average_sig_antenna_i; | 
 | 744 | 	u8 num_tx_chains; | 
 | 745 | 	u8 first_chain; | 
 | 746 | 	u16 i = 0; | 
 | 747 |  | 
 | 748 | 	average_sig[0] = data->chain_signal_a / | 
 | 749 | 			 priv->cfg->base_params->chain_noise_num_beacons; | 
 | 750 | 	average_sig[1] = data->chain_signal_b / | 
 | 751 | 			 priv->cfg->base_params->chain_noise_num_beacons; | 
 | 752 | 	average_sig[2] = data->chain_signal_c / | 
 | 753 | 			 priv->cfg->base_params->chain_noise_num_beacons; | 
 | 754 |  | 
 | 755 | 	if (average_sig[0] >= average_sig[1]) { | 
 | 756 | 		max_average_sig = average_sig[0]; | 
 | 757 | 		max_average_sig_antenna_i = 0; | 
 | 758 | 		active_chains = (1 << max_average_sig_antenna_i); | 
 | 759 | 	} else { | 
 | 760 | 		max_average_sig = average_sig[1]; | 
 | 761 | 		max_average_sig_antenna_i = 1; | 
 | 762 | 		active_chains = (1 << max_average_sig_antenna_i); | 
 | 763 | 	} | 
 | 764 |  | 
 | 765 | 	if (average_sig[2] >= max_average_sig) { | 
 | 766 | 		max_average_sig = average_sig[2]; | 
 | 767 | 		max_average_sig_antenna_i = 2; | 
 | 768 | 		active_chains = (1 << max_average_sig_antenna_i); | 
 | 769 | 	} | 
 | 770 |  | 
 | 771 | 	IWL_DEBUG_CALIB(priv, "average_sig: a %d b %d c %d\n", | 
 | 772 | 		     average_sig[0], average_sig[1], average_sig[2]); | 
 | 773 | 	IWL_DEBUG_CALIB(priv, "max_average_sig = %d, antenna %d\n", | 
 | 774 | 		     max_average_sig, max_average_sig_antenna_i); | 
 | 775 |  | 
 | 776 | 	/* Compare signal strengths for all 3 receivers. */ | 
 | 777 | 	for (i = 0; i < NUM_RX_CHAINS; i++) { | 
 | 778 | 		if (i != max_average_sig_antenna_i) { | 
 | 779 | 			s32 rssi_delta = (max_average_sig - average_sig[i]); | 
 | 780 |  | 
 | 781 | 			/* If signal is very weak, compared with | 
 | 782 | 			 * strongest, mark it as disconnected. */ | 
 | 783 | 			if (rssi_delta > MAXIMUM_ALLOWED_PATHLOSS) | 
 | 784 | 				data->disconn_array[i] = 1; | 
 | 785 | 			else | 
 | 786 | 				active_chains |= (1 << i); | 
 | 787 | 			IWL_DEBUG_CALIB(priv, "i = %d  rssiDelta = %d  " | 
 | 788 | 			     "disconn_array[i] = %d\n", | 
 | 789 | 			     i, rssi_delta, data->disconn_array[i]); | 
 | 790 | 		} | 
 | 791 | 	} | 
 | 792 |  | 
 | 793 | 	/* | 
 | 794 | 	 * The above algorithm sometimes fails when the ucode | 
 | 795 | 	 * reports 0 for all chains. It's not clear why that | 
 | 796 | 	 * happens to start with, but it is then causing trouble | 
 | 797 | 	 * because this can make us enable more chains than the | 
 | 798 | 	 * hardware really has. | 
 | 799 | 	 * | 
 | 800 | 	 * To be safe, simply mask out any chains that we know | 
 | 801 | 	 * are not on the device. | 
 | 802 | 	 */ | 
| Shanyu Zhao | 6fe8efb | 2010-11-10 09:56:49 -0800 | [diff] [blame] | 803 | 	active_chains &= priv->hw_params.valid_rx_ant; | 
| Shanyu Zhao | 3031242 | 2010-11-10 09:56:48 -0800 | [diff] [blame] | 804 |  | 
 | 805 | 	num_tx_chains = 0; | 
 | 806 | 	for (i = 0; i < NUM_RX_CHAINS; i++) { | 
 | 807 | 		/* loops on all the bits of | 
 | 808 | 		 * priv->hw_setting.valid_tx_ant */ | 
 | 809 | 		u8 ant_msk = (1 << i); | 
 | 810 | 		if (!(priv->hw_params.valid_tx_ant & ant_msk)) | 
 | 811 | 			continue; | 
 | 812 |  | 
 | 813 | 		num_tx_chains++; | 
 | 814 | 		if (data->disconn_array[i] == 0) | 
 | 815 | 			/* there is a Tx antenna connected */ | 
 | 816 | 			break; | 
 | 817 | 		if (num_tx_chains == priv->hw_params.tx_chains_num && | 
 | 818 | 		    data->disconn_array[i]) { | 
 | 819 | 			/* | 
 | 820 | 			 * If all chains are disconnected | 
 | 821 | 			 * connect the first valid tx chain | 
 | 822 | 			 */ | 
 | 823 | 			first_chain = | 
 | 824 | 				find_first_chain(priv->cfg->valid_tx_ant); | 
 | 825 | 			data->disconn_array[first_chain] = 0; | 
 | 826 | 			active_chains |= BIT(first_chain); | 
 | 827 | 			IWL_DEBUG_CALIB(priv, "All Tx chains are disconnected \ | 
 | 828 | 					W/A - declare %d as connected\n", | 
 | 829 | 					first_chain); | 
 | 830 | 			break; | 
 | 831 | 		} | 
 | 832 | 	} | 
 | 833 |  | 
 | 834 | 	if (active_chains != priv->hw_params.valid_rx_ant && | 
 | 835 | 	    active_chains != priv->chain_noise_data.active_chains) | 
 | 836 | 		IWL_DEBUG_CALIB(priv, | 
 | 837 | 				"Detected that not all antennas are connected! " | 
 | 838 | 				"Connected: %#x, valid: %#x.\n", | 
 | 839 | 				active_chains, priv->hw_params.valid_rx_ant); | 
 | 840 |  | 
 | 841 | 	/* Save for use within RXON, TX, SCAN commands, etc. */ | 
 | 842 | 	data->active_chains = active_chains; | 
 | 843 | 	IWL_DEBUG_CALIB(priv, "active_chains (bitwise) = 0x%x\n", | 
 | 844 | 			active_chains); | 
 | 845 | } | 
 | 846 |  | 
 | 847 |  | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 848 | /* | 
| Shanyu Zhao | 3031242 | 2010-11-10 09:56:48 -0800 | [diff] [blame] | 849 |  * Accumulate 16 beacons of signal and noise statistics for each of | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 850 |  *   3 receivers/antennas/rx-chains, then figure out: | 
 | 851 |  * 1)  Which antennas are connected. | 
 | 852 |  * 2)  Differential rx gain settings to balance the 3 receivers. | 
 | 853 |  */ | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 854 | void iwl_chain_noise_calibration(struct iwl_priv *priv, void *stat_resp) | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 855 | { | 
 | 856 | 	struct iwl_chain_noise_data *data = NULL; | 
 | 857 |  | 
 | 858 | 	u32 chain_noise_a; | 
 | 859 | 	u32 chain_noise_b; | 
 | 860 | 	u32 chain_noise_c; | 
 | 861 | 	u32 chain_sig_a; | 
 | 862 | 	u32 chain_sig_b; | 
 | 863 | 	u32 chain_sig_c; | 
 | 864 | 	u32 average_sig[NUM_RX_CHAINS] = {INITIALIZATION_VALUE}; | 
 | 865 | 	u32 average_noise[NUM_RX_CHAINS] = {INITIALIZATION_VALUE}; | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 866 | 	u32 min_average_noise = MIN_AVERAGE_NOISE_MAX_VALUE; | 
 | 867 | 	u16 min_average_noise_antenna_i = INITIALIZATION_VALUE; | 
 | 868 | 	u16 i = 0; | 
 | 869 | 	u16 rxon_chnum = INITIALIZATION_VALUE; | 
 | 870 | 	u16 stat_chnum = INITIALIZATION_VALUE; | 
 | 871 | 	u8 rxon_band24; | 
 | 872 | 	u8 stat_band24; | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 873 | 	unsigned long flags; | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 874 | 	struct statistics_rx_non_phy *rx_info; | 
| Shanyu Zhao | 3031242 | 2010-11-10 09:56:48 -0800 | [diff] [blame] | 875 |  | 
| Johannes Berg | 246ed35 | 2010-08-23 10:46:32 +0200 | [diff] [blame] | 876 | 	/* | 
 | 877 | 	 * MULTI-FIXME: | 
 | 878 | 	 * When we support multiple interfaces on different channels, | 
 | 879 | 	 * this must be modified/fixed. | 
 | 880 | 	 */ | 
 | 881 | 	struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS]; | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 882 |  | 
| Tomas Winkler | 445c2df | 2008-05-15 13:54:16 +0800 | [diff] [blame] | 883 | 	if (priv->disable_chain_noise_cal) | 
 | 884 | 		return; | 
 | 885 |  | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 886 | 	data = &(priv->chain_noise_data); | 
 | 887 |  | 
| Wey-Yi Guy | d8c07e7 | 2009-09-25 14:24:26 -0700 | [diff] [blame] | 888 | 	/* | 
 | 889 | 	 * Accumulate just the first "chain_noise_num_beacons" after | 
 | 890 | 	 * the first association, then we're done forever. | 
 | 891 | 	 */ | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 892 | 	if (data->state != IWL_CHAIN_NOISE_ACCUMULATE) { | 
 | 893 | 		if (data->state == IWL_CHAIN_NOISE_ALIVE) | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 894 | 			IWL_DEBUG_CALIB(priv, "Wait for noise calib reset\n"); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 895 | 		return; | 
 | 896 | 	} | 
 | 897 |  | 
 | 898 | 	spin_lock_irqsave(&priv->lock, flags); | 
| Stanislaw Gruszka | 9f60e7e | 2011-01-28 16:47:51 +0100 | [diff] [blame] | 899 | 	if (iwl_bt_statistics(priv)) { | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 900 | 		rx_info = &(((struct iwl_bt_notif_statistics *)stat_resp)-> | 
 | 901 | 			      rx.general.common); | 
 | 902 | 	} else { | 
 | 903 | 		rx_info = &(((struct iwl_notif_statistics *)stat_resp)-> | 
 | 904 | 			      rx.general); | 
 | 905 | 	} | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 906 | 	if (rx_info->interference_data_flag != INTERFERENCE_DATA_AVAILABLE) { | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 907 | 		IWL_DEBUG_CALIB(priv, " << Interference data unavailable\n"); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 908 | 		spin_unlock_irqrestore(&priv->lock, flags); | 
 | 909 | 		return; | 
 | 910 | 	} | 
 | 911 |  | 
| Johannes Berg | 246ed35 | 2010-08-23 10:46:32 +0200 | [diff] [blame] | 912 | 	rxon_band24 = !!(ctx->staging.flags & RXON_FLG_BAND_24G_MSK); | 
 | 913 | 	rxon_chnum = le16_to_cpu(ctx->staging.channel); | 
| Stanislaw Gruszka | 9f60e7e | 2011-01-28 16:47:51 +0100 | [diff] [blame] | 914 | 	if (iwl_bt_statistics(priv)) { | 
| Wey-Yi Guy | 7980fba | 2010-07-14 08:08:57 -0700 | [diff] [blame] | 915 | 		stat_band24 = !!(((struct iwl_bt_notif_statistics *) | 
 | 916 | 				 stat_resp)->flag & | 
 | 917 | 				 STATISTICS_REPLY_FLG_BAND_24G_MSK); | 
 | 918 | 		stat_chnum = le32_to_cpu(((struct iwl_bt_notif_statistics *) | 
 | 919 | 					 stat_resp)->flag) >> 16; | 
 | 920 | 	} else { | 
 | 921 | 		stat_band24 = !!(((struct iwl_notif_statistics *) | 
 | 922 | 				 stat_resp)->flag & | 
 | 923 | 				 STATISTICS_REPLY_FLG_BAND_24G_MSK); | 
 | 924 | 		stat_chnum = le32_to_cpu(((struct iwl_notif_statistics *) | 
 | 925 | 					 stat_resp)->flag) >> 16; | 
 | 926 | 	} | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 927 |  | 
 | 928 | 	/* Make sure we accumulate data for just the associated channel | 
 | 929 | 	 *   (even if scanning). */ | 
 | 930 | 	if ((rxon_chnum != stat_chnum) || (rxon_band24 != stat_band24)) { | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 931 | 		IWL_DEBUG_CALIB(priv, "Stats not from chan=%d, band24=%d\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 932 | 				rxon_chnum, rxon_band24); | 
 | 933 | 		spin_unlock_irqrestore(&priv->lock, flags); | 
 | 934 | 		return; | 
 | 935 | 	} | 
 | 936 |  | 
| Wey-Yi Guy | d8c07e7 | 2009-09-25 14:24:26 -0700 | [diff] [blame] | 937 | 	/* | 
 | 938 | 	 *  Accumulate beacon statistics values across | 
 | 939 | 	 * "chain_noise_num_beacons" | 
 | 940 | 	 */ | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 941 | 	chain_noise_a = le32_to_cpu(rx_info->beacon_silence_rssi_a) & | 
 | 942 | 				IN_BAND_FILTER; | 
 | 943 | 	chain_noise_b = le32_to_cpu(rx_info->beacon_silence_rssi_b) & | 
 | 944 | 				IN_BAND_FILTER; | 
 | 945 | 	chain_noise_c = le32_to_cpu(rx_info->beacon_silence_rssi_c) & | 
 | 946 | 				IN_BAND_FILTER; | 
 | 947 |  | 
 | 948 | 	chain_sig_a = le32_to_cpu(rx_info->beacon_rssi_a) & IN_BAND_FILTER; | 
 | 949 | 	chain_sig_b = le32_to_cpu(rx_info->beacon_rssi_b) & IN_BAND_FILTER; | 
 | 950 | 	chain_sig_c = le32_to_cpu(rx_info->beacon_rssi_c) & IN_BAND_FILTER; | 
 | 951 |  | 
 | 952 | 	spin_unlock_irqrestore(&priv->lock, flags); | 
 | 953 |  | 
 | 954 | 	data->beacon_count++; | 
 | 955 |  | 
 | 956 | 	data->chain_noise_a = (chain_noise_a + data->chain_noise_a); | 
 | 957 | 	data->chain_noise_b = (chain_noise_b + data->chain_noise_b); | 
 | 958 | 	data->chain_noise_c = (chain_noise_c + data->chain_noise_c); | 
 | 959 |  | 
 | 960 | 	data->chain_signal_a = (chain_sig_a + data->chain_signal_a); | 
 | 961 | 	data->chain_signal_b = (chain_sig_b + data->chain_signal_b); | 
 | 962 | 	data->chain_signal_c = (chain_sig_c + data->chain_signal_c); | 
 | 963 |  | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 964 | 	IWL_DEBUG_CALIB(priv, "chan=%d, band24=%d, beacon=%d\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 965 | 			rxon_chnum, rxon_band24, data->beacon_count); | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 966 | 	IWL_DEBUG_CALIB(priv, "chain_sig: a %d b %d c %d\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 967 | 			chain_sig_a, chain_sig_b, chain_sig_c); | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 968 | 	IWL_DEBUG_CALIB(priv, "chain_noise: a %d b %d c %d\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 969 | 			chain_noise_a, chain_noise_b, chain_noise_c); | 
 | 970 |  | 
| Wey-Yi Guy | d8c07e7 | 2009-09-25 14:24:26 -0700 | [diff] [blame] | 971 | 	/* If this is the "chain_noise_num_beacons", determine: | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 972 | 	 * 1)  Disconnected antennas (using signal strengths) | 
 | 973 | 	 * 2)  Differential gain (using silence noise) to balance receivers */ | 
| Wey-Yi Guy | 7cb1b08 | 2010-10-06 08:10:00 -0700 | [diff] [blame] | 974 | 	if (data->beacon_count != | 
 | 975 | 		priv->cfg->base_params->chain_noise_num_beacons) | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 976 | 		return; | 
 | 977 |  | 
 | 978 | 	/* Analyze signal for disconnected antenna */ | 
| Shanyu Zhao | 6fe8efb | 2010-11-10 09:56:49 -0800 | [diff] [blame] | 979 | 	if (priv->cfg->bt_params && | 
 | 980 | 	    priv->cfg->bt_params->advanced_bt_coexist) { | 
 | 981 | 		/* Disable disconnected antenna algorithm for advanced | 
 | 982 | 		   bt coex, assuming valid antennas are connected */ | 
 | 983 | 		data->active_chains = priv->hw_params.valid_rx_ant; | 
 | 984 | 		for (i = 0; i < NUM_RX_CHAINS; i++) | 
 | 985 | 			if (!(data->active_chains & (1<<i))) | 
 | 986 | 				data->disconn_array[i] = 1; | 
 | 987 | 	} else | 
 | 988 | 		iwl_find_disconn_antenna(priv, average_sig, data); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 989 |  | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 990 | 	/* Analyze noise for rx balance */ | 
| Wey-Yi Guy | 7cb1b08 | 2010-10-06 08:10:00 -0700 | [diff] [blame] | 991 | 	average_noise[0] = data->chain_noise_a / | 
 | 992 | 			   priv->cfg->base_params->chain_noise_num_beacons; | 
 | 993 | 	average_noise[1] = data->chain_noise_b / | 
 | 994 | 			   priv->cfg->base_params->chain_noise_num_beacons; | 
 | 995 | 	average_noise[2] = data->chain_noise_c / | 
 | 996 | 			   priv->cfg->base_params->chain_noise_num_beacons; | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 997 |  | 
 | 998 | 	for (i = 0; i < NUM_RX_CHAINS; i++) { | 
 | 999 | 		if (!(data->disconn_array[i]) && | 
 | 1000 | 		   (average_noise[i] <= min_average_noise)) { | 
 | 1001 | 			/* This means that chain i is active and has | 
 | 1002 | 			 * lower noise values so far: */ | 
 | 1003 | 			min_average_noise = average_noise[i]; | 
 | 1004 | 			min_average_noise_antenna_i = i; | 
 | 1005 | 		} | 
 | 1006 | 	} | 
 | 1007 |  | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 1008 | 	IWL_DEBUG_CALIB(priv, "average_noise: a %d b %d c %d\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 1009 | 			average_noise[0], average_noise[1], | 
 | 1010 | 			average_noise[2]); | 
 | 1011 |  | 
| Tomas Winkler | e162344 | 2009-01-27 14:27:56 -0800 | [diff] [blame] | 1012 | 	IWL_DEBUG_CALIB(priv, "min_average_noise = %d, antenna %d\n", | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 1013 | 			min_average_noise, min_average_noise_antenna_i); | 
 | 1014 |  | 
| Jay Sternberg | 29f35c1 | 2009-01-29 11:09:16 -0800 | [diff] [blame] | 1015 | 	if (priv->cfg->ops->utils->gain_computation) | 
 | 1016 | 		priv->cfg->ops->utils->gain_computation(priv, average_noise, | 
| Wey-Yi Guy | d8c07e7 | 2009-09-25 14:24:26 -0700 | [diff] [blame] | 1017 | 				min_average_noise_antenna_i, min_average_noise, | 
 | 1018 | 				find_first_chain(priv->cfg->valid_rx_ant)); | 
| Grumbach, Emmanuel | 0481644 | 2008-09-03 11:26:53 +0800 | [diff] [blame] | 1019 |  | 
 | 1020 | 	/* Some power changes may have been made during the calibration. | 
 | 1021 | 	 * Update and commit the RXON | 
 | 1022 | 	 */ | 
 | 1023 | 	if (priv->cfg->ops->lib->update_chain_flags) | 
 | 1024 | 		priv->cfg->ops->lib->update_chain_flags(priv); | 
 | 1025 |  | 
 | 1026 | 	data->state = IWL_CHAIN_NOISE_DONE; | 
| Johannes Berg | e312c24 | 2009-08-07 15:41:51 -0700 | [diff] [blame] | 1027 | 	iwl_power_update_mode(priv, false); | 
| Emmanuel Grumbach | f0832f1 | 2008-04-16 16:34:47 -0700 | [diff] [blame] | 1028 | } | 
| Tomas Winkler | 4a4a9e8 | 2008-05-29 16:34:54 +0800 | [diff] [blame] | 1029 |  | 
 | 1030 | void iwl_reset_run_time_calib(struct iwl_priv *priv) | 
 | 1031 | { | 
 | 1032 | 	int i; | 
 | 1033 | 	memset(&(priv->sensitivity_data), 0, | 
 | 1034 | 	       sizeof(struct iwl_sensitivity_data)); | 
 | 1035 | 	memset(&(priv->chain_noise_data), 0, | 
 | 1036 | 	       sizeof(struct iwl_chain_noise_data)); | 
 | 1037 | 	for (i = 0; i < NUM_RX_CHAINS; i++) | 
 | 1038 | 		priv->chain_noise_data.delta_gain_code[i] = | 
 | 1039 | 				CHAIN_NOISE_DELTA_GAIN_INIT_VAL; | 
 | 1040 |  | 
 | 1041 | 	/* Ask for statistics now, the uCode will send notification | 
 | 1042 | 	 * periodically after association */ | 
| Wey-Yi Guy | ef8d552 | 2009-11-13 11:56:28 -0800 | [diff] [blame] | 1043 | 	iwl_send_statistics_request(priv, CMD_ASYNC, true); | 
| Tomas Winkler | 4a4a9e8 | 2008-05-29 16:34:54 +0800 | [diff] [blame] | 1044 | } |