blob: 6b2007a7abd84d8356a7704b7357683231e64cc9 [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"
Tomas Winklera55360e2008-05-05 10:22:28 +080037#include "iwl-io.h"
Stanislaw Gruszka67289942011-02-28 14:33:17 +010038#include "iwl-agn-calib.h"
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010039#include "iwl-agn.h"
40
Johannes Bergd9fb6462012-03-26 08:23:39 -070041#define IWL_CMD_ENTRY(x) [x] = #x
Emmanuel Grumbach522376d2011-09-06 09:31:19 -070042
Johannes Bergd9fb6462012-03-26 08:23:39 -070043const char *iwl_dvm_cmd_strings[REPLY_MAX] = {
44 IWL_CMD_ENTRY(REPLY_ALIVE),
45 IWL_CMD_ENTRY(REPLY_ERROR),
46 IWL_CMD_ENTRY(REPLY_ECHO),
47 IWL_CMD_ENTRY(REPLY_RXON),
48 IWL_CMD_ENTRY(REPLY_RXON_ASSOC),
49 IWL_CMD_ENTRY(REPLY_QOS_PARAM),
50 IWL_CMD_ENTRY(REPLY_RXON_TIMING),
51 IWL_CMD_ENTRY(REPLY_ADD_STA),
52 IWL_CMD_ENTRY(REPLY_REMOVE_STA),
53 IWL_CMD_ENTRY(REPLY_REMOVE_ALL_STA),
54 IWL_CMD_ENTRY(REPLY_TXFIFO_FLUSH),
55 IWL_CMD_ENTRY(REPLY_WEPKEY),
56 IWL_CMD_ENTRY(REPLY_TX),
57 IWL_CMD_ENTRY(REPLY_LEDS_CMD),
58 IWL_CMD_ENTRY(REPLY_TX_LINK_QUALITY_CMD),
59 IWL_CMD_ENTRY(COEX_PRIORITY_TABLE_CMD),
60 IWL_CMD_ENTRY(COEX_MEDIUM_NOTIFICATION),
61 IWL_CMD_ENTRY(COEX_EVENT_CMD),
62 IWL_CMD_ENTRY(REPLY_QUIET_CMD),
63 IWL_CMD_ENTRY(REPLY_CHANNEL_SWITCH),
64 IWL_CMD_ENTRY(CHANNEL_SWITCH_NOTIFICATION),
65 IWL_CMD_ENTRY(REPLY_SPECTRUM_MEASUREMENT_CMD),
66 IWL_CMD_ENTRY(SPECTRUM_MEASURE_NOTIFICATION),
67 IWL_CMD_ENTRY(POWER_TABLE_CMD),
68 IWL_CMD_ENTRY(PM_SLEEP_NOTIFICATION),
69 IWL_CMD_ENTRY(PM_DEBUG_STATISTIC_NOTIFIC),
70 IWL_CMD_ENTRY(REPLY_SCAN_CMD),
71 IWL_CMD_ENTRY(REPLY_SCAN_ABORT_CMD),
72 IWL_CMD_ENTRY(SCAN_START_NOTIFICATION),
73 IWL_CMD_ENTRY(SCAN_RESULTS_NOTIFICATION),
74 IWL_CMD_ENTRY(SCAN_COMPLETE_NOTIFICATION),
75 IWL_CMD_ENTRY(BEACON_NOTIFICATION),
76 IWL_CMD_ENTRY(REPLY_TX_BEACON),
77 IWL_CMD_ENTRY(WHO_IS_AWAKE_NOTIFICATION),
78 IWL_CMD_ENTRY(QUIET_NOTIFICATION),
79 IWL_CMD_ENTRY(REPLY_TX_PWR_TABLE_CMD),
80 IWL_CMD_ENTRY(MEASURE_ABORT_NOTIFICATION),
81 IWL_CMD_ENTRY(REPLY_BT_CONFIG),
82 IWL_CMD_ENTRY(REPLY_STATISTICS_CMD),
83 IWL_CMD_ENTRY(STATISTICS_NOTIFICATION),
84 IWL_CMD_ENTRY(REPLY_CARD_STATE_CMD),
85 IWL_CMD_ENTRY(CARD_STATE_NOTIFICATION),
86 IWL_CMD_ENTRY(MISSED_BEACONS_NOTIFICATION),
87 IWL_CMD_ENTRY(REPLY_CT_KILL_CONFIG_CMD),
88 IWL_CMD_ENTRY(SENSITIVITY_CMD),
89 IWL_CMD_ENTRY(REPLY_PHY_CALIBRATION_CMD),
90 IWL_CMD_ENTRY(REPLY_RX_PHY_CMD),
91 IWL_CMD_ENTRY(REPLY_RX_MPDU_CMD),
92 IWL_CMD_ENTRY(REPLY_RX),
93 IWL_CMD_ENTRY(REPLY_COMPRESSED_BA),
94 IWL_CMD_ENTRY(CALIBRATION_CFG_CMD),
95 IWL_CMD_ENTRY(CALIBRATION_RES_NOTIFICATION),
96 IWL_CMD_ENTRY(CALIBRATION_COMPLETE_NOTIFICATION),
97 IWL_CMD_ENTRY(REPLY_TX_POWER_DBM_CMD),
98 IWL_CMD_ENTRY(TEMPERATURE_NOTIFICATION),
99 IWL_CMD_ENTRY(TX_ANT_CONFIGURATION_CMD),
100 IWL_CMD_ENTRY(REPLY_BT_COEX_PROFILE_NOTIF),
101 IWL_CMD_ENTRY(REPLY_BT_COEX_PRIO_TABLE),
102 IWL_CMD_ENTRY(REPLY_BT_COEX_PROT_ENV),
103 IWL_CMD_ENTRY(REPLY_WIPAN_PARAMS),
104 IWL_CMD_ENTRY(REPLY_WIPAN_RXON),
105 IWL_CMD_ENTRY(REPLY_WIPAN_RXON_TIMING),
106 IWL_CMD_ENTRY(REPLY_WIPAN_RXON_ASSOC),
107 IWL_CMD_ENTRY(REPLY_WIPAN_QOS_PARAM),
108 IWL_CMD_ENTRY(REPLY_WIPAN_WEPKEY),
109 IWL_CMD_ENTRY(REPLY_WIPAN_P2P_CHANNEL_SWITCH),
110 IWL_CMD_ENTRY(REPLY_WIPAN_NOA_NOTIFICATION),
111 IWL_CMD_ENTRY(REPLY_WIPAN_DEACTIVATION_COMPLETE),
112 IWL_CMD_ENTRY(REPLY_WOWLAN_PATTERNS),
113 IWL_CMD_ENTRY(REPLY_WOWLAN_WAKEUP_FILTER),
114 IWL_CMD_ENTRY(REPLY_WOWLAN_TSC_RSC_PARAMS),
115 IWL_CMD_ENTRY(REPLY_WOWLAN_TKIP_PARAMS),
116 IWL_CMD_ENTRY(REPLY_WOWLAN_KEK_KCK_MATERIAL),
117 IWL_CMD_ENTRY(REPLY_WOWLAN_GET_STATUS),
118 IWL_CMD_ENTRY(REPLY_D3_CONFIG),
119};
120#undef IWL_CMD_ENTRY
Tomas Winklera55360e2008-05-05 10:22:28 +0800121
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100122/******************************************************************************
123 *
124 * Generic RX handler implementations
125 *
126 ******************************************************************************/
127
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700128static int iwlagn_rx_reply_error(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800129 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700130 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100131{
132 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800133 struct iwl_error_resp *err_resp = (void *)pkt->data;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100134
Johannes Bergd9fb6462012-03-26 08:23:39 -0700135 IWL_ERR(priv, "Error Reply type 0x%08X cmd REPLY_ERROR (0x%02X) "
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100136 "seq 0x%04X ser 0x%08X\n",
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800137 le32_to_cpu(err_resp->error_type),
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800138 err_resp->cmd_id,
139 le16_to_cpu(err_resp->bad_cmd_seq_num),
140 le32_to_cpu(err_resp->error_info));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700141 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100142}
143
Johannes Berg48a2d662012-03-05 11:24:39 -0800144static int iwlagn_rx_csa(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700145 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100146{
147 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800148 struct iwl_csa_notification *csa = (void *)pkt->data;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100149 /*
150 * MULTI-FIXME
Wey-Yi Guyade4c642011-10-10 07:27:11 -0700151 * See iwlagn_mac_channel_switch.
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100152 */
153 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
154 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
155
Don Fry83626402012-03-07 09:52:37 -0800156 if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700157 return 0;
Stanislaw Gruszka6f213ff2011-06-02 18:17:15 +0200158
159 if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
160 rxon->channel = csa->channel;
161 ctx->staging.channel = csa->channel;
162 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100163 le16_to_cpu(csa->channel));
Stanislaw Gruszka6f213ff2011-06-02 18:17:15 +0200164 iwl_chswitch_done(priv, true);
165 } else {
166 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
167 le16_to_cpu(csa->channel));
168 iwl_chswitch_done(priv, false);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100169 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700170 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100171}
172
173
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700174static int iwlagn_rx_spectrum_measure_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800175 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700176 struct iwl_device_cmd *cmd)
Reinette Chatre81963d62010-01-22 14:22:57 -0800177{
178 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800179 struct iwl_spectrum_notification *report = (void *)pkt->data;
Reinette Chatre81963d62010-01-22 14:22:57 -0800180
181 if (!report->state) {
182 IWL_DEBUG_11H(priv,
183 "Spectrum Measure Notification: Start\n");
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700184 return 0;
Reinette Chatre81963d62010-01-22 14:22:57 -0800185 }
186
187 memcpy(&priv->measure_report, report, sizeof(*report));
188 priv->measurement_status |= MEASUREMENT_READY;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700189 return 0;
Reinette Chatre81963d62010-01-22 14:22:57 -0800190}
Reinette Chatre81963d62010-01-22 14:22:57 -0800191
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700192static int iwlagn_rx_pm_sleep_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800193 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700194 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100195{
196#ifdef CONFIG_IWLWIFI_DEBUG
197 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800198 struct iwl_sleep_notification *sleep = (void *)pkt->data;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100199 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
200 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
201#endif
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700202 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100203}
204
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700205static int iwlagn_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800206 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700207 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100208{
209 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach44856c62011-08-25 23:10:45 -0700210 u32 __maybe_unused len =
211 le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100212 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
Johannes Bergd9fb6462012-03-26 08:23:39 -0700213 "notification for PM_DEBUG_STATISTIC_NOTIFIC:\n", len);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800214 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->data, len);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700215 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100216}
217
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700218static int iwlagn_rx_beacon_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800219 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700220 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100221{
222 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800223 struct iwlagn_beacon_notif *beacon = (void *)pkt->data;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100224#ifdef CONFIG_IWLWIFI_DEBUG
225 u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
226 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
227
228 IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
229 "tsf:0x%.8x%.8x rate:%d\n",
230 status & TX_STATUS_MSK,
231 beacon->beacon_notify_hdr.failure_frame,
232 le32_to_cpu(beacon->ibss_mgr_status),
233 le32_to_cpu(beacon->high_tsf),
234 le32_to_cpu(beacon->low_tsf), rate);
235#endif
236
237 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
238
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700239 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100240}
241
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100242/**
243 * iwl_good_plcp_health - checks for plcp error.
244 *
245 * When the plcp error is exceeding the thresholds, reset the radio
246 * to improve the throughput.
247 */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700248static bool iwlagn_good_plcp_health(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700249 struct statistics_rx_phy *cur_ofdm,
250 struct statistics_rx_ht_phy *cur_ofdm_ht,
251 unsigned int msecs)
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100252{
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100253 int delta;
Johannes Bergab5c0f12012-03-06 13:30:53 -0800254 int threshold = priv->plcp_delta_threshold;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100255
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100256 if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100257 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100258 return true;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100259 }
260
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700261 delta = le32_to_cpu(cur_ofdm->plcp_err) -
262 le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
263 le32_to_cpu(cur_ofdm_ht->plcp_err) -
264 le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100265
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700266 /* Can be negative if firmware reset statistics */
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100267 if (delta <= 0)
268 return true;
269
270 if ((delta * 100 / msecs) > threshold) {
271 IWL_DEBUG_RADIO(priv,
272 "plcp health threshold %u delta %d msecs %u\n",
273 threshold, delta, msecs);
274 return false;
275 }
276
277 return true;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100278}
279
Meenakshi Venkataraman66cd9e32012-03-14 15:55:13 -0700280int iwl_force_rf_reset(struct iwl_priv *priv, bool external)
281{
282 struct iwl_rf_reset *rf_reset;
283
284 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
285 return -EAGAIN;
286
287 if (!iwl_is_any_associated(priv)) {
288 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
289 return -ENOLINK;
290 }
291
292 rf_reset = &priv->rf_reset;
293 rf_reset->reset_request_count++;
294 if (!external && rf_reset->last_reset_jiffies &&
295 time_after(rf_reset->last_reset_jiffies +
296 IWL_DELAY_NEXT_FORCE_RF_RESET, jiffies)) {
297 IWL_DEBUG_INFO(priv, "RF reset rejected\n");
298 rf_reset->reset_reject_count++;
299 return -EAGAIN;
300 }
301 rf_reset->reset_success_count++;
302 rf_reset->last_reset_jiffies = jiffies;
303
304 /*
305 * There is no easy and better way to force reset the radio,
306 * the only known method is switching channel which will force to
307 * reset and tune the radio.
308 * Use internal short scan (single channel) operation to should
309 * achieve this objective.
310 * Driver should reset the radio when number of consecutive missed
311 * beacon, or any other uCode error condition detected.
312 */
313 IWL_DEBUG_INFO(priv, "perform radio reset.\n");
314 iwl_internal_short_hw_scan(priv);
315 return 0;
316}
317
318
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700319static void iwlagn_recover_from_statistics(struct iwl_priv *priv,
320 struct statistics_rx_phy *cur_ofdm,
321 struct statistics_rx_ht_phy *cur_ofdm_ht,
322 struct statistics_tx *tx,
323 unsigned long stamp)
Wey-Yi Guyfa8f130c2010-03-05 14:22:46 -0800324{
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100325 unsigned int msecs;
Stanislaw Gruszkab7977ff2011-02-28 14:33:15 +0100326
Don Fry83626402012-03-07 09:52:37 -0800327 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100328 return;
329
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100330 msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
331
332 /* Only gather statistics and update time stamp when not associated */
333 if (!iwl_is_any_associated(priv))
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700334 return;
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100335
336 /* Do not check/recover when do not have enough statistics data */
337 if (msecs < 99)
Wey-Yi Guyfa8f130c2010-03-05 14:22:46 -0800338 return;
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100339
Don Fry9d143e92011-04-20 15:23:57 -0700340 if (iwlagn_mod_params.plcp_check &&
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700341 !iwlagn_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
Johannes Berg48dffd32012-04-09 17:46:57 -0700342 iwl_force_rf_reset(priv, false);
Wey-Yi Guybeac5492010-03-04 13:38:58 -0800343}
Wey-Yi Guybeac5492010-03-04 13:38:58 -0800344
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100345/* Calculate noise level, based on measurements during network silence just
346 * before arriving beacon. This measurement can be done only if we know
347 * exactly when to expect beacons, therefore only when we're associated. */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700348static void iwlagn_rx_calc_noise(struct iwl_priv *priv)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100349{
350 struct statistics_rx_non_phy *rx_info;
351 int num_active_rx = 0;
352 int total_silence = 0;
353 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
354 int last_rx_noise;
355
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700356 rx_info = &priv->statistics.rx_non_phy;
357
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100358 bcn_silence_a =
359 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
360 bcn_silence_b =
361 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
362 bcn_silence_c =
363 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
364
365 if (bcn_silence_a) {
366 total_silence += bcn_silence_a;
367 num_active_rx++;
368 }
369 if (bcn_silence_b) {
370 total_silence += bcn_silence_b;
371 num_active_rx++;
372 }
373 if (bcn_silence_c) {
374 total_silence += bcn_silence_c;
375 num_active_rx++;
376 }
377
378 /* Average among active antennas */
379 if (num_active_rx)
380 last_rx_noise = (total_silence / num_active_rx) - 107;
381 else
382 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
383
384 IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
385 bcn_silence_a, bcn_silence_b, bcn_silence_c,
386 last_rx_noise);
387}
388
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700389#ifdef CONFIG_IWLWIFI_DEBUGFS
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100390/*
391 * based on the assumption of all statistics counter are in DWORD
392 * FIXME: This function is for debugging, do not deal with
393 * the case of counters roll-over.
394 */
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700395static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
396 __le32 *max_delta, __le32 *accum, int size)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100397{
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700398 int i;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100399
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700400 for (i = 0;
401 i < size / sizeof(__le32);
402 i++, prev++, cur++, delta++, max_delta++, accum++) {
403 if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
404 *delta = cpu_to_le32(
405 le32_to_cpu(*cur) - le32_to_cpu(*prev));
406 le32_add_cpu(accum, le32_to_cpu(*delta));
407 if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100408 *max_delta = *delta;
409 }
410 }
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100411}
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100412
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700413static void
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700414iwlagn_accumulative_statistics(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700415 struct statistics_general_common *common,
416 struct statistics_rx_non_phy *rx_non_phy,
417 struct statistics_rx_phy *rx_ofdm,
418 struct statistics_rx_ht_phy *rx_ofdm_ht,
419 struct statistics_rx_phy *rx_cck,
420 struct statistics_tx *tx,
421 struct statistics_bt_activity *bt_activity)
422{
423#define ACCUM(_name) \
424 accum_stats((__le32 *)&priv->statistics._name, \
425 (__le32 *)_name, \
426 (__le32 *)&priv->delta_stats._name, \
427 (__le32 *)&priv->max_delta_stats._name, \
428 (__le32 *)&priv->accum_stats._name, \
429 sizeof(*_name));
430
431 ACCUM(common);
432 ACCUM(rx_non_phy);
433 ACCUM(rx_ofdm);
434 ACCUM(rx_ofdm_ht);
435 ACCUM(rx_cck);
436 ACCUM(tx);
437 if (bt_activity)
438 ACCUM(bt_activity);
439#undef ACCUM
440}
441#else
442static inline void
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700443iwlagn_accumulative_statistics(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700444 struct statistics_general_common *common,
445 struct statistics_rx_non_phy *rx_non_phy,
446 struct statistics_rx_phy *rx_ofdm,
447 struct statistics_rx_ht_phy *rx_ofdm_ht,
448 struct statistics_rx_phy *rx_cck,
449 struct statistics_tx *tx,
450 struct statistics_bt_activity *bt_activity)
451{
452}
453#endif
454
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700455static int iwlagn_rx_statistics(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800456 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700457 struct iwl_device_cmd *cmd)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100458{
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700459 unsigned long stamp = jiffies;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100460 const int reg_recalib_period = 60;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100461 int change;
462 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700463 u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
464 __le32 *flag;
465 struct statistics_general_common *common;
466 struct statistics_rx_non_phy *rx_non_phy;
467 struct statistics_rx_phy *rx_ofdm;
468 struct statistics_rx_ht_phy *rx_ofdm_ht;
469 struct statistics_rx_phy *rx_cck;
470 struct statistics_tx *tx;
471 struct statistics_bt_activity *bt_activity;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100472
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700473 len -= sizeof(struct iwl_cmd_header); /* skip header */
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100474
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700475 IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
476 len);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100477
Johannes Berg4ff70fc2012-03-05 11:24:26 -0800478 spin_lock(&priv->statistics.lock);
479
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700480 if (len == sizeof(struct iwl_bt_notif_statistics)) {
481 struct iwl_bt_notif_statistics *stats;
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800482 stats = (void *)&pkt->data;
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700483 flag = &stats->flag;
484 common = &stats->general.common;
485 rx_non_phy = &stats->rx.general.common;
486 rx_ofdm = &stats->rx.ofdm;
487 rx_ofdm_ht = &stats->rx.ofdm_ht;
488 rx_cck = &stats->rx.cck;
489 tx = &stats->tx;
490 bt_activity = &stats->general.activity;
491
492#ifdef CONFIG_IWLWIFI_DEBUGFS
493 /* handle this exception directly */
494 priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
495 le32_add_cpu(&priv->statistics.accum_num_bt_kills,
496 le32_to_cpu(stats->rx.general.num_bt_kills));
497#endif
498 } else if (len == sizeof(struct iwl_notif_statistics)) {
499 struct iwl_notif_statistics *stats;
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800500 stats = (void *)&pkt->data;
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700501 flag = &stats->flag;
502 common = &stats->general.common;
503 rx_non_phy = &stats->rx.general;
504 rx_ofdm = &stats->rx.ofdm;
505 rx_ofdm_ht = &stats->rx.ofdm_ht;
506 rx_cck = &stats->rx.cck;
507 tx = &stats->tx;
508 bt_activity = NULL;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100509 } else {
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700510 WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
511 len, sizeof(struct iwl_bt_notif_statistics),
512 sizeof(struct iwl_notif_statistics));
Johannes Berg4ff70fc2012-03-05 11:24:26 -0800513 spin_unlock(&priv->statistics.lock);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700514 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100515 }
516
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700517 change = common->temperature != priv->statistics.common.temperature ||
518 (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
519 (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
520
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700521 iwlagn_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700522 rx_ofdm_ht, rx_cck, tx, bt_activity);
523
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700524 iwlagn_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700525
526 priv->statistics.flag = *flag;
527 memcpy(&priv->statistics.common, common, sizeof(*common));
528 memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
529 memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
530 memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
531 memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
532 memcpy(&priv->statistics.tx, tx, sizeof(*tx));
533#ifdef CONFIG_IWLWIFI_DEBUGFS
534 if (bt_activity)
535 memcpy(&priv->statistics.bt_activity, bt_activity,
536 sizeof(*bt_activity));
537#endif
538
539 priv->rx_statistics_jiffies = stamp;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100540
Don Fry83626402012-03-07 09:52:37 -0800541 set_bit(STATUS_STATISTICS, &priv->status);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100542
543 /* Reschedule the statistics timer to occur in
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100544 * reg_recalib_period seconds to ensure we get a
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100545 * thermal update even if the uCode doesn't give
546 * us one */
547 mod_timer(&priv->statistics_periodic, jiffies +
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100548 msecs_to_jiffies(reg_recalib_period * 1000));
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100549
Don Fry83626402012-03-07 09:52:37 -0800550 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100551 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700552 iwlagn_rx_calc_noise(priv);
Johannes Berg1ee158d2012-02-17 10:07:44 -0800553 queue_work(priv->workqueue, &priv->run_time_calib_work);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100554 }
Johannes Berge9676692012-04-10 14:10:28 -0700555 if (priv->lib->temperature && change)
556 priv->lib->temperature(priv);
Johannes Berg4ff70fc2012-03-05 11:24:26 -0800557
558 spin_unlock(&priv->statistics.lock);
559
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700560 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100561}
562
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700563static int iwlagn_rx_reply_statistics(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800564 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700565 struct iwl_device_cmd *cmd)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100566{
567 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800568 struct iwl_notif_statistics *stats = (void *)pkt->data;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100569
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800570 if (le32_to_cpu(stats->flag) & UCODE_STATISTICS_CLEAR_MSK) {
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100571#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700572 memset(&priv->accum_stats, 0,
573 sizeof(priv->accum_stats));
574 memset(&priv->delta_stats, 0,
575 sizeof(priv->delta_stats));
576 memset(&priv->max_delta_stats, 0,
577 sizeof(priv->max_delta_stats));
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100578#endif
579 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
580 }
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700581 iwlagn_rx_statistics(priv, rxb, cmd);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700582 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100583}
584
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100585/* Handle notification from uCode that card's power state is changing
586 * due to software, hardware, or critical temperature RFKILL */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700587static int iwlagn_rx_card_state_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800588 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700589 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100590{
591 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800592 struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
593 u32 flags = le32_to_cpu(card_state_notif->flags);
Don Fry1353a7b2012-03-07 09:52:43 -0800594 unsigned long status = priv->status;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100595
596 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
597 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
598 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
599 (flags & CT_CARD_DISABLED) ?
600 "Reached" : "Not reached");
601
602 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
603 CT_CARD_DISABLED)) {
604
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700605 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100606 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
607
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700608 iwl_write_direct32(priv->trans, HBUS_TARG_MBX_C,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100609 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
610
611 if (!(flags & RXON_CARD_DISABLED)) {
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700612 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100613 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700614 iwl_write_direct32(priv->trans, HBUS_TARG_MBX_C,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100615 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
616 }
617 if (flags & CT_CARD_DISABLED)
618 iwl_tt_enter_ct_kill(priv);
619 }
620 if (!(flags & CT_CARD_DISABLED))
621 iwl_tt_exit_ct_kill(priv);
622
623 if (flags & HW_CARD_DISABLED)
Don Fry83626402012-03-07 09:52:37 -0800624 set_bit(STATUS_RF_KILL_HW, &priv->status);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100625 else
Don Fry83626402012-03-07 09:52:37 -0800626 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100627
628
629 if (!(flags & RXON_CARD_DISABLED))
630 iwl_scan_cancel(priv);
631
632 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Don Fry83626402012-03-07 09:52:37 -0800633 test_bit(STATUS_RF_KILL_HW, &priv->status)))
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100634 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
Don Fry83626402012-03-07 09:52:37 -0800635 test_bit(STATUS_RF_KILL_HW, &priv->status));
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100636 else
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700637 wake_up(&priv->trans->wait_command_queue);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700638 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100639}
640
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700641static int iwlagn_rx_missed_beacon_notif(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800642 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700643 struct iwl_device_cmd *cmd)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100644
645{
646 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800647 struct iwl_missed_beacon_notif *missed_beacon = (void *)pkt->data;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100648
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100649 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
650 priv->missed_beacon_threshold) {
651 IWL_DEBUG_CALIB(priv,
652 "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
653 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
654 le32_to_cpu(missed_beacon->total_missed_becons),
655 le32_to_cpu(missed_beacon->num_recvd_beacons),
656 le32_to_cpu(missed_beacon->num_expected_beacons));
Don Fry83626402012-03-07 09:52:37 -0800657 if (!test_bit(STATUS_SCANNING, &priv->status))
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100658 iwl_init_sensitivity(priv);
659 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700660 return 0;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100661}
662
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100663/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
664 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700665static int iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800666 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700667 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100668{
669 struct iwl_rx_packet *pkt = rxb_addr(rxb);
670
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700671 priv->last_phy_res_valid = true;
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800672 memcpy(&priv->last_phy_res, pkt->data,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100673 sizeof(struct iwl_rx_phy_res));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700674 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100675}
676
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800677/*
678 * returns non-zero if packet should be dropped
679 */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700680static int iwlagn_set_decrypted_flag(struct iwl_priv *priv,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100681 struct ieee80211_hdr *hdr,
682 u32 decrypt_res,
683 struct ieee80211_rx_status *stats)
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800684{
685 u16 fc = le16_to_cpu(hdr->frame_control);
686
Johannes Berg246ed352010-08-23 10:46:32 +0200687 /*
688 * All contexts have the same setting here due to it being
689 * a module parameter, so OK to check any context.
690 */
691 if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
692 RXON_FILTER_DIS_DECRYPT_MSK)
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800693 return 0;
694
695 if (!(fc & IEEE80211_FCTL_PROTECTED))
696 return 0;
697
Tomas Winklere1623442009-01-27 14:27:56 -0800698 IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800699 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
700 case RX_RES_STATUS_SEC_TYPE_TKIP:
701 /* The uCode has got a bad phase 1 Key, pushes the packet.
702 * Decryption will be done in SW. */
703 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
704 RX_RES_STATUS_BAD_KEY_TTAK)
705 break;
706
707 case RX_RES_STATUS_SEC_TYPE_WEP:
708 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
709 RX_RES_STATUS_BAD_ICV_MIC) {
710 /* bad ICV, the packet is destroyed since the
711 * decryption is inplace, drop it */
Tomas Winklere1623442009-01-27 14:27:56 -0800712 IWL_DEBUG_RX(priv, "Packet destroyed\n");
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800713 return -1;
714 }
715 case RX_RES_STATUS_SEC_TYPE_CCMP:
716 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
717 RX_RES_STATUS_DECRYPT_OK) {
Tomas Winklere1623442009-01-27 14:27:56 -0800718 IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800719 stats->flag |= RX_FLAG_DECRYPTED;
720 }
721 break;
722
723 default:
724 break;
725 }
726 return 0;
727}
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100728
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700729static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100730 struct ieee80211_hdr *hdr,
731 u16 len,
732 u32 ampdu_status,
Johannes Berg48a2d662012-03-05 11:24:39 -0800733 struct iwl_rx_cmd_buffer *rxb,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100734 struct ieee80211_rx_status *stats)
735{
736 struct sk_buff *skb;
737 __le16 fc = hdr->frame_control;
Garen Tamrazian68b99312011-03-30 02:29:32 -0700738 struct iwl_rxon_context *ctx;
Johannes Berg48a2d662012-03-05 11:24:39 -0800739 struct page *p;
740 int offset;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100741
742 /* We only process data packets if the interface is open */
743 if (unlikely(!priv->is_open)) {
744 IWL_DEBUG_DROP_LIMIT(priv,
745 "Dropping packet while interface is not open.\n");
746 return;
747 }
748
749 /* In case of HW accelerated crypto and bad decryption, drop */
Don Fry9d143e92011-04-20 15:23:57 -0700750 if (!iwlagn_mod_params.sw_crypto &&
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700751 iwlagn_set_decrypted_flag(priv, hdr, ampdu_status, stats))
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100752 return;
753
754 skb = dev_alloc_skb(128);
755 if (!skb) {
756 IWL_ERR(priv, "dev_alloc_skb failed\n");
757 return;
758 }
759
Johannes Berg0c197442012-03-15 13:26:43 -0700760 offset = (void *)hdr - rxb_addr(rxb) + rxb_offset(rxb);
Johannes Berg48a2d662012-03-05 11:24:39 -0800761 p = rxb_steal_page(rxb);
Eric Dumazet50269e12012-03-23 23:59:33 +0000762 skb_add_rx_frag(skb, 0, p, offset, len, len);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100763
Garen Tamrazian68b99312011-03-30 02:29:32 -0700764 /*
765 * Wake any queues that were stopped due to a passive channel tx
766 * failure. This can happen because the regulatory enforcement in
767 * the device waits for a beacon before allowing transmission,
768 * sometimes even after already having transmitted frames for the
769 * association because the new RXON may reset the information.
770 */
Johannes Berge755f882012-03-07 09:52:16 -0800771 if (unlikely(ieee80211_is_beacon(fc) && priv->passive_no_rx)) {
Garen Tamrazian68b99312011-03-30 02:29:32 -0700772 for_each_context(priv, ctx) {
Garen Tamrazian68b99312011-03-30 02:29:32 -0700773 if (compare_ether_addr(hdr->addr3,
774 ctx->active.bssid_addr))
775 continue;
Johannes Berge755f882012-03-07 09:52:16 -0800776 iwlagn_lift_passive_no_rx(priv);
Garen Tamrazian68b99312011-03-30 02:29:32 -0700777 }
778 }
779
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100780 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
781
782 ieee80211_rx(priv->hw, skb);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100783}
784
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700785static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100786{
787 u32 decrypt_out = 0;
788
789 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
790 RX_RES_STATUS_STATION_FOUND)
791 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
792 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
793
794 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
795
796 /* packet was not encrypted */
797 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
798 RX_RES_STATUS_SEC_TYPE_NONE)
799 return decrypt_out;
800
801 /* packet was encrypted with unknown alg */
802 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
803 RX_RES_STATUS_SEC_TYPE_ERR)
804 return decrypt_out;
805
806 /* decryption was not done in HW */
807 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
808 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
809 return decrypt_out;
810
811 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
812
813 case RX_RES_STATUS_SEC_TYPE_CCMP:
814 /* alg is CCM: check MIC only */
815 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
816 /* Bad MIC */
817 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
818 else
819 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
820
821 break;
822
823 case RX_RES_STATUS_SEC_TYPE_TKIP:
824 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
825 /* Bad TTAK */
826 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
827 break;
828 }
829 /* fall through if TTAK OK */
830 default:
831 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
832 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
833 else
834 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
835 break;
836 }
837
838 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
839 decrypt_in, decrypt_out);
840
841 return decrypt_out;
842}
843
Don Fry5c3d29f2011-07-08 08:46:29 -0700844/* Calc max signal level (dBm) among 3 possible receivers */
845static int iwlagn_calc_rssi(struct iwl_priv *priv,
846 struct iwl_rx_phy_res *rx_resp)
847{
848 /* data from PHY/DSP regarding signal strength, etc.,
849 * contents are always there, not configurable by host
850 */
851 struct iwlagn_non_cfg_phy *ncphy =
852 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
853 u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
854 u8 agc;
855
856 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
857 agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
858
859 /* Find max rssi among 3 possible receivers.
860 * These values are measured by the digital signal processor (DSP).
861 * They should stay fairly constant even as the signal strength varies,
862 * if the radio's automatic gain control (AGC) is working right.
863 * AGC value (see below) will provide the "interesting" info.
864 */
865 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
866 rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
867 IWLAGN_OFDM_RSSI_A_BIT_POS;
868 rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
869 IWLAGN_OFDM_RSSI_B_BIT_POS;
870 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
871 rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
872 IWLAGN_OFDM_RSSI_C_BIT_POS;
873
874 max_rssi = max_t(u32, rssi_a, rssi_b);
875 max_rssi = max_t(u32, max_rssi, rssi_c);
876
877 IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
878 rssi_a, rssi_b, rssi_c, max_rssi, agc);
879
880 /* dBm = max_rssi dB - agc dB - constant.
881 * Higher AGC (higher radio gain) means lower signal. */
882 return max_rssi - agc - IWLAGN_RSSI_OFFSET;
883}
884
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100885/* Called for REPLY_RX (legacy ABG frames), or
886 * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700887static int iwlagn_rx_reply_rx(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -0800888 struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700889 struct iwl_device_cmd *cmd)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100890{
891 struct ieee80211_hdr *header;
892 struct ieee80211_rx_status rx_status;
893 struct iwl_rx_packet *pkt = rxb_addr(rxb);
894 struct iwl_rx_phy_res *phy_res;
895 __le32 rx_pkt_status;
896 struct iwl_rx_mpdu_res_start *amsdu;
897 u32 len;
898 u32 ampdu_status;
899 u32 rate_n_flags;
900
901 /**
902 * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
903 * REPLY_RX: physical layer info is in this buffer
904 * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
905 * command and cached in priv->last_phy_res
906 *
907 * Here we set up local variables depending on which command is
908 * received.
909 */
910 if (pkt->hdr.cmd == REPLY_RX) {
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800911 phy_res = (struct iwl_rx_phy_res *)pkt->data;
912 header = (struct ieee80211_hdr *)(pkt->data + sizeof(*phy_res)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100913 + phy_res->cfg_phy_cnt);
914
915 len = le16_to_cpu(phy_res->byte_count);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800916 rx_pkt_status = *(__le32 *)(pkt->data + sizeof(*phy_res) +
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100917 phy_res->cfg_phy_cnt + len);
918 ampdu_status = le32_to_cpu(rx_pkt_status);
919 } else {
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700920 if (!priv->last_phy_res_valid) {
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100921 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700922 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100923 }
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700924 phy_res = &priv->last_phy_res;
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800925 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->data;
926 header = (struct ieee80211_hdr *)(pkt->data + sizeof(*amsdu));
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100927 len = le16_to_cpu(amsdu->byte_count);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800928 rx_pkt_status = *(__le32 *)(pkt->data + sizeof(*amsdu) + len);
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700929 ampdu_status = iwlagn_translate_rx_status(priv,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100930 le32_to_cpu(rx_pkt_status));
931 }
932
933 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
Johannes Berg0ca24da2012-03-15 13:26:46 -0700934 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d\n",
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100935 phy_res->cfg_phy_cnt);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700936 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100937 }
938
939 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
940 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
941 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
942 le32_to_cpu(rx_pkt_status));
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700943 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100944 }
945
946 /* This will be used in several places later */
947 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
948
949 /* rx_status carries information about the packet to mac80211 */
950 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
951 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
952 IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
953 rx_status.freq =
954 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
955 rx_status.band);
956 rx_status.rate_idx =
957 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
958 rx_status.flag = 0;
959
960 /* TSF isn't reliable. In order to allow smooth user experience,
961 * this W/A doesn't propagate it to the mac80211 */
962 /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
963
964 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
965
966 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
Don Fry5c3d29f2011-07-08 08:46:29 -0700967 rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100968
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100969 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
970 rx_status.signal, (unsigned long long)rx_status.mactime);
971
972 /*
973 * "antenna number"
974 *
975 * It seems that the antenna field in the phy flags value
976 * is actually a bit field. This is undefined by radiotap,
977 * it wants an actual antenna number but I always get "7"
978 * for most legacy frames I receive indicating that the
979 * same frame was received on all three RX chains.
980 *
981 * I think this field should be removed in favor of a
982 * new 802.11n radiotap field "RX chains" that is defined
983 * as a bitmask.
984 */
985 rx_status.antenna =
986 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
987 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
988
989 /* set the preamble flag if appropriate */
990 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
991 rx_status.flag |= RX_FLAG_SHORTPRE;
992
993 /* Set up the HT phy flags */
994 if (rate_n_flags & RATE_MCS_HT_MSK)
995 rx_status.flag |= RX_FLAG_HT;
996 if (rate_n_flags & RATE_MCS_HT40_MSK)
997 rx_status.flag |= RX_FLAG_40MHZ;
998 if (rate_n_flags & RATE_MCS_SGI_MSK)
999 rx_status.flag |= RX_FLAG_SHORT_GI;
1000
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -07001001 iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001002 rxb, &rx_status);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001003 return 0;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001004}
1005
Johannes Berg79d3eef2011-11-10 06:55:01 -08001006static int iwlagn_rx_noa_notification(struct iwl_priv *priv,
Johannes Berg48a2d662012-03-05 11:24:39 -08001007 struct iwl_rx_cmd_buffer *rxb,
Johannes Berg79d3eef2011-11-10 06:55:01 -08001008 struct iwl_device_cmd *cmd)
1009{
1010 struct iwl_wipan_noa_data *new_data, *old_data;
1011 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -08001012 struct iwl_wipan_noa_notification *noa_notif = (void *)pkt->data;
Johannes Berg79d3eef2011-11-10 06:55:01 -08001013
1014 /* no condition -- we're in softirq */
1015 old_data = rcu_dereference_protected(priv->noa_data, true);
1016
1017 if (noa_notif->noa_active) {
1018 u32 len = le16_to_cpu(noa_notif->noa_attribute.length);
1019 u32 copylen = len;
1020
1021 /* EID, len, OUI, subtype */
1022 len += 1 + 1 + 3 + 1;
1023 /* P2P id, P2P length */
1024 len += 1 + 2;
1025 copylen += 1 + 2;
1026
1027 new_data = kmalloc(sizeof(*new_data) + len, GFP_ATOMIC);
1028 if (new_data) {
1029 new_data->length = len;
1030 new_data->data[0] = WLAN_EID_VENDOR_SPECIFIC;
1031 new_data->data[1] = len - 2; /* not counting EID, len */
1032 new_data->data[2] = (WLAN_OUI_WFA >> 16) & 0xff;
1033 new_data->data[3] = (WLAN_OUI_WFA >> 8) & 0xff;
1034 new_data->data[4] = (WLAN_OUI_WFA >> 0) & 0xff;
1035 new_data->data[5] = WLAN_OUI_TYPE_WFA_P2P;
1036 memcpy(&new_data->data[6], &noa_notif->noa_attribute,
1037 copylen);
1038 }
1039 } else
1040 new_data = NULL;
1041
1042 rcu_assign_pointer(priv->noa_data, new_data);
1043
1044 if (old_data)
1045 kfree_rcu(old_data, rcu_head);
1046
1047 return 0;
1048}
1049
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001050/**
1051 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
1052 *
1053 * Setup the RX handlers for each of the reply types sent from the uCode
1054 * to the host.
1055 */
1056void iwl_setup_rx_handlers(struct iwl_priv *priv)
1057{
Johannes Berg48a2d662012-03-05 11:24:39 -08001058 int (**handlers)(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001059 struct iwl_device_cmd *cmd);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001060
1061 handlers = priv->rx_handlers;
1062
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -07001063 handlers[REPLY_ERROR] = iwlagn_rx_reply_error;
1064 handlers[CHANNEL_SWITCH_NOTIFICATION] = iwlagn_rx_csa;
1065 handlers[SPECTRUM_MEASURE_NOTIFICATION] =
1066 iwlagn_rx_spectrum_measure_notif;
1067 handlers[PM_SLEEP_NOTIFICATION] = iwlagn_rx_pm_sleep_notif;
1068 handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
1069 iwlagn_rx_pm_debug_statistics_notif;
1070 handlers[BEACON_NOTIFICATION] = iwlagn_rx_beacon_notif;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001071 handlers[REPLY_ADD_STA] = iwl_add_sta_callback;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001072
Johannes Berg79d3eef2011-11-10 06:55:01 -08001073 handlers[REPLY_WIPAN_NOA_NOTIFICATION] = iwlagn_rx_noa_notification;
1074
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001075 /*
1076 * The same handler is used for both the REPLY to a discrete
1077 * statistics request from the host as well as for the periodic
1078 * statistics notifications (after received beacons) from the uCode.
1079 */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -07001080 handlers[REPLY_STATISTICS_CMD] = iwlagn_rx_reply_statistics;
1081 handlers[STATISTICS_NOTIFICATION] = iwlagn_rx_statistics;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001082
1083 iwl_setup_rx_scan_handlers(priv);
1084
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -07001085 handlers[CARD_STATE_NOTIFICATION] = iwlagn_rx_card_state_notif;
1086 handlers[MISSED_BEACONS_NOTIFICATION] =
1087 iwlagn_rx_missed_beacon_notif;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001088
1089 /* Rx handlers */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -07001090 handlers[REPLY_RX_PHY_CMD] = iwlagn_rx_reply_rx_phy;
1091 handlers[REPLY_RX_MPDU_CMD] = iwlagn_rx_reply_rx;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001092
1093 /* block ack */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -07001094 handlers[REPLY_COMPRESSED_BA] =
1095 iwlagn_rx_reply_compressed_ba;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001096
Emmanuel Grumbach8d301192011-07-03 15:45:34 +03001097 priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
1098
1099 /* set up notification wait support */
Johannes Berg4bd14dd2012-03-06 13:30:58 -08001100 iwl_notification_wait_init(&priv->notif_wait);
Emmanuel Grumbach8d301192011-07-03 15:45:34 +03001101
1102 /* Set up BT Rx handlers */
Emmanuel Grumbach21522682012-03-22 17:51:44 +02001103 if (priv->cfg->bt_params)
Johannes Berg562f08e2012-03-07 09:52:18 -08001104 iwlagn_bt_rx_handler_setup(priv);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +01001105}
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001106
Johannes Berg48a2d662012-03-05 11:24:39 -08001107int iwl_rx_dispatch(struct iwl_op_mode *op_mode, struct iwl_rx_cmd_buffer *rxb,
1108 struct iwl_device_cmd *cmd)
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001109{
1110 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbachdb70f292012-02-09 16:08:15 +02001111 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
Amit Beka0aef8dd2012-03-07 09:52:29 -08001112 void (*pre_rx_handler)(struct iwl_priv *,
1113 struct iwl_rx_cmd_buffer *);
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001114 int err = 0;
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001115
1116 /*
1117 * Do the notification wait before RX handlers so
1118 * even if the RX handler consumes the RXB we have
1119 * access to it in the notification wait entry.
1120 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -08001121 iwl_notification_wait_notify(&priv->notif_wait, pkt);
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001122
Amit Beka0aef8dd2012-03-07 09:52:29 -08001123 /* RX data may be forwarded to userspace (using pre_rx_handler) in one
1124 * of two cases: the first, that the user owns the uCode through
1125 * testmode - in such case the pre_rx_handler is set and no further
1126 * processing takes place. The other case is when the user want to
1127 * monitor the rx w/o affecting the regular flow - the pre_rx_handler
1128 * will be set but the ownership flag != IWL_OWNERSHIP_TM and the flow
1129 * continues.
1130 * We need to use ACCESS_ONCE to prevent a case where the handler
1131 * changes between the check and the call.
1132 */
1133 pre_rx_handler = ACCESS_ONCE(priv->pre_rx_handler);
1134 if (pre_rx_handler)
1135 pre_rx_handler(priv, rxb);
1136 if (priv->ucode_owner != IWL_OWNERSHIP_TM) {
Wey-Yi Guy64eeacb2011-12-16 09:31:14 -08001137 /* Based on type of command response or notification,
1138 * handle those that need handling via function in
1139 * rx_handlers table. See iwl_setup_rx_handlers() */
1140 if (priv->rx_handlers[pkt->hdr.cmd]) {
1141 priv->rx_handlers_stats[pkt->hdr.cmd]++;
1142 err = priv->rx_handlers[pkt->hdr.cmd] (priv, rxb, cmd);
1143 } else {
1144 /* No handling needed */
Johannes Bergd9fb6462012-03-26 08:23:39 -07001145 IWL_DEBUG_RX(priv, "No handler needed for %s, 0x%02x\n",
1146 iwl_dvm_get_cmd_string(pkt->hdr.cmd),
1147 pkt->hdr.cmd);
Wey-Yi Guy64eeacb2011-12-16 09:31:14 -08001148 }
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001149 }
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -07001150 return err;
Emmanuel Grumbach1ab9f6c2011-07-07 07:59:02 -07001151}