iwl-core.c 30 KB

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