iwl-core.c 41 KB

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