iwl-core.c 58 KB

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