blob: c942830af2b58ed8517c529ba4deb70c14e923f6 [file] [log] [blame]
Tomas Winklera55360e2008-05-05 10:22:28 +08001/******************************************************************************
2 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02003 * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
Sharon Dvir39bdb172015-10-15 18:18:09 +03004 * Copyright(c) 2015 Intel Deutschland GmbH
Tomas Winklera55360e2008-05-05 10:22:28 +08005 *
6 * Portions of this file are derived from the ipw3945 project, as well
Emmanuel Grumbach6a686c62011-10-10 07:27:17 -07007 * as portionhelp of the ieee80211 subsystem header files.
Tomas Winklera55360e2008-05-05 10:22:28 +08008 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * more details.
17 *
18 * You should have received a copy of the GNU General Public License along with
19 * this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
21 *
22 * The full GNU General Public License is included in this distribution in the
23 * file called LICENSE.
24 *
25 * Contact Information:
Emmanuel Grumbachcb2f8272015-11-17 15:39:56 +020026 * Intel Linux Wireless <linuxwifi@intel.com>
Tomas Winklera55360e2008-05-05 10:22:28 +080027 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
28 *
29 *****************************************************************************/
30
Emmanuel Grumbach1781a072008-06-30 17:23:09 +080031#include <linux/etherdevice.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Stanislaw Gruszka118253c2011-03-07 09:22:24 +010033#include <linux/sched.h>
Tomas Winklera55360e2008-05-05 10:22:28 +080034#include <net/mac80211.h>
Tomas Winklera05ffd32008-07-10 14:28:42 +030035#include <asm/unaligned.h>
Sharon Dvir39bdb172015-10-15 18:18:09 +030036
37#include "iwl-trans.h"
Tomas Winklera55360e2008-05-05 10:22:28 +080038#include "iwl-io.h"
Johannes Berg1023fdc2012-05-15 12:16:34 +020039#include "dev.h"
40#include "calib.h"
41#include "agn.h"
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010042
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010043/******************************************************************************
44 *
45 * Generic RX handler implementations
46 *
47 ******************************************************************************/
48
Johannes Berg73e686f2015-06-23 21:22:09 +020049static void iwlagn_rx_reply_error(struct iwl_priv *priv,
50 struct iwl_rx_cmd_buffer *rxb)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010051{
52 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080053 struct iwl_error_resp *err_resp = (void *)pkt->data;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010054
Johannes Bergd9fb6462012-03-26 08:23:39 -070055 IWL_ERR(priv, "Error Reply type 0x%08X cmd REPLY_ERROR (0x%02X) "
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010056 "seq 0x%04X ser 0x%08X\n",
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080057 le32_to_cpu(err_resp->error_type),
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080058 err_resp->cmd_id,
59 le16_to_cpu(err_resp->bad_cmd_seq_num),
60 le32_to_cpu(err_resp->error_info));
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010061}
62
Johannes Berg73e686f2015-06-23 21:22:09 +020063static void iwlagn_rx_csa(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010064{
65 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080066 struct iwl_csa_notification *csa = (void *)pkt->data;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010067 /*
68 * MULTI-FIXME
Wey-Yi Guyade4c642011-10-10 07:27:11 -070069 * See iwlagn_mac_channel_switch.
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010070 */
71 struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
72 struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
73
Don Fry83626402012-03-07 09:52:37 -080074 if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
Johannes Berg73e686f2015-06-23 21:22:09 +020075 return;
Stanislaw Gruszka6f213ff2011-06-02 18:17:15 +020076
77 if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
78 rxon->channel = csa->channel;
79 ctx->staging.channel = csa->channel;
80 IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010081 le16_to_cpu(csa->channel));
Stanislaw Gruszka6f213ff2011-06-02 18:17:15 +020082 iwl_chswitch_done(priv, true);
83 } else {
84 IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
85 le16_to_cpu(csa->channel));
86 iwl_chswitch_done(priv, false);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +010087 }
88}
89
90
Johannes Berg73e686f2015-06-23 21:22:09 +020091static void iwlagn_rx_spectrum_measure_notif(struct iwl_priv *priv,
92 struct iwl_rx_cmd_buffer *rxb)
Reinette Chatre81963d62010-01-22 14:22:57 -080093{
94 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -080095 struct iwl_spectrum_notification *report = (void *)pkt->data;
Reinette Chatre81963d62010-01-22 14:22:57 -080096
97 if (!report->state) {
98 IWL_DEBUG_11H(priv,
99 "Spectrum Measure Notification: Start\n");
Johannes Berg73e686f2015-06-23 21:22:09 +0200100 return;
Reinette Chatre81963d62010-01-22 14:22:57 -0800101 }
102
103 memcpy(&priv->measure_report, report, sizeof(*report));
104 priv->measurement_status |= MEASUREMENT_READY;
105}
Reinette Chatre81963d62010-01-22 14:22:57 -0800106
Johannes Berg73e686f2015-06-23 21:22:09 +0200107static void iwlagn_rx_pm_sleep_notif(struct iwl_priv *priv,
108 struct iwl_rx_cmd_buffer *rxb)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100109{
110#ifdef CONFIG_IWLWIFI_DEBUG
111 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800112 struct iwl_sleep_notification *sleep = (void *)pkt->data;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100113 IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
114 sleep->pm_sleep_mode, sleep->pm_wakeup_src);
115#endif
116}
117
Johannes Berg73e686f2015-06-23 21:22:09 +0200118static void iwlagn_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
119 struct iwl_rx_cmd_buffer *rxb)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100120{
121 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Berg65b30342014-01-08 13:16:33 +0100122 u32 __maybe_unused len = iwl_rx_packet_len(pkt);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100123 IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
Johannes Bergd9fb6462012-03-26 08:23:39 -0700124 "notification for PM_DEBUG_STATISTIC_NOTIFIC:\n", len);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800125 iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->data, len);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100126}
127
Johannes Berg73e686f2015-06-23 21:22:09 +0200128static void iwlagn_rx_beacon_notif(struct iwl_priv *priv,
129 struct iwl_rx_cmd_buffer *rxb)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100130{
131 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800132 struct iwlagn_beacon_notif *beacon = (void *)pkt->data;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100133#ifdef CONFIG_IWLWIFI_DEBUG
134 u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
135 u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
136
137 IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
138 "tsf:0x%.8x%.8x rate:%d\n",
139 status & TX_STATUS_MSK,
140 beacon->beacon_notify_hdr.failure_frame,
141 le32_to_cpu(beacon->ibss_mgr_status),
142 le32_to_cpu(beacon->high_tsf),
143 le32_to_cpu(beacon->low_tsf), rate);
144#endif
145
146 priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100147}
148
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100149/**
150 * iwl_good_plcp_health - checks for plcp error.
151 *
152 * When the plcp error is exceeding the thresholds, reset the radio
153 * to improve the throughput.
154 */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700155static bool iwlagn_good_plcp_health(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700156 struct statistics_rx_phy *cur_ofdm,
157 struct statistics_rx_ht_phy *cur_ofdm_ht,
158 unsigned int msecs)
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100159{
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100160 int delta;
Johannes Bergab5c0f12012-03-06 13:30:53 -0800161 int threshold = priv->plcp_delta_threshold;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100162
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100163 if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100164 IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100165 return true;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100166 }
167
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700168 delta = le32_to_cpu(cur_ofdm->plcp_err) -
169 le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
170 le32_to_cpu(cur_ofdm_ht->plcp_err) -
171 le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100172
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700173 /* Can be negative if firmware reset statistics */
Stanislaw Gruszka6198c382011-03-04 17:51:50 +0100174 if (delta <= 0)
175 return true;
176
177 if ((delta * 100 / msecs) > threshold) {
178 IWL_DEBUG_RADIO(priv,
179 "plcp health threshold %u delta %d msecs %u\n",
180 threshold, delta, msecs);
181 return false;
182 }
183
184 return true;
Stanislaw Gruszkaad6e82a2011-02-28 14:33:16 +0100185}
186
Meenakshi Venkataraman66cd9e32012-03-14 15:55:13 -0700187int iwl_force_rf_reset(struct iwl_priv *priv, bool external)
188{
189 struct iwl_rf_reset *rf_reset;
190
191 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
192 return -EAGAIN;
193
194 if (!iwl_is_any_associated(priv)) {
195 IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
196 return -ENOLINK;
197 }
198
199 rf_reset = &priv->rf_reset;
200 rf_reset->reset_request_count++;
201 if (!external && rf_reset->last_reset_jiffies &&
202 time_after(rf_reset->last_reset_jiffies +
203 IWL_DELAY_NEXT_FORCE_RF_RESET, jiffies)) {
204 IWL_DEBUG_INFO(priv, "RF reset rejected\n");
205 rf_reset->reset_reject_count++;
206 return -EAGAIN;
207 }
208 rf_reset->reset_success_count++;
209 rf_reset->last_reset_jiffies = jiffies;
210
211 /*
212 * There is no easy and better way to force reset the radio,
213 * the only known method is switching channel which will force to
214 * reset and tune the radio.
215 * Use internal short scan (single channel) operation to should
216 * achieve this objective.
217 * Driver should reset the radio when number of consecutive missed
218 * beacon, or any other uCode error condition detected.
219 */
220 IWL_DEBUG_INFO(priv, "perform radio reset.\n");
221 iwl_internal_short_hw_scan(priv);
222 return 0;
223}
224
225
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700226static void iwlagn_recover_from_statistics(struct iwl_priv *priv,
227 struct statistics_rx_phy *cur_ofdm,
228 struct statistics_rx_ht_phy *cur_ofdm_ht,
229 struct statistics_tx *tx,
230 unsigned long stamp)
Wey-Yi Guyfa8f130c2010-03-05 14:22:46 -0800231{
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100232 unsigned int msecs;
Stanislaw Gruszkab7977ff2011-02-28 14:33:15 +0100233
Don Fry83626402012-03-07 09:52:37 -0800234 if (test_bit(STATUS_EXIT_PENDING, &priv->status))
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100235 return;
236
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100237 msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
238
239 /* Only gather statistics and update time stamp when not associated */
240 if (!iwl_is_any_associated(priv))
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700241 return;
Stanislaw Gruszka410f2bb2011-03-04 17:51:51 +0100242
243 /* Do not check/recover when do not have enough statistics data */
244 if (msecs < 99)
Wey-Yi Guyfa8f130c2010-03-05 14:22:46 -0800245 return;
Stanislaw Gruszkaca3d9382011-02-08 09:31:55 +0100246
Emmanuel Grumbach8b5bf332013-06-18 06:31:37 +0300247 if (!iwlagn_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
Johannes Berg48dffd32012-04-09 17:46:57 -0700248 iwl_force_rf_reset(priv, false);
Wey-Yi Guybeac5492010-03-04 13:38:58 -0800249}
Wey-Yi Guybeac5492010-03-04 13:38:58 -0800250
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100251/* Calculate noise level, based on measurements during network silence just
252 * before arriving beacon. This measurement can be done only if we know
253 * exactly when to expect beacons, therefore only when we're associated. */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700254static void iwlagn_rx_calc_noise(struct iwl_priv *priv)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100255{
256 struct statistics_rx_non_phy *rx_info;
257 int num_active_rx = 0;
258 int total_silence = 0;
259 int bcn_silence_a, bcn_silence_b, bcn_silence_c;
260 int last_rx_noise;
261
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700262 rx_info = &priv->statistics.rx_non_phy;
263
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100264 bcn_silence_a =
265 le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
266 bcn_silence_b =
267 le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
268 bcn_silence_c =
269 le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
270
271 if (bcn_silence_a) {
272 total_silence += bcn_silence_a;
273 num_active_rx++;
274 }
275 if (bcn_silence_b) {
276 total_silence += bcn_silence_b;
277 num_active_rx++;
278 }
279 if (bcn_silence_c) {
280 total_silence += bcn_silence_c;
281 num_active_rx++;
282 }
283
284 /* Average among active antennas */
285 if (num_active_rx)
286 last_rx_noise = (total_silence / num_active_rx) - 107;
287 else
288 last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
289
290 IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
291 bcn_silence_a, bcn_silence_b, bcn_silence_c,
292 last_rx_noise);
293}
294
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700295#ifdef CONFIG_IWLWIFI_DEBUGFS
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100296/*
297 * based on the assumption of all statistics counter are in DWORD
298 * FIXME: This function is for debugging, do not deal with
299 * the case of counters roll-over.
300 */
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700301static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
302 __le32 *max_delta, __le32 *accum, int size)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100303{
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700304 int i;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100305
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700306 for (i = 0;
307 i < size / sizeof(__le32);
308 i++, prev++, cur++, delta++, max_delta++, accum++) {
309 if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
310 *delta = cpu_to_le32(
311 le32_to_cpu(*cur) - le32_to_cpu(*prev));
312 le32_add_cpu(accum, le32_to_cpu(*delta));
313 if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100314 *max_delta = *delta;
315 }
316 }
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100317}
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100318
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700319static void
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700320iwlagn_accumulative_statistics(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700321 struct statistics_general_common *common,
322 struct statistics_rx_non_phy *rx_non_phy,
323 struct statistics_rx_phy *rx_ofdm,
324 struct statistics_rx_ht_phy *rx_ofdm_ht,
325 struct statistics_rx_phy *rx_cck,
326 struct statistics_tx *tx,
327 struct statistics_bt_activity *bt_activity)
328{
329#define ACCUM(_name) \
330 accum_stats((__le32 *)&priv->statistics._name, \
331 (__le32 *)_name, \
332 (__le32 *)&priv->delta_stats._name, \
333 (__le32 *)&priv->max_delta_stats._name, \
334 (__le32 *)&priv->accum_stats._name, \
335 sizeof(*_name));
336
337 ACCUM(common);
338 ACCUM(rx_non_phy);
339 ACCUM(rx_ofdm);
340 ACCUM(rx_ofdm_ht);
341 ACCUM(rx_cck);
342 ACCUM(tx);
343 if (bt_activity)
344 ACCUM(bt_activity);
345#undef ACCUM
346}
347#else
348static inline void
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700349iwlagn_accumulative_statistics(struct iwl_priv *priv,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700350 struct statistics_general_common *common,
351 struct statistics_rx_non_phy *rx_non_phy,
352 struct statistics_rx_phy *rx_ofdm,
353 struct statistics_rx_ht_phy *rx_ofdm_ht,
354 struct statistics_rx_phy *rx_cck,
355 struct statistics_tx *tx,
356 struct statistics_bt_activity *bt_activity)
357{
358}
359#endif
360
Johannes Berg73e686f2015-06-23 21:22:09 +0200361static void iwlagn_rx_statistics(struct iwl_priv *priv,
362 struct iwl_rx_cmd_buffer *rxb)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100363{
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700364 unsigned long stamp = jiffies;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100365 const int reg_recalib_period = 60;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100366 int change;
367 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Berg65b30342014-01-08 13:16:33 +0100368 u32 len = iwl_rx_packet_payload_len(pkt);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700369 __le32 *flag;
370 struct statistics_general_common *common;
371 struct statistics_rx_non_phy *rx_non_phy;
372 struct statistics_rx_phy *rx_ofdm;
373 struct statistics_rx_ht_phy *rx_ofdm_ht;
374 struct statistics_rx_phy *rx_cck;
375 struct statistics_tx *tx;
376 struct statistics_bt_activity *bt_activity;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100377
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700378 IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
379 len);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100380
Johannes Berg4ff70fc2012-03-05 11:24:26 -0800381 spin_lock(&priv->statistics.lock);
382
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700383 if (len == sizeof(struct iwl_bt_notif_statistics)) {
384 struct iwl_bt_notif_statistics *stats;
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800385 stats = (void *)&pkt->data;
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700386 flag = &stats->flag;
387 common = &stats->general.common;
388 rx_non_phy = &stats->rx.general.common;
389 rx_ofdm = &stats->rx.ofdm;
390 rx_ofdm_ht = &stats->rx.ofdm_ht;
391 rx_cck = &stats->rx.cck;
392 tx = &stats->tx;
393 bt_activity = &stats->general.activity;
394
395#ifdef CONFIG_IWLWIFI_DEBUGFS
396 /* handle this exception directly */
397 priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
398 le32_add_cpu(&priv->statistics.accum_num_bt_kills,
399 le32_to_cpu(stats->rx.general.num_bt_kills));
400#endif
401 } else if (len == sizeof(struct iwl_notif_statistics)) {
402 struct iwl_notif_statistics *stats;
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800403 stats = (void *)&pkt->data;
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700404 flag = &stats->flag;
405 common = &stats->general.common;
406 rx_non_phy = &stats->rx.general;
407 rx_ofdm = &stats->rx.ofdm;
408 rx_ofdm_ht = &stats->rx.ofdm_ht;
409 rx_cck = &stats->rx.cck;
410 tx = &stats->tx;
411 bt_activity = NULL;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100412 } else {
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700413 WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
414 len, sizeof(struct iwl_bt_notif_statistics),
415 sizeof(struct iwl_notif_statistics));
Johannes Berg4ff70fc2012-03-05 11:24:26 -0800416 spin_unlock(&priv->statistics.lock);
Johannes Berg73e686f2015-06-23 21:22:09 +0200417 return;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100418 }
419
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700420 change = common->temperature != priv->statistics.common.temperature ||
421 (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
422 (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
423
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700424 iwlagn_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700425 rx_ofdm_ht, rx_cck, tx, bt_activity);
426
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700427 iwlagn_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700428
429 priv->statistics.flag = *flag;
430 memcpy(&priv->statistics.common, common, sizeof(*common));
431 memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
432 memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
433 memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
434 memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
435 memcpy(&priv->statistics.tx, tx, sizeof(*tx));
436#ifdef CONFIG_IWLWIFI_DEBUGFS
437 if (bt_activity)
438 memcpy(&priv->statistics.bt_activity, bt_activity,
439 sizeof(*bt_activity));
440#endif
441
442 priv->rx_statistics_jiffies = stamp;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100443
Don Fry83626402012-03-07 09:52:37 -0800444 set_bit(STATUS_STATISTICS, &priv->status);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100445
446 /* Reschedule the statistics timer to occur in
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100447 * reg_recalib_period seconds to ensure we get a
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100448 * thermal update even if the uCode doesn't give
449 * us one */
450 mod_timer(&priv->statistics_periodic, jiffies +
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100451 msecs_to_jiffies(reg_recalib_period * 1000));
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100452
Don Fry83626402012-03-07 09:52:37 -0800453 if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100454 (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700455 iwlagn_rx_calc_noise(priv);
Johannes Berg1ee158d2012-02-17 10:07:44 -0800456 queue_work(priv->workqueue, &priv->run_time_calib_work);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100457 }
Johannes Berge9676692012-04-10 14:10:28 -0700458 if (priv->lib->temperature && change)
459 priv->lib->temperature(priv);
Johannes Berg4ff70fc2012-03-05 11:24:26 -0800460
461 spin_unlock(&priv->statistics.lock);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100462}
463
Johannes Berg73e686f2015-06-23 21:22:09 +0200464static void iwlagn_rx_reply_statistics(struct iwl_priv *priv,
465 struct iwl_rx_cmd_buffer *rxb)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100466{
467 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800468 struct iwl_notif_statistics *stats = (void *)pkt->data;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100469
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800470 if (le32_to_cpu(stats->flag) & UCODE_STATISTICS_CLEAR_MSK) {
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100471#ifdef CONFIG_IWLWIFI_DEBUGFS
Johannes Berg0da0e5b2011-04-08 08:14:56 -0700472 memset(&priv->accum_stats, 0,
473 sizeof(priv->accum_stats));
474 memset(&priv->delta_stats, 0,
475 sizeof(priv->delta_stats));
476 memset(&priv->max_delta_stats, 0,
477 sizeof(priv->max_delta_stats));
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100478#endif
479 IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
480 }
Johannes Berg73e686f2015-06-23 21:22:09 +0200481
482 iwlagn_rx_statistics(priv, rxb);
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100483}
484
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100485/* Handle notification from uCode that card's power state is changing
486 * due to software, hardware, or critical temperature RFKILL */
Johannes Berg73e686f2015-06-23 21:22:09 +0200487static void iwlagn_rx_card_state_notif(struct iwl_priv *priv,
488 struct iwl_rx_cmd_buffer *rxb)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100489{
490 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800491 struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
492 u32 flags = le32_to_cpu(card_state_notif->flags);
Don Fry1353a7b2012-03-07 09:52:43 -0800493 unsigned long status = priv->status;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100494
495 IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
496 (flags & HW_CARD_DISABLED) ? "Kill" : "On",
497 (flags & SW_CARD_DISABLED) ? "Kill" : "On",
498 (flags & CT_CARD_DISABLED) ?
499 "Reached" : "Not reached");
500
501 if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
502 CT_CARD_DISABLED)) {
503
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700504 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_SET,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100505 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
506
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700507 iwl_write_direct32(priv->trans, HBUS_TARG_MBX_C,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100508 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
509
510 if (!(flags & RXON_CARD_DISABLED)) {
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700511 iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100512 CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
Emmanuel Grumbach68e8dfd2012-04-18 07:28:17 -0700513 iwl_write_direct32(priv->trans, HBUS_TARG_MBX_C,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100514 HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
515 }
516 if (flags & CT_CARD_DISABLED)
517 iwl_tt_enter_ct_kill(priv);
518 }
519 if (!(flags & CT_CARD_DISABLED))
520 iwl_tt_exit_ct_kill(priv);
521
522 if (flags & HW_CARD_DISABLED)
Don Fry83626402012-03-07 09:52:37 -0800523 set_bit(STATUS_RF_KILL_HW, &priv->status);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100524 else
Don Fry83626402012-03-07 09:52:37 -0800525 clear_bit(STATUS_RF_KILL_HW, &priv->status);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100526
527
528 if (!(flags & RXON_CARD_DISABLED))
529 iwl_scan_cancel(priv);
530
531 if ((test_bit(STATUS_RF_KILL_HW, &status) !=
Don Fry83626402012-03-07 09:52:37 -0800532 test_bit(STATUS_RF_KILL_HW, &priv->status)))
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100533 wiphy_rfkill_set_hw_state(priv->hw->wiphy,
Don Fry83626402012-03-07 09:52:37 -0800534 test_bit(STATUS_RF_KILL_HW, &priv->status));
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100535}
536
Johannes Berg73e686f2015-06-23 21:22:09 +0200537static void iwlagn_rx_missed_beacon_notif(struct iwl_priv *priv,
538 struct iwl_rx_cmd_buffer *rxb)
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100539
540{
541 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800542 struct iwl_missed_beacon_notif *missed_beacon = (void *)pkt->data;
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100543
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100544 if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
545 priv->missed_beacon_threshold) {
546 IWL_DEBUG_CALIB(priv,
547 "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
548 le32_to_cpu(missed_beacon->consecutive_missed_beacons),
549 le32_to_cpu(missed_beacon->total_missed_becons),
550 le32_to_cpu(missed_beacon->num_recvd_beacons),
551 le32_to_cpu(missed_beacon->num_expected_beacons));
Don Fry83626402012-03-07 09:52:37 -0800552 if (!test_bit(STATUS_SCANNING, &priv->status))
Stanislaw Gruszka67289942011-02-28 14:33:17 +0100553 iwl_init_sensitivity(priv);
554 }
555}
556
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100557/* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
558 * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
Johannes Berg73e686f2015-06-23 21:22:09 +0200559static void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
560 struct iwl_rx_cmd_buffer *rxb)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100561{
562 struct iwl_rx_packet *pkt = rxb_addr(rxb);
563
Wey-Yi Guy898ed672011-07-13 08:38:57 -0700564 priv->last_phy_res_valid = true;
Johannes Berg12bf6f42012-07-05 13:05:08 +0200565 priv->ampdu_ref++;
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800566 memcpy(&priv->last_phy_res, pkt->data,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100567 sizeof(struct iwl_rx_phy_res));
568}
569
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800570/*
571 * returns non-zero if packet should be dropped
572 */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700573static int iwlagn_set_decrypted_flag(struct iwl_priv *priv,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100574 struct ieee80211_hdr *hdr,
575 u32 decrypt_res,
576 struct ieee80211_rx_status *stats)
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800577{
578 u16 fc = le16_to_cpu(hdr->frame_control);
579
Johannes Berg246ed352010-08-23 10:46:32 +0200580 /*
581 * All contexts have the same setting here due to it being
582 * a module parameter, so OK to check any context.
583 */
584 if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
585 RXON_FILTER_DIS_DECRYPT_MSK)
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800586 return 0;
587
588 if (!(fc & IEEE80211_FCTL_PROTECTED))
589 return 0;
590
Tomas Winklere1623442009-01-27 14:27:56 -0800591 IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800592 switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
593 case RX_RES_STATUS_SEC_TYPE_TKIP:
594 /* The uCode has got a bad phase 1 Key, pushes the packet.
595 * Decryption will be done in SW. */
596 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
597 RX_RES_STATUS_BAD_KEY_TTAK)
598 break;
599
600 case RX_RES_STATUS_SEC_TYPE_WEP:
601 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
602 RX_RES_STATUS_BAD_ICV_MIC) {
603 /* bad ICV, the packet is destroyed since the
604 * decryption is inplace, drop it */
Tomas Winklere1623442009-01-27 14:27:56 -0800605 IWL_DEBUG_RX(priv, "Packet destroyed\n");
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800606 return -1;
607 }
608 case RX_RES_STATUS_SEC_TYPE_CCMP:
609 if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
610 RX_RES_STATUS_DECRYPT_OK) {
Tomas Winklere1623442009-01-27 14:27:56 -0800611 IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
Emmanuel Grumbach1781a072008-06-30 17:23:09 +0800612 stats->flag |= RX_FLAG_DECRYPTED;
613 }
614 break;
615
616 default:
617 break;
618 }
619 return 0;
620}
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100621
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700622static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100623 struct ieee80211_hdr *hdr,
624 u16 len,
625 u32 ampdu_status,
Johannes Berg48a2d662012-03-05 11:24:39 -0800626 struct iwl_rx_cmd_buffer *rxb,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100627 struct ieee80211_rx_status *stats)
628{
629 struct sk_buff *skb;
630 __le16 fc = hdr->frame_control;
Garen Tamrazian68b99312011-03-30 02:29:32 -0700631 struct iwl_rxon_context *ctx;
Eric Dumazeted905422012-03-24 00:29:46 -0400632 unsigned int hdrlen, fraglen;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100633
634 /* We only process data packets if the interface is open */
635 if (unlikely(!priv->is_open)) {
636 IWL_DEBUG_DROP_LIMIT(priv,
637 "Dropping packet while interface is not open.\n");
638 return;
639 }
640
641 /* In case of HW accelerated crypto and bad decryption, drop */
Johannes Berg3b37f4c2017-05-30 16:45:31 +0200642 if (!iwlwifi_mod_params.swcrypto &&
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700643 iwlagn_set_decrypted_flag(priv, hdr, ampdu_status, stats))
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100644 return;
645
Eric Dumazeted905422012-03-24 00:29:46 -0400646 /* Dont use dev_alloc_skb(), we'll have enough headroom once
647 * ieee80211_hdr pulled.
648 */
649 skb = alloc_skb(128, GFP_ATOMIC);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100650 if (!skb) {
Eric Dumazeted905422012-03-24 00:29:46 -0400651 IWL_ERR(priv, "alloc_skb failed\n");
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100652 return;
653 }
Eric Dumazet56138f52012-05-18 04:48:33 +0000654 /* If frame is small enough to fit in skb->head, pull it completely.
655 * If not, only pull ieee80211_hdr so that splice() or TCP coalesce
656 * are more efficient.
657 */
658 hdrlen = (len <= skb_tailroom(skb)) ? len : sizeof(*hdr);
659
Johannes Berg59ae1d12017-06-16 14:29:20 +0200660 skb_put_data(skb, hdr, hdrlen);
Eric Dumazeted905422012-03-24 00:29:46 -0400661 fraglen = len - hdrlen;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100662
Eric Dumazeted905422012-03-24 00:29:46 -0400663 if (fraglen) {
John W. Linville341352d2012-05-08 22:18:09 -0400664 int offset = (void *)hdr + hdrlen -
665 rxb_addr(rxb) + rxb_offset(rxb);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100666
Eric Dumazeted905422012-03-24 00:29:46 -0400667 skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
668 fraglen, rxb->truesize);
669 }
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100670
Garen Tamrazian68b99312011-03-30 02:29:32 -0700671 /*
672 * Wake any queues that were stopped due to a passive channel tx
673 * failure. This can happen because the regulatory enforcement in
674 * the device waits for a beacon before allowing transmission,
675 * sometimes even after already having transmitted frames for the
676 * association because the new RXON may reset the information.
677 */
Johannes Berge755f882012-03-07 09:52:16 -0800678 if (unlikely(ieee80211_is_beacon(fc) && priv->passive_no_rx)) {
Garen Tamrazian68b99312011-03-30 02:29:32 -0700679 for_each_context(priv, ctx) {
Joe Perches2e42e472012-05-09 17:17:46 +0000680 if (!ether_addr_equal(hdr->addr3,
681 ctx->active.bssid_addr))
Garen Tamrazian68b99312011-03-30 02:29:32 -0700682 continue;
Johannes Berge755f882012-03-07 09:52:16 -0800683 iwlagn_lift_passive_no_rx(priv);
Garen Tamrazian68b99312011-03-30 02:29:32 -0700684 }
685 }
686
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100687 memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
688
Johannes Bergd63b5482016-03-31 20:02:02 +0300689 ieee80211_rx_napi(priv->hw, NULL, skb, priv->napi);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100690}
691
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700692static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100693{
694 u32 decrypt_out = 0;
695
696 if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
697 RX_RES_STATUS_STATION_FOUND)
698 decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
699 RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
700
701 decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
702
703 /* packet was not encrypted */
704 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
705 RX_RES_STATUS_SEC_TYPE_NONE)
706 return decrypt_out;
707
708 /* packet was encrypted with unknown alg */
709 if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
710 RX_RES_STATUS_SEC_TYPE_ERR)
711 return decrypt_out;
712
713 /* decryption was not done in HW */
714 if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
715 RX_MPDU_RES_STATUS_DEC_DONE_MSK)
716 return decrypt_out;
717
718 switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
719
720 case RX_RES_STATUS_SEC_TYPE_CCMP:
721 /* alg is CCM: check MIC only */
722 if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
723 /* Bad MIC */
724 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
725 else
726 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
727
728 break;
729
730 case RX_RES_STATUS_SEC_TYPE_TKIP:
731 if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
732 /* Bad TTAK */
733 decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
734 break;
735 }
736 /* fall through if TTAK OK */
737 default:
738 if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
739 decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
740 else
741 decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
742 break;
743 }
744
745 IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
746 decrypt_in, decrypt_out);
747
748 return decrypt_out;
749}
750
Don Fry5c3d29f2011-07-08 08:46:29 -0700751/* Calc max signal level (dBm) among 3 possible receivers */
752static int iwlagn_calc_rssi(struct iwl_priv *priv,
753 struct iwl_rx_phy_res *rx_resp)
754{
755 /* data from PHY/DSP regarding signal strength, etc.,
756 * contents are always there, not configurable by host
757 */
758 struct iwlagn_non_cfg_phy *ncphy =
759 (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
760 u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
761 u8 agc;
762
763 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
764 agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
765
766 /* Find max rssi among 3 possible receivers.
767 * These values are measured by the digital signal processor (DSP).
768 * They should stay fairly constant even as the signal strength varies,
769 * if the radio's automatic gain control (AGC) is working right.
770 * AGC value (see below) will provide the "interesting" info.
771 */
772 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
773 rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
774 IWLAGN_OFDM_RSSI_A_BIT_POS;
775 rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
776 IWLAGN_OFDM_RSSI_B_BIT_POS;
777 val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
778 rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
779 IWLAGN_OFDM_RSSI_C_BIT_POS;
780
781 max_rssi = max_t(u32, rssi_a, rssi_b);
782 max_rssi = max_t(u32, max_rssi, rssi_c);
783
784 IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
785 rssi_a, rssi_b, rssi_c, max_rssi, agc);
786
787 /* dBm = max_rssi dB - agc dB - constant.
788 * Higher AGC (higher radio gain) means lower signal. */
789 return max_rssi - agc - IWLAGN_RSSI_OFFSET;
790}
791
Emmanuel Grumbach6d044b92012-07-05 17:31:56 +0300792/* Called for REPLY_RX_MPDU_CMD */
Johannes Berg73e686f2015-06-23 21:22:09 +0200793static void iwlagn_rx_reply_rx(struct iwl_priv *priv,
794 struct iwl_rx_cmd_buffer *rxb)
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100795{
796 struct ieee80211_hdr *header;
Johannes Berg1d815ef2012-11-23 23:46:19 +0100797 struct ieee80211_rx_status rx_status = {};
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100798 struct iwl_rx_packet *pkt = rxb_addr(rxb);
799 struct iwl_rx_phy_res *phy_res;
800 __le32 rx_pkt_status;
801 struct iwl_rx_mpdu_res_start *amsdu;
802 u32 len;
803 u32 ampdu_status;
804 u32 rate_n_flags;
805
Emmanuel Grumbach6d044b92012-07-05 17:31:56 +0300806 if (!priv->last_phy_res_valid) {
807 IWL_ERR(priv, "MPDU frame without cached PHY data\n");
Johannes Berg73e686f2015-06-23 21:22:09 +0200808 return;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100809 }
Emmanuel Grumbach6d044b92012-07-05 17:31:56 +0300810 phy_res = &priv->last_phy_res;
811 amsdu = (struct iwl_rx_mpdu_res_start *)pkt->data;
812 header = (struct ieee80211_hdr *)(pkt->data + sizeof(*amsdu));
813 len = le16_to_cpu(amsdu->byte_count);
814 rx_pkt_status = *(__le32 *)(pkt->data + sizeof(*amsdu) + len);
815 ampdu_status = iwlagn_translate_rx_status(priv,
816 le32_to_cpu(rx_pkt_status));
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100817
818 if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
Johannes Berg0ca24da2012-03-15 13:26:46 -0700819 IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d\n",
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100820 phy_res->cfg_phy_cnt);
Johannes Berg73e686f2015-06-23 21:22:09 +0200821 return;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100822 }
823
824 if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
825 !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
826 IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
827 le32_to_cpu(rx_pkt_status));
Johannes Berg73e686f2015-06-23 21:22:09 +0200828 return;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100829 }
830
831 /* This will be used in several places later */
832 rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
833
834 /* rx_status carries information about the packet to mac80211 */
835 rx_status.mactime = le64_to_cpu(phy_res->timestamp);
836 rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
Johannes Berg57fbcce2016-04-12 15:56:15 +0200837 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100838 rx_status.freq =
839 ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
840 rx_status.band);
841 rx_status.rate_idx =
842 iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
843 rx_status.flag = 0;
844
845 /* TSF isn't reliable. In order to allow smooth user experience,
846 * this W/A doesn't propagate it to the mac80211 */
Thomas Pedersenf4bda332012-11-13 10:46:27 -0800847 /*rx_status.flag |= RX_FLAG_MACTIME_START;*/
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100848
849 priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
850
851 /* Find max signal strength (dBm) among 3 antenna/receiver chains */
Don Fry5c3d29f2011-07-08 08:46:29 -0700852 rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100853
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100854 IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
855 rx_status.signal, (unsigned long long)rx_status.mactime);
856
857 /*
858 * "antenna number"
859 *
860 * It seems that the antenna field in the phy flags value
861 * is actually a bit field. This is undefined by radiotap,
862 * it wants an actual antenna number but I always get "7"
863 * for most legacy frames I receive indicating that the
864 * same frame was received on all three RX chains.
865 *
866 * I think this field should be removed in favor of a
867 * new 802.11n radiotap field "RX chains" that is defined
868 * as a bitmask.
869 */
870 rx_status.antenna =
871 (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
872 >> RX_RES_PHY_FLAGS_ANTENNA_POS;
873
874 /* set the preamble flag if appropriate */
875 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200876 rx_status.enc_flags |= RX_ENC_FLAG_SHORTPRE;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100877
Johannes Berg12bf6f42012-07-05 13:05:08 +0200878 if (phy_res->phy_flags & RX_RES_PHY_FLAGS_AGG_MSK) {
879 /*
880 * We know which subframes of an A-MPDU belong
881 * together since we get a single PHY response
882 * from the firmware for all of them
883 */
884 rx_status.flag |= RX_FLAG_AMPDU_DETAILS;
885 rx_status.ampdu_reference = priv->ampdu_ref;
886 }
887
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100888 /* Set up the HT phy flags */
889 if (rate_n_flags & RATE_MCS_HT_MSK)
Johannes Bergda6a4352017-04-26 12:14:59 +0200890 rx_status.encoding = RX_ENC_HT;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100891 if (rate_n_flags & RATE_MCS_HT40_MSK)
Johannes Berg2f242bf2017-05-05 11:53:19 +0200892 rx_status.bw = RATE_INFO_BW_40;
893 else
894 rx_status.bw = RATE_INFO_BW_20;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100895 if (rate_n_flags & RATE_MCS_SGI_MSK)
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200896 rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
Johannes Berg53e11162012-05-16 23:59:03 +0200897 if (rate_n_flags & RATE_MCS_GF_MSK)
Johannes Berg7fdd69c2017-04-26 11:13:00 +0200898 rx_status.enc_flags |= RX_ENC_FLAG_HT_GF;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100899
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700900 iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100901 rxb, &rx_status);
902}
903
Johannes Berg73e686f2015-06-23 21:22:09 +0200904static void iwlagn_rx_noa_notification(struct iwl_priv *priv,
905 struct iwl_rx_cmd_buffer *rxb)
Johannes Berg79d3eef2011-11-10 06:55:01 -0800906{
907 struct iwl_wipan_noa_data *new_data, *old_data;
908 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Johannes Bergf8d7c1a2012-03-06 13:31:02 -0800909 struct iwl_wipan_noa_notification *noa_notif = (void *)pkt->data;
Johannes Berg79d3eef2011-11-10 06:55:01 -0800910
911 /* no condition -- we're in softirq */
912 old_data = rcu_dereference_protected(priv->noa_data, true);
913
914 if (noa_notif->noa_active) {
915 u32 len = le16_to_cpu(noa_notif->noa_attribute.length);
916 u32 copylen = len;
917
918 /* EID, len, OUI, subtype */
919 len += 1 + 1 + 3 + 1;
920 /* P2P id, P2P length */
921 len += 1 + 2;
922 copylen += 1 + 2;
923
924 new_data = kmalloc(sizeof(*new_data) + len, GFP_ATOMIC);
925 if (new_data) {
926 new_data->length = len;
927 new_data->data[0] = WLAN_EID_VENDOR_SPECIFIC;
928 new_data->data[1] = len - 2; /* not counting EID, len */
929 new_data->data[2] = (WLAN_OUI_WFA >> 16) & 0xff;
930 new_data->data[3] = (WLAN_OUI_WFA >> 8) & 0xff;
931 new_data->data[4] = (WLAN_OUI_WFA >> 0) & 0xff;
932 new_data->data[5] = WLAN_OUI_TYPE_WFA_P2P;
933 memcpy(&new_data->data[6], &noa_notif->noa_attribute,
934 copylen);
935 }
936 } else
937 new_data = NULL;
938
939 rcu_assign_pointer(priv->noa_data, new_data);
940
941 if (old_data)
942 kfree_rcu(old_data, rcu_head);
Johannes Berg79d3eef2011-11-10 06:55:01 -0800943}
944
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100945/**
946 * iwl_setup_rx_handlers - Initialize Rx handler callbacks
947 *
948 * Setup the RX handlers for each of the reply types sent from the uCode
949 * to the host.
950 */
951void iwl_setup_rx_handlers(struct iwl_priv *priv)
952{
Johannes Berg73e686f2015-06-23 21:22:09 +0200953 void (**handlers)(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100954
955 handlers = priv->rx_handlers;
956
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700957 handlers[REPLY_ERROR] = iwlagn_rx_reply_error;
958 handlers[CHANNEL_SWITCH_NOTIFICATION] = iwlagn_rx_csa;
959 handlers[SPECTRUM_MEASURE_NOTIFICATION] =
960 iwlagn_rx_spectrum_measure_notif;
961 handlers[PM_SLEEP_NOTIFICATION] = iwlagn_rx_pm_sleep_notif;
962 handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
963 iwlagn_rx_pm_debug_statistics_notif;
964 handlers[BEACON_NOTIFICATION] = iwlagn_rx_beacon_notif;
Emmanuel Grumbach247c61d2011-09-20 15:37:23 -0700965 handlers[REPLY_ADD_STA] = iwl_add_sta_callback;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100966
Johannes Berg79d3eef2011-11-10 06:55:01 -0800967 handlers[REPLY_WIPAN_NOA_NOTIFICATION] = iwlagn_rx_noa_notification;
968
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100969 /*
970 * The same handler is used for both the REPLY to a discrete
971 * statistics request from the host as well as for the periodic
972 * statistics notifications (after received beacons) from the uCode.
973 */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700974 handlers[REPLY_STATISTICS_CMD] = iwlagn_rx_reply_statistics;
975 handlers[STATISTICS_NOTIFICATION] = iwlagn_rx_statistics;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100976
977 iwl_setup_rx_scan_handlers(priv);
978
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700979 handlers[CARD_STATE_NOTIFICATION] = iwlagn_rx_card_state_notif;
980 handlers[MISSED_BEACONS_NOTIFICATION] =
981 iwlagn_rx_missed_beacon_notif;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100982
983 /* Rx handlers */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700984 handlers[REPLY_RX_PHY_CMD] = iwlagn_rx_reply_rx_phy;
985 handlers[REPLY_RX_MPDU_CMD] = iwlagn_rx_reply_rx;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100986
987 /* block ack */
Wey-Yi Guy3246c4e2011-10-10 07:27:13 -0700988 handlers[REPLY_COMPRESSED_BA] =
989 iwlagn_rx_reply_compressed_ba;
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100990
Emmanuel Grumbach8d301192011-07-03 15:45:34 +0300991 priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
992
993 /* set up notification wait support */
Johannes Berg4bd14dd2012-03-06 13:30:58 -0800994 iwl_notification_wait_init(&priv->notif_wait);
Emmanuel Grumbach8d301192011-07-03 15:45:34 +0300995
996 /* Set up BT Rx handlers */
Johannes Berg0d8877a2013-05-17 10:36:29 +0200997 if (priv->lib->bt_params)
Johannes Berg562f08e2012-03-07 09:52:18 -0800998 iwlagn_bt_rx_handler_setup(priv);
Stanislaw Gruszka466a19a2011-03-04 17:51:49 +0100999}
Emmanuel Grumbach1ab9f6c12011-07-07 07:59:02 -07001000
Johannes Berg1be5d8c2015-06-11 16:51:24 +02001001void iwl_rx_dispatch(struct iwl_op_mode *op_mode, struct napi_struct *napi,
1002 struct iwl_rx_cmd_buffer *rxb)
Emmanuel Grumbach1ab9f6c12011-07-07 07:59:02 -07001003{
1004 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbachdb70f292012-02-09 16:08:15 +02001005 struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
Emmanuel Grumbach1ab9f6c12011-07-07 07:59:02 -07001006
1007 /*
1008 * Do the notification wait before RX handlers so
1009 * even if the RX handler consumes the RXB we have
1010 * access to it in the notification wait entry.
1011 */
Johannes Berg4bd14dd2012-03-06 13:30:58 -08001012 iwl_notification_wait_notify(&priv->notif_wait, pkt);
Emmanuel Grumbach1ab9f6c12011-07-07 07:59:02 -07001013
Johannes Berg49464ae2013-06-17 23:13:31 +02001014 /* Based on type of command response or notification,
1015 * handle those that need handling via function in
1016 * rx_handlers table. See iwl_setup_rx_handlers() */
1017 if (priv->rx_handlers[pkt->hdr.cmd]) {
1018 priv->rx_handlers_stats[pkt->hdr.cmd]++;
Johannes Berg73e686f2015-06-23 21:22:09 +02001019 priv->rx_handlers[pkt->hdr.cmd](priv, rxb);
Johannes Berg49464ae2013-06-17 23:13:31 +02001020 } else {
1021 /* No handling needed */
1022 IWL_DEBUG_RX(priv, "No handler needed for %s, 0x%02x\n",
Sharon Dvir39bdb172015-10-15 18:18:09 +03001023 iwl_get_cmd_string(priv->trans,
1024 iwl_cmd_id(pkt->hdr.cmd,
1025 0, 0)),
Johannes Berg49464ae2013-06-17 23:13:31 +02001026 pkt->hdr.cmd);
Emmanuel Grumbach1ab9f6c12011-07-07 07:59:02 -07001027 }
1028}