iwl-core.c 51 KB

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