iwl-core.c 35 KB

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