iwl-core.c 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484
  1. /******************************************************************************
  2. *
  3. * GPL LICENSE SUMMARY
  4. *
  5. * Copyright(c) 2008 - 2012 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-shared.h"
  40. #include "iwl-agn.h"
  41. #include "iwl-trans.h"
  42. const u8 iwl_bcast_addr[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  43. #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
  44. #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
  45. static void iwl_init_ht_hw_capab(const struct iwl_priv *priv,
  46. struct ieee80211_sta_ht_cap *ht_info,
  47. enum ieee80211_band band)
  48. {
  49. u16 max_bit_rate = 0;
  50. u8 rx_chains_num = hw_params(priv).rx_chains_num;
  51. u8 tx_chains_num = hw_params(priv).tx_chains_num;
  52. ht_info->cap = 0;
  53. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  54. ht_info->ht_supported = true;
  55. if (cfg(priv)->ht_params &&
  56. cfg(priv)->ht_params->ht_greenfield_support)
  57. ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
  58. ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
  59. max_bit_rate = MAX_BIT_RATE_20_MHZ;
  60. if (hw_params(priv).ht40_channel & BIT(band)) {
  61. ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  62. ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
  63. ht_info->mcs.rx_mask[4] = 0x01;
  64. max_bit_rate = MAX_BIT_RATE_40_MHZ;
  65. }
  66. if (iwlagn_mod_params.amsdu_size_8K)
  67. ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  68. ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
  69. ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
  70. ht_info->mcs.rx_mask[0] = 0xFF;
  71. if (rx_chains_num >= 2)
  72. ht_info->mcs.rx_mask[1] = 0xFF;
  73. if (rx_chains_num >= 3)
  74. ht_info->mcs.rx_mask[2] = 0xFF;
  75. /* Highest supported Rx data rate */
  76. max_bit_rate *= rx_chains_num;
  77. WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
  78. ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
  79. /* Tx MCS capabilities */
  80. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  81. if (tx_chains_num != rx_chains_num) {
  82. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  83. ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
  84. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  85. }
  86. }
  87. /**
  88. * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
  89. */
  90. int iwl_init_geos(struct iwl_priv *priv)
  91. {
  92. struct iwl_channel_info *ch;
  93. struct ieee80211_supported_band *sband;
  94. struct ieee80211_channel *channels;
  95. struct ieee80211_channel *geo_ch;
  96. struct ieee80211_rate *rates;
  97. int i = 0;
  98. s8 max_tx_power = IWLAGN_TX_POWER_TARGET_POWER_MIN;
  99. if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
  100. priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
  101. IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
  102. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  103. return 0;
  104. }
  105. channels = kcalloc(priv->channel_count,
  106. sizeof(struct ieee80211_channel), GFP_KERNEL);
  107. if (!channels)
  108. return -ENOMEM;
  109. rates = kcalloc(IWL_RATE_COUNT_LEGACY, sizeof(struct ieee80211_rate),
  110. GFP_KERNEL);
  111. if (!rates) {
  112. kfree(channels);
  113. return -ENOMEM;
  114. }
  115. /* 5.2GHz channels start after the 2.4GHz channels */
  116. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  117. sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
  118. /* just OFDM */
  119. sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
  120. sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
  121. if (hw_params(priv).sku & EEPROM_SKU_CAP_11N_ENABLE)
  122. iwl_init_ht_hw_capab(priv, &sband->ht_cap,
  123. IEEE80211_BAND_5GHZ);
  124. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  125. sband->channels = channels;
  126. /* OFDM & CCK */
  127. sband->bitrates = rates;
  128. sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
  129. if (hw_params(priv).sku & EEPROM_SKU_CAP_11N_ENABLE)
  130. iwl_init_ht_hw_capab(priv, &sband->ht_cap,
  131. IEEE80211_BAND_2GHZ);
  132. priv->ieee_channels = channels;
  133. priv->ieee_rates = rates;
  134. for (i = 0; i < priv->channel_count; i++) {
  135. ch = &priv->channel_info[i];
  136. /* FIXME: might be removed if scan is OK */
  137. if (!is_channel_valid(ch))
  138. continue;
  139. sband = &priv->bands[ch->band];
  140. geo_ch = &sband->channels[sband->n_channels++];
  141. geo_ch->center_freq =
  142. ieee80211_channel_to_frequency(ch->channel, ch->band);
  143. geo_ch->max_power = ch->max_power_avg;
  144. geo_ch->max_antenna_gain = 0xff;
  145. geo_ch->hw_value = ch->channel;
  146. if (is_channel_valid(ch)) {
  147. if (!(ch->flags & EEPROM_CHANNEL_IBSS))
  148. geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
  149. if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
  150. geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  151. if (ch->flags & EEPROM_CHANNEL_RADAR)
  152. geo_ch->flags |= IEEE80211_CHAN_RADAR;
  153. geo_ch->flags |= ch->ht40_extension_channel;
  154. if (ch->max_power_avg > max_tx_power)
  155. max_tx_power = ch->max_power_avg;
  156. } else {
  157. geo_ch->flags |= IEEE80211_CHAN_DISABLED;
  158. }
  159. IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
  160. ch->channel, geo_ch->center_freq,
  161. is_channel_a_band(ch) ? "5.2" : "2.4",
  162. geo_ch->flags & IEEE80211_CHAN_DISABLED ?
  163. "restricted" : "valid",
  164. geo_ch->flags);
  165. }
  166. priv->tx_power_device_lmt = max_tx_power;
  167. priv->tx_power_user_lmt = max_tx_power;
  168. priv->tx_power_next = max_tx_power;
  169. if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
  170. hw_params(priv).sku & EEPROM_SKU_CAP_BAND_52GHZ) {
  171. IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
  172. "Please send your %s to maintainer.\n",
  173. trans(priv)->hw_id_str);
  174. hw_params(priv).sku &= ~EEPROM_SKU_CAP_BAND_52GHZ;
  175. }
  176. IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
  177. priv->bands[IEEE80211_BAND_2GHZ].n_channels,
  178. priv->bands[IEEE80211_BAND_5GHZ].n_channels);
  179. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  180. return 0;
  181. }
  182. /*
  183. * iwl_free_geos - undo allocations in iwl_init_geos
  184. */
  185. void iwl_free_geos(struct iwl_priv *priv)
  186. {
  187. kfree(priv->ieee_channels);
  188. kfree(priv->ieee_rates);
  189. clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
  190. }
  191. static bool iwl_is_channel_extension(struct iwl_priv *priv,
  192. enum ieee80211_band band,
  193. u16 channel, u8 extension_chan_offset)
  194. {
  195. const struct iwl_channel_info *ch_info;
  196. ch_info = iwl_get_channel_info(priv, band, channel);
  197. if (!is_channel_valid(ch_info))
  198. return false;
  199. if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
  200. return !(ch_info->ht40_extension_channel &
  201. IEEE80211_CHAN_NO_HT40PLUS);
  202. else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
  203. return !(ch_info->ht40_extension_channel &
  204. IEEE80211_CHAN_NO_HT40MINUS);
  205. return false;
  206. }
  207. bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
  208. struct iwl_rxon_context *ctx,
  209. struct ieee80211_sta_ht_cap *ht_cap)
  210. {
  211. if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
  212. return false;
  213. /*
  214. * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
  215. * the bit will not set if it is pure 40MHz case
  216. */
  217. if (ht_cap && !ht_cap->ht_supported)
  218. return false;
  219. #ifdef CONFIG_IWLWIFI_DEBUGFS
  220. if (priv->disable_ht40)
  221. return false;
  222. #endif
  223. return iwl_is_channel_extension(priv, priv->band,
  224. le16_to_cpu(ctx->staging.channel),
  225. ctx->ht.extension_chan_offset);
  226. }
  227. static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
  228. {
  229. u16 new_val;
  230. u16 beacon_factor;
  231. /*
  232. * If mac80211 hasn't given us a beacon interval, program
  233. * the default into the device (not checking this here
  234. * would cause the adjustment below to return the maximum
  235. * value, which may break PAN.)
  236. */
  237. if (!beacon_val)
  238. return DEFAULT_BEACON_INTERVAL;
  239. /*
  240. * If the beacon interval we obtained from the peer
  241. * is too large, we'll have to wake up more often
  242. * (and in IBSS case, we'll beacon too much)
  243. *
  244. * For example, if max_beacon_val is 4096, and the
  245. * requested beacon interval is 7000, we'll have to
  246. * use 3500 to be able to wake up on the beacons.
  247. *
  248. * This could badly influence beacon detection stats.
  249. */
  250. beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
  251. new_val = beacon_val / beacon_factor;
  252. if (!new_val)
  253. new_val = max_beacon_val;
  254. return new_val;
  255. }
  256. int iwl_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  257. {
  258. u64 tsf;
  259. s32 interval_tm, rem;
  260. struct ieee80211_conf *conf = NULL;
  261. u16 beacon_int;
  262. struct ieee80211_vif *vif = ctx->vif;
  263. conf = &priv->hw->conf;
  264. lockdep_assert_held(&priv->mutex);
  265. memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
  266. ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
  267. ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
  268. beacon_int = vif ? vif->bss_conf.beacon_int : 0;
  269. /*
  270. * TODO: For IBSS we need to get atim_window from mac80211,
  271. * for now just always use 0
  272. */
  273. ctx->timing.atim_window = 0;
  274. if (ctx->ctxid == IWL_RXON_CTX_PAN &&
  275. (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
  276. iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
  277. priv->contexts[IWL_RXON_CTX_BSS].vif &&
  278. priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
  279. ctx->timing.beacon_interval =
  280. priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
  281. beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
  282. } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
  283. iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
  284. priv->contexts[IWL_RXON_CTX_PAN].vif &&
  285. priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
  286. (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
  287. !ctx->vif->bss_conf.beacon_int)) {
  288. ctx->timing.beacon_interval =
  289. priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
  290. beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
  291. } else {
  292. beacon_int = iwl_adjust_beacon_interval(beacon_int,
  293. IWL_MAX_UCODE_BEACON_INTERVAL * TIME_UNIT);
  294. ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
  295. }
  296. ctx->beacon_int = beacon_int;
  297. tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
  298. interval_tm = beacon_int * TIME_UNIT;
  299. rem = do_div(tsf, interval_tm);
  300. ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
  301. ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
  302. IWL_DEBUG_ASSOC(priv,
  303. "beacon interval %d beacon timer %d beacon tim %d\n",
  304. le16_to_cpu(ctx->timing.beacon_interval),
  305. le32_to_cpu(ctx->timing.beacon_init_val),
  306. le16_to_cpu(ctx->timing.atim_window));
  307. return iwl_dvm_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
  308. CMD_SYNC, sizeof(ctx->timing), &ctx->timing);
  309. }
  310. void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  311. int hw_decrypt)
  312. {
  313. struct iwl_rxon_cmd *rxon = &ctx->staging;
  314. if (hw_decrypt)
  315. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  316. else
  317. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  318. }
  319. /* validate RXON structure is valid */
  320. int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  321. {
  322. struct iwl_rxon_cmd *rxon = &ctx->staging;
  323. u32 errors = 0;
  324. if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
  325. if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
  326. IWL_WARN(priv, "check 2.4G: wrong narrow\n");
  327. errors |= BIT(0);
  328. }
  329. if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
  330. IWL_WARN(priv, "check 2.4G: wrong radar\n");
  331. errors |= BIT(1);
  332. }
  333. } else {
  334. if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
  335. IWL_WARN(priv, "check 5.2G: not short slot!\n");
  336. errors |= BIT(2);
  337. }
  338. if (rxon->flags & RXON_FLG_CCK_MSK) {
  339. IWL_WARN(priv, "check 5.2G: CCK!\n");
  340. errors |= BIT(3);
  341. }
  342. }
  343. if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
  344. IWL_WARN(priv, "mac/bssid mcast!\n");
  345. errors |= BIT(4);
  346. }
  347. /* make sure basic rates 6Mbps and 1Mbps are supported */
  348. if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
  349. (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
  350. IWL_WARN(priv, "neither 1 nor 6 are basic\n");
  351. errors |= BIT(5);
  352. }
  353. if (le16_to_cpu(rxon->assoc_id) > 2007) {
  354. IWL_WARN(priv, "aid > 2007\n");
  355. errors |= BIT(6);
  356. }
  357. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
  358. == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
  359. IWL_WARN(priv, "CCK and short slot\n");
  360. errors |= BIT(7);
  361. }
  362. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
  363. == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
  364. IWL_WARN(priv, "CCK and auto detect");
  365. errors |= BIT(8);
  366. }
  367. if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
  368. RXON_FLG_TGG_PROTECT_MSK)) ==
  369. RXON_FLG_TGG_PROTECT_MSK) {
  370. IWL_WARN(priv, "TGg but no auto-detect\n");
  371. errors |= BIT(9);
  372. }
  373. if (rxon->channel == 0) {
  374. IWL_WARN(priv, "zero channel is invalid\n");
  375. errors |= BIT(10);
  376. }
  377. WARN(errors, "Invalid RXON (%#x), channel %d",
  378. errors, le16_to_cpu(rxon->channel));
  379. return errors ? -EINVAL : 0;
  380. }
  381. /**
  382. * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
  383. * @priv: staging_rxon is compared to active_rxon
  384. *
  385. * If the RXON structure is changing enough to require a new tune,
  386. * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
  387. * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
  388. */
  389. int iwl_full_rxon_required(struct iwl_priv *priv,
  390. struct iwl_rxon_context *ctx)
  391. {
  392. const struct iwl_rxon_cmd *staging = &ctx->staging;
  393. const struct iwl_rxon_cmd *active = &ctx->active;
  394. #define CHK(cond) \
  395. if ((cond)) { \
  396. IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n"); \
  397. return 1; \
  398. }
  399. #define CHK_NEQ(c1, c2) \
  400. if ((c1) != (c2)) { \
  401. IWL_DEBUG_INFO(priv, "need full RXON - " \
  402. #c1 " != " #c2 " - %d != %d\n", \
  403. (c1), (c2)); \
  404. return 1; \
  405. }
  406. /* These items are only settable from the full RXON command */
  407. CHK(!iwl_is_associated_ctx(ctx));
  408. CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
  409. CHK(compare_ether_addr(staging->node_addr, active->node_addr));
  410. CHK(compare_ether_addr(staging->wlap_bssid_addr,
  411. active->wlap_bssid_addr));
  412. CHK_NEQ(staging->dev_type, active->dev_type);
  413. CHK_NEQ(staging->channel, active->channel);
  414. CHK_NEQ(staging->air_propagation, active->air_propagation);
  415. CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
  416. active->ofdm_ht_single_stream_basic_rates);
  417. CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
  418. active->ofdm_ht_dual_stream_basic_rates);
  419. CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
  420. active->ofdm_ht_triple_stream_basic_rates);
  421. CHK_NEQ(staging->assoc_id, active->assoc_id);
  422. /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
  423. * be updated with the RXON_ASSOC command -- however only some
  424. * flag transitions are allowed using RXON_ASSOC */
  425. /* Check if we are not switching bands */
  426. CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
  427. active->flags & RXON_FLG_BAND_24G_MSK);
  428. /* Check if we are switching association toggle */
  429. CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
  430. active->filter_flags & RXON_FILTER_ASSOC_MSK);
  431. #undef CHK
  432. #undef CHK_NEQ
  433. return 0;
  434. }
  435. static void _iwl_set_rxon_ht(struct iwl_priv *priv,
  436. struct iwl_ht_config *ht_conf,
  437. struct iwl_rxon_context *ctx)
  438. {
  439. struct iwl_rxon_cmd *rxon = &ctx->staging;
  440. if (!ctx->ht.enabled) {
  441. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  442. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  443. RXON_FLG_HT40_PROT_MSK |
  444. RXON_FLG_HT_PROT_MSK);
  445. return;
  446. }
  447. /* FIXME: if the definition of ht.protection changed, the "translation"
  448. * will be needed for rxon->flags
  449. */
  450. rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
  451. /* Set up channel bandwidth:
  452. * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
  453. /* clear the HT channel mode before set the mode */
  454. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  455. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  456. if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
  457. /* pure ht40 */
  458. if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
  459. rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
  460. /* Note: control channel is opposite of extension channel */
  461. switch (ctx->ht.extension_chan_offset) {
  462. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  463. rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  464. break;
  465. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  466. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  467. break;
  468. }
  469. } else {
  470. /* Note: control channel is opposite of extension channel */
  471. switch (ctx->ht.extension_chan_offset) {
  472. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  473. rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  474. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  475. break;
  476. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  477. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  478. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  479. break;
  480. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  481. default:
  482. /* channel location only valid if in Mixed mode */
  483. IWL_ERR(priv, "invalid extension channel offset\n");
  484. break;
  485. }
  486. }
  487. } else {
  488. rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
  489. }
  490. iwlagn_set_rxon_chain(priv, ctx);
  491. IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
  492. "extension channel offset 0x%x\n",
  493. le32_to_cpu(rxon->flags), ctx->ht.protection,
  494. ctx->ht.extension_chan_offset);
  495. }
  496. void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
  497. {
  498. struct iwl_rxon_context *ctx;
  499. for_each_context(priv, ctx)
  500. _iwl_set_rxon_ht(priv, ht_conf, ctx);
  501. }
  502. /* Return valid, unused, channel for a passive scan to reset the RF */
  503. u8 iwl_get_single_channel_number(struct iwl_priv *priv,
  504. enum ieee80211_band band)
  505. {
  506. const struct iwl_channel_info *ch_info;
  507. int i;
  508. u8 channel = 0;
  509. u8 min, max;
  510. struct iwl_rxon_context *ctx;
  511. if (band == IEEE80211_BAND_5GHZ) {
  512. min = 14;
  513. max = priv->channel_count;
  514. } else {
  515. min = 0;
  516. max = 14;
  517. }
  518. for (i = min; i < max; i++) {
  519. bool busy = false;
  520. for_each_context(priv, ctx) {
  521. busy = priv->channel_info[i].channel ==
  522. le16_to_cpu(ctx->staging.channel);
  523. if (busy)
  524. break;
  525. }
  526. if (busy)
  527. continue;
  528. channel = priv->channel_info[i].channel;
  529. ch_info = iwl_get_channel_info(priv, band, channel);
  530. if (is_channel_valid(ch_info))
  531. break;
  532. }
  533. return channel;
  534. }
  535. /**
  536. * iwl_set_rxon_channel - Set the band and channel values in staging RXON
  537. * @ch: requested channel as a pointer to struct ieee80211_channel
  538. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  539. * in the staging RXON flag structure based on the ch->band
  540. */
  541. void iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
  542. struct iwl_rxon_context *ctx)
  543. {
  544. enum ieee80211_band band = ch->band;
  545. u16 channel = ch->hw_value;
  546. if ((le16_to_cpu(ctx->staging.channel) == channel) &&
  547. (priv->band == band))
  548. return;
  549. ctx->staging.channel = cpu_to_le16(channel);
  550. if (band == IEEE80211_BAND_5GHZ)
  551. ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
  552. else
  553. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  554. priv->band = band;
  555. IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
  556. }
  557. void iwl_set_flags_for_band(struct iwl_priv *priv,
  558. struct iwl_rxon_context *ctx,
  559. enum ieee80211_band band,
  560. struct ieee80211_vif *vif)
  561. {
  562. if (band == IEEE80211_BAND_5GHZ) {
  563. ctx->staging.flags &=
  564. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
  565. | RXON_FLG_CCK_MSK);
  566. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  567. } else {
  568. /* Copied from iwl_post_associate() */
  569. if (vif && vif->bss_conf.use_short_slot)
  570. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  571. else
  572. ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  573. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  574. ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
  575. ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
  576. }
  577. }
  578. /*
  579. * initialize rxon structure with default values from eeprom
  580. */
  581. void iwl_connection_init_rx_config(struct iwl_priv *priv,
  582. struct iwl_rxon_context *ctx)
  583. {
  584. const struct iwl_channel_info *ch_info;
  585. memset(&ctx->staging, 0, sizeof(ctx->staging));
  586. if (!ctx->vif) {
  587. ctx->staging.dev_type = ctx->unused_devtype;
  588. } else switch (ctx->vif->type) {
  589. case NL80211_IFTYPE_AP:
  590. ctx->staging.dev_type = ctx->ap_devtype;
  591. break;
  592. case NL80211_IFTYPE_STATION:
  593. ctx->staging.dev_type = ctx->station_devtype;
  594. ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  595. break;
  596. case NL80211_IFTYPE_ADHOC:
  597. ctx->staging.dev_type = ctx->ibss_devtype;
  598. ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  599. ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
  600. RXON_FILTER_ACCEPT_GRP_MSK;
  601. break;
  602. default:
  603. IWL_ERR(priv, "Unsupported interface type %d\n",
  604. ctx->vif->type);
  605. break;
  606. }
  607. #if 0
  608. /* TODO: Figure out when short_preamble would be set and cache from
  609. * that */
  610. if (!hw_to_local(priv->hw)->short_preamble)
  611. ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  612. else
  613. ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  614. #endif
  615. ch_info = iwl_get_channel_info(priv, priv->band,
  616. le16_to_cpu(ctx->active.channel));
  617. if (!ch_info)
  618. ch_info = &priv->channel_info[0];
  619. ctx->staging.channel = cpu_to_le16(ch_info->channel);
  620. priv->band = ch_info->band;
  621. iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
  622. ctx->staging.ofdm_basic_rates =
  623. (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  624. ctx->staging.cck_basic_rates =
  625. (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  626. /* clear both MIX and PURE40 mode flag */
  627. ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
  628. RXON_FLG_CHANNEL_MODE_PURE_40);
  629. if (ctx->vif)
  630. memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
  631. ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
  632. ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
  633. ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
  634. }
  635. void iwl_set_rate(struct iwl_priv *priv)
  636. {
  637. const struct ieee80211_supported_band *hw = NULL;
  638. struct ieee80211_rate *rate;
  639. struct iwl_rxon_context *ctx;
  640. int i;
  641. hw = iwl_get_hw_mode(priv, priv->band);
  642. if (!hw) {
  643. IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
  644. return;
  645. }
  646. priv->active_rate = 0;
  647. for (i = 0; i < hw->n_bitrates; i++) {
  648. rate = &(hw->bitrates[i]);
  649. if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
  650. priv->active_rate |= (1 << rate->hw_value);
  651. }
  652. IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
  653. for_each_context(priv, ctx) {
  654. ctx->staging.cck_basic_rates =
  655. (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  656. ctx->staging.ofdm_basic_rates =
  657. (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  658. }
  659. }
  660. void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
  661. {
  662. /*
  663. * MULTI-FIXME
  664. * See iwlagn_mac_channel_switch.
  665. */
  666. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  667. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  668. return;
  669. if (!test_and_clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->status))
  670. return;
  671. if (ctx->vif)
  672. ieee80211_chswitch_done(ctx->vif, is_success);
  673. }
  674. #ifdef CONFIG_IWLWIFI_DEBUG
  675. void iwl_print_rx_config_cmd(struct iwl_priv *priv,
  676. enum iwl_rxon_context_id ctxid)
  677. {
  678. struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
  679. struct iwl_rxon_cmd *rxon = &ctx->staging;
  680. IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
  681. iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  682. IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  683. IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  684. IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
  685. le32_to_cpu(rxon->filter_flags));
  686. IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
  687. IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
  688. rxon->ofdm_basic_rates);
  689. IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  690. IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
  691. IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  692. IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  693. }
  694. #endif
  695. static void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
  696. {
  697. unsigned int reload_msec;
  698. unsigned long reload_jiffies;
  699. #ifdef CONFIG_IWLWIFI_DEBUG
  700. if (iwl_have_debug_level(IWL_DL_FW_ERRORS))
  701. iwl_print_rx_config_cmd(priv, IWL_RXON_CTX_BSS);
  702. #endif
  703. /* uCode is no longer loaded. */
  704. priv->ucode_loaded = false;
  705. /* Set the FW error flag -- cleared on iwl_down */
  706. set_bit(STATUS_FW_ERROR, &priv->shrd->status);
  707. /* Cancel currently queued command. */
  708. clear_bit(STATUS_HCMD_ACTIVE, &priv->shrd->status);
  709. iwl_abort_notification_waits(&priv->notif_wait);
  710. /* Keep the restart process from trying to send host
  711. * commands by clearing the ready bit */
  712. clear_bit(STATUS_READY, &priv->status);
  713. wake_up(&trans(priv)->wait_command_queue);
  714. if (!ondemand) {
  715. /*
  716. * If firmware keep reloading, then it indicate something
  717. * serious wrong and firmware having problem to recover
  718. * from it. Instead of keep trying which will fill the syslog
  719. * and hang the system, let's just stop it
  720. */
  721. reload_jiffies = jiffies;
  722. reload_msec = jiffies_to_msecs((long) reload_jiffies -
  723. (long) priv->reload_jiffies);
  724. priv->reload_jiffies = reload_jiffies;
  725. if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
  726. priv->reload_count++;
  727. if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
  728. IWL_ERR(priv, "BUG_ON, Stop restarting\n");
  729. return;
  730. }
  731. } else
  732. priv->reload_count = 0;
  733. }
  734. if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  735. if (iwlagn_mod_params.restart_fw) {
  736. IWL_DEBUG_FW_ERRORS(priv,
  737. "Restarting adapter due to uCode error.\n");
  738. queue_work(priv->workqueue, &priv->restart);
  739. } else
  740. IWL_DEBUG_FW_ERRORS(priv,
  741. "Detected FW error, but not restarting\n");
  742. }
  743. }
  744. int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
  745. {
  746. int ret;
  747. s8 prev_tx_power;
  748. bool defer;
  749. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  750. lockdep_assert_held(&priv->mutex);
  751. if (priv->tx_power_user_lmt == tx_power && !force)
  752. return 0;
  753. if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
  754. IWL_WARN(priv,
  755. "Requested user TXPOWER %d below lower limit %d.\n",
  756. tx_power,
  757. IWLAGN_TX_POWER_TARGET_POWER_MIN);
  758. return -EINVAL;
  759. }
  760. if (tx_power > priv->tx_power_device_lmt) {
  761. IWL_WARN(priv,
  762. "Requested user TXPOWER %d above upper limit %d.\n",
  763. tx_power, priv->tx_power_device_lmt);
  764. return -EINVAL;
  765. }
  766. if (!iwl_is_ready_rf(priv))
  767. return -EIO;
  768. /* scan complete and commit_rxon use tx_power_next value,
  769. * it always need to be updated for newest request */
  770. priv->tx_power_next = tx_power;
  771. /* do not set tx power when scanning or channel changing */
  772. defer = test_bit(STATUS_SCANNING, &priv->status) ||
  773. memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
  774. if (defer && !force) {
  775. IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
  776. return 0;
  777. }
  778. prev_tx_power = priv->tx_power_user_lmt;
  779. priv->tx_power_user_lmt = tx_power;
  780. ret = iwlagn_send_tx_power(priv);
  781. /* if fail to set tx_power, restore the orig. tx power */
  782. if (ret) {
  783. priv->tx_power_user_lmt = prev_tx_power;
  784. priv->tx_power_next = prev_tx_power;
  785. }
  786. return ret;
  787. }
  788. void iwl_send_bt_config(struct iwl_priv *priv)
  789. {
  790. struct iwl_bt_cmd bt_cmd = {
  791. .lead_time = BT_LEAD_TIME_DEF,
  792. .max_kill = BT_MAX_KILL_DEF,
  793. .kill_ack_mask = 0,
  794. .kill_cts_mask = 0,
  795. };
  796. if (!iwlagn_mod_params.bt_coex_active)
  797. bt_cmd.flags = BT_COEX_DISABLE;
  798. else
  799. bt_cmd.flags = BT_COEX_ENABLE;
  800. priv->bt_enable_flag = bt_cmd.flags;
  801. IWL_DEBUG_INFO(priv, "BT coex %s\n",
  802. (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
  803. if (iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  804. CMD_SYNC, sizeof(struct iwl_bt_cmd), &bt_cmd))
  805. IWL_ERR(priv, "failed to send BT Coex Config\n");
  806. }
  807. int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
  808. {
  809. struct iwl_statistics_cmd statistics_cmd = {
  810. .configuration_flags =
  811. clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
  812. };
  813. if (flags & CMD_ASYNC)
  814. return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
  815. CMD_ASYNC,
  816. sizeof(struct iwl_statistics_cmd),
  817. &statistics_cmd);
  818. else
  819. return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
  820. CMD_SYNC,
  821. sizeof(struct iwl_statistics_cmd),
  822. &statistics_cmd);
  823. }
  824. #ifdef CONFIG_IWLWIFI_DEBUGFS
  825. #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
  826. void iwl_reset_traffic_log(struct iwl_priv *priv)
  827. {
  828. priv->tx_traffic_idx = 0;
  829. priv->rx_traffic_idx = 0;
  830. if (priv->tx_traffic)
  831. memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  832. if (priv->rx_traffic)
  833. memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  834. }
  835. int iwl_alloc_traffic_mem(struct iwl_priv *priv)
  836. {
  837. u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
  838. if (iwl_have_debug_level(IWL_DL_TX)) {
  839. if (!priv->tx_traffic) {
  840. priv->tx_traffic =
  841. kzalloc(traffic_size, GFP_KERNEL);
  842. if (!priv->tx_traffic)
  843. return -ENOMEM;
  844. }
  845. }
  846. if (iwl_have_debug_level(IWL_DL_RX)) {
  847. if (!priv->rx_traffic) {
  848. priv->rx_traffic =
  849. kzalloc(traffic_size, GFP_KERNEL);
  850. if (!priv->rx_traffic)
  851. return -ENOMEM;
  852. }
  853. }
  854. iwl_reset_traffic_log(priv);
  855. return 0;
  856. }
  857. void iwl_free_traffic_mem(struct iwl_priv *priv)
  858. {
  859. kfree(priv->tx_traffic);
  860. priv->tx_traffic = NULL;
  861. kfree(priv->rx_traffic);
  862. priv->rx_traffic = NULL;
  863. }
  864. void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
  865. u16 length, struct ieee80211_hdr *header)
  866. {
  867. __le16 fc;
  868. u16 len;
  869. if (likely(!iwl_have_debug_level(IWL_DL_TX)))
  870. return;
  871. if (!priv->tx_traffic)
  872. return;
  873. fc = header->frame_control;
  874. if (ieee80211_is_data(fc)) {
  875. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  876. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  877. memcpy((priv->tx_traffic +
  878. (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  879. header, len);
  880. priv->tx_traffic_idx =
  881. (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  882. }
  883. }
  884. void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
  885. u16 length, struct ieee80211_hdr *header)
  886. {
  887. __le16 fc;
  888. u16 len;
  889. if (likely(!iwl_have_debug_level(IWL_DL_RX)))
  890. return;
  891. if (!priv->rx_traffic)
  892. return;
  893. fc = header->frame_control;
  894. if (ieee80211_is_data(fc)) {
  895. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  896. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  897. memcpy((priv->rx_traffic +
  898. (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  899. header, len);
  900. priv->rx_traffic_idx =
  901. (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  902. }
  903. }
  904. const char *get_mgmt_string(int cmd)
  905. {
  906. switch (cmd) {
  907. IWL_CMD(MANAGEMENT_ASSOC_REQ);
  908. IWL_CMD(MANAGEMENT_ASSOC_RESP);
  909. IWL_CMD(MANAGEMENT_REASSOC_REQ);
  910. IWL_CMD(MANAGEMENT_REASSOC_RESP);
  911. IWL_CMD(MANAGEMENT_PROBE_REQ);
  912. IWL_CMD(MANAGEMENT_PROBE_RESP);
  913. IWL_CMD(MANAGEMENT_BEACON);
  914. IWL_CMD(MANAGEMENT_ATIM);
  915. IWL_CMD(MANAGEMENT_DISASSOC);
  916. IWL_CMD(MANAGEMENT_AUTH);
  917. IWL_CMD(MANAGEMENT_DEAUTH);
  918. IWL_CMD(MANAGEMENT_ACTION);
  919. default:
  920. return "UNKNOWN";
  921. }
  922. }
  923. const char *get_ctrl_string(int cmd)
  924. {
  925. switch (cmd) {
  926. IWL_CMD(CONTROL_BACK_REQ);
  927. IWL_CMD(CONTROL_BACK);
  928. IWL_CMD(CONTROL_PSPOLL);
  929. IWL_CMD(CONTROL_RTS);
  930. IWL_CMD(CONTROL_CTS);
  931. IWL_CMD(CONTROL_ACK);
  932. IWL_CMD(CONTROL_CFEND);
  933. IWL_CMD(CONTROL_CFENDACK);
  934. default:
  935. return "UNKNOWN";
  936. }
  937. }
  938. void iwl_clear_traffic_stats(struct iwl_priv *priv)
  939. {
  940. memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
  941. memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
  942. }
  943. /*
  944. * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
  945. * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
  946. * Use debugFs to display the rx/rx_statistics
  947. * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
  948. * information will be recorded, but DATA pkt still will be recorded
  949. * for the reason of iwl_led.c need to control the led blinking based on
  950. * number of tx and rx data.
  951. *
  952. */
  953. void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
  954. {
  955. struct traffic_stats *stats;
  956. if (is_tx)
  957. stats = &priv->tx_stats;
  958. else
  959. stats = &priv->rx_stats;
  960. if (ieee80211_is_mgmt(fc)) {
  961. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  962. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  963. stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
  964. break;
  965. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  966. stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
  967. break;
  968. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  969. stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
  970. break;
  971. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  972. stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
  973. break;
  974. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  975. stats->mgmt[MANAGEMENT_PROBE_REQ]++;
  976. break;
  977. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  978. stats->mgmt[MANAGEMENT_PROBE_RESP]++;
  979. break;
  980. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  981. stats->mgmt[MANAGEMENT_BEACON]++;
  982. break;
  983. case cpu_to_le16(IEEE80211_STYPE_ATIM):
  984. stats->mgmt[MANAGEMENT_ATIM]++;
  985. break;
  986. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  987. stats->mgmt[MANAGEMENT_DISASSOC]++;
  988. break;
  989. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  990. stats->mgmt[MANAGEMENT_AUTH]++;
  991. break;
  992. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  993. stats->mgmt[MANAGEMENT_DEAUTH]++;
  994. break;
  995. case cpu_to_le16(IEEE80211_STYPE_ACTION):
  996. stats->mgmt[MANAGEMENT_ACTION]++;
  997. break;
  998. }
  999. } else if (ieee80211_is_ctl(fc)) {
  1000. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  1001. case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
  1002. stats->ctrl[CONTROL_BACK_REQ]++;
  1003. break;
  1004. case cpu_to_le16(IEEE80211_STYPE_BACK):
  1005. stats->ctrl[CONTROL_BACK]++;
  1006. break;
  1007. case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
  1008. stats->ctrl[CONTROL_PSPOLL]++;
  1009. break;
  1010. case cpu_to_le16(IEEE80211_STYPE_RTS):
  1011. stats->ctrl[CONTROL_RTS]++;
  1012. break;
  1013. case cpu_to_le16(IEEE80211_STYPE_CTS):
  1014. stats->ctrl[CONTROL_CTS]++;
  1015. break;
  1016. case cpu_to_le16(IEEE80211_STYPE_ACK):
  1017. stats->ctrl[CONTROL_ACK]++;
  1018. break;
  1019. case cpu_to_le16(IEEE80211_STYPE_CFEND):
  1020. stats->ctrl[CONTROL_CFEND]++;
  1021. break;
  1022. case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
  1023. stats->ctrl[CONTROL_CFENDACK]++;
  1024. break;
  1025. }
  1026. } else {
  1027. /* data */
  1028. stats->data_cnt++;
  1029. stats->data_bytes += len;
  1030. }
  1031. }
  1032. #endif
  1033. static void iwl_force_rf_reset(struct iwl_priv *priv)
  1034. {
  1035. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1036. return;
  1037. if (!iwl_is_any_associated(priv)) {
  1038. IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
  1039. return;
  1040. }
  1041. /*
  1042. * There is no easy and better way to force reset the radio,
  1043. * the only known method is switching channel which will force to
  1044. * reset and tune the radio.
  1045. * Use internal short scan (single channel) operation to should
  1046. * achieve this objective.
  1047. * Driver should reset the radio when number of consecutive missed
  1048. * beacon, or any other uCode error condition detected.
  1049. */
  1050. IWL_DEBUG_INFO(priv, "perform radio reset.\n");
  1051. iwl_internal_short_hw_scan(priv);
  1052. }
  1053. int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
  1054. {
  1055. struct iwl_force_reset *force_reset;
  1056. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1057. return -EINVAL;
  1058. if (mode >= IWL_MAX_FORCE_RESET) {
  1059. IWL_DEBUG_INFO(priv, "invalid reset request.\n");
  1060. return -EINVAL;
  1061. }
  1062. force_reset = &priv->force_reset[mode];
  1063. force_reset->reset_request_count++;
  1064. if (!external) {
  1065. if (force_reset->last_force_reset_jiffies &&
  1066. time_after(force_reset->last_force_reset_jiffies +
  1067. force_reset->reset_duration, jiffies)) {
  1068. IWL_DEBUG_INFO(priv, "force reset rejected\n");
  1069. force_reset->reset_reject_count++;
  1070. return -EAGAIN;
  1071. }
  1072. }
  1073. force_reset->reset_success_count++;
  1074. force_reset->last_force_reset_jiffies = jiffies;
  1075. IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
  1076. switch (mode) {
  1077. case IWL_RF_RESET:
  1078. iwl_force_rf_reset(priv);
  1079. break;
  1080. case IWL_FW_RESET:
  1081. /*
  1082. * if the request is from external(ex: debugfs),
  1083. * then always perform the request in regardless the module
  1084. * parameter setting
  1085. * if the request is from internal (uCode error or driver
  1086. * detect failure), then fw_restart module parameter
  1087. * need to be check before performing firmware reload
  1088. */
  1089. if (!external && !iwlagn_mod_params.restart_fw) {
  1090. IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
  1091. "module parameter setting\n");
  1092. break;
  1093. }
  1094. IWL_ERR(priv, "On demand firmware reload\n");
  1095. iwlagn_fw_error(priv, true);
  1096. break;
  1097. }
  1098. return 0;
  1099. }
  1100. int iwl_cmd_echo_test(struct iwl_priv *priv)
  1101. {
  1102. int ret;
  1103. struct iwl_host_cmd cmd = {
  1104. .id = REPLY_ECHO,
  1105. .len = { 0 },
  1106. .flags = CMD_SYNC,
  1107. };
  1108. ret = iwl_dvm_send_cmd(priv, &cmd);
  1109. if (ret)
  1110. IWL_ERR(priv, "echo testing fail: 0X%x\n", ret);
  1111. else
  1112. IWL_DEBUG_INFO(priv, "echo testing pass\n");
  1113. return ret;
  1114. }
  1115. static inline int iwl_check_stuck_queue(struct iwl_priv *priv, int txq)
  1116. {
  1117. if (iwl_trans_check_stuck_queue(trans(priv), txq)) {
  1118. int ret;
  1119. ret = iwl_force_reset(priv, IWL_FW_RESET, false);
  1120. return (ret == -EAGAIN) ? 0 : 1;
  1121. }
  1122. return 0;
  1123. }
  1124. /*
  1125. * Making watchdog tick be a quarter of timeout assure we will
  1126. * discover the queue hung between timeout and 1.25*timeout
  1127. */
  1128. #define IWL_WD_TICK(timeout) ((timeout) / 4)
  1129. /*
  1130. * Watchdog timer callback, we check each tx queue for stuck, if if hung
  1131. * we reset the firmware. If everything is fine just rearm the timer.
  1132. */
  1133. void iwl_bg_watchdog(unsigned long data)
  1134. {
  1135. struct iwl_priv *priv = (struct iwl_priv *)data;
  1136. int cnt;
  1137. unsigned long timeout;
  1138. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1139. return;
  1140. if (iwl_is_rfkill(priv))
  1141. return;
  1142. timeout = hw_params(priv).wd_timeout;
  1143. if (timeout == 0)
  1144. return;
  1145. /* monitor and check for stuck queues */
  1146. for (cnt = 0; cnt < cfg(priv)->base_params->num_of_queues; cnt++)
  1147. if (iwl_check_stuck_queue(priv, cnt))
  1148. return;
  1149. mod_timer(&priv->watchdog, jiffies +
  1150. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  1151. }
  1152. void iwl_setup_watchdog(struct iwl_priv *priv)
  1153. {
  1154. unsigned int timeout = hw_params(priv).wd_timeout;
  1155. if (!iwlagn_mod_params.wd_disable) {
  1156. /* use system default */
  1157. if (timeout && !cfg(priv)->base_params->wd_disable)
  1158. mod_timer(&priv->watchdog,
  1159. jiffies +
  1160. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  1161. else
  1162. del_timer(&priv->watchdog);
  1163. } else {
  1164. /* module parameter overwrite default configuration */
  1165. if (timeout && iwlagn_mod_params.wd_disable == 2)
  1166. mod_timer(&priv->watchdog,
  1167. jiffies +
  1168. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  1169. else
  1170. del_timer(&priv->watchdog);
  1171. }
  1172. }
  1173. /**
  1174. * iwl_beacon_time_mask_low - mask of lower 32 bit of beacon time
  1175. * @priv -- pointer to iwl_priv data structure
  1176. * @tsf_bits -- number of bits need to shift for masking)
  1177. */
  1178. static inline u32 iwl_beacon_time_mask_low(struct iwl_priv *priv,
  1179. u16 tsf_bits)
  1180. {
  1181. return (1 << tsf_bits) - 1;
  1182. }
  1183. /**
  1184. * iwl_beacon_time_mask_high - mask of higher 32 bit of beacon time
  1185. * @priv -- pointer to iwl_priv data structure
  1186. * @tsf_bits -- number of bits need to shift for masking)
  1187. */
  1188. static inline u32 iwl_beacon_time_mask_high(struct iwl_priv *priv,
  1189. u16 tsf_bits)
  1190. {
  1191. return ((1 << (32 - tsf_bits)) - 1) << tsf_bits;
  1192. }
  1193. /*
  1194. * extended beacon time format
  1195. * time in usec will be changed into a 32-bit value in extended:internal format
  1196. * the extended part is the beacon counts
  1197. * the internal part is the time in usec within one beacon interval
  1198. */
  1199. u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
  1200. {
  1201. u32 quot;
  1202. u32 rem;
  1203. u32 interval = beacon_interval * TIME_UNIT;
  1204. if (!interval || !usec)
  1205. return 0;
  1206. quot = (usec / interval) &
  1207. (iwl_beacon_time_mask_high(priv, IWLAGN_EXT_BEACON_TIME_POS) >>
  1208. IWLAGN_EXT_BEACON_TIME_POS);
  1209. rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
  1210. IWLAGN_EXT_BEACON_TIME_POS);
  1211. return (quot << IWLAGN_EXT_BEACON_TIME_POS) + rem;
  1212. }
  1213. /* base is usually what we get from ucode with each received frame,
  1214. * the same as HW timer counter counting down
  1215. */
  1216. __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
  1217. u32 addon, u32 beacon_interval)
  1218. {
  1219. u32 base_low = base & iwl_beacon_time_mask_low(priv,
  1220. IWLAGN_EXT_BEACON_TIME_POS);
  1221. u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
  1222. IWLAGN_EXT_BEACON_TIME_POS);
  1223. u32 interval = beacon_interval * TIME_UNIT;
  1224. u32 res = (base & iwl_beacon_time_mask_high(priv,
  1225. IWLAGN_EXT_BEACON_TIME_POS)) +
  1226. (addon & iwl_beacon_time_mask_high(priv,
  1227. IWLAGN_EXT_BEACON_TIME_POS));
  1228. if (base_low > addon_low)
  1229. res += base_low - addon_low;
  1230. else if (base_low < addon_low) {
  1231. res += interval + base_low - addon_low;
  1232. res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
  1233. } else
  1234. res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
  1235. return cpu_to_le32(res);
  1236. }
  1237. void iwl_nic_error(struct iwl_op_mode *op_mode)
  1238. {
  1239. struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
  1240. iwlagn_fw_error(priv, false);
  1241. }
  1242. void iwl_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
  1243. {
  1244. struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
  1245. if (state)
  1246. set_bit(STATUS_RF_KILL_HW, &priv->status);
  1247. else
  1248. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  1249. wiphy_rfkill_set_hw_state(priv->hw->wiphy, state);
  1250. }
  1251. void iwl_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
  1252. {
  1253. struct ieee80211_tx_info *info;
  1254. info = IEEE80211_SKB_CB(skb);
  1255. kmem_cache_free(iwl_tx_cmd_pool, (info->driver_data[1]));
  1256. dev_kfree_skb_any(skb);
  1257. }