iwl-core.c 52 KB

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