iwl-core.c 32 KB

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