iwl-core.c 54 KB

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