iwl-core.c 83 KB

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