iwl-core.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2011 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-dev.h" /* FIXME: remove */
  36. #include "iwl-debug.h"
  37. #include "iwl-core.h"
  38. #include "iwl-io.h"
  39. #include "iwl-power.h"
  40. #include "iwl-sta.h"
  41. #include "iwl-agn.h"
  42. #include "iwl-helpers.h"
  43. #include "iwl-shared.h"
  44. #include "iwl-agn.h"
  45. #include "iwl-trans.h"
  46. const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  47. #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
  48. #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
  49. static void iwl_init_ht_hw_capab(const struct iwl_priv *priv,
  50. struct ieee80211_sta_ht_cap *ht_info,
  51. enum ieee80211_band band)
  52. {
  53. u16 max_bit_rate = 0;
  54. u8 rx_chains_num = hw_params(priv).rx_chains_num;
  55. u8 tx_chains_num = hw_params(priv).tx_chains_num;
  56. ht_info->cap = 0;
  57. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  58. ht_info->ht_supported = true;
  59. if (priv->cfg->ht_params &&
  60. priv->cfg->ht_params->ht_greenfield_support)
  61. ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
  62. ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
  63. max_bit_rate = MAX_BIT_RATE_20_MHZ;
  64. if (hw_params(priv).ht40_channel & BIT(band)) {
  65. ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  66. ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
  67. ht_info->mcs.rx_mask[4] = 0x01;
  68. max_bit_rate = MAX_BIT_RATE_40_MHZ;
  69. }
  70. if (iwlagn_mod_params.amsdu_size_8K)
  71. ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  72. ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
  73. if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_factor)
  74. ht_info->ampdu_factor = priv->cfg->bt_params->ampdu_factor;
  75. ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
  76. if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_density)
  77. ht_info->ampdu_density = priv->cfg->bt_params->ampdu_density;
  78. ht_info->mcs.rx_mask[0] = 0xFF;
  79. if (rx_chains_num >= 2)
  80. ht_info->mcs.rx_mask[1] = 0xFF;
  81. if (rx_chains_num >= 3)
  82. ht_info->mcs.rx_mask[2] = 0xFF;
  83. /* Highest supported Rx data rate */
  84. max_bit_rate *= rx_chains_num;
  85. WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
  86. ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
  87. /* Tx MCS capabilities */
  88. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  89. if (tx_chains_num != rx_chains_num) {
  90. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  91. ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
  92. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  93. }
  94. }
  95. /**
  96. * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
  97. */
  98. int iwl_init_geos(struct iwl_priv *priv)
  99. {
  100. struct iwl_channel_info *ch;
  101. struct ieee80211_supported_band *sband;
  102. struct ieee80211_channel *channels;
  103. struct ieee80211_channel *geo_ch;
  104. struct ieee80211_rate *rates;
  105. int i = 0;
  106. s8 max_tx_power = IWLAGN_TX_POWER_TARGET_POWER_MIN;
  107. if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
  108. priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
  109. IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
  110. set_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status);
  111. return 0;
  112. }
  113. channels = kzalloc(sizeof(struct ieee80211_channel) *
  114. priv->channel_count, GFP_KERNEL);
  115. if (!channels)
  116. return -ENOMEM;
  117. rates = kzalloc((sizeof(struct ieee80211_rate) * IWL_RATE_COUNT_LEGACY),
  118. GFP_KERNEL);
  119. if (!rates) {
  120. kfree(channels);
  121. return -ENOMEM;
  122. }
  123. /* 5.2GHz channels start after the 2.4GHz channels */
  124. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  125. sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
  126. /* just OFDM */
  127. sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
  128. sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
  129. if (priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE)
  130. iwl_init_ht_hw_capab(priv, &sband->ht_cap,
  131. IEEE80211_BAND_5GHZ);
  132. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  133. sband->channels = channels;
  134. /* OFDM & CCK */
  135. sband->bitrates = rates;
  136. sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
  137. if (priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE)
  138. iwl_init_ht_hw_capab(priv, &sband->ht_cap,
  139. IEEE80211_BAND_2GHZ);
  140. priv->ieee_channels = channels;
  141. priv->ieee_rates = rates;
  142. for (i = 0; i < priv->channel_count; i++) {
  143. ch = &priv->channel_info[i];
  144. /* FIXME: might be removed if scan is OK */
  145. if (!is_channel_valid(ch))
  146. continue;
  147. sband = &priv->bands[ch->band];
  148. geo_ch = &sband->channels[sband->n_channels++];
  149. geo_ch->center_freq =
  150. ieee80211_channel_to_frequency(ch->channel, ch->band);
  151. geo_ch->max_power = ch->max_power_avg;
  152. geo_ch->max_antenna_gain = 0xff;
  153. geo_ch->hw_value = ch->channel;
  154. if (is_channel_valid(ch)) {
  155. if (!(ch->flags & EEPROM_CHANNEL_IBSS))
  156. geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
  157. if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
  158. geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  159. if (ch->flags & EEPROM_CHANNEL_RADAR)
  160. geo_ch->flags |= IEEE80211_CHAN_RADAR;
  161. geo_ch->flags |= ch->ht40_extension_channel;
  162. if (ch->max_power_avg > max_tx_power)
  163. max_tx_power = ch->max_power_avg;
  164. } else {
  165. geo_ch->flags |= IEEE80211_CHAN_DISABLED;
  166. }
  167. IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
  168. ch->channel, geo_ch->center_freq,
  169. is_channel_a_band(ch) ? "5.2" : "2.4",
  170. geo_ch->flags & IEEE80211_CHAN_DISABLED ?
  171. "restricted" : "valid",
  172. geo_ch->flags);
  173. }
  174. priv->tx_power_device_lmt = max_tx_power;
  175. priv->tx_power_user_lmt = max_tx_power;
  176. priv->tx_power_next = max_tx_power;
  177. if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
  178. priv->cfg->sku & EEPROM_SKU_CAP_BAND_52GHZ) {
  179. char buf[32];
  180. bus_get_hw_id(priv->bus, buf, sizeof(buf));
  181. IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
  182. "Please send your %s to maintainer.\n", buf);
  183. priv->cfg->sku &= ~EEPROM_SKU_CAP_BAND_52GHZ;
  184. }
  185. IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
  186. priv->bands[IEEE80211_BAND_2GHZ].n_channels,
  187. priv->bands[IEEE80211_BAND_5GHZ].n_channels);
  188. set_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status);
  189. return 0;
  190. }
  191. /*
  192. * iwl_free_geos - undo allocations in iwl_init_geos
  193. */
  194. void iwl_free_geos(struct iwl_priv *priv)
  195. {
  196. kfree(priv->ieee_channels);
  197. kfree(priv->ieee_rates);
  198. clear_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status);
  199. }
  200. static bool iwl_is_channel_extension(struct iwl_priv *priv,
  201. enum ieee80211_band band,
  202. u16 channel, u8 extension_chan_offset)
  203. {
  204. const struct iwl_channel_info *ch_info;
  205. ch_info = iwl_get_channel_info(priv, band, channel);
  206. if (!is_channel_valid(ch_info))
  207. return false;
  208. if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
  209. return !(ch_info->ht40_extension_channel &
  210. IEEE80211_CHAN_NO_HT40PLUS);
  211. else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
  212. return !(ch_info->ht40_extension_channel &
  213. IEEE80211_CHAN_NO_HT40MINUS);
  214. return false;
  215. }
  216. bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
  217. struct iwl_rxon_context *ctx,
  218. struct ieee80211_sta_ht_cap *ht_cap)
  219. {
  220. if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
  221. return false;
  222. /*
  223. * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
  224. * the bit will not set if it is pure 40MHz case
  225. */
  226. if (ht_cap && !ht_cap->ht_supported)
  227. return false;
  228. #ifdef CONFIG_IWLWIFI_DEBUGFS
  229. if (priv->disable_ht40)
  230. return false;
  231. #endif
  232. return iwl_is_channel_extension(priv, priv->band,
  233. le16_to_cpu(ctx->staging.channel),
  234. ctx->ht.extension_chan_offset);
  235. }
  236. static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
  237. {
  238. u16 new_val;
  239. u16 beacon_factor;
  240. /*
  241. * If mac80211 hasn't given us a beacon interval, program
  242. * the default into the device (not checking this here
  243. * would cause the adjustment below to return the maximum
  244. * value, which may break PAN.)
  245. */
  246. if (!beacon_val)
  247. return DEFAULT_BEACON_INTERVAL;
  248. /*
  249. * If the beacon interval we obtained from the peer
  250. * is too large, we'll have to wake up more often
  251. * (and in IBSS case, we'll beacon too much)
  252. *
  253. * For example, if max_beacon_val is 4096, and the
  254. * requested beacon interval is 7000, we'll have to
  255. * use 3500 to be able to wake up on the beacons.
  256. *
  257. * This could badly influence beacon detection stats.
  258. */
  259. beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
  260. new_val = beacon_val / beacon_factor;
  261. if (!new_val)
  262. new_val = max_beacon_val;
  263. return new_val;
  264. }
  265. int iwl_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  266. {
  267. u64 tsf;
  268. s32 interval_tm, rem;
  269. struct ieee80211_conf *conf = NULL;
  270. u16 beacon_int;
  271. struct ieee80211_vif *vif = ctx->vif;
  272. conf = ieee80211_get_hw_conf(priv->hw);
  273. lockdep_assert_held(&priv->shrd->mutex);
  274. memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
  275. ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
  276. ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
  277. beacon_int = vif ? vif->bss_conf.beacon_int : 0;
  278. /*
  279. * TODO: For IBSS we need to get atim_window from mac80211,
  280. * for now just always use 0
  281. */
  282. ctx->timing.atim_window = 0;
  283. if (ctx->ctxid == IWL_RXON_CTX_PAN &&
  284. (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
  285. iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
  286. priv->contexts[IWL_RXON_CTX_BSS].vif &&
  287. priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
  288. ctx->timing.beacon_interval =
  289. priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
  290. beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
  291. } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
  292. iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
  293. priv->contexts[IWL_RXON_CTX_PAN].vif &&
  294. priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
  295. (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
  296. !ctx->vif->bss_conf.beacon_int)) {
  297. ctx->timing.beacon_interval =
  298. priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
  299. beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
  300. } else {
  301. beacon_int = iwl_adjust_beacon_interval(beacon_int,
  302. IWL_MAX_UCODE_BEACON_INTERVAL * TIME_UNIT);
  303. ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
  304. }
  305. ctx->beacon_int = beacon_int;
  306. tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
  307. interval_tm = beacon_int * TIME_UNIT;
  308. rem = do_div(tsf, interval_tm);
  309. ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
  310. ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
  311. IWL_DEBUG_ASSOC(priv,
  312. "beacon interval %d beacon timer %d beacon tim %d\n",
  313. le16_to_cpu(ctx->timing.beacon_interval),
  314. le32_to_cpu(ctx->timing.beacon_init_val),
  315. le16_to_cpu(ctx->timing.atim_window));
  316. return iwl_trans_send_cmd_pdu(trans(priv), ctx->rxon_timing_cmd,
  317. CMD_SYNC, sizeof(ctx->timing), &ctx->timing);
  318. }
  319. void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  320. int hw_decrypt)
  321. {
  322. struct iwl_rxon_cmd *rxon = &ctx->staging;
  323. if (hw_decrypt)
  324. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  325. else
  326. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  327. }
  328. /* validate RXON structure is valid */
  329. int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  330. {
  331. struct iwl_rxon_cmd *rxon = &ctx->staging;
  332. u32 errors = 0;
  333. if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
  334. if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
  335. IWL_WARN(priv, "check 2.4G: wrong narrow\n");
  336. errors |= BIT(0);
  337. }
  338. if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
  339. IWL_WARN(priv, "check 2.4G: wrong radar\n");
  340. errors |= BIT(1);
  341. }
  342. } else {
  343. if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
  344. IWL_WARN(priv, "check 5.2G: not short slot!\n");
  345. errors |= BIT(2);
  346. }
  347. if (rxon->flags & RXON_FLG_CCK_MSK) {
  348. IWL_WARN(priv, "check 5.2G: CCK!\n");
  349. errors |= BIT(3);
  350. }
  351. }
  352. if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
  353. IWL_WARN(priv, "mac/bssid mcast!\n");
  354. errors |= BIT(4);
  355. }
  356. /* make sure basic rates 6Mbps and 1Mbps are supported */
  357. if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
  358. (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
  359. IWL_WARN(priv, "neither 1 nor 6 are basic\n");
  360. errors |= BIT(5);
  361. }
  362. if (le16_to_cpu(rxon->assoc_id) > 2007) {
  363. IWL_WARN(priv, "aid > 2007\n");
  364. errors |= BIT(6);
  365. }
  366. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
  367. == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
  368. IWL_WARN(priv, "CCK and short slot\n");
  369. errors |= BIT(7);
  370. }
  371. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
  372. == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
  373. IWL_WARN(priv, "CCK and auto detect");
  374. errors |= BIT(8);
  375. }
  376. if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
  377. RXON_FLG_TGG_PROTECT_MSK)) ==
  378. RXON_FLG_TGG_PROTECT_MSK) {
  379. IWL_WARN(priv, "TGg but no auto-detect\n");
  380. errors |= BIT(9);
  381. }
  382. if (rxon->channel == 0) {
  383. IWL_WARN(priv, "zero channel is invalid\n");
  384. errors |= BIT(10);
  385. }
  386. WARN(errors, "Invalid RXON (%#x), channel %d",
  387. errors, le16_to_cpu(rxon->channel));
  388. return errors ? -EINVAL : 0;
  389. }
  390. /**
  391. * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
  392. * @priv: staging_rxon is compared to active_rxon
  393. *
  394. * If the RXON structure is changing enough to require a new tune,
  395. * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
  396. * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
  397. */
  398. int iwl_full_rxon_required(struct iwl_priv *priv,
  399. struct iwl_rxon_context *ctx)
  400. {
  401. const struct iwl_rxon_cmd *staging = &ctx->staging;
  402. const struct iwl_rxon_cmd *active = &ctx->active;
  403. #define CHK(cond) \
  404. if ((cond)) { \
  405. IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n"); \
  406. return 1; \
  407. }
  408. #define CHK_NEQ(c1, c2) \
  409. if ((c1) != (c2)) { \
  410. IWL_DEBUG_INFO(priv, "need full RXON - " \
  411. #c1 " != " #c2 " - %d != %d\n", \
  412. (c1), (c2)); \
  413. return 1; \
  414. }
  415. /* These items are only settable from the full RXON command */
  416. CHK(!iwl_is_associated_ctx(ctx));
  417. CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
  418. CHK(compare_ether_addr(staging->node_addr, active->node_addr));
  419. CHK(compare_ether_addr(staging->wlap_bssid_addr,
  420. active->wlap_bssid_addr));
  421. CHK_NEQ(staging->dev_type, active->dev_type);
  422. CHK_NEQ(staging->channel, active->channel);
  423. CHK_NEQ(staging->air_propagation, active->air_propagation);
  424. CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
  425. active->ofdm_ht_single_stream_basic_rates);
  426. CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
  427. active->ofdm_ht_dual_stream_basic_rates);
  428. CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
  429. active->ofdm_ht_triple_stream_basic_rates);
  430. CHK_NEQ(staging->assoc_id, active->assoc_id);
  431. /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
  432. * be updated with the RXON_ASSOC command -- however only some
  433. * flag transitions are allowed using RXON_ASSOC */
  434. /* Check if we are not switching bands */
  435. CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
  436. active->flags & RXON_FLG_BAND_24G_MSK);
  437. /* Check if we are switching association toggle */
  438. CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
  439. active->filter_flags & RXON_FILTER_ASSOC_MSK);
  440. #undef CHK
  441. #undef CHK_NEQ
  442. return 0;
  443. }
  444. static void _iwl_set_rxon_ht(struct iwl_priv *priv,
  445. struct iwl_ht_config *ht_conf,
  446. struct iwl_rxon_context *ctx)
  447. {
  448. struct iwl_rxon_cmd *rxon = &ctx->staging;
  449. if (!ctx->ht.enabled) {
  450. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  451. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  452. RXON_FLG_HT40_PROT_MSK |
  453. RXON_FLG_HT_PROT_MSK);
  454. return;
  455. }
  456. /* FIXME: if the definition of ht.protection changed, the "translation"
  457. * will be needed for rxon->flags
  458. */
  459. rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
  460. /* Set up channel bandwidth:
  461. * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
  462. /* clear the HT channel mode before set the mode */
  463. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  464. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  465. if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
  466. /* pure ht40 */
  467. if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
  468. rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
  469. /* Note: control channel is opposite of extension channel */
  470. switch (ctx->ht.extension_chan_offset) {
  471. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  472. rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  473. break;
  474. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  475. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  476. break;
  477. }
  478. } else {
  479. /* Note: control channel is opposite of extension channel */
  480. switch (ctx->ht.extension_chan_offset) {
  481. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  482. rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  483. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  484. break;
  485. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  486. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  487. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  488. break;
  489. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  490. default:
  491. /* channel location only valid if in Mixed mode */
  492. IWL_ERR(priv, "invalid extension channel offset\n");
  493. break;
  494. }
  495. }
  496. } else {
  497. rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
  498. }
  499. iwlagn_set_rxon_chain(priv, ctx);
  500. IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
  501. "extension channel offset 0x%x\n",
  502. le32_to_cpu(rxon->flags), ctx->ht.protection,
  503. ctx->ht.extension_chan_offset);
  504. }
  505. void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
  506. {
  507. struct iwl_rxon_context *ctx;
  508. for_each_context(priv, ctx)
  509. _iwl_set_rxon_ht(priv, ht_conf, ctx);
  510. }
  511. /* Return valid, unused, channel for a passive scan to reset the RF */
  512. u8 iwl_get_single_channel_number(struct iwl_priv *priv,
  513. enum ieee80211_band band)
  514. {
  515. const struct iwl_channel_info *ch_info;
  516. int i;
  517. u8 channel = 0;
  518. u8 min, max;
  519. struct iwl_rxon_context *ctx;
  520. if (band == IEEE80211_BAND_5GHZ) {
  521. min = 14;
  522. max = priv->channel_count;
  523. } else {
  524. min = 0;
  525. max = 14;
  526. }
  527. for (i = min; i < max; i++) {
  528. bool busy = false;
  529. for_each_context(priv, ctx) {
  530. busy = priv->channel_info[i].channel ==
  531. le16_to_cpu(ctx->staging.channel);
  532. if (busy)
  533. break;
  534. }
  535. if (busy)
  536. continue;
  537. channel = priv->channel_info[i].channel;
  538. ch_info = iwl_get_channel_info(priv, band, channel);
  539. if (is_channel_valid(ch_info))
  540. break;
  541. }
  542. return channel;
  543. }
  544. /**
  545. * iwl_set_rxon_channel - Set the band and channel values in staging RXON
  546. * @ch: requested channel as a pointer to struct ieee80211_channel
  547. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  548. * in the staging RXON flag structure based on the ch->band
  549. */
  550. int iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
  551. struct iwl_rxon_context *ctx)
  552. {
  553. enum ieee80211_band band = ch->band;
  554. u16 channel = ch->hw_value;
  555. if ((le16_to_cpu(ctx->staging.channel) == channel) &&
  556. (priv->band == band))
  557. return 0;
  558. ctx->staging.channel = cpu_to_le16(channel);
  559. if (band == IEEE80211_BAND_5GHZ)
  560. ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
  561. else
  562. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  563. priv->band = band;
  564. IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
  565. return 0;
  566. }
  567. void iwl_set_flags_for_band(struct iwl_priv *priv,
  568. struct iwl_rxon_context *ctx,
  569. enum ieee80211_band band,
  570. struct ieee80211_vif *vif)
  571. {
  572. if (band == IEEE80211_BAND_5GHZ) {
  573. ctx->staging.flags &=
  574. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
  575. | RXON_FLG_CCK_MSK);
  576. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  577. } else {
  578. /* Copied from iwl_post_associate() */
  579. if (vif && vif->bss_conf.use_short_slot)
  580. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  581. else
  582. ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  583. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  584. ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
  585. ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
  586. }
  587. }
  588. /*
  589. * initialize rxon structure with default values from eeprom
  590. */
  591. void iwl_connection_init_rx_config(struct iwl_priv *priv,
  592. struct iwl_rxon_context *ctx)
  593. {
  594. const struct iwl_channel_info *ch_info;
  595. memset(&ctx->staging, 0, sizeof(ctx->staging));
  596. if (!ctx->vif) {
  597. ctx->staging.dev_type = ctx->unused_devtype;
  598. } else switch (ctx->vif->type) {
  599. case NL80211_IFTYPE_AP:
  600. ctx->staging.dev_type = ctx->ap_devtype;
  601. break;
  602. case NL80211_IFTYPE_STATION:
  603. ctx->staging.dev_type = ctx->station_devtype;
  604. ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  605. break;
  606. case NL80211_IFTYPE_ADHOC:
  607. ctx->staging.dev_type = ctx->ibss_devtype;
  608. ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  609. ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
  610. RXON_FILTER_ACCEPT_GRP_MSK;
  611. break;
  612. default:
  613. IWL_ERR(priv, "Unsupported interface type %d\n",
  614. ctx->vif->type);
  615. break;
  616. }
  617. #if 0
  618. /* TODO: Figure out when short_preamble would be set and cache from
  619. * that */
  620. if (!hw_to_local(priv->hw)->short_preamble)
  621. ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  622. else
  623. ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  624. #endif
  625. ch_info = iwl_get_channel_info(priv, priv->band,
  626. le16_to_cpu(ctx->active.channel));
  627. if (!ch_info)
  628. ch_info = &priv->channel_info[0];
  629. ctx->staging.channel = cpu_to_le16(ch_info->channel);
  630. priv->band = ch_info->band;
  631. iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
  632. ctx->staging.ofdm_basic_rates =
  633. (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  634. ctx->staging.cck_basic_rates =
  635. (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  636. /* clear both MIX and PURE40 mode flag */
  637. ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
  638. RXON_FLG_CHANNEL_MODE_PURE_40);
  639. if (ctx->vif)
  640. memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
  641. ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
  642. ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
  643. ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
  644. }
  645. void iwl_set_rate(struct iwl_priv *priv)
  646. {
  647. const struct ieee80211_supported_band *hw = NULL;
  648. struct ieee80211_rate *rate;
  649. struct iwl_rxon_context *ctx;
  650. int i;
  651. hw = iwl_get_hw_mode(priv, priv->band);
  652. if (!hw) {
  653. IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
  654. return;
  655. }
  656. priv->active_rate = 0;
  657. for (i = 0; i < hw->n_bitrates; i++) {
  658. rate = &(hw->bitrates[i]);
  659. if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
  660. priv->active_rate |= (1 << rate->hw_value);
  661. }
  662. IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
  663. for_each_context(priv, ctx) {
  664. ctx->staging.cck_basic_rates =
  665. (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  666. ctx->staging.ofdm_basic_rates =
  667. (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  668. }
  669. }
  670. void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
  671. {
  672. /*
  673. * MULTI-FIXME
  674. * See iwl_mac_channel_switch.
  675. */
  676. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  677. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  678. return;
  679. if (test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING,
  680. &priv->shrd->status))
  681. ieee80211_chswitch_done(ctx->vif, is_success);
  682. }
  683. #ifdef CONFIG_IWLWIFI_DEBUG
  684. void iwl_print_rx_config_cmd(struct iwl_priv *priv,
  685. struct iwl_rxon_context *ctx)
  686. {
  687. struct iwl_rxon_cmd *rxon = &ctx->staging;
  688. IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
  689. iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  690. IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  691. IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  692. IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
  693. le32_to_cpu(rxon->filter_flags));
  694. IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
  695. IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
  696. rxon->ofdm_basic_rates);
  697. IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  698. IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
  699. IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  700. IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  701. }
  702. #endif
  703. static void iwlagn_abort_notification_waits(struct iwl_priv *priv)
  704. {
  705. unsigned long flags;
  706. struct iwl_notification_wait *wait_entry;
  707. spin_lock_irqsave(&priv->notif_wait_lock, flags);
  708. list_for_each_entry(wait_entry, &priv->notif_waits, list)
  709. wait_entry->aborted = true;
  710. spin_unlock_irqrestore(&priv->notif_wait_lock, flags);
  711. wake_up_all(&priv->notif_waitq);
  712. }
  713. void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
  714. {
  715. unsigned int reload_msec;
  716. unsigned long reload_jiffies;
  717. /* Set the FW error flag -- cleared on iwl_down */
  718. set_bit(STATUS_FW_ERROR, &priv->shrd->status);
  719. /* Cancel currently queued command. */
  720. clear_bit(STATUS_HCMD_ACTIVE, &priv->shrd->status);
  721. iwlagn_abort_notification_waits(priv);
  722. /* Keep the restart process from trying to send host
  723. * commands by clearing the ready bit */
  724. clear_bit(STATUS_READY, &priv->shrd->status);
  725. wake_up_interruptible(&priv->wait_command_queue);
  726. if (!ondemand) {
  727. /*
  728. * If firmware keep reloading, then it indicate something
  729. * serious wrong and firmware having problem to recover
  730. * from it. Instead of keep trying which will fill the syslog
  731. * and hang the system, let's just stop it
  732. */
  733. reload_jiffies = jiffies;
  734. reload_msec = jiffies_to_msecs((long) reload_jiffies -
  735. (long) priv->reload_jiffies);
  736. priv->reload_jiffies = reload_jiffies;
  737. if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
  738. priv->reload_count++;
  739. if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
  740. IWL_ERR(priv, "BUG_ON, Stop restarting\n");
  741. return;
  742. }
  743. } else
  744. priv->reload_count = 0;
  745. }
  746. if (!test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
  747. if (iwlagn_mod_params.restart_fw) {
  748. IWL_DEBUG_FW_ERRORS(priv,
  749. "Restarting adapter due to uCode error.\n");
  750. queue_work(priv->shrd->workqueue, &priv->restart);
  751. } else
  752. IWL_DEBUG_FW_ERRORS(priv,
  753. "Detected FW error, but not restarting\n");
  754. }
  755. }
  756. static int iwl_apm_stop_master(struct iwl_priv *priv)
  757. {
  758. int ret = 0;
  759. /* stop device's busmaster DMA activity */
  760. iwl_set_bit(bus(priv), CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  761. ret = iwl_poll_bit(bus(priv), CSR_RESET,
  762. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  763. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  764. if (ret)
  765. IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
  766. IWL_DEBUG_INFO(priv, "stop master\n");
  767. return ret;
  768. }
  769. void iwl_apm_stop(struct iwl_priv *priv)
  770. {
  771. IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
  772. clear_bit(STATUS_DEVICE_ENABLED, &priv->shrd->status);
  773. /* Stop device's DMA activity */
  774. iwl_apm_stop_master(priv);
  775. /* Reset the entire device */
  776. iwl_set_bit(bus(priv), CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  777. udelay(10);
  778. /*
  779. * Clear "initialization complete" bit to move adapter from
  780. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  781. */
  782. iwl_clear_bit(bus(priv), CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  783. }
  784. /*
  785. * Start up NIC's basic functionality after it has been reset
  786. * (e.g. after platform boot, or shutdown via iwl_apm_stop())
  787. * NOTE: This does not load uCode nor start the embedded processor
  788. */
  789. int iwl_apm_init(struct iwl_priv *priv)
  790. {
  791. int ret = 0;
  792. IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
  793. /*
  794. * Use "set_bit" below rather than "write", to preserve any hardware
  795. * bits already set by default after reset.
  796. */
  797. /* Disable L0S exit timer (platform NMI Work/Around) */
  798. iwl_set_bit(bus(priv), CSR_GIO_CHICKEN_BITS,
  799. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  800. /*
  801. * Disable L0s without affecting L1;
  802. * don't wait for ICH L0s (ICH bug W/A)
  803. */
  804. iwl_set_bit(bus(priv), CSR_GIO_CHICKEN_BITS,
  805. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  806. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  807. iwl_set_bit(bus(priv), CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  808. /*
  809. * Enable HAP INTA (interrupt from management bus) to
  810. * wake device's PCI Express link L1a -> L0s
  811. */
  812. iwl_set_bit(bus(priv), CSR_HW_IF_CONFIG_REG,
  813. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  814. bus_apm_config(priv->bus);
  815. /* Configure analog phase-lock-loop before activating to D0A */
  816. if (priv->cfg->base_params->pll_cfg_val)
  817. iwl_set_bit(bus(priv), CSR_ANA_PLL_CFG,
  818. priv->cfg->base_params->pll_cfg_val);
  819. /*
  820. * Set "initialization complete" bit to move adapter from
  821. * D0U* --> D0A* (powered-up active) state.
  822. */
  823. iwl_set_bit(bus(priv), CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  824. /*
  825. * Wait for clock stabilization; once stabilized, access to
  826. * device-internal resources is supported, e.g. iwl_write_prph()
  827. * and accesses to uCode SRAM.
  828. */
  829. ret = iwl_poll_bit(bus(priv), CSR_GP_CNTRL,
  830. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  831. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  832. if (ret < 0) {
  833. IWL_DEBUG_INFO(priv, "Failed to init the card\n");
  834. goto out;
  835. }
  836. /*
  837. * Enable DMA clock and wait for it to stabilize.
  838. *
  839. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
  840. * do not disable clocks. This preserves any hardware bits already
  841. * set by default in "CLK_CTRL_REG" after reset.
  842. */
  843. iwl_write_prph(bus(priv), APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  844. udelay(20);
  845. /* Disable L1-Active */
  846. iwl_set_bits_prph(bus(priv), APMG_PCIDEV_STT_REG,
  847. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  848. set_bit(STATUS_DEVICE_ENABLED, &priv->shrd->status);
  849. out:
  850. return ret;
  851. }
  852. int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
  853. {
  854. int ret;
  855. s8 prev_tx_power;
  856. bool defer;
  857. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  858. lockdep_assert_held(&priv->shrd->mutex);
  859. if (priv->tx_power_user_lmt == tx_power && !force)
  860. return 0;
  861. if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
  862. IWL_WARN(priv,
  863. "Requested user TXPOWER %d below lower limit %d.\n",
  864. tx_power,
  865. IWLAGN_TX_POWER_TARGET_POWER_MIN);
  866. return -EINVAL;
  867. }
  868. if (tx_power > priv->tx_power_device_lmt) {
  869. IWL_WARN(priv,
  870. "Requested user TXPOWER %d above upper limit %d.\n",
  871. tx_power, priv->tx_power_device_lmt);
  872. return -EINVAL;
  873. }
  874. if (!iwl_is_ready_rf(priv->shrd))
  875. return -EIO;
  876. /* scan complete and commit_rxon use tx_power_next value,
  877. * it always need to be updated for newest request */
  878. priv->tx_power_next = tx_power;
  879. /* do not set tx power when scanning or channel changing */
  880. defer = test_bit(STATUS_SCANNING, &priv->shrd->status) ||
  881. memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
  882. if (defer && !force) {
  883. IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
  884. return 0;
  885. }
  886. prev_tx_power = priv->tx_power_user_lmt;
  887. priv->tx_power_user_lmt = tx_power;
  888. ret = iwlagn_send_tx_power(priv);
  889. /* if fail to set tx_power, restore the orig. tx power */
  890. if (ret) {
  891. priv->tx_power_user_lmt = prev_tx_power;
  892. priv->tx_power_next = prev_tx_power;
  893. }
  894. return ret;
  895. }
  896. void iwl_send_bt_config(struct iwl_priv *priv)
  897. {
  898. struct iwl_bt_cmd bt_cmd = {
  899. .lead_time = BT_LEAD_TIME_DEF,
  900. .max_kill = BT_MAX_KILL_DEF,
  901. .kill_ack_mask = 0,
  902. .kill_cts_mask = 0,
  903. };
  904. if (!iwlagn_mod_params.bt_coex_active)
  905. bt_cmd.flags = BT_COEX_DISABLE;
  906. else
  907. bt_cmd.flags = BT_COEX_ENABLE;
  908. priv->bt_enable_flag = bt_cmd.flags;
  909. IWL_DEBUG_INFO(priv, "BT coex %s\n",
  910. (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
  911. if (iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
  912. CMD_SYNC, sizeof(struct iwl_bt_cmd), &bt_cmd))
  913. IWL_ERR(priv, "failed to send BT Coex Config\n");
  914. }
  915. int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
  916. {
  917. struct iwl_statistics_cmd statistics_cmd = {
  918. .configuration_flags =
  919. clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
  920. };
  921. if (flags & CMD_ASYNC)
  922. return iwl_trans_send_cmd_pdu(trans(priv), REPLY_STATISTICS_CMD,
  923. CMD_ASYNC,
  924. sizeof(struct iwl_statistics_cmd),
  925. &statistics_cmd);
  926. else
  927. return iwl_trans_send_cmd_pdu(trans(priv), REPLY_STATISTICS_CMD,
  928. CMD_SYNC,
  929. sizeof(struct iwl_statistics_cmd),
  930. &statistics_cmd);
  931. }
  932. int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
  933. const struct ieee80211_tx_queue_params *params)
  934. {
  935. struct iwl_priv *priv = hw->priv;
  936. struct iwl_rxon_context *ctx;
  937. unsigned long flags;
  938. int q;
  939. IWL_DEBUG_MAC80211(priv, "enter\n");
  940. if (!iwl_is_ready_rf(priv->shrd)) {
  941. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  942. return -EIO;
  943. }
  944. if (queue >= AC_NUM) {
  945. IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
  946. return 0;
  947. }
  948. q = AC_NUM - 1 - queue;
  949. spin_lock_irqsave(&priv->shrd->lock, flags);
  950. /*
  951. * MULTI-FIXME
  952. * This may need to be done per interface in nl80211/cfg80211/mac80211.
  953. */
  954. for_each_context(priv, ctx) {
  955. ctx->qos_data.def_qos_parm.ac[q].cw_min =
  956. cpu_to_le16(params->cw_min);
  957. ctx->qos_data.def_qos_parm.ac[q].cw_max =
  958. cpu_to_le16(params->cw_max);
  959. ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  960. ctx->qos_data.def_qos_parm.ac[q].edca_txop =
  961. cpu_to_le16((params->txop * 32));
  962. ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  963. }
  964. spin_unlock_irqrestore(&priv->shrd->lock, flags);
  965. IWL_DEBUG_MAC80211(priv, "leave\n");
  966. return 0;
  967. }
  968. int iwl_mac_tx_last_beacon(struct ieee80211_hw *hw)
  969. {
  970. struct iwl_priv *priv = hw->priv;
  971. return priv->ibss_manager == IWL_IBSS_MANAGER;
  972. }
  973. static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  974. {
  975. iwl_connection_init_rx_config(priv, ctx);
  976. iwlagn_set_rxon_chain(priv, ctx);
  977. return iwlagn_commit_rxon(priv, ctx);
  978. }
  979. static int iwl_setup_interface(struct iwl_priv *priv,
  980. struct iwl_rxon_context *ctx)
  981. {
  982. struct ieee80211_vif *vif = ctx->vif;
  983. int err;
  984. lockdep_assert_held(&priv->shrd->mutex);
  985. /*
  986. * This variable will be correct only when there's just
  987. * a single context, but all code using it is for hardware
  988. * that supports only one context.
  989. */
  990. priv->iw_mode = vif->type;
  991. ctx->is_active = true;
  992. err = iwl_set_mode(priv, ctx);
  993. if (err) {
  994. if (!ctx->always_active)
  995. ctx->is_active = false;
  996. return err;
  997. }
  998. if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
  999. vif->type == NL80211_IFTYPE_ADHOC) {
  1000. /*
  1001. * pretend to have high BT traffic as long as we
  1002. * are operating in IBSS mode, as this will cause
  1003. * the rate scaling etc. to behave as intended.
  1004. */
  1005. priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1006. }
  1007. return 0;
  1008. }
  1009. int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1010. {
  1011. struct iwl_priv *priv = hw->priv;
  1012. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1013. struct iwl_rxon_context *tmp, *ctx = NULL;
  1014. int err;
  1015. enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
  1016. IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
  1017. viftype, vif->addr);
  1018. cancel_delayed_work_sync(&priv->hw_roc_disable_work);
  1019. mutex_lock(&priv->shrd->mutex);
  1020. iwlagn_disable_roc(priv);
  1021. if (!iwl_is_ready_rf(priv->shrd)) {
  1022. IWL_WARN(priv, "Try to add interface when device not ready\n");
  1023. err = -EINVAL;
  1024. goto out;
  1025. }
  1026. for_each_context(priv, tmp) {
  1027. u32 possible_modes =
  1028. tmp->interface_modes | tmp->exclusive_interface_modes;
  1029. if (tmp->vif) {
  1030. /* check if this busy context is exclusive */
  1031. if (tmp->exclusive_interface_modes &
  1032. BIT(tmp->vif->type)) {
  1033. err = -EINVAL;
  1034. goto out;
  1035. }
  1036. continue;
  1037. }
  1038. if (!(possible_modes & BIT(viftype)))
  1039. continue;
  1040. /* have maybe usable context w/o interface */
  1041. ctx = tmp;
  1042. break;
  1043. }
  1044. if (!ctx) {
  1045. err = -EOPNOTSUPP;
  1046. goto out;
  1047. }
  1048. vif_priv->ctx = ctx;
  1049. ctx->vif = vif;
  1050. err = iwl_setup_interface(priv, ctx);
  1051. if (!err)
  1052. goto out;
  1053. ctx->vif = NULL;
  1054. priv->iw_mode = NL80211_IFTYPE_STATION;
  1055. out:
  1056. mutex_unlock(&priv->shrd->mutex);
  1057. IWL_DEBUG_MAC80211(priv, "leave\n");
  1058. return err;
  1059. }
  1060. static void iwl_teardown_interface(struct iwl_priv *priv,
  1061. struct ieee80211_vif *vif,
  1062. bool mode_change)
  1063. {
  1064. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1065. lockdep_assert_held(&priv->shrd->mutex);
  1066. if (priv->scan_vif == vif) {
  1067. iwl_scan_cancel_timeout(priv, 200);
  1068. iwl_force_scan_end(priv);
  1069. }
  1070. if (!mode_change) {
  1071. iwl_set_mode(priv, ctx);
  1072. if (!ctx->always_active)
  1073. ctx->is_active = false;
  1074. }
  1075. /*
  1076. * When removing the IBSS interface, overwrite the
  1077. * BT traffic load with the stored one from the last
  1078. * notification, if any. If this is a device that
  1079. * doesn't implement this, this has no effect since
  1080. * both values are the same and zero.
  1081. */
  1082. if (vif->type == NL80211_IFTYPE_ADHOC)
  1083. priv->bt_traffic_load = priv->last_bt_traffic_load;
  1084. }
  1085. void iwl_mac_remove_interface(struct ieee80211_hw *hw,
  1086. struct ieee80211_vif *vif)
  1087. {
  1088. struct iwl_priv *priv = hw->priv;
  1089. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1090. IWL_DEBUG_MAC80211(priv, "enter\n");
  1091. mutex_lock(&priv->shrd->mutex);
  1092. WARN_ON(ctx->vif != vif);
  1093. ctx->vif = NULL;
  1094. iwl_teardown_interface(priv, vif, false);
  1095. mutex_unlock(&priv->shrd->mutex);
  1096. IWL_DEBUG_MAC80211(priv, "leave\n");
  1097. }
  1098. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1099. #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
  1100. void iwl_reset_traffic_log(struct iwl_priv *priv)
  1101. {
  1102. priv->tx_traffic_idx = 0;
  1103. priv->rx_traffic_idx = 0;
  1104. if (priv->tx_traffic)
  1105. memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  1106. if (priv->rx_traffic)
  1107. memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  1108. }
  1109. int iwl_alloc_traffic_mem(struct iwl_priv *priv)
  1110. {
  1111. u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
  1112. if (iwl_get_debug_level(priv->shrd) & IWL_DL_TX) {
  1113. if (!priv->tx_traffic) {
  1114. priv->tx_traffic =
  1115. kzalloc(traffic_size, GFP_KERNEL);
  1116. if (!priv->tx_traffic)
  1117. return -ENOMEM;
  1118. }
  1119. }
  1120. if (iwl_get_debug_level(priv->shrd) & IWL_DL_RX) {
  1121. if (!priv->rx_traffic) {
  1122. priv->rx_traffic =
  1123. kzalloc(traffic_size, GFP_KERNEL);
  1124. if (!priv->rx_traffic)
  1125. return -ENOMEM;
  1126. }
  1127. }
  1128. iwl_reset_traffic_log(priv);
  1129. return 0;
  1130. }
  1131. void iwl_free_traffic_mem(struct iwl_priv *priv)
  1132. {
  1133. kfree(priv->tx_traffic);
  1134. priv->tx_traffic = NULL;
  1135. kfree(priv->rx_traffic);
  1136. priv->rx_traffic = NULL;
  1137. }
  1138. void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
  1139. u16 length, struct ieee80211_hdr *header)
  1140. {
  1141. __le16 fc;
  1142. u16 len;
  1143. if (likely(!(iwl_get_debug_level(priv->shrd) & IWL_DL_TX)))
  1144. return;
  1145. if (!priv->tx_traffic)
  1146. return;
  1147. fc = header->frame_control;
  1148. if (ieee80211_is_data(fc)) {
  1149. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  1150. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  1151. memcpy((priv->tx_traffic +
  1152. (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  1153. header, len);
  1154. priv->tx_traffic_idx =
  1155. (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  1156. }
  1157. }
  1158. void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
  1159. u16 length, struct ieee80211_hdr *header)
  1160. {
  1161. __le16 fc;
  1162. u16 len;
  1163. if (likely(!(iwl_get_debug_level(priv->shrd) & IWL_DL_RX)))
  1164. return;
  1165. if (!priv->rx_traffic)
  1166. return;
  1167. fc = header->frame_control;
  1168. if (ieee80211_is_data(fc)) {
  1169. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  1170. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  1171. memcpy((priv->rx_traffic +
  1172. (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  1173. header, len);
  1174. priv->rx_traffic_idx =
  1175. (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  1176. }
  1177. }
  1178. const char *get_mgmt_string(int cmd)
  1179. {
  1180. switch (cmd) {
  1181. IWL_CMD(MANAGEMENT_ASSOC_REQ);
  1182. IWL_CMD(MANAGEMENT_ASSOC_RESP);
  1183. IWL_CMD(MANAGEMENT_REASSOC_REQ);
  1184. IWL_CMD(MANAGEMENT_REASSOC_RESP);
  1185. IWL_CMD(MANAGEMENT_PROBE_REQ);
  1186. IWL_CMD(MANAGEMENT_PROBE_RESP);
  1187. IWL_CMD(MANAGEMENT_BEACON);
  1188. IWL_CMD(MANAGEMENT_ATIM);
  1189. IWL_CMD(MANAGEMENT_DISASSOC);
  1190. IWL_CMD(MANAGEMENT_AUTH);
  1191. IWL_CMD(MANAGEMENT_DEAUTH);
  1192. IWL_CMD(MANAGEMENT_ACTION);
  1193. default:
  1194. return "UNKNOWN";
  1195. }
  1196. }
  1197. const char *get_ctrl_string(int cmd)
  1198. {
  1199. switch (cmd) {
  1200. IWL_CMD(CONTROL_BACK_REQ);
  1201. IWL_CMD(CONTROL_BACK);
  1202. IWL_CMD(CONTROL_PSPOLL);
  1203. IWL_CMD(CONTROL_RTS);
  1204. IWL_CMD(CONTROL_CTS);
  1205. IWL_CMD(CONTROL_ACK);
  1206. IWL_CMD(CONTROL_CFEND);
  1207. IWL_CMD(CONTROL_CFENDACK);
  1208. default:
  1209. return "UNKNOWN";
  1210. }
  1211. }
  1212. void iwl_clear_traffic_stats(struct iwl_priv *priv)
  1213. {
  1214. memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
  1215. memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
  1216. }
  1217. /*
  1218. * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
  1219. * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
  1220. * Use debugFs to display the rx/rx_statistics
  1221. * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
  1222. * information will be recorded, but DATA pkt still will be recorded
  1223. * for the reason of iwl_led.c need to control the led blinking based on
  1224. * number of tx and rx data.
  1225. *
  1226. */
  1227. void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
  1228. {
  1229. struct traffic_stats *stats;
  1230. if (is_tx)
  1231. stats = &priv->tx_stats;
  1232. else
  1233. stats = &priv->rx_stats;
  1234. if (ieee80211_is_mgmt(fc)) {
  1235. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  1236. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  1237. stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
  1238. break;
  1239. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  1240. stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
  1241. break;
  1242. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  1243. stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
  1244. break;
  1245. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  1246. stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
  1247. break;
  1248. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  1249. stats->mgmt[MANAGEMENT_PROBE_REQ]++;
  1250. break;
  1251. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  1252. stats->mgmt[MANAGEMENT_PROBE_RESP]++;
  1253. break;
  1254. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  1255. stats->mgmt[MANAGEMENT_BEACON]++;
  1256. break;
  1257. case cpu_to_le16(IEEE80211_STYPE_ATIM):
  1258. stats->mgmt[MANAGEMENT_ATIM]++;
  1259. break;
  1260. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  1261. stats->mgmt[MANAGEMENT_DISASSOC]++;
  1262. break;
  1263. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  1264. stats->mgmt[MANAGEMENT_AUTH]++;
  1265. break;
  1266. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  1267. stats->mgmt[MANAGEMENT_DEAUTH]++;
  1268. break;
  1269. case cpu_to_le16(IEEE80211_STYPE_ACTION):
  1270. stats->mgmt[MANAGEMENT_ACTION]++;
  1271. break;
  1272. }
  1273. } else if (ieee80211_is_ctl(fc)) {
  1274. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  1275. case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
  1276. stats->ctrl[CONTROL_BACK_REQ]++;
  1277. break;
  1278. case cpu_to_le16(IEEE80211_STYPE_BACK):
  1279. stats->ctrl[CONTROL_BACK]++;
  1280. break;
  1281. case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
  1282. stats->ctrl[CONTROL_PSPOLL]++;
  1283. break;
  1284. case cpu_to_le16(IEEE80211_STYPE_RTS):
  1285. stats->ctrl[CONTROL_RTS]++;
  1286. break;
  1287. case cpu_to_le16(IEEE80211_STYPE_CTS):
  1288. stats->ctrl[CONTROL_CTS]++;
  1289. break;
  1290. case cpu_to_le16(IEEE80211_STYPE_ACK):
  1291. stats->ctrl[CONTROL_ACK]++;
  1292. break;
  1293. case cpu_to_le16(IEEE80211_STYPE_CFEND):
  1294. stats->ctrl[CONTROL_CFEND]++;
  1295. break;
  1296. case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
  1297. stats->ctrl[CONTROL_CFENDACK]++;
  1298. break;
  1299. }
  1300. } else {
  1301. /* data */
  1302. stats->data_cnt++;
  1303. stats->data_bytes += len;
  1304. }
  1305. }
  1306. #endif
  1307. static void iwl_force_rf_reset(struct iwl_priv *priv)
  1308. {
  1309. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  1310. return;
  1311. if (!iwl_is_any_associated(priv)) {
  1312. IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
  1313. return;
  1314. }
  1315. /*
  1316. * There is no easy and better way to force reset the radio,
  1317. * the only known method is switching channel which will force to
  1318. * reset and tune the radio.
  1319. * Use internal short scan (single channel) operation to should
  1320. * achieve this objective.
  1321. * Driver should reset the radio when number of consecutive missed
  1322. * beacon, or any other uCode error condition detected.
  1323. */
  1324. IWL_DEBUG_INFO(priv, "perform radio reset.\n");
  1325. iwl_internal_short_hw_scan(priv);
  1326. }
  1327. int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
  1328. {
  1329. struct iwl_force_reset *force_reset;
  1330. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  1331. return -EINVAL;
  1332. if (mode >= IWL_MAX_FORCE_RESET) {
  1333. IWL_DEBUG_INFO(priv, "invalid reset request.\n");
  1334. return -EINVAL;
  1335. }
  1336. force_reset = &priv->force_reset[mode];
  1337. force_reset->reset_request_count++;
  1338. if (!external) {
  1339. if (force_reset->last_force_reset_jiffies &&
  1340. time_after(force_reset->last_force_reset_jiffies +
  1341. force_reset->reset_duration, jiffies)) {
  1342. IWL_DEBUG_INFO(priv, "force reset rejected\n");
  1343. force_reset->reset_reject_count++;
  1344. return -EAGAIN;
  1345. }
  1346. }
  1347. force_reset->reset_success_count++;
  1348. force_reset->last_force_reset_jiffies = jiffies;
  1349. IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
  1350. switch (mode) {
  1351. case IWL_RF_RESET:
  1352. iwl_force_rf_reset(priv);
  1353. break;
  1354. case IWL_FW_RESET:
  1355. /*
  1356. * if the request is from external(ex: debugfs),
  1357. * then always perform the request in regardless the module
  1358. * parameter setting
  1359. * if the request is from internal (uCode error or driver
  1360. * detect failure), then fw_restart module parameter
  1361. * need to be check before performing firmware reload
  1362. */
  1363. if (!external && !iwlagn_mod_params.restart_fw) {
  1364. IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
  1365. "module parameter setting\n");
  1366. break;
  1367. }
  1368. IWL_ERR(priv, "On demand firmware reload\n");
  1369. iwlagn_fw_error(priv, true);
  1370. break;
  1371. }
  1372. return 0;
  1373. }
  1374. int iwl_mac_change_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1375. enum nl80211_iftype newtype, bool newp2p)
  1376. {
  1377. struct iwl_priv *priv = hw->priv;
  1378. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1379. struct iwl_rxon_context *bss_ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1380. struct iwl_rxon_context *tmp;
  1381. enum nl80211_iftype newviftype = newtype;
  1382. u32 interface_modes;
  1383. int err;
  1384. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  1385. mutex_lock(&priv->shrd->mutex);
  1386. if (!ctx->vif || !iwl_is_ready_rf(priv->shrd)) {
  1387. /*
  1388. * Huh? But wait ... this can maybe happen when
  1389. * we're in the middle of a firmware restart!
  1390. */
  1391. err = -EBUSY;
  1392. goto out;
  1393. }
  1394. interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
  1395. if (!(interface_modes & BIT(newtype))) {
  1396. err = -EBUSY;
  1397. goto out;
  1398. }
  1399. /*
  1400. * Refuse a change that should be done by moving from the PAN
  1401. * context to the BSS context instead, if the BSS context is
  1402. * available and can support the new interface type.
  1403. */
  1404. if (ctx->ctxid == IWL_RXON_CTX_PAN && !bss_ctx->vif &&
  1405. (bss_ctx->interface_modes & BIT(newtype) ||
  1406. bss_ctx->exclusive_interface_modes & BIT(newtype))) {
  1407. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  1408. err = -EBUSY;
  1409. goto out;
  1410. }
  1411. if (ctx->exclusive_interface_modes & BIT(newtype)) {
  1412. for_each_context(priv, tmp) {
  1413. if (ctx == tmp)
  1414. continue;
  1415. if (!tmp->vif)
  1416. continue;
  1417. /*
  1418. * The current mode switch would be exclusive, but
  1419. * another context is active ... refuse the switch.
  1420. */
  1421. err = -EBUSY;
  1422. goto out;
  1423. }
  1424. }
  1425. /* success */
  1426. iwl_teardown_interface(priv, vif, true);
  1427. vif->type = newviftype;
  1428. vif->p2p = newp2p;
  1429. err = iwl_setup_interface(priv, ctx);
  1430. WARN_ON(err);
  1431. /*
  1432. * We've switched internally, but submitting to the
  1433. * device may have failed for some reason. Mask this
  1434. * error, because otherwise mac80211 will not switch
  1435. * (and set the interface type back) and we'll be
  1436. * out of sync with it.
  1437. */
  1438. err = 0;
  1439. out:
  1440. mutex_unlock(&priv->shrd->mutex);
  1441. return err;
  1442. }
  1443. /*
  1444. * On every watchdog tick we check (latest) time stamp. If it does not
  1445. * change during timeout period and queue is not empty we reset firmware.
  1446. */
  1447. static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
  1448. {
  1449. struct iwl_tx_queue *txq = &priv->txq[cnt];
  1450. struct iwl_queue *q = &txq->q;
  1451. unsigned long timeout;
  1452. int ret;
  1453. if (q->read_ptr == q->write_ptr) {
  1454. txq->time_stamp = jiffies;
  1455. return 0;
  1456. }
  1457. timeout = txq->time_stamp +
  1458. msecs_to_jiffies(priv->cfg->base_params->wd_timeout);
  1459. if (time_after(jiffies, timeout)) {
  1460. IWL_ERR(priv, "Queue %d stuck for %u ms.\n",
  1461. q->id, priv->cfg->base_params->wd_timeout);
  1462. ret = iwl_force_reset(priv, IWL_FW_RESET, false);
  1463. return (ret == -EAGAIN) ? 0 : 1;
  1464. }
  1465. return 0;
  1466. }
  1467. /*
  1468. * Making watchdog tick be a quarter of timeout assure we will
  1469. * discover the queue hung between timeout and 1.25*timeout
  1470. */
  1471. #define IWL_WD_TICK(timeout) ((timeout) / 4)
  1472. /*
  1473. * Watchdog timer callback, we check each tx queue for stuck, if if hung
  1474. * we reset the firmware. If everything is fine just rearm the timer.
  1475. */
  1476. void iwl_bg_watchdog(unsigned long data)
  1477. {
  1478. struct iwl_priv *priv = (struct iwl_priv *)data;
  1479. int cnt;
  1480. unsigned long timeout;
  1481. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  1482. return;
  1483. timeout = priv->cfg->base_params->wd_timeout;
  1484. if (timeout == 0)
  1485. return;
  1486. /* monitor and check for stuck cmd queue */
  1487. if (iwl_check_stuck_queue(priv, priv->shrd->cmd_queue))
  1488. return;
  1489. /* monitor and check for other stuck queues */
  1490. if (iwl_is_any_associated(priv)) {
  1491. for (cnt = 0; cnt < hw_params(priv).max_txq_num; cnt++) {
  1492. /* skip as we already checked the command queue */
  1493. if (cnt == priv->shrd->cmd_queue)
  1494. continue;
  1495. if (iwl_check_stuck_queue(priv, cnt))
  1496. return;
  1497. }
  1498. }
  1499. mod_timer(&priv->watchdog, jiffies +
  1500. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  1501. }
  1502. void iwl_setup_watchdog(struct iwl_priv *priv)
  1503. {
  1504. unsigned int timeout = priv->cfg->base_params->wd_timeout;
  1505. if (timeout && !iwlagn_mod_params.wd_disable)
  1506. mod_timer(&priv->watchdog,
  1507. jiffies + msecs_to_jiffies(IWL_WD_TICK(timeout)));
  1508. else
  1509. del_timer(&priv->watchdog);
  1510. }
  1511. /*
  1512. * extended beacon time format
  1513. * time in usec will be changed into a 32-bit value in extended:internal format
  1514. * the extended part is the beacon counts
  1515. * the internal part is the time in usec within one beacon interval
  1516. */
  1517. u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
  1518. {
  1519. u32 quot;
  1520. u32 rem;
  1521. u32 interval = beacon_interval * TIME_UNIT;
  1522. if (!interval || !usec)
  1523. return 0;
  1524. quot = (usec / interval) &
  1525. (iwl_beacon_time_mask_high(priv,
  1526. hw_params(priv).beacon_time_tsf_bits) >>
  1527. hw_params(priv).beacon_time_tsf_bits);
  1528. rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
  1529. hw_params(priv).beacon_time_tsf_bits);
  1530. return (quot << hw_params(priv).beacon_time_tsf_bits) + rem;
  1531. }
  1532. /* base is usually what we get from ucode with each received frame,
  1533. * the same as HW timer counter counting down
  1534. */
  1535. __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
  1536. u32 addon, u32 beacon_interval)
  1537. {
  1538. u32 base_low = base & iwl_beacon_time_mask_low(priv,
  1539. hw_params(priv).beacon_time_tsf_bits);
  1540. u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
  1541. hw_params(priv).beacon_time_tsf_bits);
  1542. u32 interval = beacon_interval * TIME_UNIT;
  1543. u32 res = (base & iwl_beacon_time_mask_high(priv,
  1544. hw_params(priv).beacon_time_tsf_bits)) +
  1545. (addon & iwl_beacon_time_mask_high(priv,
  1546. hw_params(priv).beacon_time_tsf_bits));
  1547. if (base_low > addon_low)
  1548. res += base_low - addon_low;
  1549. else if (base_low < addon_low) {
  1550. res += interval + base_low - addon_low;
  1551. res += (1 << hw_params(priv).beacon_time_tsf_bits);
  1552. } else
  1553. res += (1 << hw_params(priv).beacon_time_tsf_bits);
  1554. return cpu_to_le32(res);
  1555. }