iwl-core.c 53 KB

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