iwl-agn-rx.c 36 KB

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