Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
rx.c
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2012 Intel Corporation. All rights reserved.
4  *
5  * Portions of this file are derived from the ipw3945 project, as well
6  * as portionhelp of the ieee80211 subsystem header files.
7  *
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:
25  * Intel Linux Wireless <[email protected]>
26  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27  *
28  *****************************************************************************/
29 
30 #include <linux/etherdevice.h>
31 #include <linux/slab.h>
32 #include <linux/sched.h>
33 #include <net/mac80211.h>
34 #include <asm/unaligned.h>
35 #include "iwl-io.h"
36 #include "dev.h"
37 #include "calib.h"
38 #include "agn.h"
39 
40 #define IWL_CMD_ENTRY(x) [x] = #x
41 
117 };
118 #undef IWL_CMD_ENTRY
119 
120 /******************************************************************************
121  *
122  * Generic RX handler implementations
123  *
124  ******************************************************************************/
125 
126 static int iwlagn_rx_reply_error(struct iwl_priv *priv,
127  struct iwl_rx_cmd_buffer *rxb,
128  struct iwl_device_cmd *cmd)
129 {
130  struct iwl_rx_packet *pkt = rxb_addr(rxb);
131  struct iwl_error_resp *err_resp = (void *)pkt->data;
132 
133  IWL_ERR(priv, "Error Reply type 0x%08X cmd REPLY_ERROR (0x%02X) "
134  "seq 0x%04X ser 0x%08X\n",
135  le32_to_cpu(err_resp->error_type),
136  err_resp->cmd_id,
137  le16_to_cpu(err_resp->bad_cmd_seq_num),
138  le32_to_cpu(err_resp->error_info));
139  return 0;
140 }
141 
142 static int iwlagn_rx_csa(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
143  struct iwl_device_cmd *cmd)
144 {
145  struct iwl_rx_packet *pkt = rxb_addr(rxb);
146  struct iwl_csa_notification *csa = (void *)pkt->data;
147  /*
148  * MULTI-FIXME
149  * See iwlagn_mac_channel_switch.
150  */
151  struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
152  struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
153 
155  return 0;
156 
157  if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
158  rxon->channel = csa->channel;
159  ctx->staging.channel = csa->channel;
160  IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
161  le16_to_cpu(csa->channel));
162  iwl_chswitch_done(priv, true);
163  } else {
164  IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
165  le16_to_cpu(csa->channel));
166  iwl_chswitch_done(priv, false);
167  }
168  return 0;
169 }
170 
171 
172 static int iwlagn_rx_spectrum_measure_notif(struct iwl_priv *priv,
173  struct iwl_rx_cmd_buffer *rxb,
174  struct iwl_device_cmd *cmd)
175 {
176  struct iwl_rx_packet *pkt = rxb_addr(rxb);
177  struct iwl_spectrum_notification *report = (void *)pkt->data;
178 
179  if (!report->state) {
180  IWL_DEBUG_11H(priv,
181  "Spectrum Measure Notification: Start\n");
182  return 0;
183  }
184 
185  memcpy(&priv->measure_report, report, sizeof(*report));
187  return 0;
188 }
189 
190 static int iwlagn_rx_pm_sleep_notif(struct iwl_priv *priv,
191  struct iwl_rx_cmd_buffer *rxb,
192  struct iwl_device_cmd *cmd)
193 {
194 #ifdef CONFIG_IWLWIFI_DEBUG
195  struct iwl_rx_packet *pkt = rxb_addr(rxb);
196  struct iwl_sleep_notification *sleep = (void *)pkt->data;
197  IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
198  sleep->pm_sleep_mode, sleep->pm_wakeup_src);
199 #endif
200  return 0;
201 }
202 
203 static int iwlagn_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
204  struct iwl_rx_cmd_buffer *rxb,
205  struct iwl_device_cmd *cmd)
206 {
207  struct iwl_rx_packet *pkt = rxb_addr(rxb);
210  IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
211  "notification for PM_DEBUG_STATISTIC_NOTIFIC:\n", len);
212  iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->data, len);
213  return 0;
214 }
215 
216 static int iwlagn_rx_beacon_notif(struct iwl_priv *priv,
217  struct iwl_rx_cmd_buffer *rxb,
218  struct iwl_device_cmd *cmd)
219 {
220  struct iwl_rx_packet *pkt = rxb_addr(rxb);
221  struct iwlagn_beacon_notif *beacon = (void *)pkt->data;
222 #ifdef CONFIG_IWLWIFI_DEBUG
223  u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
224  u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
225 
226  IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
227  "tsf:0x%.8x%.8x rate:%d\n",
229  beacon->beacon_notify_hdr.failure_frame,
230  le32_to_cpu(beacon->ibss_mgr_status),
231  le32_to_cpu(beacon->high_tsf),
232  le32_to_cpu(beacon->low_tsf), rate);
233 #endif
234 
235  priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
236 
237  return 0;
238 }
239 
246 static bool iwlagn_good_plcp_health(struct iwl_priv *priv,
247  struct statistics_rx_phy *cur_ofdm,
248  struct statistics_rx_ht_phy *cur_ofdm_ht,
249  unsigned int msecs)
250 {
251  int delta;
252  int threshold = priv->plcp_delta_threshold;
253 
254  if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
255  IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
256  return true;
257  }
258 
259  delta = le32_to_cpu(cur_ofdm->plcp_err) -
260  le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
261  le32_to_cpu(cur_ofdm_ht->plcp_err) -
262  le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
263 
264  /* Can be negative if firmware reset statistics */
265  if (delta <= 0)
266  return true;
267 
268  if ((delta * 100 / msecs) > threshold) {
269  IWL_DEBUG_RADIO(priv,
270  "plcp health threshold %u delta %d msecs %u\n",
271  threshold, delta, msecs);
272  return false;
273  }
274 
275  return true;
276 }
277 
278 int iwl_force_rf_reset(struct iwl_priv *priv, bool external)
279 {
280  struct iwl_rf_reset *rf_reset;
281 
282  if (test_bit(STATUS_EXIT_PENDING, &priv->status))
283  return -EAGAIN;
284 
285  if (!iwl_is_any_associated(priv)) {
286  IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
287  return -ENOLINK;
288  }
289 
290  rf_reset = &priv->rf_reset;
291  rf_reset->reset_request_count++;
292  if (!external && rf_reset->last_reset_jiffies &&
293  time_after(rf_reset->last_reset_jiffies +
295  IWL_DEBUG_INFO(priv, "RF reset rejected\n");
296  rf_reset->reset_reject_count++;
297  return -EAGAIN;
298  }
299  rf_reset->reset_success_count++;
300  rf_reset->last_reset_jiffies = jiffies;
301 
302  /*
303  * There is no easy and better way to force reset the radio,
304  * the only known method is switching channel which will force to
305  * reset and tune the radio.
306  * Use internal short scan (single channel) operation to should
307  * achieve this objective.
308  * Driver should reset the radio when number of consecutive missed
309  * beacon, or any other uCode error condition detected.
310  */
311  IWL_DEBUG_INFO(priv, "perform radio reset.\n");
313  return 0;
314 }
315 
316 
317 static void iwlagn_recover_from_statistics(struct iwl_priv *priv,
318  struct statistics_rx_phy *cur_ofdm,
319  struct statistics_rx_ht_phy *cur_ofdm_ht,
320  struct statistics_tx *tx,
321  unsigned long stamp)
322 {
323  unsigned int msecs;
324 
325  if (test_bit(STATUS_EXIT_PENDING, &priv->status))
326  return;
327 
328  msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
329 
330  /* Only gather statistics and update time stamp when not associated */
331  if (!iwl_is_any_associated(priv))
332  return;
333 
334  /* Do not check/recover when do not have enough statistics data */
335  if (msecs < 99)
336  return;
337 
338  if (iwlwifi_mod_params.plcp_check &&
339  !iwlagn_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
340  iwl_force_rf_reset(priv, false);
341 }
342 
343 /* Calculate noise level, based on measurements during network silence just
344  * before arriving beacon. This measurement can be done only if we know
345  * exactly when to expect beacons, therefore only when we're associated. */
346 static void iwlagn_rx_calc_noise(struct iwl_priv *priv)
347 {
349  int num_active_rx = 0;
350  int total_silence = 0;
351  int bcn_silence_a, bcn_silence_b, bcn_silence_c;
352  int last_rx_noise;
353 
354  rx_info = &priv->statistics.rx_non_phy;
355 
356  bcn_silence_a =
358  bcn_silence_b =
360  bcn_silence_c =
362 
363  if (bcn_silence_a) {
364  total_silence += bcn_silence_a;
365  num_active_rx++;
366  }
367  if (bcn_silence_b) {
368  total_silence += bcn_silence_b;
369  num_active_rx++;
370  }
371  if (bcn_silence_c) {
372  total_silence += bcn_silence_c;
373  num_active_rx++;
374  }
375 
376  /* Average among active antennas */
377  if (num_active_rx)
378  last_rx_noise = (total_silence / num_active_rx) - 107;
379  else
380  last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
381 
382  IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
383  bcn_silence_a, bcn_silence_b, bcn_silence_c,
384  last_rx_noise);
385 }
386 
387 #ifdef CONFIG_IWLWIFI_DEBUGFS
388 /*
389  * based on the assumption of all statistics counter are in DWORD
390  * FIXME: This function is for debugging, do not deal with
391  * the case of counters roll-over.
392  */
393 static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
394  __le32 *max_delta, __le32 *accum, int size)
395 {
396  int i;
397 
398  for (i = 0;
399  i < size / sizeof(__le32);
400  i++, prev++, cur++, delta++, max_delta++, accum++) {
401  if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
402  *delta = cpu_to_le32(
403  le32_to_cpu(*cur) - le32_to_cpu(*prev));
404  le32_add_cpu(accum, le32_to_cpu(*delta));
405  if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
406  *max_delta = *delta;
407  }
408  }
409 }
410 
411 static void
412 iwlagn_accumulative_statistics(struct iwl_priv *priv,
414  struct statistics_rx_non_phy *rx_non_phy,
415  struct statistics_rx_phy *rx_ofdm,
416  struct statistics_rx_ht_phy *rx_ofdm_ht,
417  struct statistics_rx_phy *rx_cck,
418  struct statistics_tx *tx,
419  struct statistics_bt_activity *bt_activity)
420 {
421 #define ACCUM(_name) \
422  accum_stats((__le32 *)&priv->statistics._name, \
423  (__le32 *)_name, \
424  (__le32 *)&priv->delta_stats._name, \
425  (__le32 *)&priv->max_delta_stats._name, \
426  (__le32 *)&priv->accum_stats._name, \
427  sizeof(*_name));
428 
429  ACCUM(common);
430  ACCUM(rx_non_phy);
431  ACCUM(rx_ofdm);
432  ACCUM(rx_ofdm_ht);
433  ACCUM(rx_cck);
434  ACCUM(tx);
435  if (bt_activity)
436  ACCUM(bt_activity);
437 #undef ACCUM
438 }
439 #else
440 static inline void
441 iwlagn_accumulative_statistics(struct iwl_priv *priv,
442  struct statistics_general_common *common,
443  struct statistics_rx_non_phy *rx_non_phy,
444  struct statistics_rx_phy *rx_ofdm,
445  struct statistics_rx_ht_phy *rx_ofdm_ht,
446  struct statistics_rx_phy *rx_cck,
447  struct statistics_tx *tx,
448  struct statistics_bt_activity *bt_activity)
449 {
450 }
451 #endif
452 
453 static int iwlagn_rx_statistics(struct iwl_priv *priv,
454  struct iwl_rx_cmd_buffer *rxb,
455  struct iwl_device_cmd *cmd)
456 {
457  unsigned long stamp = jiffies;
458  const int reg_recalib_period = 60;
459  int change;
460  struct iwl_rx_packet *pkt = rxb_addr(rxb);
462  __le32 *flag;
464  struct statistics_rx_non_phy *rx_non_phy;
465  struct statistics_rx_phy *rx_ofdm;
466  struct statistics_rx_ht_phy *rx_ofdm_ht;
467  struct statistics_rx_phy *rx_cck;
468  struct statistics_tx *tx;
469  struct statistics_bt_activity *bt_activity;
470 
471  len -= sizeof(struct iwl_cmd_header); /* skip header */
472 
473  IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
474  len);
475 
476  spin_lock(&priv->statistics.lock);
477 
478  if (len == sizeof(struct iwl_bt_notif_statistics)) {
480  stats = (void *)&pkt->data;
481  flag = &stats->flag;
482  common = &stats->general.common;
483  rx_non_phy = &stats->rx.general.common;
484  rx_ofdm = &stats->rx.ofdm;
485  rx_ofdm_ht = &stats->rx.ofdm_ht;
486  rx_cck = &stats->rx.cck;
487  tx = &stats->tx;
488  bt_activity = &stats->general.activity;
489 
490 #ifdef CONFIG_IWLWIFI_DEBUGFS
491  /* handle this exception directly */
492  priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
493  le32_add_cpu(&priv->statistics.accum_num_bt_kills,
494  le32_to_cpu(stats->rx.general.num_bt_kills));
495 #endif
496  } else if (len == sizeof(struct iwl_notif_statistics)) {
497  struct iwl_notif_statistics *stats;
498  stats = (void *)&pkt->data;
499  flag = &stats->flag;
500  common = &stats->general.common;
501  rx_non_phy = &stats->rx.general;
502  rx_ofdm = &stats->rx.ofdm;
503  rx_ofdm_ht = &stats->rx.ofdm_ht;
504  rx_cck = &stats->rx.cck;
505  tx = &stats->tx;
506  bt_activity = NULL;
507  } else {
508  WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
509  len, sizeof(struct iwl_bt_notif_statistics),
510  sizeof(struct iwl_notif_statistics));
511  spin_unlock(&priv->statistics.lock);
512  return 0;
513  }
514 
515  change = common->temperature != priv->statistics.common.temperature ||
518 
519  iwlagn_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
520  rx_ofdm_ht, rx_cck, tx, bt_activity);
521 
522  iwlagn_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
523 
524  priv->statistics.flag = *flag;
525  memcpy(&priv->statistics.common, common, sizeof(*common));
526  memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
527  memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
528  memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
529  memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
530  memcpy(&priv->statistics.tx, tx, sizeof(*tx));
531 #ifdef CONFIG_IWLWIFI_DEBUGFS
532  if (bt_activity)
533  memcpy(&priv->statistics.bt_activity, bt_activity,
534  sizeof(*bt_activity));
535 #endif
536 
538 
540 
541  /* Reschedule the statistics timer to occur in
542  * reg_recalib_period seconds to ensure we get a
543  * thermal update even if the uCode doesn't give
544  * us one */
545  mod_timer(&priv->statistics_periodic, jiffies +
546  msecs_to_jiffies(reg_recalib_period * 1000));
547 
548  if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
549  (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
550  iwlagn_rx_calc_noise(priv);
552  }
553  if (priv->lib->temperature && change)
554  priv->lib->temperature(priv);
555 
556  spin_unlock(&priv->statistics.lock);
557 
558  return 0;
559 }
560 
561 static int iwlagn_rx_reply_statistics(struct iwl_priv *priv,
562  struct iwl_rx_cmd_buffer *rxb,
563  struct iwl_device_cmd *cmd)
564 {
565  struct iwl_rx_packet *pkt = rxb_addr(rxb);
566  struct iwl_notif_statistics *stats = (void *)pkt->data;
567 
569 #ifdef CONFIG_IWLWIFI_DEBUGFS
570  memset(&priv->accum_stats, 0,
571  sizeof(priv->accum_stats));
572  memset(&priv->delta_stats, 0,
573  sizeof(priv->delta_stats));
574  memset(&priv->max_delta_stats, 0,
575  sizeof(priv->max_delta_stats));
576 #endif
577  IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
578  }
579  iwlagn_rx_statistics(priv, rxb, cmd);
580  return 0;
581 }
582 
583 /* Handle notification from uCode that card's power state is changing
584  * due to software, hardware, or critical temperature RFKILL */
585 static int iwlagn_rx_card_state_notif(struct iwl_priv *priv,
586  struct iwl_rx_cmd_buffer *rxb,
587  struct iwl_device_cmd *cmd)
588 {
589  struct iwl_rx_packet *pkt = rxb_addr(rxb);
590  struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
591  u32 flags = le32_to_cpu(card_state_notif->flags);
592  unsigned long status = priv->status;
593 
594  IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
595  (flags & HW_CARD_DISABLED) ? "Kill" : "On",
596  (flags & SW_CARD_DISABLED) ? "Kill" : "On",
597  (flags & CT_CARD_DISABLED) ?
598  "Reached" : "Not reached");
599 
600  if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
601  CT_CARD_DISABLED)) {
602 
603  iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_SET,
605 
608 
609  if (!(flags & RXON_CARD_DISABLED)) {
610  iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
614  }
615  if (flags & CT_CARD_DISABLED)
616  iwl_tt_enter_ct_kill(priv);
617  }
618  if (!(flags & CT_CARD_DISABLED))
619  iwl_tt_exit_ct_kill(priv);
620 
621  if (flags & HW_CARD_DISABLED)
623  else
625 
626 
627  if (!(flags & RXON_CARD_DISABLED))
628  iwl_scan_cancel(priv);
629 
630  if ((test_bit(STATUS_RF_KILL_HW, &status) !=
632  wiphy_rfkill_set_hw_state(priv->hw->wiphy,
634  else
635  wake_up(&priv->trans->wait_command_queue);
636  return 0;
637 }
638 
639 static int iwlagn_rx_missed_beacon_notif(struct iwl_priv *priv,
640  struct iwl_rx_cmd_buffer *rxb,
641  struct iwl_device_cmd *cmd)
642 
643 {
644  struct iwl_rx_packet *pkt = rxb_addr(rxb);
645  struct iwl_missed_beacon_notif *missed_beacon = (void *)pkt->data;
646 
647  if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
648  priv->missed_beacon_threshold) {
649  IWL_DEBUG_CALIB(priv,
650  "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
651  le32_to_cpu(missed_beacon->consecutive_missed_beacons),
652  le32_to_cpu(missed_beacon->total_missed_becons),
653  le32_to_cpu(missed_beacon->num_recvd_beacons),
654  le32_to_cpu(missed_beacon->num_expected_beacons));
655  if (!test_bit(STATUS_SCANNING, &priv->status))
656  iwl_init_sensitivity(priv);
657  }
658  return 0;
659 }
660 
661 /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
662  * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
663 static int iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
664  struct iwl_rx_cmd_buffer *rxb,
665  struct iwl_device_cmd *cmd)
666 {
667  struct iwl_rx_packet *pkt = rxb_addr(rxb);
668 
669  priv->last_phy_res_valid = true;
670  priv->ampdu_ref++;
671  memcpy(&priv->last_phy_res, pkt->data,
672  sizeof(struct iwl_rx_phy_res));
673  return 0;
674 }
675 
676 /*
677  * returns non-zero if packet should be dropped
678  */
679 static int iwlagn_set_decrypted_flag(struct iwl_priv *priv,
680  struct ieee80211_hdr *hdr,
681  u32 decrypt_res,
682  struct ieee80211_rx_status *stats)
683 {
685 
686  /*
687  * All contexts have the same setting here due to it being
688  * a module parameter, so OK to check any context.
689  */
690  if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
692  return 0;
693 
694  if (!(fc & IEEE80211_FCTL_PROTECTED))
695  return 0;
696 
697  IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
698  switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
700  /* The uCode has got a bad phase 1 Key, pushes the packet.
701  * Decryption will be done in SW. */
702  if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
704  break;
705 
707  if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
709  /* bad ICV, the packet is destroyed since the
710  * decryption is inplace, drop it */
711  IWL_DEBUG_RX(priv, "Packet destroyed\n");
712  return -1;
713  }
715  if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
717  IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
718  stats->flag |= RX_FLAG_DECRYPTED;
719  }
720  break;
721 
722  default:
723  break;
724  }
725  return 0;
726 }
727 
728 static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
729  struct ieee80211_hdr *hdr,
730  u16 len,
731  u32 ampdu_status,
732  struct iwl_rx_cmd_buffer *rxb,
733  struct ieee80211_rx_status *stats)
734 {
735  struct sk_buff *skb;
736  __le16 fc = hdr->frame_control;
737  struct iwl_rxon_context *ctx;
738  unsigned int hdrlen, fraglen;
739 
740  /* We only process data packets if the interface is open */
741  if (unlikely(!priv->is_open)) {
743  "Dropping packet while interface is not open.\n");
744  return;
745  }
746 
747  /* In case of HW accelerated crypto and bad decryption, drop */
748  if (!iwlwifi_mod_params.sw_crypto &&
749  iwlagn_set_decrypted_flag(priv, hdr, ampdu_status, stats))
750  return;
751 
752  /* Dont use dev_alloc_skb(), we'll have enough headroom once
753  * ieee80211_hdr pulled.
754  */
755  skb = alloc_skb(128, GFP_ATOMIC);
756  if (!skb) {
757  IWL_ERR(priv, "alloc_skb failed\n");
758  return;
759  }
760  /* If frame is small enough to fit in skb->head, pull it completely.
761  * If not, only pull ieee80211_hdr so that splice() or TCP coalesce
762  * are more efficient.
763  */
764  hdrlen = (len <= skb_tailroom(skb)) ? len : sizeof(*hdr);
765 
766  memcpy(skb_put(skb, hdrlen), hdr, hdrlen);
767  fraglen = len - hdrlen;
768 
769  if (fraglen) {
770  int offset = (void *)hdr + hdrlen -
771  rxb_addr(rxb) + rxb_offset(rxb);
772 
773  skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
774  fraglen, rxb->truesize);
775  }
776 
777  /*
778  * Wake any queues that were stopped due to a passive channel tx
779  * failure. This can happen because the regulatory enforcement in
780  * the device waits for a beacon before allowing transmission,
781  * sometimes even after already having transmitted frames for the
782  * association because the new RXON may reset the information.
783  */
784  if (unlikely(ieee80211_is_beacon(fc) && priv->passive_no_rx)) {
785  for_each_context(priv, ctx) {
786  if (!ether_addr_equal(hdr->addr3,
787  ctx->active.bssid_addr))
788  continue;
790  }
791  }
792 
793  memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
794 
795  ieee80211_rx(priv->hw, skb);
796 }
797 
798 static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
799 {
800  u32 decrypt_out = 0;
801 
802  if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
803  RX_RES_STATUS_STATION_FOUND)
804  decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
806 
807  decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
808 
809  /* packet was not encrypted */
810  if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
812  return decrypt_out;
813 
814  /* packet was encrypted with unknown alg */
815  if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
817  return decrypt_out;
818 
819  /* decryption was not done in HW */
820  if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
822  return decrypt_out;
823 
824  switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
825 
827  /* alg is CCM: check MIC only */
828  if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
829  /* Bad MIC */
830  decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
831  else
832  decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
833 
834  break;
835 
837  if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
838  /* Bad TTAK */
839  decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
840  break;
841  }
842  /* fall through if TTAK OK */
843  default:
844  if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
845  decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
846  else
847  decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
848  break;
849  }
850 
851  IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
852  decrypt_in, decrypt_out);
853 
854  return decrypt_out;
855 }
856 
857 /* Calc max signal level (dBm) among 3 possible receivers */
858 static int iwlagn_calc_rssi(struct iwl_priv *priv,
859  struct iwl_rx_phy_res *rx_resp)
860 {
861  /* data from PHY/DSP regarding signal strength, etc.,
862  * contents are always there, not configurable by host
863  */
864  struct iwlagn_non_cfg_phy *ncphy =
865  (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
866  u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
867  u8 agc;
868 
871 
872  /* Find max rssi among 3 possible receivers.
873  * These values are measured by the digital signal processor (DSP).
874  * They should stay fairly constant even as the signal strength varies,
875  * if the radio's automatic gain control (AGC) is working right.
876  * AGC value (see below) will provide the "interesting" info.
877  */
879  rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
881  rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
884  rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
886 
887  max_rssi = max_t(u32, rssi_a, rssi_b);
888  max_rssi = max_t(u32, max_rssi, rssi_c);
889 
890  IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
891  rssi_a, rssi_b, rssi_c, max_rssi, agc);
892 
893  /* dBm = max_rssi dB - agc dB - constant.
894  * Higher AGC (higher radio gain) means lower signal. */
895  return max_rssi - agc - IWLAGN_RSSI_OFFSET;
896 }
897 
898 /* Called for REPLY_RX_MPDU_CMD */
899 static int iwlagn_rx_reply_rx(struct iwl_priv *priv,
900  struct iwl_rx_cmd_buffer *rxb,
901  struct iwl_device_cmd *cmd)
902 {
903  struct ieee80211_hdr *header;
905  struct iwl_rx_packet *pkt = rxb_addr(rxb);
906  struct iwl_rx_phy_res *phy_res;
908  struct iwl_rx_mpdu_res_start *amsdu;
909  u32 len;
910  u32 ampdu_status;
911  u32 rate_n_flags;
912 
913  if (!priv->last_phy_res_valid) {
914  IWL_ERR(priv, "MPDU frame without cached PHY data\n");
915  return 0;
916  }
917  phy_res = &priv->last_phy_res;
918  amsdu = (struct iwl_rx_mpdu_res_start *)pkt->data;
919  header = (struct ieee80211_hdr *)(pkt->data + sizeof(*amsdu));
920  len = le16_to_cpu(amsdu->byte_count);
921  rx_pkt_status = *(__le32 *)(pkt->data + sizeof(*amsdu) + len);
922  ampdu_status = iwlagn_translate_rx_status(priv,
923  le32_to_cpu(rx_pkt_status));
924 
925  if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
926  IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d\n",
927  phy_res->cfg_phy_cnt);
928  return 0;
929  }
930 
931  if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
932  !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
933  IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
934  le32_to_cpu(rx_pkt_status));
935  return 0;
936  }
937 
938  /* This will be used in several places later */
939  rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
940 
941  /* rx_status carries information about the packet to mac80211 */
942  rx_status.mactime = le64_to_cpu(phy_res->timestamp);
943  rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
945  rx_status.freq =
947  rx_status.band);
948  rx_status.rate_idx =
949  iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
950  rx_status.flag = 0;
951 
952  /* TSF isn't reliable. In order to allow smooth user experience,
953  * this W/A doesn't propagate it to the mac80211 */
954  /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
955 
957 
958  /* Find max signal strength (dBm) among 3 antenna/receiver chains */
959  rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
960 
961  IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
962  rx_status.signal, (unsigned long long)rx_status.mactime);
963 
964  /*
965  * "antenna number"
966  *
967  * It seems that the antenna field in the phy flags value
968  * is actually a bit field. This is undefined by radiotap,
969  * it wants an actual antenna number but I always get "7"
970  * for most legacy frames I receive indicating that the
971  * same frame was received on all three RX chains.
972  *
973  * I think this field should be removed in favor of a
974  * new 802.11n radiotap field "RX chains" that is defined
975  * as a bitmask.
976  */
977  rx_status.antenna =
980 
981  /* set the preamble flag if appropriate */
983  rx_status.flag |= RX_FLAG_SHORTPRE;
984 
985  if (phy_res->phy_flags & RX_RES_PHY_FLAGS_AGG_MSK) {
986  /*
987  * We know which subframes of an A-MPDU belong
988  * together since we get a single PHY response
989  * from the firmware for all of them
990  */
992  rx_status.ampdu_reference = priv->ampdu_ref;
993  }
994 
995  /* Set up the HT phy flags */
996  if (rate_n_flags & RATE_MCS_HT_MSK)
997  rx_status.flag |= RX_FLAG_HT;
998  if (rate_n_flags & RATE_MCS_HT40_MSK)
999  rx_status.flag |= RX_FLAG_40MHZ;
1000  if (rate_n_flags & RATE_MCS_SGI_MSK)
1001  rx_status.flag |= RX_FLAG_SHORT_GI;
1002  if (rate_n_flags & RATE_MCS_GF_MSK)
1003  rx_status.flag |= RX_FLAG_HT_GF;
1004 
1005  iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
1006  rxb, &rx_status);
1007  return 0;
1008 }
1009 
1010 static int iwlagn_rx_noa_notification(struct iwl_priv *priv,
1011  struct iwl_rx_cmd_buffer *rxb,
1012  struct iwl_device_cmd *cmd)
1013 {
1014  struct iwl_wipan_noa_data *new_data, *old_data;
1015  struct iwl_rx_packet *pkt = rxb_addr(rxb);
1016  struct iwl_wipan_noa_notification *noa_notif = (void *)pkt->data;
1017 
1018  /* no condition -- we're in softirq */
1019  old_data = rcu_dereference_protected(priv->noa_data, true);
1020 
1021  if (noa_notif->noa_active) {
1022  u32 len = le16_to_cpu(noa_notif->noa_attribute.length);
1023  u32 copylen = len;
1024 
1025  /* EID, len, OUI, subtype */
1026  len += 1 + 1 + 3 + 1;
1027  /* P2P id, P2P length */
1028  len += 1 + 2;
1029  copylen += 1 + 2;
1030 
1031  new_data = kmalloc(sizeof(*new_data) + len, GFP_ATOMIC);
1032  if (new_data) {
1033  new_data->length = len;
1034  new_data->data[0] = WLAN_EID_VENDOR_SPECIFIC;
1035  new_data->data[1] = len - 2; /* not counting EID, len */
1036  new_data->data[2] = (WLAN_OUI_WFA >> 16) & 0xff;
1037  new_data->data[3] = (WLAN_OUI_WFA >> 8) & 0xff;
1038  new_data->data[4] = (WLAN_OUI_WFA >> 0) & 0xff;
1039  new_data->data[5] = WLAN_OUI_TYPE_WFA_P2P;
1040  memcpy(&new_data->data[6], &noa_notif->noa_attribute,
1041  copylen);
1042  }
1043  } else
1044  new_data = NULL;
1045 
1046  rcu_assign_pointer(priv->noa_data, new_data);
1047 
1048  if (old_data)
1049  kfree_rcu(old_data, rcu_head);
1050 
1051  return 0;
1052 }
1053 
1061 {
1062  int (**handlers)(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb,
1063  struct iwl_device_cmd *cmd);
1064 
1065  handlers = priv->rx_handlers;
1066 
1067  handlers[REPLY_ERROR] = iwlagn_rx_reply_error;
1068  handlers[CHANNEL_SWITCH_NOTIFICATION] = iwlagn_rx_csa;
1069  handlers[SPECTRUM_MEASURE_NOTIFICATION] =
1070  iwlagn_rx_spectrum_measure_notif;
1071  handlers[PM_SLEEP_NOTIFICATION] = iwlagn_rx_pm_sleep_notif;
1072  handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
1073  iwlagn_rx_pm_debug_statistics_notif;
1074  handlers[BEACON_NOTIFICATION] = iwlagn_rx_beacon_notif;
1075  handlers[REPLY_ADD_STA] = iwl_add_sta_callback;
1076 
1077  handlers[REPLY_WIPAN_NOA_NOTIFICATION] = iwlagn_rx_noa_notification;
1078 
1079  /*
1080  * The same handler is used for both the REPLY to a discrete
1081  * statistics request from the host as well as for the periodic
1082  * statistics notifications (after received beacons) from the uCode.
1083  */
1084  handlers[REPLY_STATISTICS_CMD] = iwlagn_rx_reply_statistics;
1085  handlers[STATISTICS_NOTIFICATION] = iwlagn_rx_statistics;
1086 
1088 
1089  handlers[CARD_STATE_NOTIFICATION] = iwlagn_rx_card_state_notif;
1090  handlers[MISSED_BEACONS_NOTIFICATION] =
1091  iwlagn_rx_missed_beacon_notif;
1092 
1093  /* Rx handlers */
1094  handlers[REPLY_RX_PHY_CMD] = iwlagn_rx_reply_rx_phy;
1095  handlers[REPLY_RX_MPDU_CMD] = iwlagn_rx_reply_rx;
1096 
1097  /* block ack */
1098  handlers[REPLY_COMPRESSED_BA] =
1100 
1102 
1103  /* set up notification wait support */
1105 
1106  /* Set up BT Rx handlers */
1107  if (priv->cfg->bt_params)
1109 }
1110 
1111 int iwl_rx_dispatch(struct iwl_op_mode *op_mode, struct iwl_rx_cmd_buffer *rxb,
1112  struct iwl_device_cmd *cmd)
1113 {
1114  struct iwl_rx_packet *pkt = rxb_addr(rxb);
1115  struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
1116  int err = 0;
1117 
1118  /*
1119  * Do the notification wait before RX handlers so
1120  * even if the RX handler consumes the RXB we have
1121  * access to it in the notification wait entry.
1122  */
1124 
1125 #ifdef CONFIG_IWLWIFI_DEVICE_TESTMODE
1126  /*
1127  * RX data may be forwarded to userspace in one
1128  * of two cases: the user owns the fw through testmode or when
1129  * the user requested to monitor the rx w/o affecting the regular flow.
1130  * In these cases the iwl_test object will handle forwarding the rx
1131  * data to user space.
1132  * Note that if the ownership flag != IWL_OWNERSHIP_TM the flow
1133  * continues.
1134  */
1135  iwl_test_rx(&priv->tst, rxb);
1136 #endif
1137 
1138  if (priv->ucode_owner != IWL_OWNERSHIP_TM) {
1139  /* Based on type of command response or notification,
1140  * handle those that need handling via function in
1141  * rx_handlers table. See iwl_setup_rx_handlers() */
1142  if (priv->rx_handlers[pkt->hdr.cmd]) {
1143  priv->rx_handlers_stats[pkt->hdr.cmd]++;
1144  err = priv->rx_handlers[pkt->hdr.cmd] (priv, rxb, cmd);
1145  } else {
1146  /* No handling needed */
1147  IWL_DEBUG_RX(priv, "No handler needed for %s, 0x%02x\n",
1148  iwl_dvm_get_cmd_string(pkt->hdr.cmd),
1149  pkt->hdr.cmd);
1150  }
1151  }
1152  return err;
1153 }