iwl-agn-rx.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 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 <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/etherdevice.h>
  30. #include <linux/slab.h>
  31. #include <linux/sched.h>
  32. #include <net/mac80211.h>
  33. #include <asm/unaligned.h>
  34. #include "iwl-eeprom.h"
  35. #include "iwl-dev.h"
  36. #include "iwl-core.h"
  37. #include "iwl-io.h"
  38. #include "iwl-agn-calib.h"
  39. #include "iwl-agn.h"
  40. #include "iwl-shared.h"
  41. const char *get_cmd_string(u8 cmd)
  42. {
  43. switch (cmd) {
  44. IWL_CMD(REPLY_ALIVE);
  45. IWL_CMD(REPLY_ERROR);
  46. IWL_CMD(REPLY_ECHO);
  47. IWL_CMD(REPLY_RXON);
  48. IWL_CMD(REPLY_RXON_ASSOC);
  49. IWL_CMD(REPLY_QOS_PARAM);
  50. IWL_CMD(REPLY_RXON_TIMING);
  51. IWL_CMD(REPLY_ADD_STA);
  52. IWL_CMD(REPLY_REMOVE_STA);
  53. IWL_CMD(REPLY_REMOVE_ALL_STA);
  54. IWL_CMD(REPLY_TXFIFO_FLUSH);
  55. IWL_CMD(REPLY_WEPKEY);
  56. IWL_CMD(REPLY_TX);
  57. IWL_CMD(REPLY_LEDS_CMD);
  58. IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
  59. IWL_CMD(COEX_PRIORITY_TABLE_CMD);
  60. IWL_CMD(COEX_MEDIUM_NOTIFICATION);
  61. IWL_CMD(COEX_EVENT_CMD);
  62. IWL_CMD(REPLY_QUIET_CMD);
  63. IWL_CMD(REPLY_CHANNEL_SWITCH);
  64. IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
  65. IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
  66. IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
  67. IWL_CMD(POWER_TABLE_CMD);
  68. IWL_CMD(PM_SLEEP_NOTIFICATION);
  69. IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
  70. IWL_CMD(REPLY_SCAN_CMD);
  71. IWL_CMD(REPLY_SCAN_ABORT_CMD);
  72. IWL_CMD(SCAN_START_NOTIFICATION);
  73. IWL_CMD(SCAN_RESULTS_NOTIFICATION);
  74. IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
  75. IWL_CMD(BEACON_NOTIFICATION);
  76. IWL_CMD(REPLY_TX_BEACON);
  77. IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
  78. IWL_CMD(QUIET_NOTIFICATION);
  79. IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
  80. IWL_CMD(MEASURE_ABORT_NOTIFICATION);
  81. IWL_CMD(REPLY_BT_CONFIG);
  82. IWL_CMD(REPLY_STATISTICS_CMD);
  83. IWL_CMD(STATISTICS_NOTIFICATION);
  84. IWL_CMD(REPLY_CARD_STATE_CMD);
  85. IWL_CMD(CARD_STATE_NOTIFICATION);
  86. IWL_CMD(MISSED_BEACONS_NOTIFICATION);
  87. IWL_CMD(REPLY_CT_KILL_CONFIG_CMD);
  88. IWL_CMD(SENSITIVITY_CMD);
  89. IWL_CMD(REPLY_PHY_CALIBRATION_CMD);
  90. IWL_CMD(REPLY_RX_PHY_CMD);
  91. IWL_CMD(REPLY_RX_MPDU_CMD);
  92. IWL_CMD(REPLY_RX);
  93. IWL_CMD(REPLY_COMPRESSED_BA);
  94. IWL_CMD(CALIBRATION_CFG_CMD);
  95. IWL_CMD(CALIBRATION_RES_NOTIFICATION);
  96. IWL_CMD(CALIBRATION_COMPLETE_NOTIFICATION);
  97. IWL_CMD(REPLY_TX_POWER_DBM_CMD);
  98. IWL_CMD(TEMPERATURE_NOTIFICATION);
  99. IWL_CMD(TX_ANT_CONFIGURATION_CMD);
  100. IWL_CMD(REPLY_BT_COEX_PROFILE_NOTIF);
  101. IWL_CMD(REPLY_BT_COEX_PRIO_TABLE);
  102. IWL_CMD(REPLY_BT_COEX_PROT_ENV);
  103. IWL_CMD(REPLY_WIPAN_PARAMS);
  104. IWL_CMD(REPLY_WIPAN_RXON);
  105. IWL_CMD(REPLY_WIPAN_RXON_TIMING);
  106. IWL_CMD(REPLY_WIPAN_RXON_ASSOC);
  107. IWL_CMD(REPLY_WIPAN_QOS_PARAM);
  108. IWL_CMD(REPLY_WIPAN_WEPKEY);
  109. IWL_CMD(REPLY_WIPAN_P2P_CHANNEL_SWITCH);
  110. IWL_CMD(REPLY_WIPAN_NOA_NOTIFICATION);
  111. IWL_CMD(REPLY_WIPAN_DEACTIVATION_COMPLETE);
  112. IWL_CMD(REPLY_WOWLAN_PATTERNS);
  113. IWL_CMD(REPLY_WOWLAN_WAKEUP_FILTER);
  114. IWL_CMD(REPLY_WOWLAN_TSC_RSC_PARAMS);
  115. IWL_CMD(REPLY_WOWLAN_TKIP_PARAMS);
  116. IWL_CMD(REPLY_WOWLAN_KEK_KCK_MATERIAL);
  117. IWL_CMD(REPLY_WOWLAN_GET_STATUS);
  118. default:
  119. return "UNKNOWN";
  120. }
  121. }
  122. /******************************************************************************
  123. *
  124. * Generic RX handler implementations
  125. *
  126. ******************************************************************************/
  127. static int iwlagn_rx_reply_error(struct iwl_priv *priv,
  128. struct iwl_rx_mem_buffer *rxb,
  129. struct iwl_device_cmd *cmd)
  130. {
  131. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  132. IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
  133. "seq 0x%04X ser 0x%08X\n",
  134. le32_to_cpu(pkt->u.err_resp.error_type),
  135. get_cmd_string(pkt->u.err_resp.cmd_id),
  136. pkt->u.err_resp.cmd_id,
  137. le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
  138. le32_to_cpu(pkt->u.err_resp.error_info));
  139. return 0;
  140. }
  141. static int iwlagn_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
  142. struct iwl_device_cmd *cmd)
  143. {
  144. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  145. struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
  146. /*
  147. * MULTI-FIXME
  148. * See iwlagn_mac_channel_switch.
  149. */
  150. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  151. struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
  152. if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status))
  153. return 0;
  154. if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
  155. rxon->channel = csa->channel;
  156. ctx->staging.channel = csa->channel;
  157. IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
  158. le16_to_cpu(csa->channel));
  159. iwl_chswitch_done(priv, true);
  160. } else {
  161. IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
  162. le16_to_cpu(csa->channel));
  163. iwl_chswitch_done(priv, false);
  164. }
  165. return 0;
  166. }
  167. static int iwlagn_rx_spectrum_measure_notif(struct iwl_priv *priv,
  168. struct iwl_rx_mem_buffer *rxb,
  169. struct iwl_device_cmd *cmd)
  170. {
  171. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  172. struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
  173. if (!report->state) {
  174. IWL_DEBUG_11H(priv,
  175. "Spectrum Measure Notification: Start\n");
  176. return 0;
  177. }
  178. memcpy(&priv->measure_report, report, sizeof(*report));
  179. priv->measurement_status |= MEASUREMENT_READY;
  180. return 0;
  181. }
  182. static int iwlagn_rx_pm_sleep_notif(struct iwl_priv *priv,
  183. struct iwl_rx_mem_buffer *rxb,
  184. struct iwl_device_cmd *cmd)
  185. {
  186. #ifdef CONFIG_IWLWIFI_DEBUG
  187. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  188. struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
  189. IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
  190. sleep->pm_sleep_mode, sleep->pm_wakeup_src);
  191. #endif
  192. return 0;
  193. }
  194. static int iwlagn_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
  195. struct iwl_rx_mem_buffer *rxb,
  196. struct iwl_device_cmd *cmd)
  197. {
  198. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  199. u32 __maybe_unused len =
  200. le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  201. IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
  202. "notification for %s:\n", len,
  203. get_cmd_string(pkt->hdr.cmd));
  204. iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
  205. return 0;
  206. }
  207. static int iwlagn_rx_beacon_notif(struct iwl_priv *priv,
  208. struct iwl_rx_mem_buffer *rxb,
  209. struct iwl_device_cmd *cmd)
  210. {
  211. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  212. struct iwlagn_beacon_notif *beacon = (void *)pkt->u.raw;
  213. #ifdef CONFIG_IWLWIFI_DEBUG
  214. u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
  215. u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
  216. IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
  217. "tsf:0x%.8x%.8x rate:%d\n",
  218. status & TX_STATUS_MSK,
  219. beacon->beacon_notify_hdr.failure_frame,
  220. le32_to_cpu(beacon->ibss_mgr_status),
  221. le32_to_cpu(beacon->high_tsf),
  222. le32_to_cpu(beacon->low_tsf), rate);
  223. #endif
  224. priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
  225. return 0;
  226. }
  227. /* the threshold ratio of actual_ack_cnt to expected_ack_cnt in percent */
  228. #define ACK_CNT_RATIO (50)
  229. #define BA_TIMEOUT_CNT (5)
  230. #define BA_TIMEOUT_MAX (16)
  231. /**
  232. * iwl_good_ack_health - checks for ACK count ratios, BA timeout retries.
  233. *
  234. * When the ACK count ratio is low and aggregated BA timeout retries exceeding
  235. * the BA_TIMEOUT_MAX, reload firmware and bring system back to normal
  236. * operation state.
  237. */
  238. static bool iwlagn_good_ack_health(struct iwl_priv *priv,
  239. struct statistics_tx *cur)
  240. {
  241. int actual_delta, expected_delta, ba_timeout_delta;
  242. struct statistics_tx *old;
  243. if (priv->agg_tids_count)
  244. return true;
  245. old = &priv->statistics.tx;
  246. actual_delta = le32_to_cpu(cur->actual_ack_cnt) -
  247. le32_to_cpu(old->actual_ack_cnt);
  248. expected_delta = le32_to_cpu(cur->expected_ack_cnt) -
  249. le32_to_cpu(old->expected_ack_cnt);
  250. /* Values should not be negative, but we do not trust the firmware */
  251. if (actual_delta <= 0 || expected_delta <= 0)
  252. return true;
  253. ba_timeout_delta = le32_to_cpu(cur->agg.ba_timeout) -
  254. le32_to_cpu(old->agg.ba_timeout);
  255. if ((actual_delta * 100 / expected_delta) < ACK_CNT_RATIO &&
  256. ba_timeout_delta > BA_TIMEOUT_CNT) {
  257. IWL_DEBUG_RADIO(priv,
  258. "deltas: actual %d expected %d ba_timeout %d\n",
  259. actual_delta, expected_delta, ba_timeout_delta);
  260. #ifdef CONFIG_IWLWIFI_DEBUGFS
  261. /*
  262. * This is ifdef'ed on DEBUGFS because otherwise the
  263. * statistics aren't available. If DEBUGFS is set but
  264. * DEBUG is not, these will just compile out.
  265. */
  266. IWL_DEBUG_RADIO(priv, "rx_detected_cnt delta %d\n",
  267. priv->delta_stats.tx.rx_detected_cnt);
  268. IWL_DEBUG_RADIO(priv,
  269. "ack_or_ba_timeout_collision delta %d\n",
  270. priv->delta_stats.tx.ack_or_ba_timeout_collision);
  271. #endif
  272. if (ba_timeout_delta >= BA_TIMEOUT_MAX)
  273. return false;
  274. }
  275. return true;
  276. }
  277. /**
  278. * iwl_good_plcp_health - checks for plcp error.
  279. *
  280. * When the plcp error is exceeding the thresholds, reset the radio
  281. * to improve the throughput.
  282. */
  283. static bool iwlagn_good_plcp_health(struct iwl_priv *priv,
  284. struct statistics_rx_phy *cur_ofdm,
  285. struct statistics_rx_ht_phy *cur_ofdm_ht,
  286. unsigned int msecs)
  287. {
  288. int delta;
  289. int threshold = priv->cfg->base_params->plcp_delta_threshold;
  290. if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
  291. IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
  292. return true;
  293. }
  294. delta = le32_to_cpu(cur_ofdm->plcp_err) -
  295. le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
  296. le32_to_cpu(cur_ofdm_ht->plcp_err) -
  297. le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
  298. /* Can be negative if firmware reset statistics */
  299. if (delta <= 0)
  300. return true;
  301. if ((delta * 100 / msecs) > threshold) {
  302. IWL_DEBUG_RADIO(priv,
  303. "plcp health threshold %u delta %d msecs %u\n",
  304. threshold, delta, msecs);
  305. return false;
  306. }
  307. return true;
  308. }
  309. static void iwlagn_recover_from_statistics(struct iwl_priv *priv,
  310. struct statistics_rx_phy *cur_ofdm,
  311. struct statistics_rx_ht_phy *cur_ofdm_ht,
  312. struct statistics_tx *tx,
  313. unsigned long stamp)
  314. {
  315. unsigned int msecs;
  316. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  317. return;
  318. msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
  319. /* Only gather statistics and update time stamp when not associated */
  320. if (!iwl_is_any_associated(priv))
  321. return;
  322. /* Do not check/recover when do not have enough statistics data */
  323. if (msecs < 99)
  324. return;
  325. if (iwlagn_mod_params.ack_check && !iwlagn_good_ack_health(priv, tx)) {
  326. IWL_ERR(priv, "low ack count detected, restart firmware\n");
  327. if (!iwl_force_reset(priv, IWL_FW_RESET, false))
  328. return;
  329. }
  330. if (iwlagn_mod_params.plcp_check &&
  331. !iwlagn_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
  332. iwl_force_reset(priv, IWL_RF_RESET, false);
  333. }
  334. /* Calculate noise level, based on measurements during network silence just
  335. * before arriving beacon. This measurement can be done only if we know
  336. * exactly when to expect beacons, therefore only when we're associated. */
  337. static void iwlagn_rx_calc_noise(struct iwl_priv *priv)
  338. {
  339. struct statistics_rx_non_phy *rx_info;
  340. int num_active_rx = 0;
  341. int total_silence = 0;
  342. int bcn_silence_a, bcn_silence_b, bcn_silence_c;
  343. int last_rx_noise;
  344. rx_info = &priv->statistics.rx_non_phy;
  345. bcn_silence_a =
  346. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  347. bcn_silence_b =
  348. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  349. bcn_silence_c =
  350. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  351. if (bcn_silence_a) {
  352. total_silence += bcn_silence_a;
  353. num_active_rx++;
  354. }
  355. if (bcn_silence_b) {
  356. total_silence += bcn_silence_b;
  357. num_active_rx++;
  358. }
  359. if (bcn_silence_c) {
  360. total_silence += bcn_silence_c;
  361. num_active_rx++;
  362. }
  363. /* Average among active antennas */
  364. if (num_active_rx)
  365. last_rx_noise = (total_silence / num_active_rx) - 107;
  366. else
  367. last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  368. IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
  369. bcn_silence_a, bcn_silence_b, bcn_silence_c,
  370. last_rx_noise);
  371. }
  372. #ifdef CONFIG_IWLWIFI_DEBUGFS
  373. /*
  374. * based on the assumption of all statistics counter are in DWORD
  375. * FIXME: This function is for debugging, do not deal with
  376. * the case of counters roll-over.
  377. */
  378. static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
  379. __le32 *max_delta, __le32 *accum, int size)
  380. {
  381. int i;
  382. for (i = 0;
  383. i < size / sizeof(__le32);
  384. i++, prev++, cur++, delta++, max_delta++, accum++) {
  385. if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
  386. *delta = cpu_to_le32(
  387. le32_to_cpu(*cur) - le32_to_cpu(*prev));
  388. le32_add_cpu(accum, le32_to_cpu(*delta));
  389. if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
  390. *max_delta = *delta;
  391. }
  392. }
  393. }
  394. static void
  395. iwlagn_accumulative_statistics(struct iwl_priv *priv,
  396. struct statistics_general_common *common,
  397. struct statistics_rx_non_phy *rx_non_phy,
  398. struct statistics_rx_phy *rx_ofdm,
  399. struct statistics_rx_ht_phy *rx_ofdm_ht,
  400. struct statistics_rx_phy *rx_cck,
  401. struct statistics_tx *tx,
  402. struct statistics_bt_activity *bt_activity)
  403. {
  404. #define ACCUM(_name) \
  405. accum_stats((__le32 *)&priv->statistics._name, \
  406. (__le32 *)_name, \
  407. (__le32 *)&priv->delta_stats._name, \
  408. (__le32 *)&priv->max_delta_stats._name, \
  409. (__le32 *)&priv->accum_stats._name, \
  410. sizeof(*_name));
  411. ACCUM(common);
  412. ACCUM(rx_non_phy);
  413. ACCUM(rx_ofdm);
  414. ACCUM(rx_ofdm_ht);
  415. ACCUM(rx_cck);
  416. ACCUM(tx);
  417. if (bt_activity)
  418. ACCUM(bt_activity);
  419. #undef ACCUM
  420. }
  421. #else
  422. static inline void
  423. iwlagn_accumulative_statistics(struct iwl_priv *priv,
  424. struct statistics_general_common *common,
  425. struct statistics_rx_non_phy *rx_non_phy,
  426. struct statistics_rx_phy *rx_ofdm,
  427. struct statistics_rx_ht_phy *rx_ofdm_ht,
  428. struct statistics_rx_phy *rx_cck,
  429. struct statistics_tx *tx,
  430. struct statistics_bt_activity *bt_activity)
  431. {
  432. }
  433. #endif
  434. static int iwlagn_rx_statistics(struct iwl_priv *priv,
  435. struct iwl_rx_mem_buffer *rxb,
  436. struct iwl_device_cmd *cmd)
  437. {
  438. unsigned long stamp = jiffies;
  439. const int reg_recalib_period = 60;
  440. int change;
  441. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  442. u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  443. __le32 *flag;
  444. 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. len -= sizeof(struct iwl_cmd_header); /* skip header */
  452. IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
  453. len);
  454. if (len == sizeof(struct iwl_bt_notif_statistics)) {
  455. struct iwl_bt_notif_statistics *stats;
  456. stats = &pkt->u.stats_bt;
  457. flag = &stats->flag;
  458. common = &stats->general.common;
  459. rx_non_phy = &stats->rx.general.common;
  460. rx_ofdm = &stats->rx.ofdm;
  461. rx_ofdm_ht = &stats->rx.ofdm_ht;
  462. rx_cck = &stats->rx.cck;
  463. tx = &stats->tx;
  464. bt_activity = &stats->general.activity;
  465. #ifdef CONFIG_IWLWIFI_DEBUGFS
  466. /* handle this exception directly */
  467. priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
  468. le32_add_cpu(&priv->statistics.accum_num_bt_kills,
  469. le32_to_cpu(stats->rx.general.num_bt_kills));
  470. #endif
  471. } else if (len == sizeof(struct iwl_notif_statistics)) {
  472. struct iwl_notif_statistics *stats;
  473. stats = &pkt->u.stats;
  474. flag = &stats->flag;
  475. common = &stats->general.common;
  476. rx_non_phy = &stats->rx.general;
  477. rx_ofdm = &stats->rx.ofdm;
  478. rx_ofdm_ht = &stats->rx.ofdm_ht;
  479. rx_cck = &stats->rx.cck;
  480. tx = &stats->tx;
  481. bt_activity = NULL;
  482. } else {
  483. WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
  484. len, sizeof(struct iwl_bt_notif_statistics),
  485. sizeof(struct iwl_notif_statistics));
  486. return 0;
  487. }
  488. change = common->temperature != priv->statistics.common.temperature ||
  489. (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
  490. (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
  491. iwlagn_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
  492. rx_ofdm_ht, rx_cck, tx, bt_activity);
  493. iwlagn_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
  494. priv->statistics.flag = *flag;
  495. memcpy(&priv->statistics.common, common, sizeof(*common));
  496. memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
  497. memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
  498. memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
  499. memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
  500. memcpy(&priv->statistics.tx, tx, sizeof(*tx));
  501. #ifdef CONFIG_IWLWIFI_DEBUGFS
  502. if (bt_activity)
  503. memcpy(&priv->statistics.bt_activity, bt_activity,
  504. sizeof(*bt_activity));
  505. #endif
  506. priv->rx_statistics_jiffies = stamp;
  507. set_bit(STATUS_STATISTICS, &priv->shrd->status);
  508. /* Reschedule the statistics timer to occur in
  509. * reg_recalib_period seconds to ensure we get a
  510. * thermal update even if the uCode doesn't give
  511. * us one */
  512. mod_timer(&priv->statistics_periodic, jiffies +
  513. msecs_to_jiffies(reg_recalib_period * 1000));
  514. if (unlikely(!test_bit(STATUS_SCANNING, &priv->shrd->status)) &&
  515. (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
  516. iwlagn_rx_calc_noise(priv);
  517. queue_work(priv->shrd->workqueue, &priv->run_time_calib_work);
  518. }
  519. if (priv->cfg->lib->temperature && change)
  520. priv->cfg->lib->temperature(priv);
  521. return 0;
  522. }
  523. static int iwlagn_rx_reply_statistics(struct iwl_priv *priv,
  524. struct iwl_rx_mem_buffer *rxb,
  525. struct iwl_device_cmd *cmd)
  526. {
  527. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  528. if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATISTICS_CLEAR_MSK) {
  529. #ifdef CONFIG_IWLWIFI_DEBUGFS
  530. memset(&priv->accum_stats, 0,
  531. sizeof(priv->accum_stats));
  532. memset(&priv->delta_stats, 0,
  533. sizeof(priv->delta_stats));
  534. memset(&priv->max_delta_stats, 0,
  535. sizeof(priv->max_delta_stats));
  536. #endif
  537. IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
  538. }
  539. iwlagn_rx_statistics(priv, rxb, cmd);
  540. return 0;
  541. }
  542. /* Handle notification from uCode that card's power state is changing
  543. * due to software, hardware, or critical temperature RFKILL */
  544. static int iwlagn_rx_card_state_notif(struct iwl_priv *priv,
  545. struct iwl_rx_mem_buffer *rxb,
  546. struct iwl_device_cmd *cmd)
  547. {
  548. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  549. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  550. unsigned long status = priv->shrd->status;
  551. IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
  552. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  553. (flags & SW_CARD_DISABLED) ? "Kill" : "On",
  554. (flags & CT_CARD_DISABLED) ?
  555. "Reached" : "Not reached");
  556. if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
  557. CT_CARD_DISABLED)) {
  558. iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_SET,
  559. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  560. iwl_write_direct32(bus(priv), HBUS_TARG_MBX_C,
  561. HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  562. if (!(flags & RXON_CARD_DISABLED)) {
  563. iwl_write32(bus(priv), CSR_UCODE_DRV_GP1_CLR,
  564. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  565. iwl_write_direct32(bus(priv), HBUS_TARG_MBX_C,
  566. HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  567. }
  568. if (flags & CT_CARD_DISABLED)
  569. iwl_tt_enter_ct_kill(priv);
  570. }
  571. if (!(flags & CT_CARD_DISABLED))
  572. iwl_tt_exit_ct_kill(priv);
  573. if (flags & HW_CARD_DISABLED)
  574. set_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
  575. else
  576. clear_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
  577. if (!(flags & RXON_CARD_DISABLED))
  578. iwl_scan_cancel(priv);
  579. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  580. test_bit(STATUS_RF_KILL_HW, &priv->shrd->status)))
  581. wiphy_rfkill_set_hw_state(priv->hw->wiphy,
  582. test_bit(STATUS_RF_KILL_HW, &priv->shrd->status));
  583. else
  584. wake_up(&priv->shrd->wait_command_queue);
  585. return 0;
  586. }
  587. static int iwlagn_rx_missed_beacon_notif(struct iwl_priv *priv,
  588. struct iwl_rx_mem_buffer *rxb,
  589. struct iwl_device_cmd *cmd)
  590. {
  591. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  592. struct iwl_missed_beacon_notif *missed_beacon;
  593. missed_beacon = &pkt->u.missed_beacon;
  594. if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
  595. priv->missed_beacon_threshold) {
  596. IWL_DEBUG_CALIB(priv,
  597. "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  598. le32_to_cpu(missed_beacon->consecutive_missed_beacons),
  599. le32_to_cpu(missed_beacon->total_missed_becons),
  600. le32_to_cpu(missed_beacon->num_recvd_beacons),
  601. le32_to_cpu(missed_beacon->num_expected_beacons));
  602. if (!test_bit(STATUS_SCANNING, &priv->shrd->status))
  603. iwl_init_sensitivity(priv);
  604. }
  605. return 0;
  606. }
  607. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  608. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  609. static int iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
  610. struct iwl_rx_mem_buffer *rxb,
  611. struct iwl_device_cmd *cmd)
  612. {
  613. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  614. priv->last_phy_res_valid = true;
  615. memcpy(&priv->last_phy_res, pkt->u.raw,
  616. sizeof(struct iwl_rx_phy_res));
  617. return 0;
  618. }
  619. /*
  620. * returns non-zero if packet should be dropped
  621. */
  622. static int iwlagn_set_decrypted_flag(struct iwl_priv *priv,
  623. struct ieee80211_hdr *hdr,
  624. u32 decrypt_res,
  625. struct ieee80211_rx_status *stats)
  626. {
  627. u16 fc = le16_to_cpu(hdr->frame_control);
  628. /*
  629. * All contexts have the same setting here due to it being
  630. * a module parameter, so OK to check any context.
  631. */
  632. if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
  633. RXON_FILTER_DIS_DECRYPT_MSK)
  634. return 0;
  635. if (!(fc & IEEE80211_FCTL_PROTECTED))
  636. return 0;
  637. IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
  638. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  639. case RX_RES_STATUS_SEC_TYPE_TKIP:
  640. /* The uCode has got a bad phase 1 Key, pushes the packet.
  641. * Decryption will be done in SW. */
  642. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  643. RX_RES_STATUS_BAD_KEY_TTAK)
  644. break;
  645. case RX_RES_STATUS_SEC_TYPE_WEP:
  646. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  647. RX_RES_STATUS_BAD_ICV_MIC) {
  648. /* bad ICV, the packet is destroyed since the
  649. * decryption is inplace, drop it */
  650. IWL_DEBUG_RX(priv, "Packet destroyed\n");
  651. return -1;
  652. }
  653. case RX_RES_STATUS_SEC_TYPE_CCMP:
  654. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  655. RX_RES_STATUS_DECRYPT_OK) {
  656. IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
  657. stats->flag |= RX_FLAG_DECRYPTED;
  658. }
  659. break;
  660. default:
  661. break;
  662. }
  663. return 0;
  664. }
  665. static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
  666. struct ieee80211_hdr *hdr,
  667. u16 len,
  668. u32 ampdu_status,
  669. struct iwl_rx_mem_buffer *rxb,
  670. struct ieee80211_rx_status *stats)
  671. {
  672. struct sk_buff *skb;
  673. __le16 fc = hdr->frame_control;
  674. struct iwl_rxon_context *ctx;
  675. /* We only process data packets if the interface is open */
  676. if (unlikely(!priv->is_open)) {
  677. IWL_DEBUG_DROP_LIMIT(priv,
  678. "Dropping packet while interface is not open.\n");
  679. return;
  680. }
  681. /* In case of HW accelerated crypto and bad decryption, drop */
  682. if (!iwlagn_mod_params.sw_crypto &&
  683. iwlagn_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  684. return;
  685. skb = dev_alloc_skb(128);
  686. if (!skb) {
  687. IWL_ERR(priv, "dev_alloc_skb failed\n");
  688. return;
  689. }
  690. skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len);
  691. iwl_update_stats(priv, false, fc, len);
  692. /*
  693. * Wake any queues that were stopped due to a passive channel tx
  694. * failure. This can happen because the regulatory enforcement in
  695. * the device waits for a beacon before allowing transmission,
  696. * sometimes even after already having transmitted frames for the
  697. * association because the new RXON may reset the information.
  698. */
  699. if (unlikely(ieee80211_is_beacon(fc))) {
  700. for_each_context(priv, ctx) {
  701. if (!ctx->last_tx_rejected)
  702. continue;
  703. if (compare_ether_addr(hdr->addr3,
  704. ctx->active.bssid_addr))
  705. continue;
  706. ctx->last_tx_rejected = false;
  707. iwl_trans_wake_any_queue(trans(priv), ctx->ctxid);
  708. }
  709. }
  710. memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
  711. ieee80211_rx(priv->hw, skb);
  712. rxb->page = NULL;
  713. }
  714. static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  715. {
  716. u32 decrypt_out = 0;
  717. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  718. RX_RES_STATUS_STATION_FOUND)
  719. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  720. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  721. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  722. /* packet was not encrypted */
  723. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  724. RX_RES_STATUS_SEC_TYPE_NONE)
  725. return decrypt_out;
  726. /* packet was encrypted with unknown alg */
  727. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  728. RX_RES_STATUS_SEC_TYPE_ERR)
  729. return decrypt_out;
  730. /* decryption was not done in HW */
  731. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  732. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  733. return decrypt_out;
  734. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  735. case RX_RES_STATUS_SEC_TYPE_CCMP:
  736. /* alg is CCM: check MIC only */
  737. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  738. /* Bad MIC */
  739. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  740. else
  741. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  742. break;
  743. case RX_RES_STATUS_SEC_TYPE_TKIP:
  744. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  745. /* Bad TTAK */
  746. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  747. break;
  748. }
  749. /* fall through if TTAK OK */
  750. default:
  751. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  752. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  753. else
  754. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  755. break;
  756. }
  757. IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
  758. decrypt_in, decrypt_out);
  759. return decrypt_out;
  760. }
  761. /* Calc max signal level (dBm) among 3 possible receivers */
  762. static int iwlagn_calc_rssi(struct iwl_priv *priv,
  763. struct iwl_rx_phy_res *rx_resp)
  764. {
  765. /* data from PHY/DSP regarding signal strength, etc.,
  766. * contents are always there, not configurable by host
  767. */
  768. struct iwlagn_non_cfg_phy *ncphy =
  769. (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
  770. u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
  771. u8 agc;
  772. val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
  773. agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
  774. /* Find max rssi among 3 possible receivers.
  775. * These values are measured by the digital signal processor (DSP).
  776. * They should stay fairly constant even as the signal strength varies,
  777. * if the radio's automatic gain control (AGC) is working right.
  778. * AGC value (see below) will provide the "interesting" info.
  779. */
  780. val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
  781. rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
  782. IWLAGN_OFDM_RSSI_A_BIT_POS;
  783. rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
  784. IWLAGN_OFDM_RSSI_B_BIT_POS;
  785. val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
  786. rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
  787. IWLAGN_OFDM_RSSI_C_BIT_POS;
  788. max_rssi = max_t(u32, rssi_a, rssi_b);
  789. max_rssi = max_t(u32, max_rssi, rssi_c);
  790. IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
  791. rssi_a, rssi_b, rssi_c, max_rssi, agc);
  792. /* dBm = max_rssi dB - agc dB - constant.
  793. * Higher AGC (higher radio gain) means lower signal. */
  794. return max_rssi - agc - IWLAGN_RSSI_OFFSET;
  795. }
  796. /* Called for REPLY_RX (legacy ABG frames), or
  797. * REPLY_RX_MPDU_CMD (HT high-throughput N frames). */
  798. static int iwlagn_rx_reply_rx(struct iwl_priv *priv,
  799. struct iwl_rx_mem_buffer *rxb,
  800. struct iwl_device_cmd *cmd)
  801. {
  802. struct ieee80211_hdr *header;
  803. struct ieee80211_rx_status rx_status;
  804. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  805. struct iwl_rx_phy_res *phy_res;
  806. __le32 rx_pkt_status;
  807. struct iwl_rx_mpdu_res_start *amsdu;
  808. u32 len;
  809. u32 ampdu_status;
  810. u32 rate_n_flags;
  811. /**
  812. * REPLY_RX and REPLY_RX_MPDU_CMD are handled differently.
  813. * REPLY_RX: physical layer info is in this buffer
  814. * REPLY_RX_MPDU_CMD: physical layer info was sent in separate
  815. * command and cached in priv->last_phy_res
  816. *
  817. * Here we set up local variables depending on which command is
  818. * received.
  819. */
  820. if (pkt->hdr.cmd == REPLY_RX) {
  821. phy_res = (struct iwl_rx_phy_res *)pkt->u.raw;
  822. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res)
  823. + phy_res->cfg_phy_cnt);
  824. len = le16_to_cpu(phy_res->byte_count);
  825. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*phy_res) +
  826. phy_res->cfg_phy_cnt + len);
  827. ampdu_status = le32_to_cpu(rx_pkt_status);
  828. } else {
  829. if (!priv->last_phy_res_valid) {
  830. IWL_ERR(priv, "MPDU frame without cached PHY data\n");
  831. return 0;
  832. }
  833. phy_res = &priv->last_phy_res;
  834. amsdu = (struct iwl_rx_mpdu_res_start *)pkt->u.raw;
  835. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
  836. len = le16_to_cpu(amsdu->byte_count);
  837. rx_pkt_status = *(__le32 *)(pkt->u.raw + sizeof(*amsdu) + len);
  838. ampdu_status = iwlagn_translate_rx_status(priv,
  839. le32_to_cpu(rx_pkt_status));
  840. }
  841. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  842. IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d/n",
  843. phy_res->cfg_phy_cnt);
  844. return 0;
  845. }
  846. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  847. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  848. IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
  849. le32_to_cpu(rx_pkt_status));
  850. return 0;
  851. }
  852. /* This will be used in several places later */
  853. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  854. /* rx_status carries information about the packet to mac80211 */
  855. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  856. rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  857. IEEE80211_BAND_2GHZ : IEEE80211_BAND_5GHZ;
  858. rx_status.freq =
  859. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
  860. rx_status.band);
  861. rx_status.rate_idx =
  862. iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  863. rx_status.flag = 0;
  864. /* TSF isn't reliable. In order to allow smooth user experience,
  865. * this W/A doesn't propagate it to the mac80211 */
  866. /*rx_status.flag |= RX_FLAG_MACTIME_MPDU;*/
  867. priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  868. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  869. rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
  870. iwl_dbg_log_rx_data_frame(priv, len, header);
  871. IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
  872. rx_status.signal, (unsigned long long)rx_status.mactime);
  873. /*
  874. * "antenna number"
  875. *
  876. * It seems that the antenna field in the phy flags value
  877. * is actually a bit field. This is undefined by radiotap,
  878. * it wants an actual antenna number but I always get "7"
  879. * for most legacy frames I receive indicating that the
  880. * same frame was received on all three RX chains.
  881. *
  882. * I think this field should be removed in favor of a
  883. * new 802.11n radiotap field "RX chains" that is defined
  884. * as a bitmask.
  885. */
  886. rx_status.antenna =
  887. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
  888. >> RX_RES_PHY_FLAGS_ANTENNA_POS;
  889. /* set the preamble flag if appropriate */
  890. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  891. rx_status.flag |= RX_FLAG_SHORTPRE;
  892. /* Set up the HT phy flags */
  893. if (rate_n_flags & RATE_MCS_HT_MSK)
  894. rx_status.flag |= RX_FLAG_HT;
  895. if (rate_n_flags & RATE_MCS_HT40_MSK)
  896. rx_status.flag |= RX_FLAG_40MHZ;
  897. if (rate_n_flags & RATE_MCS_SGI_MSK)
  898. rx_status.flag |= RX_FLAG_SHORT_GI;
  899. iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
  900. rxb, &rx_status);
  901. return 0;
  902. }
  903. /**
  904. * iwl_setup_rx_handlers - Initialize Rx handler callbacks
  905. *
  906. * Setup the RX handlers for each of the reply types sent from the uCode
  907. * to the host.
  908. */
  909. void iwl_setup_rx_handlers(struct iwl_priv *priv)
  910. {
  911. int (**handlers)(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
  912. struct iwl_device_cmd *cmd);
  913. handlers = priv->rx_handlers;
  914. handlers[REPLY_ERROR] = iwlagn_rx_reply_error;
  915. handlers[CHANNEL_SWITCH_NOTIFICATION] = iwlagn_rx_csa;
  916. handlers[SPECTRUM_MEASURE_NOTIFICATION] =
  917. iwlagn_rx_spectrum_measure_notif;
  918. handlers[PM_SLEEP_NOTIFICATION] = iwlagn_rx_pm_sleep_notif;
  919. handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  920. iwlagn_rx_pm_debug_statistics_notif;
  921. handlers[BEACON_NOTIFICATION] = iwlagn_rx_beacon_notif;
  922. handlers[REPLY_ADD_STA] = iwl_add_sta_callback;
  923. /*
  924. * The same handler is used for both the REPLY to a discrete
  925. * statistics request from the host as well as for the periodic
  926. * statistics notifications (after received beacons) from the uCode.
  927. */
  928. handlers[REPLY_STATISTICS_CMD] = iwlagn_rx_reply_statistics;
  929. handlers[STATISTICS_NOTIFICATION] = iwlagn_rx_statistics;
  930. iwl_setup_rx_scan_handlers(priv);
  931. handlers[CARD_STATE_NOTIFICATION] = iwlagn_rx_card_state_notif;
  932. handlers[MISSED_BEACONS_NOTIFICATION] =
  933. iwlagn_rx_missed_beacon_notif;
  934. /* Rx handlers */
  935. handlers[REPLY_RX_PHY_CMD] = iwlagn_rx_reply_rx_phy;
  936. handlers[REPLY_RX_MPDU_CMD] = iwlagn_rx_reply_rx;
  937. /* block ack */
  938. handlers[REPLY_COMPRESSED_BA] =
  939. iwlagn_rx_reply_compressed_ba;
  940. /* init calibration handlers */
  941. priv->rx_handlers[CALIBRATION_RES_NOTIFICATION] =
  942. iwlagn_rx_calib_result;
  943. priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
  944. /* set up notification wait support */
  945. spin_lock_init(&priv->notif_wait_lock);
  946. INIT_LIST_HEAD(&priv->notif_waits);
  947. init_waitqueue_head(&priv->notif_waitq);
  948. /* Set up BT Rx handlers */
  949. if (priv->cfg->lib->bt_rx_handler_setup)
  950. priv->cfg->lib->bt_rx_handler_setup(priv);
  951. }
  952. int iwl_rx_dispatch(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
  953. struct iwl_device_cmd *cmd)
  954. {
  955. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  956. int err = 0;
  957. /*
  958. * Do the notification wait before RX handlers so
  959. * even if the RX handler consumes the RXB we have
  960. * access to it in the notification wait entry.
  961. */
  962. if (!list_empty(&priv->notif_waits)) {
  963. struct iwl_notification_wait *w;
  964. spin_lock(&priv->notif_wait_lock);
  965. list_for_each_entry(w, &priv->notif_waits, list) {
  966. if (w->cmd != pkt->hdr.cmd)
  967. continue;
  968. IWL_DEBUG_RX(priv,
  969. "Notif: %s, 0x%02x - wake the callers up\n",
  970. get_cmd_string(pkt->hdr.cmd),
  971. pkt->hdr.cmd);
  972. w->triggered = true;
  973. if (w->fn)
  974. w->fn(priv, pkt, w->fn_data);
  975. }
  976. spin_unlock(&priv->notif_wait_lock);
  977. wake_up_all(&priv->notif_waitq);
  978. }
  979. if (priv->pre_rx_handler)
  980. priv->pre_rx_handler(priv, rxb);
  981. /* Based on type of command response or notification,
  982. * handle those that need handling via function in
  983. * rx_handlers table. See iwl_setup_rx_handlers() */
  984. if (priv->rx_handlers[pkt->hdr.cmd]) {
  985. priv->rx_handlers_stats[pkt->hdr.cmd]++;
  986. err = priv->rx_handlers[pkt->hdr.cmd] (priv, rxb, cmd);
  987. } else {
  988. /* No handling needed */
  989. IWL_DEBUG_RX(priv,
  990. "No handler needed for %s, 0x%02x\n",
  991. get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  992. }
  993. return err;
  994. }