iwl-core.c 53 KB

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