iwl-core.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  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. void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
  751. {
  752. unsigned int reload_msec;
  753. unsigned long reload_jiffies;
  754. /* Set the FW error flag -- cleared on iwl_down */
  755. set_bit(STATUS_FW_ERROR, &priv->status);
  756. /* Cancel currently queued command. */
  757. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  758. /* Keep the restart process from trying to send host
  759. * commands by clearing the ready bit */
  760. clear_bit(STATUS_READY, &priv->status);
  761. wake_up_interruptible(&priv->wait_command_queue);
  762. if (!ondemand) {
  763. /*
  764. * If firmware keep reloading, then it indicate something
  765. * serious wrong and firmware having problem to recover
  766. * from it. Instead of keep trying which will fill the syslog
  767. * and hang the system, let's just stop it
  768. */
  769. reload_jiffies = jiffies;
  770. reload_msec = jiffies_to_msecs((long) reload_jiffies -
  771. (long) priv->reload_jiffies);
  772. priv->reload_jiffies = reload_jiffies;
  773. if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
  774. priv->reload_count++;
  775. if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
  776. IWL_ERR(priv, "BUG_ON, Stop restarting\n");
  777. return;
  778. }
  779. } else
  780. priv->reload_count = 0;
  781. }
  782. if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  783. if (priv->cfg->mod_params->restart_fw) {
  784. IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
  785. "Restarting adapter due to uCode error.\n");
  786. queue_work(priv->workqueue, &priv->restart);
  787. } else
  788. IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
  789. "Detected FW error, but not restarting\n");
  790. }
  791. }
  792. /**
  793. * iwl_irq_handle_error - called for HW or SW error interrupt from card
  794. */
  795. void iwl_irq_handle_error(struct iwl_priv *priv)
  796. {
  797. /* W/A for WiFi/WiMAX coex and WiMAX own the RF */
  798. if (priv->cfg->internal_wimax_coex &&
  799. (!(iwl_read_prph(priv, APMG_CLK_CTRL_REG) &
  800. APMS_CLK_VAL_MRB_FUNC_MODE) ||
  801. (iwl_read_prph(priv, APMG_PS_CTRL_REG) &
  802. APMG_PS_CTRL_VAL_RESET_REQ))) {
  803. /*
  804. * Keep the restart process from trying to send host
  805. * commands by clearing the ready bit.
  806. */
  807. clear_bit(STATUS_READY, &priv->status);
  808. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  809. wake_up_interruptible(&priv->wait_command_queue);
  810. IWL_ERR(priv, "RF is used by WiMAX\n");
  811. return;
  812. }
  813. IWL_ERR(priv, "Loaded firmware version: %s\n",
  814. priv->hw->wiphy->fw_version);
  815. priv->cfg->ops->lib->dump_nic_error_log(priv);
  816. if (priv->cfg->ops->lib->dump_csr)
  817. priv->cfg->ops->lib->dump_csr(priv);
  818. if (priv->cfg->ops->lib->dump_fh)
  819. priv->cfg->ops->lib->dump_fh(priv, NULL, false);
  820. priv->cfg->ops->lib->dump_nic_event_log(priv, false, NULL, false);
  821. #ifdef CONFIG_IWLWIFI_DEBUG
  822. if (iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS)
  823. iwl_print_rx_config_cmd(priv,
  824. &priv->contexts[IWL_RXON_CTX_BSS]);
  825. #endif
  826. iwlagn_fw_error(priv, false);
  827. }
  828. static int iwl_apm_stop_master(struct iwl_priv *priv)
  829. {
  830. int ret = 0;
  831. /* stop device's busmaster DMA activity */
  832. iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  833. ret = iwl_poll_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
  834. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  835. if (ret)
  836. IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
  837. IWL_DEBUG_INFO(priv, "stop master\n");
  838. return ret;
  839. }
  840. void iwl_apm_stop(struct iwl_priv *priv)
  841. {
  842. IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
  843. /* Stop device's DMA activity */
  844. iwl_apm_stop_master(priv);
  845. /* Reset the entire device */
  846. iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  847. udelay(10);
  848. /*
  849. * Clear "initialization complete" bit to move adapter from
  850. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  851. */
  852. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  853. }
  854. /*
  855. * Start up NIC's basic functionality after it has been reset
  856. * (e.g. after platform boot, or shutdown via iwl_apm_stop())
  857. * NOTE: This does not load uCode nor start the embedded processor
  858. */
  859. int iwl_apm_init(struct iwl_priv *priv)
  860. {
  861. int ret = 0;
  862. u16 lctl;
  863. IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
  864. /*
  865. * Use "set_bit" below rather than "write", to preserve any hardware
  866. * bits already set by default after reset.
  867. */
  868. /* Disable L0S exit timer (platform NMI Work/Around) */
  869. iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
  870. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  871. /*
  872. * Disable L0s without affecting L1;
  873. * don't wait for ICH L0s (ICH bug W/A)
  874. */
  875. iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
  876. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  877. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  878. iwl_set_bit(priv, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  879. /*
  880. * Enable HAP INTA (interrupt from management bus) to
  881. * wake device's PCI Express link L1a -> L0s
  882. * NOTE: This is no-op for 3945 (non-existant bit)
  883. */
  884. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  885. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  886. /*
  887. * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
  888. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  889. * If so (likely), disable L0S, so device moves directly L0->L1;
  890. * costs negligible amount of power savings.
  891. * If not (unlikely), enable L0S, so there is at least some
  892. * power savings, even without L1.
  893. */
  894. if (priv->cfg->base_params->set_l0s) {
  895. lctl = iwl_pcie_link_ctl(priv);
  896. if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
  897. PCI_CFG_LINK_CTRL_VAL_L1_EN) {
  898. /* L1-ASPM enabled; disable(!) L0S */
  899. iwl_set_bit(priv, CSR_GIO_REG,
  900. CSR_GIO_REG_VAL_L0S_ENABLED);
  901. IWL_DEBUG_POWER(priv, "L1 Enabled; Disabling L0S\n");
  902. } else {
  903. /* L1-ASPM disabled; enable(!) L0S */
  904. iwl_clear_bit(priv, CSR_GIO_REG,
  905. CSR_GIO_REG_VAL_L0S_ENABLED);
  906. IWL_DEBUG_POWER(priv, "L1 Disabled; Enabling L0S\n");
  907. }
  908. }
  909. /* Configure analog phase-lock-loop before activating to D0A */
  910. if (priv->cfg->base_params->pll_cfg_val)
  911. iwl_set_bit(priv, CSR_ANA_PLL_CFG,
  912. priv->cfg->base_params->pll_cfg_val);
  913. /*
  914. * Set "initialization complete" bit to move adapter from
  915. * D0U* --> D0A* (powered-up active) state.
  916. */
  917. iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  918. /*
  919. * Wait for clock stabilization; once stabilized, access to
  920. * device-internal resources is supported, e.g. iwl_write_prph()
  921. * and accesses to uCode SRAM.
  922. */
  923. ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
  924. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  925. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  926. if (ret < 0) {
  927. IWL_DEBUG_INFO(priv, "Failed to init the card\n");
  928. goto out;
  929. }
  930. /*
  931. * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
  932. * BSM (Boostrap State Machine) is only in 3945 and 4965;
  933. * later devices (i.e. 5000 and later) have non-volatile SRAM,
  934. * and don't need BSM to restore data after power-saving sleep.
  935. *
  936. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
  937. * do not disable clocks. This preserves any hardware bits already
  938. * set by default in "CLK_CTRL_REG" after reset.
  939. */
  940. if (priv->cfg->base_params->use_bsm)
  941. iwl_write_prph(priv, APMG_CLK_EN_REG,
  942. APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
  943. else
  944. iwl_write_prph(priv, APMG_CLK_EN_REG,
  945. APMG_CLK_VAL_DMA_CLK_RQT);
  946. udelay(20);
  947. /* Disable L1-Active */
  948. iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
  949. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  950. out:
  951. return ret;
  952. }
  953. int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
  954. {
  955. int ret;
  956. s8 prev_tx_power;
  957. bool defer;
  958. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  959. lockdep_assert_held(&priv->mutex);
  960. if (priv->tx_power_user_lmt == tx_power && !force)
  961. return 0;
  962. if (!priv->cfg->ops->lib->send_tx_power)
  963. return -EOPNOTSUPP;
  964. if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
  965. IWL_WARN(priv,
  966. "Requested user TXPOWER %d below lower limit %d.\n",
  967. tx_power,
  968. IWLAGN_TX_POWER_TARGET_POWER_MIN);
  969. return -EINVAL;
  970. }
  971. if (tx_power > priv->tx_power_device_lmt) {
  972. IWL_WARN(priv,
  973. "Requested user TXPOWER %d above upper limit %d.\n",
  974. tx_power, priv->tx_power_device_lmt);
  975. return -EINVAL;
  976. }
  977. if (!iwl_is_ready_rf(priv))
  978. return -EIO;
  979. /* scan complete and commit_rxon use tx_power_next value,
  980. * it always need to be updated for newest request */
  981. priv->tx_power_next = tx_power;
  982. /* do not set tx power when scanning or channel changing */
  983. defer = test_bit(STATUS_SCANNING, &priv->status) ||
  984. memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
  985. if (defer && !force) {
  986. IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
  987. return 0;
  988. }
  989. prev_tx_power = priv->tx_power_user_lmt;
  990. priv->tx_power_user_lmt = tx_power;
  991. ret = priv->cfg->ops->lib->send_tx_power(priv);
  992. /* if fail to set tx_power, restore the orig. tx power */
  993. if (ret) {
  994. priv->tx_power_user_lmt = prev_tx_power;
  995. priv->tx_power_next = prev_tx_power;
  996. }
  997. return ret;
  998. }
  999. void iwl_send_bt_config(struct iwl_priv *priv)
  1000. {
  1001. struct iwl_bt_cmd bt_cmd = {
  1002. .lead_time = BT_LEAD_TIME_DEF,
  1003. .max_kill = BT_MAX_KILL_DEF,
  1004. .kill_ack_mask = 0,
  1005. .kill_cts_mask = 0,
  1006. };
  1007. if (!bt_coex_active)
  1008. bt_cmd.flags = BT_COEX_DISABLE;
  1009. else
  1010. bt_cmd.flags = BT_COEX_ENABLE;
  1011. priv->bt_enable_flag = bt_cmd.flags;
  1012. IWL_DEBUG_INFO(priv, "BT coex %s\n",
  1013. (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
  1014. if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  1015. sizeof(struct iwl_bt_cmd), &bt_cmd))
  1016. IWL_ERR(priv, "failed to send BT Coex Config\n");
  1017. }
  1018. int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
  1019. {
  1020. struct iwl_statistics_cmd statistics_cmd = {
  1021. .configuration_flags =
  1022. clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
  1023. };
  1024. if (flags & CMD_ASYNC)
  1025. return iwl_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
  1026. sizeof(struct iwl_statistics_cmd),
  1027. &statistics_cmd, NULL);
  1028. else
  1029. return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
  1030. sizeof(struct iwl_statistics_cmd),
  1031. &statistics_cmd);
  1032. }
  1033. void iwl_clear_isr_stats(struct iwl_priv *priv)
  1034. {
  1035. memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
  1036. }
  1037. int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1038. const struct ieee80211_tx_queue_params *params)
  1039. {
  1040. struct iwl_priv *priv = hw->priv;
  1041. struct iwl_rxon_context *ctx;
  1042. unsigned long flags;
  1043. int q;
  1044. IWL_DEBUG_MAC80211(priv, "enter\n");
  1045. if (!iwl_is_ready_rf(priv)) {
  1046. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  1047. return -EIO;
  1048. }
  1049. if (queue >= AC_NUM) {
  1050. IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
  1051. return 0;
  1052. }
  1053. q = AC_NUM - 1 - queue;
  1054. spin_lock_irqsave(&priv->lock, flags);
  1055. /*
  1056. * MULTI-FIXME
  1057. * This may need to be done per interface in nl80211/cfg80211/mac80211.
  1058. */
  1059. for_each_context(priv, ctx) {
  1060. ctx->qos_data.def_qos_parm.ac[q].cw_min =
  1061. cpu_to_le16(params->cw_min);
  1062. ctx->qos_data.def_qos_parm.ac[q].cw_max =
  1063. cpu_to_le16(params->cw_max);
  1064. ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  1065. ctx->qos_data.def_qos_parm.ac[q].edca_txop =
  1066. cpu_to_le16((params->txop * 32));
  1067. ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  1068. }
  1069. spin_unlock_irqrestore(&priv->lock, flags);
  1070. IWL_DEBUG_MAC80211(priv, "leave\n");
  1071. return 0;
  1072. }
  1073. int iwl_mac_tx_last_beacon(struct ieee80211_hw *hw)
  1074. {
  1075. struct iwl_priv *priv = hw->priv;
  1076. return priv->ibss_manager == IWL_IBSS_MANAGER;
  1077. }
  1078. static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  1079. {
  1080. iwl_connection_init_rx_config(priv, ctx);
  1081. if (priv->cfg->ops->hcmd->set_rxon_chain)
  1082. priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
  1083. return iwlcore_commit_rxon(priv, ctx);
  1084. }
  1085. static int iwl_setup_interface(struct iwl_priv *priv,
  1086. struct iwl_rxon_context *ctx)
  1087. {
  1088. struct ieee80211_vif *vif = ctx->vif;
  1089. int err;
  1090. lockdep_assert_held(&priv->mutex);
  1091. /*
  1092. * This variable will be correct only when there's just
  1093. * a single context, but all code using it is for hardware
  1094. * that supports only one context.
  1095. */
  1096. priv->iw_mode = vif->type;
  1097. ctx->is_active = true;
  1098. err = iwl_set_mode(priv, ctx);
  1099. if (err) {
  1100. if (!ctx->always_active)
  1101. ctx->is_active = false;
  1102. return err;
  1103. }
  1104. if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
  1105. vif->type == NL80211_IFTYPE_ADHOC) {
  1106. /*
  1107. * pretend to have high BT traffic as long as we
  1108. * are operating in IBSS mode, as this will cause
  1109. * the rate scaling etc. to behave as intended.
  1110. */
  1111. priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1112. }
  1113. return 0;
  1114. }
  1115. int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1116. {
  1117. struct iwl_priv *priv = hw->priv;
  1118. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1119. struct iwl_rxon_context *tmp, *ctx = NULL;
  1120. int err;
  1121. enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
  1122. IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
  1123. viftype, vif->addr);
  1124. mutex_lock(&priv->mutex);
  1125. if (!iwl_is_ready_rf(priv)) {
  1126. IWL_WARN(priv, "Try to add interface when device not ready\n");
  1127. err = -EINVAL;
  1128. goto out;
  1129. }
  1130. for_each_context(priv, tmp) {
  1131. u32 possible_modes =
  1132. tmp->interface_modes | tmp->exclusive_interface_modes;
  1133. if (tmp->vif) {
  1134. /* check if this busy context is exclusive */
  1135. if (tmp->exclusive_interface_modes &
  1136. BIT(tmp->vif->type)) {
  1137. err = -EINVAL;
  1138. goto out;
  1139. }
  1140. continue;
  1141. }
  1142. if (!(possible_modes & BIT(viftype)))
  1143. continue;
  1144. /* have maybe usable context w/o interface */
  1145. ctx = tmp;
  1146. break;
  1147. }
  1148. if (!ctx) {
  1149. err = -EOPNOTSUPP;
  1150. goto out;
  1151. }
  1152. vif_priv->ctx = ctx;
  1153. ctx->vif = vif;
  1154. err = iwl_setup_interface(priv, ctx);
  1155. if (!err)
  1156. goto out;
  1157. ctx->vif = NULL;
  1158. priv->iw_mode = NL80211_IFTYPE_STATION;
  1159. out:
  1160. mutex_unlock(&priv->mutex);
  1161. IWL_DEBUG_MAC80211(priv, "leave\n");
  1162. return err;
  1163. }
  1164. static void iwl_teardown_interface(struct iwl_priv *priv,
  1165. struct ieee80211_vif *vif,
  1166. bool mode_change)
  1167. {
  1168. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1169. lockdep_assert_held(&priv->mutex);
  1170. if (priv->scan_vif == vif) {
  1171. iwl_scan_cancel_timeout(priv, 200);
  1172. iwl_force_scan_end(priv);
  1173. }
  1174. if (!mode_change) {
  1175. iwl_set_mode(priv, ctx);
  1176. if (!ctx->always_active)
  1177. ctx->is_active = false;
  1178. }
  1179. /*
  1180. * When removing the IBSS interface, overwrite the
  1181. * BT traffic load with the stored one from the last
  1182. * notification, if any. If this is a device that
  1183. * doesn't implement this, this has no effect since
  1184. * both values are the same and zero.
  1185. */
  1186. if (vif->type == NL80211_IFTYPE_ADHOC)
  1187. priv->bt_traffic_load = priv->last_bt_traffic_load;
  1188. }
  1189. void iwl_mac_remove_interface(struct ieee80211_hw *hw,
  1190. struct ieee80211_vif *vif)
  1191. {
  1192. struct iwl_priv *priv = hw->priv;
  1193. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1194. IWL_DEBUG_MAC80211(priv, "enter\n");
  1195. mutex_lock(&priv->mutex);
  1196. WARN_ON(ctx->vif != vif);
  1197. ctx->vif = NULL;
  1198. iwl_teardown_interface(priv, vif, false);
  1199. mutex_unlock(&priv->mutex);
  1200. IWL_DEBUG_MAC80211(priv, "leave\n");
  1201. }
  1202. int iwl_alloc_txq_mem(struct iwl_priv *priv)
  1203. {
  1204. if (!priv->txq)
  1205. priv->txq = kzalloc(
  1206. sizeof(struct iwl_tx_queue) *
  1207. priv->cfg->base_params->num_of_queues,
  1208. GFP_KERNEL);
  1209. if (!priv->txq) {
  1210. IWL_ERR(priv, "Not enough memory for txq\n");
  1211. return -ENOMEM;
  1212. }
  1213. return 0;
  1214. }
  1215. void iwl_free_txq_mem(struct iwl_priv *priv)
  1216. {
  1217. kfree(priv->txq);
  1218. priv->txq = NULL;
  1219. }
  1220. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1221. #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
  1222. void iwl_reset_traffic_log(struct iwl_priv *priv)
  1223. {
  1224. priv->tx_traffic_idx = 0;
  1225. priv->rx_traffic_idx = 0;
  1226. if (priv->tx_traffic)
  1227. memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  1228. if (priv->rx_traffic)
  1229. memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  1230. }
  1231. int iwl_alloc_traffic_mem(struct iwl_priv *priv)
  1232. {
  1233. u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
  1234. if (iwl_debug_level & IWL_DL_TX) {
  1235. if (!priv->tx_traffic) {
  1236. priv->tx_traffic =
  1237. kzalloc(traffic_size, GFP_KERNEL);
  1238. if (!priv->tx_traffic)
  1239. return -ENOMEM;
  1240. }
  1241. }
  1242. if (iwl_debug_level & IWL_DL_RX) {
  1243. if (!priv->rx_traffic) {
  1244. priv->rx_traffic =
  1245. kzalloc(traffic_size, GFP_KERNEL);
  1246. if (!priv->rx_traffic)
  1247. return -ENOMEM;
  1248. }
  1249. }
  1250. iwl_reset_traffic_log(priv);
  1251. return 0;
  1252. }
  1253. void iwl_free_traffic_mem(struct iwl_priv *priv)
  1254. {
  1255. kfree(priv->tx_traffic);
  1256. priv->tx_traffic = NULL;
  1257. kfree(priv->rx_traffic);
  1258. priv->rx_traffic = NULL;
  1259. }
  1260. void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
  1261. u16 length, struct ieee80211_hdr *header)
  1262. {
  1263. __le16 fc;
  1264. u16 len;
  1265. if (likely(!(iwl_debug_level & IWL_DL_TX)))
  1266. return;
  1267. if (!priv->tx_traffic)
  1268. return;
  1269. fc = header->frame_control;
  1270. if (ieee80211_is_data(fc)) {
  1271. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  1272. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  1273. memcpy((priv->tx_traffic +
  1274. (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  1275. header, len);
  1276. priv->tx_traffic_idx =
  1277. (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  1278. }
  1279. }
  1280. void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
  1281. u16 length, struct ieee80211_hdr *header)
  1282. {
  1283. __le16 fc;
  1284. u16 len;
  1285. if (likely(!(iwl_debug_level & IWL_DL_RX)))
  1286. return;
  1287. if (!priv->rx_traffic)
  1288. return;
  1289. fc = header->frame_control;
  1290. if (ieee80211_is_data(fc)) {
  1291. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  1292. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  1293. memcpy((priv->rx_traffic +
  1294. (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  1295. header, len);
  1296. priv->rx_traffic_idx =
  1297. (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  1298. }
  1299. }
  1300. const char *get_mgmt_string(int cmd)
  1301. {
  1302. switch (cmd) {
  1303. IWL_CMD(MANAGEMENT_ASSOC_REQ);
  1304. IWL_CMD(MANAGEMENT_ASSOC_RESP);
  1305. IWL_CMD(MANAGEMENT_REASSOC_REQ);
  1306. IWL_CMD(MANAGEMENT_REASSOC_RESP);
  1307. IWL_CMD(MANAGEMENT_PROBE_REQ);
  1308. IWL_CMD(MANAGEMENT_PROBE_RESP);
  1309. IWL_CMD(MANAGEMENT_BEACON);
  1310. IWL_CMD(MANAGEMENT_ATIM);
  1311. IWL_CMD(MANAGEMENT_DISASSOC);
  1312. IWL_CMD(MANAGEMENT_AUTH);
  1313. IWL_CMD(MANAGEMENT_DEAUTH);
  1314. IWL_CMD(MANAGEMENT_ACTION);
  1315. default:
  1316. return "UNKNOWN";
  1317. }
  1318. }
  1319. const char *get_ctrl_string(int cmd)
  1320. {
  1321. switch (cmd) {
  1322. IWL_CMD(CONTROL_BACK_REQ);
  1323. IWL_CMD(CONTROL_BACK);
  1324. IWL_CMD(CONTROL_PSPOLL);
  1325. IWL_CMD(CONTROL_RTS);
  1326. IWL_CMD(CONTROL_CTS);
  1327. IWL_CMD(CONTROL_ACK);
  1328. IWL_CMD(CONTROL_CFEND);
  1329. IWL_CMD(CONTROL_CFENDACK);
  1330. default:
  1331. return "UNKNOWN";
  1332. }
  1333. }
  1334. void iwl_clear_traffic_stats(struct iwl_priv *priv)
  1335. {
  1336. memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
  1337. memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
  1338. }
  1339. /*
  1340. * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
  1341. * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
  1342. * Use debugFs to display the rx/rx_statistics
  1343. * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
  1344. * information will be recorded, but DATA pkt still will be recorded
  1345. * for the reason of iwl_led.c need to control the led blinking based on
  1346. * number of tx and rx data.
  1347. *
  1348. */
  1349. void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
  1350. {
  1351. struct traffic_stats *stats;
  1352. if (is_tx)
  1353. stats = &priv->tx_stats;
  1354. else
  1355. stats = &priv->rx_stats;
  1356. if (ieee80211_is_mgmt(fc)) {
  1357. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  1358. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  1359. stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
  1360. break;
  1361. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  1362. stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
  1363. break;
  1364. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  1365. stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
  1366. break;
  1367. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  1368. stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
  1369. break;
  1370. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  1371. stats->mgmt[MANAGEMENT_PROBE_REQ]++;
  1372. break;
  1373. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  1374. stats->mgmt[MANAGEMENT_PROBE_RESP]++;
  1375. break;
  1376. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  1377. stats->mgmt[MANAGEMENT_BEACON]++;
  1378. break;
  1379. case cpu_to_le16(IEEE80211_STYPE_ATIM):
  1380. stats->mgmt[MANAGEMENT_ATIM]++;
  1381. break;
  1382. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  1383. stats->mgmt[MANAGEMENT_DISASSOC]++;
  1384. break;
  1385. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  1386. stats->mgmt[MANAGEMENT_AUTH]++;
  1387. break;
  1388. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  1389. stats->mgmt[MANAGEMENT_DEAUTH]++;
  1390. break;
  1391. case cpu_to_le16(IEEE80211_STYPE_ACTION):
  1392. stats->mgmt[MANAGEMENT_ACTION]++;
  1393. break;
  1394. }
  1395. } else if (ieee80211_is_ctl(fc)) {
  1396. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  1397. case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
  1398. stats->ctrl[CONTROL_BACK_REQ]++;
  1399. break;
  1400. case cpu_to_le16(IEEE80211_STYPE_BACK):
  1401. stats->ctrl[CONTROL_BACK]++;
  1402. break;
  1403. case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
  1404. stats->ctrl[CONTROL_PSPOLL]++;
  1405. break;
  1406. case cpu_to_le16(IEEE80211_STYPE_RTS):
  1407. stats->ctrl[CONTROL_RTS]++;
  1408. break;
  1409. case cpu_to_le16(IEEE80211_STYPE_CTS):
  1410. stats->ctrl[CONTROL_CTS]++;
  1411. break;
  1412. case cpu_to_le16(IEEE80211_STYPE_ACK):
  1413. stats->ctrl[CONTROL_ACK]++;
  1414. break;
  1415. case cpu_to_le16(IEEE80211_STYPE_CFEND):
  1416. stats->ctrl[CONTROL_CFEND]++;
  1417. break;
  1418. case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
  1419. stats->ctrl[CONTROL_CFENDACK]++;
  1420. break;
  1421. }
  1422. } else {
  1423. /* data */
  1424. stats->data_cnt++;
  1425. stats->data_bytes += len;
  1426. }
  1427. }
  1428. #endif
  1429. static void iwl_force_rf_reset(struct iwl_priv *priv)
  1430. {
  1431. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1432. return;
  1433. if (!iwl_is_any_associated(priv)) {
  1434. IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
  1435. return;
  1436. }
  1437. /*
  1438. * There is no easy and better way to force reset the radio,
  1439. * the only known method is switching channel which will force to
  1440. * reset and tune the radio.
  1441. * Use internal short scan (single channel) operation to should
  1442. * achieve this objective.
  1443. * Driver should reset the radio when number of consecutive missed
  1444. * beacon, or any other uCode error condition detected.
  1445. */
  1446. IWL_DEBUG_INFO(priv, "perform radio reset.\n");
  1447. iwl_internal_short_hw_scan(priv);
  1448. }
  1449. int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
  1450. {
  1451. struct iwl_force_reset *force_reset;
  1452. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1453. return -EINVAL;
  1454. if (mode >= IWL_MAX_FORCE_RESET) {
  1455. IWL_DEBUG_INFO(priv, "invalid reset request.\n");
  1456. return -EINVAL;
  1457. }
  1458. force_reset = &priv->force_reset[mode];
  1459. force_reset->reset_request_count++;
  1460. if (!external) {
  1461. if (force_reset->last_force_reset_jiffies &&
  1462. time_after(force_reset->last_force_reset_jiffies +
  1463. force_reset->reset_duration, jiffies)) {
  1464. IWL_DEBUG_INFO(priv, "force reset rejected\n");
  1465. force_reset->reset_reject_count++;
  1466. return -EAGAIN;
  1467. }
  1468. }
  1469. force_reset->reset_success_count++;
  1470. force_reset->last_force_reset_jiffies = jiffies;
  1471. IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
  1472. switch (mode) {
  1473. case IWL_RF_RESET:
  1474. iwl_force_rf_reset(priv);
  1475. break;
  1476. case IWL_FW_RESET:
  1477. /*
  1478. * if the request is from external(ex: debugfs),
  1479. * then always perform the request in regardless the module
  1480. * parameter setting
  1481. * if the request is from internal (uCode error or driver
  1482. * detect failure), then fw_restart module parameter
  1483. * need to be check before performing firmware reload
  1484. */
  1485. if (!external && !priv->cfg->mod_params->restart_fw) {
  1486. IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
  1487. "module parameter setting\n");
  1488. break;
  1489. }
  1490. IWL_ERR(priv, "On demand firmware reload\n");
  1491. iwlagn_fw_error(priv, true);
  1492. break;
  1493. }
  1494. return 0;
  1495. }
  1496. int iwl_mac_change_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1497. enum nl80211_iftype newtype, bool newp2p)
  1498. {
  1499. struct iwl_priv *priv = hw->priv;
  1500. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1501. struct iwl_rxon_context *tmp;
  1502. u32 interface_modes;
  1503. int err;
  1504. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  1505. mutex_lock(&priv->mutex);
  1506. interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
  1507. if (!(interface_modes & BIT(newtype))) {
  1508. err = -EBUSY;
  1509. goto out;
  1510. }
  1511. if (ctx->exclusive_interface_modes & BIT(newtype)) {
  1512. for_each_context(priv, tmp) {
  1513. if (ctx == tmp)
  1514. continue;
  1515. if (!tmp->vif)
  1516. continue;
  1517. /*
  1518. * The current mode switch would be exclusive, but
  1519. * another context is active ... refuse the switch.
  1520. */
  1521. err = -EBUSY;
  1522. goto out;
  1523. }
  1524. }
  1525. /* success */
  1526. iwl_teardown_interface(priv, vif, true);
  1527. vif->type = newtype;
  1528. err = iwl_setup_interface(priv, ctx);
  1529. WARN_ON(err);
  1530. /*
  1531. * We've switched internally, but submitting to the
  1532. * device may have failed for some reason. Mask this
  1533. * error, because otherwise mac80211 will not switch
  1534. * (and set the interface type back) and we'll be
  1535. * out of sync with it.
  1536. */
  1537. err = 0;
  1538. out:
  1539. mutex_unlock(&priv->mutex);
  1540. return err;
  1541. }
  1542. /*
  1543. * On every watchdog tick we check (latest) time stamp. If it does not
  1544. * change during timeout period and queue is not empty we reset firmware.
  1545. */
  1546. static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
  1547. {
  1548. struct iwl_tx_queue *txq = &priv->txq[cnt];
  1549. struct iwl_queue *q = &txq->q;
  1550. unsigned long timeout;
  1551. int ret;
  1552. if (q->read_ptr == q->write_ptr) {
  1553. txq->time_stamp = jiffies;
  1554. return 0;
  1555. }
  1556. timeout = txq->time_stamp +
  1557. msecs_to_jiffies(priv->cfg->base_params->wd_timeout);
  1558. if (time_after(jiffies, timeout)) {
  1559. IWL_ERR(priv, "Queue %d stuck for %u ms.\n",
  1560. q->id, priv->cfg->base_params->wd_timeout);
  1561. ret = iwl_force_reset(priv, IWL_FW_RESET, false);
  1562. return (ret == -EAGAIN) ? 0 : 1;
  1563. }
  1564. return 0;
  1565. }
  1566. /*
  1567. * Making watchdog tick be a quarter of timeout assure we will
  1568. * discover the queue hung between timeout and 1.25*timeout
  1569. */
  1570. #define IWL_WD_TICK(timeout) ((timeout) / 4)
  1571. /*
  1572. * Watchdog timer callback, we check each tx queue for stuck, if if hung
  1573. * we reset the firmware. If everything is fine just rearm the timer.
  1574. */
  1575. void iwl_bg_watchdog(unsigned long data)
  1576. {
  1577. struct iwl_priv *priv = (struct iwl_priv *)data;
  1578. int cnt;
  1579. unsigned long timeout;
  1580. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1581. return;
  1582. timeout = priv->cfg->base_params->wd_timeout;
  1583. if (timeout == 0)
  1584. return;
  1585. /* monitor and check for stuck cmd queue */
  1586. if (iwl_check_stuck_queue(priv, priv->cmd_queue))
  1587. return;
  1588. /* monitor and check for other stuck queues */
  1589. if (iwl_is_any_associated(priv)) {
  1590. for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
  1591. /* skip as we already checked the command queue */
  1592. if (cnt == priv->cmd_queue)
  1593. continue;
  1594. if (iwl_check_stuck_queue(priv, cnt))
  1595. return;
  1596. }
  1597. }
  1598. mod_timer(&priv->watchdog, jiffies +
  1599. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  1600. }
  1601. void iwl_setup_watchdog(struct iwl_priv *priv)
  1602. {
  1603. unsigned int timeout = priv->cfg->base_params->wd_timeout;
  1604. if (timeout)
  1605. mod_timer(&priv->watchdog,
  1606. jiffies + msecs_to_jiffies(IWL_WD_TICK(timeout)));
  1607. else
  1608. del_timer(&priv->watchdog);
  1609. }
  1610. /*
  1611. * extended beacon time format
  1612. * time in usec will be changed into a 32-bit value in extended:internal format
  1613. * the extended part is the beacon counts
  1614. * the internal part is the time in usec within one beacon interval
  1615. */
  1616. u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
  1617. {
  1618. u32 quot;
  1619. u32 rem;
  1620. u32 interval = beacon_interval * TIME_UNIT;
  1621. if (!interval || !usec)
  1622. return 0;
  1623. quot = (usec / interval) &
  1624. (iwl_beacon_time_mask_high(priv,
  1625. priv->hw_params.beacon_time_tsf_bits) >>
  1626. priv->hw_params.beacon_time_tsf_bits);
  1627. rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
  1628. priv->hw_params.beacon_time_tsf_bits);
  1629. return (quot << priv->hw_params.beacon_time_tsf_bits) + rem;
  1630. }
  1631. /* base is usually what we get from ucode with each received frame,
  1632. * the same as HW timer counter counting down
  1633. */
  1634. __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
  1635. u32 addon, u32 beacon_interval)
  1636. {
  1637. u32 base_low = base & iwl_beacon_time_mask_low(priv,
  1638. priv->hw_params.beacon_time_tsf_bits);
  1639. u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
  1640. priv->hw_params.beacon_time_tsf_bits);
  1641. u32 interval = beacon_interval * TIME_UNIT;
  1642. u32 res = (base & iwl_beacon_time_mask_high(priv,
  1643. priv->hw_params.beacon_time_tsf_bits)) +
  1644. (addon & iwl_beacon_time_mask_high(priv,
  1645. priv->hw_params.beacon_time_tsf_bits));
  1646. if (base_low > addon_low)
  1647. res += base_low - addon_low;
  1648. else if (base_low < addon_low) {
  1649. res += interval + base_low - addon_low;
  1650. res += (1 << priv->hw_params.beacon_time_tsf_bits);
  1651. } else
  1652. res += (1 << priv->hw_params.beacon_time_tsf_bits);
  1653. return cpu_to_le32(res);
  1654. }
  1655. #ifdef CONFIG_PM
  1656. int iwl_pci_suspend(struct device *device)
  1657. {
  1658. struct pci_dev *pdev = to_pci_dev(device);
  1659. struct iwl_priv *priv = pci_get_drvdata(pdev);
  1660. /*
  1661. * This function is called when system goes into suspend state
  1662. * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
  1663. * first but since iwl_mac_stop() has no knowledge of who the caller is,
  1664. * it will not call apm_ops.stop() to stop the DMA operation.
  1665. * Calling apm_ops.stop here to make sure we stop the DMA.
  1666. */
  1667. iwl_apm_stop(priv);
  1668. return 0;
  1669. }
  1670. int iwl_pci_resume(struct device *device)
  1671. {
  1672. struct pci_dev *pdev = to_pci_dev(device);
  1673. struct iwl_priv *priv = pci_get_drvdata(pdev);
  1674. bool hw_rfkill = false;
  1675. /*
  1676. * We disable the RETRY_TIMEOUT register (0x41) to keep
  1677. * PCI Tx retries from interfering with C3 CPU state.
  1678. */
  1679. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  1680. iwl_enable_interrupts(priv);
  1681. if (!(iwl_read32(priv, CSR_GP_CNTRL) &
  1682. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  1683. hw_rfkill = true;
  1684. if (hw_rfkill)
  1685. set_bit(STATUS_RF_KILL_HW, &priv->status);
  1686. else
  1687. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  1688. wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rfkill);
  1689. return 0;
  1690. }
  1691. const struct dev_pm_ops iwl_pm_ops = {
  1692. .suspend = iwl_pci_suspend,
  1693. .resume = iwl_pci_resume,
  1694. .freeze = iwl_pci_suspend,
  1695. .thaw = iwl_pci_resume,
  1696. .poweroff = iwl_pci_suspend,
  1697. .restore = iwl_pci_resume,
  1698. };
  1699. #endif /* CONFIG_PM */