iwl-core.c 58 KB

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