iwl-core.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2012 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
  19. * USA
  20. *
  21. * The full GNU General Public License is included in this distribution
  22. * in the file called LICENSE.GPL.
  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. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/sched.h>
  31. #include <linux/slab.h>
  32. #include <net/mac80211.h>
  33. #include "iwl-eeprom.h"
  34. #include "iwl-debug.h"
  35. #include "iwl-core.h"
  36. #include "iwl-io.h"
  37. #include "iwl-power.h"
  38. #include "iwl-shared.h"
  39. #include "iwl-agn.h"
  40. #include "iwl-trans.h"
  41. const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  42. static bool iwl_is_channel_extension(struct iwl_priv *priv,
  43. enum ieee80211_band band,
  44. u16 channel, u8 extension_chan_offset)
  45. {
  46. const struct iwl_channel_info *ch_info;
  47. ch_info = iwl_get_channel_info(priv, band, channel);
  48. if (!is_channel_valid(ch_info))
  49. return false;
  50. if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
  51. return !(ch_info->ht40_extension_channel &
  52. IEEE80211_CHAN_NO_HT40PLUS);
  53. else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
  54. return !(ch_info->ht40_extension_channel &
  55. IEEE80211_CHAN_NO_HT40MINUS);
  56. return false;
  57. }
  58. bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
  59. struct iwl_rxon_context *ctx,
  60. struct ieee80211_sta_ht_cap *ht_cap)
  61. {
  62. if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
  63. return false;
  64. /*
  65. * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
  66. * the bit will not set if it is pure 40MHz case
  67. */
  68. if (ht_cap && !ht_cap->ht_supported)
  69. return false;
  70. #ifdef CONFIG_IWLWIFI_DEBUGFS
  71. if (priv->disable_ht40)
  72. return false;
  73. #endif
  74. return iwl_is_channel_extension(priv, priv->band,
  75. le16_to_cpu(ctx->staging.channel),
  76. ctx->ht.extension_chan_offset);
  77. }
  78. static void _iwl_set_rxon_ht(struct iwl_priv *priv,
  79. struct iwl_ht_config *ht_conf,
  80. struct iwl_rxon_context *ctx)
  81. {
  82. struct iwl_rxon_cmd *rxon = &ctx->staging;
  83. if (!ctx->ht.enabled) {
  84. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  85. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  86. RXON_FLG_HT40_PROT_MSK |
  87. RXON_FLG_HT_PROT_MSK);
  88. return;
  89. }
  90. /* FIXME: if the definition of ht.protection changed, the "translation"
  91. * will be needed for rxon->flags
  92. */
  93. rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
  94. /* Set up channel bandwidth:
  95. * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
  96. /* clear the HT channel mode before set the mode */
  97. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  98. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  99. if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
  100. /* pure ht40 */
  101. if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
  102. rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
  103. /* Note: control channel is opposite of extension channel */
  104. switch (ctx->ht.extension_chan_offset) {
  105. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  106. rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  107. break;
  108. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  109. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  110. break;
  111. }
  112. } else {
  113. /* Note: control channel is opposite of extension channel */
  114. switch (ctx->ht.extension_chan_offset) {
  115. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  116. rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  117. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  118. break;
  119. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  120. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  121. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  122. break;
  123. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  124. default:
  125. /* channel location only valid if in Mixed mode */
  126. IWL_ERR(priv, "invalid extension channel offset\n");
  127. break;
  128. }
  129. }
  130. } else {
  131. rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
  132. }
  133. iwlagn_set_rxon_chain(priv, ctx);
  134. IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
  135. "extension channel offset 0x%x\n",
  136. le32_to_cpu(rxon->flags), ctx->ht.protection,
  137. ctx->ht.extension_chan_offset);
  138. }
  139. void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
  140. {
  141. struct iwl_rxon_context *ctx;
  142. for_each_context(priv, ctx)
  143. _iwl_set_rxon_ht(priv, ht_conf, ctx);
  144. }
  145. /**
  146. * iwl_set_rxon_channel - Set the band and channel values in staging RXON
  147. * @ch: requested channel as a pointer to struct ieee80211_channel
  148. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  149. * in the staging RXON flag structure based on the ch->band
  150. */
  151. void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
  152. struct iwl_rxon_context *ctx)
  153. {
  154. enum ieee80211_band band = ch->band;
  155. u16 channel = ch->hw_value;
  156. if ((le16_to_cpu(ctx->staging.channel) == channel) &&
  157. (priv->band == band))
  158. return;
  159. ctx->staging.channel = cpu_to_le16(channel);
  160. if (band == IEEE80211_BAND_5GHZ)
  161. ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
  162. else
  163. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  164. priv->band = band;
  165. IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
  166. }
  167. void iwl_set_flags_for_band(struct iwl_priv *priv,
  168. struct iwl_rxon_context *ctx,
  169. enum ieee80211_band band,
  170. struct ieee80211_vif *vif)
  171. {
  172. if (band == IEEE80211_BAND_5GHZ) {
  173. ctx->staging.flags &=
  174. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
  175. | RXON_FLG_CCK_MSK);
  176. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  177. } else {
  178. /* Copied from iwl_post_associate() */
  179. if (vif && vif->bss_conf.use_short_slot)
  180. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  181. else
  182. ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  183. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  184. ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
  185. ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
  186. }
  187. }
  188. /*
  189. * initialize rxon structure with default values from eeprom
  190. */
  191. void iwl_connection_init_rx_config(struct iwl_priv *priv,
  192. struct iwl_rxon_context *ctx)
  193. {
  194. const struct iwl_channel_info *ch_info;
  195. memset(&ctx->staging, 0, sizeof(ctx->staging));
  196. if (!ctx->vif) {
  197. ctx->staging.dev_type = ctx->unused_devtype;
  198. } else switch (ctx->vif->type) {
  199. case NL80211_IFTYPE_AP:
  200. ctx->staging.dev_type = ctx->ap_devtype;
  201. break;
  202. case NL80211_IFTYPE_STATION:
  203. ctx->staging.dev_type = ctx->station_devtype;
  204. ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  205. break;
  206. case NL80211_IFTYPE_ADHOC:
  207. ctx->staging.dev_type = ctx->ibss_devtype;
  208. ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  209. ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
  210. RXON_FILTER_ACCEPT_GRP_MSK;
  211. break;
  212. default:
  213. IWL_ERR(priv, "Unsupported interface type %d\n",
  214. ctx->vif->type);
  215. break;
  216. }
  217. #if 0
  218. /* TODO: Figure out when short_preamble would be set and cache from
  219. * that */
  220. if (!hw_to_local(priv->hw)->short_preamble)
  221. ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  222. else
  223. ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  224. #endif
  225. ch_info = iwl_get_channel_info(priv, priv->band,
  226. le16_to_cpu(ctx->active.channel));
  227. if (!ch_info)
  228. ch_info = &priv->channel_info[0];
  229. ctx->staging.channel = cpu_to_le16(ch_info->channel);
  230. priv->band = ch_info->band;
  231. iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
  232. ctx->staging.ofdm_basic_rates =
  233. (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  234. ctx->staging.cck_basic_rates =
  235. (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  236. /* clear both MIX and PURE40 mode flag */
  237. ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
  238. RXON_FLG_CHANNEL_MODE_PURE_40);
  239. if (ctx->vif)
  240. memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
  241. ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
  242. ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
  243. ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
  244. }
  245. void iwl_set_rate(struct iwl_priv *priv)
  246. {
  247. const struct ieee80211_supported_band *hw = NULL;
  248. struct ieee80211_rate *rate;
  249. struct iwl_rxon_context *ctx;
  250. int i;
  251. hw = iwl_get_hw_mode(priv, priv->band);
  252. if (!hw) {
  253. IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
  254. return;
  255. }
  256. priv->active_rate = 0;
  257. for (i = 0; i < hw->n_bitrates; i++) {
  258. rate = &(hw->bitrates[i]);
  259. if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
  260. priv->active_rate |= (1 << rate->hw_value);
  261. }
  262. IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
  263. for_each_context(priv, ctx) {
  264. ctx->staging.cck_basic_rates =
  265. (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  266. ctx->staging.ofdm_basic_rates =
  267. (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  268. }
  269. }
  270. void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
  271. {
  272. /*
  273. * MULTI-FIXME
  274. * See iwlagn_mac_channel_switch.
  275. */
  276. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  277. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  278. return;
  279. if (test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
  280. ieee80211_chswitch_done(ctx->vif, is_success);
  281. }
  282. #ifdef CONFIG_IWLWIFI_DEBUG
  283. void iwl_print_rx_config_cmd(struct iwl_priv *priv,
  284. enum iwl_rxon_context_id ctxid)
  285. {
  286. struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
  287. struct iwl_rxon_cmd *rxon = &ctx->staging;
  288. IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
  289. iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  290. IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  291. IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  292. IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
  293. le32_to_cpu(rxon->filter_flags));
  294. IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
  295. IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
  296. rxon->ofdm_basic_rates);
  297. IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  298. IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
  299. IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  300. IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  301. }
  302. #endif
  303. void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
  304. {
  305. unsigned int reload_msec;
  306. unsigned long reload_jiffies;
  307. #ifdef CONFIG_IWLWIFI_DEBUG
  308. if (iwl_have_debug_level(IWL_DL_FW_ERRORS))
  309. iwl_print_rx_config_cmd(priv, IWL_RXON_CTX_BSS);
  310. #endif
  311. /* uCode is no longer loaded. */
  312. priv->ucode_loaded = false;
  313. /* Set the FW error flag -- cleared on iwl_down */
  314. set_bit(STATUS_FW_ERROR, &priv->status);
  315. /* Cancel currently queued command. */
  316. clear_bit(STATUS_HCMD_ACTIVE, &priv->shrd->status);
  317. iwl_abort_notification_waits(&priv->notif_wait);
  318. /* Keep the restart process from trying to send host
  319. * commands by clearing the ready bit */
  320. clear_bit(STATUS_READY, &priv->status);
  321. wake_up(&trans(priv)->wait_command_queue);
  322. if (!ondemand) {
  323. /*
  324. * If firmware keep reloading, then it indicate something
  325. * serious wrong and firmware having problem to recover
  326. * from it. Instead of keep trying which will fill the syslog
  327. * and hang the system, let's just stop it
  328. */
  329. reload_jiffies = jiffies;
  330. reload_msec = jiffies_to_msecs((long) reload_jiffies -
  331. (long) priv->reload_jiffies);
  332. priv->reload_jiffies = reload_jiffies;
  333. if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
  334. priv->reload_count++;
  335. if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
  336. IWL_ERR(priv, "BUG_ON, Stop restarting\n");
  337. return;
  338. }
  339. } else
  340. priv->reload_count = 0;
  341. }
  342. if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  343. if (iwlagn_mod_params.restart_fw) {
  344. IWL_DEBUG_FW_ERRORS(priv,
  345. "Restarting adapter due to uCode error.\n");
  346. queue_work(priv->workqueue, &priv->restart);
  347. } else
  348. IWL_DEBUG_FW_ERRORS(priv,
  349. "Detected FW error, but not restarting\n");
  350. }
  351. }
  352. int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
  353. {
  354. int ret;
  355. s8 prev_tx_power;
  356. bool defer;
  357. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  358. lockdep_assert_held(&priv->mutex);
  359. if (priv->tx_power_user_lmt == tx_power && !force)
  360. return 0;
  361. if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
  362. IWL_WARN(priv,
  363. "Requested user TXPOWER %d below lower limit %d.\n",
  364. tx_power,
  365. IWLAGN_TX_POWER_TARGET_POWER_MIN);
  366. return -EINVAL;
  367. }
  368. if (tx_power > priv->tx_power_device_lmt) {
  369. IWL_WARN(priv,
  370. "Requested user TXPOWER %d above upper limit %d.\n",
  371. tx_power, priv->tx_power_device_lmt);
  372. return -EINVAL;
  373. }
  374. if (!iwl_is_ready_rf(priv))
  375. return -EIO;
  376. /* scan complete and commit_rxon use tx_power_next value,
  377. * it always need to be updated for newest request */
  378. priv->tx_power_next = tx_power;
  379. /* do not set tx power when scanning or channel changing */
  380. defer = test_bit(STATUS_SCANNING, &priv->status) ||
  381. memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
  382. if (defer && !force) {
  383. IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
  384. return 0;
  385. }
  386. prev_tx_power = priv->tx_power_user_lmt;
  387. priv->tx_power_user_lmt = tx_power;
  388. ret = iwlagn_send_tx_power(priv);
  389. /* if fail to set tx_power, restore the orig. tx power */
  390. if (ret) {
  391. priv->tx_power_user_lmt = prev_tx_power;
  392. priv->tx_power_next = prev_tx_power;
  393. }
  394. return ret;
  395. }
  396. void iwl_send_bt_config(struct iwl_priv *priv)
  397. {
  398. struct iwl_bt_cmd bt_cmd = {
  399. .lead_time = BT_LEAD_TIME_DEF,
  400. .max_kill = BT_MAX_KILL_DEF,
  401. .kill_ack_mask = 0,
  402. .kill_cts_mask = 0,
  403. };
  404. if (!iwlagn_mod_params.bt_coex_active)
  405. bt_cmd.flags = BT_COEX_DISABLE;
  406. else
  407. bt_cmd.flags = BT_COEX_ENABLE;
  408. priv->bt_enable_flag = bt_cmd.flags;
  409. IWL_DEBUG_INFO(priv, "BT coex %s\n",
  410. (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
  411. if (iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  412. CMD_SYNC, sizeof(struct iwl_bt_cmd), &bt_cmd))
  413. IWL_ERR(priv, "failed to send BT Coex Config\n");
  414. }
  415. int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
  416. {
  417. struct iwl_statistics_cmd statistics_cmd = {
  418. .configuration_flags =
  419. clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
  420. };
  421. if (flags & CMD_ASYNC)
  422. return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
  423. CMD_ASYNC,
  424. sizeof(struct iwl_statistics_cmd),
  425. &statistics_cmd);
  426. else
  427. return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
  428. CMD_SYNC,
  429. sizeof(struct iwl_statistics_cmd),
  430. &statistics_cmd);
  431. }
  432. #ifdef CONFIG_IWLWIFI_DEBUGFS
  433. #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
  434. void iwl_reset_traffic_log(struct iwl_priv *priv)
  435. {
  436. priv->tx_traffic_idx = 0;
  437. priv->rx_traffic_idx = 0;
  438. if (priv->tx_traffic)
  439. memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  440. if (priv->rx_traffic)
  441. memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  442. }
  443. int iwl_alloc_traffic_mem(struct iwl_priv *priv)
  444. {
  445. u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
  446. if (iwl_have_debug_level(IWL_DL_TX)) {
  447. if (!priv->tx_traffic) {
  448. priv->tx_traffic =
  449. kzalloc(traffic_size, GFP_KERNEL);
  450. if (!priv->tx_traffic)
  451. return -ENOMEM;
  452. }
  453. }
  454. if (iwl_have_debug_level(IWL_DL_RX)) {
  455. if (!priv->rx_traffic) {
  456. priv->rx_traffic =
  457. kzalloc(traffic_size, GFP_KERNEL);
  458. if (!priv->rx_traffic)
  459. return -ENOMEM;
  460. }
  461. }
  462. iwl_reset_traffic_log(priv);
  463. return 0;
  464. }
  465. void iwl_free_traffic_mem(struct iwl_priv *priv)
  466. {
  467. kfree(priv->tx_traffic);
  468. priv->tx_traffic = NULL;
  469. kfree(priv->rx_traffic);
  470. priv->rx_traffic = NULL;
  471. }
  472. void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
  473. u16 length, struct ieee80211_hdr *header)
  474. {
  475. __le16 fc;
  476. u16 len;
  477. if (likely(!iwl_have_debug_level(IWL_DL_TX)))
  478. return;
  479. if (!priv->tx_traffic)
  480. return;
  481. fc = header->frame_control;
  482. if (ieee80211_is_data(fc)) {
  483. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  484. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  485. memcpy((priv->tx_traffic +
  486. (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  487. header, len);
  488. priv->tx_traffic_idx =
  489. (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  490. }
  491. }
  492. void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
  493. u16 length, struct ieee80211_hdr *header)
  494. {
  495. __le16 fc;
  496. u16 len;
  497. if (likely(!iwl_have_debug_level(IWL_DL_RX)))
  498. return;
  499. if (!priv->rx_traffic)
  500. return;
  501. fc = header->frame_control;
  502. if (ieee80211_is_data(fc)) {
  503. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  504. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  505. memcpy((priv->rx_traffic +
  506. (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  507. header, len);
  508. priv->rx_traffic_idx =
  509. (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  510. }
  511. }
  512. const char *get_mgmt_string(int cmd)
  513. {
  514. switch (cmd) {
  515. IWL_CMD(MANAGEMENT_ASSOC_REQ);
  516. IWL_CMD(MANAGEMENT_ASSOC_RESP);
  517. IWL_CMD(MANAGEMENT_REASSOC_REQ);
  518. IWL_CMD(MANAGEMENT_REASSOC_RESP);
  519. IWL_CMD(MANAGEMENT_PROBE_REQ);
  520. IWL_CMD(MANAGEMENT_PROBE_RESP);
  521. IWL_CMD(MANAGEMENT_BEACON);
  522. IWL_CMD(MANAGEMENT_ATIM);
  523. IWL_CMD(MANAGEMENT_DISASSOC);
  524. IWL_CMD(MANAGEMENT_AUTH);
  525. IWL_CMD(MANAGEMENT_DEAUTH);
  526. IWL_CMD(MANAGEMENT_ACTION);
  527. default:
  528. return "UNKNOWN";
  529. }
  530. }
  531. const char *get_ctrl_string(int cmd)
  532. {
  533. switch (cmd) {
  534. IWL_CMD(CONTROL_BACK_REQ);
  535. IWL_CMD(CONTROL_BACK);
  536. IWL_CMD(CONTROL_PSPOLL);
  537. IWL_CMD(CONTROL_RTS);
  538. IWL_CMD(CONTROL_CTS);
  539. IWL_CMD(CONTROL_ACK);
  540. IWL_CMD(CONTROL_CFEND);
  541. IWL_CMD(CONTROL_CFENDACK);
  542. default:
  543. return "UNKNOWN";
  544. }
  545. }
  546. void iwl_clear_traffic_stats(struct iwl_priv *priv)
  547. {
  548. memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
  549. memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
  550. }
  551. /*
  552. * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
  553. * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
  554. * Use debugFs to display the rx/rx_statistics
  555. * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
  556. * information will be recorded, but DATA pkt still will be recorded
  557. * for the reason of iwl_led.c need to control the led blinking based on
  558. * number of tx and rx data.
  559. *
  560. */
  561. void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
  562. {
  563. struct traffic_stats *stats;
  564. if (is_tx)
  565. stats = &priv->tx_stats;
  566. else
  567. stats = &priv->rx_stats;
  568. if (ieee80211_is_mgmt(fc)) {
  569. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  570. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  571. stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
  572. break;
  573. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  574. stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
  575. break;
  576. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  577. stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
  578. break;
  579. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  580. stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
  581. break;
  582. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  583. stats->mgmt[MANAGEMENT_PROBE_REQ]++;
  584. break;
  585. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  586. stats->mgmt[MANAGEMENT_PROBE_RESP]++;
  587. break;
  588. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  589. stats->mgmt[MANAGEMENT_BEACON]++;
  590. break;
  591. case cpu_to_le16(IEEE80211_STYPE_ATIM):
  592. stats->mgmt[MANAGEMENT_ATIM]++;
  593. break;
  594. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  595. stats->mgmt[MANAGEMENT_DISASSOC]++;
  596. break;
  597. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  598. stats->mgmt[MANAGEMENT_AUTH]++;
  599. break;
  600. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  601. stats->mgmt[MANAGEMENT_DEAUTH]++;
  602. break;
  603. case cpu_to_le16(IEEE80211_STYPE_ACTION):
  604. stats->mgmt[MANAGEMENT_ACTION]++;
  605. break;
  606. }
  607. } else if (ieee80211_is_ctl(fc)) {
  608. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  609. case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
  610. stats->ctrl[CONTROL_BACK_REQ]++;
  611. break;
  612. case cpu_to_le16(IEEE80211_STYPE_BACK):
  613. stats->ctrl[CONTROL_BACK]++;
  614. break;
  615. case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
  616. stats->ctrl[CONTROL_PSPOLL]++;
  617. break;
  618. case cpu_to_le16(IEEE80211_STYPE_RTS):
  619. stats->ctrl[CONTROL_RTS]++;
  620. break;
  621. case cpu_to_le16(IEEE80211_STYPE_CTS):
  622. stats->ctrl[CONTROL_CTS]++;
  623. break;
  624. case cpu_to_le16(IEEE80211_STYPE_ACK):
  625. stats->ctrl[CONTROL_ACK]++;
  626. break;
  627. case cpu_to_le16(IEEE80211_STYPE_CFEND):
  628. stats->ctrl[CONTROL_CFEND]++;
  629. break;
  630. case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
  631. stats->ctrl[CONTROL_CFENDACK]++;
  632. break;
  633. }
  634. } else {
  635. /* data */
  636. stats->data_cnt++;
  637. stats->data_bytes += len;
  638. }
  639. }
  640. #endif
  641. static void iwl_force_rf_reset(struct iwl_priv *priv)
  642. {
  643. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  644. return;
  645. if (!iwl_is_any_associated(priv)) {
  646. IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
  647. return;
  648. }
  649. /*
  650. * There is no easy and better way to force reset the radio,
  651. * the only known method is switching channel which will force to
  652. * reset and tune the radio.
  653. * Use internal short scan (single channel) operation to should
  654. * achieve this objective.
  655. * Driver should reset the radio when number of consecutive missed
  656. * beacon, or any other uCode error condition detected.
  657. */
  658. IWL_DEBUG_INFO(priv, "perform radio reset.\n");
  659. iwl_internal_short_hw_scan(priv);
  660. }
  661. int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
  662. {
  663. struct iwl_force_reset *force_reset;
  664. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  665. return -EINVAL;
  666. if (mode >= IWL_MAX_FORCE_RESET) {
  667. IWL_DEBUG_INFO(priv, "invalid reset request.\n");
  668. return -EINVAL;
  669. }
  670. force_reset = &priv->force_reset[mode];
  671. force_reset->reset_request_count++;
  672. if (!external) {
  673. if (force_reset->last_force_reset_jiffies &&
  674. time_after(force_reset->last_force_reset_jiffies +
  675. force_reset->reset_duration, jiffies)) {
  676. IWL_DEBUG_INFO(priv, "force reset rejected\n");
  677. force_reset->reset_reject_count++;
  678. return -EAGAIN;
  679. }
  680. }
  681. force_reset->reset_success_count++;
  682. force_reset->last_force_reset_jiffies = jiffies;
  683. IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
  684. switch (mode) {
  685. case IWL_RF_RESET:
  686. iwl_force_rf_reset(priv);
  687. break;
  688. case IWL_FW_RESET:
  689. /*
  690. * if the request is from external(ex: debugfs),
  691. * then always perform the request in regardless the module
  692. * parameter setting
  693. * if the request is from internal (uCode error or driver
  694. * detect failure), then fw_restart module parameter
  695. * need to be check before performing firmware reload
  696. */
  697. if (!external && !iwlagn_mod_params.restart_fw) {
  698. IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
  699. "module parameter setting\n");
  700. break;
  701. }
  702. IWL_ERR(priv, "On demand firmware reload\n");
  703. iwlagn_fw_error(priv, true);
  704. break;
  705. }
  706. return 0;
  707. }
  708. int iwl_cmd_echo_test(struct iwl_priv *priv)
  709. {
  710. int ret;
  711. struct iwl_host_cmd cmd = {
  712. .id = REPLY_ECHO,
  713. .len = { 0 },
  714. .flags = CMD_SYNC,
  715. };
  716. ret = iwl_dvm_send_cmd(priv, &cmd);
  717. if (ret)
  718. IWL_ERR(priv, "echo testing fail: 0X%x\n", ret);
  719. else
  720. IWL_DEBUG_INFO(priv, "echo testing pass\n");
  721. return ret;
  722. }
  723. static inline int iwl_check_stuck_queue(struct iwl_priv *priv, int txq)
  724. {
  725. if (iwl_trans_check_stuck_queue(trans(priv), txq)) {
  726. int ret;
  727. ret = iwl_force_reset(priv, IWL_FW_RESET, false);
  728. return (ret == -EAGAIN) ? 0 : 1;
  729. }
  730. return 0;
  731. }
  732. /*
  733. * Making watchdog tick be a quarter of timeout assure we will
  734. * discover the queue hung between timeout and 1.25*timeout
  735. */
  736. #define IWL_WD_TICK(timeout) ((timeout) / 4)
  737. /*
  738. * Watchdog timer callback, we check each tx queue for stuck, if if hung
  739. * we reset the firmware. If everything is fine just rearm the timer.
  740. */
  741. void iwl_bg_watchdog(unsigned long data)
  742. {
  743. struct iwl_priv *priv = (struct iwl_priv *)data;
  744. int cnt;
  745. unsigned long timeout;
  746. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  747. return;
  748. if (iwl_is_rfkill(priv))
  749. return;
  750. timeout = hw_params(priv).wd_timeout;
  751. if (timeout == 0)
  752. return;
  753. /* monitor and check for stuck queues */
  754. for (cnt = 0; cnt < cfg(priv)->base_params->num_of_queues; cnt++)
  755. if (iwl_check_stuck_queue(priv, cnt))
  756. return;
  757. mod_timer(&priv->watchdog, jiffies +
  758. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  759. }
  760. void iwl_setup_watchdog(struct iwl_priv *priv)
  761. {
  762. unsigned int timeout = hw_params(priv).wd_timeout;
  763. if (!iwlagn_mod_params.wd_disable) {
  764. /* use system default */
  765. if (timeout && !cfg(priv)->base_params->wd_disable)
  766. mod_timer(&priv->watchdog,
  767. jiffies +
  768. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  769. else
  770. del_timer(&priv->watchdog);
  771. } else {
  772. /* module parameter overwrite default configuration */
  773. if (timeout && iwlagn_mod_params.wd_disable == 2)
  774. mod_timer(&priv->watchdog,
  775. jiffies +
  776. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  777. else
  778. del_timer(&priv->watchdog);
  779. }
  780. }
  781. /**
  782. * iwl_beacon_time_mask_low - mask of lower 32 bit of beacon time
  783. * @priv -- pointer to iwl_priv data structure
  784. * @tsf_bits -- number of bits need to shift for masking)
  785. */
  786. static inline u32 iwl_beacon_time_mask_low(struct iwl_priv *priv,
  787. u16 tsf_bits)
  788. {
  789. return (1 << tsf_bits) - 1;
  790. }
  791. /**
  792. * iwl_beacon_time_mask_high - mask of higher 32 bit of beacon time
  793. * @priv -- pointer to iwl_priv data structure
  794. * @tsf_bits -- number of bits need to shift for masking)
  795. */
  796. static inline u32 iwl_beacon_time_mask_high(struct iwl_priv *priv,
  797. u16 tsf_bits)
  798. {
  799. return ((1 << (32 - tsf_bits)) - 1) << tsf_bits;
  800. }
  801. /*
  802. * extended beacon time format
  803. * time in usec will be changed into a 32-bit value in extended:internal format
  804. * the extended part is the beacon counts
  805. * the internal part is the time in usec within one beacon interval
  806. */
  807. u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
  808. {
  809. u32 quot;
  810. u32 rem;
  811. u32 interval = beacon_interval * TIME_UNIT;
  812. if (!interval || !usec)
  813. return 0;
  814. quot = (usec / interval) &
  815. (iwl_beacon_time_mask_high(priv, IWLAGN_EXT_BEACON_TIME_POS) >>
  816. IWLAGN_EXT_BEACON_TIME_POS);
  817. rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
  818. IWLAGN_EXT_BEACON_TIME_POS);
  819. return (quot << IWLAGN_EXT_BEACON_TIME_POS) + rem;
  820. }
  821. /* base is usually what we get from ucode with each received frame,
  822. * the same as HW timer counter counting down
  823. */
  824. __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
  825. u32 addon, u32 beacon_interval)
  826. {
  827. u32 base_low = base & iwl_beacon_time_mask_low(priv,
  828. IWLAGN_EXT_BEACON_TIME_POS);
  829. u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
  830. IWLAGN_EXT_BEACON_TIME_POS);
  831. u32 interval = beacon_interval * TIME_UNIT;
  832. u32 res = (base & iwl_beacon_time_mask_high(priv,
  833. IWLAGN_EXT_BEACON_TIME_POS)) +
  834. (addon & iwl_beacon_time_mask_high(priv,
  835. IWLAGN_EXT_BEACON_TIME_POS));
  836. if (base_low > addon_low)
  837. res += base_low - addon_low;
  838. else if (base_low < addon_low) {
  839. res += interval + base_low - addon_low;
  840. res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
  841. } else
  842. res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
  843. return cpu_to_le32(res);
  844. }
  845. void iwl_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
  846. {
  847. struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
  848. if (state)
  849. set_bit(STATUS_RF_KILL_HW, &priv->status);
  850. else
  851. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  852. wiphy_rfkill_set_hw_state(priv->hw->wiphy, state);
  853. }
  854. void iwl_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
  855. {
  856. struct ieee80211_tx_info *info;
  857. info = IEEE80211_SKB_CB(skb);
  858. kmem_cache_free(iwl_tx_cmd_pool, (info->driver_data[1]));
  859. dev_kfree_skb_any(skb);
  860. }