iwl-core.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903
  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 = kcalloc(priv->channel_count,
  114. sizeof(struct ieee80211_channel), GFP_KERNEL);
  115. if (!channels)
  116. return -ENOMEM;
  117. rates = kcalloc(IWL_RATE_COUNT_LEGACY, sizeof(struct ieee80211_rate),
  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. enum iwl_rxon_context_id ctxid)
  686. {
  687. struct iwl_rxon_context *ctx = &priv->contexts[ctxid];
  688. struct iwl_rxon_cmd *rxon = &ctx->staging;
  689. IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
  690. iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  691. IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  692. IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  693. IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
  694. le32_to_cpu(rxon->filter_flags));
  695. IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
  696. IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
  697. rxon->ofdm_basic_rates);
  698. IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  699. IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
  700. IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  701. IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  702. }
  703. #endif
  704. static void iwlagn_abort_notification_waits(struct iwl_priv *priv)
  705. {
  706. unsigned long flags;
  707. struct iwl_notification_wait *wait_entry;
  708. spin_lock_irqsave(&priv->notif_wait_lock, flags);
  709. list_for_each_entry(wait_entry, &priv->notif_waits, list)
  710. wait_entry->aborted = true;
  711. spin_unlock_irqrestore(&priv->notif_wait_lock, flags);
  712. wake_up_all(&priv->notif_waitq);
  713. }
  714. void iwlagn_fw_error(struct iwl_priv *priv, bool ondemand)
  715. {
  716. unsigned int reload_msec;
  717. unsigned long reload_jiffies;
  718. /* Set the FW error flag -- cleared on iwl_down */
  719. set_bit(STATUS_FW_ERROR, &priv->shrd->status);
  720. /* Cancel currently queued command. */
  721. clear_bit(STATUS_HCMD_ACTIVE, &priv->shrd->status);
  722. iwlagn_abort_notification_waits(priv);
  723. /* Keep the restart process from trying to send host
  724. * commands by clearing the ready bit */
  725. clear_bit(STATUS_READY, &priv->shrd->status);
  726. wake_up(&priv->shrd->wait_command_queue);
  727. if (!ondemand) {
  728. /*
  729. * If firmware keep reloading, then it indicate something
  730. * serious wrong and firmware having problem to recover
  731. * from it. Instead of keep trying which will fill the syslog
  732. * and hang the system, let's just stop it
  733. */
  734. reload_jiffies = jiffies;
  735. reload_msec = jiffies_to_msecs((long) reload_jiffies -
  736. (long) priv->reload_jiffies);
  737. priv->reload_jiffies = reload_jiffies;
  738. if (reload_msec <= IWL_MIN_RELOAD_DURATION) {
  739. priv->reload_count++;
  740. if (priv->reload_count >= IWL_MAX_CONTINUE_RELOAD_CNT) {
  741. IWL_ERR(priv, "BUG_ON, Stop restarting\n");
  742. return;
  743. }
  744. } else
  745. priv->reload_count = 0;
  746. }
  747. if (!test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
  748. if (iwlagn_mod_params.restart_fw) {
  749. IWL_DEBUG_FW_ERRORS(priv,
  750. "Restarting adapter due to uCode error.\n");
  751. queue_work(priv->shrd->workqueue, &priv->restart);
  752. } else
  753. IWL_DEBUG_FW_ERRORS(priv,
  754. "Detected FW error, but not restarting\n");
  755. }
  756. }
  757. static int iwl_apm_stop_master(struct iwl_priv *priv)
  758. {
  759. int ret = 0;
  760. /* stop device's busmaster DMA activity */
  761. iwl_set_bit(bus(priv), CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  762. ret = iwl_poll_bit(bus(priv), CSR_RESET,
  763. CSR_RESET_REG_FLAG_MASTER_DISABLED,
  764. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  765. if (ret)
  766. IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
  767. IWL_DEBUG_INFO(priv, "stop master\n");
  768. return ret;
  769. }
  770. void iwl_apm_stop(struct iwl_priv *priv)
  771. {
  772. IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
  773. clear_bit(STATUS_DEVICE_ENABLED, &priv->shrd->status);
  774. /* Stop device's DMA activity */
  775. iwl_apm_stop_master(priv);
  776. /* Reset the entire device */
  777. iwl_set_bit(bus(priv), CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  778. udelay(10);
  779. /*
  780. * Clear "initialization complete" bit to move adapter from
  781. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  782. */
  783. iwl_clear_bit(bus(priv), CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  784. }
  785. /*
  786. * Start up NIC's basic functionality after it has been reset
  787. * (e.g. after platform boot, or shutdown via iwl_apm_stop())
  788. * NOTE: This does not load uCode nor start the embedded processor
  789. */
  790. int iwl_apm_init(struct iwl_priv *priv)
  791. {
  792. int ret = 0;
  793. IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
  794. /*
  795. * Use "set_bit" below rather than "write", to preserve any hardware
  796. * bits already set by default after reset.
  797. */
  798. /* Disable L0S exit timer (platform NMI Work/Around) */
  799. iwl_set_bit(bus(priv), CSR_GIO_CHICKEN_BITS,
  800. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  801. /*
  802. * Disable L0s without affecting L1;
  803. * don't wait for ICH L0s (ICH bug W/A)
  804. */
  805. iwl_set_bit(bus(priv), CSR_GIO_CHICKEN_BITS,
  806. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  807. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  808. iwl_set_bit(bus(priv), CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  809. /*
  810. * Enable HAP INTA (interrupt from management bus) to
  811. * wake device's PCI Express link L1a -> L0s
  812. */
  813. iwl_set_bit(bus(priv), CSR_HW_IF_CONFIG_REG,
  814. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  815. bus_apm_config(priv->bus);
  816. /* Configure analog phase-lock-loop before activating to D0A */
  817. if (priv->cfg->base_params->pll_cfg_val)
  818. iwl_set_bit(bus(priv), CSR_ANA_PLL_CFG,
  819. priv->cfg->base_params->pll_cfg_val);
  820. /*
  821. * Set "initialization complete" bit to move adapter from
  822. * D0U* --> D0A* (powered-up active) state.
  823. */
  824. iwl_set_bit(bus(priv), CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  825. /*
  826. * Wait for clock stabilization; once stabilized, access to
  827. * device-internal resources is supported, e.g. iwl_write_prph()
  828. * and accesses to uCode SRAM.
  829. */
  830. ret = iwl_poll_bit(bus(priv), CSR_GP_CNTRL,
  831. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  832. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  833. if (ret < 0) {
  834. IWL_DEBUG_INFO(priv, "Failed to init the card\n");
  835. goto out;
  836. }
  837. /*
  838. * Enable DMA clock and wait for it to stabilize.
  839. *
  840. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
  841. * do not disable clocks. This preserves any hardware bits already
  842. * set by default in "CLK_CTRL_REG" after reset.
  843. */
  844. iwl_write_prph(bus(priv), APMG_CLK_EN_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  845. udelay(20);
  846. /* Disable L1-Active */
  847. iwl_set_bits_prph(bus(priv), APMG_PCIDEV_STT_REG,
  848. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  849. set_bit(STATUS_DEVICE_ENABLED, &priv->shrd->status);
  850. out:
  851. return ret;
  852. }
  853. int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
  854. {
  855. int ret;
  856. s8 prev_tx_power;
  857. bool defer;
  858. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  859. lockdep_assert_held(&priv->shrd->mutex);
  860. if (priv->tx_power_user_lmt == tx_power && !force)
  861. return 0;
  862. if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
  863. IWL_WARN(priv,
  864. "Requested user TXPOWER %d below lower limit %d.\n",
  865. tx_power,
  866. IWLAGN_TX_POWER_TARGET_POWER_MIN);
  867. return -EINVAL;
  868. }
  869. if (tx_power > priv->tx_power_device_lmt) {
  870. IWL_WARN(priv,
  871. "Requested user TXPOWER %d above upper limit %d.\n",
  872. tx_power, priv->tx_power_device_lmt);
  873. return -EINVAL;
  874. }
  875. if (!iwl_is_ready_rf(priv->shrd))
  876. return -EIO;
  877. /* scan complete and commit_rxon use tx_power_next value,
  878. * it always need to be updated for newest request */
  879. priv->tx_power_next = tx_power;
  880. /* do not set tx power when scanning or channel changing */
  881. defer = test_bit(STATUS_SCANNING, &priv->shrd->status) ||
  882. memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging));
  883. if (defer && !force) {
  884. IWL_DEBUG_INFO(priv, "Deferring tx power set\n");
  885. return 0;
  886. }
  887. prev_tx_power = priv->tx_power_user_lmt;
  888. priv->tx_power_user_lmt = tx_power;
  889. ret = iwlagn_send_tx_power(priv);
  890. /* if fail to set tx_power, restore the orig. tx power */
  891. if (ret) {
  892. priv->tx_power_user_lmt = prev_tx_power;
  893. priv->tx_power_next = prev_tx_power;
  894. }
  895. return ret;
  896. }
  897. void iwl_send_bt_config(struct iwl_priv *priv)
  898. {
  899. struct iwl_bt_cmd bt_cmd = {
  900. .lead_time = BT_LEAD_TIME_DEF,
  901. .max_kill = BT_MAX_KILL_DEF,
  902. .kill_ack_mask = 0,
  903. .kill_cts_mask = 0,
  904. };
  905. if (!iwlagn_mod_params.bt_coex_active)
  906. bt_cmd.flags = BT_COEX_DISABLE;
  907. else
  908. bt_cmd.flags = BT_COEX_ENABLE;
  909. priv->bt_enable_flag = bt_cmd.flags;
  910. IWL_DEBUG_INFO(priv, "BT coex %s\n",
  911. (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
  912. if (iwl_trans_send_cmd_pdu(trans(priv), REPLY_BT_CONFIG,
  913. CMD_SYNC, sizeof(struct iwl_bt_cmd), &bt_cmd))
  914. IWL_ERR(priv, "failed to send BT Coex Config\n");
  915. }
  916. int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
  917. {
  918. struct iwl_statistics_cmd statistics_cmd = {
  919. .configuration_flags =
  920. clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
  921. };
  922. if (flags & CMD_ASYNC)
  923. return iwl_trans_send_cmd_pdu(trans(priv), REPLY_STATISTICS_CMD,
  924. CMD_ASYNC,
  925. sizeof(struct iwl_statistics_cmd),
  926. &statistics_cmd);
  927. else
  928. return iwl_trans_send_cmd_pdu(trans(priv), REPLY_STATISTICS_CMD,
  929. CMD_SYNC,
  930. sizeof(struct iwl_statistics_cmd),
  931. &statistics_cmd);
  932. }
  933. int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
  934. const struct ieee80211_tx_queue_params *params)
  935. {
  936. struct iwl_priv *priv = hw->priv;
  937. struct iwl_rxon_context *ctx;
  938. unsigned long flags;
  939. int q;
  940. IWL_DEBUG_MAC80211(priv, "enter\n");
  941. if (!iwl_is_ready_rf(priv->shrd)) {
  942. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  943. return -EIO;
  944. }
  945. if (queue >= AC_NUM) {
  946. IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
  947. return 0;
  948. }
  949. q = AC_NUM - 1 - queue;
  950. spin_lock_irqsave(&priv->shrd->lock, flags);
  951. /*
  952. * MULTI-FIXME
  953. * This may need to be done per interface in nl80211/cfg80211/mac80211.
  954. */
  955. for_each_context(priv, ctx) {
  956. ctx->qos_data.def_qos_parm.ac[q].cw_min =
  957. cpu_to_le16(params->cw_min);
  958. ctx->qos_data.def_qos_parm.ac[q].cw_max =
  959. cpu_to_le16(params->cw_max);
  960. ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  961. ctx->qos_data.def_qos_parm.ac[q].edca_txop =
  962. cpu_to_le16((params->txop * 32));
  963. ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  964. }
  965. spin_unlock_irqrestore(&priv->shrd->lock, flags);
  966. IWL_DEBUG_MAC80211(priv, "leave\n");
  967. return 0;
  968. }
  969. int iwl_mac_tx_last_beacon(struct ieee80211_hw *hw)
  970. {
  971. struct iwl_priv *priv = hw->priv;
  972. return priv->ibss_manager == IWL_IBSS_MANAGER;
  973. }
  974. static int iwl_set_mode(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  975. {
  976. iwl_connection_init_rx_config(priv, ctx);
  977. iwlagn_set_rxon_chain(priv, ctx);
  978. return iwlagn_commit_rxon(priv, ctx);
  979. }
  980. static int iwl_setup_interface(struct iwl_priv *priv,
  981. struct iwl_rxon_context *ctx)
  982. {
  983. struct ieee80211_vif *vif = ctx->vif;
  984. int err;
  985. lockdep_assert_held(&priv->shrd->mutex);
  986. /*
  987. * This variable will be correct only when there's just
  988. * a single context, but all code using it is for hardware
  989. * that supports only one context.
  990. */
  991. priv->iw_mode = vif->type;
  992. ctx->is_active = true;
  993. err = iwl_set_mode(priv, ctx);
  994. if (err) {
  995. if (!ctx->always_active)
  996. ctx->is_active = false;
  997. return err;
  998. }
  999. if (priv->cfg->bt_params && priv->cfg->bt_params->advanced_bt_coexist &&
  1000. vif->type == NL80211_IFTYPE_ADHOC) {
  1001. /*
  1002. * pretend to have high BT traffic as long as we
  1003. * are operating in IBSS mode, as this will cause
  1004. * the rate scaling etc. to behave as intended.
  1005. */
  1006. priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1007. }
  1008. return 0;
  1009. }
  1010. int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1011. {
  1012. struct iwl_priv *priv = hw->priv;
  1013. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1014. struct iwl_rxon_context *tmp, *ctx = NULL;
  1015. int err;
  1016. enum nl80211_iftype viftype = ieee80211_vif_type_p2p(vif);
  1017. IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
  1018. viftype, vif->addr);
  1019. cancel_delayed_work_sync(&priv->hw_roc_disable_work);
  1020. mutex_lock(&priv->shrd->mutex);
  1021. iwlagn_disable_roc(priv);
  1022. if (!iwl_is_ready_rf(priv->shrd)) {
  1023. IWL_WARN(priv, "Try to add interface when device not ready\n");
  1024. err = -EINVAL;
  1025. goto out;
  1026. }
  1027. for_each_context(priv, tmp) {
  1028. u32 possible_modes =
  1029. tmp->interface_modes | tmp->exclusive_interface_modes;
  1030. if (tmp->vif) {
  1031. /* check if this busy context is exclusive */
  1032. if (tmp->exclusive_interface_modes &
  1033. BIT(tmp->vif->type)) {
  1034. err = -EINVAL;
  1035. goto out;
  1036. }
  1037. continue;
  1038. }
  1039. if (!(possible_modes & BIT(viftype)))
  1040. continue;
  1041. /* have maybe usable context w/o interface */
  1042. ctx = tmp;
  1043. break;
  1044. }
  1045. if (!ctx) {
  1046. err = -EOPNOTSUPP;
  1047. goto out;
  1048. }
  1049. vif_priv->ctx = ctx;
  1050. ctx->vif = vif;
  1051. err = iwl_setup_interface(priv, ctx);
  1052. if (!err)
  1053. goto out;
  1054. ctx->vif = NULL;
  1055. priv->iw_mode = NL80211_IFTYPE_STATION;
  1056. out:
  1057. mutex_unlock(&priv->shrd->mutex);
  1058. IWL_DEBUG_MAC80211(priv, "leave\n");
  1059. return err;
  1060. }
  1061. static void iwl_teardown_interface(struct iwl_priv *priv,
  1062. struct ieee80211_vif *vif,
  1063. bool mode_change)
  1064. {
  1065. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1066. lockdep_assert_held(&priv->shrd->mutex);
  1067. if (priv->scan_vif == vif) {
  1068. iwl_scan_cancel_timeout(priv, 200);
  1069. iwl_force_scan_end(priv);
  1070. }
  1071. if (!mode_change) {
  1072. iwl_set_mode(priv, ctx);
  1073. if (!ctx->always_active)
  1074. ctx->is_active = false;
  1075. }
  1076. /*
  1077. * When removing the IBSS interface, overwrite the
  1078. * BT traffic load with the stored one from the last
  1079. * notification, if any. If this is a device that
  1080. * doesn't implement this, this has no effect since
  1081. * both values are the same and zero.
  1082. */
  1083. if (vif->type == NL80211_IFTYPE_ADHOC)
  1084. priv->bt_traffic_load = priv->last_bt_traffic_load;
  1085. }
  1086. void iwl_mac_remove_interface(struct ieee80211_hw *hw,
  1087. struct ieee80211_vif *vif)
  1088. {
  1089. struct iwl_priv *priv = hw->priv;
  1090. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1091. IWL_DEBUG_MAC80211(priv, "enter\n");
  1092. mutex_lock(&priv->shrd->mutex);
  1093. if (WARN_ON(ctx->vif != vif)) {
  1094. struct iwl_rxon_context *tmp;
  1095. IWL_ERR(priv, "ctx->vif = %p, vif = %p\n", ctx->vif, vif);
  1096. for_each_context(priv, tmp)
  1097. IWL_ERR(priv, "\tID = %d:\tctx = %p\tctx->vif = %p\n",
  1098. tmp->ctxid, tmp, tmp->vif);
  1099. }
  1100. ctx->vif = NULL;
  1101. iwl_teardown_interface(priv, vif, false);
  1102. mutex_unlock(&priv->shrd->mutex);
  1103. IWL_DEBUG_MAC80211(priv, "leave\n");
  1104. }
  1105. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1106. #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
  1107. void iwl_reset_traffic_log(struct iwl_priv *priv)
  1108. {
  1109. priv->tx_traffic_idx = 0;
  1110. priv->rx_traffic_idx = 0;
  1111. if (priv->tx_traffic)
  1112. memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  1113. if (priv->rx_traffic)
  1114. memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  1115. }
  1116. int iwl_alloc_traffic_mem(struct iwl_priv *priv)
  1117. {
  1118. u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
  1119. if (iwl_get_debug_level(priv->shrd) & IWL_DL_TX) {
  1120. if (!priv->tx_traffic) {
  1121. priv->tx_traffic =
  1122. kzalloc(traffic_size, GFP_KERNEL);
  1123. if (!priv->tx_traffic)
  1124. return -ENOMEM;
  1125. }
  1126. }
  1127. if (iwl_get_debug_level(priv->shrd) & IWL_DL_RX) {
  1128. if (!priv->rx_traffic) {
  1129. priv->rx_traffic =
  1130. kzalloc(traffic_size, GFP_KERNEL);
  1131. if (!priv->rx_traffic)
  1132. return -ENOMEM;
  1133. }
  1134. }
  1135. iwl_reset_traffic_log(priv);
  1136. return 0;
  1137. }
  1138. void iwl_free_traffic_mem(struct iwl_priv *priv)
  1139. {
  1140. kfree(priv->tx_traffic);
  1141. priv->tx_traffic = NULL;
  1142. kfree(priv->rx_traffic);
  1143. priv->rx_traffic = NULL;
  1144. }
  1145. void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
  1146. u16 length, struct ieee80211_hdr *header)
  1147. {
  1148. __le16 fc;
  1149. u16 len;
  1150. if (likely(!(iwl_get_debug_level(priv->shrd) & IWL_DL_TX)))
  1151. return;
  1152. if (!priv->tx_traffic)
  1153. return;
  1154. fc = header->frame_control;
  1155. if (ieee80211_is_data(fc)) {
  1156. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  1157. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  1158. memcpy((priv->tx_traffic +
  1159. (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  1160. header, len);
  1161. priv->tx_traffic_idx =
  1162. (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  1163. }
  1164. }
  1165. void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
  1166. u16 length, struct ieee80211_hdr *header)
  1167. {
  1168. __le16 fc;
  1169. u16 len;
  1170. if (likely(!(iwl_get_debug_level(priv->shrd) & IWL_DL_RX)))
  1171. return;
  1172. if (!priv->rx_traffic)
  1173. return;
  1174. fc = header->frame_control;
  1175. if (ieee80211_is_data(fc)) {
  1176. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  1177. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  1178. memcpy((priv->rx_traffic +
  1179. (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  1180. header, len);
  1181. priv->rx_traffic_idx =
  1182. (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  1183. }
  1184. }
  1185. const char *get_mgmt_string(int cmd)
  1186. {
  1187. switch (cmd) {
  1188. IWL_CMD(MANAGEMENT_ASSOC_REQ);
  1189. IWL_CMD(MANAGEMENT_ASSOC_RESP);
  1190. IWL_CMD(MANAGEMENT_REASSOC_REQ);
  1191. IWL_CMD(MANAGEMENT_REASSOC_RESP);
  1192. IWL_CMD(MANAGEMENT_PROBE_REQ);
  1193. IWL_CMD(MANAGEMENT_PROBE_RESP);
  1194. IWL_CMD(MANAGEMENT_BEACON);
  1195. IWL_CMD(MANAGEMENT_ATIM);
  1196. IWL_CMD(MANAGEMENT_DISASSOC);
  1197. IWL_CMD(MANAGEMENT_AUTH);
  1198. IWL_CMD(MANAGEMENT_DEAUTH);
  1199. IWL_CMD(MANAGEMENT_ACTION);
  1200. default:
  1201. return "UNKNOWN";
  1202. }
  1203. }
  1204. const char *get_ctrl_string(int cmd)
  1205. {
  1206. switch (cmd) {
  1207. IWL_CMD(CONTROL_BACK_REQ);
  1208. IWL_CMD(CONTROL_BACK);
  1209. IWL_CMD(CONTROL_PSPOLL);
  1210. IWL_CMD(CONTROL_RTS);
  1211. IWL_CMD(CONTROL_CTS);
  1212. IWL_CMD(CONTROL_ACK);
  1213. IWL_CMD(CONTROL_CFEND);
  1214. IWL_CMD(CONTROL_CFENDACK);
  1215. default:
  1216. return "UNKNOWN";
  1217. }
  1218. }
  1219. void iwl_clear_traffic_stats(struct iwl_priv *priv)
  1220. {
  1221. memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
  1222. memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
  1223. }
  1224. /*
  1225. * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
  1226. * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
  1227. * Use debugFs to display the rx/rx_statistics
  1228. * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
  1229. * information will be recorded, but DATA pkt still will be recorded
  1230. * for the reason of iwl_led.c need to control the led blinking based on
  1231. * number of tx and rx data.
  1232. *
  1233. */
  1234. void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
  1235. {
  1236. struct traffic_stats *stats;
  1237. if (is_tx)
  1238. stats = &priv->tx_stats;
  1239. else
  1240. stats = &priv->rx_stats;
  1241. if (ieee80211_is_mgmt(fc)) {
  1242. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  1243. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  1244. stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
  1245. break;
  1246. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  1247. stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
  1248. break;
  1249. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  1250. stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
  1251. break;
  1252. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  1253. stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
  1254. break;
  1255. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  1256. stats->mgmt[MANAGEMENT_PROBE_REQ]++;
  1257. break;
  1258. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  1259. stats->mgmt[MANAGEMENT_PROBE_RESP]++;
  1260. break;
  1261. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  1262. stats->mgmt[MANAGEMENT_BEACON]++;
  1263. break;
  1264. case cpu_to_le16(IEEE80211_STYPE_ATIM):
  1265. stats->mgmt[MANAGEMENT_ATIM]++;
  1266. break;
  1267. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  1268. stats->mgmt[MANAGEMENT_DISASSOC]++;
  1269. break;
  1270. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  1271. stats->mgmt[MANAGEMENT_AUTH]++;
  1272. break;
  1273. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  1274. stats->mgmt[MANAGEMENT_DEAUTH]++;
  1275. break;
  1276. case cpu_to_le16(IEEE80211_STYPE_ACTION):
  1277. stats->mgmt[MANAGEMENT_ACTION]++;
  1278. break;
  1279. }
  1280. } else if (ieee80211_is_ctl(fc)) {
  1281. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  1282. case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
  1283. stats->ctrl[CONTROL_BACK_REQ]++;
  1284. break;
  1285. case cpu_to_le16(IEEE80211_STYPE_BACK):
  1286. stats->ctrl[CONTROL_BACK]++;
  1287. break;
  1288. case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
  1289. stats->ctrl[CONTROL_PSPOLL]++;
  1290. break;
  1291. case cpu_to_le16(IEEE80211_STYPE_RTS):
  1292. stats->ctrl[CONTROL_RTS]++;
  1293. break;
  1294. case cpu_to_le16(IEEE80211_STYPE_CTS):
  1295. stats->ctrl[CONTROL_CTS]++;
  1296. break;
  1297. case cpu_to_le16(IEEE80211_STYPE_ACK):
  1298. stats->ctrl[CONTROL_ACK]++;
  1299. break;
  1300. case cpu_to_le16(IEEE80211_STYPE_CFEND):
  1301. stats->ctrl[CONTROL_CFEND]++;
  1302. break;
  1303. case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
  1304. stats->ctrl[CONTROL_CFENDACK]++;
  1305. break;
  1306. }
  1307. } else {
  1308. /* data */
  1309. stats->data_cnt++;
  1310. stats->data_bytes += len;
  1311. }
  1312. }
  1313. #endif
  1314. static void iwl_force_rf_reset(struct iwl_priv *priv)
  1315. {
  1316. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  1317. return;
  1318. if (!iwl_is_any_associated(priv)) {
  1319. IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
  1320. return;
  1321. }
  1322. /*
  1323. * There is no easy and better way to force reset the radio,
  1324. * the only known method is switching channel which will force to
  1325. * reset and tune the radio.
  1326. * Use internal short scan (single channel) operation to should
  1327. * achieve this objective.
  1328. * Driver should reset the radio when number of consecutive missed
  1329. * beacon, or any other uCode error condition detected.
  1330. */
  1331. IWL_DEBUG_INFO(priv, "perform radio reset.\n");
  1332. iwl_internal_short_hw_scan(priv);
  1333. }
  1334. int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
  1335. {
  1336. struct iwl_force_reset *force_reset;
  1337. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  1338. return -EINVAL;
  1339. if (mode >= IWL_MAX_FORCE_RESET) {
  1340. IWL_DEBUG_INFO(priv, "invalid reset request.\n");
  1341. return -EINVAL;
  1342. }
  1343. force_reset = &priv->force_reset[mode];
  1344. force_reset->reset_request_count++;
  1345. if (!external) {
  1346. if (force_reset->last_force_reset_jiffies &&
  1347. time_after(force_reset->last_force_reset_jiffies +
  1348. force_reset->reset_duration, jiffies)) {
  1349. IWL_DEBUG_INFO(priv, "force reset rejected\n");
  1350. force_reset->reset_reject_count++;
  1351. return -EAGAIN;
  1352. }
  1353. }
  1354. force_reset->reset_success_count++;
  1355. force_reset->last_force_reset_jiffies = jiffies;
  1356. IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
  1357. switch (mode) {
  1358. case IWL_RF_RESET:
  1359. iwl_force_rf_reset(priv);
  1360. break;
  1361. case IWL_FW_RESET:
  1362. /*
  1363. * if the request is from external(ex: debugfs),
  1364. * then always perform the request in regardless the module
  1365. * parameter setting
  1366. * if the request is from internal (uCode error or driver
  1367. * detect failure), then fw_restart module parameter
  1368. * need to be check before performing firmware reload
  1369. */
  1370. if (!external && !iwlagn_mod_params.restart_fw) {
  1371. IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
  1372. "module parameter setting\n");
  1373. break;
  1374. }
  1375. IWL_ERR(priv, "On demand firmware reload\n");
  1376. iwlagn_fw_error(priv, true);
  1377. break;
  1378. }
  1379. return 0;
  1380. }
  1381. int iwl_mac_change_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  1382. enum nl80211_iftype newtype, bool newp2p)
  1383. {
  1384. struct iwl_priv *priv = hw->priv;
  1385. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1386. struct iwl_rxon_context *bss_ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1387. struct iwl_rxon_context *tmp;
  1388. enum nl80211_iftype newviftype = newtype;
  1389. u32 interface_modes;
  1390. int err;
  1391. newtype = ieee80211_iftype_p2p(newtype, newp2p);
  1392. mutex_lock(&priv->shrd->mutex);
  1393. if (!ctx->vif || !iwl_is_ready_rf(priv->shrd)) {
  1394. /*
  1395. * Huh? But wait ... this can maybe happen when
  1396. * we're in the middle of a firmware restart!
  1397. */
  1398. err = -EBUSY;
  1399. goto out;
  1400. }
  1401. interface_modes = ctx->interface_modes | ctx->exclusive_interface_modes;
  1402. if (!(interface_modes & BIT(newtype))) {
  1403. err = -EBUSY;
  1404. goto out;
  1405. }
  1406. /*
  1407. * Refuse a change that should be done by moving from the PAN
  1408. * context to the BSS context instead, if the BSS context is
  1409. * available and can support the new interface type.
  1410. */
  1411. if (ctx->ctxid == IWL_RXON_CTX_PAN && !bss_ctx->vif &&
  1412. (bss_ctx->interface_modes & BIT(newtype) ||
  1413. bss_ctx->exclusive_interface_modes & BIT(newtype))) {
  1414. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  1415. err = -EBUSY;
  1416. goto out;
  1417. }
  1418. if (ctx->exclusive_interface_modes & BIT(newtype)) {
  1419. for_each_context(priv, tmp) {
  1420. if (ctx == tmp)
  1421. continue;
  1422. if (!tmp->vif)
  1423. continue;
  1424. /*
  1425. * The current mode switch would be exclusive, but
  1426. * another context is active ... refuse the switch.
  1427. */
  1428. err = -EBUSY;
  1429. goto out;
  1430. }
  1431. }
  1432. /* success */
  1433. iwl_teardown_interface(priv, vif, true);
  1434. vif->type = newviftype;
  1435. vif->p2p = newp2p;
  1436. err = iwl_setup_interface(priv, ctx);
  1437. WARN_ON(err);
  1438. /*
  1439. * We've switched internally, but submitting to the
  1440. * device may have failed for some reason. Mask this
  1441. * error, because otherwise mac80211 will not switch
  1442. * (and set the interface type back) and we'll be
  1443. * out of sync with it.
  1444. */
  1445. err = 0;
  1446. out:
  1447. mutex_unlock(&priv->shrd->mutex);
  1448. return err;
  1449. }
  1450. static inline int iwl_check_stuck_queue(struct iwl_priv *priv, int txq)
  1451. {
  1452. if (iwl_trans_check_stuck_queue(trans(priv), txq)) {
  1453. int ret = iwl_force_reset(priv, IWL_FW_RESET, false);
  1454. return (ret == -EAGAIN) ? 0 : 1;
  1455. }
  1456. return 0;
  1457. }
  1458. /*
  1459. * Making watchdog tick be a quarter of timeout assure we will
  1460. * discover the queue hung between timeout and 1.25*timeout
  1461. */
  1462. #define IWL_WD_TICK(timeout) ((timeout) / 4)
  1463. /*
  1464. * Watchdog timer callback, we check each tx queue for stuck, if if hung
  1465. * we reset the firmware. If everything is fine just rearm the timer.
  1466. */
  1467. void iwl_bg_watchdog(unsigned long data)
  1468. {
  1469. struct iwl_priv *priv = (struct iwl_priv *)data;
  1470. int cnt;
  1471. unsigned long timeout;
  1472. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  1473. return;
  1474. timeout = priv->cfg->base_params->wd_timeout;
  1475. if (timeout == 0)
  1476. return;
  1477. /* monitor and check for stuck cmd queue */
  1478. if (iwl_check_stuck_queue(priv, priv->shrd->cmd_queue))
  1479. return;
  1480. /* monitor and check for other stuck queues */
  1481. if (iwl_is_any_associated(priv)) {
  1482. for (cnt = 0; cnt < hw_params(priv).max_txq_num; cnt++) {
  1483. /* skip as we already checked the command queue */
  1484. if (cnt == priv->shrd->cmd_queue)
  1485. continue;
  1486. if (iwl_check_stuck_queue(priv, cnt))
  1487. return;
  1488. }
  1489. }
  1490. mod_timer(&priv->watchdog, jiffies +
  1491. msecs_to_jiffies(IWL_WD_TICK(timeout)));
  1492. }
  1493. void iwl_setup_watchdog(struct iwl_priv *priv)
  1494. {
  1495. unsigned int timeout = priv->cfg->base_params->wd_timeout;
  1496. if (timeout && !iwlagn_mod_params.wd_disable)
  1497. mod_timer(&priv->watchdog,
  1498. jiffies + msecs_to_jiffies(IWL_WD_TICK(timeout)));
  1499. else
  1500. del_timer(&priv->watchdog);
  1501. }
  1502. /*
  1503. * extended beacon time format
  1504. * time in usec will be changed into a 32-bit value in extended:internal format
  1505. * the extended part is the beacon counts
  1506. * the internal part is the time in usec within one beacon interval
  1507. */
  1508. u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
  1509. {
  1510. u32 quot;
  1511. u32 rem;
  1512. u32 interval = beacon_interval * TIME_UNIT;
  1513. if (!interval || !usec)
  1514. return 0;
  1515. quot = (usec / interval) &
  1516. (iwl_beacon_time_mask_high(priv, IWLAGN_EXT_BEACON_TIME_POS) >>
  1517. IWLAGN_EXT_BEACON_TIME_POS);
  1518. rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
  1519. IWLAGN_EXT_BEACON_TIME_POS);
  1520. return (quot << IWLAGN_EXT_BEACON_TIME_POS) + rem;
  1521. }
  1522. /* base is usually what we get from ucode with each received frame,
  1523. * the same as HW timer counter counting down
  1524. */
  1525. __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
  1526. u32 addon, u32 beacon_interval)
  1527. {
  1528. u32 base_low = base & iwl_beacon_time_mask_low(priv,
  1529. IWLAGN_EXT_BEACON_TIME_POS);
  1530. u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
  1531. IWLAGN_EXT_BEACON_TIME_POS);
  1532. u32 interval = beacon_interval * TIME_UNIT;
  1533. u32 res = (base & iwl_beacon_time_mask_high(priv,
  1534. IWLAGN_EXT_BEACON_TIME_POS)) +
  1535. (addon & iwl_beacon_time_mask_high(priv,
  1536. IWLAGN_EXT_BEACON_TIME_POS));
  1537. if (base_low > addon_low)
  1538. res += base_low - addon_low;
  1539. else if (base_low < addon_low) {
  1540. res += interval + base_low - addon_low;
  1541. res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
  1542. } else
  1543. res += (1 << IWLAGN_EXT_BEACON_TIME_POS);
  1544. return cpu_to_le32(res);
  1545. }
  1546. void iwl_start_tx_ba_trans_ready(struct iwl_priv *priv,
  1547. enum iwl_rxon_context_id ctx,
  1548. u8 sta_id, u8 tid)
  1549. {
  1550. struct ieee80211_vif *vif;
  1551. u8 *addr = priv->stations[sta_id].sta.sta.addr;
  1552. if (ctx == NUM_IWL_RXON_CTX)
  1553. ctx = priv->stations[sta_id].ctxid;
  1554. vif = priv->contexts[ctx].vif;
  1555. ieee80211_start_tx_ba_cb_irqsafe(vif, addr, tid);
  1556. }
  1557. void iwl_stop_tx_ba_trans_ready(struct iwl_priv *priv,
  1558. enum iwl_rxon_context_id ctx,
  1559. u8 sta_id, u8 tid)
  1560. {
  1561. struct ieee80211_vif *vif;
  1562. u8 *addr = priv->stations[sta_id].sta.sta.addr;
  1563. if (ctx == NUM_IWL_RXON_CTX)
  1564. ctx = priv->stations[sta_id].ctxid;
  1565. vif = priv->contexts[ctx].vif;
  1566. ieee80211_stop_tx_ba_cb_irqsafe(vif, addr, tid);
  1567. }
  1568. void iwl_set_hw_rfkill_state(struct iwl_priv *priv, bool state)
  1569. {
  1570. wiphy_rfkill_set_hw_state(priv->hw->wiphy, state);
  1571. }
  1572. void iwl_nic_config(struct iwl_priv *priv)
  1573. {
  1574. priv->cfg->lib->nic_config(priv);
  1575. }
  1576. void iwl_free_skb(struct iwl_priv *priv, struct sk_buff *skb)
  1577. {
  1578. struct ieee80211_tx_info *info;
  1579. info = IEEE80211_SKB_CB(skb);
  1580. kmem_cache_free(priv->tx_cmd_pool, (info->driver_data[1]));
  1581. dev_kfree_skb_any(skb);
  1582. }
  1583. void iwl_stop_sw_queue(struct iwl_priv *priv, u8 ac)
  1584. {
  1585. ieee80211_stop_queue(priv->hw, ac);
  1586. }
  1587. void iwl_wake_sw_queue(struct iwl_priv *priv, u8 ac)
  1588. {
  1589. ieee80211_wake_queue(priv->hw, ac);
  1590. }