iwl-core.c 32 KB

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