iwl-agn-rx.c 36 KB

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