iwl-agn-rx.c 36 KB

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