iwl-core.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  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. struct ieee80211_ops *hw_ops)
  73. {
  74. struct iwl_priv *priv;
  75. /* mac80211 allocates memory for this device instance, including
  76. * space for this driver's private structure */
  77. struct ieee80211_hw *hw =
  78. ieee80211_alloc_hw(sizeof(struct iwl_priv), hw_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. /*
  91. * QoS support
  92. */
  93. static void iwl_update_qos(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  94. {
  95. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  96. return;
  97. if (!ctx->is_active)
  98. return;
  99. ctx->qos_data.def_qos_parm.qos_flags = 0;
  100. if (ctx->qos_data.qos_active)
  101. ctx->qos_data.def_qos_parm.qos_flags |=
  102. QOS_PARAM_FLG_UPDATE_EDCA_MSK;
  103. if (ctx->ht.enabled)
  104. ctx->qos_data.def_qos_parm.qos_flags |= QOS_PARAM_FLG_TGN_MSK;
  105. IWL_DEBUG_QOS(priv, "send QoS cmd with Qos active=%d FLAGS=0x%X\n",
  106. ctx->qos_data.qos_active,
  107. ctx->qos_data.def_qos_parm.qos_flags);
  108. iwl_send_cmd_pdu_async(priv, ctx->qos_cmd,
  109. sizeof(struct iwl_qosparam_cmd),
  110. &ctx->qos_data.def_qos_parm, NULL);
  111. }
  112. #define MAX_BIT_RATE_40_MHZ 150 /* Mbps */
  113. #define MAX_BIT_RATE_20_MHZ 72 /* Mbps */
  114. static void iwlcore_init_ht_hw_capab(const struct iwl_priv *priv,
  115. struct ieee80211_sta_ht_cap *ht_info,
  116. enum ieee80211_band band)
  117. {
  118. u16 max_bit_rate = 0;
  119. u8 rx_chains_num = priv->hw_params.rx_chains_num;
  120. u8 tx_chains_num = priv->hw_params.tx_chains_num;
  121. ht_info->cap = 0;
  122. memset(&ht_info->mcs, 0, sizeof(ht_info->mcs));
  123. ht_info->ht_supported = true;
  124. if (priv->cfg->ht_params &&
  125. priv->cfg->ht_params->ht_greenfield_support)
  126. ht_info->cap |= IEEE80211_HT_CAP_GRN_FLD;
  127. ht_info->cap |= IEEE80211_HT_CAP_SGI_20;
  128. max_bit_rate = MAX_BIT_RATE_20_MHZ;
  129. if (priv->hw_params.ht40_channel & BIT(band)) {
  130. ht_info->cap |= IEEE80211_HT_CAP_SUP_WIDTH_20_40;
  131. ht_info->cap |= IEEE80211_HT_CAP_SGI_40;
  132. ht_info->mcs.rx_mask[4] = 0x01;
  133. max_bit_rate = MAX_BIT_RATE_40_MHZ;
  134. }
  135. if (priv->cfg->mod_params->amsdu_size_8K)
  136. ht_info->cap |= IEEE80211_HT_CAP_MAX_AMSDU;
  137. ht_info->ampdu_factor = CFG_HT_RX_AMPDU_FACTOR_DEF;
  138. if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_factor)
  139. ht_info->ampdu_factor = priv->cfg->bt_params->ampdu_factor;
  140. ht_info->ampdu_density = CFG_HT_MPDU_DENSITY_DEF;
  141. if (priv->cfg->bt_params && priv->cfg->bt_params->ampdu_density)
  142. ht_info->ampdu_density = priv->cfg->bt_params->ampdu_density;
  143. ht_info->mcs.rx_mask[0] = 0xFF;
  144. if (rx_chains_num >= 2)
  145. ht_info->mcs.rx_mask[1] = 0xFF;
  146. if (rx_chains_num >= 3)
  147. ht_info->mcs.rx_mask[2] = 0xFF;
  148. /* Highest supported Rx data rate */
  149. max_bit_rate *= rx_chains_num;
  150. WARN_ON(max_bit_rate & ~IEEE80211_HT_MCS_RX_HIGHEST_MASK);
  151. ht_info->mcs.rx_highest = cpu_to_le16(max_bit_rate);
  152. /* Tx MCS capabilities */
  153. ht_info->mcs.tx_params = IEEE80211_HT_MCS_TX_DEFINED;
  154. if (tx_chains_num != rx_chains_num) {
  155. ht_info->mcs.tx_params |= IEEE80211_HT_MCS_TX_RX_DIFF;
  156. ht_info->mcs.tx_params |= ((tx_chains_num - 1) <<
  157. IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT);
  158. }
  159. }
  160. /**
  161. * iwlcore_init_geos - Initialize mac80211's geo/channel info based from eeprom
  162. */
  163. int iwlcore_init_geos(struct iwl_priv *priv)
  164. {
  165. struct iwl_channel_info *ch;
  166. struct ieee80211_supported_band *sband;
  167. struct ieee80211_channel *channels;
  168. struct ieee80211_channel *geo_ch;
  169. struct ieee80211_rate *rates;
  170. int i = 0;
  171. if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
  172. priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
  173. IWL_DEBUG_INFO(priv, "Geography modes already initialized.\n");
  174. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  175. return 0;
  176. }
  177. channels = kzalloc(sizeof(struct ieee80211_channel) *
  178. priv->channel_count, GFP_KERNEL);
  179. if (!channels)
  180. return -ENOMEM;
  181. rates = kzalloc((sizeof(struct ieee80211_rate) * IWL_RATE_COUNT_LEGACY),
  182. GFP_KERNEL);
  183. if (!rates) {
  184. kfree(channels);
  185. return -ENOMEM;
  186. }
  187. /* 5.2GHz channels start after the 2.4GHz channels */
  188. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  189. sband->channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
  190. /* just OFDM */
  191. sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
  192. sband->n_bitrates = IWL_RATE_COUNT_LEGACY - IWL_FIRST_OFDM_RATE;
  193. if (priv->cfg->sku & IWL_SKU_N)
  194. iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
  195. IEEE80211_BAND_5GHZ);
  196. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  197. sband->channels = channels;
  198. /* OFDM & CCK */
  199. sband->bitrates = rates;
  200. sband->n_bitrates = IWL_RATE_COUNT_LEGACY;
  201. if (priv->cfg->sku & IWL_SKU_N)
  202. iwlcore_init_ht_hw_capab(priv, &sband->ht_cap,
  203. IEEE80211_BAND_2GHZ);
  204. priv->ieee_channels = channels;
  205. priv->ieee_rates = rates;
  206. for (i = 0; i < priv->channel_count; i++) {
  207. ch = &priv->channel_info[i];
  208. /* FIXME: might be removed if scan is OK */
  209. if (!is_channel_valid(ch))
  210. continue;
  211. if (is_channel_a_band(ch))
  212. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  213. else
  214. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  215. geo_ch = &sband->channels[sband->n_channels++];
  216. geo_ch->center_freq =
  217. ieee80211_channel_to_frequency(ch->channel);
  218. geo_ch->max_power = ch->max_power_avg;
  219. geo_ch->max_antenna_gain = 0xff;
  220. geo_ch->hw_value = ch->channel;
  221. if (is_channel_valid(ch)) {
  222. if (!(ch->flags & EEPROM_CHANNEL_IBSS))
  223. geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
  224. if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
  225. geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  226. if (ch->flags & EEPROM_CHANNEL_RADAR)
  227. geo_ch->flags |= IEEE80211_CHAN_RADAR;
  228. geo_ch->flags |= ch->ht40_extension_channel;
  229. if (ch->max_power_avg > priv->tx_power_device_lmt)
  230. priv->tx_power_device_lmt = ch->max_power_avg;
  231. } else {
  232. geo_ch->flags |= IEEE80211_CHAN_DISABLED;
  233. }
  234. IWL_DEBUG_INFO(priv, "Channel %d Freq=%d[%sGHz] %s flag=0x%X\n",
  235. ch->channel, geo_ch->center_freq,
  236. is_channel_a_band(ch) ? "5.2" : "2.4",
  237. geo_ch->flags & IEEE80211_CHAN_DISABLED ?
  238. "restricted" : "valid",
  239. geo_ch->flags);
  240. }
  241. if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
  242. priv->cfg->sku & IWL_SKU_A) {
  243. IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
  244. "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
  245. priv->pci_dev->device,
  246. priv->pci_dev->subsystem_device);
  247. priv->cfg->sku &= ~IWL_SKU_A;
  248. }
  249. IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
  250. priv->bands[IEEE80211_BAND_2GHZ].n_channels,
  251. priv->bands[IEEE80211_BAND_5GHZ].n_channels);
  252. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  253. return 0;
  254. }
  255. EXPORT_SYMBOL(iwlcore_init_geos);
  256. /*
  257. * iwlcore_free_geos - undo allocations in iwlcore_init_geos
  258. */
  259. void iwlcore_free_geos(struct iwl_priv *priv)
  260. {
  261. kfree(priv->ieee_channels);
  262. kfree(priv->ieee_rates);
  263. clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
  264. }
  265. EXPORT_SYMBOL(iwlcore_free_geos);
  266. /*
  267. * iwlcore_tx_cmd_protection: Set rts/cts. 3945 and 4965 only share this
  268. * function.
  269. */
  270. void iwlcore_tx_cmd_protection(struct iwl_priv *priv,
  271. struct ieee80211_tx_info *info,
  272. __le16 fc, __le32 *tx_flags)
  273. {
  274. if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  275. *tx_flags |= TX_CMD_FLG_RTS_MSK;
  276. *tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  277. *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  278. if (!ieee80211_is_mgmt(fc))
  279. return;
  280. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  281. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  282. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  283. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  284. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  285. *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  286. *tx_flags |= TX_CMD_FLG_CTS_MSK;
  287. break;
  288. }
  289. } else if (info->control.rates[0].flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  290. *tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  291. *tx_flags |= TX_CMD_FLG_CTS_MSK;
  292. *tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  293. }
  294. }
  295. EXPORT_SYMBOL(iwlcore_tx_cmd_protection);
  296. static bool iwl_is_channel_extension(struct iwl_priv *priv,
  297. enum ieee80211_band band,
  298. u16 channel, u8 extension_chan_offset)
  299. {
  300. const struct iwl_channel_info *ch_info;
  301. ch_info = iwl_get_channel_info(priv, band, channel);
  302. if (!is_channel_valid(ch_info))
  303. return false;
  304. if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_ABOVE)
  305. return !(ch_info->ht40_extension_channel &
  306. IEEE80211_CHAN_NO_HT40PLUS);
  307. else if (extension_chan_offset == IEEE80211_HT_PARAM_CHA_SEC_BELOW)
  308. return !(ch_info->ht40_extension_channel &
  309. IEEE80211_CHAN_NO_HT40MINUS);
  310. return false;
  311. }
  312. bool iwl_is_ht40_tx_allowed(struct iwl_priv *priv,
  313. struct iwl_rxon_context *ctx,
  314. struct ieee80211_sta_ht_cap *ht_cap)
  315. {
  316. if (!ctx->ht.enabled || !ctx->ht.is_40mhz)
  317. return false;
  318. /*
  319. * We do not check for IEEE80211_HT_CAP_SUP_WIDTH_20_40
  320. * the bit will not set if it is pure 40MHz case
  321. */
  322. if (ht_cap && !ht_cap->ht_supported)
  323. return false;
  324. #ifdef CONFIG_IWLWIFI_DEBUGFS
  325. if (priv->disable_ht40)
  326. return false;
  327. #endif
  328. return iwl_is_channel_extension(priv, priv->band,
  329. le16_to_cpu(ctx->staging.channel),
  330. ctx->ht.extension_chan_offset);
  331. }
  332. EXPORT_SYMBOL(iwl_is_ht40_tx_allowed);
  333. static u16 iwl_adjust_beacon_interval(u16 beacon_val, u16 max_beacon_val)
  334. {
  335. u16 new_val;
  336. u16 beacon_factor;
  337. /*
  338. * If mac80211 hasn't given us a beacon interval, program
  339. * the default into the device (not checking this here
  340. * would cause the adjustment below to return the maximum
  341. * value, which may break PAN.)
  342. */
  343. if (!beacon_val)
  344. return DEFAULT_BEACON_INTERVAL;
  345. /*
  346. * If the beacon interval we obtained from the peer
  347. * is too large, we'll have to wake up more often
  348. * (and in IBSS case, we'll beacon too much)
  349. *
  350. * For example, if max_beacon_val is 4096, and the
  351. * requested beacon interval is 7000, we'll have to
  352. * use 3500 to be able to wake up on the beacons.
  353. *
  354. * This could badly influence beacon detection stats.
  355. */
  356. beacon_factor = (beacon_val + max_beacon_val) / max_beacon_val;
  357. new_val = beacon_val / beacon_factor;
  358. if (!new_val)
  359. new_val = max_beacon_val;
  360. return new_val;
  361. }
  362. int iwl_send_rxon_timing(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  363. {
  364. u64 tsf;
  365. s32 interval_tm, rem;
  366. struct ieee80211_conf *conf = NULL;
  367. u16 beacon_int;
  368. struct ieee80211_vif *vif = ctx->vif;
  369. conf = ieee80211_get_hw_conf(priv->hw);
  370. lockdep_assert_held(&priv->mutex);
  371. memset(&ctx->timing, 0, sizeof(struct iwl_rxon_time_cmd));
  372. ctx->timing.timestamp = cpu_to_le64(priv->timestamp);
  373. ctx->timing.listen_interval = cpu_to_le16(conf->listen_interval);
  374. beacon_int = vif ? vif->bss_conf.beacon_int : 0;
  375. /*
  376. * TODO: For IBSS we need to get atim_window from mac80211,
  377. * for now just always use 0
  378. */
  379. ctx->timing.atim_window = 0;
  380. if (ctx->ctxid == IWL_RXON_CTX_PAN &&
  381. (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION) &&
  382. iwl_is_associated(priv, IWL_RXON_CTX_BSS) &&
  383. priv->contexts[IWL_RXON_CTX_BSS].vif &&
  384. priv->contexts[IWL_RXON_CTX_BSS].vif->bss_conf.beacon_int) {
  385. ctx->timing.beacon_interval =
  386. priv->contexts[IWL_RXON_CTX_BSS].timing.beacon_interval;
  387. beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
  388. } else if (ctx->ctxid == IWL_RXON_CTX_BSS &&
  389. iwl_is_associated(priv, IWL_RXON_CTX_PAN) &&
  390. priv->contexts[IWL_RXON_CTX_PAN].vif &&
  391. priv->contexts[IWL_RXON_CTX_PAN].vif->bss_conf.beacon_int &&
  392. (!iwl_is_associated_ctx(ctx) || !ctx->vif ||
  393. !ctx->vif->bss_conf.beacon_int)) {
  394. ctx->timing.beacon_interval =
  395. priv->contexts[IWL_RXON_CTX_PAN].timing.beacon_interval;
  396. beacon_int = le16_to_cpu(ctx->timing.beacon_interval);
  397. } else {
  398. beacon_int = iwl_adjust_beacon_interval(beacon_int,
  399. priv->hw_params.max_beacon_itrvl * TIME_UNIT);
  400. ctx->timing.beacon_interval = cpu_to_le16(beacon_int);
  401. }
  402. tsf = priv->timestamp; /* tsf is modifed by do_div: copy it */
  403. interval_tm = beacon_int * TIME_UNIT;
  404. rem = do_div(tsf, interval_tm);
  405. ctx->timing.beacon_init_val = cpu_to_le32(interval_tm - rem);
  406. ctx->timing.dtim_period = vif ? (vif->bss_conf.dtim_period ?: 1) : 1;
  407. IWL_DEBUG_ASSOC(priv,
  408. "beacon interval %d beacon timer %d beacon tim %d\n",
  409. le16_to_cpu(ctx->timing.beacon_interval),
  410. le32_to_cpu(ctx->timing.beacon_init_val),
  411. le16_to_cpu(ctx->timing.atim_window));
  412. return iwl_send_cmd_pdu(priv, ctx->rxon_timing_cmd,
  413. sizeof(ctx->timing), &ctx->timing);
  414. }
  415. EXPORT_SYMBOL(iwl_send_rxon_timing);
  416. void iwl_set_rxon_hwcrypto(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
  417. int hw_decrypt)
  418. {
  419. struct iwl_rxon_cmd *rxon = &ctx->staging;
  420. if (hw_decrypt)
  421. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  422. else
  423. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  424. }
  425. EXPORT_SYMBOL(iwl_set_rxon_hwcrypto);
  426. /* validate RXON structure is valid */
  427. int iwl_check_rxon_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
  428. {
  429. struct iwl_rxon_cmd *rxon = &ctx->staging;
  430. bool error = false;
  431. if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
  432. if (rxon->flags & RXON_FLG_TGJ_NARROW_BAND_MSK) {
  433. IWL_WARN(priv, "check 2.4G: wrong narrow\n");
  434. error = true;
  435. }
  436. if (rxon->flags & RXON_FLG_RADAR_DETECT_MSK) {
  437. IWL_WARN(priv, "check 2.4G: wrong radar\n");
  438. error = true;
  439. }
  440. } else {
  441. if (!(rxon->flags & RXON_FLG_SHORT_SLOT_MSK)) {
  442. IWL_WARN(priv, "check 5.2G: not short slot!\n");
  443. error = true;
  444. }
  445. if (rxon->flags & RXON_FLG_CCK_MSK) {
  446. IWL_WARN(priv, "check 5.2G: CCK!\n");
  447. error = true;
  448. }
  449. }
  450. if ((rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1) {
  451. IWL_WARN(priv, "mac/bssid mcast!\n");
  452. error = true;
  453. }
  454. /* make sure basic rates 6Mbps and 1Mbps are supported */
  455. if ((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0 &&
  456. (rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0) {
  457. IWL_WARN(priv, "neither 1 nor 6 are basic\n");
  458. error = true;
  459. }
  460. if (le16_to_cpu(rxon->assoc_id) > 2007) {
  461. IWL_WARN(priv, "aid > 2007\n");
  462. error = true;
  463. }
  464. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
  465. == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK)) {
  466. IWL_WARN(priv, "CCK and short slot\n");
  467. error = true;
  468. }
  469. if ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
  470. == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK)) {
  471. IWL_WARN(priv, "CCK and auto detect");
  472. error = true;
  473. }
  474. if ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
  475. RXON_FLG_TGG_PROTECT_MSK)) ==
  476. RXON_FLG_TGG_PROTECT_MSK) {
  477. IWL_WARN(priv, "TGg but no auto-detect\n");
  478. error = true;
  479. }
  480. if (error)
  481. IWL_WARN(priv, "Tuning to channel %d\n",
  482. le16_to_cpu(rxon->channel));
  483. if (error) {
  484. IWL_ERR(priv, "Invalid RXON\n");
  485. return -EINVAL;
  486. }
  487. return 0;
  488. }
  489. EXPORT_SYMBOL(iwl_check_rxon_cmd);
  490. /**
  491. * iwl_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
  492. * @priv: staging_rxon is compared to active_rxon
  493. *
  494. * If the RXON structure is changing enough to require a new tune,
  495. * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
  496. * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
  497. */
  498. int iwl_full_rxon_required(struct iwl_priv *priv,
  499. struct iwl_rxon_context *ctx)
  500. {
  501. const struct iwl_rxon_cmd *staging = &ctx->staging;
  502. const struct iwl_rxon_cmd *active = &ctx->active;
  503. #define CHK(cond) \
  504. if ((cond)) { \
  505. IWL_DEBUG_INFO(priv, "need full RXON - " #cond "\n"); \
  506. return 1; \
  507. }
  508. #define CHK_NEQ(c1, c2) \
  509. if ((c1) != (c2)) { \
  510. IWL_DEBUG_INFO(priv, "need full RXON - " \
  511. #c1 " != " #c2 " - %d != %d\n", \
  512. (c1), (c2)); \
  513. return 1; \
  514. }
  515. /* These items are only settable from the full RXON command */
  516. CHK(!iwl_is_associated_ctx(ctx));
  517. CHK(compare_ether_addr(staging->bssid_addr, active->bssid_addr));
  518. CHK(compare_ether_addr(staging->node_addr, active->node_addr));
  519. CHK(compare_ether_addr(staging->wlap_bssid_addr,
  520. active->wlap_bssid_addr));
  521. CHK_NEQ(staging->dev_type, active->dev_type);
  522. CHK_NEQ(staging->channel, active->channel);
  523. CHK_NEQ(staging->air_propagation, active->air_propagation);
  524. CHK_NEQ(staging->ofdm_ht_single_stream_basic_rates,
  525. active->ofdm_ht_single_stream_basic_rates);
  526. CHK_NEQ(staging->ofdm_ht_dual_stream_basic_rates,
  527. active->ofdm_ht_dual_stream_basic_rates);
  528. CHK_NEQ(staging->ofdm_ht_triple_stream_basic_rates,
  529. active->ofdm_ht_triple_stream_basic_rates);
  530. CHK_NEQ(staging->assoc_id, active->assoc_id);
  531. /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
  532. * be updated with the RXON_ASSOC command -- however only some
  533. * flag transitions are allowed using RXON_ASSOC */
  534. /* Check if we are not switching bands */
  535. CHK_NEQ(staging->flags & RXON_FLG_BAND_24G_MSK,
  536. active->flags & RXON_FLG_BAND_24G_MSK);
  537. /* Check if we are switching association toggle */
  538. CHK_NEQ(staging->filter_flags & RXON_FILTER_ASSOC_MSK,
  539. active->filter_flags & RXON_FILTER_ASSOC_MSK);
  540. #undef CHK
  541. #undef CHK_NEQ
  542. return 0;
  543. }
  544. EXPORT_SYMBOL(iwl_full_rxon_required);
  545. u8 iwl_rate_get_lowest_plcp(struct iwl_priv *priv,
  546. struct iwl_rxon_context *ctx)
  547. {
  548. /*
  549. * Assign the lowest rate -- should really get this from
  550. * the beacon skb from mac80211.
  551. */
  552. if (ctx->staging.flags & RXON_FLG_BAND_24G_MSK)
  553. return IWL_RATE_1M_PLCP;
  554. else
  555. return IWL_RATE_6M_PLCP;
  556. }
  557. EXPORT_SYMBOL(iwl_rate_get_lowest_plcp);
  558. static void _iwl_set_rxon_ht(struct iwl_priv *priv,
  559. struct iwl_ht_config *ht_conf,
  560. struct iwl_rxon_context *ctx)
  561. {
  562. struct iwl_rxon_cmd *rxon = &ctx->staging;
  563. if (!ctx->ht.enabled) {
  564. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  565. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK |
  566. RXON_FLG_HT40_PROT_MSK |
  567. RXON_FLG_HT_PROT_MSK);
  568. return;
  569. }
  570. /* FIXME: if the definition of ht.protection changed, the "translation"
  571. * will be needed for rxon->flags
  572. */
  573. rxon->flags |= cpu_to_le32(ctx->ht.protection << RXON_FLG_HT_OPERATING_MODE_POS);
  574. /* Set up channel bandwidth:
  575. * 20 MHz only, 20/40 mixed or pure 40 if ht40 ok */
  576. /* clear the HT channel mode before set the mode */
  577. rxon->flags &= ~(RXON_FLG_CHANNEL_MODE_MSK |
  578. RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  579. if (iwl_is_ht40_tx_allowed(priv, ctx, NULL)) {
  580. /* pure ht40 */
  581. if (ctx->ht.protection == IEEE80211_HT_OP_MODE_PROTECTION_20MHZ) {
  582. rxon->flags |= RXON_FLG_CHANNEL_MODE_PURE_40;
  583. /* Note: control channel is opposite of extension channel */
  584. switch (ctx->ht.extension_chan_offset) {
  585. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  586. rxon->flags &= ~RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  587. break;
  588. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  589. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  590. break;
  591. }
  592. } else {
  593. /* Note: control channel is opposite of extension channel */
  594. switch (ctx->ht.extension_chan_offset) {
  595. case IEEE80211_HT_PARAM_CHA_SEC_ABOVE:
  596. rxon->flags &= ~(RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK);
  597. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  598. break;
  599. case IEEE80211_HT_PARAM_CHA_SEC_BELOW:
  600. rxon->flags |= RXON_FLG_CTRL_CHANNEL_LOC_HI_MSK;
  601. rxon->flags |= RXON_FLG_CHANNEL_MODE_MIXED;
  602. break;
  603. case IEEE80211_HT_PARAM_CHA_SEC_NONE:
  604. default:
  605. /* channel location only valid if in Mixed mode */
  606. IWL_ERR(priv, "invalid extension channel offset\n");
  607. break;
  608. }
  609. }
  610. } else {
  611. rxon->flags |= RXON_FLG_CHANNEL_MODE_LEGACY;
  612. }
  613. if (priv->cfg->ops->hcmd->set_rxon_chain)
  614. priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
  615. IWL_DEBUG_ASSOC(priv, "rxon flags 0x%X operation mode :0x%X "
  616. "extension channel offset 0x%x\n",
  617. le32_to_cpu(rxon->flags), ctx->ht.protection,
  618. ctx->ht.extension_chan_offset);
  619. }
  620. void iwl_set_rxon_ht(struct iwl_priv *priv, struct iwl_ht_config *ht_conf)
  621. {
  622. struct iwl_rxon_context *ctx;
  623. for_each_context(priv, ctx)
  624. _iwl_set_rxon_ht(priv, ht_conf, ctx);
  625. }
  626. EXPORT_SYMBOL(iwl_set_rxon_ht);
  627. /* Return valid, unused, channel for a passive scan to reset the RF */
  628. u8 iwl_get_single_channel_number(struct iwl_priv *priv,
  629. enum ieee80211_band band)
  630. {
  631. const struct iwl_channel_info *ch_info;
  632. int i;
  633. u8 channel = 0;
  634. u8 min, max;
  635. struct iwl_rxon_context *ctx;
  636. if (band == IEEE80211_BAND_5GHZ) {
  637. min = 14;
  638. max = priv->channel_count;
  639. } else {
  640. min = 0;
  641. max = 14;
  642. }
  643. for (i = min; i < max; i++) {
  644. bool busy = false;
  645. for_each_context(priv, ctx) {
  646. busy = priv->channel_info[i].channel ==
  647. le16_to_cpu(ctx->staging.channel);
  648. if (busy)
  649. break;
  650. }
  651. if (busy)
  652. continue;
  653. channel = priv->channel_info[i].channel;
  654. ch_info = iwl_get_channel_info(priv, band, channel);
  655. if (is_channel_valid(ch_info))
  656. break;
  657. }
  658. return channel;
  659. }
  660. EXPORT_SYMBOL(iwl_get_single_channel_number);
  661. /**
  662. * iwl_set_rxon_channel - Set the band and channel values in staging RXON
  663. * @ch: requested channel as a pointer to struct ieee80211_channel
  664. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  665. * in the staging RXON flag structure based on the ch->band
  666. */
  667. int iwl_set_rxon_channel(struct iwl_priv *priv, struct ieee80211_channel *ch,
  668. struct iwl_rxon_context *ctx)
  669. {
  670. enum ieee80211_band band = ch->band;
  671. u16 channel = ch->hw_value;
  672. if ((le16_to_cpu(ctx->staging.channel) == channel) &&
  673. (priv->band == band))
  674. return 0;
  675. ctx->staging.channel = cpu_to_le16(channel);
  676. if (band == IEEE80211_BAND_5GHZ)
  677. ctx->staging.flags &= ~RXON_FLG_BAND_24G_MSK;
  678. else
  679. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  680. priv->band = band;
  681. IWL_DEBUG_INFO(priv, "Staging channel set to %d [%d]\n", channel, band);
  682. return 0;
  683. }
  684. EXPORT_SYMBOL(iwl_set_rxon_channel);
  685. void iwl_set_flags_for_band(struct iwl_priv *priv,
  686. struct iwl_rxon_context *ctx,
  687. enum ieee80211_band band,
  688. struct ieee80211_vif *vif)
  689. {
  690. if (band == IEEE80211_BAND_5GHZ) {
  691. ctx->staging.flags &=
  692. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
  693. | RXON_FLG_CCK_MSK);
  694. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  695. } else {
  696. /* Copied from iwl_post_associate() */
  697. if (vif && vif->bss_conf.use_short_slot)
  698. ctx->staging.flags |= RXON_FLG_SHORT_SLOT_MSK;
  699. else
  700. ctx->staging.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  701. ctx->staging.flags |= RXON_FLG_BAND_24G_MSK;
  702. ctx->staging.flags |= RXON_FLG_AUTO_DETECT_MSK;
  703. ctx->staging.flags &= ~RXON_FLG_CCK_MSK;
  704. }
  705. }
  706. EXPORT_SYMBOL(iwl_set_flags_for_band);
  707. /*
  708. * initialize rxon structure with default values from eeprom
  709. */
  710. void iwl_connection_init_rx_config(struct iwl_priv *priv,
  711. struct iwl_rxon_context *ctx)
  712. {
  713. const struct iwl_channel_info *ch_info;
  714. memset(&ctx->staging, 0, sizeof(ctx->staging));
  715. if (!ctx->vif) {
  716. ctx->staging.dev_type = ctx->unused_devtype;
  717. } else switch (ctx->vif->type) {
  718. case NL80211_IFTYPE_AP:
  719. ctx->staging.dev_type = ctx->ap_devtype;
  720. break;
  721. case NL80211_IFTYPE_STATION:
  722. ctx->staging.dev_type = ctx->station_devtype;
  723. ctx->staging.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  724. break;
  725. case NL80211_IFTYPE_ADHOC:
  726. ctx->staging.dev_type = ctx->ibss_devtype;
  727. ctx->staging.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  728. ctx->staging.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
  729. RXON_FILTER_ACCEPT_GRP_MSK;
  730. break;
  731. default:
  732. IWL_ERR(priv, "Unsupported interface type %d\n",
  733. ctx->vif->type);
  734. break;
  735. }
  736. #if 0
  737. /* TODO: Figure out when short_preamble would be set and cache from
  738. * that */
  739. if (!hw_to_local(priv->hw)->short_preamble)
  740. ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  741. else
  742. ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  743. #endif
  744. ch_info = iwl_get_channel_info(priv, priv->band,
  745. le16_to_cpu(ctx->active.channel));
  746. if (!ch_info)
  747. ch_info = &priv->channel_info[0];
  748. ctx->staging.channel = cpu_to_le16(ch_info->channel);
  749. priv->band = ch_info->band;
  750. iwl_set_flags_for_band(priv, ctx, priv->band, ctx->vif);
  751. ctx->staging.ofdm_basic_rates =
  752. (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  753. ctx->staging.cck_basic_rates =
  754. (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  755. /* clear both MIX and PURE40 mode flag */
  756. ctx->staging.flags &= ~(RXON_FLG_CHANNEL_MODE_MIXED |
  757. RXON_FLG_CHANNEL_MODE_PURE_40);
  758. if (ctx->vif)
  759. memcpy(ctx->staging.node_addr, ctx->vif->addr, ETH_ALEN);
  760. ctx->staging.ofdm_ht_single_stream_basic_rates = 0xff;
  761. ctx->staging.ofdm_ht_dual_stream_basic_rates = 0xff;
  762. ctx->staging.ofdm_ht_triple_stream_basic_rates = 0xff;
  763. }
  764. EXPORT_SYMBOL(iwl_connection_init_rx_config);
  765. void iwl_set_rate(struct iwl_priv *priv)
  766. {
  767. const struct ieee80211_supported_band *hw = NULL;
  768. struct ieee80211_rate *rate;
  769. struct iwl_rxon_context *ctx;
  770. int i;
  771. hw = iwl_get_hw_mode(priv, priv->band);
  772. if (!hw) {
  773. IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
  774. return;
  775. }
  776. priv->active_rate = 0;
  777. for (i = 0; i < hw->n_bitrates; i++) {
  778. rate = &(hw->bitrates[i]);
  779. if (rate->hw_value < IWL_RATE_COUNT_LEGACY)
  780. priv->active_rate |= (1 << rate->hw_value);
  781. }
  782. IWL_DEBUG_RATE(priv, "Set active_rate = %0x\n", priv->active_rate);
  783. for_each_context(priv, ctx) {
  784. ctx->staging.cck_basic_rates =
  785. (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  786. ctx->staging.ofdm_basic_rates =
  787. (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  788. }
  789. }
  790. EXPORT_SYMBOL(iwl_set_rate);
  791. void iwl_chswitch_done(struct iwl_priv *priv, bool is_success)
  792. {
  793. /*
  794. * MULTI-FIXME
  795. * See iwl_mac_channel_switch.
  796. */
  797. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  798. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  799. return;
  800. if (priv->switch_rxon.switch_in_progress) {
  801. ieee80211_chswitch_done(ctx->vif, is_success);
  802. mutex_lock(&priv->mutex);
  803. priv->switch_rxon.switch_in_progress = false;
  804. mutex_unlock(&priv->mutex);
  805. }
  806. }
  807. EXPORT_SYMBOL(iwl_chswitch_done);
  808. void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
  809. {
  810. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  811. struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
  812. /*
  813. * MULTI-FIXME
  814. * See iwl_mac_channel_switch.
  815. */
  816. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  817. struct iwl_rxon_cmd *rxon = (void *)&ctx->active;
  818. if (priv->switch_rxon.switch_in_progress) {
  819. if (!le32_to_cpu(csa->status) &&
  820. (csa->channel == priv->switch_rxon.channel)) {
  821. rxon->channel = csa->channel;
  822. ctx->staging.channel = csa->channel;
  823. IWL_DEBUG_11H(priv, "CSA notif: channel %d\n",
  824. le16_to_cpu(csa->channel));
  825. iwl_chswitch_done(priv, true);
  826. } else {
  827. IWL_ERR(priv, "CSA notif (fail) : channel %d\n",
  828. le16_to_cpu(csa->channel));
  829. iwl_chswitch_done(priv, false);
  830. }
  831. }
  832. }
  833. EXPORT_SYMBOL(iwl_rx_csa);
  834. #ifdef CONFIG_IWLWIFI_DEBUG
  835. void iwl_print_rx_config_cmd(struct iwl_priv *priv,
  836. struct iwl_rxon_context *ctx)
  837. {
  838. struct iwl_rxon_cmd *rxon = &ctx->staging;
  839. IWL_DEBUG_RADIO(priv, "RX CONFIG:\n");
  840. iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  841. IWL_DEBUG_RADIO(priv, "u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  842. IWL_DEBUG_RADIO(priv, "u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  843. IWL_DEBUG_RADIO(priv, "u32 filter_flags: 0x%08x\n",
  844. le32_to_cpu(rxon->filter_flags));
  845. IWL_DEBUG_RADIO(priv, "u8 dev_type: 0x%x\n", rxon->dev_type);
  846. IWL_DEBUG_RADIO(priv, "u8 ofdm_basic_rates: 0x%02x\n",
  847. rxon->ofdm_basic_rates);
  848. IWL_DEBUG_RADIO(priv, "u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  849. IWL_DEBUG_RADIO(priv, "u8[6] node_addr: %pM\n", rxon->node_addr);
  850. IWL_DEBUG_RADIO(priv, "u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  851. IWL_DEBUG_RADIO(priv, "u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  852. }
  853. EXPORT_SYMBOL(iwl_print_rx_config_cmd);
  854. #endif
  855. /**
  856. * iwl_irq_handle_error - called for HW or SW error interrupt from card
  857. */
  858. void iwl_irq_handle_error(struct iwl_priv *priv)
  859. {
  860. /* Set the FW error flag -- cleared on iwl_down */
  861. set_bit(STATUS_FW_ERROR, &priv->status);
  862. /* Cancel currently queued command. */
  863. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  864. IWL_ERR(priv, "Loaded firmware version: %s\n",
  865. priv->hw->wiphy->fw_version);
  866. priv->cfg->ops->lib->dump_nic_error_log(priv);
  867. if (priv->cfg->ops->lib->dump_csr)
  868. priv->cfg->ops->lib->dump_csr(priv);
  869. if (priv->cfg->ops->lib->dump_fh)
  870. priv->cfg->ops->lib->dump_fh(priv, NULL, false);
  871. priv->cfg->ops->lib->dump_nic_event_log(priv, false, NULL, false);
  872. #ifdef CONFIG_IWLWIFI_DEBUG
  873. if (iwl_get_debug_level(priv) & IWL_DL_FW_ERRORS)
  874. iwl_print_rx_config_cmd(priv,
  875. &priv->contexts[IWL_RXON_CTX_BSS]);
  876. #endif
  877. wake_up_interruptible(&priv->wait_command_queue);
  878. /* Keep the restart process from trying to send host
  879. * commands by clearing the INIT status bit */
  880. clear_bit(STATUS_READY, &priv->status);
  881. if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  882. IWL_DEBUG(priv, IWL_DL_FW_ERRORS,
  883. "Restarting adapter due to uCode error.\n");
  884. if (priv->cfg->mod_params->restart_fw)
  885. queue_work(priv->workqueue, &priv->restart);
  886. }
  887. }
  888. EXPORT_SYMBOL(iwl_irq_handle_error);
  889. static int iwl_apm_stop_master(struct iwl_priv *priv)
  890. {
  891. int ret = 0;
  892. /* stop device's busmaster DMA activity */
  893. iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_STOP_MASTER);
  894. ret = iwl_poll_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_MASTER_DISABLED,
  895. CSR_RESET_REG_FLAG_MASTER_DISABLED, 100);
  896. if (ret)
  897. IWL_WARN(priv, "Master Disable Timed Out, 100 usec\n");
  898. IWL_DEBUG_INFO(priv, "stop master\n");
  899. return ret;
  900. }
  901. void iwl_apm_stop(struct iwl_priv *priv)
  902. {
  903. IWL_DEBUG_INFO(priv, "Stop card, put in low power state\n");
  904. /* Stop device's DMA activity */
  905. iwl_apm_stop_master(priv);
  906. /* Reset the entire device */
  907. iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  908. udelay(10);
  909. /*
  910. * Clear "initialization complete" bit to move adapter from
  911. * D0A* (powered-up Active) --> D0U* (Uninitialized) state.
  912. */
  913. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  914. }
  915. EXPORT_SYMBOL(iwl_apm_stop);
  916. /*
  917. * Start up NIC's basic functionality after it has been reset
  918. * (e.g. after platform boot, or shutdown via iwl_apm_stop())
  919. * NOTE: This does not load uCode nor start the embedded processor
  920. */
  921. int iwl_apm_init(struct iwl_priv *priv)
  922. {
  923. int ret = 0;
  924. u16 lctl;
  925. IWL_DEBUG_INFO(priv, "Init card's basic functions\n");
  926. /*
  927. * Use "set_bit" below rather than "write", to preserve any hardware
  928. * bits already set by default after reset.
  929. */
  930. /* Disable L0S exit timer (platform NMI Work/Around) */
  931. iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
  932. CSR_GIO_CHICKEN_BITS_REG_BIT_DIS_L0S_EXIT_TIMER);
  933. /*
  934. * Disable L0s without affecting L1;
  935. * don't wait for ICH L0s (ICH bug W/A)
  936. */
  937. iwl_set_bit(priv, CSR_GIO_CHICKEN_BITS,
  938. CSR_GIO_CHICKEN_BITS_REG_BIT_L1A_NO_L0S_RX);
  939. /* Set FH wait threshold to maximum (HW error during stress W/A) */
  940. iwl_set_bit(priv, CSR_DBG_HPET_MEM_REG, CSR_DBG_HPET_MEM_REG_VAL);
  941. /*
  942. * Enable HAP INTA (interrupt from management bus) to
  943. * wake device's PCI Express link L1a -> L0s
  944. * NOTE: This is no-op for 3945 (non-existant bit)
  945. */
  946. iwl_set_bit(priv, CSR_HW_IF_CONFIG_REG,
  947. CSR_HW_IF_CONFIG_REG_BIT_HAP_WAKE_L1A);
  948. /*
  949. * HW bug W/A for instability in PCIe bus L0->L0S->L1 transition.
  950. * Check if BIOS (or OS) enabled L1-ASPM on this device.
  951. * If so (likely), disable L0S, so device moves directly L0->L1;
  952. * costs negligible amount of power savings.
  953. * If not (unlikely), enable L0S, so there is at least some
  954. * power savings, even without L1.
  955. */
  956. if (priv->cfg->base_params->set_l0s) {
  957. lctl = iwl_pcie_link_ctl(priv);
  958. if ((lctl & PCI_CFG_LINK_CTRL_VAL_L1_EN) ==
  959. PCI_CFG_LINK_CTRL_VAL_L1_EN) {
  960. /* L1-ASPM enabled; disable(!) L0S */
  961. iwl_set_bit(priv, CSR_GIO_REG,
  962. CSR_GIO_REG_VAL_L0S_ENABLED);
  963. IWL_DEBUG_POWER(priv, "L1 Enabled; Disabling L0S\n");
  964. } else {
  965. /* L1-ASPM disabled; enable(!) L0S */
  966. iwl_clear_bit(priv, CSR_GIO_REG,
  967. CSR_GIO_REG_VAL_L0S_ENABLED);
  968. IWL_DEBUG_POWER(priv, "L1 Disabled; Enabling L0S\n");
  969. }
  970. }
  971. /* Configure analog phase-lock-loop before activating to D0A */
  972. if (priv->cfg->base_params->pll_cfg_val)
  973. iwl_set_bit(priv, CSR_ANA_PLL_CFG,
  974. priv->cfg->base_params->pll_cfg_val);
  975. /*
  976. * Set "initialization complete" bit to move adapter from
  977. * D0U* --> D0A* (powered-up active) state.
  978. */
  979. iwl_set_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_INIT_DONE);
  980. /*
  981. * Wait for clock stabilization; once stabilized, access to
  982. * device-internal resources is supported, e.g. iwl_write_prph()
  983. * and accesses to uCode SRAM.
  984. */
  985. ret = iwl_poll_bit(priv, CSR_GP_CNTRL,
  986. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY,
  987. CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY, 25000);
  988. if (ret < 0) {
  989. IWL_DEBUG_INFO(priv, "Failed to init the card\n");
  990. goto out;
  991. }
  992. /*
  993. * Enable DMA and BSM (if used) clocks, wait for them to stabilize.
  994. * BSM (Boostrap State Machine) is only in 3945 and 4965;
  995. * later devices (i.e. 5000 and later) have non-volatile SRAM,
  996. * and don't need BSM to restore data after power-saving sleep.
  997. *
  998. * Write to "CLK_EN_REG"; "1" bits enable clocks, while "0" bits
  999. * do not disable clocks. This preserves any hardware bits already
  1000. * set by default in "CLK_CTRL_REG" after reset.
  1001. */
  1002. if (priv->cfg->base_params->use_bsm)
  1003. iwl_write_prph(priv, APMG_CLK_EN_REG,
  1004. APMG_CLK_VAL_DMA_CLK_RQT | APMG_CLK_VAL_BSM_CLK_RQT);
  1005. else
  1006. iwl_write_prph(priv, APMG_CLK_EN_REG,
  1007. APMG_CLK_VAL_DMA_CLK_RQT);
  1008. udelay(20);
  1009. /* Disable L1-Active */
  1010. iwl_set_bits_prph(priv, APMG_PCIDEV_STT_REG,
  1011. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  1012. out:
  1013. return ret;
  1014. }
  1015. EXPORT_SYMBOL(iwl_apm_init);
  1016. int iwl_set_tx_power(struct iwl_priv *priv, s8 tx_power, bool force)
  1017. {
  1018. int ret = 0;
  1019. s8 prev_tx_power = priv->tx_power_user_lmt;
  1020. if (tx_power < IWLAGN_TX_POWER_TARGET_POWER_MIN) {
  1021. IWL_WARN(priv,
  1022. "Requested user TXPOWER %d below lower limit %d.\n",
  1023. tx_power,
  1024. IWLAGN_TX_POWER_TARGET_POWER_MIN);
  1025. return -EINVAL;
  1026. }
  1027. if (tx_power > priv->tx_power_device_lmt) {
  1028. IWL_WARN(priv,
  1029. "Requested user TXPOWER %d above upper limit %d.\n",
  1030. tx_power, priv->tx_power_device_lmt);
  1031. return -EINVAL;
  1032. }
  1033. if (priv->tx_power_user_lmt != tx_power)
  1034. force = true;
  1035. /* if nic is not up don't send command */
  1036. if (iwl_is_ready_rf(priv)) {
  1037. priv->tx_power_user_lmt = tx_power;
  1038. if (force && priv->cfg->ops->lib->send_tx_power)
  1039. ret = priv->cfg->ops->lib->send_tx_power(priv);
  1040. else if (!priv->cfg->ops->lib->send_tx_power)
  1041. ret = -EOPNOTSUPP;
  1042. /*
  1043. * if fail to set tx_power, restore the orig. tx power
  1044. */
  1045. if (ret)
  1046. priv->tx_power_user_lmt = prev_tx_power;
  1047. }
  1048. /*
  1049. * Even this is an async host command, the command
  1050. * will always report success from uCode
  1051. * So once driver can placing the command into the queue
  1052. * successfully, driver can use priv->tx_power_user_lmt
  1053. * to reflect the current tx power
  1054. */
  1055. return ret;
  1056. }
  1057. EXPORT_SYMBOL(iwl_set_tx_power);
  1058. irqreturn_t iwl_isr_legacy(int irq, void *data)
  1059. {
  1060. struct iwl_priv *priv = data;
  1061. u32 inta, inta_mask;
  1062. u32 inta_fh;
  1063. unsigned long flags;
  1064. if (!priv)
  1065. return IRQ_NONE;
  1066. spin_lock_irqsave(&priv->lock, flags);
  1067. /* Disable (but don't clear!) interrupts here to avoid
  1068. * back-to-back ISRs and sporadic interrupts from our NIC.
  1069. * If we have something to service, the tasklet will re-enable ints.
  1070. * If we *don't* have something, we'll re-enable before leaving here. */
  1071. inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
  1072. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  1073. /* Discover which interrupts are active/pending */
  1074. inta = iwl_read32(priv, CSR_INT);
  1075. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  1076. /* Ignore interrupt if there's nothing in NIC to service.
  1077. * This may be due to IRQ shared with another device,
  1078. * or due to sporadic interrupts thrown from our NIC. */
  1079. if (!inta && !inta_fh) {
  1080. IWL_DEBUG_ISR(priv, "Ignore interrupt, inta == 0, inta_fh == 0\n");
  1081. goto none;
  1082. }
  1083. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  1084. /* Hardware disappeared. It might have already raised
  1085. * an interrupt */
  1086. IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  1087. goto unplugged;
  1088. }
  1089. IWL_DEBUG_ISR(priv, "ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  1090. inta, inta_mask, inta_fh);
  1091. inta &= ~CSR_INT_BIT_SCD;
  1092. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  1093. if (likely(inta || inta_fh))
  1094. tasklet_schedule(&priv->irq_tasklet);
  1095. unplugged:
  1096. spin_unlock_irqrestore(&priv->lock, flags);
  1097. return IRQ_HANDLED;
  1098. none:
  1099. /* re-enable interrupts here since we don't have anything to service. */
  1100. /* only Re-enable if diabled by irq */
  1101. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  1102. iwl_enable_interrupts(priv);
  1103. spin_unlock_irqrestore(&priv->lock, flags);
  1104. return IRQ_NONE;
  1105. }
  1106. EXPORT_SYMBOL(iwl_isr_legacy);
  1107. void iwl_send_bt_config(struct iwl_priv *priv)
  1108. {
  1109. struct iwl_bt_cmd bt_cmd = {
  1110. .lead_time = BT_LEAD_TIME_DEF,
  1111. .max_kill = BT_MAX_KILL_DEF,
  1112. .kill_ack_mask = 0,
  1113. .kill_cts_mask = 0,
  1114. };
  1115. if (!bt_coex_active)
  1116. bt_cmd.flags = BT_COEX_DISABLE;
  1117. else
  1118. bt_cmd.flags = BT_COEX_ENABLE;
  1119. IWL_DEBUG_INFO(priv, "BT coex %s\n",
  1120. (bt_cmd.flags == BT_COEX_DISABLE) ? "disable" : "active");
  1121. if (iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  1122. sizeof(struct iwl_bt_cmd), &bt_cmd))
  1123. IWL_ERR(priv, "failed to send BT Coex Config\n");
  1124. }
  1125. EXPORT_SYMBOL(iwl_send_bt_config);
  1126. int iwl_send_statistics_request(struct iwl_priv *priv, u8 flags, bool clear)
  1127. {
  1128. struct iwl_statistics_cmd statistics_cmd = {
  1129. .configuration_flags =
  1130. clear ? IWL_STATS_CONF_CLEAR_STATS : 0,
  1131. };
  1132. if (flags & CMD_ASYNC)
  1133. return iwl_send_cmd_pdu_async(priv, REPLY_STATISTICS_CMD,
  1134. sizeof(struct iwl_statistics_cmd),
  1135. &statistics_cmd, NULL);
  1136. else
  1137. return iwl_send_cmd_pdu(priv, REPLY_STATISTICS_CMD,
  1138. sizeof(struct iwl_statistics_cmd),
  1139. &statistics_cmd);
  1140. }
  1141. EXPORT_SYMBOL(iwl_send_statistics_request);
  1142. void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
  1143. struct iwl_rx_mem_buffer *rxb)
  1144. {
  1145. #ifdef CONFIG_IWLWIFI_DEBUG
  1146. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1147. struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
  1148. IWL_DEBUG_RX(priv, "sleep mode: %d, src: %d\n",
  1149. sleep->pm_sleep_mode, sleep->pm_wakeup_src);
  1150. #endif
  1151. }
  1152. EXPORT_SYMBOL(iwl_rx_pm_sleep_notif);
  1153. void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
  1154. struct iwl_rx_mem_buffer *rxb)
  1155. {
  1156. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1157. u32 len = le32_to_cpu(pkt->len_n_flags) & FH_RSCSR_FRAME_SIZE_MSK;
  1158. IWL_DEBUG_RADIO(priv, "Dumping %d bytes of unhandled "
  1159. "notification for %s:\n", len,
  1160. get_cmd_string(pkt->hdr.cmd));
  1161. iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw, len);
  1162. }
  1163. EXPORT_SYMBOL(iwl_rx_pm_debug_statistics_notif);
  1164. void iwl_rx_reply_error(struct iwl_priv *priv,
  1165. struct iwl_rx_mem_buffer *rxb)
  1166. {
  1167. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1168. IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
  1169. "seq 0x%04X ser 0x%08X\n",
  1170. le32_to_cpu(pkt->u.err_resp.error_type),
  1171. get_cmd_string(pkt->u.err_resp.cmd_id),
  1172. pkt->u.err_resp.cmd_id,
  1173. le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
  1174. le32_to_cpu(pkt->u.err_resp.error_info));
  1175. }
  1176. EXPORT_SYMBOL(iwl_rx_reply_error);
  1177. void iwl_clear_isr_stats(struct iwl_priv *priv)
  1178. {
  1179. memset(&priv->isr_stats, 0, sizeof(priv->isr_stats));
  1180. }
  1181. int iwl_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
  1182. const struct ieee80211_tx_queue_params *params)
  1183. {
  1184. struct iwl_priv *priv = hw->priv;
  1185. struct iwl_rxon_context *ctx;
  1186. unsigned long flags;
  1187. int q;
  1188. IWL_DEBUG_MAC80211(priv, "enter\n");
  1189. if (!iwl_is_ready_rf(priv)) {
  1190. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  1191. return -EIO;
  1192. }
  1193. if (queue >= AC_NUM) {
  1194. IWL_DEBUG_MAC80211(priv, "leave - queue >= AC_NUM %d\n", queue);
  1195. return 0;
  1196. }
  1197. q = AC_NUM - 1 - queue;
  1198. spin_lock_irqsave(&priv->lock, flags);
  1199. /*
  1200. * MULTI-FIXME
  1201. * This may need to be done per interface in nl80211/cfg80211/mac80211.
  1202. */
  1203. for_each_context(priv, ctx) {
  1204. ctx->qos_data.def_qos_parm.ac[q].cw_min =
  1205. cpu_to_le16(params->cw_min);
  1206. ctx->qos_data.def_qos_parm.ac[q].cw_max =
  1207. cpu_to_le16(params->cw_max);
  1208. ctx->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  1209. ctx->qos_data.def_qos_parm.ac[q].edca_txop =
  1210. cpu_to_le16((params->txop * 32));
  1211. ctx->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  1212. }
  1213. spin_unlock_irqrestore(&priv->lock, flags);
  1214. IWL_DEBUG_MAC80211(priv, "leave\n");
  1215. return 0;
  1216. }
  1217. EXPORT_SYMBOL(iwl_mac_conf_tx);
  1218. int iwl_mac_tx_last_beacon(struct ieee80211_hw *hw)
  1219. {
  1220. struct iwl_priv *priv = hw->priv;
  1221. return priv->ibss_manager == IWL_IBSS_MANAGER;
  1222. }
  1223. EXPORT_SYMBOL_GPL(iwl_mac_tx_last_beacon);
  1224. static void iwl_ht_conf(struct iwl_priv *priv,
  1225. struct ieee80211_vif *vif)
  1226. {
  1227. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  1228. struct ieee80211_sta *sta;
  1229. struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
  1230. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1231. IWL_DEBUG_MAC80211(priv, "enter:\n");
  1232. if (!ctx->ht.enabled)
  1233. return;
  1234. ctx->ht.protection =
  1235. bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_PROTECTION;
  1236. ctx->ht.non_gf_sta_present =
  1237. !!(bss_conf->ht_operation_mode & IEEE80211_HT_OP_MODE_NON_GF_STA_PRSNT);
  1238. ht_conf->single_chain_sufficient = false;
  1239. switch (vif->type) {
  1240. case NL80211_IFTYPE_STATION:
  1241. rcu_read_lock();
  1242. sta = ieee80211_find_sta(vif, bss_conf->bssid);
  1243. if (sta) {
  1244. struct ieee80211_sta_ht_cap *ht_cap = &sta->ht_cap;
  1245. int maxstreams;
  1246. maxstreams = (ht_cap->mcs.tx_params &
  1247. IEEE80211_HT_MCS_TX_MAX_STREAMS_MASK)
  1248. >> IEEE80211_HT_MCS_TX_MAX_STREAMS_SHIFT;
  1249. maxstreams += 1;
  1250. if ((ht_cap->mcs.rx_mask[1] == 0) &&
  1251. (ht_cap->mcs.rx_mask[2] == 0))
  1252. ht_conf->single_chain_sufficient = true;
  1253. if (maxstreams <= 1)
  1254. ht_conf->single_chain_sufficient = true;
  1255. } else {
  1256. /*
  1257. * If at all, this can only happen through a race
  1258. * when the AP disconnects us while we're still
  1259. * setting up the connection, in that case mac80211
  1260. * will soon tell us about that.
  1261. */
  1262. ht_conf->single_chain_sufficient = true;
  1263. }
  1264. rcu_read_unlock();
  1265. break;
  1266. case NL80211_IFTYPE_ADHOC:
  1267. ht_conf->single_chain_sufficient = true;
  1268. break;
  1269. default:
  1270. break;
  1271. }
  1272. IWL_DEBUG_MAC80211(priv, "leave\n");
  1273. }
  1274. static inline void iwl_set_no_assoc(struct iwl_priv *priv,
  1275. struct ieee80211_vif *vif)
  1276. {
  1277. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1278. iwl_led_disassociate(priv);
  1279. /*
  1280. * inform the ucode that there is no longer an
  1281. * association and that no more packets should be
  1282. * sent
  1283. */
  1284. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1285. ctx->staging.assoc_id = 0;
  1286. iwlcore_commit_rxon(priv, ctx);
  1287. }
  1288. static void iwlcore_beacon_update(struct ieee80211_hw *hw,
  1289. struct ieee80211_vif *vif)
  1290. {
  1291. struct iwl_priv *priv = hw->priv;
  1292. unsigned long flags;
  1293. __le64 timestamp;
  1294. struct sk_buff *skb = ieee80211_beacon_get(hw, vif);
  1295. if (!skb)
  1296. return;
  1297. IWL_DEBUG_ASSOC(priv, "enter\n");
  1298. lockdep_assert_held(&priv->mutex);
  1299. if (!priv->beacon_ctx) {
  1300. IWL_ERR(priv, "update beacon but no beacon context!\n");
  1301. dev_kfree_skb(skb);
  1302. return;
  1303. }
  1304. spin_lock_irqsave(&priv->lock, flags);
  1305. if (priv->beacon_skb)
  1306. dev_kfree_skb(priv->beacon_skb);
  1307. priv->beacon_skb = skb;
  1308. timestamp = ((struct ieee80211_mgmt *)skb->data)->u.beacon.timestamp;
  1309. priv->timestamp = le64_to_cpu(timestamp);
  1310. IWL_DEBUG_ASSOC(priv, "leave\n");
  1311. spin_unlock_irqrestore(&priv->lock, flags);
  1312. if (!iwl_is_ready_rf(priv)) {
  1313. IWL_DEBUG_MAC80211(priv, "leave - RF not ready\n");
  1314. return;
  1315. }
  1316. priv->cfg->ops->lib->post_associate(priv, priv->beacon_ctx->vif);
  1317. }
  1318. void iwl_bss_info_changed(struct ieee80211_hw *hw,
  1319. struct ieee80211_vif *vif,
  1320. struct ieee80211_bss_conf *bss_conf,
  1321. u32 changes)
  1322. {
  1323. struct iwl_priv *priv = hw->priv;
  1324. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1325. int ret;
  1326. IWL_DEBUG_MAC80211(priv, "changes = 0x%X\n", changes);
  1327. if (!iwl_is_alive(priv))
  1328. return;
  1329. mutex_lock(&priv->mutex);
  1330. if (changes & BSS_CHANGED_QOS) {
  1331. unsigned long flags;
  1332. spin_lock_irqsave(&priv->lock, flags);
  1333. ctx->qos_data.qos_active = bss_conf->qos;
  1334. iwl_update_qos(priv, ctx);
  1335. spin_unlock_irqrestore(&priv->lock, flags);
  1336. }
  1337. if (changes & BSS_CHANGED_BEACON_ENABLED) {
  1338. /*
  1339. * the add_interface code must make sure we only ever
  1340. * have a single interface that could be beaconing at
  1341. * any time.
  1342. */
  1343. if (vif->bss_conf.enable_beacon)
  1344. priv->beacon_ctx = ctx;
  1345. else
  1346. priv->beacon_ctx = NULL;
  1347. }
  1348. if (changes & BSS_CHANGED_BEACON && vif->type == NL80211_IFTYPE_AP) {
  1349. dev_kfree_skb(priv->beacon_skb);
  1350. priv->beacon_skb = ieee80211_beacon_get(hw, vif);
  1351. }
  1352. if (changes & BSS_CHANGED_BEACON_INT && vif->type == NL80211_IFTYPE_AP)
  1353. iwl_send_rxon_timing(priv, ctx);
  1354. if (changes & BSS_CHANGED_BSSID) {
  1355. IWL_DEBUG_MAC80211(priv, "BSSID %pM\n", bss_conf->bssid);
  1356. /*
  1357. * If there is currently a HW scan going on in the
  1358. * background then we need to cancel it else the RXON
  1359. * below/in post_associate will fail.
  1360. */
  1361. if (iwl_scan_cancel_timeout(priv, 100)) {
  1362. IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
  1363. IWL_DEBUG_MAC80211(priv, "leaving - scan abort failed.\n");
  1364. mutex_unlock(&priv->mutex);
  1365. return;
  1366. }
  1367. /* mac80211 only sets assoc when in STATION mode */
  1368. if (vif->type == NL80211_IFTYPE_ADHOC || bss_conf->assoc) {
  1369. memcpy(ctx->staging.bssid_addr,
  1370. bss_conf->bssid, ETH_ALEN);
  1371. /* currently needed in a few places */
  1372. memcpy(priv->bssid, bss_conf->bssid, ETH_ALEN);
  1373. } else {
  1374. ctx->staging.filter_flags &=
  1375. ~RXON_FILTER_ASSOC_MSK;
  1376. }
  1377. }
  1378. /*
  1379. * This needs to be after setting the BSSID in case
  1380. * mac80211 decides to do both changes at once because
  1381. * it will invoke post_associate.
  1382. */
  1383. if (vif->type == NL80211_IFTYPE_ADHOC && changes & BSS_CHANGED_BEACON)
  1384. iwlcore_beacon_update(hw, vif);
  1385. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  1386. IWL_DEBUG_MAC80211(priv, "ERP_PREAMBLE %d\n",
  1387. bss_conf->use_short_preamble);
  1388. if (bss_conf->use_short_preamble)
  1389. ctx->staging.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  1390. else
  1391. ctx->staging.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  1392. }
  1393. if (changes & BSS_CHANGED_ERP_CTS_PROT) {
  1394. IWL_DEBUG_MAC80211(priv, "ERP_CTS %d\n", bss_conf->use_cts_prot);
  1395. if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
  1396. ctx->staging.flags |= RXON_FLG_TGG_PROTECT_MSK;
  1397. else
  1398. ctx->staging.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
  1399. if (bss_conf->use_cts_prot)
  1400. ctx->staging.flags |= RXON_FLG_SELF_CTS_EN;
  1401. else
  1402. ctx->staging.flags &= ~RXON_FLG_SELF_CTS_EN;
  1403. }
  1404. if (changes & BSS_CHANGED_BASIC_RATES) {
  1405. /* XXX use this information
  1406. *
  1407. * To do that, remove code from iwl_set_rate() and put something
  1408. * like this here:
  1409. *
  1410. if (A-band)
  1411. ctx->staging.ofdm_basic_rates =
  1412. bss_conf->basic_rates;
  1413. else
  1414. ctx->staging.ofdm_basic_rates =
  1415. bss_conf->basic_rates >> 4;
  1416. ctx->staging.cck_basic_rates =
  1417. bss_conf->basic_rates & 0xF;
  1418. */
  1419. }
  1420. if (changes & BSS_CHANGED_HT) {
  1421. iwl_ht_conf(priv, vif);
  1422. if (priv->cfg->ops->hcmd->set_rxon_chain)
  1423. priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
  1424. }
  1425. if (changes & BSS_CHANGED_ASSOC) {
  1426. IWL_DEBUG_MAC80211(priv, "ASSOC %d\n", bss_conf->assoc);
  1427. if (bss_conf->assoc) {
  1428. priv->timestamp = bss_conf->timestamp;
  1429. iwl_led_associate(priv);
  1430. if (!iwl_is_rfkill(priv))
  1431. priv->cfg->ops->lib->post_associate(priv, vif);
  1432. } else
  1433. iwl_set_no_assoc(priv, vif);
  1434. }
  1435. if (changes && iwl_is_associated_ctx(ctx) && bss_conf->aid) {
  1436. IWL_DEBUG_MAC80211(priv, "Changes (%#x) while associated\n",
  1437. changes);
  1438. ret = iwl_send_rxon_assoc(priv, ctx);
  1439. if (!ret) {
  1440. /* Sync active_rxon with latest change. */
  1441. memcpy((void *)&ctx->active,
  1442. &ctx->staging,
  1443. sizeof(struct iwl_rxon_cmd));
  1444. }
  1445. }
  1446. if (changes & BSS_CHANGED_BEACON_ENABLED) {
  1447. if (vif->bss_conf.enable_beacon) {
  1448. memcpy(ctx->staging.bssid_addr,
  1449. bss_conf->bssid, ETH_ALEN);
  1450. memcpy(priv->bssid, bss_conf->bssid, ETH_ALEN);
  1451. iwl_led_associate(priv);
  1452. iwlcore_config_ap(priv, vif);
  1453. } else
  1454. iwl_set_no_assoc(priv, vif);
  1455. }
  1456. if (changes & BSS_CHANGED_IBSS) {
  1457. ret = priv->cfg->ops->lib->manage_ibss_station(priv, vif,
  1458. bss_conf->ibss_joined);
  1459. if (ret)
  1460. IWL_ERR(priv, "failed to %s IBSS station %pM\n",
  1461. bss_conf->ibss_joined ? "add" : "remove",
  1462. bss_conf->bssid);
  1463. }
  1464. if (changes & BSS_CHANGED_IDLE &&
  1465. priv->cfg->ops->hcmd->set_pan_params) {
  1466. if (priv->cfg->ops->hcmd->set_pan_params(priv))
  1467. IWL_ERR(priv, "failed to update PAN params\n");
  1468. }
  1469. mutex_unlock(&priv->mutex);
  1470. IWL_DEBUG_MAC80211(priv, "leave\n");
  1471. }
  1472. EXPORT_SYMBOL(iwl_bss_info_changed);
  1473. static int iwl_set_mode(struct iwl_priv *priv, struct ieee80211_vif *vif)
  1474. {
  1475. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1476. iwl_connection_init_rx_config(priv, ctx);
  1477. if (priv->cfg->ops->hcmd->set_rxon_chain)
  1478. priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
  1479. return iwlcore_commit_rxon(priv, ctx);
  1480. }
  1481. int iwl_mac_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  1482. {
  1483. struct iwl_priv *priv = hw->priv;
  1484. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  1485. struct iwl_rxon_context *tmp, *ctx = NULL;
  1486. int err = 0;
  1487. IWL_DEBUG_MAC80211(priv, "enter: type %d, addr %pM\n",
  1488. vif->type, vif->addr);
  1489. mutex_lock(&priv->mutex);
  1490. if (!iwl_is_ready_rf(priv)) {
  1491. IWL_WARN(priv, "Try to add interface when device not ready\n");
  1492. err = -EINVAL;
  1493. goto out;
  1494. }
  1495. for_each_context(priv, tmp) {
  1496. u32 possible_modes =
  1497. tmp->interface_modes | tmp->exclusive_interface_modes;
  1498. if (tmp->vif) {
  1499. /* check if this busy context is exclusive */
  1500. if (tmp->exclusive_interface_modes &
  1501. BIT(tmp->vif->type)) {
  1502. err = -EINVAL;
  1503. goto out;
  1504. }
  1505. continue;
  1506. }
  1507. if (!(possible_modes & BIT(vif->type)))
  1508. continue;
  1509. /* have maybe usable context w/o interface */
  1510. ctx = tmp;
  1511. break;
  1512. }
  1513. if (!ctx) {
  1514. err = -EOPNOTSUPP;
  1515. goto out;
  1516. }
  1517. vif_priv->ctx = ctx;
  1518. ctx->vif = vif;
  1519. /*
  1520. * This variable will be correct only when there's just
  1521. * a single context, but all code using it is for hardware
  1522. * that supports only one context.
  1523. */
  1524. priv->iw_mode = vif->type;
  1525. ctx->is_active = true;
  1526. err = iwl_set_mode(priv, vif);
  1527. if (err) {
  1528. if (!ctx->always_active)
  1529. ctx->is_active = false;
  1530. goto out_err;
  1531. }
  1532. if (priv->cfg->bt_params &&
  1533. priv->cfg->bt_params->advanced_bt_coexist &&
  1534. vif->type == NL80211_IFTYPE_ADHOC) {
  1535. /*
  1536. * pretend to have high BT traffic as long as we
  1537. * are operating in IBSS mode, as this will cause
  1538. * the rate scaling etc. to behave as intended.
  1539. */
  1540. priv->bt_traffic_load = IWL_BT_COEX_TRAFFIC_LOAD_HIGH;
  1541. }
  1542. goto out;
  1543. out_err:
  1544. ctx->vif = NULL;
  1545. priv->iw_mode = NL80211_IFTYPE_STATION;
  1546. out:
  1547. mutex_unlock(&priv->mutex);
  1548. IWL_DEBUG_MAC80211(priv, "leave\n");
  1549. return err;
  1550. }
  1551. EXPORT_SYMBOL(iwl_mac_add_interface);
  1552. void iwl_mac_remove_interface(struct ieee80211_hw *hw,
  1553. struct ieee80211_vif *vif)
  1554. {
  1555. struct iwl_priv *priv = hw->priv;
  1556. struct iwl_rxon_context *ctx = iwl_rxon_ctx_from_vif(vif);
  1557. IWL_DEBUG_MAC80211(priv, "enter\n");
  1558. mutex_lock(&priv->mutex);
  1559. WARN_ON(ctx->vif != vif);
  1560. ctx->vif = NULL;
  1561. if (priv->scan_vif == vif) {
  1562. iwl_scan_cancel_timeout(priv, 200);
  1563. iwl_force_scan_end(priv);
  1564. }
  1565. iwl_set_mode(priv, vif);
  1566. if (!ctx->always_active)
  1567. ctx->is_active = false;
  1568. /*
  1569. * When removing the IBSS interface, overwrite the
  1570. * BT traffic load with the stored one from the last
  1571. * notification, if any. If this is a device that
  1572. * doesn't implement this, this has no effect since
  1573. * both values are the same and zero.
  1574. */
  1575. if (vif->type == NL80211_IFTYPE_ADHOC)
  1576. priv->bt_traffic_load = priv->notif_bt_traffic_load;
  1577. memset(priv->bssid, 0, ETH_ALEN);
  1578. mutex_unlock(&priv->mutex);
  1579. IWL_DEBUG_MAC80211(priv, "leave\n");
  1580. }
  1581. EXPORT_SYMBOL(iwl_mac_remove_interface);
  1582. /**
  1583. * iwl_mac_config - mac80211 config callback
  1584. */
  1585. int iwl_mac_config(struct ieee80211_hw *hw, u32 changed)
  1586. {
  1587. struct iwl_priv *priv = hw->priv;
  1588. const struct iwl_channel_info *ch_info;
  1589. struct ieee80211_conf *conf = &hw->conf;
  1590. struct ieee80211_channel *channel = conf->channel;
  1591. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  1592. struct iwl_rxon_context *ctx;
  1593. unsigned long flags = 0;
  1594. int ret = 0;
  1595. u16 ch;
  1596. int scan_active = 0;
  1597. mutex_lock(&priv->mutex);
  1598. IWL_DEBUG_MAC80211(priv, "enter to channel %d changed 0x%X\n",
  1599. channel->hw_value, changed);
  1600. if (unlikely(!priv->cfg->mod_params->disable_hw_scan &&
  1601. test_bit(STATUS_SCANNING, &priv->status))) {
  1602. scan_active = 1;
  1603. IWL_DEBUG_MAC80211(priv, "leave - scanning\n");
  1604. }
  1605. if (changed & (IEEE80211_CONF_CHANGE_SMPS |
  1606. IEEE80211_CONF_CHANGE_CHANNEL)) {
  1607. /* mac80211 uses static for non-HT which is what we want */
  1608. priv->current_ht_config.smps = conf->smps_mode;
  1609. /*
  1610. * Recalculate chain counts.
  1611. *
  1612. * If monitor mode is enabled then mac80211 will
  1613. * set up the SM PS mode to OFF if an HT channel is
  1614. * configured.
  1615. */
  1616. if (priv->cfg->ops->hcmd->set_rxon_chain)
  1617. for_each_context(priv, ctx)
  1618. priv->cfg->ops->hcmd->set_rxon_chain(priv, ctx);
  1619. }
  1620. /* during scanning mac80211 will delay channel setting until
  1621. * scan finish with changed = 0
  1622. */
  1623. if (!changed || (changed & IEEE80211_CONF_CHANGE_CHANNEL)) {
  1624. if (scan_active)
  1625. goto set_ch_out;
  1626. ch = channel->hw_value;
  1627. ch_info = iwl_get_channel_info(priv, channel->band, ch);
  1628. if (!is_channel_valid(ch_info)) {
  1629. IWL_DEBUG_MAC80211(priv, "leave - invalid channel\n");
  1630. ret = -EINVAL;
  1631. goto set_ch_out;
  1632. }
  1633. spin_lock_irqsave(&priv->lock, flags);
  1634. for_each_context(priv, ctx) {
  1635. /* Configure HT40 channels */
  1636. ctx->ht.enabled = conf_is_ht(conf);
  1637. if (ctx->ht.enabled) {
  1638. if (conf_is_ht40_minus(conf)) {
  1639. ctx->ht.extension_chan_offset =
  1640. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  1641. ctx->ht.is_40mhz = true;
  1642. } else if (conf_is_ht40_plus(conf)) {
  1643. ctx->ht.extension_chan_offset =
  1644. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  1645. ctx->ht.is_40mhz = true;
  1646. } else {
  1647. ctx->ht.extension_chan_offset =
  1648. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  1649. ctx->ht.is_40mhz = false;
  1650. }
  1651. } else
  1652. ctx->ht.is_40mhz = false;
  1653. /*
  1654. * Default to no protection. Protection mode will
  1655. * later be set from BSS config in iwl_ht_conf
  1656. */
  1657. ctx->ht.protection = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
  1658. /* if we are switching from ht to 2.4 clear flags
  1659. * from any ht related info since 2.4 does not
  1660. * support ht */
  1661. if ((le16_to_cpu(ctx->staging.channel) != ch))
  1662. ctx->staging.flags = 0;
  1663. iwl_set_rxon_channel(priv, channel, ctx);
  1664. iwl_set_rxon_ht(priv, ht_conf);
  1665. iwl_set_flags_for_band(priv, ctx, channel->band,
  1666. ctx->vif);
  1667. }
  1668. spin_unlock_irqrestore(&priv->lock, flags);
  1669. if (priv->cfg->ops->lib->update_bcast_stations)
  1670. ret = priv->cfg->ops->lib->update_bcast_stations(priv);
  1671. set_ch_out:
  1672. /* The list of supported rates and rate mask can be different
  1673. * for each band; since the band may have changed, reset
  1674. * the rate mask to what mac80211 lists */
  1675. iwl_set_rate(priv);
  1676. }
  1677. if (changed & (IEEE80211_CONF_CHANGE_PS |
  1678. IEEE80211_CONF_CHANGE_IDLE)) {
  1679. ret = iwl_power_update_mode(priv, false);
  1680. if (ret)
  1681. IWL_DEBUG_MAC80211(priv, "Error setting sleep level\n");
  1682. }
  1683. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  1684. IWL_DEBUG_MAC80211(priv, "TX Power old=%d new=%d\n",
  1685. priv->tx_power_user_lmt, conf->power_level);
  1686. iwl_set_tx_power(priv, conf->power_level, false);
  1687. }
  1688. if (!iwl_is_ready(priv)) {
  1689. IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
  1690. goto out;
  1691. }
  1692. if (scan_active)
  1693. goto out;
  1694. for_each_context(priv, ctx) {
  1695. if (memcmp(&ctx->active, &ctx->staging, sizeof(ctx->staging)))
  1696. iwlcore_commit_rxon(priv, ctx);
  1697. else
  1698. IWL_DEBUG_INFO(priv,
  1699. "Not re-sending same RXON configuration.\n");
  1700. }
  1701. out:
  1702. IWL_DEBUG_MAC80211(priv, "leave\n");
  1703. mutex_unlock(&priv->mutex);
  1704. return ret;
  1705. }
  1706. EXPORT_SYMBOL(iwl_mac_config);
  1707. void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
  1708. {
  1709. struct iwl_priv *priv = hw->priv;
  1710. unsigned long flags;
  1711. /* IBSS can only be the IWL_RXON_CTX_BSS context */
  1712. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1713. mutex_lock(&priv->mutex);
  1714. IWL_DEBUG_MAC80211(priv, "enter\n");
  1715. spin_lock_irqsave(&priv->lock, flags);
  1716. memset(&priv->current_ht_config, 0, sizeof(struct iwl_ht_config));
  1717. spin_unlock_irqrestore(&priv->lock, flags);
  1718. spin_lock_irqsave(&priv->lock, flags);
  1719. /* new association get rid of ibss beacon skb */
  1720. if (priv->beacon_skb)
  1721. dev_kfree_skb(priv->beacon_skb);
  1722. priv->beacon_skb = NULL;
  1723. priv->timestamp = 0;
  1724. spin_unlock_irqrestore(&priv->lock, flags);
  1725. iwl_scan_cancel_timeout(priv, 100);
  1726. if (!iwl_is_ready_rf(priv)) {
  1727. IWL_DEBUG_MAC80211(priv, "leave - not ready\n");
  1728. mutex_unlock(&priv->mutex);
  1729. return;
  1730. }
  1731. /* we are restarting association process
  1732. * clear RXON_FILTER_ASSOC_MSK bit
  1733. */
  1734. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1735. iwlcore_commit_rxon(priv, ctx);
  1736. iwl_set_rate(priv);
  1737. mutex_unlock(&priv->mutex);
  1738. IWL_DEBUG_MAC80211(priv, "leave\n");
  1739. }
  1740. EXPORT_SYMBOL(iwl_mac_reset_tsf);
  1741. int iwl_alloc_txq_mem(struct iwl_priv *priv)
  1742. {
  1743. if (!priv->txq)
  1744. priv->txq = kzalloc(
  1745. sizeof(struct iwl_tx_queue) *
  1746. priv->cfg->base_params->num_of_queues,
  1747. GFP_KERNEL);
  1748. if (!priv->txq) {
  1749. IWL_ERR(priv, "Not enough memory for txq\n");
  1750. return -ENOMEM;
  1751. }
  1752. return 0;
  1753. }
  1754. EXPORT_SYMBOL(iwl_alloc_txq_mem);
  1755. void iwl_free_txq_mem(struct iwl_priv *priv)
  1756. {
  1757. kfree(priv->txq);
  1758. priv->txq = NULL;
  1759. }
  1760. EXPORT_SYMBOL(iwl_free_txq_mem);
  1761. #ifdef CONFIG_IWLWIFI_DEBUGFS
  1762. #define IWL_TRAFFIC_DUMP_SIZE (IWL_TRAFFIC_ENTRY_SIZE * IWL_TRAFFIC_ENTRIES)
  1763. void iwl_reset_traffic_log(struct iwl_priv *priv)
  1764. {
  1765. priv->tx_traffic_idx = 0;
  1766. priv->rx_traffic_idx = 0;
  1767. if (priv->tx_traffic)
  1768. memset(priv->tx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  1769. if (priv->rx_traffic)
  1770. memset(priv->rx_traffic, 0, IWL_TRAFFIC_DUMP_SIZE);
  1771. }
  1772. int iwl_alloc_traffic_mem(struct iwl_priv *priv)
  1773. {
  1774. u32 traffic_size = IWL_TRAFFIC_DUMP_SIZE;
  1775. if (iwl_debug_level & IWL_DL_TX) {
  1776. if (!priv->tx_traffic) {
  1777. priv->tx_traffic =
  1778. kzalloc(traffic_size, GFP_KERNEL);
  1779. if (!priv->tx_traffic)
  1780. return -ENOMEM;
  1781. }
  1782. }
  1783. if (iwl_debug_level & IWL_DL_RX) {
  1784. if (!priv->rx_traffic) {
  1785. priv->rx_traffic =
  1786. kzalloc(traffic_size, GFP_KERNEL);
  1787. if (!priv->rx_traffic)
  1788. return -ENOMEM;
  1789. }
  1790. }
  1791. iwl_reset_traffic_log(priv);
  1792. return 0;
  1793. }
  1794. EXPORT_SYMBOL(iwl_alloc_traffic_mem);
  1795. void iwl_free_traffic_mem(struct iwl_priv *priv)
  1796. {
  1797. kfree(priv->tx_traffic);
  1798. priv->tx_traffic = NULL;
  1799. kfree(priv->rx_traffic);
  1800. priv->rx_traffic = NULL;
  1801. }
  1802. EXPORT_SYMBOL(iwl_free_traffic_mem);
  1803. void iwl_dbg_log_tx_data_frame(struct iwl_priv *priv,
  1804. u16 length, struct ieee80211_hdr *header)
  1805. {
  1806. __le16 fc;
  1807. u16 len;
  1808. if (likely(!(iwl_debug_level & IWL_DL_TX)))
  1809. return;
  1810. if (!priv->tx_traffic)
  1811. return;
  1812. fc = header->frame_control;
  1813. if (ieee80211_is_data(fc)) {
  1814. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  1815. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  1816. memcpy((priv->tx_traffic +
  1817. (priv->tx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  1818. header, len);
  1819. priv->tx_traffic_idx =
  1820. (priv->tx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  1821. }
  1822. }
  1823. EXPORT_SYMBOL(iwl_dbg_log_tx_data_frame);
  1824. void iwl_dbg_log_rx_data_frame(struct iwl_priv *priv,
  1825. u16 length, struct ieee80211_hdr *header)
  1826. {
  1827. __le16 fc;
  1828. u16 len;
  1829. if (likely(!(iwl_debug_level & IWL_DL_RX)))
  1830. return;
  1831. if (!priv->rx_traffic)
  1832. return;
  1833. fc = header->frame_control;
  1834. if (ieee80211_is_data(fc)) {
  1835. len = (length > IWL_TRAFFIC_ENTRY_SIZE)
  1836. ? IWL_TRAFFIC_ENTRY_SIZE : length;
  1837. memcpy((priv->rx_traffic +
  1838. (priv->rx_traffic_idx * IWL_TRAFFIC_ENTRY_SIZE)),
  1839. header, len);
  1840. priv->rx_traffic_idx =
  1841. (priv->rx_traffic_idx + 1) % IWL_TRAFFIC_ENTRIES;
  1842. }
  1843. }
  1844. EXPORT_SYMBOL(iwl_dbg_log_rx_data_frame);
  1845. const char *get_mgmt_string(int cmd)
  1846. {
  1847. switch (cmd) {
  1848. IWL_CMD(MANAGEMENT_ASSOC_REQ);
  1849. IWL_CMD(MANAGEMENT_ASSOC_RESP);
  1850. IWL_CMD(MANAGEMENT_REASSOC_REQ);
  1851. IWL_CMD(MANAGEMENT_REASSOC_RESP);
  1852. IWL_CMD(MANAGEMENT_PROBE_REQ);
  1853. IWL_CMD(MANAGEMENT_PROBE_RESP);
  1854. IWL_CMD(MANAGEMENT_BEACON);
  1855. IWL_CMD(MANAGEMENT_ATIM);
  1856. IWL_CMD(MANAGEMENT_DISASSOC);
  1857. IWL_CMD(MANAGEMENT_AUTH);
  1858. IWL_CMD(MANAGEMENT_DEAUTH);
  1859. IWL_CMD(MANAGEMENT_ACTION);
  1860. default:
  1861. return "UNKNOWN";
  1862. }
  1863. }
  1864. const char *get_ctrl_string(int cmd)
  1865. {
  1866. switch (cmd) {
  1867. IWL_CMD(CONTROL_BACK_REQ);
  1868. IWL_CMD(CONTROL_BACK);
  1869. IWL_CMD(CONTROL_PSPOLL);
  1870. IWL_CMD(CONTROL_RTS);
  1871. IWL_CMD(CONTROL_CTS);
  1872. IWL_CMD(CONTROL_ACK);
  1873. IWL_CMD(CONTROL_CFEND);
  1874. IWL_CMD(CONTROL_CFENDACK);
  1875. default:
  1876. return "UNKNOWN";
  1877. }
  1878. }
  1879. void iwl_clear_traffic_stats(struct iwl_priv *priv)
  1880. {
  1881. memset(&priv->tx_stats, 0, sizeof(struct traffic_stats));
  1882. memset(&priv->rx_stats, 0, sizeof(struct traffic_stats));
  1883. priv->led_tpt = 0;
  1884. }
  1885. /*
  1886. * if CONFIG_IWLWIFI_DEBUGFS defined, iwl_update_stats function will
  1887. * record all the MGMT, CTRL and DATA pkt for both TX and Rx pass.
  1888. * Use debugFs to display the rx/rx_statistics
  1889. * if CONFIG_IWLWIFI_DEBUGFS not being defined, then no MGMT and CTRL
  1890. * information will be recorded, but DATA pkt still will be recorded
  1891. * for the reason of iwl_led.c need to control the led blinking based on
  1892. * number of tx and rx data.
  1893. *
  1894. */
  1895. void iwl_update_stats(struct iwl_priv *priv, bool is_tx, __le16 fc, u16 len)
  1896. {
  1897. struct traffic_stats *stats;
  1898. if (is_tx)
  1899. stats = &priv->tx_stats;
  1900. else
  1901. stats = &priv->rx_stats;
  1902. if (ieee80211_is_mgmt(fc)) {
  1903. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  1904. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  1905. stats->mgmt[MANAGEMENT_ASSOC_REQ]++;
  1906. break;
  1907. case cpu_to_le16(IEEE80211_STYPE_ASSOC_RESP):
  1908. stats->mgmt[MANAGEMENT_ASSOC_RESP]++;
  1909. break;
  1910. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  1911. stats->mgmt[MANAGEMENT_REASSOC_REQ]++;
  1912. break;
  1913. case cpu_to_le16(IEEE80211_STYPE_REASSOC_RESP):
  1914. stats->mgmt[MANAGEMENT_REASSOC_RESP]++;
  1915. break;
  1916. case cpu_to_le16(IEEE80211_STYPE_PROBE_REQ):
  1917. stats->mgmt[MANAGEMENT_PROBE_REQ]++;
  1918. break;
  1919. case cpu_to_le16(IEEE80211_STYPE_PROBE_RESP):
  1920. stats->mgmt[MANAGEMENT_PROBE_RESP]++;
  1921. break;
  1922. case cpu_to_le16(IEEE80211_STYPE_BEACON):
  1923. stats->mgmt[MANAGEMENT_BEACON]++;
  1924. break;
  1925. case cpu_to_le16(IEEE80211_STYPE_ATIM):
  1926. stats->mgmt[MANAGEMENT_ATIM]++;
  1927. break;
  1928. case cpu_to_le16(IEEE80211_STYPE_DISASSOC):
  1929. stats->mgmt[MANAGEMENT_DISASSOC]++;
  1930. break;
  1931. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  1932. stats->mgmt[MANAGEMENT_AUTH]++;
  1933. break;
  1934. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  1935. stats->mgmt[MANAGEMENT_DEAUTH]++;
  1936. break;
  1937. case cpu_to_le16(IEEE80211_STYPE_ACTION):
  1938. stats->mgmt[MANAGEMENT_ACTION]++;
  1939. break;
  1940. }
  1941. } else if (ieee80211_is_ctl(fc)) {
  1942. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  1943. case cpu_to_le16(IEEE80211_STYPE_BACK_REQ):
  1944. stats->ctrl[CONTROL_BACK_REQ]++;
  1945. break;
  1946. case cpu_to_le16(IEEE80211_STYPE_BACK):
  1947. stats->ctrl[CONTROL_BACK]++;
  1948. break;
  1949. case cpu_to_le16(IEEE80211_STYPE_PSPOLL):
  1950. stats->ctrl[CONTROL_PSPOLL]++;
  1951. break;
  1952. case cpu_to_le16(IEEE80211_STYPE_RTS):
  1953. stats->ctrl[CONTROL_RTS]++;
  1954. break;
  1955. case cpu_to_le16(IEEE80211_STYPE_CTS):
  1956. stats->ctrl[CONTROL_CTS]++;
  1957. break;
  1958. case cpu_to_le16(IEEE80211_STYPE_ACK):
  1959. stats->ctrl[CONTROL_ACK]++;
  1960. break;
  1961. case cpu_to_le16(IEEE80211_STYPE_CFEND):
  1962. stats->ctrl[CONTROL_CFEND]++;
  1963. break;
  1964. case cpu_to_le16(IEEE80211_STYPE_CFENDACK):
  1965. stats->ctrl[CONTROL_CFENDACK]++;
  1966. break;
  1967. }
  1968. } else {
  1969. /* data */
  1970. stats->data_cnt++;
  1971. stats->data_bytes += len;
  1972. }
  1973. iwl_leds_background(priv);
  1974. }
  1975. EXPORT_SYMBOL(iwl_update_stats);
  1976. #endif
  1977. static void iwl_force_rf_reset(struct iwl_priv *priv)
  1978. {
  1979. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1980. return;
  1981. if (!iwl_is_any_associated(priv)) {
  1982. IWL_DEBUG_SCAN(priv, "force reset rejected: not associated\n");
  1983. return;
  1984. }
  1985. /*
  1986. * There is no easy and better way to force reset the radio,
  1987. * the only known method is switching channel which will force to
  1988. * reset and tune the radio.
  1989. * Use internal short scan (single channel) operation to should
  1990. * achieve this objective.
  1991. * Driver should reset the radio when number of consecutive missed
  1992. * beacon, or any other uCode error condition detected.
  1993. */
  1994. IWL_DEBUG_INFO(priv, "perform radio reset.\n");
  1995. iwl_internal_short_hw_scan(priv);
  1996. }
  1997. int iwl_force_reset(struct iwl_priv *priv, int mode, bool external)
  1998. {
  1999. struct iwl_force_reset *force_reset;
  2000. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2001. return -EINVAL;
  2002. if (mode >= IWL_MAX_FORCE_RESET) {
  2003. IWL_DEBUG_INFO(priv, "invalid reset request.\n");
  2004. return -EINVAL;
  2005. }
  2006. force_reset = &priv->force_reset[mode];
  2007. force_reset->reset_request_count++;
  2008. if (!external) {
  2009. if (force_reset->last_force_reset_jiffies &&
  2010. time_after(force_reset->last_force_reset_jiffies +
  2011. force_reset->reset_duration, jiffies)) {
  2012. IWL_DEBUG_INFO(priv, "force reset rejected\n");
  2013. force_reset->reset_reject_count++;
  2014. return -EAGAIN;
  2015. }
  2016. }
  2017. force_reset->reset_success_count++;
  2018. force_reset->last_force_reset_jiffies = jiffies;
  2019. IWL_DEBUG_INFO(priv, "perform force reset (%d)\n", mode);
  2020. switch (mode) {
  2021. case IWL_RF_RESET:
  2022. iwl_force_rf_reset(priv);
  2023. break;
  2024. case IWL_FW_RESET:
  2025. /*
  2026. * if the request is from external(ex: debugfs),
  2027. * then always perform the request in regardless the module
  2028. * parameter setting
  2029. * if the request is from internal (uCode error or driver
  2030. * detect failure), then fw_restart module parameter
  2031. * need to be check before performing firmware reload
  2032. */
  2033. if (!external && !priv->cfg->mod_params->restart_fw) {
  2034. IWL_DEBUG_INFO(priv, "Cancel firmware reload based on "
  2035. "module parameter setting\n");
  2036. break;
  2037. }
  2038. IWL_ERR(priv, "On demand firmware reload\n");
  2039. /* Set the FW error flag -- cleared on iwl_down */
  2040. set_bit(STATUS_FW_ERROR, &priv->status);
  2041. wake_up_interruptible(&priv->wait_command_queue);
  2042. /*
  2043. * Keep the restart process from trying to send host
  2044. * commands by clearing the INIT status bit
  2045. */
  2046. clear_bit(STATUS_READY, &priv->status);
  2047. queue_work(priv->workqueue, &priv->restart);
  2048. break;
  2049. }
  2050. return 0;
  2051. }
  2052. /**
  2053. * iwl_bg_monitor_recover - Timer callback to check for stuck queue and recover
  2054. *
  2055. * During normal condition (no queue is stuck), the timer is continually set to
  2056. * execute every monitor_recover_period milliseconds after the last timer
  2057. * expired. When the queue read_ptr is at the same place, the timer is
  2058. * shorten to 100mSecs. This is
  2059. * 1) to reduce the chance that the read_ptr may wrap around (not stuck)
  2060. * 2) to detect the stuck queues quicker before the station and AP can
  2061. * disassociate each other.
  2062. *
  2063. * This function monitors all the tx queues and recover from it if any
  2064. * of the queues are stuck.
  2065. * 1. It first check the cmd queue for stuck conditions. If it is stuck,
  2066. * it will recover by resetting the firmware and return.
  2067. * 2. Then, it checks for station association. If it associates it will check
  2068. * other queues. If any queue is stuck, it will recover by resetting
  2069. * the firmware.
  2070. * Note: It the number of times the queue read_ptr to be at the same place to
  2071. * be MAX_REPEAT+1 in order to consider to be stuck.
  2072. */
  2073. /*
  2074. * The maximum number of times the read pointer of the tx queue at the
  2075. * same place without considering to be stuck.
  2076. */
  2077. #define MAX_REPEAT (2)
  2078. static int iwl_check_stuck_queue(struct iwl_priv *priv, int cnt)
  2079. {
  2080. struct iwl_tx_queue *txq;
  2081. struct iwl_queue *q;
  2082. txq = &priv->txq[cnt];
  2083. q = &txq->q;
  2084. /* queue is empty, skip */
  2085. if (q->read_ptr == q->write_ptr)
  2086. return 0;
  2087. if (q->read_ptr == q->last_read_ptr) {
  2088. /* a queue has not been read from last time */
  2089. if (q->repeat_same_read_ptr > MAX_REPEAT) {
  2090. IWL_ERR(priv,
  2091. "queue %d stuck %d time. Fw reload.\n",
  2092. q->id, q->repeat_same_read_ptr);
  2093. q->repeat_same_read_ptr = 0;
  2094. iwl_force_reset(priv, IWL_FW_RESET, false);
  2095. } else {
  2096. q->repeat_same_read_ptr++;
  2097. IWL_DEBUG_RADIO(priv,
  2098. "queue %d, not read %d time\n",
  2099. q->id,
  2100. q->repeat_same_read_ptr);
  2101. mod_timer(&priv->monitor_recover,
  2102. jiffies + msecs_to_jiffies(
  2103. IWL_ONE_HUNDRED_MSECS));
  2104. return 1;
  2105. }
  2106. } else {
  2107. q->last_read_ptr = q->read_ptr;
  2108. q->repeat_same_read_ptr = 0;
  2109. }
  2110. return 0;
  2111. }
  2112. void iwl_bg_monitor_recover(unsigned long data)
  2113. {
  2114. struct iwl_priv *priv = (struct iwl_priv *)data;
  2115. int cnt;
  2116. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2117. return;
  2118. /* monitor and check for stuck cmd queue */
  2119. if (iwl_check_stuck_queue(priv, priv->cmd_queue))
  2120. return;
  2121. /* monitor and check for other stuck queues */
  2122. if (iwl_is_any_associated(priv)) {
  2123. for (cnt = 0; cnt < priv->hw_params.max_txq_num; cnt++) {
  2124. /* skip as we already checked the command queue */
  2125. if (cnt == priv->cmd_queue)
  2126. continue;
  2127. if (iwl_check_stuck_queue(priv, cnt))
  2128. return;
  2129. }
  2130. }
  2131. if (priv->cfg->base_params->monitor_recover_period) {
  2132. /*
  2133. * Reschedule the timer to occur in
  2134. * priv->cfg->base_params->monitor_recover_period
  2135. */
  2136. mod_timer(&priv->monitor_recover, jiffies + msecs_to_jiffies(
  2137. priv->cfg->base_params->monitor_recover_period));
  2138. }
  2139. }
  2140. EXPORT_SYMBOL(iwl_bg_monitor_recover);
  2141. /*
  2142. * extended beacon time format
  2143. * time in usec will be changed into a 32-bit value in extended:internal format
  2144. * the extended part is the beacon counts
  2145. * the internal part is the time in usec within one beacon interval
  2146. */
  2147. u32 iwl_usecs_to_beacons(struct iwl_priv *priv, u32 usec, u32 beacon_interval)
  2148. {
  2149. u32 quot;
  2150. u32 rem;
  2151. u32 interval = beacon_interval * TIME_UNIT;
  2152. if (!interval || !usec)
  2153. return 0;
  2154. quot = (usec / interval) &
  2155. (iwl_beacon_time_mask_high(priv,
  2156. priv->hw_params.beacon_time_tsf_bits) >>
  2157. priv->hw_params.beacon_time_tsf_bits);
  2158. rem = (usec % interval) & iwl_beacon_time_mask_low(priv,
  2159. priv->hw_params.beacon_time_tsf_bits);
  2160. return (quot << priv->hw_params.beacon_time_tsf_bits) + rem;
  2161. }
  2162. EXPORT_SYMBOL(iwl_usecs_to_beacons);
  2163. /* base is usually what we get from ucode with each received frame,
  2164. * the same as HW timer counter counting down
  2165. */
  2166. __le32 iwl_add_beacon_time(struct iwl_priv *priv, u32 base,
  2167. u32 addon, u32 beacon_interval)
  2168. {
  2169. u32 base_low = base & iwl_beacon_time_mask_low(priv,
  2170. priv->hw_params.beacon_time_tsf_bits);
  2171. u32 addon_low = addon & iwl_beacon_time_mask_low(priv,
  2172. priv->hw_params.beacon_time_tsf_bits);
  2173. u32 interval = beacon_interval * TIME_UNIT;
  2174. u32 res = (base & iwl_beacon_time_mask_high(priv,
  2175. priv->hw_params.beacon_time_tsf_bits)) +
  2176. (addon & iwl_beacon_time_mask_high(priv,
  2177. priv->hw_params.beacon_time_tsf_bits));
  2178. if (base_low > addon_low)
  2179. res += base_low - addon_low;
  2180. else if (base_low < addon_low) {
  2181. res += interval + base_low - addon_low;
  2182. res += (1 << priv->hw_params.beacon_time_tsf_bits);
  2183. } else
  2184. res += (1 << priv->hw_params.beacon_time_tsf_bits);
  2185. return cpu_to_le32(res);
  2186. }
  2187. EXPORT_SYMBOL(iwl_add_beacon_time);
  2188. #ifdef CONFIG_PM
  2189. int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  2190. {
  2191. struct iwl_priv *priv = pci_get_drvdata(pdev);
  2192. /*
  2193. * This function is called when system goes into suspend state
  2194. * mac80211 will call iwl_mac_stop() from the mac80211 suspend function
  2195. * first but since iwl_mac_stop() has no knowledge of who the caller is,
  2196. * it will not call apm_ops.stop() to stop the DMA operation.
  2197. * Calling apm_ops.stop here to make sure we stop the DMA.
  2198. */
  2199. iwl_apm_stop(priv);
  2200. pci_save_state(pdev);
  2201. pci_disable_device(pdev);
  2202. pci_set_power_state(pdev, PCI_D3hot);
  2203. return 0;
  2204. }
  2205. EXPORT_SYMBOL(iwl_pci_suspend);
  2206. int iwl_pci_resume(struct pci_dev *pdev)
  2207. {
  2208. struct iwl_priv *priv = pci_get_drvdata(pdev);
  2209. int ret;
  2210. bool hw_rfkill = false;
  2211. /*
  2212. * We disable the RETRY_TIMEOUT register (0x41) to keep
  2213. * PCI Tx retries from interfering with C3 CPU state.
  2214. */
  2215. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  2216. pci_set_power_state(pdev, PCI_D0);
  2217. ret = pci_enable_device(pdev);
  2218. if (ret)
  2219. return ret;
  2220. pci_restore_state(pdev);
  2221. iwl_enable_interrupts(priv);
  2222. if (!(iwl_read32(priv, CSR_GP_CNTRL) &
  2223. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  2224. hw_rfkill = true;
  2225. if (hw_rfkill)
  2226. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2227. else
  2228. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2229. wiphy_rfkill_set_hw_state(priv->hw->wiphy, hw_rfkill);
  2230. return 0;
  2231. }
  2232. EXPORT_SYMBOL(iwl_pci_resume);
  2233. #endif /* CONFIG_PM */