blob: a9bdbad6663ee0b02a9f8f52e5cf820ad3c85974 [file] [log] [blame]
Tomas Winklera55360e2008-05-05 10:22:28 +08001/******************************************************************************
2 *
Wey-Yi Guy4e318262011-12-27 11:21:32 -08003 * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
Tomas Winklera55360e2008-05-05 10:22:28 +08004 *
5 * Portions of this file are derived from the ipw3945 project, as well
Emmanuel Grumbach6a686c62011-10-10 07:27:17 -07006 * as portionhelp of the ieee80211 subsystem header files.
Tomas Winklera55360e2008-05-05 10:22:28 +08007 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
Winkler, Tomas759ef892008-12-09 11:28:58 -080025 * Intel Linux Wireless <ilw@linux.intel.com>
Tomas Winklera55360e2008-05-05 10:22:28 +080026 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
Emmanuel Grumbach1781a072008-06-30 17:23:09 +080030#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090031#include <linux/slab.h>
Stanislaw Gruszka118253c2011-03-07 09:22:24 +010032#include <linux/sched.h>
Tomas Winklera55360e2008-05-05 10:22:28 +080033#include <net/mac80211.h>
Tomas Winklera05ffd32008-07-10 14:28:42 +030034#include <asm/unaligned.h>
Tomas Winklera55360e2008-05-05 10:22:28 +080035#include "iwl-eeprom.h"
36#include "iwl-dev.h"
37#include "iwl-core.h"
Tomas Winklera55360e2008-05-05 10:22:28 +080038#include "iwl-io.h"
Stanislaw Gruszka67289942011-02-28 14:33:17 +010039#include "iwl-agn-calib.h"
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010040#include "iwl-agn.h"
Emmanuel Grumbach48f20d32011-08-25 23:10:36 -070041#include "iwl-shared.h"
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010042
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070043const char *get_cmd_string(u8 cmd)
44{
45 switch (cmd) {
46 IWL_CMD(REPLY_ALIVE);
47 IWL_CMD(REPLY_ERROR);
Wey-Yi Guy7e4005c2011-10-10 07:26:51 -070048 IWL_CMD(REPLY_ECHO);
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070049 IWL_CMD(REPLY_RXON);
50 IWL_CMD(REPLY_RXON_ASSOC);
51 IWL_CMD(REPLY_QOS_PARAM);
52 IWL_CMD(REPLY_RXON_TIMING);
53 IWL_CMD(REPLY_ADD_STA);
54 IWL_CMD(REPLY_REMOVE_STA);
55 IWL_CMD(REPLY_REMOVE_ALL_STA);
56 IWL_CMD(REPLY_TXFIFO_FLUSH);
57 IWL_CMD(REPLY_WEPKEY);
58 IWL_CMD(REPLY_TX);
59 IWL_CMD(REPLY_LEDS_CMD);
60 IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
61 IWL_CMD(COEX_PRIORITY_TABLE_CMD);
62 IWL_CMD(COEX_MEDIUM_NOTIFICATION);
63 IWL_CMD(COEX_EVENT_CMD);
64 IWL_CMD(REPLY_QUIET_CMD);
65 IWL_CMD(REPLY_CHANNEL_SWITCH);
66 IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
67 IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
68 IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
69 IWL_CMD(POWER_TABLE_CMD);
70 IWL_CMD(PM_SLEEP_NOTIFICATION);
71 IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
72 IWL_CMD(REPLY_SCAN_CMD);
73 IWL_CMD(REPLY_SCAN_ABORT_CMD);
74 IWL_CMD(SCAN_START_NOTIFICATION);
75 IWL_CMD(SCAN_RESULTS_NOTIFICATION);
76 IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
77 IWL_CMD(BEACON_NOTIFICATION);
78 IWL_CMD(REPLY_TX_BEACON);
79 IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
80 IWL_CMD(QUIET_NOTIFICATION);
81 IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
82 IWL_CMD(MEASURE_ABORT_NOTIFICATION);
83 IWL_CMD(REPLY_BT_CONFIG);
84 IWL_CMD(REPLY_STATISTICS_CMD);
85 IWL_CMD(STATISTICS_NOTIFICATION);
86 IWL_CMD(REPLY_CARD_STATE_CMD);
87 IWL_CMD(CARD_STATE_NOTIFICATION);
88 IWL_CMD(MISSED_BEACONS_NOTIFICATION);
89 IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
90 IWL_CMD(SENSITIVITY_CMD);
91 IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
92 IWL_CMD(REPLY_RX_PHY_CMD);
93 IWL_CMD(REPLY_RX_MPDU_CMD);
94 IWL_CMD(REPLY_RX);
95 IWL_CMD(REPLY_COMPRESSED_BA);
96 IWL_CMD(CALIBRATION_CFG_CMD);
97 IWL_CMD(CALIBRATION_RES_NOTIFICATION);
98 IWL_CMD(CALIBRATION_COMPLETE_NOTIFICATION);
99 IWL_CMD(REPLY_TX_POWER_DBM_CMD);
100 IWL_CMD(TEMPERATURE_NOTIFICATION);
101 IWL_CMD(TX_ANT_CONFIGURATION_CMD);
102 IWL_CMD(REPLY_BT_COEX_PROFILE_NOTIF);
103 IWL_CMD(REPLY_BT_COEX_PRIO_TABLE);
104 IWL_CMD(REPLY_BT_COEX_PROT_ENV);
105 IWL_CMD(REPLY_WIPAN_PARAMS);
106 IWL_CMD(REPLY_WIPAN_RXON);
107 IWL_CMD(REPLY_WIPAN_RXON_TIMING);
108 IWL_CMD(REPLY_WIPAN_RXON_ASSOC);
109 IWL_CMD(REPLY_WIPAN_QOS_PARAM);
110 IWL_CMD(REPLY_WIPAN_WEPKEY);
111 IWL_CMD(REPLY_WIPAN_P2P_CHANNEL_SWITCH);
112 IWL_CMD(REPLY_WIPAN_NOA_NOTIFICATION);
113 IWL_CMD(REPLY_WIPAN_DEACTIVATION_COMPLETE);
114 IWL_CMD(REPLY_WOWLAN_PATTERNS);
115 IWL_CMD(REPLY_WOWLAN_WAKEUP_FILTER);
116 IWL_CMD(REPLY_WOWLAN_TSC_RSC_PARAMS);
117 IWL_CMD(REPLY_WOWLAN_TKIP_PARAMS);
118 IWL_CMD(REPLY_WOWLAN_KEK_KCK_MATERIAL);
119 IWL_CMD(REPLY_WOWLAN_GET_STATUS);
Wey-Yi Guy1b3c0c32011-11-17 09:02:11 -0800120 IWL_CMD(REPLY_D3_CONFIG);
Emmanuel Grumbach522376d2011-09-06 09:31:19 -0700121 default:
122 return "UNKNOWN";
123
124 }
125}
Tomas Winklera55360e2008-05-05 10:22:28 +0800126
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100127/******************************************************************************
128 *
129 * Generic RX handler implementations
130 *
131 ******************************************************************************/
132
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700133static int iwlagn_rx_reply_error(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800134 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700135 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100136{
137 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800138 struct iwl_error_resp *err_resp = (void *)pkt->data;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100139
140 IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
141 "seq 0x%04X ser 0x%08X\n",
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800142 le32_to_cpu(err_resp->error_type),
143 get_cmd_string(err_resp->cmd_id),
144 err_resp->cmd_id,
145 le16_to_cpu(err_resp->bad_cmd_seq_num),
146 le32_to_cpu(err_resp->error_info));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700147 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100148}
149
Johannes Berg48a2d662012-03-05 11:24:39 -0800150static int iwlagn_rx_csa(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700151 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100152{
153 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800154 struct iwl_csa_notification *csa = (void *)pkt->data;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100155 /*
156 * MULTI-FIXME
Wey-Yi Guyade4c642011-10-10 07:27:11 -0700157 * See iwlagn_mac_channel_switch.
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100158 */
159 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
160 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
161
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700162 if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status))
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700163 return 0;
Stanislaw Gruszka6f213ff2011-06-02 18:17:15 +0200164
165 if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
166 rxon->channel = csa->channel;
167 ctx->staging.channel = csa->channel;
168 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100169 le16_to_cpu(csa->channel));
Stanislaw Gruszka6f213ff2011-06-02 18:17:15 +0200170 iwl_chswitch_done(priv, true);
171 } else {
172 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
173 le16_to_cpu(csa->channel));
174 iwl_chswitch_done(priv, false);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100175 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700176 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100177}
178
179
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700180static int iwlagn_rx_spectrum_measure_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800181 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700182 struct iwl_device_cmd *cmd)
Reinette Chatre81963d62010-01-22 14:22:57 -0800183{
184 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800185 struct iwl_spectrum_notification *report = (void *)pkt->data;
Reinette Chatre81963d62010-01-22 14:22:57 -0800186
187 if (!report->state) {
188 IWL_DEBUG_11H(priv,
189 "Spectrum Measure Notification: Start\n");
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700190 return 0;
Reinette Chatre81963d62010-01-22 14:22:57 -0800191 }
192
193 memcpy(&priv->measure_report, report, sizeof(*report));
194 priv->measurement_status |= MEASUREMENT_READY;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700195 return 0;
Reinette Chatre81963d62010-01-22 14:22:57 -0800196}
Reinette Chatre81963d62010-01-22 14:22:57 -0800197
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700198static int iwlagn_rx_pm_sleep_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800199 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700200 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100201{
202#ifdef CONFIG_IWLWIFI_DEBUG
203 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800204 struct iwl_sleep_notification *sleep = (void *)pkt->data;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100205 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
206 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
207#endif
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700208 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100209}
210
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700211static int iwlagn_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800212 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700213 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100214{
215 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach44856c62011-08-25 23:10:45 -0700216 u32 __maybe_unused len =
217 le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100218 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
219 "notification for %s:\n", len,
220 get_cmd_string(pkt->hdr.cmd));
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800221 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->data, len);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700222 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100223}
224
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700225static int iwlagn_rx_beacon_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800226 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700227 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100228{
229 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800230 struct iwlagn_beacon_notif *beacon = (void *)pkt->data;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100231#ifdef CONFIG_IWLWIFI_DEBUG
232 u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
233 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
234
235 IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
236 "tsf:0x%.8x%.8x rate:%d\n",
237 status & TX_STATUS_MSK,
238 beacon->beacon_notify_hdr.failure_frame,
239 le32_to_cpu(beacon->ibss_mgr_status),
240 le32_to_cpu(beacon->high_tsf),
241 le32_to_cpu(beacon->low_tsf), rate);
242#endif
243
244 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
245
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700246 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100247}
248
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100249/* the threshold ratio of actual_ack_cnt to expected_ack_cnt in percent */
250#define ACK_CNT_RATIO (50)
251#define BA_TIMEOUT_CNT (5)
252#define BA_TIMEOUT_MAX (16)
253
254/**
255 * iwl_good_ack_health - checks for ACK count ratios, BA timeout retries.
256 *
257 * When the ACK count ratio is low and aggregated BA timeout retries exceeding
258 * the BA_TIMEOUT_MAX, reload firmware and bring system back to normal
259 * operation state.
260 */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700261static bool iwlagn_good_ack_health(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700262 struct statistics_tx *cur)
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100263{
264 int actual_delta, expected_delta, ba_timeout_delta;
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700265 struct statistics_tx *old;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100266
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700267 if (priv->agg_tids_count)
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100268 return true;
269
Johannes Berg4ff70fcd2012-03-05 11:24:26 -0800270 lockdep_assert_held(&priv->statistics.lock);
271
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700272 old = &priv->statistics.tx;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100273
274 actual_delta = le32_to_cpu(cur->actual_ack_cnt) -
275 le32_to_cpu(old->actual_ack_cnt);
276 expected_delta = le32_to_cpu(cur->expected_ack_cnt) -
277 le32_to_cpu(old->expected_ack_cnt);
278
279 /* Values should not be negative, but we do not trust the firmware */
280 if (actual_delta <= 0 || expected_delta <= 0)
281 return true;
282
283 ba_timeout_delta = le32_to_cpu(cur->agg.ba_timeout) -
284 le32_to_cpu(old->agg.ba_timeout);
285
286 if ((actual_delta * 100 / expected_delta) < ACK_CNT_RATIO &&
287 ba_timeout_delta > BA_TIMEOUT_CNT) {
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700288 IWL_DEBUG_RADIO(priv,
289 "deltas: actual %d expected %d ba_timeout %d\n",
290 actual_delta, expected_delta, ba_timeout_delta);
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100291
292#ifdef CONFIG_IWLWIFI_DEBUGFS
293 /*
294 * This is ifdef'ed on DEBUGFS because otherwise the
295 * statistics aren't available. If DEBUGFS is set but
296 * DEBUG is not, these will just compile out.
297 */
298 IWL_DEBUG_RADIO(priv, "rx_detected_cnt delta %d\n",
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700299 priv->delta_stats.tx.rx_detected_cnt);
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100300 IWL_DEBUG_RADIO(priv,
301 "ack_or_ba_timeout_collision delta %d\n",
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700302 priv->delta_stats.tx.ack_or_ba_timeout_collision);
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100303#endif
304
305 if (ba_timeout_delta >= BA_TIMEOUT_MAX)
306 return false;
307 }
308
309 return true;
310}
311
312/**
313 * iwl_good_plcp_health - checks for plcp error.
314 *
315 * When the plcp error is exceeding the thresholds, reset the radio
316 * to improve the throughput.
317 */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700318static bool iwlagn_good_plcp_health(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700319 struct statistics_rx_phy *cur_ofdm,
320 struct statistics_rx_ht_phy *cur_ofdm_ht,
321 unsigned int msecs)
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100322{
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100323 int delta;
Johannes Bergab5c0f12012-03-06 13:30:53 -0800324 int threshold = priv->plcp_delta_threshold;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100325
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100326 if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100327 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100328 return true;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100329 }
330
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700331 delta = le32_to_cpu(cur_ofdm->plcp_err) -
332 le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
333 le32_to_cpu(cur_ofdm_ht->plcp_err) -
334 le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100335
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700336 /* Can be negative if firmware reset statistics */
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100337 if (delta <= 0)
338 return true;
339
340 if ((delta * 100 / msecs) > threshold) {
341 IWL_DEBUG_RADIO(priv,
342 "plcp health threshold %u delta %d msecs %u\n",
343 threshold, delta, msecs);
344 return false;
345 }
346
347 return true;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100348}
349
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700350static void iwlagn_recover_from_statistics(struct iwl_priv *priv,
351 struct statistics_rx_phy *cur_ofdm,
352 struct statistics_rx_ht_phy *cur_ofdm_ht,
353 struct statistics_tx *tx,
354 unsigned long stamp)
Wey-Yi Guyfa8f1302010-03-05 14:22:46 -0800355{
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100356 unsigned int msecs;
Stanislaw Gruszkab7977ff2011-02-28 14:33:15 +0100357
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700358 if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100359 return;
360
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100361 msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
362
363 /* Only gather statistics and update time stamp when not associated */
364 if (!iwl_is_any_associated(priv))
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700365 return;
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100366
367 /* Do not check/recover when do not have enough statistics data */
368 if (msecs < 99)
Wey-Yi Guyfa8f1302010-03-05 14:22:46 -0800369 return;
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100370
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700371 if (iwlagn_mod_params.ack_check && !iwlagn_good_ack_health(priv, tx)) {
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100372 IWL_ERR(priv, "low ack count detected, restart firmware\n");
373 if (!iwl_force_reset(priv, IWL_FW_RESET, false))
374 return;
Trieu 'Andrew' Nguyen3e4fb5f2010-01-22 14:22:46 -0800375 }
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100376
Don Fry9d143e92011-04-20 15:23:57 -0700377 if (iwlagn_mod_params.plcp_check &&
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700378 !iwlagn_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100379 iwl_force_reset(priv, IWL_RF_RESET, false);
Wey-Yi Guybeac5492010-03-04 13:38:58 -0800380}
Wey-Yi Guybeac5492010-03-04 13:38:58 -0800381
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100382/* Calculate noise level, based on measurements during network silence just
383 * before arriving beacon. This measurement can be done only if we know
384 * exactly when to expect beacons, therefore only when we're associated. */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700385static void iwlagn_rx_calc_noise(struct iwl_priv *priv)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100386{
387 struct statistics_rx_non_phy *rx_info;
388 int num_active_rx = 0;
389 int total_silence = 0;
390 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
391 int last_rx_noise;
392
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700393 rx_info = &priv->statistics.rx_non_phy;
394
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100395 bcn_silence_a =
396 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
397 bcn_silence_b =
398 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
399 bcn_silence_c =
400 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
401
402 if (bcn_silence_a) {
403 total_silence += bcn_silence_a;
404 num_active_rx++;
405 }
406 if (bcn_silence_b) {
407 total_silence += bcn_silence_b;
408 num_active_rx++;
409 }
410 if (bcn_silence_c) {
411 total_silence += bcn_silence_c;
412 num_active_rx++;
413 }
414
415 /* Average among active antennas */
416 if (num_active_rx)
417 last_rx_noise = (total_silence / num_active_rx) - 107;
418 else
419 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
420
421 IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
422 bcn_silence_a, bcn_silence_b, bcn_silence_c,
423 last_rx_noise);
424}
425
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700426#ifdef CONFIG_IWLWIFI_DEBUGFS
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100427/*
428 * based on the assumption of all statistics counter are in DWORD
429 * FIXME: This function is for debugging, do not deal with
430 * the case of counters roll-over.
431 */
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700432static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
433 __le32 *max_delta, __le32 *accum, int size)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100434{
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700435 int i;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100436
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700437 for (i = 0;
438 i < size / sizeof(__le32);
439 i++, prev++, cur++, delta++, max_delta++, accum++) {
440 if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
441 *delta = cpu_to_le32(
442 le32_to_cpu(*cur) - le32_to_cpu(*prev));
443 le32_add_cpu(accum, le32_to_cpu(*delta));
444 if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100445 *max_delta = *delta;
446 }
447 }
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100448}
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100449
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700450static void
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700451iwlagn_accumulative_statistics(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700452 struct statistics_general_common *common,
453 struct statistics_rx_non_phy *rx_non_phy,
454 struct statistics_rx_phy *rx_ofdm,
455 struct statistics_rx_ht_phy *rx_ofdm_ht,
456 struct statistics_rx_phy *rx_cck,
457 struct statistics_tx *tx,
458 struct statistics_bt_activity *bt_activity)
459{
460#define ACCUM(_name) \
461 accum_stats((__le32 *)&priv->statistics._name, \
462 (__le32 *)_name, \
463 (__le32 *)&priv->delta_stats._name, \
464 (__le32 *)&priv->max_delta_stats._name, \
465 (__le32 *)&priv->accum_stats._name, \
466 sizeof(*_name));
467
468 ACCUM(common);
469 ACCUM(rx_non_phy);
470 ACCUM(rx_ofdm);
471 ACCUM(rx_ofdm_ht);
472 ACCUM(rx_cck);
473 ACCUM(tx);
474 if (bt_activity)
475 ACCUM(bt_activity);
476#undef ACCUM
477}
478#else
479static inline void
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700480iwlagn_accumulative_statistics(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700481 struct statistics_general_common *common,
482 struct statistics_rx_non_phy *rx_non_phy,
483 struct statistics_rx_phy *rx_ofdm,
484 struct statistics_rx_ht_phy *rx_ofdm_ht,
485 struct statistics_rx_phy *rx_cck,
486 struct statistics_tx *tx,
487 struct statistics_bt_activity *bt_activity)
488{
489}
490#endif
491
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700492static int iwlagn_rx_statistics(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800493 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700494 struct iwl_device_cmd *cmd)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100495{
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700496 unsigned long stamp = jiffies;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100497 const int reg_recalib_period = 60;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100498 int change;
499 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700500 u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
501 __le32 *flag;
502 struct statistics_general_common *common;
503 struct statistics_rx_non_phy *rx_non_phy;
504 struct statistics_rx_phy *rx_ofdm;
505 struct statistics_rx_ht_phy *rx_ofdm_ht;
506 struct statistics_rx_phy *rx_cck;
507 struct statistics_tx *tx;
508 struct statistics_bt_activity *bt_activity;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100509
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700510 len -= sizeof(struct iwl_cmd_header); /* skip header */
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100511
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700512 IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
513 len);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100514
Johannes Berg4ff70fcd2012-03-05 11:24:26 -0800515 spin_lock(&priv->statistics.lock);
516
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700517 if (len == sizeof(struct iwl_bt_notif_statistics)) {
518 struct iwl_bt_notif_statistics *stats;
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800519 stats = (void *)&pkt->data;
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700520 flag = &stats->flag;
521 common = &stats->general.common;
522 rx_non_phy = &stats->rx.general.common;
523 rx_ofdm = &stats->rx.ofdm;
524 rx_ofdm_ht = &stats->rx.ofdm_ht;
525 rx_cck = &stats->rx.cck;
526 tx = &stats->tx;
527 bt_activity = &stats->general.activity;
528
529#ifdef CONFIG_IWLWIFI_DEBUGFS
530 /* handle this exception directly */
531 priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
532 le32_add_cpu(&priv->statistics.accum_num_bt_kills,
533 le32_to_cpu(stats->rx.general.num_bt_kills));
534#endif
535 } else if (len == sizeof(struct iwl_notif_statistics)) {
536 struct iwl_notif_statistics *stats;
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800537 stats = (void *)&pkt->data;
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700538 flag = &stats->flag;
539 common = &stats->general.common;
540 rx_non_phy = &stats->rx.general;
541 rx_ofdm = &stats->rx.ofdm;
542 rx_ofdm_ht = &stats->rx.ofdm_ht;
543 rx_cck = &stats->rx.cck;
544 tx = &stats->tx;
545 bt_activity = NULL;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100546 } else {
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700547 WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
548 len, sizeof(struct iwl_bt_notif_statistics),
549 sizeof(struct iwl_notif_statistics));
Johannes Berg4ff70fcd2012-03-05 11:24:26 -0800550 spin_unlock(&priv->statistics.lock);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700551 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100552 }
553
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700554 change = common->temperature != priv->statistics.common.temperature ||
555 (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
556 (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
557
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700558 iwlagn_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700559 rx_ofdm_ht, rx_cck, tx, bt_activity);
560
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700561 iwlagn_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700562
563 priv->statistics.flag = *flag;
564 memcpy(&priv->statistics.common, common, sizeof(*common));
565 memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
566 memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
567 memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
568 memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
569 memcpy(&priv->statistics.tx, tx, sizeof(*tx));
570#ifdef CONFIG_IWLWIFI_DEBUGFS
571 if (bt_activity)
572 memcpy(&priv->statistics.bt_activity, bt_activity,
573 sizeof(*bt_activity));
574#endif
575
576 priv->rx_statistics_jiffies = stamp;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100577
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700578 set_bit(STATUS_STATISTICS, &priv->shrd->status);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100579
580 /* Reschedule the statistics timer to occur in
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100581 * reg_recalib_period seconds to ensure we get a
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100582 * thermal update even if the uCode doesn't give
583 * us one */
584 mod_timer(&priv->statistics_periodic, jiffies +
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100585 msecs_to_jiffies(reg_recalib_period * 1000));
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100586
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700587 if (unlikely(!test_bit(STATUS_SCANNING, &priv->shrd->status)) &&
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100588 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700589 iwlagn_rx_calc_noise(priv);
Johannes Berg1ee158d2012-02-17 10:07:44 -0800590 queue_work(priv->workqueue, &priv->run_time_calib_work);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100591 }
Don Fry38622412011-12-16 07:07:36 -0800592 if (cfg(priv)->lib->temperature && change)
593 cfg(priv)->lib->temperature(priv);
Johannes Berg4ff70fcd2012-03-05 11:24:26 -0800594
595 spin_unlock(&priv->statistics.lock);
596
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700597 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100598}
599
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700600static int iwlagn_rx_reply_statistics(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800601 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700602 struct iwl_device_cmd *cmd)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100603{
604 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800605 struct iwl_notif_statistics *stats = (void *)pkt->data;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100606
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800607 if (le32_to_cpu(stats->flag) & UCODE_STATISTICS_CLEAR_MSK) {
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100608#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700609 memset(&priv->accum_stats, 0,
610 sizeof(priv->accum_stats));
611 memset(&priv->delta_stats, 0,
612 sizeof(priv->delta_stats));
613 memset(&priv->max_delta_stats, 0,
614 sizeof(priv->max_delta_stats));
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100615#endif
616 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
617 }
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700618 iwlagn_rx_statistics(priv, rxb, cmd);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700619 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100620}
621
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100622/* Handle notification from uCode that card's power state is changing
623 * due to software, hardware, or critical temperature RFKILL */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700624static int iwlagn_rx_card_state_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800625 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700626 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100627{
628 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800629 struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
630 u32 flags = le32_to_cpu(card_state_notif->flags);
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700631 unsigned long status = priv->shrd->status;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100632
633 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
634 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
635 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
636 (flags & CT_CARD_DISABLED) ?
637 "Reached" : "Not reached");
638
639 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
640 CT_CARD_DISABLED)) {
641
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200642 iwl_write32(trans(priv), CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100643 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
644
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200645 iwl_write_direct32(trans(priv), HBUS_TARG_MBX_C,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100646 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
647
648 if (!(flags & RXON_CARD_DISABLED)) {
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200649 iwl_write32(trans(priv), CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100650 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Emmanuel Grumbach1042db22012-01-03 16:56:15 +0200651 iwl_write_direct32(trans(priv), HBUS_TARG_MBX_C,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100652 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
653 }
654 if (flags & CT_CARD_DISABLED)
655 iwl_tt_enter_ct_kill(priv);
656 }
657 if (!(flags & CT_CARD_DISABLED))
658 iwl_tt_exit_ct_kill(priv);
659
660 if (flags & HW_CARD_DISABLED)
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700661 set_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100662 else
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700663 clear_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100664
665
666 if (!(flags & RXON_CARD_DISABLED))
667 iwl_scan_cancel(priv);
668
669 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700670 test_bit(STATUS_RF_KILL_HW, &priv->shrd->status)))
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100671 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700672 test_bit(STATUS_RF_KILL_HW, &priv->shrd->status));
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100673 else
Johannes Bergeffd4d92011-09-15 11:46:52 -0700674 wake_up(&priv->shrd->wait_command_queue);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700675 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100676}
677
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700678static int iwlagn_rx_missed_beacon_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800679 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700680 struct iwl_device_cmd *cmd)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100681
682{
683 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800684 struct iwl_missed_beacon_notif *missed_beacon = (void *)pkt->data;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100685
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100686 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
687 priv->missed_beacon_threshold) {
688 IWL_DEBUG_CALIB(priv,
689 "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
690 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
691 le32_to_cpu(missed_beacon->total_missed_becons),
692 le32_to_cpu(missed_beacon->num_recvd_beacons),
693 le32_to_cpu(missed_beacon->num_expected_beacons));
Emmanuel Grumbach63013ae2011-08-25 23:10:42 -0700694 if (!test_bit(STATUS_SCANNING, &priv->shrd->status))
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100695 iwl_init_sensitivity(priv);
696 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700697 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100698}
699
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100700/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
701 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700702static int iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800703 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700704 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100705{
706 struct iwl_rx_packet *pkt = rxb_addr(rxb);
707
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700708 priv->last_phy_res_valid = true;
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800709 memcpy(&priv->last_phy_res, pkt->data,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100710 sizeof(struct iwl_rx_phy_res));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700711 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100712}
713
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800714/*
715 * returns non-zero if packet should be dropped
716 */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700717static int iwlagn_set_decrypted_flag(struct iwl_priv *priv,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100718 struct ieee80211_hdr *hdr,
719 u32 decrypt_res,
720 struct ieee80211_rx_status *stats)
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800721{
722 u16 fc = le16_to_cpu(hdr->frame_control);
723
Johannes Berg246ed352010-08-23 10:46:32 +0200724 /*
725 * All contexts have the same setting here due to it being
726 * a module parameter, so OK to check any context.
727 */
728 if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
729 RXON_FILTER_DIS_DECRYPT_MSK)
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800730 return 0;
731
732 if (!(fc & IEEE80211_FCTL_PROTECTED))
733 return 0;
734
Tomas Winklere1623442009-01-27 14:27:56 -0800735 IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800736 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
737 case RX_RES_STATUS_SEC_TYPE_TKIP:
738 /* The uCode has got a bad phase 1 Key, pushes the packet.
739 * Decryption will be done in SW. */
740 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
741 RX_RES_STATUS_BAD_KEY_TTAK)
742 break;
743
744 case RX_RES_STATUS_SEC_TYPE_WEP:
745 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
746 RX_RES_STATUS_BAD_ICV_MIC) {
747 /* bad ICV, the packet is destroyed since the
748 * decryption is inplace, drop it */
Tomas Winklere1623442009-01-27 14:27:56 -0800749 IWL_DEBUG_RX(priv, "Packet destroyed\n");
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800750 return -1;
751 }
752 case RX_RES_STATUS_SEC_TYPE_CCMP:
753 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
754 RX_RES_STATUS_DECRYPT_OK) {
Tomas Winklere1623442009-01-27 14:27:56 -0800755 IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800756 stats->flag |= RX_FLAG_DECRYPTED;
757 }
758 break;
759
760 default:
761 break;
762 }
763 return 0;
764}
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100765
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700766static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100767 struct ieee80211_hdr *hdr,
768 u16 len,
769 u32 ampdu_status,
Johannes Berg48a2d662012-03-05 11:24:39 -0800770 struct iwl_rx_cmd_buffer *rxb,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100771 struct ieee80211_rx_status *stats)
772{
773 struct sk_buff *skb;
774 __le16 fc = hdr->frame_control;
Garen Tamrazian68b99312011-03-30 02:29:32 -0700775 struct iwl_rxon_context *ctx;
Johannes Berg48a2d662012-03-05 11:24:39 -0800776 struct page *p;
777 int offset;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100778
779 /* We only process data packets if the interface is open */
780 if (unlikely(!priv->is_open)) {
781 IWL_DEBUG_DROP_LIMIT(priv,
782 "Dropping packet while interface is not open.\n");
783 return;
784 }
785
786 /* In case of HW accelerated crypto and bad decryption, drop */
Don Fry9d143e92011-04-20 15:23:57 -0700787 if (!iwlagn_mod_params.sw_crypto &&
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700788 iwlagn_set_decrypted_flag(priv, hdr, ampdu_status, stats))
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100789 return;
790
791 skb = dev_alloc_skb(128);
792 if (!skb) {
793 IWL_ERR(priv, "dev_alloc_skb failed\n");
794 return;
795 }
796
Johannes Berg48a2d662012-03-05 11:24:39 -0800797 offset = (void *)hdr - rxb_addr(rxb);
798 p = rxb_steal_page(rxb);
799 skb_add_rx_frag(skb, 0, p, offset, len);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100800
801 iwl_update_stats(priv, false, fc, len);
Garen Tamrazian68b99312011-03-30 02:29:32 -0700802
803 /*
804 * Wake any queues that were stopped due to a passive channel tx
805 * failure. This can happen because the regulatory enforcement in
806 * the device waits for a beacon before allowing transmission,
807 * sometimes even after already having transmitted frames for the
808 * association because the new RXON may reset the information.
809 */
Johannes Berge755f882012-03-07 09:52:16 -0800810 if (unlikely(ieee80211_is_beacon(fc) && priv->passive_no_rx)) {
Garen Tamrazian68b99312011-03-30 02:29:32 -0700811 for_each_context(priv, ctx) {
Garen Tamrazian68b99312011-03-30 02:29:32 -0700812 if (compare_ether_addr(hdr->addr3,
813 ctx->active.bssid_addr))
814 continue;
Johannes Berge755f882012-03-07 09:52:16 -0800815 iwlagn_lift_passive_no_rx(priv);
Garen Tamrazian68b99312011-03-30 02:29:32 -0700816 }
817 }
818
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100819 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
820
821 ieee80211_rx(priv->hw, skb);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100822}
823
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700824static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100825{
826 u32 decrypt_out = 0;
827
828 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
829 RX_RES_STATUS_STATION_FOUND)
830 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
831 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
832
833 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
834
835 /* packet was not encrypted */
836 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
837 RX_RES_STATUS_SEC_TYPE_NONE)
838 return decrypt_out;
839
840 /* packet was encrypted with unknown alg */
841 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
842 RX_RES_STATUS_SEC_TYPE_ERR)
843 return decrypt_out;
844
845 /* decryption was not done in HW */
846 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
847 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
848 return decrypt_out;
849
850 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
851
852 case RX_RES_STATUS_SEC_TYPE_CCMP:
853 /* alg is CCM: check MIC only */
854 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
855 /* Bad MIC */
856 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
857 else
858 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
859
860 break;
861
862 case RX_RES_STATUS_SEC_TYPE_TKIP:
863 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
864 /* Bad TTAK */
865 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
866 break;
867 }
868 /* fall through if TTAK OK */
869 default:
870 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
871 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
872 else
873 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
874 break;
875 }
876
877 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
878 decrypt_in, decrypt_out);
879
880 return decrypt_out;
881}
882
Don Fry5c3d29f2011-07-08 08:46:29 -0700883/* Calc max signal level (dBm) among 3 possible receivers */
884static int iwlagn_calc_rssi(struct iwl_priv *priv,
885 struct iwl_rx_phy_res *rx_resp)
886{
887 /* data from PHY/DSP regarding signal strength, etc.,
888 * contents are always there, not configurable by host
889 */
890 struct iwlagn_non_cfg_phy *ncphy =
891 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
892 u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
893 u8 agc;
894
895 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
896 agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
897
898 /* Find max rssi among 3 possible receivers.
899 * These values are measured by the digital signal processor (DSP).
900 * They should stay fairly constant even as the signal strength varies,
901 * if the radio's automatic gain control (AGC) is working right.
902 * AGC value (see below) will provide the "interesting" info.
903 */
904 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
905 rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
906 IWLAGN_OFDM_RSSI_A_BIT_POS;
907 rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
908 IWLAGN_OFDM_RSSI_B_BIT_POS;
909 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
910 rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
911 IWLAGN_OFDM_RSSI_C_BIT_POS;
912
913 max_rssi = max_t(u32, rssi_a, rssi_b);
914 max_rssi = max_t(u32, max_rssi, rssi_c);
915
916 IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
917 rssi_a, rssi_b, rssi_c, max_rssi, agc);
918
919 /* dBm = max_rssi dB - agc dB - constant.
920 * Higher AGC (higher radio gain) means lower signal. */
921 return max_rssi - agc - IWLAGN_RSSI_OFFSET;
922}
923
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100924/* Called for REPLY_RX (legacy ABG frames), or
925 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700926static int iwlagn_rx_reply_rx(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800927 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700928 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100929{
930 struct ieee80211_hdr *header;
931 struct ieee80211_rx_status rx_status;
932 struct iwl_rx_packet *pkt = rxb_addr(rxb);
933 struct iwl_rx_phy_res *phy_res;
934 __le32 rx_pkt_status;
935 struct iwl_rx_mpdu_res_start *amsdu;
936 u32 len;
937 u32 ampdu_status;
938 u32 rate_n_flags;
939
940 /**
941 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
942 * REPLY_RX: physical layer info is in this buffer
943 * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
944 * command and cached in priv->last_phy_res
945 *
946 * Here we set up local variables depending on which command is
947 * received.
948 */
949 if (pkt->hdr.cmd == REPLY_RX) {
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800950 phy_res = (struct iwl_rx_phy_res *)pkt->data;
951 header = (struct ieee80211_hdr *)(pkt->data + sizeof(*phy_res)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100952 + phy_res->cfg_phy_cnt);
953
954 len = le16_to_cpu(phy_res->byte_count);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800955 rx_pkt_status = *(__le32 *)(pkt->data + sizeof(*phy_res) +
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100956 phy_res->cfg_phy_cnt + len);
957 ampdu_status = le32_to_cpu(rx_pkt_status);
958 } else {
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700959 if (!priv->last_phy_res_valid) {
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100960 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700961 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100962 }
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700963 phy_res = &priv->last_phy_res;
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800964 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->data;
965 header = (struct ieee80211_hdr *)(pkt->data + sizeof(*amsdu));
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100966 len = le16_to_cpu(amsdu->byte_count);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800967 rx_pkt_status = *(__le32 *)(pkt->data + sizeof(*amsdu) + len);
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700968 ampdu_status = iwlagn_translate_rx_status(priv,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100969 le32_to_cpu(rx_pkt_status));
970 }
971
972 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
973 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
974 phy_res->cfg_phy_cnt);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700975 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100976 }
977
978 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
979 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
980 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
981 le32_to_cpu(rx_pkt_status));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700982 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100983 }
984
985 /* This will be used in several places later */
986 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
987
988 /* rx_status carries information about the packet to mac80211 */
989 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
990 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
991 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
992 rx_status.freq =
993 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
994 rx_status.band);
995 rx_status.rate_idx =
996 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
997 rx_status.flag = 0;
998
999 /* TSF isn't reliable. In order to allow smooth user experience,
1000 * this W/A doesn't propagate it to the mac80211 */
1001 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
1002
1003 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
1004
1005 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
Don Fry5c3d29f2011-07-08 08:46:29 -07001006 rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001007
1008 iwl_dbg_log_rx_data_frame(priv, len, header);
1009 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
1010 rx_status.signal, (unsigned long long)rx_status.mactime);
1011
1012 /*
1013 * "antenna number"
1014 *
1015 * It seems that the antenna field in the phy flags value
1016 * is actually a bit field. This is undefined by radiotap,
1017 * it wants an actual antenna number but I always get "7"
1018 * for most legacy frames I receive indicating that the
1019 * same frame was received on all three RX chains.
1020 *
1021 * I think this field should be removed in favor of a
1022 * new 802.11n radiotap field "RX chains" that is defined
1023 * as a bitmask.
1024 */
1025 rx_status.antenna =
1026 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
1027 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
1028
1029 /* set the preamble flag if appropriate */
1030 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
1031 rx_status.flag |= RX_FLAG_SHORTPRE;
1032
1033 /* Set up the HT phy flags */
1034 if (rate_n_flags & RATE_MCS_HT_MSK)
1035 rx_status.flag |= RX_FLAG_HT;
1036 if (rate_n_flags & RATE_MCS_HT40_MSK)
1037 rx_status.flag |= RX_FLAG_40MHZ;
1038 if (rate_n_flags & RATE_MCS_SGI_MSK)
1039 rx_status.flag |= RX_FLAG_SHORT_GI;
1040
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -07001041 iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001042 rxb, &rx_status);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001043 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001044}
1045
Johannes Berg79d3eef2011-11-10 06:55:01 -08001046static int iwlagn_rx_noa_notification(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -08001047 struct iwl_rx_cmd_buffer *rxb,
Johannes Berg79d3eef2011-11-10 06:55:01 -08001048 struct iwl_device_cmd *cmd)
1049{
1050 struct iwl_wipan_noa_data *new_data, *old_data;
1051 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -08001052 struct iwl_wipan_noa_notification *noa_notif = (void *)pkt->data;
Johannes Berg79d3eef2011-11-10 06:55:01 -08001053
1054 /* no condition -- we're in softirq */
1055 old_data = rcu_dereference_protected(priv->noa_data, true);
1056
1057 if (noa_notif->noa_active) {
1058 u32 len = le16_to_cpu(noa_notif->noa_attribute.length);
1059 u32 copylen = len;
1060
1061 /* EID, len, OUI, subtype */
1062 len += 1 + 1 + 3 + 1;
1063 /* P2P id, P2P length */
1064 len += 1 + 2;
1065 copylen += 1 + 2;
1066
1067 new_data = kmalloc(sizeof(*new_data) + len, GFP_ATOMIC);
1068 if (new_data) {
1069 new_data->length = len;
1070 new_data->data[0] = WLAN_EID_VENDOR_SPECIFIC;
1071 new_data->data[1] = len - 2; /* not counting EID, len */
1072 new_data->data[2] = (WLAN_OUI_WFA >> 16) & 0xff;
1073 new_data->data[3] = (WLAN_OUI_WFA >> 8) & 0xff;
1074 new_data->data[4] = (WLAN_OUI_WFA >> 0) & 0xff;
1075 new_data->data[5] = WLAN_OUI_TYPE_WFA_P2P;
1076 memcpy(&new_data->data[6], &noa_notif->noa_attribute,
1077 copylen);
1078 }
1079 } else
1080 new_data = NULL;
1081
1082 rcu_assign_pointer(priv->noa_data, new_data);
1083
1084 if (old_data)
1085 kfree_rcu(old_data, rcu_head);
1086
1087 return 0;
1088}
1089
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001090/**
1091 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
1092 *
1093 * Setup the RX handlers for each of the reply types sent from the uCode
1094 * to the host.
1095 */
1096void iwl_setup_rx_handlers(struct iwl_priv *priv)
1097{
Johannes Berg48a2d662012-03-05 11:24:39 -08001098 int (**handlers)(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001099 struct iwl_device_cmd *cmd);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001100
1101 handlers = priv->rx_handlers;
1102
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -07001103 handlers[REPLY_ERROR] = iwlagn_rx_reply_error;
1104 handlers[CHANNEL_SWITCH_NOTIFICATION] = iwlagn_rx_csa;
1105 handlers[SPECTRUM_MEASURE_NOTIFICATION] =
1106 iwlagn_rx_spectrum_measure_notif;
1107 handlers[PM_SLEEP_NOTIFICATION] = iwlagn_rx_pm_sleep_notif;
1108 handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
1109 iwlagn_rx_pm_debug_statistics_notif;
1110 handlers[BEACON_NOTIFICATION] = iwlagn_rx_beacon_notif;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001111 handlers[REPLY_ADD_STA] = iwl_add_sta_callback;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001112
Johannes Berg79d3eef2011-11-10 06:55:01 -08001113 handlers[REPLY_WIPAN_NOA_NOTIFICATION] = iwlagn_rx_noa_notification;
1114
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001115 /*
1116 * The same handler is used for both the REPLY to a discrete
1117 * statistics request from the host as well as for the periodic
1118 * statistics notifications (after received beacons) from the uCode.
1119 */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -07001120 handlers[REPLY_STATISTICS_CMD] = iwlagn_rx_reply_statistics;
1121 handlers[STATISTICS_NOTIFICATION] = iwlagn_rx_statistics;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001122
1123 iwl_setup_rx_scan_handlers(priv);
1124
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -07001125 handlers[CARD_STATE_NOTIFICATION] = iwlagn_rx_card_state_notif;
1126 handlers[MISSED_BEACONS_NOTIFICATION] =
1127 iwlagn_rx_missed_beacon_notif;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001128
1129 /* Rx handlers */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -07001130 handlers[REPLY_RX_PHY_CMD] = iwlagn_rx_reply_rx_phy;
1131 handlers[REPLY_RX_MPDU_CMD] = iwlagn_rx_reply_rx;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001132
1133 /* block ack */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -07001134 handlers[REPLY_COMPRESSED_BA] =
1135 iwlagn_rx_reply_compressed_ba;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001136
Emmanuel Grumbach8d301192011-07-03 15:45:34 +03001137 /* init calibration handlers */
1138 priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
1139 iwlagn_rx_calib_result;
1140 priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
1141
1142 /* set up notification wait support */
Johannes Berg4bd14dd2012-03-06 13:30:58 -08001143 iwl_notification_wait_init(&priv->notif_wait);
Emmanuel Grumbach8d301192011-07-03 15:45:34 +03001144
1145 /* Set up BT Rx handlers */
Don Fry38622412011-12-16 07:07:36 -08001146 if (cfg(priv)->lib->bt_rx_handler_setup)
1147 cfg(priv)->lib->bt_rx_handler_setup(priv);
Emmanuel Grumbach8d301192011-07-03 15:45:34 +03001148
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001149}
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001150
Johannes Berg48a2d662012-03-05 11:24:39 -08001151int iwl_rx_dispatch(struct iwl_op_mode *op_mode, struct iwl_rx_cmd_buffer *rxb,
1152 struct iwl_device_cmd *cmd)
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001153{
1154 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbachdb70f292012-02-09 16:08:15 +02001155 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001156 int err = 0;
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001157
1158 /*
1159 * Do the notification wait before RX handlers so
1160 * even if the RX handler consumes the RXB we have
1161 * access to it in the notification wait entry.
1162 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -08001163 iwl_notification_wait_notify(&priv->notif_wait, pkt);
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001164
Wey-Yi Guy64eeacb2011-12-16 09:31:14 -08001165 if (priv->pre_rx_handler &&
Johannes Berg947a9402012-03-06 13:30:59 -08001166 priv->ucode_owner == IWL_OWNERSHIP_TM)
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001167 priv->pre_rx_handler(priv, rxb);
Wey-Yi Guy64eeacb2011-12-16 09:31:14 -08001168 else {
1169 /* Based on type of command response or notification,
1170 * handle those that need handling via function in
1171 * rx_handlers table. See iwl_setup_rx_handlers() */
1172 if (priv->rx_handlers[pkt->hdr.cmd]) {
1173 priv->rx_handlers_stats[pkt->hdr.cmd]++;
1174 err = priv->rx_handlers[pkt->hdr.cmd] (priv, rxb, cmd);
1175 } else {
1176 /* No handling needed */
1177 IWL_DEBUG_RX(priv,
1178 "No handler needed for %s, 0x%02x\n",
1179 get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
1180 }
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001181 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001182 return err;
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001183}