rx.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
  4. * Copyright(c) 2015 Intel Deutschland GmbH
  5. *
  6. * Portions of this file are derived from the ipw3945 project, as well
  7. * as portionhelp of the ieee80211 subsystem header files.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  21. *
  22. * The full GNU General Public License is included in this distribution in the
  23. * file called LICENSE.
  24. *
  25. * Contact Information:
  26. * Intel Linux Wireless <linuxwifi@intel.com>
  27. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  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-trans.h"
  36. #include "iwl-io.h"
  37. #include "dev.h"
  38. #include "calib.h"
  39. #include "agn.h"
  40. /******************************************************************************
  41. *
  42. * Generic RX handler implementations
  43. *
  44. ******************************************************************************/
  45. static void iwlagn_rx_reply_error(struct iwl_priv *priv,
  46. struct iwl_rx_cmd_buffer *rxb)
  47. {
  48. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  49. struct iwl_error_resp *err_resp = (void *)pkt->data;
  50. IWL_ERR(priv, "Error Reply type 0x%08X cmd REPLY_ERROR (0x%02X) "
  51. "seq 0x%04X ser 0x%08X\n",
  52. le32_to_cpu(err_resp->error_type),
  53. err_resp->cmd_id,
  54. le16_to_cpu(err_resp->bad_cmd_seq_num),
  55. le32_to_cpu(err_resp->error_info));
  56. }
  57. static void iwlagn_rx_csa(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb)
  58. {
  59. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  60. struct iwl_csa_notification *csa = (void *)pkt->data;
  61. /*
  62. * MULTI-FIXME
  63. * See iwlagn_mac_channel_switch.
  64. */
  65. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  66. struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
  67. if (!test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
  68. return;
  69. if (!le32_to_cpu(csa->status) && csa->channel == priv->switch_channel) {
  70. rxon->channel = csa->channel;
  71. ctx->staging.channel = csa->channel;
  72. IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
  73. le16_to_cpu(csa->channel));
  74. iwl_chswitch_done(priv, true);
  75. } else {
  76. IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
  77. le16_to_cpu(csa->channel));
  78. iwl_chswitch_done(priv, false);
  79. }
  80. }
  81. static void iwlagn_rx_spectrum_measure_notif(struct iwl_priv *priv,
  82. struct iwl_rx_cmd_buffer *rxb)
  83. {
  84. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  85. struct iwl_spectrum_notification *report = (void *)pkt->data;
  86. if (!report->state) {
  87. IWL_DEBUG_11H(priv,
  88. "Spectrum Measure Notification: Start\n");
  89. return;
  90. }
  91. memcpy(&priv->measure_report, report, sizeof(*report));
  92. priv->measurement_status |= MEASUREMENT_READY;
  93. }
  94. static void iwlagn_rx_pm_sleep_notif(struct iwl_priv *priv,
  95. struct iwl_rx_cmd_buffer *rxb)
  96. {
  97. #ifdef CONFIG_IWLWIFI_DEBUG
  98. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  99. struct iwl_sleep_notification *sleep = (void *)pkt->data;
  100. IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
  101. sleep->pm_sleep_mode, sleep->pm_wakeup_src);
  102. #endif
  103. }
  104. static void iwlagn_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
  105. struct iwl_rx_cmd_buffer *rxb)
  106. {
  107. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  108. u32 __maybe_unused len = iwl_rx_packet_len(pkt);
  109. IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
  110. "notification for PM_DEBUG_STATISTIC_NOTIFIC:\n", len);
  111. iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->data, len);
  112. }
  113. static void iwlagn_rx_beacon_notif(struct iwl_priv *priv,
  114. struct iwl_rx_cmd_buffer *rxb)
  115. {
  116. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  117. struct iwlagn_beacon_notif *beacon = (void *)pkt->data;
  118. #ifdef CONFIG_IWLWIFI_DEBUG
  119. u16 status = le16_to_cpu(beacon->beacon_notify_hdr.status.status);
  120. u8 rate = iwl_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
  121. IWL_DEBUG_RX(priv, "beacon status %#x, retries:%d ibssmgr:%d "
  122. "tsf:0x%.8x%.8x rate:%d\n",
  123. status & TX_STATUS_MSK,
  124. beacon->beacon_notify_hdr.failure_frame,
  125. le32_to_cpu(beacon->ibss_mgr_status),
  126. le32_to_cpu(beacon->high_tsf),
  127. le32_to_cpu(beacon->low_tsf), rate);
  128. #endif
  129. priv->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
  130. }
  131. /**
  132. * iwl_good_plcp_health - checks for plcp error.
  133. *
  134. * When the plcp error is exceeding the thresholds, reset the radio
  135. * to improve the throughput.
  136. */
  137. static bool iwlagn_good_plcp_health(struct iwl_priv *priv,
  138. struct statistics_rx_phy *cur_ofdm,
  139. struct statistics_rx_ht_phy *cur_ofdm_ht,
  140. unsigned int msecs)
  141. {
  142. int delta;
  143. int threshold = priv->plcp_delta_threshold;
  144. if (threshold == IWL_MAX_PLCP_ERR_THRESHOLD_DISABLE) {
  145. IWL_DEBUG_RADIO(priv, "plcp_err check disabled\n");
  146. return true;
  147. }
  148. delta = le32_to_cpu(cur_ofdm->plcp_err) -
  149. le32_to_cpu(priv->statistics.rx_ofdm.plcp_err) +
  150. le32_to_cpu(cur_ofdm_ht->plcp_err) -
  151. le32_to_cpu(priv->statistics.rx_ofdm_ht.plcp_err);
  152. /* Can be negative if firmware reset statistics */
  153. if (delta <= 0)
  154. return true;
  155. if ((delta * 100 / msecs) > threshold) {
  156. IWL_DEBUG_RADIO(priv,
  157. "plcp health threshold %u delta %d msecs %u\n",
  158. threshold, delta, msecs);
  159. return false;
  160. }
  161. return true;
  162. }
  163. int iwl_force_rf_reset(struct iwl_priv *priv, bool external)
  164. {
  165. struct iwl_rf_reset *rf_reset;
  166. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  167. return -EAGAIN;
  168. if (!iwl_is_any_associated(priv)) {
  169. IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
  170. return -ENOLINK;
  171. }
  172. rf_reset = &priv->rf_reset;
  173. rf_reset->reset_request_count++;
  174. if (!external && rf_reset->last_reset_jiffies &&
  175. time_after(rf_reset->last_reset_jiffies +
  176. IWL_DELAY_NEXT_FORCE_RF_RESET, jiffies)) {
  177. IWL_DEBUG_INFO(priv, "RF reset rejected\n");
  178. rf_reset->reset_reject_count++;
  179. return -EAGAIN;
  180. }
  181. rf_reset->reset_success_count++;
  182. rf_reset->last_reset_jiffies = jiffies;
  183. /*
  184. * There is no easy and better way to force reset the radio,
  185. * the only known method is switching channel which will force to
  186. * reset and tune the radio.
  187. * Use internal short scan (single channel) operation to should
  188. * achieve this objective.
  189. * Driver should reset the radio when number of consecutive missed
  190. * beacon, or any other uCode error condition detected.
  191. */
  192. IWL_DEBUG_INFO(priv, "perform radio reset.\n");
  193. iwl_internal_short_hw_scan(priv);
  194. return 0;
  195. }
  196. static void iwlagn_recover_from_statistics(struct iwl_priv *priv,
  197. struct statistics_rx_phy *cur_ofdm,
  198. struct statistics_rx_ht_phy *cur_ofdm_ht,
  199. struct statistics_tx *tx,
  200. unsigned long stamp)
  201. {
  202. unsigned int msecs;
  203. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  204. return;
  205. msecs = jiffies_to_msecs(stamp - priv->rx_statistics_jiffies);
  206. /* Only gather statistics and update time stamp when not associated */
  207. if (!iwl_is_any_associated(priv))
  208. return;
  209. /* Do not check/recover when do not have enough statistics data */
  210. if (msecs < 99)
  211. return;
  212. if (!iwlagn_good_plcp_health(priv, cur_ofdm, cur_ofdm_ht, msecs))
  213. iwl_force_rf_reset(priv, false);
  214. }
  215. /* Calculate noise level, based on measurements during network silence just
  216. * before arriving beacon. This measurement can be done only if we know
  217. * exactly when to expect beacons, therefore only when we're associated. */
  218. static void iwlagn_rx_calc_noise(struct iwl_priv *priv)
  219. {
  220. struct statistics_rx_non_phy *rx_info;
  221. int num_active_rx = 0;
  222. int total_silence = 0;
  223. int bcn_silence_a, bcn_silence_b, bcn_silence_c;
  224. int last_rx_noise;
  225. rx_info = &priv->statistics.rx_non_phy;
  226. bcn_silence_a =
  227. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  228. bcn_silence_b =
  229. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  230. bcn_silence_c =
  231. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  232. if (bcn_silence_a) {
  233. total_silence += bcn_silence_a;
  234. num_active_rx++;
  235. }
  236. if (bcn_silence_b) {
  237. total_silence += bcn_silence_b;
  238. num_active_rx++;
  239. }
  240. if (bcn_silence_c) {
  241. total_silence += bcn_silence_c;
  242. num_active_rx++;
  243. }
  244. /* Average among active antennas */
  245. if (num_active_rx)
  246. last_rx_noise = (total_silence / num_active_rx) - 107;
  247. else
  248. last_rx_noise = IWL_NOISE_MEAS_NOT_AVAILABLE;
  249. IWL_DEBUG_CALIB(priv, "inband silence a %u, b %u, c %u, dBm %d\n",
  250. bcn_silence_a, bcn_silence_b, bcn_silence_c,
  251. last_rx_noise);
  252. }
  253. #ifdef CONFIG_IWLWIFI_DEBUGFS
  254. /*
  255. * based on the assumption of all statistics counter are in DWORD
  256. * FIXME: This function is for debugging, do not deal with
  257. * the case of counters roll-over.
  258. */
  259. static void accum_stats(__le32 *prev, __le32 *cur, __le32 *delta,
  260. __le32 *max_delta, __le32 *accum, int size)
  261. {
  262. int i;
  263. for (i = 0;
  264. i < size / sizeof(__le32);
  265. i++, prev++, cur++, delta++, max_delta++, accum++) {
  266. if (le32_to_cpu(*cur) > le32_to_cpu(*prev)) {
  267. *delta = cpu_to_le32(
  268. le32_to_cpu(*cur) - le32_to_cpu(*prev));
  269. le32_add_cpu(accum, le32_to_cpu(*delta));
  270. if (le32_to_cpu(*delta) > le32_to_cpu(*max_delta))
  271. *max_delta = *delta;
  272. }
  273. }
  274. }
  275. static void
  276. iwlagn_accumulative_statistics(struct iwl_priv *priv,
  277. struct statistics_general_common *common,
  278. struct statistics_rx_non_phy *rx_non_phy,
  279. struct statistics_rx_phy *rx_ofdm,
  280. struct statistics_rx_ht_phy *rx_ofdm_ht,
  281. struct statistics_rx_phy *rx_cck,
  282. struct statistics_tx *tx,
  283. struct statistics_bt_activity *bt_activity)
  284. {
  285. #define ACCUM(_name) \
  286. accum_stats((__le32 *)&priv->statistics._name, \
  287. (__le32 *)_name, \
  288. (__le32 *)&priv->delta_stats._name, \
  289. (__le32 *)&priv->max_delta_stats._name, \
  290. (__le32 *)&priv->accum_stats._name, \
  291. sizeof(*_name));
  292. ACCUM(common);
  293. ACCUM(rx_non_phy);
  294. ACCUM(rx_ofdm);
  295. ACCUM(rx_ofdm_ht);
  296. ACCUM(rx_cck);
  297. ACCUM(tx);
  298. if (bt_activity)
  299. ACCUM(bt_activity);
  300. #undef ACCUM
  301. }
  302. #else
  303. static inline void
  304. iwlagn_accumulative_statistics(struct iwl_priv *priv,
  305. struct statistics_general_common *common,
  306. struct statistics_rx_non_phy *rx_non_phy,
  307. struct statistics_rx_phy *rx_ofdm,
  308. struct statistics_rx_ht_phy *rx_ofdm_ht,
  309. struct statistics_rx_phy *rx_cck,
  310. struct statistics_tx *tx,
  311. struct statistics_bt_activity *bt_activity)
  312. {
  313. }
  314. #endif
  315. static void iwlagn_rx_statistics(struct iwl_priv *priv,
  316. struct iwl_rx_cmd_buffer *rxb)
  317. {
  318. unsigned long stamp = jiffies;
  319. const int reg_recalib_period = 60;
  320. int change;
  321. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  322. u32 len = iwl_rx_packet_payload_len(pkt);
  323. __le32 *flag;
  324. struct statistics_general_common *common;
  325. struct statistics_rx_non_phy *rx_non_phy;
  326. struct statistics_rx_phy *rx_ofdm;
  327. struct statistics_rx_ht_phy *rx_ofdm_ht;
  328. struct statistics_rx_phy *rx_cck;
  329. struct statistics_tx *tx;
  330. struct statistics_bt_activity *bt_activity;
  331. IWL_DEBUG_RX(priv, "Statistics notification received (%d bytes).\n",
  332. len);
  333. spin_lock(&priv->statistics.lock);
  334. if (len == sizeof(struct iwl_bt_notif_statistics)) {
  335. struct iwl_bt_notif_statistics *stats;
  336. stats = (void *)&pkt->data;
  337. flag = &stats->flag;
  338. common = &stats->general.common;
  339. rx_non_phy = &stats->rx.general.common;
  340. rx_ofdm = &stats->rx.ofdm;
  341. rx_ofdm_ht = &stats->rx.ofdm_ht;
  342. rx_cck = &stats->rx.cck;
  343. tx = &stats->tx;
  344. bt_activity = &stats->general.activity;
  345. #ifdef CONFIG_IWLWIFI_DEBUGFS
  346. /* handle this exception directly */
  347. priv->statistics.num_bt_kills = stats->rx.general.num_bt_kills;
  348. le32_add_cpu(&priv->statistics.accum_num_bt_kills,
  349. le32_to_cpu(stats->rx.general.num_bt_kills));
  350. #endif
  351. } else if (len == sizeof(struct iwl_notif_statistics)) {
  352. struct iwl_notif_statistics *stats;
  353. stats = (void *)&pkt->data;
  354. flag = &stats->flag;
  355. common = &stats->general.common;
  356. rx_non_phy = &stats->rx.general;
  357. rx_ofdm = &stats->rx.ofdm;
  358. rx_ofdm_ht = &stats->rx.ofdm_ht;
  359. rx_cck = &stats->rx.cck;
  360. tx = &stats->tx;
  361. bt_activity = NULL;
  362. } else {
  363. WARN_ONCE(1, "len %d doesn't match BT (%zu) or normal (%zu)\n",
  364. len, sizeof(struct iwl_bt_notif_statistics),
  365. sizeof(struct iwl_notif_statistics));
  366. spin_unlock(&priv->statistics.lock);
  367. return;
  368. }
  369. change = common->temperature != priv->statistics.common.temperature ||
  370. (*flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK) !=
  371. (priv->statistics.flag & STATISTICS_REPLY_FLG_HT40_MODE_MSK);
  372. iwlagn_accumulative_statistics(priv, common, rx_non_phy, rx_ofdm,
  373. rx_ofdm_ht, rx_cck, tx, bt_activity);
  374. iwlagn_recover_from_statistics(priv, rx_ofdm, rx_ofdm_ht, tx, stamp);
  375. priv->statistics.flag = *flag;
  376. memcpy(&priv->statistics.common, common, sizeof(*common));
  377. memcpy(&priv->statistics.rx_non_phy, rx_non_phy, sizeof(*rx_non_phy));
  378. memcpy(&priv->statistics.rx_ofdm, rx_ofdm, sizeof(*rx_ofdm));
  379. memcpy(&priv->statistics.rx_ofdm_ht, rx_ofdm_ht, sizeof(*rx_ofdm_ht));
  380. memcpy(&priv->statistics.rx_cck, rx_cck, sizeof(*rx_cck));
  381. memcpy(&priv->statistics.tx, tx, sizeof(*tx));
  382. #ifdef CONFIG_IWLWIFI_DEBUGFS
  383. if (bt_activity)
  384. memcpy(&priv->statistics.bt_activity, bt_activity,
  385. sizeof(*bt_activity));
  386. #endif
  387. priv->rx_statistics_jiffies = stamp;
  388. set_bit(STATUS_STATISTICS, &priv->status);
  389. /* Reschedule the statistics timer to occur in
  390. * reg_recalib_period seconds to ensure we get a
  391. * thermal update even if the uCode doesn't give
  392. * us one */
  393. mod_timer(&priv->statistics_periodic, jiffies +
  394. msecs_to_jiffies(reg_recalib_period * 1000));
  395. if (unlikely(!test_bit(STATUS_SCANNING, &priv->status)) &&
  396. (pkt->hdr.cmd == STATISTICS_NOTIFICATION)) {
  397. iwlagn_rx_calc_noise(priv);
  398. queue_work(priv->workqueue, &priv->run_time_calib_work);
  399. }
  400. if (priv->lib->temperature && change)
  401. priv->lib->temperature(priv);
  402. spin_unlock(&priv->statistics.lock);
  403. }
  404. static void iwlagn_rx_reply_statistics(struct iwl_priv *priv,
  405. struct iwl_rx_cmd_buffer *rxb)
  406. {
  407. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  408. struct iwl_notif_statistics *stats = (void *)pkt->data;
  409. if (le32_to_cpu(stats->flag) & UCODE_STATISTICS_CLEAR_MSK) {
  410. #ifdef CONFIG_IWLWIFI_DEBUGFS
  411. memset(&priv->accum_stats, 0,
  412. sizeof(priv->accum_stats));
  413. memset(&priv->delta_stats, 0,
  414. sizeof(priv->delta_stats));
  415. memset(&priv->max_delta_stats, 0,
  416. sizeof(priv->max_delta_stats));
  417. #endif
  418. IWL_DEBUG_RX(priv, "Statistics have been cleared\n");
  419. }
  420. iwlagn_rx_statistics(priv, rxb);
  421. }
  422. /* Handle notification from uCode that card's power state is changing
  423. * due to software, hardware, or critical temperature RFKILL */
  424. static void iwlagn_rx_card_state_notif(struct iwl_priv *priv,
  425. struct iwl_rx_cmd_buffer *rxb)
  426. {
  427. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  428. struct iwl_card_state_notif *card_state_notif = (void *)pkt->data;
  429. u32 flags = le32_to_cpu(card_state_notif->flags);
  430. unsigned long status = priv->status;
  431. IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s CT:%s\n",
  432. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  433. (flags & SW_CARD_DISABLED) ? "Kill" : "On",
  434. (flags & CT_CARD_DISABLED) ?
  435. "Reached" : "Not reached");
  436. if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED |
  437. CT_CARD_DISABLED)) {
  438. iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_SET,
  439. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  440. iwl_write_direct32(priv->trans, HBUS_TARG_MBX_C,
  441. HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  442. if (!(flags & RXON_CARD_DISABLED)) {
  443. iwl_write32(priv->trans, CSR_UCODE_DRV_GP1_CLR,
  444. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  445. iwl_write_direct32(priv->trans, HBUS_TARG_MBX_C,
  446. HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  447. }
  448. if (flags & CT_CARD_DISABLED)
  449. iwl_tt_enter_ct_kill(priv);
  450. }
  451. if (!(flags & CT_CARD_DISABLED))
  452. iwl_tt_exit_ct_kill(priv);
  453. if (flags & HW_CARD_DISABLED)
  454. set_bit(STATUS_RF_KILL_HW, &priv->status);
  455. else
  456. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  457. if (!(flags & RXON_CARD_DISABLED))
  458. iwl_scan_cancel(priv);
  459. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  460. test_bit(STATUS_RF_KILL_HW, &priv->status)))
  461. wiphy_rfkill_set_hw_state(priv->hw->wiphy,
  462. test_bit(STATUS_RF_KILL_HW, &priv->status));
  463. }
  464. static void iwlagn_rx_missed_beacon_notif(struct iwl_priv *priv,
  465. struct iwl_rx_cmd_buffer *rxb)
  466. {
  467. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  468. struct iwl_missed_beacon_notif *missed_beacon = (void *)pkt->data;
  469. if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
  470. priv->missed_beacon_threshold) {
  471. IWL_DEBUG_CALIB(priv,
  472. "missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  473. le32_to_cpu(missed_beacon->consecutive_missed_beacons),
  474. le32_to_cpu(missed_beacon->total_missed_becons),
  475. le32_to_cpu(missed_beacon->num_recvd_beacons),
  476. le32_to_cpu(missed_beacon->num_expected_beacons));
  477. if (!test_bit(STATUS_SCANNING, &priv->status))
  478. iwl_init_sensitivity(priv);
  479. }
  480. }
  481. /* Cache phy data (Rx signal strength, etc) for HT frame (REPLY_RX_PHY_CMD).
  482. * This will be used later in iwl_rx_reply_rx() for REPLY_RX_MPDU_CMD. */
  483. static void iwlagn_rx_reply_rx_phy(struct iwl_priv *priv,
  484. struct iwl_rx_cmd_buffer *rxb)
  485. {
  486. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  487. priv->last_phy_res_valid = true;
  488. priv->ampdu_ref++;
  489. memcpy(&priv->last_phy_res, pkt->data,
  490. sizeof(struct iwl_rx_phy_res));
  491. }
  492. /*
  493. * returns non-zero if packet should be dropped
  494. */
  495. static int iwlagn_set_decrypted_flag(struct iwl_priv *priv,
  496. struct ieee80211_hdr *hdr,
  497. u32 decrypt_res,
  498. struct ieee80211_rx_status *stats)
  499. {
  500. u16 fc = le16_to_cpu(hdr->frame_control);
  501. /*
  502. * All contexts have the same setting here due to it being
  503. * a module parameter, so OK to check any context.
  504. */
  505. if (priv->contexts[IWL_RXON_CTX_BSS].active.filter_flags &
  506. RXON_FILTER_DIS_DECRYPT_MSK)
  507. return 0;
  508. if (!(fc & IEEE80211_FCTL_PROTECTED))
  509. return 0;
  510. IWL_DEBUG_RX(priv, "decrypt_res:0x%x\n", decrypt_res);
  511. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  512. case RX_RES_STATUS_SEC_TYPE_TKIP:
  513. /* The uCode has got a bad phase 1 Key, pushes the packet.
  514. * Decryption will be done in SW. */
  515. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  516. RX_RES_STATUS_BAD_KEY_TTAK)
  517. break;
  518. case RX_RES_STATUS_SEC_TYPE_WEP:
  519. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  520. RX_RES_STATUS_BAD_ICV_MIC) {
  521. /* bad ICV, the packet is destroyed since the
  522. * decryption is inplace, drop it */
  523. IWL_DEBUG_RX(priv, "Packet destroyed\n");
  524. return -1;
  525. }
  526. case RX_RES_STATUS_SEC_TYPE_CCMP:
  527. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  528. RX_RES_STATUS_DECRYPT_OK) {
  529. IWL_DEBUG_RX(priv, "hw decrypt successfully!!!\n");
  530. stats->flag |= RX_FLAG_DECRYPTED;
  531. }
  532. break;
  533. default:
  534. break;
  535. }
  536. return 0;
  537. }
  538. static void iwlagn_pass_packet_to_mac80211(struct iwl_priv *priv,
  539. struct ieee80211_hdr *hdr,
  540. u16 len,
  541. u32 ampdu_status,
  542. struct iwl_rx_cmd_buffer *rxb,
  543. struct ieee80211_rx_status *stats)
  544. {
  545. struct sk_buff *skb;
  546. __le16 fc = hdr->frame_control;
  547. struct iwl_rxon_context *ctx;
  548. unsigned int hdrlen, fraglen;
  549. /* We only process data packets if the interface is open */
  550. if (unlikely(!priv->is_open)) {
  551. IWL_DEBUG_DROP_LIMIT(priv,
  552. "Dropping packet while interface is not open.\n");
  553. return;
  554. }
  555. /* In case of HW accelerated crypto and bad decryption, drop */
  556. if (!iwlwifi_mod_params.swcrypto &&
  557. iwlagn_set_decrypted_flag(priv, hdr, ampdu_status, stats))
  558. return;
  559. /* Dont use dev_alloc_skb(), we'll have enough headroom once
  560. * ieee80211_hdr pulled.
  561. */
  562. skb = alloc_skb(128, GFP_ATOMIC);
  563. if (!skb) {
  564. IWL_ERR(priv, "alloc_skb failed\n");
  565. return;
  566. }
  567. /* If frame is small enough to fit in skb->head, pull it completely.
  568. * If not, only pull ieee80211_hdr so that splice() or TCP coalesce
  569. * are more efficient.
  570. */
  571. hdrlen = (len <= skb_tailroom(skb)) ? len : sizeof(*hdr);
  572. skb_put_data(skb, hdr, hdrlen);
  573. fraglen = len - hdrlen;
  574. if (fraglen) {
  575. int offset = (void *)hdr + hdrlen -
  576. rxb_addr(rxb) + rxb_offset(rxb);
  577. skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset,
  578. fraglen, rxb->truesize);
  579. }
  580. /*
  581. * Wake any queues that were stopped due to a passive channel tx
  582. * failure. This can happen because the regulatory enforcement in
  583. * the device waits for a beacon before allowing transmission,
  584. * sometimes even after already having transmitted frames for the
  585. * association because the new RXON may reset the information.
  586. */
  587. if (unlikely(ieee80211_is_beacon(fc) && priv->passive_no_rx)) {
  588. for_each_context(priv, ctx) {
  589. if (!ether_addr_equal(hdr->addr3,
  590. ctx->active.bssid_addr))
  591. continue;
  592. iwlagn_lift_passive_no_rx(priv);
  593. }
  594. }
  595. memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
  596. ieee80211_rx_napi(priv->hw, NULL, skb, priv->napi);
  597. }
  598. static u32 iwlagn_translate_rx_status(struct iwl_priv *priv, u32 decrypt_in)
  599. {
  600. u32 decrypt_out = 0;
  601. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  602. RX_RES_STATUS_STATION_FOUND)
  603. decrypt_out |= (RX_RES_STATUS_STATION_FOUND |
  604. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  605. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  606. /* packet was not encrypted */
  607. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  608. RX_RES_STATUS_SEC_TYPE_NONE)
  609. return decrypt_out;
  610. /* packet was encrypted with unknown alg */
  611. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  612. RX_RES_STATUS_SEC_TYPE_ERR)
  613. return decrypt_out;
  614. /* decryption was not done in HW */
  615. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  616. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  617. return decrypt_out;
  618. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  619. case RX_RES_STATUS_SEC_TYPE_CCMP:
  620. /* alg is CCM: check MIC only */
  621. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  622. /* Bad MIC */
  623. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  624. else
  625. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  626. break;
  627. case RX_RES_STATUS_SEC_TYPE_TKIP:
  628. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  629. /* Bad TTAK */
  630. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  631. break;
  632. }
  633. /* fall through if TTAK OK */
  634. default:
  635. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  636. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  637. else
  638. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  639. break;
  640. }
  641. IWL_DEBUG_RX(priv, "decrypt_in:0x%x decrypt_out = 0x%x\n",
  642. decrypt_in, decrypt_out);
  643. return decrypt_out;
  644. }
  645. /* Calc max signal level (dBm) among 3 possible receivers */
  646. static int iwlagn_calc_rssi(struct iwl_priv *priv,
  647. struct iwl_rx_phy_res *rx_resp)
  648. {
  649. /* data from PHY/DSP regarding signal strength, etc.,
  650. * contents are always there, not configurable by host
  651. */
  652. struct iwlagn_non_cfg_phy *ncphy =
  653. (struct iwlagn_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
  654. u32 val, rssi_a, rssi_b, rssi_c, max_rssi;
  655. u8 agc;
  656. val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_AGC_IDX]);
  657. agc = (val & IWLAGN_OFDM_AGC_MSK) >> IWLAGN_OFDM_AGC_BIT_POS;
  658. /* Find max rssi among 3 possible receivers.
  659. * These values are measured by the digital signal processor (DSP).
  660. * They should stay fairly constant even as the signal strength varies,
  661. * if the radio's automatic gain control (AGC) is working right.
  662. * AGC value (see below) will provide the "interesting" info.
  663. */
  664. val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_AB_IDX]);
  665. rssi_a = (val & IWLAGN_OFDM_RSSI_INBAND_A_BITMSK) >>
  666. IWLAGN_OFDM_RSSI_A_BIT_POS;
  667. rssi_b = (val & IWLAGN_OFDM_RSSI_INBAND_B_BITMSK) >>
  668. IWLAGN_OFDM_RSSI_B_BIT_POS;
  669. val = le32_to_cpu(ncphy->non_cfg_phy[IWLAGN_RX_RES_RSSI_C_IDX]);
  670. rssi_c = (val & IWLAGN_OFDM_RSSI_INBAND_C_BITMSK) >>
  671. IWLAGN_OFDM_RSSI_C_BIT_POS;
  672. max_rssi = max_t(u32, rssi_a, rssi_b);
  673. max_rssi = max_t(u32, max_rssi, rssi_c);
  674. IWL_DEBUG_STATS(priv, "Rssi In A %d B %d C %d Max %d AGC dB %d\n",
  675. rssi_a, rssi_b, rssi_c, max_rssi, agc);
  676. /* dBm = max_rssi dB - agc dB - constant.
  677. * Higher AGC (higher radio gain) means lower signal. */
  678. return max_rssi - agc - IWLAGN_RSSI_OFFSET;
  679. }
  680. /* Called for REPLY_RX_MPDU_CMD */
  681. static void iwlagn_rx_reply_rx(struct iwl_priv *priv,
  682. struct iwl_rx_cmd_buffer *rxb)
  683. {
  684. struct ieee80211_hdr *header;
  685. struct ieee80211_rx_status rx_status = {};
  686. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  687. struct iwl_rx_phy_res *phy_res;
  688. __le32 rx_pkt_status;
  689. struct iwl_rx_mpdu_res_start *amsdu;
  690. u32 len;
  691. u32 ampdu_status;
  692. u32 rate_n_flags;
  693. if (!priv->last_phy_res_valid) {
  694. IWL_ERR(priv, "MPDU frame without cached PHY data\n");
  695. return;
  696. }
  697. phy_res = &priv->last_phy_res;
  698. amsdu = (struct iwl_rx_mpdu_res_start *)pkt->data;
  699. header = (struct ieee80211_hdr *)(pkt->data + sizeof(*amsdu));
  700. len = le16_to_cpu(amsdu->byte_count);
  701. rx_pkt_status = *(__le32 *)(pkt->data + sizeof(*amsdu) + len);
  702. ampdu_status = iwlagn_translate_rx_status(priv,
  703. le32_to_cpu(rx_pkt_status));
  704. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  705. IWL_DEBUG_DROP(priv, "dsp size out of range [0,20]: %d\n",
  706. phy_res->cfg_phy_cnt);
  707. return;
  708. }
  709. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  710. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  711. IWL_DEBUG_RX(priv, "Bad CRC or FIFO: 0x%08X.\n",
  712. le32_to_cpu(rx_pkt_status));
  713. return;
  714. }
  715. /* This will be used in several places later */
  716. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  717. /* rx_status carries information about the packet to mac80211 */
  718. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  719. rx_status.band = (phy_res->phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ?
  720. NL80211_BAND_2GHZ : NL80211_BAND_5GHZ;
  721. rx_status.freq =
  722. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
  723. rx_status.band);
  724. rx_status.rate_idx =
  725. iwlagn_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  726. rx_status.flag = 0;
  727. /* TSF isn't reliable. In order to allow smooth user experience,
  728. * this W/A doesn't propagate it to the mac80211 */
  729. /*rx_status.flag |= RX_FLAG_MACTIME_START;*/
  730. priv->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  731. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  732. rx_status.signal = iwlagn_calc_rssi(priv, phy_res);
  733. IWL_DEBUG_STATS_LIMIT(priv, "Rssi %d, TSF %llu\n",
  734. rx_status.signal, (unsigned long long)rx_status.mactime);
  735. /*
  736. * "antenna number"
  737. *
  738. * It seems that the antenna field in the phy flags value
  739. * is actually a bit field. This is undefined by radiotap,
  740. * it wants an actual antenna number but I always get "7"
  741. * for most legacy frames I receive indicating that the
  742. * same frame was received on all three RX chains.
  743. *
  744. * I think this field should be removed in favor of a
  745. * new 802.11n radiotap field "RX chains" that is defined
  746. * as a bitmask.
  747. */
  748. rx_status.antenna =
  749. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK)
  750. >> RX_RES_PHY_FLAGS_ANTENNA_POS;
  751. /* set the preamble flag if appropriate */
  752. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  753. rx_status.enc_flags |= RX_ENC_FLAG_SHORTPRE;
  754. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_AGG_MSK) {
  755. /*
  756. * We know which subframes of an A-MPDU belong
  757. * together since we get a single PHY response
  758. * from the firmware for all of them
  759. */
  760. rx_status.flag |= RX_FLAG_AMPDU_DETAILS;
  761. rx_status.ampdu_reference = priv->ampdu_ref;
  762. }
  763. /* Set up the HT phy flags */
  764. if (rate_n_flags & RATE_MCS_HT_MSK)
  765. rx_status.encoding = RX_ENC_HT;
  766. if (rate_n_flags & RATE_MCS_HT40_MSK)
  767. rx_status.bw = RATE_INFO_BW_40;
  768. else
  769. rx_status.bw = RATE_INFO_BW_20;
  770. if (rate_n_flags & RATE_MCS_SGI_MSK)
  771. rx_status.enc_flags |= RX_ENC_FLAG_SHORT_GI;
  772. if (rate_n_flags & RATE_MCS_GF_MSK)
  773. rx_status.enc_flags |= RX_ENC_FLAG_HT_GF;
  774. iwlagn_pass_packet_to_mac80211(priv, header, len, ampdu_status,
  775. rxb, &rx_status);
  776. }
  777. static void iwlagn_rx_noa_notification(struct iwl_priv *priv,
  778. struct iwl_rx_cmd_buffer *rxb)
  779. {
  780. struct iwl_wipan_noa_data *new_data, *old_data;
  781. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  782. struct iwl_wipan_noa_notification *noa_notif = (void *)pkt->data;
  783. /* no condition -- we're in softirq */
  784. old_data = rcu_dereference_protected(priv->noa_data, true);
  785. if (noa_notif->noa_active) {
  786. u32 len = le16_to_cpu(noa_notif->noa_attribute.length);
  787. u32 copylen = len;
  788. /* EID, len, OUI, subtype */
  789. len += 1 + 1 + 3 + 1;
  790. /* P2P id, P2P length */
  791. len += 1 + 2;
  792. copylen += 1 + 2;
  793. new_data = kmalloc(sizeof(*new_data) + len, GFP_ATOMIC);
  794. if (new_data) {
  795. new_data->length = len;
  796. new_data->data[0] = WLAN_EID_VENDOR_SPECIFIC;
  797. new_data->data[1] = len - 2; /* not counting EID, len */
  798. new_data->data[2] = (WLAN_OUI_WFA >> 16) & 0xff;
  799. new_data->data[3] = (WLAN_OUI_WFA >> 8) & 0xff;
  800. new_data->data[4] = (WLAN_OUI_WFA >> 0) & 0xff;
  801. new_data->data[5] = WLAN_OUI_TYPE_WFA_P2P;
  802. memcpy(&new_data->data[6], &noa_notif->noa_attribute,
  803. copylen);
  804. }
  805. } else
  806. new_data = NULL;
  807. rcu_assign_pointer(priv->noa_data, new_data);
  808. if (old_data)
  809. kfree_rcu(old_data, rcu_head);
  810. }
  811. /**
  812. * iwl_setup_rx_handlers - Initialize Rx handler callbacks
  813. *
  814. * Setup the RX handlers for each of the reply types sent from the uCode
  815. * to the host.
  816. */
  817. void iwl_setup_rx_handlers(struct iwl_priv *priv)
  818. {
  819. void (**handlers)(struct iwl_priv *priv, struct iwl_rx_cmd_buffer *rxb);
  820. handlers = priv->rx_handlers;
  821. handlers[REPLY_ERROR] = iwlagn_rx_reply_error;
  822. handlers[CHANNEL_SWITCH_NOTIFICATION] = iwlagn_rx_csa;
  823. handlers[SPECTRUM_MEASURE_NOTIFICATION] =
  824. iwlagn_rx_spectrum_measure_notif;
  825. handlers[PM_SLEEP_NOTIFICATION] = iwlagn_rx_pm_sleep_notif;
  826. handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  827. iwlagn_rx_pm_debug_statistics_notif;
  828. handlers[BEACON_NOTIFICATION] = iwlagn_rx_beacon_notif;
  829. handlers[REPLY_ADD_STA] = iwl_add_sta_callback;
  830. handlers[REPLY_WIPAN_NOA_NOTIFICATION] = iwlagn_rx_noa_notification;
  831. /*
  832. * The same handler is used for both the REPLY to a discrete
  833. * statistics request from the host as well as for the periodic
  834. * statistics notifications (after received beacons) from the uCode.
  835. */
  836. handlers[REPLY_STATISTICS_CMD] = iwlagn_rx_reply_statistics;
  837. handlers[STATISTICS_NOTIFICATION] = iwlagn_rx_statistics;
  838. iwl_setup_rx_scan_handlers(priv);
  839. handlers[CARD_STATE_NOTIFICATION] = iwlagn_rx_card_state_notif;
  840. handlers[MISSED_BEACONS_NOTIFICATION] =
  841. iwlagn_rx_missed_beacon_notif;
  842. /* Rx handlers */
  843. handlers[REPLY_RX_PHY_CMD] = iwlagn_rx_reply_rx_phy;
  844. handlers[REPLY_RX_MPDU_CMD] = iwlagn_rx_reply_rx;
  845. /* block ack */
  846. handlers[REPLY_COMPRESSED_BA] =
  847. iwlagn_rx_reply_compressed_ba;
  848. priv->rx_handlers[REPLY_TX] = iwlagn_rx_reply_tx;
  849. /* set up notification wait support */
  850. iwl_notification_wait_init(&priv->notif_wait);
  851. /* Set up BT Rx handlers */
  852. if (priv->lib->bt_params)
  853. iwlagn_bt_rx_handler_setup(priv);
  854. }
  855. void iwl_rx_dispatch(struct iwl_op_mode *op_mode, struct napi_struct *napi,
  856. struct iwl_rx_cmd_buffer *rxb)
  857. {
  858. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  859. struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
  860. /*
  861. * Do the notification wait before RX handlers so
  862. * even if the RX handler consumes the RXB we have
  863. * access to it in the notification wait entry.
  864. */
  865. iwl_notification_wait_notify(&priv->notif_wait, pkt);
  866. /* Based on type of command response or notification,
  867. * handle those that need handling via function in
  868. * rx_handlers table. See iwl_setup_rx_handlers() */
  869. if (priv->rx_handlers[pkt->hdr.cmd]) {
  870. priv->rx_handlers_stats[pkt->hdr.cmd]++;
  871. priv->rx_handlers[pkt->hdr.cmd](priv, rxb);
  872. } else {
  873. /* No handling needed */
  874. IWL_DEBUG_RX(priv, "No handler needed for %s, 0x%02x\n",
  875. iwl_get_cmd_string(priv->trans,
  876. iwl_cmd_id(pkt->hdr.cmd,
  877. 0, 0)),
  878. pkt->hdr.cmd);
  879. }
  880. }