iwl-core.c 55 KB

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