iwl-core.c 52 KB

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