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