iwl-core.c 52 KB

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