iwl-core.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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. ieee80211_chswitch_done(ctx->vif, is_success);
  671. }
  672. #ifdef CONFIG_IWLWIFI_DEBUG
  673. void iwl_print_rx_config_cmd(struct iwl_priv *priv,
  674. enum iwl_rxon_context_id ctxid)
  675. {
  676. struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
  677. struct iwl_rxon_cmd *rxon = &ctx->staging;
  678. IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
  679. iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  680. IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  681. IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  682. IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
  683. le32_to_cpu(rxon->filter_flags));
  684. IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
  685. IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
  686. rxon->ofdm_basic_rates);
  687. IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  688. IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
  689. IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  690. IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  691. }
  692. #endif
  693. static void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
  694. {
  695. unsigned int reload_msec;
  696. unsigned long reload_jiffies;
  697. #ifdef CONFIG_IWLWIFI_DEBUG
  698. if (iwl_have_debug_level(IWL_DL_FW_ERRORS))
  699. iwl_print_rx_config_cmd(priv, IWL_RXON_CTX_BSS);
  700. #endif
  701. /* uCode is no longer loaded. */
  702. priv->ucode_loaded = false;
  703. /* Set the FW error flag -- cleared on iwl_down */
  704. set_bit(STATUS_FW_ERROR, &priv->shrd->status);
  705. /* Cancel currently queued command. */
  706. clear_bit(STATUS_HCMD_ACTIVE, &priv->shrd->status);
  707. iwl_abort_notification_waits(&priv->notif_wait);
  708. /* Keep the restart process from trying to send host
  709. * commands by clearing the ready bit */
  710. clear_bit(STATUS_READY, &priv->status);
  711. wake_up(&trans(priv)->wait_command_queue);
  712. if (!ondemand) {
  713. /*
  714. * If firmware keep reloading, then it indicate something
  715. * serious wrong and firmware having problem to recover
  716. * from it. Instead of keep trying which will fill the syslog
  717. * and hang the system, let's just stop it
  718. */
  719. reload_jiffies = jiffies;
  720. reload_msec = jiffies_to_msecs((long) reload_jiffies -
  721. (long) priv->reload_jiffies);
  722. priv->reload_jiffies = reload_jiffies;
  723. if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
  724. priv->reload_count++;
  725. if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
  726. IWL_ERR(priv, "BUG_ON, Stop restarting\n");
  727. return;
  728. }
  729. } else
  730. priv->reload_count = 0;
  731. }
  732. if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  733. if (iwlagn_mod_params.restart_fw) {
  734. IWL_DEBUG_FW_ERRORS(priv,
  735. "Restarting adapter due to uCode error.\n");
  736. queue_work(priv->workqueue, &priv->restart);
  737. } else
  738. IWL_DEBUG_FW_ERRORS(priv,
  739. "Detected FW error, but not restarting\n");
  740. }
  741. }
  742. int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
  743. {
  744. int ret;
  745. s8 prev_tx_power;
  746. bool defer;
  747. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  748. lockdep_assert_held(&priv->mutex);
  749. if (priv->tx_power_user_lmt == tx_power && !force)
  750. return 0;
  751. if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
  752. IWL_WARN(priv,
  753. "Requested user TXPOWER %d below lower limit %d.\n",
  754. tx_power,
  755. IWLAGN_TX_POWER_TARGET_POWER_MIN);
  756. return -EINVAL;
  757. }
  758. if (tx_power > priv->tx_power_device_lmt) {
  759. IWL_WARN(priv,
  760. "Requested user TXPOWER %d above upper limit %d.\n",
  761. tx_power, priv->tx_power_device_lmt);
  762. return -EINVAL;
  763. }
  764. if (!iwl_is_ready_rf(priv))
  765. return -EIO;
  766. /* scan complete and commit_rxon use tx_power_next value,
  767. * it always need to be updated for newest request */
  768. priv->tx_power_next = tx_power;
  769. /* do not set tx power when scanning or channel changing */
  770. defer = test_bit(STATUS_SCANNING, &priv->status) ||
  771. memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
  772. if (defer && !force) {
  773. IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
  774. return 0;
  775. }
  776. prev_tx_power = priv->tx_power_user_lmt;
  777. priv->tx_power_user_lmt = tx_power;
  778. ret = iwlagn_send_tx_power(priv);
  779. /* if fail to set tx_power, restore the orig. tx power */
  780. if (ret) {
  781. priv->tx_power_user_lmt = prev_tx_power;
  782. priv->tx_power_next = prev_tx_power;
  783. }
  784. return ret;
  785. }
  786. void iwl_send_bt_config(struct iwl_priv *priv)
  787. {
  788. struct iwl_bt_cmd bt_cmd = {
  789. .lead_time = BT_LEAD_TIME_DEF,
  790. .max_kill = BT_MAX_KILL_DEF,
  791. .kill_ack_mask = 0,
  792. .kill_cts_mask = 0,
  793. };
  794. if (!iwlagn_mod_params.bt_coex_active)
  795. bt_cmd.flags = BT_COEX_DISABLE;
  796. else
  797. bt_cmd.flags = BT_COEX_ENABLE;
  798. priv->bt_enable_flag = bt_cmd.flags;
  799. IWL_DEBUG_INFO(priv, "BT coex %s\n",
  800. (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
  801. if (iwl_dvm_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  802. CMD_SYNC, sizeof(struct iwl_bt_cmd), &bt_cmd))
  803. IWL_ERR(priv, "failed to send BT Coex Config\n");
  804. }
  805. int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
  806. {
  807. struct iwl_statistics_cmd statistics_cmd = {
  808. .configuration_flags =
  809. clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
  810. };
  811. if (flags & CMD_ASYNC)
  812. return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
  813. CMD_ASYNC,
  814. sizeof(struct iwl_statistics_cmd),
  815. &statistics_cmd);
  816. else
  817. return iwl_dvm_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
  818. CMD_SYNC,
  819. sizeof(struct iwl_statistics_cmd),
  820. &statistics_cmd);
  821. }
  822. #ifdef CONFIG_IWLWIFI_DEBUGFS
  823. #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
  824. void iwl_reset_traffic_log(struct iwl_priv *priv)
  825. {
  826. priv->tx_traffic_idx = 0;
  827. priv->rx_traffic_idx = 0;
  828. if (priv->tx_traffic)
  829. memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  830. if (priv->rx_traffic)
  831. memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  832. }
  833. int iwl_alloc_traffic_mem(struct iwl_priv *priv)
  834. {
  835. u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
  836. if (iwl_have_debug_level(IWL_DL_TX)) {
  837. if (!priv->tx_traffic) {
  838. priv->tx_traffic =
  839. kzalloc(traffic_size, GFP_KERNEL);
  840. if (!priv->tx_traffic)
  841. return -ENOMEM;
  842. }
  843. }
  844. if (iwl_have_debug_level(IWL_DL_RX)) {
  845. if (!priv->rx_traffic) {
  846. priv->rx_traffic =
  847. kzalloc(traffic_size, GFP_KERNEL);
  848. if (!priv->rx_traffic)
  849. return -ENOMEM;
  850. }
  851. }
  852. iwl_reset_traffic_log(priv);
  853. return 0;
  854. }
  855. void iwl_free_traffic_mem(struct iwl_priv *priv)
  856. {
  857. kfree(priv->tx_traffic);
  858. priv->tx_traffic = NULL;
  859. kfree(priv->rx_traffic);
  860. priv->rx_traffic = NULL;
  861. }
  862. void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
  863. u16 length, struct ieee80211_hdr *header)
  864. {
  865. __le16 fc;
  866. u16 len;
  867. if (likely(!iwl_have_debug_level(IWL_DL_TX)))
  868. return;
  869. if (!priv->tx_traffic)
  870. return;
  871. fc = header->frame_control;
  872. if (ieee80211_is_data(fc)) {
  873. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  874. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  875. memcpy((priv->tx_traffic +
  876. (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  877. header, len);
  878. priv->tx_traffic_idx =
  879. (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  880. }
  881. }
  882. void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
  883. u16 length, struct ieee80211_hdr *header)
  884. {
  885. __le16 fc;
  886. u16 len;
  887. if (likely(!iwl_have_debug_level(IWL_DL_RX)))
  888. return;
  889. if (!priv->rx_traffic)
  890. return;
  891. fc = header->frame_control;
  892. if (ieee80211_is_data(fc)) {
  893. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  894. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  895. memcpy((priv->rx_traffic +
  896. (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  897. header, len);
  898. priv->rx_traffic_idx =
  899. (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  900. }
  901. }
  902. const char *get_mgmt_string(int cmd)
  903. {
  904. switch (cmd) {
  905. IWL_CMD(MANAGEMENT_ASSOC_REQ);
  906. IWL_CMD(MANAGEMENT_ASSOC_RESP);
  907. IWL_CMD(MANAGEMENT_REASSOC_REQ);
  908. IWL_CMD(MANAGEMENT_REASSOC_RESP);
  909. IWL_CMD(MANAGEMENT_PROBE_REQ);
  910. IWL_CMD(MANAGEMENT_PROBE_RESP);
  911. IWL_CMD(MANAGEMENT_BEACON);
  912. IWL_CMD(MANAGEMENT_ATIM);
  913. IWL_CMD(MANAGEMENT_DISASSOC);
  914. IWL_CMD(MANAGEMENT_AUTH);
  915. IWL_CMD(MANAGEMENT_DEAUTH);
  916. IWL_CMD(MANAGEMENT_ACTION);
  917. default:
  918. return "UNKNOWN";
  919. }
  920. }
  921. const char *get_ctrl_string(int cmd)
  922. {
  923. switch (cmd) {
  924. IWL_CMD(CONTROL_BACK_REQ);
  925. IWL_CMD(CONTROL_BACK);
  926. IWL_CMD(CONTROL_PSPOLL);
  927. IWL_CMD(CONTROL_RTS);
  928. IWL_CMD(CONTROL_CTS);
  929. IWL_CMD(CONTROL_ACK);
  930. IWL_CMD(CONTROL_CFEND);
  931. IWL_CMD(CONTROL_CFENDACK);
  932. default:
  933. return "UNKNOWN";
  934. }
  935. }
  936. void iwl_clear_traffic_stats(struct iwl_priv *priv)
  937. {
  938. memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
  939. memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
  940. }
  941. /*
  942. * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
  943. * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
  944. * Use debugFs to display the rx/rx_statistics
  945. * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
  946. * information will be recorded, but DATA pkt still will be recorded
  947. * for the reason of iwl_led.c need to control the led blinking based on
  948. * number of tx and rx data.
  949. *
  950. */
  951. void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
  952. {
  953. struct traffic_stats *stats;
  954. if (is_tx)
  955. stats = &priv->tx_stats;
  956. else
  957. stats = &priv->rx_stats;
  958. if (ieee80211_is_mgmt(fc)) {
  959. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  960. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  961. stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
  962. break;
  963. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  964. stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
  965. break;
  966. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  967. stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
  968. break;
  969. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  970. stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
  971. break;
  972. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  973. stats->mgmt[MANAGEMENT_PROBE_REQ]++;
  974. break;
  975. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  976. stats->mgmt[MANAGEMENT_PROBE_RESP]++;
  977. break;
  978. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  979. stats->mgmt[MANAGEMENT_BEACON]++;
  980. break;
  981. case cpu_to_le16(IEEE80211_STYPE_ATIM):
  982. stats->mgmt[MANAGEMENT_ATIM]++;
  983. break;
  984. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  985. stats->mgmt[MANAGEMENT_DISASSOC]++;
  986. break;
  987. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  988. stats->mgmt[MANAGEMENT_AUTH]++;
  989. break;
  990. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  991. stats->mgmt[MANAGEMENT_DEAUTH]++;
  992. break;
  993. case cpu_to_le16(IEEE80211_STYPE_ACTION):
  994. stats->mgmt[MANAGEMENT_ACTION]++;
  995. break;
  996. }
  997. } else if (ieee80211_is_ctl(fc)) {
  998. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  999. case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
  1000. stats->ctrl[CONTROL_BACK_REQ]++;
  1001. break;
  1002. case cpu_to_le16(IEEE80211_STYPE_BACK):
  1003. stats->ctrl[CONTROL_BACK]++;
  1004. break;
  1005. case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
  1006. stats->ctrl[CONTROL_PSPOLL]++;
  1007. break;
  1008. case cpu_to_le16(IEEE80211_STYPE_RTS):
  1009. stats->ctrl[CONTROL_RTS]++;
  1010. break;
  1011. case cpu_to_le16(IEEE80211_STYPE_CTS):
  1012. stats->ctrl[CONTROL_CTS]++;
  1013. break;
  1014. case cpu_to_le16(IEEE80211_STYPE_ACK):
  1015. stats->ctrl[CONTROL_ACK]++;
  1016. break;
  1017. case cpu_to_le16(IEEE80211_STYPE_CFEND):
  1018. stats->ctrl[CONTROL_CFEND]++;
  1019. break;
  1020. case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
  1021. stats->ctrl[CONTROL_CFENDACK]++;
  1022. break;
  1023. }
  1024. } else {
  1025. /* data */
  1026. stats->data_cnt++;
  1027. stats->data_bytes += len;
  1028. }
  1029. }
  1030. #endif
  1031. static void iwl_force_rf_reset(struct iwl_priv *priv)
  1032. {
  1033. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1034. return;
  1035. if (!iwl_is_any_associated(priv)) {
  1036. IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
  1037. return;
  1038. }
  1039. /*
  1040. * There is no easy and better way to force reset the radio,
  1041. * the only known method is switching channel which will force to
  1042. * reset and tune the radio.
  1043. * Use internal short scan (single channel) operation to should
  1044. * achieve this objective.
  1045. * Driver should reset the radio when number of consecutive missed
  1046. * beacon, or any other uCode error condition detected.
  1047. */
  1048. IWL_DEBUG_INFO(priv, "perform radio reset.\n");
  1049. iwl_internal_short_hw_scan(priv);
  1050. }
  1051. int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
  1052. {
  1053. struct iwl_force_reset *force_reset;
  1054. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1055. return -EINVAL;
  1056. if (mode >= IWL_MAX_FORCE_RESET) {
  1057. IWL_DEBUG_INFO(priv, "invalid reset request.\n");
  1058. return -EINVAL;
  1059. }
  1060. force_reset = &priv->force_reset[mode];
  1061. force_reset->reset_request_count++;
  1062. if (!external) {
  1063. if (force_reset->last_force_reset_jiffies &&
  1064. time_after(force_reset->last_force_reset_jiffies +
  1065. force_reset->reset_duration, jiffies)) {
  1066. IWL_DEBUG_INFO(priv, "force reset rejected\n");
  1067. force_reset->reset_reject_count++;
  1068. return -EAGAIN;
  1069. }
  1070. }
  1071. force_reset->reset_success_count++;
  1072. force_reset->last_force_reset_jiffies = jiffies;
  1073. IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
  1074. switch (mode) {
  1075. case IWL_RF_RESET:
  1076. iwl_force_rf_reset(priv);
  1077. break;
  1078. case IWL_FW_RESET:
  1079. /*
  1080. * if the request is from external(ex: debugfs),
  1081. * then always perform the request in regardless the module
  1082. * parameter setting
  1083. * if the request is from internal (uCode error or driver
  1084. * detect failure), then fw_restart module parameter
  1085. * need to be check before performing firmware reload
  1086. */
  1087. if (!external && !iwlagn_mod_params.restart_fw) {
  1088. IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
  1089. "module parameter setting\n");
  1090. break;
  1091. }
  1092. IWL_ERR(priv, "On demand firmware reload\n");
  1093. iwlagn_fw_error(priv, true);
  1094. break;
  1095. }
  1096. return 0;
  1097. }
  1098. int iwl_cmd_echo_test(struct iwl_priv *priv)
  1099. {
  1100. int ret;
  1101. struct iwl_host_cmd cmd = {
  1102. .id = REPLY_ECHO,
  1103. .len = { 0 },
  1104. .flags = CMD_SYNC,
  1105. };
  1106. ret = iwl_dvm_send_cmd(priv, &cmd);
  1107. if (ret)
  1108. IWL_ERR(priv, "echo testing fail: 0X%x\n", ret);
  1109. else
  1110. IWL_DEBUG_INFO(priv, "echo testing pass\n");
  1111. return ret;
  1112. }
  1113. static inline int iwl_check_stuck_queue(struct iwl_priv *priv, int txq)
  1114. {
  1115. if (iwl_trans_check_stuck_queue(trans(priv), txq)) {
  1116. int ret;
  1117. ret = iwl_force_reset(priv, IWL_FW_RESET, false);
  1118. return (ret == -EAGAIN) ? 0 : 1;
  1119. }
  1120. return 0;
  1121. }
  1122. /*
  1123. * Making watchdog tick be a quarter of timeout assure we will
  1124. * discover the queue hung between timeout and 1.25*timeout
  1125. */
  1126. #define IWL_WD_TICK(timeout) ((timeout) / 4)
  1127. /*
  1128. * Watchdog timer callback, we check each tx queue for stuck, if if hung
  1129. * we reset the firmware. If everything is fine just rearm the timer.
  1130. */
  1131. void iwl_bg_watchdog(unsigned long data)
  1132. {
  1133. struct iwl_priv *priv = (struct iwl_priv *)data;
  1134. int cnt;
  1135. unsigned long timeout;
  1136. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1137. return;
  1138. if (iwl_is_rfkill(priv))
  1139. return;
  1140. timeout = hw_params(priv).wd_timeout;
  1141. if (timeout == 0)
  1142. return;
  1143. /* monitor and check for stuck queues */
  1144. for (cnt = 0; cnt < cfg(priv)->base_params->num_of_queues; cnt++)
  1145. if (iwl_check_stuck_queue(priv, cnt))
  1146. return;
  1147. mod_timer(&priv->watchdog, jiffies +
  1148. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  1149. }
  1150. void iwl_setup_watchdog(struct iwl_priv *priv)
  1151. {
  1152. unsigned int timeout = hw_params(priv).wd_timeout;
  1153. if (!iwlagn_mod_params.wd_disable) {
  1154. /* use system default */
  1155. if (timeout && !cfg(priv)->base_params->wd_disable)
  1156. mod_timer(&priv->watchdog,
  1157. jiffies +
  1158. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  1159. else
  1160. del_timer(&priv->watchdog);
  1161. } else {
  1162. /* module parameter overwrite default configuration */
  1163. if (timeout && iwlagn_mod_params.wd_disable == 2)
  1164. mod_timer(&priv->watchdog,
  1165. jiffies +
  1166. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  1167. else
  1168. del_timer(&priv->watchdog);
  1169. }
  1170. }
  1171. /**
  1172. * iwl_beacon_time_mask_low - mask of lower 32 bit of beacon time
  1173. * @priv -- pointer to iwl_priv data structure
  1174. * @tsf_bits -- number of bits need to shift for masking)
  1175. */
  1176. static inline u32 iwl_beacon_time_mask_low(struct iwl_priv *priv,
  1177. u16 tsf_bits)
  1178. {
  1179. return (1 << tsf_bits) - 1;
  1180. }
  1181. /**
  1182. * iwl_beacon_time_mask_high - mask of higher 32 bit of beacon time
  1183. * @priv -- pointer to iwl_priv data structure
  1184. * @tsf_bits -- number of bits need to shift for masking)
  1185. */
  1186. static inline u32 iwl_beacon_time_mask_high(struct iwl_priv *priv,
  1187. u16 tsf_bits)
  1188. {
  1189. return ((1 << (32 - tsf_bits)) - 1) << tsf_bits;
  1190. }
  1191. /*
  1192. * extended beacon time format
  1193. * time in usec will be changed into a 32-bit value in extended:internal format
  1194. * the extended part is the beacon counts
  1195. * the internal part is the time in usec within one beacon interval
  1196. */
  1197. u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
  1198. {
  1199. u32 quot;
  1200. u32 rem;
  1201. u32 interval = beacon_interval * TIME_UNIT;
  1202. if (!interval || !usec)
  1203. return 0;
  1204. quot = (usec / interval) &
  1205. (iwl_beacon_time_mask_high(priv, IWLAGN_EXT_BEACON_TIME_POS) >>
  1206. IWLAGN_EXT_BEACON_TIME_POS);
  1207. rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
  1208. IWLAGN_EXT_BEACON_TIME_POS);
  1209. return (quot << IWLAGN_EXT_BEACON_TIME_POS) + rem;
  1210. }
  1211. /* base is usually what we get from ucode with each received frame,
  1212. * the same as HW timer counter counting down
  1213. */
  1214. __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
  1215. u32 addon, u32 beacon_interval)
  1216. {
  1217. u32 base_low = base & iwl_beacon_time_mask_low(priv,
  1218. IWLAGN_EXT_BEACON_TIME_POS);
  1219. u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
  1220. IWLAGN_EXT_BEACON_TIME_POS);
  1221. u32 interval = beacon_interval * TIME_UNIT;
  1222. u32 res = (base & iwl_beacon_time_mask_high(priv,
  1223. IWLAGN_EXT_BEACON_TIME_POS)) +
  1224. (addon & iwl_beacon_time_mask_high(priv,
  1225. IWLAGN_EXT_BEACON_TIME_POS));
  1226. if (base_low > addon_low)
  1227. res += base_low - addon_low;
  1228. else if (base_low < addon_low) {
  1229. res += interval + base_low - addon_low;
  1230. res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
  1231. } else
  1232. res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
  1233. return cpu_to_le32(res);
  1234. }
  1235. void iwl_nic_error(struct iwl_op_mode *op_mode)
  1236. {
  1237. struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
  1238. iwlagn_fw_error(priv, false);
  1239. }
  1240. void iwl_set_hw_rfkill_state(struct iwl_op_mode *op_mode, bool state)
  1241. {
  1242. struct iwl_priv *priv = IWL_OP_MODE_GET_DVM(op_mode);
  1243. if (state)
  1244. set_bit(STATUS_RF_KILL_HW, &priv->status);
  1245. else
  1246. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  1247. wiphy_rfkill_set_hw_state(priv->hw->wiphy, state);
  1248. }
  1249. void iwl_free_skb(struct iwl_op_mode *op_mode, struct sk_buff *skb)
  1250. {
  1251. struct ieee80211_tx_info *info;
  1252. info = IEEE80211_SKB_CB(skb);
  1253. kmem_cache_free(iwl_tx_cmd_pool, (info->driver_data[1]));
  1254. dev_kfree_skb_any(skb);
  1255. }