iwl-core.c 82 KB

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