iwl-core.c 53 KB

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