iwl-core.c 83 KB

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