iwl-agn.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  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. *****************************************************************************/
  29. #include <linux/kernel.h>
  30. #include <linux/module.h>
  31. #include <linux/init.h>
  32. #include <linux/slab.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/delay.h>
  35. #include <linux/sched.h>
  36. #include <linux/skbuff.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/firmware.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/if_arp.h>
  41. #include <net/mac80211.h>
  42. #include <asm/div64.h>
  43. #include "iwl-eeprom.h"
  44. #include "iwl-dev.h"
  45. #include "iwl-core.h"
  46. #include "iwl-io.h"
  47. #include "iwl-helpers.h"
  48. #include "iwl-sta.h"
  49. #include "iwl-agn-calib.h"
  50. #include "iwl-agn.h"
  51. #include "iwl-shared.h"
  52. #include "iwl-bus.h"
  53. #include "iwl-trans.h"
  54. /******************************************************************************
  55. *
  56. * module boiler plate
  57. *
  58. ******************************************************************************/
  59. /*
  60. * module name, copyright, version, etc.
  61. */
  62. #define DRV_DESCRIPTION "Intel(R) Wireless WiFi Link AGN driver for Linux"
  63. #ifdef CONFIG_IWLWIFI_DEBUG
  64. #define VD "d"
  65. #else
  66. #define VD
  67. #endif
  68. #define DRV_VERSION IWLWIFI_VERSION VD
  69. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  70. MODULE_VERSION(DRV_VERSION);
  71. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  72. MODULE_LICENSE("GPL");
  73. void iwl_update_chain_flags(struct iwl_priv *priv)
  74. {
  75. struct iwl_rxon_context *ctx;
  76. for_each_context(priv, ctx) {
  77. iwlagn_set_rxon_chain(priv, ctx);
  78. if (ctx->active.rx_chain != ctx->staging.rx_chain)
  79. iwlagn_commit_rxon(priv, ctx);
  80. }
  81. }
  82. /* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
  83. static void iwl_set_beacon_tim(struct iwl_priv *priv,
  84. struct iwl_tx_beacon_cmd *tx_beacon_cmd,
  85. u8 *beacon, u32 frame_size)
  86. {
  87. u16 tim_idx;
  88. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
  89. /*
  90. * The index is relative to frame start but we start looking at the
  91. * variable-length part of the beacon.
  92. */
  93. tim_idx = mgmt->u.beacon.variable - beacon;
  94. /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
  95. while ((tim_idx < (frame_size - 2)) &&
  96. (beacon[tim_idx] != WLAN_EID_TIM))
  97. tim_idx += beacon[tim_idx+1] + 2;
  98. /* If TIM field was found, set variables */
  99. if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
  100. tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
  101. tx_beacon_cmd->tim_size = beacon[tim_idx+1];
  102. } else
  103. IWL_WARN(priv, "Unable to find TIM Element in beacon\n");
  104. }
  105. int iwlagn_send_beacon_cmd(struct iwl_priv *priv)
  106. {
  107. struct iwl_tx_beacon_cmd *tx_beacon_cmd;
  108. struct iwl_host_cmd cmd = {
  109. .id = REPLY_TX_BEACON,
  110. .flags = CMD_SYNC,
  111. };
  112. struct ieee80211_tx_info *info;
  113. u32 frame_size;
  114. u32 rate_flags;
  115. u32 rate;
  116. /*
  117. * We have to set up the TX command, the TX Beacon command, and the
  118. * beacon contents.
  119. */
  120. lockdep_assert_held(&priv->shrd->mutex);
  121. if (!priv->beacon_ctx) {
  122. IWL_ERR(priv, "trying to build beacon w/o beacon context!\n");
  123. return 0;
  124. }
  125. if (WARN_ON(!priv->beacon_skb))
  126. return -EINVAL;
  127. /* Allocate beacon command */
  128. if (!priv->beacon_cmd)
  129. priv->beacon_cmd = kzalloc(sizeof(*tx_beacon_cmd), GFP_KERNEL);
  130. tx_beacon_cmd = priv->beacon_cmd;
  131. if (!tx_beacon_cmd)
  132. return -ENOMEM;
  133. frame_size = priv->beacon_skb->len;
  134. /* Set up TX command fields */
  135. tx_beacon_cmd->tx.len = cpu_to_le16((u16)frame_size);
  136. tx_beacon_cmd->tx.sta_id = priv->beacon_ctx->bcast_sta_id;
  137. tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  138. tx_beacon_cmd->tx.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK |
  139. TX_CMD_FLG_TSF_MSK | TX_CMD_FLG_STA_RATE_MSK;
  140. /* Set up TX beacon command fields */
  141. iwl_set_beacon_tim(priv, tx_beacon_cmd, priv->beacon_skb->data,
  142. frame_size);
  143. /* Set up packet rate and flags */
  144. info = IEEE80211_SKB_CB(priv->beacon_skb);
  145. /*
  146. * Let's set up the rate at least somewhat correctly;
  147. * it will currently not actually be used by the uCode,
  148. * it uses the broadcast station's rate instead.
  149. */
  150. if (info->control.rates[0].idx < 0 ||
  151. info->control.rates[0].flags & IEEE80211_TX_RC_MCS)
  152. rate = 0;
  153. else
  154. rate = info->control.rates[0].idx;
  155. priv->mgmt_tx_ant = iwl_toggle_tx_ant(priv, priv->mgmt_tx_ant,
  156. hw_params(priv).valid_tx_ant);
  157. rate_flags = iwl_ant_idx_to_flags(priv->mgmt_tx_ant);
  158. /* In mac80211, rates for 5 GHz start at 0 */
  159. if (info->band == IEEE80211_BAND_5GHZ)
  160. rate += IWL_FIRST_OFDM_RATE;
  161. else if (rate >= IWL_FIRST_CCK_RATE && rate <= IWL_LAST_CCK_RATE)
  162. rate_flags |= RATE_MCS_CCK_MSK;
  163. tx_beacon_cmd->tx.rate_n_flags =
  164. iwl_hw_set_rate_n_flags(rate, rate_flags);
  165. /* Submit command */
  166. cmd.len[0] = sizeof(*tx_beacon_cmd);
  167. cmd.data[0] = tx_beacon_cmd;
  168. cmd.dataflags[0] = IWL_HCMD_DFL_NOCOPY;
  169. cmd.len[1] = frame_size;
  170. cmd.data[1] = priv->beacon_skb->data;
  171. cmd.dataflags[1] = IWL_HCMD_DFL_NOCOPY;
  172. return iwl_trans_send_cmd(trans(priv), &cmd);
  173. }
  174. static void iwl_bg_beacon_update(struct work_struct *work)
  175. {
  176. struct iwl_priv *priv =
  177. container_of(work, struct iwl_priv, beacon_update);
  178. struct sk_buff *beacon;
  179. mutex_lock(&priv->shrd->mutex);
  180. if (!priv->beacon_ctx) {
  181. IWL_ERR(priv, "updating beacon w/o beacon context!\n");
  182. goto out;
  183. }
  184. if (priv->beacon_ctx->vif->type != NL80211_IFTYPE_AP) {
  185. /*
  186. * The ucode will send beacon notifications even in
  187. * IBSS mode, but we don't want to process them. But
  188. * we need to defer the type check to here due to
  189. * requiring locking around the beacon_ctx access.
  190. */
  191. goto out;
  192. }
  193. /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
  194. beacon = ieee80211_beacon_get(priv->hw, priv->beacon_ctx->vif);
  195. if (!beacon) {
  196. IWL_ERR(priv, "update beacon failed -- keeping old\n");
  197. goto out;
  198. }
  199. /* new beacon skb is allocated every time; dispose previous.*/
  200. dev_kfree_skb(priv->beacon_skb);
  201. priv->beacon_skb = beacon;
  202. iwlagn_send_beacon_cmd(priv);
  203. out:
  204. mutex_unlock(&priv->shrd->mutex);
  205. }
  206. static void iwl_bg_bt_runtime_config(struct work_struct *work)
  207. {
  208. struct iwl_priv *priv =
  209. container_of(work, struct iwl_priv, bt_runtime_config);
  210. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  211. return;
  212. /* dont send host command if rf-kill is on */
  213. if (!iwl_is_ready_rf(priv->shrd))
  214. return;
  215. iwlagn_send_advance_bt_config(priv);
  216. }
  217. static void iwl_bg_bt_full_concurrency(struct work_struct *work)
  218. {
  219. struct iwl_priv *priv =
  220. container_of(work, struct iwl_priv, bt_full_concurrency);
  221. struct iwl_rxon_context *ctx;
  222. mutex_lock(&priv->shrd->mutex);
  223. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  224. goto out;
  225. /* dont send host command if rf-kill is on */
  226. if (!iwl_is_ready_rf(priv->shrd))
  227. goto out;
  228. IWL_DEBUG_INFO(priv, "BT coex in %s mode\n",
  229. priv->bt_full_concurrent ?
  230. "full concurrency" : "3-wire");
  231. /*
  232. * LQ & RXON updated cmds must be sent before BT Config cmd
  233. * to avoid 3-wire collisions
  234. */
  235. for_each_context(priv, ctx) {
  236. iwlagn_set_rxon_chain(priv, ctx);
  237. iwlagn_commit_rxon(priv, ctx);
  238. }
  239. iwlagn_send_advance_bt_config(priv);
  240. out:
  241. mutex_unlock(&priv->shrd->mutex);
  242. }
  243. /**
  244. * iwl_bg_statistics_periodic - Timer callback to queue statistics
  245. *
  246. * This callback is provided in order to send a statistics request.
  247. *
  248. * This timer function is continually reset to execute within
  249. * REG_RECALIB_PERIOD seconds since the last STATISTICS_NOTIFICATION
  250. * was received. We need to ensure we receive the statistics in order
  251. * to update the temperature used for calibrating the TXPOWER.
  252. */
  253. static void iwl_bg_statistics_periodic(unsigned long data)
  254. {
  255. struct iwl_priv *priv = (struct iwl_priv *)data;
  256. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  257. return;
  258. /* dont send host command if rf-kill is on */
  259. if (!iwl_is_ready_rf(priv->shrd))
  260. return;
  261. iwl_send_statistics_request(priv, CMD_ASYNC, false);
  262. }
  263. static void iwl_print_cont_event_trace(struct iwl_priv *priv, u32 base,
  264. u32 start_idx, u32 num_events,
  265. u32 mode)
  266. {
  267. u32 i;
  268. u32 ptr; /* SRAM byte address of log data */
  269. u32 ev, time, data; /* event log data */
  270. unsigned long reg_flags;
  271. if (mode == 0)
  272. ptr = base + (4 * sizeof(u32)) + (start_idx * 2 * sizeof(u32));
  273. else
  274. ptr = base + (4 * sizeof(u32)) + (start_idx * 3 * sizeof(u32));
  275. /* Make sure device is powered up for SRAM reads */
  276. spin_lock_irqsave(&priv->reg_lock, reg_flags);
  277. if (iwl_grab_nic_access(priv)) {
  278. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  279. return;
  280. }
  281. /* Set starting address; reads will auto-increment */
  282. iwl_write32(priv, HBUS_TARG_MEM_RADDR, ptr);
  283. rmb();
  284. /*
  285. * "time" is actually "data" for mode 0 (no timestamp).
  286. * place event id # at far right for easier visual parsing.
  287. */
  288. for (i = 0; i < num_events; i++) {
  289. ev = iwl_read32(priv, HBUS_TARG_MEM_RDAT);
  290. time = iwl_read32(priv, HBUS_TARG_MEM_RDAT);
  291. if (mode == 0) {
  292. trace_iwlwifi_dev_ucode_cont_event(priv,
  293. 0, time, ev);
  294. } else {
  295. data = iwl_read32(priv, HBUS_TARG_MEM_RDAT);
  296. trace_iwlwifi_dev_ucode_cont_event(priv,
  297. time, data, ev);
  298. }
  299. }
  300. /* Allow device to power down */
  301. iwl_release_nic_access(priv);
  302. spin_unlock_irqrestore(&priv->reg_lock, reg_flags);
  303. }
  304. static void iwl_continuous_event_trace(struct iwl_priv *priv)
  305. {
  306. u32 capacity; /* event log capacity in # entries */
  307. u32 base; /* SRAM byte address of event log header */
  308. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  309. u32 num_wraps; /* # times uCode wrapped to top of log */
  310. u32 next_entry; /* index of next entry to be written by uCode */
  311. base = priv->device_pointers.error_event_table;
  312. if (iwlagn_hw_valid_rtc_data_addr(base)) {
  313. capacity = iwl_read_targ_mem(priv, base);
  314. num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
  315. mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
  316. next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
  317. } else
  318. return;
  319. if (num_wraps == priv->event_log.num_wraps) {
  320. iwl_print_cont_event_trace(priv,
  321. base, priv->event_log.next_entry,
  322. next_entry - priv->event_log.next_entry,
  323. mode);
  324. priv->event_log.non_wraps_count++;
  325. } else {
  326. if ((num_wraps - priv->event_log.num_wraps) > 1)
  327. priv->event_log.wraps_more_count++;
  328. else
  329. priv->event_log.wraps_once_count++;
  330. trace_iwlwifi_dev_ucode_wrap_event(priv,
  331. num_wraps - priv->event_log.num_wraps,
  332. next_entry, priv->event_log.next_entry);
  333. if (next_entry < priv->event_log.next_entry) {
  334. iwl_print_cont_event_trace(priv, base,
  335. priv->event_log.next_entry,
  336. capacity - priv->event_log.next_entry,
  337. mode);
  338. iwl_print_cont_event_trace(priv, base, 0,
  339. next_entry, mode);
  340. } else {
  341. iwl_print_cont_event_trace(priv, base,
  342. next_entry, capacity - next_entry,
  343. mode);
  344. iwl_print_cont_event_trace(priv, base, 0,
  345. next_entry, mode);
  346. }
  347. }
  348. priv->event_log.num_wraps = num_wraps;
  349. priv->event_log.next_entry = next_entry;
  350. }
  351. /**
  352. * iwl_bg_ucode_trace - Timer callback to log ucode event
  353. *
  354. * The timer is continually set to execute every
  355. * UCODE_TRACE_PERIOD milliseconds after the last timer expired
  356. * this function is to perform continuous uCode event logging operation
  357. * if enabled
  358. */
  359. static void iwl_bg_ucode_trace(unsigned long data)
  360. {
  361. struct iwl_priv *priv = (struct iwl_priv *)data;
  362. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  363. return;
  364. if (priv->event_log.ucode_trace) {
  365. iwl_continuous_event_trace(priv);
  366. /* Reschedule the timer to occur in UCODE_TRACE_PERIOD */
  367. mod_timer(&priv->ucode_trace,
  368. jiffies + msecs_to_jiffies(UCODE_TRACE_PERIOD));
  369. }
  370. }
  371. static void iwl_bg_tx_flush(struct work_struct *work)
  372. {
  373. struct iwl_priv *priv =
  374. container_of(work, struct iwl_priv, tx_flush);
  375. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  376. return;
  377. /* do nothing if rf-kill is on */
  378. if (!iwl_is_ready_rf(priv->shrd))
  379. return;
  380. IWL_DEBUG_INFO(priv, "device request: flush all tx frames\n");
  381. iwlagn_dev_txfifo_flush(priv, IWL_DROP_ALL);
  382. }
  383. /*****************************************************************************
  384. *
  385. * sysfs attributes
  386. *
  387. *****************************************************************************/
  388. #ifdef CONFIG_IWLWIFI_DEBUG
  389. /*
  390. * The following adds a new attribute to the sysfs representation
  391. * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
  392. * used for controlling the debug level.
  393. *
  394. * See the level definitions in iwl for details.
  395. *
  396. * The debug_level being managed using sysfs below is a per device debug
  397. * level that is used instead of the global debug level if it (the per
  398. * device debug level) is set.
  399. */
  400. static ssize_t show_debug_level(struct device *d,
  401. struct device_attribute *attr, char *buf)
  402. {
  403. struct iwl_shared *shrd = dev_get_drvdata(d);
  404. return sprintf(buf, "0x%08X\n", iwl_get_debug_level(shrd));
  405. }
  406. static ssize_t store_debug_level(struct device *d,
  407. struct device_attribute *attr,
  408. const char *buf, size_t count)
  409. {
  410. struct iwl_shared *shrd = dev_get_drvdata(d);
  411. struct iwl_priv *priv = shrd->priv;
  412. unsigned long val;
  413. int ret;
  414. ret = strict_strtoul(buf, 0, &val);
  415. if (ret)
  416. IWL_ERR(priv, "%s is not in hex or decimal form.\n", buf);
  417. else {
  418. shrd->dbg_level_dev = val;
  419. if (iwl_alloc_traffic_mem(priv))
  420. IWL_ERR(shrd->priv,
  421. "Not enough memory to generate traffic log\n");
  422. }
  423. return strnlen(buf, count);
  424. }
  425. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  426. show_debug_level, store_debug_level);
  427. #endif /* CONFIG_IWLWIFI_DEBUG */
  428. static ssize_t show_temperature(struct device *d,
  429. struct device_attribute *attr, char *buf)
  430. {
  431. struct iwl_shared *shrd = dev_get_drvdata(d);
  432. struct iwl_priv *priv = shrd->priv;
  433. if (!iwl_is_alive(priv->shrd))
  434. return -EAGAIN;
  435. return sprintf(buf, "%d\n", priv->temperature);
  436. }
  437. static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
  438. static ssize_t show_tx_power(struct device *d,
  439. struct device_attribute *attr, char *buf)
  440. {
  441. struct iwl_priv *priv = dev_get_drvdata(d);
  442. if (!iwl_is_ready_rf(priv->shrd))
  443. return sprintf(buf, "off\n");
  444. else
  445. return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
  446. }
  447. static ssize_t store_tx_power(struct device *d,
  448. struct device_attribute *attr,
  449. const char *buf, size_t count)
  450. {
  451. struct iwl_priv *priv = dev_get_drvdata(d);
  452. unsigned long val;
  453. int ret;
  454. ret = strict_strtoul(buf, 10, &val);
  455. if (ret)
  456. IWL_INFO(priv, "%s is not in decimal form.\n", buf);
  457. else {
  458. ret = iwl_set_tx_power(priv, val, false);
  459. if (ret)
  460. IWL_ERR(priv, "failed setting tx power (0x%d).\n",
  461. ret);
  462. else
  463. ret = count;
  464. }
  465. return ret;
  466. }
  467. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
  468. static struct attribute *iwl_sysfs_entries[] = {
  469. &dev_attr_temperature.attr,
  470. &dev_attr_tx_power.attr,
  471. #ifdef CONFIG_IWLWIFI_DEBUG
  472. &dev_attr_debug_level.attr,
  473. #endif
  474. NULL
  475. };
  476. static struct attribute_group iwl_attribute_group = {
  477. .name = NULL, /* put in device directory */
  478. .attrs = iwl_sysfs_entries,
  479. };
  480. /******************************************************************************
  481. *
  482. * uCode download functions
  483. *
  484. ******************************************************************************/
  485. static void iwl_free_fw_desc(struct iwl_priv *priv, struct fw_desc *desc)
  486. {
  487. if (desc->v_addr)
  488. dma_free_coherent(priv->bus->dev, desc->len,
  489. desc->v_addr, desc->p_addr);
  490. desc->v_addr = NULL;
  491. desc->len = 0;
  492. }
  493. static void iwl_free_fw_img(struct iwl_priv *priv, struct fw_img *img)
  494. {
  495. iwl_free_fw_desc(priv, &img->code);
  496. iwl_free_fw_desc(priv, &img->data);
  497. }
  498. static void iwl_dealloc_ucode(struct iwl_priv *priv)
  499. {
  500. iwl_free_fw_img(priv, &priv->ucode_rt);
  501. iwl_free_fw_img(priv, &priv->ucode_init);
  502. iwl_free_fw_img(priv, &priv->ucode_wowlan);
  503. }
  504. static int iwl_alloc_fw_desc(struct iwl_priv *priv, struct fw_desc *desc,
  505. const void *data, size_t len)
  506. {
  507. if (!len) {
  508. desc->v_addr = NULL;
  509. return -EINVAL;
  510. }
  511. desc->v_addr = dma_alloc_coherent(priv->bus->dev, len,
  512. &desc->p_addr, GFP_KERNEL);
  513. if (!desc->v_addr)
  514. return -ENOMEM;
  515. desc->len = len;
  516. memcpy(desc->v_addr, data, len);
  517. return 0;
  518. }
  519. static void iwl_init_context(struct iwl_priv *priv, u32 ucode_flags)
  520. {
  521. static const u8 iwlagn_bss_ac_to_fifo[] = {
  522. IWL_TX_FIFO_VO,
  523. IWL_TX_FIFO_VI,
  524. IWL_TX_FIFO_BE,
  525. IWL_TX_FIFO_BK,
  526. };
  527. static const u8 iwlagn_bss_ac_to_queue[] = {
  528. 0, 1, 2, 3,
  529. };
  530. static const u8 iwlagn_pan_ac_to_fifo[] = {
  531. IWL_TX_FIFO_VO_IPAN,
  532. IWL_TX_FIFO_VI_IPAN,
  533. IWL_TX_FIFO_BE_IPAN,
  534. IWL_TX_FIFO_BK_IPAN,
  535. };
  536. static const u8 iwlagn_pan_ac_to_queue[] = {
  537. 7, 6, 5, 4,
  538. };
  539. int i;
  540. /*
  541. * The default context is always valid,
  542. * the PAN context depends on uCode.
  543. */
  544. priv->valid_contexts = BIT(IWL_RXON_CTX_BSS);
  545. if (ucode_flags & IWL_UCODE_TLV_FLAGS_PAN)
  546. priv->valid_contexts |= BIT(IWL_RXON_CTX_PAN);
  547. for (i = 0; i < NUM_IWL_RXON_CTX; i++)
  548. priv->contexts[i].ctxid = i;
  549. priv->contexts[IWL_RXON_CTX_BSS].always_active = true;
  550. priv->contexts[IWL_RXON_CTX_BSS].is_active = true;
  551. priv->contexts[IWL_RXON_CTX_BSS].rxon_cmd = REPLY_RXON;
  552. priv->contexts[IWL_RXON_CTX_BSS].rxon_timing_cmd = REPLY_RXON_TIMING;
  553. priv->contexts[IWL_RXON_CTX_BSS].rxon_assoc_cmd = REPLY_RXON_ASSOC;
  554. priv->contexts[IWL_RXON_CTX_BSS].qos_cmd = REPLY_QOS_PARAM;
  555. priv->contexts[IWL_RXON_CTX_BSS].ap_sta_id = IWL_AP_ID;
  556. priv->contexts[IWL_RXON_CTX_BSS].wep_key_cmd = REPLY_WEPKEY;
  557. priv->contexts[IWL_RXON_CTX_BSS].ac_to_fifo = iwlagn_bss_ac_to_fifo;
  558. priv->contexts[IWL_RXON_CTX_BSS].ac_to_queue = iwlagn_bss_ac_to_queue;
  559. priv->contexts[IWL_RXON_CTX_BSS].exclusive_interface_modes =
  560. BIT(NL80211_IFTYPE_ADHOC);
  561. priv->contexts[IWL_RXON_CTX_BSS].interface_modes =
  562. BIT(NL80211_IFTYPE_STATION);
  563. priv->contexts[IWL_RXON_CTX_BSS].ap_devtype = RXON_DEV_TYPE_AP;
  564. priv->contexts[IWL_RXON_CTX_BSS].ibss_devtype = RXON_DEV_TYPE_IBSS;
  565. priv->contexts[IWL_RXON_CTX_BSS].station_devtype = RXON_DEV_TYPE_ESS;
  566. priv->contexts[IWL_RXON_CTX_BSS].unused_devtype = RXON_DEV_TYPE_ESS;
  567. priv->contexts[IWL_RXON_CTX_PAN].rxon_cmd = REPLY_WIPAN_RXON;
  568. priv->contexts[IWL_RXON_CTX_PAN].rxon_timing_cmd =
  569. REPLY_WIPAN_RXON_TIMING;
  570. priv->contexts[IWL_RXON_CTX_PAN].rxon_assoc_cmd =
  571. REPLY_WIPAN_RXON_ASSOC;
  572. priv->contexts[IWL_RXON_CTX_PAN].qos_cmd = REPLY_WIPAN_QOS_PARAM;
  573. priv->contexts[IWL_RXON_CTX_PAN].ap_sta_id = IWL_AP_ID_PAN;
  574. priv->contexts[IWL_RXON_CTX_PAN].wep_key_cmd = REPLY_WIPAN_WEPKEY;
  575. priv->contexts[IWL_RXON_CTX_PAN].bcast_sta_id = IWLAGN_PAN_BCAST_ID;
  576. priv->contexts[IWL_RXON_CTX_PAN].station_flags = STA_FLG_PAN_STATION;
  577. priv->contexts[IWL_RXON_CTX_PAN].ac_to_fifo = iwlagn_pan_ac_to_fifo;
  578. priv->contexts[IWL_RXON_CTX_PAN].ac_to_queue = iwlagn_pan_ac_to_queue;
  579. priv->contexts[IWL_RXON_CTX_PAN].mcast_queue = IWL_IPAN_MCAST_QUEUE;
  580. priv->contexts[IWL_RXON_CTX_PAN].interface_modes =
  581. BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP);
  582. if (ucode_flags & IWL_UCODE_TLV_FLAGS_P2P)
  583. priv->contexts[IWL_RXON_CTX_PAN].interface_modes |=
  584. BIT(NL80211_IFTYPE_P2P_CLIENT) |
  585. BIT(NL80211_IFTYPE_P2P_GO);
  586. priv->contexts[IWL_RXON_CTX_PAN].ap_devtype = RXON_DEV_TYPE_CP;
  587. priv->contexts[IWL_RXON_CTX_PAN].station_devtype = RXON_DEV_TYPE_2STA;
  588. priv->contexts[IWL_RXON_CTX_PAN].unused_devtype = RXON_DEV_TYPE_P2P;
  589. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  590. }
  591. struct iwlagn_ucode_capabilities {
  592. u32 max_probe_length;
  593. u32 standard_phy_calibration_size;
  594. u32 flags;
  595. };
  596. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context);
  597. static int iwl_mac_setup_register(struct iwl_priv *priv,
  598. struct iwlagn_ucode_capabilities *capa);
  599. #define UCODE_EXPERIMENTAL_INDEX 100
  600. #define UCODE_EXPERIMENTAL_TAG "exp"
  601. static int __must_check iwl_request_firmware(struct iwl_priv *priv, bool first)
  602. {
  603. const char *name_pre = priv->cfg->fw_name_pre;
  604. char tag[8];
  605. if (first) {
  606. #ifdef CONFIG_IWLWIFI_DEBUG_EXPERIMENTAL_UCODE
  607. priv->fw_index = UCODE_EXPERIMENTAL_INDEX;
  608. strcpy(tag, UCODE_EXPERIMENTAL_TAG);
  609. } else if (priv->fw_index == UCODE_EXPERIMENTAL_INDEX) {
  610. #endif
  611. priv->fw_index = priv->cfg->ucode_api_max;
  612. sprintf(tag, "%d", priv->fw_index);
  613. } else {
  614. priv->fw_index--;
  615. sprintf(tag, "%d", priv->fw_index);
  616. }
  617. if (priv->fw_index < priv->cfg->ucode_api_min) {
  618. IWL_ERR(priv, "no suitable firmware found!\n");
  619. return -ENOENT;
  620. }
  621. sprintf(priv->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  622. IWL_DEBUG_INFO(priv, "attempting to load firmware %s'%s'\n",
  623. (priv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  624. ? "EXPERIMENTAL " : "",
  625. priv->firmware_name);
  626. return request_firmware_nowait(THIS_MODULE, 1, priv->firmware_name,
  627. priv->bus->dev,
  628. GFP_KERNEL, priv, iwl_ucode_callback);
  629. }
  630. struct iwlagn_firmware_pieces {
  631. const void *inst, *data, *init, *init_data, *wowlan_inst, *wowlan_data;
  632. size_t inst_size, data_size, init_size, init_data_size,
  633. wowlan_inst_size, wowlan_data_size;
  634. u32 build;
  635. u32 init_evtlog_ptr, init_evtlog_size, init_errlog_ptr;
  636. u32 inst_evtlog_ptr, inst_evtlog_size, inst_errlog_ptr;
  637. };
  638. static int iwlagn_load_legacy_firmware(struct iwl_priv *priv,
  639. const struct firmware *ucode_raw,
  640. struct iwlagn_firmware_pieces *pieces)
  641. {
  642. struct iwl_ucode_header *ucode = (void *)ucode_raw->data;
  643. u32 api_ver, hdr_size;
  644. const u8 *src;
  645. priv->ucode_ver = le32_to_cpu(ucode->ver);
  646. api_ver = IWL_UCODE_API(priv->ucode_ver);
  647. switch (api_ver) {
  648. default:
  649. hdr_size = 28;
  650. if (ucode_raw->size < hdr_size) {
  651. IWL_ERR(priv, "File size too small!\n");
  652. return -EINVAL;
  653. }
  654. pieces->build = le32_to_cpu(ucode->u.v2.build);
  655. pieces->inst_size = le32_to_cpu(ucode->u.v2.inst_size);
  656. pieces->data_size = le32_to_cpu(ucode->u.v2.data_size);
  657. pieces->init_size = le32_to_cpu(ucode->u.v2.init_size);
  658. pieces->init_data_size = le32_to_cpu(ucode->u.v2.init_data_size);
  659. src = ucode->u.v2.data;
  660. break;
  661. case 0:
  662. case 1:
  663. case 2:
  664. hdr_size = 24;
  665. if (ucode_raw->size < hdr_size) {
  666. IWL_ERR(priv, "File size too small!\n");
  667. return -EINVAL;
  668. }
  669. pieces->build = 0;
  670. pieces->inst_size = le32_to_cpu(ucode->u.v1.inst_size);
  671. pieces->data_size = le32_to_cpu(ucode->u.v1.data_size);
  672. pieces->init_size = le32_to_cpu(ucode->u.v1.init_size);
  673. pieces->init_data_size = le32_to_cpu(ucode->u.v1.init_data_size);
  674. src = ucode->u.v1.data;
  675. break;
  676. }
  677. /* Verify size of file vs. image size info in file's header */
  678. if (ucode_raw->size != hdr_size + pieces->inst_size +
  679. pieces->data_size + pieces->init_size +
  680. pieces->init_data_size) {
  681. IWL_ERR(priv,
  682. "uCode file size %d does not match expected size\n",
  683. (int)ucode_raw->size);
  684. return -EINVAL;
  685. }
  686. pieces->inst = src;
  687. src += pieces->inst_size;
  688. pieces->data = src;
  689. src += pieces->data_size;
  690. pieces->init = src;
  691. src += pieces->init_size;
  692. pieces->init_data = src;
  693. src += pieces->init_data_size;
  694. return 0;
  695. }
  696. static int iwlagn_load_firmware(struct iwl_priv *priv,
  697. const struct firmware *ucode_raw,
  698. struct iwlagn_firmware_pieces *pieces,
  699. struct iwlagn_ucode_capabilities *capa)
  700. {
  701. struct iwl_tlv_ucode_header *ucode = (void *)ucode_raw->data;
  702. struct iwl_ucode_tlv *tlv;
  703. size_t len = ucode_raw->size;
  704. const u8 *data;
  705. int wanted_alternative = iwlagn_mod_params.wanted_ucode_alternative;
  706. int tmp;
  707. u64 alternatives;
  708. u32 tlv_len;
  709. enum iwl_ucode_tlv_type tlv_type;
  710. const u8 *tlv_data;
  711. if (len < sizeof(*ucode)) {
  712. IWL_ERR(priv, "uCode has invalid length: %zd\n", len);
  713. return -EINVAL;
  714. }
  715. if (ucode->magic != cpu_to_le32(IWL_TLV_UCODE_MAGIC)) {
  716. IWL_ERR(priv, "invalid uCode magic: 0X%x\n",
  717. le32_to_cpu(ucode->magic));
  718. return -EINVAL;
  719. }
  720. /*
  721. * Check which alternatives are present, and "downgrade"
  722. * when the chosen alternative is not present, warning
  723. * the user when that happens. Some files may not have
  724. * any alternatives, so don't warn in that case.
  725. */
  726. alternatives = le64_to_cpu(ucode->alternatives);
  727. tmp = wanted_alternative;
  728. if (wanted_alternative > 63)
  729. wanted_alternative = 63;
  730. while (wanted_alternative && !(alternatives & BIT(wanted_alternative)))
  731. wanted_alternative--;
  732. if (wanted_alternative && wanted_alternative != tmp)
  733. IWL_WARN(priv,
  734. "uCode alternative %d not available, choosing %d\n",
  735. tmp, wanted_alternative);
  736. priv->ucode_ver = le32_to_cpu(ucode->ver);
  737. pieces->build = le32_to_cpu(ucode->build);
  738. data = ucode->data;
  739. len -= sizeof(*ucode);
  740. while (len >= sizeof(*tlv)) {
  741. u16 tlv_alt;
  742. len -= sizeof(*tlv);
  743. tlv = (void *)data;
  744. tlv_len = le32_to_cpu(tlv->length);
  745. tlv_type = le16_to_cpu(tlv->type);
  746. tlv_alt = le16_to_cpu(tlv->alternative);
  747. tlv_data = tlv->data;
  748. if (len < tlv_len) {
  749. IWL_ERR(priv, "invalid TLV len: %zd/%u\n",
  750. len, tlv_len);
  751. return -EINVAL;
  752. }
  753. len -= ALIGN(tlv_len, 4);
  754. data += sizeof(*tlv) + ALIGN(tlv_len, 4);
  755. /*
  756. * Alternative 0 is always valid.
  757. *
  758. * Skip alternative TLVs that are not selected.
  759. */
  760. if (tlv_alt != 0 && tlv_alt != wanted_alternative)
  761. continue;
  762. switch (tlv_type) {
  763. case IWL_UCODE_TLV_INST:
  764. pieces->inst = tlv_data;
  765. pieces->inst_size = tlv_len;
  766. break;
  767. case IWL_UCODE_TLV_DATA:
  768. pieces->data = tlv_data;
  769. pieces->data_size = tlv_len;
  770. break;
  771. case IWL_UCODE_TLV_INIT:
  772. pieces->init = tlv_data;
  773. pieces->init_size = tlv_len;
  774. break;
  775. case IWL_UCODE_TLV_INIT_DATA:
  776. pieces->init_data = tlv_data;
  777. pieces->init_data_size = tlv_len;
  778. break;
  779. case IWL_UCODE_TLV_BOOT:
  780. IWL_ERR(priv, "Found unexpected BOOT ucode\n");
  781. break;
  782. case IWL_UCODE_TLV_PROBE_MAX_LEN:
  783. if (tlv_len != sizeof(u32))
  784. goto invalid_tlv_len;
  785. capa->max_probe_length =
  786. le32_to_cpup((__le32 *)tlv_data);
  787. break;
  788. case IWL_UCODE_TLV_PAN:
  789. if (tlv_len)
  790. goto invalid_tlv_len;
  791. capa->flags |= IWL_UCODE_TLV_FLAGS_PAN;
  792. break;
  793. case IWL_UCODE_TLV_FLAGS:
  794. /* must be at least one u32 */
  795. if (tlv_len < sizeof(u32))
  796. goto invalid_tlv_len;
  797. /* and a proper number of u32s */
  798. if (tlv_len % sizeof(u32))
  799. goto invalid_tlv_len;
  800. /*
  801. * This driver only reads the first u32 as
  802. * right now no more features are defined,
  803. * if that changes then either the driver
  804. * will not work with the new firmware, or
  805. * it'll not take advantage of new features.
  806. */
  807. capa->flags = le32_to_cpup((__le32 *)tlv_data);
  808. break;
  809. case IWL_UCODE_TLV_INIT_EVTLOG_PTR:
  810. if (tlv_len != sizeof(u32))
  811. goto invalid_tlv_len;
  812. pieces->init_evtlog_ptr =
  813. le32_to_cpup((__le32 *)tlv_data);
  814. break;
  815. case IWL_UCODE_TLV_INIT_EVTLOG_SIZE:
  816. if (tlv_len != sizeof(u32))
  817. goto invalid_tlv_len;
  818. pieces->init_evtlog_size =
  819. le32_to_cpup((__le32 *)tlv_data);
  820. break;
  821. case IWL_UCODE_TLV_INIT_ERRLOG_PTR:
  822. if (tlv_len != sizeof(u32))
  823. goto invalid_tlv_len;
  824. pieces->init_errlog_ptr =
  825. le32_to_cpup((__le32 *)tlv_data);
  826. break;
  827. case IWL_UCODE_TLV_RUNT_EVTLOG_PTR:
  828. if (tlv_len != sizeof(u32))
  829. goto invalid_tlv_len;
  830. pieces->inst_evtlog_ptr =
  831. le32_to_cpup((__le32 *)tlv_data);
  832. break;
  833. case IWL_UCODE_TLV_RUNT_EVTLOG_SIZE:
  834. if (tlv_len != sizeof(u32))
  835. goto invalid_tlv_len;
  836. pieces->inst_evtlog_size =
  837. le32_to_cpup((__le32 *)tlv_data);
  838. break;
  839. case IWL_UCODE_TLV_RUNT_ERRLOG_PTR:
  840. if (tlv_len != sizeof(u32))
  841. goto invalid_tlv_len;
  842. pieces->inst_errlog_ptr =
  843. le32_to_cpup((__le32 *)tlv_data);
  844. break;
  845. case IWL_UCODE_TLV_ENHANCE_SENS_TBL:
  846. if (tlv_len)
  847. goto invalid_tlv_len;
  848. priv->enhance_sensitivity_table = true;
  849. break;
  850. case IWL_UCODE_TLV_WOWLAN_INST:
  851. pieces->wowlan_inst = tlv_data;
  852. pieces->wowlan_inst_size = tlv_len;
  853. break;
  854. case IWL_UCODE_TLV_WOWLAN_DATA:
  855. pieces->wowlan_data = tlv_data;
  856. pieces->wowlan_data_size = tlv_len;
  857. break;
  858. case IWL_UCODE_TLV_PHY_CALIBRATION_SIZE:
  859. if (tlv_len != sizeof(u32))
  860. goto invalid_tlv_len;
  861. capa->standard_phy_calibration_size =
  862. le32_to_cpup((__le32 *)tlv_data);
  863. break;
  864. default:
  865. IWL_DEBUG_INFO(priv, "unknown TLV: %d\n", tlv_type);
  866. break;
  867. }
  868. }
  869. if (len) {
  870. IWL_ERR(priv, "invalid TLV after parsing: %zd\n", len);
  871. iwl_print_hex_dump(priv, IWL_DL_FW, (u8 *)data, len);
  872. return -EINVAL;
  873. }
  874. return 0;
  875. invalid_tlv_len:
  876. IWL_ERR(priv, "TLV %d has invalid size: %u\n", tlv_type, tlv_len);
  877. iwl_print_hex_dump(priv, IWL_DL_FW, tlv_data, tlv_len);
  878. return -EINVAL;
  879. }
  880. /**
  881. * iwl_ucode_callback - callback when firmware was loaded
  882. *
  883. * If loaded successfully, copies the firmware into buffers
  884. * for the card to fetch (via DMA).
  885. */
  886. static void iwl_ucode_callback(const struct firmware *ucode_raw, void *context)
  887. {
  888. struct iwl_priv *priv = context;
  889. struct iwl_ucode_header *ucode;
  890. int err;
  891. struct iwlagn_firmware_pieces pieces;
  892. const unsigned int api_max = priv->cfg->ucode_api_max;
  893. unsigned int api_ok = priv->cfg->ucode_api_ok;
  894. const unsigned int api_min = priv->cfg->ucode_api_min;
  895. u32 api_ver;
  896. char buildstr[25];
  897. u32 build;
  898. struct iwlagn_ucode_capabilities ucode_capa = {
  899. .max_probe_length = 200,
  900. .standard_phy_calibration_size =
  901. IWL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE,
  902. };
  903. if (!api_ok)
  904. api_ok = api_max;
  905. memset(&pieces, 0, sizeof(pieces));
  906. if (!ucode_raw) {
  907. if (priv->fw_index <= api_ok)
  908. IWL_ERR(priv,
  909. "request for firmware file '%s' failed.\n",
  910. priv->firmware_name);
  911. goto try_again;
  912. }
  913. IWL_DEBUG_INFO(priv, "Loaded firmware file '%s' (%zd bytes).\n",
  914. priv->firmware_name, ucode_raw->size);
  915. /* Make sure that we got at least the API version number */
  916. if (ucode_raw->size < 4) {
  917. IWL_ERR(priv, "File size way too small!\n");
  918. goto try_again;
  919. }
  920. /* Data from ucode file: header followed by uCode images */
  921. ucode = (struct iwl_ucode_header *)ucode_raw->data;
  922. if (ucode->ver)
  923. err = iwlagn_load_legacy_firmware(priv, ucode_raw, &pieces);
  924. else
  925. err = iwlagn_load_firmware(priv, ucode_raw, &pieces,
  926. &ucode_capa);
  927. if (err)
  928. goto try_again;
  929. api_ver = IWL_UCODE_API(priv->ucode_ver);
  930. build = pieces.build;
  931. /*
  932. * api_ver should match the api version forming part of the
  933. * firmware filename ... but we don't check for that and only rely
  934. * on the API version read from firmware header from here on forward
  935. */
  936. /* no api version check required for experimental uCode */
  937. if (priv->fw_index != UCODE_EXPERIMENTAL_INDEX) {
  938. if (api_ver < api_min || api_ver > api_max) {
  939. IWL_ERR(priv,
  940. "Driver unable to support your firmware API. "
  941. "Driver supports v%u, firmware is v%u.\n",
  942. api_max, api_ver);
  943. goto try_again;
  944. }
  945. if (api_ver < api_ok) {
  946. if (api_ok != api_max)
  947. IWL_ERR(priv, "Firmware has old API version, "
  948. "expected v%u through v%u, got v%u.\n",
  949. api_ok, api_max, api_ver);
  950. else
  951. IWL_ERR(priv, "Firmware has old API version, "
  952. "expected v%u, got v%u.\n",
  953. api_max, api_ver);
  954. IWL_ERR(priv, "New firmware can be obtained from "
  955. "http://www.intellinuxwireless.org/.\n");
  956. }
  957. }
  958. if (build)
  959. sprintf(buildstr, " build %u%s", build,
  960. (priv->fw_index == UCODE_EXPERIMENTAL_INDEX)
  961. ? " (EXP)" : "");
  962. else
  963. buildstr[0] = '\0';
  964. IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u%s\n",
  965. IWL_UCODE_MAJOR(priv->ucode_ver),
  966. IWL_UCODE_MINOR(priv->ucode_ver),
  967. IWL_UCODE_API(priv->ucode_ver),
  968. IWL_UCODE_SERIAL(priv->ucode_ver),
  969. buildstr);
  970. snprintf(priv->hw->wiphy->fw_version,
  971. sizeof(priv->hw->wiphy->fw_version),
  972. "%u.%u.%u.%u%s",
  973. IWL_UCODE_MAJOR(priv->ucode_ver),
  974. IWL_UCODE_MINOR(priv->ucode_ver),
  975. IWL_UCODE_API(priv->ucode_ver),
  976. IWL_UCODE_SERIAL(priv->ucode_ver),
  977. buildstr);
  978. /*
  979. * For any of the failures below (before allocating pci memory)
  980. * we will try to load a version with a smaller API -- maybe the
  981. * user just got a corrupted version of the latest API.
  982. */
  983. IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
  984. priv->ucode_ver);
  985. IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %Zd\n",
  986. pieces.inst_size);
  987. IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %Zd\n",
  988. pieces.data_size);
  989. IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %Zd\n",
  990. pieces.init_size);
  991. IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %Zd\n",
  992. pieces.init_data_size);
  993. /* Verify that uCode images will fit in card's SRAM */
  994. if (pieces.inst_size > hw_params(priv).max_inst_size) {
  995. IWL_ERR(priv, "uCode instr len %Zd too large to fit in\n",
  996. pieces.inst_size);
  997. goto try_again;
  998. }
  999. if (pieces.data_size > hw_params(priv).max_data_size) {
  1000. IWL_ERR(priv, "uCode data len %Zd too large to fit in\n",
  1001. pieces.data_size);
  1002. goto try_again;
  1003. }
  1004. if (pieces.init_size > hw_params(priv).max_inst_size) {
  1005. IWL_ERR(priv, "uCode init instr len %Zd too large to fit in\n",
  1006. pieces.init_size);
  1007. goto try_again;
  1008. }
  1009. if (pieces.init_data_size > hw_params(priv).max_data_size) {
  1010. IWL_ERR(priv, "uCode init data len %Zd too large to fit in\n",
  1011. pieces.init_data_size);
  1012. goto try_again;
  1013. }
  1014. /* Allocate ucode buffers for card's bus-master loading ... */
  1015. /* Runtime instructions and 2 copies of data:
  1016. * 1) unmodified from disk
  1017. * 2) backup cache for save/restore during power-downs */
  1018. if (iwl_alloc_fw_desc(priv, &priv->ucode_rt.code,
  1019. pieces.inst, pieces.inst_size))
  1020. goto err_pci_alloc;
  1021. if (iwl_alloc_fw_desc(priv, &priv->ucode_rt.data,
  1022. pieces.data, pieces.data_size))
  1023. goto err_pci_alloc;
  1024. /* Initialization instructions and data */
  1025. if (pieces.init_size && pieces.init_data_size) {
  1026. if (iwl_alloc_fw_desc(priv, &priv->ucode_init.code,
  1027. pieces.init, pieces.init_size))
  1028. goto err_pci_alloc;
  1029. if (iwl_alloc_fw_desc(priv, &priv->ucode_init.data,
  1030. pieces.init_data, pieces.init_data_size))
  1031. goto err_pci_alloc;
  1032. }
  1033. /* WoWLAN instructions and data */
  1034. if (pieces.wowlan_inst_size && pieces.wowlan_data_size) {
  1035. if (iwl_alloc_fw_desc(priv, &priv->ucode_wowlan.code,
  1036. pieces.wowlan_inst,
  1037. pieces.wowlan_inst_size))
  1038. goto err_pci_alloc;
  1039. if (iwl_alloc_fw_desc(priv, &priv->ucode_wowlan.data,
  1040. pieces.wowlan_data,
  1041. pieces.wowlan_data_size))
  1042. goto err_pci_alloc;
  1043. }
  1044. /* Now that we can no longer fail, copy information */
  1045. /*
  1046. * The (size - 16) / 12 formula is based on the information recorded
  1047. * for each event, which is of mode 1 (including timestamp) for all
  1048. * new microcodes that include this information.
  1049. */
  1050. priv->init_evtlog_ptr = pieces.init_evtlog_ptr;
  1051. if (pieces.init_evtlog_size)
  1052. priv->init_evtlog_size = (pieces.init_evtlog_size - 16)/12;
  1053. else
  1054. priv->init_evtlog_size =
  1055. priv->cfg->base_params->max_event_log_size;
  1056. priv->init_errlog_ptr = pieces.init_errlog_ptr;
  1057. priv->inst_evtlog_ptr = pieces.inst_evtlog_ptr;
  1058. if (pieces.inst_evtlog_size)
  1059. priv->inst_evtlog_size = (pieces.inst_evtlog_size - 16)/12;
  1060. else
  1061. priv->inst_evtlog_size =
  1062. priv->cfg->base_params->max_event_log_size;
  1063. priv->inst_errlog_ptr = pieces.inst_errlog_ptr;
  1064. priv->new_scan_threshold_behaviour =
  1065. !!(ucode_capa.flags & IWL_UCODE_TLV_FLAGS_NEWSCAN);
  1066. if (!(priv->cfg->sku & EEPROM_SKU_CAP_IPAN_ENABLE))
  1067. ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_PAN;
  1068. /*
  1069. * if not PAN, then don't support P2P -- might be a uCode
  1070. * packaging bug or due to the eeprom check above
  1071. */
  1072. if (!(ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN))
  1073. ucode_capa.flags &= ~IWL_UCODE_TLV_FLAGS_P2P;
  1074. if (ucode_capa.flags & IWL_UCODE_TLV_FLAGS_PAN) {
  1075. priv->sta_key_max_num = STA_KEY_MAX_NUM_PAN;
  1076. priv->shrd->cmd_queue = IWL_IPAN_CMD_QUEUE_NUM;
  1077. } else {
  1078. priv->sta_key_max_num = STA_KEY_MAX_NUM;
  1079. priv->shrd->cmd_queue = IWL_DEFAULT_CMD_QUEUE_NUM;
  1080. }
  1081. /*
  1082. * figure out the offset of chain noise reset and gain commands
  1083. * base on the size of standard phy calibration commands table size
  1084. */
  1085. if (ucode_capa.standard_phy_calibration_size >
  1086. IWL_MAX_PHY_CALIBRATE_TBL_SIZE)
  1087. ucode_capa.standard_phy_calibration_size =
  1088. IWL_MAX_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  1089. priv->phy_calib_chain_noise_reset_cmd =
  1090. ucode_capa.standard_phy_calibration_size;
  1091. priv->phy_calib_chain_noise_gain_cmd =
  1092. ucode_capa.standard_phy_calibration_size + 1;
  1093. /* initialize all valid contexts */
  1094. iwl_init_context(priv, ucode_capa.flags);
  1095. /**************************************************
  1096. * This is still part of probe() in a sense...
  1097. *
  1098. * 9. Setup and register with mac80211 and debugfs
  1099. **************************************************/
  1100. err = iwl_mac_setup_register(priv, &ucode_capa);
  1101. if (err)
  1102. goto out_unbind;
  1103. err = iwl_dbgfs_register(priv, DRV_NAME);
  1104. if (err)
  1105. IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
  1106. err = sysfs_create_group(&(priv->bus->dev->kobj),
  1107. &iwl_attribute_group);
  1108. if (err) {
  1109. IWL_ERR(priv, "failed to create sysfs device attributes\n");
  1110. goto out_unbind;
  1111. }
  1112. /* We have our copies now, allow OS release its copies */
  1113. release_firmware(ucode_raw);
  1114. complete(&priv->firmware_loading_complete);
  1115. return;
  1116. try_again:
  1117. /* try next, if any */
  1118. if (iwl_request_firmware(priv, false))
  1119. goto out_unbind;
  1120. release_firmware(ucode_raw);
  1121. return;
  1122. err_pci_alloc:
  1123. IWL_ERR(priv, "failed to allocate pci memory\n");
  1124. iwl_dealloc_ucode(priv);
  1125. out_unbind:
  1126. complete(&priv->firmware_loading_complete);
  1127. device_release_driver(priv->bus->dev);
  1128. release_firmware(ucode_raw);
  1129. }
  1130. static void iwl_rf_kill_ct_config(struct iwl_priv *priv)
  1131. {
  1132. struct iwl_ct_kill_config cmd;
  1133. struct iwl_ct_kill_throttling_config adv_cmd;
  1134. unsigned long flags;
  1135. int ret = 0;
  1136. spin_lock_irqsave(&priv->shrd->lock, flags);
  1137. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  1138. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  1139. spin_unlock_irqrestore(&priv->shrd->lock, flags);
  1140. priv->thermal_throttle.ct_kill_toggle = false;
  1141. if (priv->cfg->base_params->support_ct_kill_exit) {
  1142. adv_cmd.critical_temperature_enter =
  1143. cpu_to_le32(hw_params(priv).ct_kill_threshold);
  1144. adv_cmd.critical_temperature_exit =
  1145. cpu_to_le32(hw_params(priv).ct_kill_exit_threshold);
  1146. ret = iwl_trans_send_cmd_pdu(trans(priv),
  1147. REPLY_CT_KILL_CONFIG_CMD,
  1148. CMD_SYNC, sizeof(adv_cmd), &adv_cmd);
  1149. if (ret)
  1150. IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
  1151. else
  1152. IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
  1153. "succeeded, critical temperature enter is %d,"
  1154. "exit is %d\n",
  1155. hw_params(priv).ct_kill_threshold,
  1156. hw_params(priv).ct_kill_exit_threshold);
  1157. } else {
  1158. cmd.critical_temperature_R =
  1159. cpu_to_le32(hw_params(priv).ct_kill_threshold);
  1160. ret = iwl_trans_send_cmd_pdu(trans(priv),
  1161. REPLY_CT_KILL_CONFIG_CMD,
  1162. CMD_SYNC, sizeof(cmd), &cmd);
  1163. if (ret)
  1164. IWL_ERR(priv, "REPLY_CT_KILL_CONFIG_CMD failed\n");
  1165. else
  1166. IWL_DEBUG_INFO(priv, "REPLY_CT_KILL_CONFIG_CMD "
  1167. "succeeded, "
  1168. "critical temperature is %d\n",
  1169. hw_params(priv).ct_kill_threshold);
  1170. }
  1171. }
  1172. static int iwlagn_send_calib_cfg_rt(struct iwl_priv *priv, u32 cfg)
  1173. {
  1174. struct iwl_calib_cfg_cmd calib_cfg_cmd;
  1175. struct iwl_host_cmd cmd = {
  1176. .id = CALIBRATION_CFG_CMD,
  1177. .len = { sizeof(struct iwl_calib_cfg_cmd), },
  1178. .data = { &calib_cfg_cmd, },
  1179. };
  1180. memset(&calib_cfg_cmd, 0, sizeof(calib_cfg_cmd));
  1181. calib_cfg_cmd.ucd_calib_cfg.once.is_enable = IWL_CALIB_INIT_CFG_ALL;
  1182. calib_cfg_cmd.ucd_calib_cfg.once.start = cpu_to_le32(cfg);
  1183. return iwl_trans_send_cmd(trans(priv), &cmd);
  1184. }
  1185. static int iwlagn_send_tx_ant_config(struct iwl_priv *priv, u8 valid_tx_ant)
  1186. {
  1187. struct iwl_tx_ant_config_cmd tx_ant_cmd = {
  1188. .valid = cpu_to_le32(valid_tx_ant),
  1189. };
  1190. if (IWL_UCODE_API(priv->ucode_ver) > 1) {
  1191. IWL_DEBUG_HC(priv, "select valid tx ant: %u\n", valid_tx_ant);
  1192. return iwl_trans_send_cmd_pdu(trans(priv),
  1193. TX_ANT_CONFIGURATION_CMD,
  1194. CMD_SYNC,
  1195. sizeof(struct iwl_tx_ant_config_cmd),
  1196. &tx_ant_cmd);
  1197. } else {
  1198. IWL_DEBUG_HC(priv, "TX_ANT_CONFIGURATION_CMD not supported\n");
  1199. return -EOPNOTSUPP;
  1200. }
  1201. }
  1202. /**
  1203. * iwl_alive_start - called after REPLY_ALIVE notification received
  1204. * from protocol/runtime uCode (initialization uCode's
  1205. * Alive gets handled by iwl_init_alive_start()).
  1206. */
  1207. int iwl_alive_start(struct iwl_priv *priv)
  1208. {
  1209. int ret = 0;
  1210. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1211. /*TODO: this should go to the transport layer */
  1212. iwl_reset_ict(trans(priv));
  1213. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  1214. /* After the ALIVE response, we can send host commands to the uCode */
  1215. set_bit(STATUS_ALIVE, &priv->shrd->status);
  1216. /* Enable watchdog to monitor the driver tx queues */
  1217. iwl_setup_watchdog(priv);
  1218. if (iwl_is_rfkill(priv->shrd))
  1219. return -ERFKILL;
  1220. /* download priority table before any calibration request */
  1221. if (priv->cfg->bt_params &&
  1222. priv->cfg->bt_params->advanced_bt_coexist) {
  1223. /* Configure Bluetooth device coexistence support */
  1224. if (priv->cfg->bt_params->bt_sco_disable)
  1225. priv->bt_enable_pspoll = false;
  1226. else
  1227. priv->bt_enable_pspoll = true;
  1228. priv->bt_valid = IWLAGN_BT_ALL_VALID_MSK;
  1229. priv->kill_ack_mask = IWLAGN_BT_KILL_ACK_MASK_DEFAULT;
  1230. priv->kill_cts_mask = IWLAGN_BT_KILL_CTS_MASK_DEFAULT;
  1231. iwlagn_send_advance_bt_config(priv);
  1232. priv->bt_valid = IWLAGN_BT_VALID_ENABLE_FLAGS;
  1233. priv->cur_rssi_ctx = NULL;
  1234. iwlagn_send_prio_tbl(priv);
  1235. /* FIXME: w/a to force change uCode BT state machine */
  1236. ret = iwlagn_send_bt_env(priv, IWL_BT_COEX_ENV_OPEN,
  1237. BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
  1238. if (ret)
  1239. return ret;
  1240. ret = iwlagn_send_bt_env(priv, IWL_BT_COEX_ENV_CLOSE,
  1241. BT_COEX_PRIO_TBL_EVT_INIT_CALIB2);
  1242. if (ret)
  1243. return ret;
  1244. } else {
  1245. /*
  1246. * default is 2-wire BT coexexistence support
  1247. */
  1248. iwl_send_bt_config(priv);
  1249. }
  1250. if (hw_params(priv).calib_rt_cfg)
  1251. iwlagn_send_calib_cfg_rt(priv,
  1252. hw_params(priv).calib_rt_cfg);
  1253. ieee80211_wake_queues(priv->hw);
  1254. priv->active_rate = IWL_RATES_MASK;
  1255. /* Configure Tx antenna selection based on H/W config */
  1256. iwlagn_send_tx_ant_config(priv, priv->cfg->valid_tx_ant);
  1257. if (iwl_is_associated_ctx(ctx) && !priv->shrd->wowlan) {
  1258. struct iwl_rxon_cmd *active_rxon =
  1259. (struct iwl_rxon_cmd *)&ctx->active;
  1260. /* apply any changes in staging */
  1261. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  1262. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  1263. } else {
  1264. struct iwl_rxon_context *tmp;
  1265. /* Initialize our rx_config data */
  1266. for_each_context(priv, tmp)
  1267. iwl_connection_init_rx_config(priv, tmp);
  1268. iwlagn_set_rxon_chain(priv, ctx);
  1269. }
  1270. if (!priv->shrd->wowlan) {
  1271. /* WoWLAN ucode will not reply in the same way, skip it */
  1272. iwl_reset_run_time_calib(priv);
  1273. }
  1274. set_bit(STATUS_READY, &priv->shrd->status);
  1275. /* Configure the adapter for unassociated operation */
  1276. ret = iwlagn_commit_rxon(priv, ctx);
  1277. if (ret)
  1278. return ret;
  1279. /* At this point, the NIC is initialized and operational */
  1280. iwl_rf_kill_ct_config(priv);
  1281. IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
  1282. return iwl_power_update_mode(priv, true);
  1283. }
  1284. static void iwl_cancel_deferred_work(struct iwl_priv *priv);
  1285. static void __iwl_down(struct iwl_priv *priv)
  1286. {
  1287. int exit_pending;
  1288. IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
  1289. iwl_scan_cancel_timeout(priv, 200);
  1290. /*
  1291. * If active, scanning won't cancel it, so say it expired.
  1292. * No race since we hold the mutex here and a new one
  1293. * can't come in at this time.
  1294. */
  1295. ieee80211_remain_on_channel_expired(priv->hw);
  1296. exit_pending =
  1297. test_and_set_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  1298. /* Stop TX queues watchdog. We need to have STATUS_EXIT_PENDING bit set
  1299. * to prevent rearm timer */
  1300. del_timer_sync(&priv->watchdog);
  1301. iwl_clear_ucode_stations(priv, NULL);
  1302. iwl_dealloc_bcast_stations(priv);
  1303. iwl_clear_driver_stations(priv);
  1304. /* reset BT coex data */
  1305. priv->bt_status = 0;
  1306. priv->cur_rssi_ctx = NULL;
  1307. priv->bt_is_sco = 0;
  1308. if (priv->cfg->bt_params)
  1309. priv->bt_traffic_load =
  1310. priv->cfg->bt_params->bt_init_traffic_load;
  1311. else
  1312. priv->bt_traffic_load = 0;
  1313. priv->bt_full_concurrent = false;
  1314. priv->bt_ci_compliance = 0;
  1315. /* Wipe out the EXIT_PENDING status bit if we are not actually
  1316. * exiting the module */
  1317. if (!exit_pending)
  1318. clear_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  1319. if (priv->mac80211_registered)
  1320. ieee80211_stop_queues(priv->hw);
  1321. /* Clear out all status bits but a few that are stable across reset */
  1322. priv->shrd->status &=
  1323. test_bit(STATUS_RF_KILL_HW, &priv->shrd->status) <<
  1324. STATUS_RF_KILL_HW |
  1325. test_bit(STATUS_GEO_CONFIGURED, &priv->shrd->status) <<
  1326. STATUS_GEO_CONFIGURED |
  1327. test_bit(STATUS_FW_ERROR, &priv->shrd->status) <<
  1328. STATUS_FW_ERROR |
  1329. test_bit(STATUS_EXIT_PENDING, &priv->shrd->status) <<
  1330. STATUS_EXIT_PENDING;
  1331. iwl_trans_stop_device(trans(priv));
  1332. dev_kfree_skb(priv->beacon_skb);
  1333. priv->beacon_skb = NULL;
  1334. }
  1335. static void iwl_down(struct iwl_priv *priv)
  1336. {
  1337. mutex_lock(&priv->shrd->mutex);
  1338. __iwl_down(priv);
  1339. mutex_unlock(&priv->shrd->mutex);
  1340. iwl_cancel_deferred_work(priv);
  1341. }
  1342. #define MAX_HW_RESTARTS 5
  1343. static int __iwl_up(struct iwl_priv *priv)
  1344. {
  1345. struct iwl_rxon_context *ctx;
  1346. int ret;
  1347. lockdep_assert_held(&priv->shrd->mutex);
  1348. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
  1349. IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
  1350. return -EIO;
  1351. }
  1352. for_each_context(priv, ctx) {
  1353. ret = iwlagn_alloc_bcast_station(priv, ctx);
  1354. if (ret) {
  1355. iwl_dealloc_bcast_stations(priv);
  1356. return ret;
  1357. }
  1358. }
  1359. ret = iwlagn_run_init_ucode(priv);
  1360. if (ret) {
  1361. IWL_ERR(priv, "Failed to run INIT ucode: %d\n", ret);
  1362. goto error;
  1363. }
  1364. ret = iwlagn_load_ucode_wait_alive(priv,
  1365. &priv->ucode_rt,
  1366. IWL_UCODE_REGULAR);
  1367. if (ret) {
  1368. IWL_ERR(priv, "Failed to start RT ucode: %d\n", ret);
  1369. goto error;
  1370. }
  1371. ret = iwl_alive_start(priv);
  1372. if (ret)
  1373. goto error;
  1374. return 0;
  1375. error:
  1376. set_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  1377. __iwl_down(priv);
  1378. clear_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  1379. IWL_ERR(priv, "Unable to initialize device.\n");
  1380. return ret;
  1381. }
  1382. /*****************************************************************************
  1383. *
  1384. * Workqueue callbacks
  1385. *
  1386. *****************************************************************************/
  1387. static void iwl_bg_run_time_calib_work(struct work_struct *work)
  1388. {
  1389. struct iwl_priv *priv = container_of(work, struct iwl_priv,
  1390. run_time_calib_work);
  1391. mutex_lock(&priv->shrd->mutex);
  1392. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status) ||
  1393. test_bit(STATUS_SCANNING, &priv->shrd->status)) {
  1394. mutex_unlock(&priv->shrd->mutex);
  1395. return;
  1396. }
  1397. if (priv->start_calib) {
  1398. iwl_chain_noise_calibration(priv);
  1399. iwl_sensitivity_calibration(priv);
  1400. }
  1401. mutex_unlock(&priv->shrd->mutex);
  1402. }
  1403. static void iwlagn_prepare_restart(struct iwl_priv *priv)
  1404. {
  1405. struct iwl_rxon_context *ctx;
  1406. bool bt_full_concurrent;
  1407. u8 bt_ci_compliance;
  1408. u8 bt_load;
  1409. u8 bt_status;
  1410. bool bt_is_sco;
  1411. lockdep_assert_held(&priv->shrd->mutex);
  1412. for_each_context(priv, ctx)
  1413. ctx->vif = NULL;
  1414. priv->is_open = 0;
  1415. /*
  1416. * __iwl_down() will clear the BT status variables,
  1417. * which is correct, but when we restart we really
  1418. * want to keep them so restore them afterwards.
  1419. *
  1420. * The restart process will later pick them up and
  1421. * re-configure the hw when we reconfigure the BT
  1422. * command.
  1423. */
  1424. bt_full_concurrent = priv->bt_full_concurrent;
  1425. bt_ci_compliance = priv->bt_ci_compliance;
  1426. bt_load = priv->bt_traffic_load;
  1427. bt_status = priv->bt_status;
  1428. bt_is_sco = priv->bt_is_sco;
  1429. __iwl_down(priv);
  1430. priv->bt_full_concurrent = bt_full_concurrent;
  1431. priv->bt_ci_compliance = bt_ci_compliance;
  1432. priv->bt_traffic_load = bt_load;
  1433. priv->bt_status = bt_status;
  1434. priv->bt_is_sco = bt_is_sco;
  1435. }
  1436. static void iwl_bg_restart(struct work_struct *data)
  1437. {
  1438. struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
  1439. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  1440. return;
  1441. if (test_and_clear_bit(STATUS_FW_ERROR, &priv->shrd->status)) {
  1442. mutex_lock(&priv->shrd->mutex);
  1443. iwlagn_prepare_restart(priv);
  1444. mutex_unlock(&priv->shrd->mutex);
  1445. iwl_cancel_deferred_work(priv);
  1446. ieee80211_restart_hw(priv->hw);
  1447. } else {
  1448. WARN_ON(1);
  1449. }
  1450. }
  1451. /*****************************************************************************
  1452. *
  1453. * mac80211 entry point functions
  1454. *
  1455. *****************************************************************************/
  1456. static const struct ieee80211_iface_limit iwlagn_sta_ap_limits[] = {
  1457. {
  1458. .max = 1,
  1459. .types = BIT(NL80211_IFTYPE_STATION),
  1460. },
  1461. {
  1462. .max = 1,
  1463. .types = BIT(NL80211_IFTYPE_AP),
  1464. },
  1465. };
  1466. static const struct ieee80211_iface_limit iwlagn_2sta_limits[] = {
  1467. {
  1468. .max = 2,
  1469. .types = BIT(NL80211_IFTYPE_STATION),
  1470. },
  1471. };
  1472. static const struct ieee80211_iface_limit iwlagn_p2p_sta_go_limits[] = {
  1473. {
  1474. .max = 1,
  1475. .types = BIT(NL80211_IFTYPE_STATION),
  1476. },
  1477. {
  1478. .max = 1,
  1479. .types = BIT(NL80211_IFTYPE_P2P_GO) |
  1480. BIT(NL80211_IFTYPE_AP),
  1481. },
  1482. };
  1483. static const struct ieee80211_iface_limit iwlagn_p2p_2sta_limits[] = {
  1484. {
  1485. .max = 2,
  1486. .types = BIT(NL80211_IFTYPE_STATION),
  1487. },
  1488. {
  1489. .max = 1,
  1490. .types = BIT(NL80211_IFTYPE_P2P_CLIENT),
  1491. },
  1492. };
  1493. static const struct ieee80211_iface_combination
  1494. iwlagn_iface_combinations_dualmode[] = {
  1495. { .num_different_channels = 1,
  1496. .max_interfaces = 2,
  1497. .beacon_int_infra_match = true,
  1498. .limits = iwlagn_sta_ap_limits,
  1499. .n_limits = ARRAY_SIZE(iwlagn_sta_ap_limits),
  1500. },
  1501. { .num_different_channels = 1,
  1502. .max_interfaces = 2,
  1503. .limits = iwlagn_2sta_limits,
  1504. .n_limits = ARRAY_SIZE(iwlagn_2sta_limits),
  1505. },
  1506. };
  1507. static const struct ieee80211_iface_combination
  1508. iwlagn_iface_combinations_p2p[] = {
  1509. { .num_different_channels = 1,
  1510. .max_interfaces = 2,
  1511. .beacon_int_infra_match = true,
  1512. .limits = iwlagn_p2p_sta_go_limits,
  1513. .n_limits = ARRAY_SIZE(iwlagn_p2p_sta_go_limits),
  1514. },
  1515. { .num_different_channels = 1,
  1516. .max_interfaces = 2,
  1517. .limits = iwlagn_p2p_2sta_limits,
  1518. .n_limits = ARRAY_SIZE(iwlagn_p2p_2sta_limits),
  1519. },
  1520. };
  1521. /*
  1522. * Not a mac80211 entry point function, but it fits in with all the
  1523. * other mac80211 functions grouped here.
  1524. */
  1525. static int iwl_mac_setup_register(struct iwl_priv *priv,
  1526. struct iwlagn_ucode_capabilities *capa)
  1527. {
  1528. int ret;
  1529. struct ieee80211_hw *hw = priv->hw;
  1530. struct iwl_rxon_context *ctx;
  1531. hw->rate_control_algorithm = "iwl-agn-rs";
  1532. /* Tell mac80211 our characteristics */
  1533. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  1534. IEEE80211_HW_AMPDU_AGGREGATION |
  1535. IEEE80211_HW_NEED_DTIM_PERIOD |
  1536. IEEE80211_HW_SPECTRUM_MGMT |
  1537. IEEE80211_HW_REPORTS_TX_ACK_STATUS;
  1538. hw->max_tx_aggregation_subframes = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  1539. hw->flags |= IEEE80211_HW_SUPPORTS_PS |
  1540. IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
  1541. if (priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE)
  1542. hw->flags |= IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  1543. IEEE80211_HW_SUPPORTS_STATIC_SMPS;
  1544. if (capa->flags & IWL_UCODE_TLV_FLAGS_MFP)
  1545. hw->flags |= IEEE80211_HW_MFP_CAPABLE;
  1546. hw->sta_data_size = sizeof(struct iwl_station_priv);
  1547. hw->vif_data_size = sizeof(struct iwl_vif_priv);
  1548. for_each_context(priv, ctx) {
  1549. hw->wiphy->interface_modes |= ctx->interface_modes;
  1550. hw->wiphy->interface_modes |= ctx->exclusive_interface_modes;
  1551. }
  1552. BUILD_BUG_ON(NUM_IWL_RXON_CTX != 2);
  1553. if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)) {
  1554. hw->wiphy->iface_combinations = iwlagn_iface_combinations_p2p;
  1555. hw->wiphy->n_iface_combinations =
  1556. ARRAY_SIZE(iwlagn_iface_combinations_p2p);
  1557. } else if (hw->wiphy->interface_modes & BIT(NL80211_IFTYPE_AP)) {
  1558. hw->wiphy->iface_combinations = iwlagn_iface_combinations_dualmode;
  1559. hw->wiphy->n_iface_combinations =
  1560. ARRAY_SIZE(iwlagn_iface_combinations_dualmode);
  1561. }
  1562. hw->wiphy->max_remain_on_channel_duration = 1000;
  1563. hw->wiphy->flags |= WIPHY_FLAG_CUSTOM_REGULATORY |
  1564. WIPHY_FLAG_DISABLE_BEACON_HINTS |
  1565. WIPHY_FLAG_IBSS_RSN;
  1566. if (priv->ucode_wowlan.code.len && device_can_wakeup(priv->bus->dev)) {
  1567. hw->wiphy->wowlan.flags = WIPHY_WOWLAN_MAGIC_PKT |
  1568. WIPHY_WOWLAN_DISCONNECT |
  1569. WIPHY_WOWLAN_EAP_IDENTITY_REQ |
  1570. WIPHY_WOWLAN_RFKILL_RELEASE;
  1571. if (!iwlagn_mod_params.sw_crypto)
  1572. hw->wiphy->wowlan.flags |=
  1573. WIPHY_WOWLAN_SUPPORTS_GTK_REKEY |
  1574. WIPHY_WOWLAN_GTK_REKEY_FAILURE;
  1575. hw->wiphy->wowlan.n_patterns = IWLAGN_WOWLAN_MAX_PATTERNS;
  1576. hw->wiphy->wowlan.pattern_min_len =
  1577. IWLAGN_WOWLAN_MIN_PATTERN_LEN;
  1578. hw->wiphy->wowlan.pattern_max_len =
  1579. IWLAGN_WOWLAN_MAX_PATTERN_LEN;
  1580. }
  1581. if (iwlagn_mod_params.power_save)
  1582. hw->wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
  1583. else
  1584. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  1585. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  1586. /* we create the 802.11 header and a zero-length SSID element */
  1587. hw->wiphy->max_scan_ie_len = capa->max_probe_length - 24 - 2;
  1588. /* Default value; 4 EDCA QOS priorities */
  1589. hw->queues = 4;
  1590. hw->max_listen_interval = IWL_CONN_MAX_LISTEN_INTERVAL;
  1591. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  1592. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  1593. &priv->bands[IEEE80211_BAND_2GHZ];
  1594. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  1595. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  1596. &priv->bands[IEEE80211_BAND_5GHZ];
  1597. iwl_leds_init(priv);
  1598. ret = ieee80211_register_hw(priv->hw);
  1599. if (ret) {
  1600. IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
  1601. return ret;
  1602. }
  1603. priv->mac80211_registered = 1;
  1604. return 0;
  1605. }
  1606. static int iwlagn_mac_start(struct ieee80211_hw *hw)
  1607. {
  1608. struct iwl_priv *priv = hw->priv;
  1609. int ret;
  1610. IWL_DEBUG_MAC80211(priv, "enter\n");
  1611. /* we should be verifying the device is ready to be opened */
  1612. mutex_lock(&priv->shrd->mutex);
  1613. ret = __iwl_up(priv);
  1614. mutex_unlock(&priv->shrd->mutex);
  1615. if (ret)
  1616. return ret;
  1617. IWL_DEBUG_INFO(priv, "Start UP work done.\n");
  1618. /* Now we should be done, and the READY bit should be set. */
  1619. if (WARN_ON(!test_bit(STATUS_READY, &priv->shrd->status)))
  1620. ret = -EIO;
  1621. iwlagn_led_enable(priv);
  1622. priv->is_open = 1;
  1623. IWL_DEBUG_MAC80211(priv, "leave\n");
  1624. return 0;
  1625. }
  1626. static void iwlagn_mac_stop(struct ieee80211_hw *hw)
  1627. {
  1628. struct iwl_priv *priv = hw->priv;
  1629. IWL_DEBUG_MAC80211(priv, "enter\n");
  1630. if (!priv->is_open)
  1631. return;
  1632. priv->is_open = 0;
  1633. iwl_down(priv);
  1634. flush_workqueue(priv->shrd->workqueue);
  1635. /* User space software may expect getting rfkill changes
  1636. * even if interface is down */
  1637. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  1638. iwl_enable_rfkill_int(priv);
  1639. IWL_DEBUG_MAC80211(priv, "leave\n");
  1640. }
  1641. #ifdef CONFIG_PM
  1642. static int iwlagn_send_patterns(struct iwl_priv *priv,
  1643. struct cfg80211_wowlan *wowlan)
  1644. {
  1645. struct iwlagn_wowlan_patterns_cmd *pattern_cmd;
  1646. struct iwl_host_cmd cmd = {
  1647. .id = REPLY_WOWLAN_PATTERNS,
  1648. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  1649. .flags = CMD_SYNC,
  1650. };
  1651. int i, err;
  1652. if (!wowlan->n_patterns)
  1653. return 0;
  1654. cmd.len[0] = sizeof(*pattern_cmd) +
  1655. wowlan->n_patterns * sizeof(struct iwlagn_wowlan_pattern);
  1656. pattern_cmd = kmalloc(cmd.len[0], GFP_KERNEL);
  1657. if (!pattern_cmd)
  1658. return -ENOMEM;
  1659. pattern_cmd->n_patterns = cpu_to_le32(wowlan->n_patterns);
  1660. for (i = 0; i < wowlan->n_patterns; i++) {
  1661. int mask_len = DIV_ROUND_UP(wowlan->patterns[i].pattern_len, 8);
  1662. memcpy(&pattern_cmd->patterns[i].mask,
  1663. wowlan->patterns[i].mask, mask_len);
  1664. memcpy(&pattern_cmd->patterns[i].pattern,
  1665. wowlan->patterns[i].pattern,
  1666. wowlan->patterns[i].pattern_len);
  1667. pattern_cmd->patterns[i].mask_size = mask_len;
  1668. pattern_cmd->patterns[i].pattern_size =
  1669. wowlan->patterns[i].pattern_len;
  1670. }
  1671. cmd.data[0] = pattern_cmd;
  1672. err = iwl_trans_send_cmd(trans(priv), &cmd);
  1673. kfree(pattern_cmd);
  1674. return err;
  1675. }
  1676. #endif
  1677. static void iwlagn_mac_set_rekey_data(struct ieee80211_hw *hw,
  1678. struct ieee80211_vif *vif,
  1679. struct cfg80211_gtk_rekey_data *data)
  1680. {
  1681. struct iwl_priv *priv = hw->priv;
  1682. if (iwlagn_mod_params.sw_crypto)
  1683. return;
  1684. mutex_lock(&priv->shrd->mutex);
  1685. if (priv->contexts[IWL_RXON_CTX_BSS].vif != vif)
  1686. goto out;
  1687. memcpy(priv->kek, data->kek, NL80211_KEK_LEN);
  1688. memcpy(priv->kck, data->kck, NL80211_KCK_LEN);
  1689. priv->replay_ctr = cpu_to_le64(be64_to_cpup((__be64 *)&data->replay_ctr));
  1690. priv->have_rekey_data = true;
  1691. out:
  1692. mutex_unlock(&priv->shrd->mutex);
  1693. }
  1694. struct wowlan_key_data {
  1695. struct iwl_rxon_context *ctx;
  1696. struct iwlagn_wowlan_rsc_tsc_params_cmd *rsc_tsc;
  1697. struct iwlagn_wowlan_tkip_params_cmd *tkip;
  1698. const u8 *bssid;
  1699. bool error, use_rsc_tsc, use_tkip;
  1700. };
  1701. #ifdef CONFIG_PM
  1702. static void iwlagn_convert_p1k(u16 *p1k, __le16 *out)
  1703. {
  1704. int i;
  1705. for (i = 0; i < IWLAGN_P1K_SIZE; i++)
  1706. out[i] = cpu_to_le16(p1k[i]);
  1707. }
  1708. static void iwlagn_wowlan_program_keys(struct ieee80211_hw *hw,
  1709. struct ieee80211_vif *vif,
  1710. struct ieee80211_sta *sta,
  1711. struct ieee80211_key_conf *key,
  1712. void *_data)
  1713. {
  1714. struct iwl_priv *priv = hw->priv;
  1715. struct wowlan_key_data *data = _data;
  1716. struct iwl_rxon_context *ctx = data->ctx;
  1717. struct aes_sc *aes_sc, *aes_tx_sc = NULL;
  1718. struct tkip_sc *tkip_sc, *tkip_tx_sc = NULL;
  1719. struct iwlagn_p1k_cache *rx_p1ks;
  1720. u8 *rx_mic_key;
  1721. struct ieee80211_key_seq seq;
  1722. u32 cur_rx_iv32 = 0;
  1723. u16 p1k[IWLAGN_P1K_SIZE];
  1724. int ret, i;
  1725. mutex_lock(&priv->shrd->mutex);
  1726. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  1727. key->cipher == WLAN_CIPHER_SUITE_WEP104) &&
  1728. !sta && !ctx->key_mapping_keys)
  1729. ret = iwl_set_default_wep_key(priv, ctx, key);
  1730. else
  1731. ret = iwl_set_dynamic_key(priv, ctx, key, sta);
  1732. if (ret) {
  1733. IWL_ERR(priv, "Error setting key during suspend!\n");
  1734. data->error = true;
  1735. }
  1736. switch (key->cipher) {
  1737. case WLAN_CIPHER_SUITE_TKIP:
  1738. if (sta) {
  1739. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.unicast_rsc;
  1740. tkip_tx_sc = &data->rsc_tsc->all_tsc_rsc.tkip.tsc;
  1741. rx_p1ks = data->tkip->rx_uni;
  1742. ieee80211_get_key_tx_seq(key, &seq);
  1743. tkip_tx_sc->iv16 = cpu_to_le16(seq.tkip.iv16);
  1744. tkip_tx_sc->iv32 = cpu_to_le32(seq.tkip.iv32);
  1745. ieee80211_get_tkip_p1k_iv(key, seq.tkip.iv32, p1k);
  1746. iwlagn_convert_p1k(p1k, data->tkip->tx.p1k);
  1747. memcpy(data->tkip->mic_keys.tx,
  1748. &key->key[NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY],
  1749. IWLAGN_MIC_KEY_SIZE);
  1750. rx_mic_key = data->tkip->mic_keys.rx_unicast;
  1751. } else {
  1752. tkip_sc = data->rsc_tsc->all_tsc_rsc.tkip.multicast_rsc;
  1753. rx_p1ks = data->tkip->rx_multi;
  1754. rx_mic_key = data->tkip->mic_keys.rx_mcast;
  1755. }
  1756. /*
  1757. * For non-QoS this relies on the fact that both the uCode and
  1758. * mac80211 use TID 0 (as they need to to avoid replay attacks)
  1759. * for checking the IV in the frames.
  1760. */
  1761. for (i = 0; i < IWLAGN_NUM_RSC; i++) {
  1762. ieee80211_get_key_rx_seq(key, i, &seq);
  1763. tkip_sc[i].iv16 = cpu_to_le16(seq.tkip.iv16);
  1764. tkip_sc[i].iv32 = cpu_to_le32(seq.tkip.iv32);
  1765. /* wrapping isn't allowed, AP must rekey */
  1766. if (seq.tkip.iv32 > cur_rx_iv32)
  1767. cur_rx_iv32 = seq.tkip.iv32;
  1768. }
  1769. ieee80211_get_tkip_rx_p1k(key, data->bssid, cur_rx_iv32, p1k);
  1770. iwlagn_convert_p1k(p1k, rx_p1ks[0].p1k);
  1771. ieee80211_get_tkip_rx_p1k(key, data->bssid,
  1772. cur_rx_iv32 + 1, p1k);
  1773. iwlagn_convert_p1k(p1k, rx_p1ks[1].p1k);
  1774. memcpy(rx_mic_key,
  1775. &key->key[NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY],
  1776. IWLAGN_MIC_KEY_SIZE);
  1777. data->use_tkip = true;
  1778. data->use_rsc_tsc = true;
  1779. break;
  1780. case WLAN_CIPHER_SUITE_CCMP:
  1781. if (sta) {
  1782. u8 *pn = seq.ccmp.pn;
  1783. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.unicast_rsc;
  1784. aes_tx_sc = &data->rsc_tsc->all_tsc_rsc.aes.tsc;
  1785. ieee80211_get_key_tx_seq(key, &seq);
  1786. aes_tx_sc->pn = cpu_to_le64(
  1787. (u64)pn[5] |
  1788. ((u64)pn[4] << 8) |
  1789. ((u64)pn[3] << 16) |
  1790. ((u64)pn[2] << 24) |
  1791. ((u64)pn[1] << 32) |
  1792. ((u64)pn[0] << 40));
  1793. } else
  1794. aes_sc = data->rsc_tsc->all_tsc_rsc.aes.multicast_rsc;
  1795. /*
  1796. * For non-QoS this relies on the fact that both the uCode and
  1797. * mac80211 use TID 0 for checking the IV in the frames.
  1798. */
  1799. for (i = 0; i < IWLAGN_NUM_RSC; i++) {
  1800. u8 *pn = seq.ccmp.pn;
  1801. ieee80211_get_key_rx_seq(key, i, &seq);
  1802. aes_sc->pn = cpu_to_le64(
  1803. (u64)pn[5] |
  1804. ((u64)pn[4] << 8) |
  1805. ((u64)pn[3] << 16) |
  1806. ((u64)pn[2] << 24) |
  1807. ((u64)pn[1] << 32) |
  1808. ((u64)pn[0] << 40));
  1809. }
  1810. data->use_rsc_tsc = true;
  1811. break;
  1812. }
  1813. mutex_unlock(&priv->shrd->mutex);
  1814. }
  1815. static int iwlagn_mac_suspend(struct ieee80211_hw *hw,
  1816. struct cfg80211_wowlan *wowlan)
  1817. {
  1818. struct iwl_priv *priv = hw->priv;
  1819. struct iwlagn_wowlan_wakeup_filter_cmd wakeup_filter_cmd;
  1820. struct iwl_rxon_cmd rxon;
  1821. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1822. struct iwlagn_wowlan_kek_kck_material_cmd kek_kck_cmd;
  1823. struct iwlagn_wowlan_tkip_params_cmd tkip_cmd = {};
  1824. struct wowlan_key_data key_data = {
  1825. .ctx = ctx,
  1826. .bssid = ctx->active.bssid_addr,
  1827. .use_rsc_tsc = false,
  1828. .tkip = &tkip_cmd,
  1829. .use_tkip = false,
  1830. };
  1831. int ret, i;
  1832. u16 seq;
  1833. if (WARN_ON(!wowlan))
  1834. return -EINVAL;
  1835. mutex_lock(&priv->shrd->mutex);
  1836. /* Don't attempt WoWLAN when not associated, tear down instead. */
  1837. if (!ctx->vif || ctx->vif->type != NL80211_IFTYPE_STATION ||
  1838. !iwl_is_associated_ctx(ctx)) {
  1839. ret = 1;
  1840. goto out;
  1841. }
  1842. key_data.rsc_tsc = kzalloc(sizeof(*key_data.rsc_tsc), GFP_KERNEL);
  1843. if (!key_data.rsc_tsc) {
  1844. ret = -ENOMEM;
  1845. goto out;
  1846. }
  1847. memset(&wakeup_filter_cmd, 0, sizeof(wakeup_filter_cmd));
  1848. /*
  1849. * We know the last used seqno, and the uCode expects to know that
  1850. * one, it will increment before TX.
  1851. */
  1852. seq = le16_to_cpu(priv->last_seq_ctl) & IEEE80211_SCTL_SEQ;
  1853. wakeup_filter_cmd.non_qos_seq = cpu_to_le16(seq);
  1854. /*
  1855. * For QoS counters, we store the one to use next, so subtract 0x10
  1856. * since the uCode will add 0x10 before using the value.
  1857. */
  1858. for (i = 0; i < 8; i++) {
  1859. seq = priv->stations[IWL_AP_ID].tid[i].seq_number;
  1860. seq -= 0x10;
  1861. wakeup_filter_cmd.qos_seq[i] = cpu_to_le16(seq);
  1862. }
  1863. if (wowlan->disconnect)
  1864. wakeup_filter_cmd.enabled |=
  1865. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_BEACON_MISS |
  1866. IWLAGN_WOWLAN_WAKEUP_LINK_CHANGE);
  1867. if (wowlan->magic_pkt)
  1868. wakeup_filter_cmd.enabled |=
  1869. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_MAGIC_PACKET);
  1870. if (wowlan->gtk_rekey_failure)
  1871. wakeup_filter_cmd.enabled |=
  1872. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_GTK_REKEY_FAIL);
  1873. if (wowlan->eap_identity_req)
  1874. wakeup_filter_cmd.enabled |=
  1875. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_EAP_IDENT_REQ);
  1876. if (wowlan->four_way_handshake)
  1877. wakeup_filter_cmd.enabled |=
  1878. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_4WAY_HANDSHAKE);
  1879. if (wowlan->rfkill_release)
  1880. wakeup_filter_cmd.enabled |=
  1881. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_RFKILL);
  1882. if (wowlan->n_patterns)
  1883. wakeup_filter_cmd.enabled |=
  1884. cpu_to_le32(IWLAGN_WOWLAN_WAKEUP_PATTERN_MATCH);
  1885. iwl_scan_cancel_timeout(priv, 200);
  1886. memcpy(&rxon, &ctx->active, sizeof(rxon));
  1887. iwl_trans_stop_device(trans(priv));
  1888. priv->shrd->wowlan = true;
  1889. ret = iwlagn_load_ucode_wait_alive(priv, &priv->ucode_wowlan,
  1890. IWL_UCODE_WOWLAN);
  1891. if (ret)
  1892. goto error;
  1893. /* now configure WoWLAN ucode */
  1894. ret = iwl_alive_start(priv);
  1895. if (ret)
  1896. goto error;
  1897. memcpy(&ctx->staging, &rxon, sizeof(rxon));
  1898. ret = iwlagn_commit_rxon(priv, ctx);
  1899. if (ret)
  1900. goto error;
  1901. ret = iwl_power_update_mode(priv, true);
  1902. if (ret)
  1903. goto error;
  1904. if (!iwlagn_mod_params.sw_crypto) {
  1905. /* mark all keys clear */
  1906. priv->ucode_key_table = 0;
  1907. ctx->key_mapping_keys = 0;
  1908. /*
  1909. * This needs to be unlocked due to lock ordering
  1910. * constraints. Since we're in the suspend path
  1911. * that isn't really a problem though.
  1912. */
  1913. mutex_unlock(&priv->shrd->mutex);
  1914. ieee80211_iter_keys(priv->hw, ctx->vif,
  1915. iwlagn_wowlan_program_keys,
  1916. &key_data);
  1917. mutex_lock(&priv->shrd->mutex);
  1918. if (key_data.error) {
  1919. ret = -EIO;
  1920. goto error;
  1921. }
  1922. if (key_data.use_rsc_tsc) {
  1923. struct iwl_host_cmd rsc_tsc_cmd = {
  1924. .id = REPLY_WOWLAN_TSC_RSC_PARAMS,
  1925. .flags = CMD_SYNC,
  1926. .data[0] = key_data.rsc_tsc,
  1927. .dataflags[0] = IWL_HCMD_DFL_NOCOPY,
  1928. .len[0] = sizeof(*key_data.rsc_tsc),
  1929. };
  1930. ret = iwl_trans_send_cmd(trans(priv), &rsc_tsc_cmd);
  1931. if (ret)
  1932. goto error;
  1933. }
  1934. if (key_data.use_tkip) {
  1935. ret = iwl_trans_send_cmd_pdu(trans(priv),
  1936. REPLY_WOWLAN_TKIP_PARAMS,
  1937. CMD_SYNC, sizeof(tkip_cmd),
  1938. &tkip_cmd);
  1939. if (ret)
  1940. goto error;
  1941. }
  1942. if (priv->have_rekey_data) {
  1943. memset(&kek_kck_cmd, 0, sizeof(kek_kck_cmd));
  1944. memcpy(kek_kck_cmd.kck, priv->kck, NL80211_KCK_LEN);
  1945. kek_kck_cmd.kck_len = cpu_to_le16(NL80211_KCK_LEN);
  1946. memcpy(kek_kck_cmd.kek, priv->kek, NL80211_KEK_LEN);
  1947. kek_kck_cmd.kek_len = cpu_to_le16(NL80211_KEK_LEN);
  1948. kek_kck_cmd.replay_ctr = priv->replay_ctr;
  1949. ret = iwl_trans_send_cmd_pdu(trans(priv),
  1950. REPLY_WOWLAN_KEK_KCK_MATERIAL,
  1951. CMD_SYNC, sizeof(kek_kck_cmd),
  1952. &kek_kck_cmd);
  1953. if (ret)
  1954. goto error;
  1955. }
  1956. }
  1957. ret = iwl_trans_send_cmd_pdu(trans(priv), REPLY_WOWLAN_WAKEUP_FILTER,
  1958. CMD_SYNC, sizeof(wakeup_filter_cmd),
  1959. &wakeup_filter_cmd);
  1960. if (ret)
  1961. goto error;
  1962. ret = iwlagn_send_patterns(priv, wowlan);
  1963. if (ret)
  1964. goto error;
  1965. device_set_wakeup_enable(priv->bus->dev, true);
  1966. /* Now let the ucode operate on its own */
  1967. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  1968. CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
  1969. goto out;
  1970. error:
  1971. priv->shrd->wowlan = false;
  1972. iwlagn_prepare_restart(priv);
  1973. ieee80211_restart_hw(priv->hw);
  1974. out:
  1975. mutex_unlock(&priv->shrd->mutex);
  1976. kfree(key_data.rsc_tsc);
  1977. return ret;
  1978. }
  1979. static int iwlagn_mac_resume(struct ieee80211_hw *hw)
  1980. {
  1981. struct iwl_priv *priv = hw->priv;
  1982. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  1983. struct ieee80211_vif *vif;
  1984. unsigned long flags;
  1985. u32 base, status = 0xffffffff;
  1986. int ret = -EIO;
  1987. mutex_lock(&priv->shrd->mutex);
  1988. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  1989. CSR_UCODE_DRV_GP1_BIT_D3_CFG_COMPLETE);
  1990. base = priv->device_pointers.error_event_table;
  1991. if (iwlagn_hw_valid_rtc_data_addr(base)) {
  1992. spin_lock_irqsave(&priv->reg_lock, flags);
  1993. ret = iwl_grab_nic_access_silent(priv);
  1994. if (ret == 0) {
  1995. iwl_write32(priv, HBUS_TARG_MEM_RADDR, base);
  1996. status = iwl_read32(priv, HBUS_TARG_MEM_RDAT);
  1997. iwl_release_nic_access(priv);
  1998. }
  1999. spin_unlock_irqrestore(&priv->reg_lock, flags);
  2000. #ifdef CONFIG_IWLWIFI_DEBUGFS
  2001. if (ret == 0) {
  2002. if (!priv->wowlan_sram)
  2003. priv->wowlan_sram =
  2004. kzalloc(priv->ucode_wowlan.data.len,
  2005. GFP_KERNEL);
  2006. if (priv->wowlan_sram)
  2007. _iwl_read_targ_mem_words(
  2008. priv, 0x800000, priv->wowlan_sram,
  2009. priv->ucode_wowlan.data.len / 4);
  2010. }
  2011. #endif
  2012. }
  2013. /* we'll clear ctx->vif during iwlagn_prepare_restart() */
  2014. vif = ctx->vif;
  2015. priv->shrd->wowlan = false;
  2016. device_set_wakeup_enable(priv->bus->dev, false);
  2017. iwlagn_prepare_restart(priv);
  2018. memset((void *)&ctx->active, 0, sizeof(ctx->active));
  2019. iwl_connection_init_rx_config(priv, ctx);
  2020. iwlagn_set_rxon_chain(priv, ctx);
  2021. mutex_unlock(&priv->shrd->mutex);
  2022. ieee80211_resume_disconnect(vif);
  2023. return 1;
  2024. }
  2025. #endif
  2026. static void iwlagn_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2027. {
  2028. struct iwl_priv *priv = hw->priv;
  2029. IWL_DEBUG_MACDUMP(priv, "enter\n");
  2030. IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  2031. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  2032. if (iwlagn_tx_skb(priv, skb))
  2033. dev_kfree_skb_any(skb);
  2034. IWL_DEBUG_MACDUMP(priv, "leave\n");
  2035. }
  2036. static void iwlagn_mac_update_tkip_key(struct ieee80211_hw *hw,
  2037. struct ieee80211_vif *vif,
  2038. struct ieee80211_key_conf *keyconf,
  2039. struct ieee80211_sta *sta,
  2040. u32 iv32, u16 *phase1key)
  2041. {
  2042. struct iwl_priv *priv = hw->priv;
  2043. iwl_update_tkip_key(priv, vif, keyconf, sta, iv32, phase1key);
  2044. }
  2045. static int iwlagn_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2046. struct ieee80211_vif *vif,
  2047. struct ieee80211_sta *sta,
  2048. struct ieee80211_key_conf *key)
  2049. {
  2050. struct iwl_priv *priv = hw->priv;
  2051. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  2052. struct iwl_rxon_context *ctx = vif_priv->ctx;
  2053. int ret;
  2054. bool is_default_wep_key = false;
  2055. IWL_DEBUG_MAC80211(priv, "enter\n");
  2056. if (iwlagn_mod_params.sw_crypto) {
  2057. IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
  2058. return -EOPNOTSUPP;
  2059. }
  2060. /*
  2061. * We could program these keys into the hardware as well, but we
  2062. * don't expect much multicast traffic in IBSS and having keys
  2063. * for more stations is probably more useful.
  2064. *
  2065. * Mark key TX-only and return 0.
  2066. */
  2067. if (vif->type == NL80211_IFTYPE_ADHOC &&
  2068. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  2069. key->hw_key_idx = WEP_INVALID_OFFSET;
  2070. return 0;
  2071. }
  2072. /* If they key was TX-only, accept deletion */
  2073. if (cmd == DISABLE_KEY && key->hw_key_idx == WEP_INVALID_OFFSET)
  2074. return 0;
  2075. mutex_lock(&priv->shrd->mutex);
  2076. iwl_scan_cancel_timeout(priv, 100);
  2077. BUILD_BUG_ON(WEP_INVALID_OFFSET == IWLAGN_HW_KEY_DEFAULT);
  2078. /*
  2079. * If we are getting WEP group key and we didn't receive any key mapping
  2080. * so far, we are in legacy wep mode (group key only), otherwise we are
  2081. * in 1X mode.
  2082. * In legacy wep mode, we use another host command to the uCode.
  2083. */
  2084. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  2085. key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
  2086. if (cmd == SET_KEY)
  2087. is_default_wep_key = !ctx->key_mapping_keys;
  2088. else
  2089. is_default_wep_key =
  2090. key->hw_key_idx == IWLAGN_HW_KEY_DEFAULT;
  2091. }
  2092. switch (cmd) {
  2093. case SET_KEY:
  2094. if (is_default_wep_key) {
  2095. ret = iwl_set_default_wep_key(priv, vif_priv->ctx, key);
  2096. break;
  2097. }
  2098. ret = iwl_set_dynamic_key(priv, vif_priv->ctx, key, sta);
  2099. if (ret) {
  2100. /*
  2101. * can't add key for RX, but we don't need it
  2102. * in the device for TX so still return 0
  2103. */
  2104. ret = 0;
  2105. key->hw_key_idx = WEP_INVALID_OFFSET;
  2106. }
  2107. IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
  2108. break;
  2109. case DISABLE_KEY:
  2110. if (is_default_wep_key)
  2111. ret = iwl_remove_default_wep_key(priv, ctx, key);
  2112. else
  2113. ret = iwl_remove_dynamic_key(priv, ctx, key, sta);
  2114. IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
  2115. break;
  2116. default:
  2117. ret = -EINVAL;
  2118. }
  2119. mutex_unlock(&priv->shrd->mutex);
  2120. IWL_DEBUG_MAC80211(priv, "leave\n");
  2121. return ret;
  2122. }
  2123. static int iwlagn_mac_ampdu_action(struct ieee80211_hw *hw,
  2124. struct ieee80211_vif *vif,
  2125. enum ieee80211_ampdu_mlme_action action,
  2126. struct ieee80211_sta *sta, u16 tid, u16 *ssn,
  2127. u8 buf_size)
  2128. {
  2129. struct iwl_priv *priv = hw->priv;
  2130. int ret = -EINVAL;
  2131. struct iwl_station_priv *sta_priv = (void *) sta->drv_priv;
  2132. IWL_DEBUG_HT(priv, "A-MPDU action on addr %pM tid %d\n",
  2133. sta->addr, tid);
  2134. if (!(priv->cfg->sku & EEPROM_SKU_CAP_11N_ENABLE))
  2135. return -EACCES;
  2136. mutex_lock(&priv->shrd->mutex);
  2137. switch (action) {
  2138. case IEEE80211_AMPDU_RX_START:
  2139. IWL_DEBUG_HT(priv, "start Rx\n");
  2140. ret = iwl_sta_rx_agg_start(priv, sta, tid, *ssn);
  2141. break;
  2142. case IEEE80211_AMPDU_RX_STOP:
  2143. IWL_DEBUG_HT(priv, "stop Rx\n");
  2144. ret = iwl_sta_rx_agg_stop(priv, sta, tid);
  2145. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  2146. ret = 0;
  2147. break;
  2148. case IEEE80211_AMPDU_TX_START:
  2149. IWL_DEBUG_HT(priv, "start Tx\n");
  2150. ret = iwlagn_tx_agg_start(priv, vif, sta, tid, ssn);
  2151. if (ret == 0) {
  2152. priv->agg_tids_count++;
  2153. IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
  2154. priv->agg_tids_count);
  2155. }
  2156. break;
  2157. case IEEE80211_AMPDU_TX_STOP:
  2158. IWL_DEBUG_HT(priv, "stop Tx\n");
  2159. ret = iwlagn_tx_agg_stop(priv, vif, sta, tid);
  2160. if ((ret == 0) && (priv->agg_tids_count > 0)) {
  2161. priv->agg_tids_count--;
  2162. IWL_DEBUG_HT(priv, "priv->agg_tids_count = %u\n",
  2163. priv->agg_tids_count);
  2164. }
  2165. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status))
  2166. ret = 0;
  2167. if (priv->cfg->ht_params &&
  2168. priv->cfg->ht_params->use_rts_for_aggregation) {
  2169. /*
  2170. * switch off RTS/CTS if it was previously enabled
  2171. */
  2172. sta_priv->lq_sta.lq.general_params.flags &=
  2173. ~LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
  2174. iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
  2175. &sta_priv->lq_sta.lq, CMD_ASYNC, false);
  2176. }
  2177. break;
  2178. case IEEE80211_AMPDU_TX_OPERATIONAL:
  2179. buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
  2180. iwl_trans_txq_agg_setup(trans(priv), iwl_sta_id(sta), tid,
  2181. buf_size);
  2182. /*
  2183. * If the limit is 0, then it wasn't initialised yet,
  2184. * use the default. We can do that since we take the
  2185. * minimum below, and we don't want to go above our
  2186. * default due to hardware restrictions.
  2187. */
  2188. if (sta_priv->max_agg_bufsize == 0)
  2189. sta_priv->max_agg_bufsize =
  2190. LINK_QUAL_AGG_FRAME_LIMIT_DEF;
  2191. /*
  2192. * Even though in theory the peer could have different
  2193. * aggregation reorder buffer sizes for different sessions,
  2194. * our ucode doesn't allow for that and has a global limit
  2195. * for each station. Therefore, use the minimum of all the
  2196. * aggregation sessions and our default value.
  2197. */
  2198. sta_priv->max_agg_bufsize =
  2199. min(sta_priv->max_agg_bufsize, buf_size);
  2200. if (priv->cfg->ht_params &&
  2201. priv->cfg->ht_params->use_rts_for_aggregation) {
  2202. /*
  2203. * switch to RTS/CTS if it is the prefer protection
  2204. * method for HT traffic
  2205. */
  2206. sta_priv->lq_sta.lq.general_params.flags |=
  2207. LINK_QUAL_FLAGS_SET_STA_TLC_RTS_MSK;
  2208. }
  2209. sta_priv->lq_sta.lq.agg_params.agg_frame_cnt_limit =
  2210. sta_priv->max_agg_bufsize;
  2211. iwl_send_lq_cmd(priv, iwl_rxon_ctx_from_vif(vif),
  2212. &sta_priv->lq_sta.lq, CMD_ASYNC, false);
  2213. IWL_INFO(priv, "Tx aggregation enabled on ra = %pM tid = %d\n",
  2214. sta->addr, tid);
  2215. ret = 0;
  2216. break;
  2217. }
  2218. mutex_unlock(&priv->shrd->mutex);
  2219. return ret;
  2220. }
  2221. static int iwlagn_mac_sta_add(struct ieee80211_hw *hw,
  2222. struct ieee80211_vif *vif,
  2223. struct ieee80211_sta *sta)
  2224. {
  2225. struct iwl_priv *priv = hw->priv;
  2226. struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
  2227. struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
  2228. bool is_ap = vif->type == NL80211_IFTYPE_STATION;
  2229. int ret;
  2230. u8 sta_id;
  2231. IWL_DEBUG_INFO(priv, "received request to add station %pM\n",
  2232. sta->addr);
  2233. mutex_lock(&priv->shrd->mutex);
  2234. IWL_DEBUG_INFO(priv, "proceeding to add station %pM\n",
  2235. sta->addr);
  2236. sta_priv->common.sta_id = IWL_INVALID_STATION;
  2237. atomic_set(&sta_priv->pending_frames, 0);
  2238. if (vif->type == NL80211_IFTYPE_AP)
  2239. sta_priv->client = true;
  2240. ret = iwl_add_station_common(priv, vif_priv->ctx, sta->addr,
  2241. is_ap, sta, &sta_id);
  2242. if (ret) {
  2243. IWL_ERR(priv, "Unable to add station %pM (%d)\n",
  2244. sta->addr, ret);
  2245. /* Should we return success if return code is EEXIST ? */
  2246. mutex_unlock(&priv->shrd->mutex);
  2247. return ret;
  2248. }
  2249. sta_priv->common.sta_id = sta_id;
  2250. /* Initialize rate scaling */
  2251. IWL_DEBUG_INFO(priv, "Initializing rate scaling for station %pM\n",
  2252. sta->addr);
  2253. iwl_rs_rate_init(priv, sta, sta_id);
  2254. mutex_unlock(&priv->shrd->mutex);
  2255. return 0;
  2256. }
  2257. static void iwlagn_mac_channel_switch(struct ieee80211_hw *hw,
  2258. struct ieee80211_channel_switch *ch_switch)
  2259. {
  2260. struct iwl_priv *priv = hw->priv;
  2261. const struct iwl_channel_info *ch_info;
  2262. struct ieee80211_conf *conf = &hw->conf;
  2263. struct ieee80211_channel *channel = ch_switch->channel;
  2264. struct iwl_ht_config *ht_conf = &priv->current_ht_config;
  2265. /*
  2266. * MULTI-FIXME
  2267. * When we add support for multiple interfaces, we need to
  2268. * revisit this. The channel switch command in the device
  2269. * only affects the BSS context, but what does that really
  2270. * mean? And what if we get a CSA on the second interface?
  2271. * This needs a lot of work.
  2272. */
  2273. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_BSS];
  2274. u16 ch;
  2275. IWL_DEBUG_MAC80211(priv, "enter\n");
  2276. mutex_lock(&priv->shrd->mutex);
  2277. if (iwl_is_rfkill(priv->shrd))
  2278. goto out;
  2279. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status) ||
  2280. test_bit(STATUS_SCANNING, &priv->shrd->status) ||
  2281. test_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status))
  2282. goto out;
  2283. if (!iwl_is_associated_ctx(ctx))
  2284. goto out;
  2285. if (!priv->cfg->lib->set_channel_switch)
  2286. goto out;
  2287. ch = channel->hw_value;
  2288. if (le16_to_cpu(ctx->active.channel) == ch)
  2289. goto out;
  2290. ch_info = iwl_get_channel_info(priv, channel->band, ch);
  2291. if (!is_channel_valid(ch_info)) {
  2292. IWL_DEBUG_MAC80211(priv, "invalid channel\n");
  2293. goto out;
  2294. }
  2295. spin_lock_irq(&priv->shrd->lock);
  2296. priv->current_ht_config.smps = conf->smps_mode;
  2297. /* Configure HT40 channels */
  2298. ctx->ht.enabled = conf_is_ht(conf);
  2299. if (ctx->ht.enabled) {
  2300. if (conf_is_ht40_minus(conf)) {
  2301. ctx->ht.extension_chan_offset =
  2302. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  2303. ctx->ht.is_40mhz = true;
  2304. } else if (conf_is_ht40_plus(conf)) {
  2305. ctx->ht.extension_chan_offset =
  2306. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  2307. ctx->ht.is_40mhz = true;
  2308. } else {
  2309. ctx->ht.extension_chan_offset =
  2310. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  2311. ctx->ht.is_40mhz = false;
  2312. }
  2313. } else
  2314. ctx->ht.is_40mhz = false;
  2315. if ((le16_to_cpu(ctx->staging.channel) != ch))
  2316. ctx->staging.flags = 0;
  2317. iwl_set_rxon_channel(priv, channel, ctx);
  2318. iwl_set_rxon_ht(priv, ht_conf);
  2319. iwl_set_flags_for_band(priv, ctx, channel->band, ctx->vif);
  2320. spin_unlock_irq(&priv->shrd->lock);
  2321. iwl_set_rate(priv);
  2322. /*
  2323. * at this point, staging_rxon has the
  2324. * configuration for channel switch
  2325. */
  2326. set_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status);
  2327. priv->switch_channel = cpu_to_le16(ch);
  2328. if (priv->cfg->lib->set_channel_switch(priv, ch_switch)) {
  2329. clear_bit(STATUS_CHANNEL_SWITCH_PENDING, &priv->shrd->status);
  2330. priv->switch_channel = 0;
  2331. ieee80211_chswitch_done(ctx->vif, false);
  2332. }
  2333. out:
  2334. mutex_unlock(&priv->shrd->mutex);
  2335. IWL_DEBUG_MAC80211(priv, "leave\n");
  2336. }
  2337. static void iwlagn_configure_filter(struct ieee80211_hw *hw,
  2338. unsigned int changed_flags,
  2339. unsigned int *total_flags,
  2340. u64 multicast)
  2341. {
  2342. struct iwl_priv *priv = hw->priv;
  2343. __le32 filter_or = 0, filter_nand = 0;
  2344. struct iwl_rxon_context *ctx;
  2345. #define CHK(test, flag) do { \
  2346. if (*total_flags & (test)) \
  2347. filter_or |= (flag); \
  2348. else \
  2349. filter_nand |= (flag); \
  2350. } while (0)
  2351. IWL_DEBUG_MAC80211(priv, "Enter: changed: 0x%x, total: 0x%x\n",
  2352. changed_flags, *total_flags);
  2353. CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
  2354. /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
  2355. CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
  2356. CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
  2357. #undef CHK
  2358. mutex_lock(&priv->shrd->mutex);
  2359. for_each_context(priv, ctx) {
  2360. ctx->staging.filter_flags &= ~filter_nand;
  2361. ctx->staging.filter_flags |= filter_or;
  2362. /*
  2363. * Not committing directly because hardware can perform a scan,
  2364. * but we'll eventually commit the filter flags change anyway.
  2365. */
  2366. }
  2367. mutex_unlock(&priv->shrd->mutex);
  2368. /*
  2369. * Receiving all multicast frames is always enabled by the
  2370. * default flags setup in iwl_connection_init_rx_config()
  2371. * since we currently do not support programming multicast
  2372. * filters into the device.
  2373. */
  2374. *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
  2375. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  2376. }
  2377. static void iwlagn_mac_flush(struct ieee80211_hw *hw, bool drop)
  2378. {
  2379. struct iwl_priv *priv = hw->priv;
  2380. mutex_lock(&priv->shrd->mutex);
  2381. IWL_DEBUG_MAC80211(priv, "enter\n");
  2382. if (test_bit(STATUS_EXIT_PENDING, &priv->shrd->status)) {
  2383. IWL_DEBUG_TX(priv, "Aborting flush due to device shutdown\n");
  2384. goto done;
  2385. }
  2386. if (iwl_is_rfkill(priv->shrd)) {
  2387. IWL_DEBUG_TX(priv, "Aborting flush due to RF Kill\n");
  2388. goto done;
  2389. }
  2390. /*
  2391. * mac80211 will not push any more frames for transmit
  2392. * until the flush is completed
  2393. */
  2394. if (drop) {
  2395. IWL_DEBUG_MAC80211(priv, "send flush command\n");
  2396. if (iwlagn_txfifo_flush(priv, IWL_DROP_ALL)) {
  2397. IWL_ERR(priv, "flush request fail\n");
  2398. goto done;
  2399. }
  2400. }
  2401. IWL_DEBUG_MAC80211(priv, "wait transmit/flush all frames\n");
  2402. iwlagn_wait_tx_queue_empty(priv);
  2403. done:
  2404. mutex_unlock(&priv->shrd->mutex);
  2405. IWL_DEBUG_MAC80211(priv, "leave\n");
  2406. }
  2407. void iwlagn_disable_roc(struct iwl_priv *priv)
  2408. {
  2409. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
  2410. lockdep_assert_held(&priv->shrd->mutex);
  2411. if (!priv->hw_roc_setup)
  2412. return;
  2413. ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
  2414. ctx->staging.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2415. priv->hw_roc_channel = NULL;
  2416. memset(ctx->staging.node_addr, 0, ETH_ALEN);
  2417. iwlagn_commit_rxon(priv, ctx);
  2418. ctx->is_active = false;
  2419. priv->hw_roc_setup = false;
  2420. }
  2421. static void iwlagn_disable_roc_work(struct work_struct *work)
  2422. {
  2423. struct iwl_priv *priv = container_of(work, struct iwl_priv,
  2424. hw_roc_disable_work.work);
  2425. mutex_lock(&priv->shrd->mutex);
  2426. iwlagn_disable_roc(priv);
  2427. mutex_unlock(&priv->shrd->mutex);
  2428. }
  2429. static int iwl_mac_remain_on_channel(struct ieee80211_hw *hw,
  2430. struct ieee80211_channel *channel,
  2431. enum nl80211_channel_type channel_type,
  2432. int duration)
  2433. {
  2434. struct iwl_priv *priv = hw->priv;
  2435. struct iwl_rxon_context *ctx = &priv->contexts[IWL_RXON_CTX_PAN];
  2436. int err = 0;
  2437. if (!(priv->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
  2438. return -EOPNOTSUPP;
  2439. if (!(ctx->interface_modes & BIT(NL80211_IFTYPE_P2P_CLIENT)))
  2440. return -EOPNOTSUPP;
  2441. mutex_lock(&priv->shrd->mutex);
  2442. /*
  2443. * TODO: Remove this hack! Firmware needs to be updated
  2444. * to allow longer off-channel periods in scanning for
  2445. * this use case, based on a flag (and we'll need an API
  2446. * flag in the firmware when it has that).
  2447. */
  2448. if (iwl_is_associated(priv, IWL_RXON_CTX_BSS) && duration > 80)
  2449. duration = 80;
  2450. if (test_bit(STATUS_SCAN_HW, &priv->shrd->status)) {
  2451. err = -EBUSY;
  2452. goto out;
  2453. }
  2454. priv->hw_roc_channel = channel;
  2455. priv->hw_roc_chantype = channel_type;
  2456. priv->hw_roc_duration = duration;
  2457. cancel_delayed_work(&priv->hw_roc_disable_work);
  2458. if (!ctx->is_active) {
  2459. ctx->is_active = true;
  2460. ctx->staging.dev_type = RXON_DEV_TYPE_P2P;
  2461. memcpy(ctx->staging.node_addr,
  2462. priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
  2463. ETH_ALEN);
  2464. memcpy(ctx->staging.bssid_addr,
  2465. priv->contexts[IWL_RXON_CTX_BSS].staging.node_addr,
  2466. ETH_ALEN);
  2467. err = iwlagn_commit_rxon(priv, ctx);
  2468. if (err)
  2469. goto out;
  2470. ctx->staging.filter_flags |= RXON_FILTER_ASSOC_MSK |
  2471. RXON_FILTER_PROMISC_MSK |
  2472. RXON_FILTER_CTL2HOST_MSK;
  2473. err = iwlagn_commit_rxon(priv, ctx);
  2474. if (err) {
  2475. iwlagn_disable_roc(priv);
  2476. goto out;
  2477. }
  2478. priv->hw_roc_setup = true;
  2479. }
  2480. err = iwl_scan_initiate(priv, ctx->vif, IWL_SCAN_ROC, channel->band);
  2481. if (err)
  2482. iwlagn_disable_roc(priv);
  2483. out:
  2484. mutex_unlock(&priv->shrd->mutex);
  2485. return err;
  2486. }
  2487. static int iwl_mac_cancel_remain_on_channel(struct ieee80211_hw *hw)
  2488. {
  2489. struct iwl_priv *priv = hw->priv;
  2490. if (!(priv->valid_contexts & BIT(IWL_RXON_CTX_PAN)))
  2491. return -EOPNOTSUPP;
  2492. mutex_lock(&priv->shrd->mutex);
  2493. iwl_scan_cancel_timeout(priv, priv->hw_roc_duration);
  2494. iwlagn_disable_roc(priv);
  2495. mutex_unlock(&priv->shrd->mutex);
  2496. return 0;
  2497. }
  2498. /*****************************************************************************
  2499. *
  2500. * driver setup and teardown
  2501. *
  2502. *****************************************************************************/
  2503. static void iwl_setup_deferred_work(struct iwl_priv *priv)
  2504. {
  2505. priv->shrd->workqueue = create_singlethread_workqueue(DRV_NAME);
  2506. init_waitqueue_head(&priv->wait_command_queue);
  2507. INIT_WORK(&priv->restart, iwl_bg_restart);
  2508. INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
  2509. INIT_WORK(&priv->run_time_calib_work, iwl_bg_run_time_calib_work);
  2510. INIT_WORK(&priv->tx_flush, iwl_bg_tx_flush);
  2511. INIT_WORK(&priv->bt_full_concurrency, iwl_bg_bt_full_concurrency);
  2512. INIT_WORK(&priv->bt_runtime_config, iwl_bg_bt_runtime_config);
  2513. INIT_DELAYED_WORK(&priv->hw_roc_disable_work,
  2514. iwlagn_disable_roc_work);
  2515. iwl_setup_scan_deferred_work(priv);
  2516. if (priv->cfg->lib->bt_setup_deferred_work)
  2517. priv->cfg->lib->bt_setup_deferred_work(priv);
  2518. init_timer(&priv->statistics_periodic);
  2519. priv->statistics_periodic.data = (unsigned long)priv;
  2520. priv->statistics_periodic.function = iwl_bg_statistics_periodic;
  2521. init_timer(&priv->ucode_trace);
  2522. priv->ucode_trace.data = (unsigned long)priv;
  2523. priv->ucode_trace.function = iwl_bg_ucode_trace;
  2524. init_timer(&priv->watchdog);
  2525. priv->watchdog.data = (unsigned long)priv;
  2526. priv->watchdog.function = iwl_bg_watchdog;
  2527. }
  2528. static void iwl_cancel_deferred_work(struct iwl_priv *priv)
  2529. {
  2530. if (priv->cfg->lib->cancel_deferred_work)
  2531. priv->cfg->lib->cancel_deferred_work(priv);
  2532. cancel_work_sync(&priv->run_time_calib_work);
  2533. cancel_work_sync(&priv->beacon_update);
  2534. iwl_cancel_scan_deferred_work(priv);
  2535. cancel_work_sync(&priv->bt_full_concurrency);
  2536. cancel_work_sync(&priv->bt_runtime_config);
  2537. cancel_delayed_work_sync(&priv->hw_roc_disable_work);
  2538. del_timer_sync(&priv->statistics_periodic);
  2539. del_timer_sync(&priv->ucode_trace);
  2540. }
  2541. static void iwl_init_hw_rates(struct iwl_priv *priv,
  2542. struct ieee80211_rate *rates)
  2543. {
  2544. int i;
  2545. for (i = 0; i < IWL_RATE_COUNT_LEGACY; i++) {
  2546. rates[i].bitrate = iwl_rates[i].ieee * 5;
  2547. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  2548. rates[i].hw_value_short = i;
  2549. rates[i].flags = 0;
  2550. if ((i >= IWL_FIRST_CCK_RATE) && (i <= IWL_LAST_CCK_RATE)) {
  2551. /*
  2552. * If CCK != 1M then set short preamble rate flag.
  2553. */
  2554. rates[i].flags |=
  2555. (iwl_rates[i].plcp == IWL_RATE_1M_PLCP) ?
  2556. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  2557. }
  2558. }
  2559. }
  2560. static int iwl_init_drv(struct iwl_priv *priv)
  2561. {
  2562. int ret;
  2563. spin_lock_init(&priv->shrd->sta_lock);
  2564. mutex_init(&priv->shrd->mutex);
  2565. priv->ieee_channels = NULL;
  2566. priv->ieee_rates = NULL;
  2567. priv->band = IEEE80211_BAND_2GHZ;
  2568. priv->iw_mode = NL80211_IFTYPE_STATION;
  2569. priv->current_ht_config.smps = IEEE80211_SMPS_STATIC;
  2570. priv->missed_beacon_threshold = IWL_MISSED_BEACON_THRESHOLD_DEF;
  2571. priv->agg_tids_count = 0;
  2572. /* initialize force reset */
  2573. priv->force_reset[IWL_RF_RESET].reset_duration =
  2574. IWL_DELAY_NEXT_FORCE_RF_RESET;
  2575. priv->force_reset[IWL_FW_RESET].reset_duration =
  2576. IWL_DELAY_NEXT_FORCE_FW_RELOAD;
  2577. priv->rx_statistics_jiffies = jiffies;
  2578. /* Choose which receivers/antennas to use */
  2579. iwlagn_set_rxon_chain(priv, &priv->contexts[IWL_RXON_CTX_BSS]);
  2580. iwl_init_scan_params(priv);
  2581. /* init bt coex */
  2582. if (priv->cfg->bt_params &&
  2583. priv->cfg->bt_params->advanced_bt_coexist) {
  2584. priv->kill_ack_mask = IWLAGN_BT_KILL_ACK_MASK_DEFAULT;
  2585. priv->kill_cts_mask = IWLAGN_BT_KILL_CTS_MASK_DEFAULT;
  2586. priv->bt_valid = IWLAGN_BT_ALL_VALID_MSK;
  2587. priv->bt_on_thresh = BT_ON_THRESHOLD_DEF;
  2588. priv->bt_duration = BT_DURATION_LIMIT_DEF;
  2589. priv->dynamic_frag_thresh = BT_FRAG_THRESHOLD_DEF;
  2590. }
  2591. ret = iwl_init_channel_map(priv);
  2592. if (ret) {
  2593. IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
  2594. goto err;
  2595. }
  2596. ret = iwl_init_geos(priv);
  2597. if (ret) {
  2598. IWL_ERR(priv, "initializing geos failed: %d\n", ret);
  2599. goto err_free_channel_map;
  2600. }
  2601. iwl_init_hw_rates(priv, priv->ieee_rates);
  2602. return 0;
  2603. err_free_channel_map:
  2604. iwl_free_channel_map(priv);
  2605. err:
  2606. return ret;
  2607. }
  2608. static void iwl_uninit_drv(struct iwl_priv *priv)
  2609. {
  2610. iwl_calib_free_results(priv);
  2611. iwl_free_geos(priv);
  2612. iwl_free_channel_map(priv);
  2613. kfree(priv->scan_cmd);
  2614. kfree(priv->beacon_cmd);
  2615. #ifdef CONFIG_IWLWIFI_DEBUGFS
  2616. kfree(priv->wowlan_sram);
  2617. #endif
  2618. }
  2619. static void iwl_mac_rssi_callback(struct ieee80211_hw *hw,
  2620. enum ieee80211_rssi_event rssi_event)
  2621. {
  2622. struct iwl_priv *priv = hw->priv;
  2623. mutex_lock(&priv->shrd->mutex);
  2624. if (priv->cfg->bt_params &&
  2625. priv->cfg->bt_params->advanced_bt_coexist) {
  2626. if (rssi_event == RSSI_EVENT_LOW)
  2627. priv->bt_enable_pspoll = true;
  2628. else if (rssi_event == RSSI_EVENT_HIGH)
  2629. priv->bt_enable_pspoll = false;
  2630. iwlagn_send_advance_bt_config(priv);
  2631. } else {
  2632. IWL_DEBUG_MAC80211(priv, "Advanced BT coex disabled,"
  2633. "ignoring RSSI callback\n");
  2634. }
  2635. mutex_unlock(&priv->shrd->mutex);
  2636. }
  2637. struct ieee80211_ops iwlagn_hw_ops = {
  2638. .tx = iwlagn_mac_tx,
  2639. .start = iwlagn_mac_start,
  2640. .stop = iwlagn_mac_stop,
  2641. #ifdef CONFIG_PM
  2642. .suspend = iwlagn_mac_suspend,
  2643. .resume = iwlagn_mac_resume,
  2644. #endif
  2645. .add_interface = iwl_mac_add_interface,
  2646. .remove_interface = iwl_mac_remove_interface,
  2647. .change_interface = iwl_mac_change_interface,
  2648. .config = iwlagn_mac_config,
  2649. .configure_filter = iwlagn_configure_filter,
  2650. .set_key = iwlagn_mac_set_key,
  2651. .update_tkip_key = iwlagn_mac_update_tkip_key,
  2652. .set_rekey_data = iwlagn_mac_set_rekey_data,
  2653. .conf_tx = iwl_mac_conf_tx,
  2654. .bss_info_changed = iwlagn_bss_info_changed,
  2655. .ampdu_action = iwlagn_mac_ampdu_action,
  2656. .hw_scan = iwl_mac_hw_scan,
  2657. .sta_notify = iwlagn_mac_sta_notify,
  2658. .sta_add = iwlagn_mac_sta_add,
  2659. .sta_remove = iwl_mac_sta_remove,
  2660. .channel_switch = iwlagn_mac_channel_switch,
  2661. .flush = iwlagn_mac_flush,
  2662. .tx_last_beacon = iwl_mac_tx_last_beacon,
  2663. .remain_on_channel = iwl_mac_remain_on_channel,
  2664. .cancel_remain_on_channel = iwl_mac_cancel_remain_on_channel,
  2665. .rssi_callback = iwl_mac_rssi_callback,
  2666. CFG80211_TESTMODE_CMD(iwl_testmode_cmd)
  2667. CFG80211_TESTMODE_DUMP(iwl_testmode_dump)
  2668. };
  2669. static u32 iwl_hw_detect(struct iwl_priv *priv)
  2670. {
  2671. return iwl_read32(priv, CSR_HW_REV);
  2672. }
  2673. /* Size of one Rx buffer in host DRAM */
  2674. #define IWL_RX_BUF_SIZE_4K (4 * 1024)
  2675. #define IWL_RX_BUF_SIZE_8K (8 * 1024)
  2676. static int iwl_set_hw_params(struct iwl_priv *priv)
  2677. {
  2678. if (iwlagn_mod_params.amsdu_size_8K)
  2679. hw_params(priv).rx_page_order =
  2680. get_order(IWL_RX_BUF_SIZE_8K);
  2681. else
  2682. hw_params(priv).rx_page_order =
  2683. get_order(IWL_RX_BUF_SIZE_4K);
  2684. if (iwlagn_mod_params.disable_11n)
  2685. priv->cfg->sku &= ~EEPROM_SKU_CAP_11N_ENABLE;
  2686. /* Device-specific setup */
  2687. return priv->cfg->lib->set_hw_params(priv);
  2688. }
  2689. /* This function both allocates and initializes hw and priv. */
  2690. static struct ieee80211_hw *iwl_alloc_all(struct iwl_cfg *cfg)
  2691. {
  2692. struct iwl_priv *priv;
  2693. /* mac80211 allocates memory for this device instance, including
  2694. * space for this driver's private structure */
  2695. struct ieee80211_hw *hw;
  2696. hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwlagn_hw_ops);
  2697. if (hw == NULL) {
  2698. pr_err("%s: Can not allocate network device\n",
  2699. cfg->name);
  2700. goto out;
  2701. }
  2702. priv = hw->priv;
  2703. priv->hw = hw;
  2704. out:
  2705. return hw;
  2706. }
  2707. int iwl_probe(struct iwl_bus *bus, const struct iwl_trans_ops *trans_ops,
  2708. struct iwl_cfg *cfg)
  2709. {
  2710. int err = 0;
  2711. struct iwl_priv *priv;
  2712. struct ieee80211_hw *hw;
  2713. u16 num_mac;
  2714. u32 hw_rev;
  2715. /************************
  2716. * 1. Allocating HW data
  2717. ************************/
  2718. hw = iwl_alloc_all(cfg);
  2719. if (!hw) {
  2720. err = -ENOMEM;
  2721. goto out;
  2722. }
  2723. priv = hw->priv;
  2724. priv->bus = bus;
  2725. priv->shrd = &priv->_shrd;
  2726. priv->shrd->bus = bus;
  2727. priv->shrd->priv = priv;
  2728. bus_set_drv_data(priv->bus, priv->shrd);
  2729. priv->shrd->trans = trans_ops->alloc(priv->shrd);
  2730. if (priv->shrd->trans == NULL) {
  2731. err = -ENOMEM;
  2732. goto out_free_traffic_mem;
  2733. }
  2734. /* At this point both hw and priv are allocated. */
  2735. SET_IEEE80211_DEV(hw, priv->bus->dev);
  2736. IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
  2737. priv->cfg = cfg;
  2738. /* is antenna coupling more than 35dB ? */
  2739. priv->bt_ant_couple_ok =
  2740. (iwlagn_mod_params.ant_coupling >
  2741. IWL_BT_ANTENNA_COUPLING_THRESHOLD) ?
  2742. true : false;
  2743. /* enable/disable bt channel inhibition */
  2744. priv->bt_ch_announce = iwlagn_mod_params.bt_ch_announce;
  2745. IWL_DEBUG_INFO(priv, "BT channel inhibition is %s\n",
  2746. (priv->bt_ch_announce) ? "On" : "Off");
  2747. if (iwl_alloc_traffic_mem(priv))
  2748. IWL_ERR(priv, "Not enough memory to generate traffic log\n");
  2749. /* these spin locks will be used in apm_ops.init and EEPROM access
  2750. * we should init now
  2751. */
  2752. spin_lock_init(&priv->reg_lock);
  2753. spin_lock_init(&priv->shrd->lock);
  2754. /*
  2755. * stop and reset the on-board processor just in case it is in a
  2756. * strange state ... like being left stranded by a primary kernel
  2757. * and this is now the kdump kernel trying to start up
  2758. */
  2759. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  2760. /***********************
  2761. * 3. Read REV register
  2762. ***********************/
  2763. hw_rev = iwl_hw_detect(priv);
  2764. IWL_INFO(priv, "Detected %s, REV=0x%X\n",
  2765. priv->cfg->name, hw_rev);
  2766. err = iwl_trans_request_irq(trans(priv));
  2767. if (err)
  2768. goto out_free_trans;
  2769. if (iwl_trans_prepare_card_hw(trans(priv))) {
  2770. err = -EIO;
  2771. IWL_WARN(priv, "Failed, HW not ready\n");
  2772. goto out_free_trans;
  2773. }
  2774. /*****************
  2775. * 4. Read EEPROM
  2776. *****************/
  2777. /* Read the EEPROM */
  2778. err = iwl_eeprom_init(priv, hw_rev);
  2779. if (err) {
  2780. IWL_ERR(priv, "Unable to init EEPROM\n");
  2781. goto out_free_trans;
  2782. }
  2783. err = iwl_eeprom_check_version(priv);
  2784. if (err)
  2785. goto out_free_eeprom;
  2786. err = iwl_eeprom_check_sku(priv);
  2787. if (err)
  2788. goto out_free_eeprom;
  2789. /* extract MAC Address */
  2790. iwl_eeprom_get_mac(priv, priv->addresses[0].addr);
  2791. IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->addresses[0].addr);
  2792. priv->hw->wiphy->addresses = priv->addresses;
  2793. priv->hw->wiphy->n_addresses = 1;
  2794. num_mac = iwl_eeprom_query16(priv, EEPROM_NUM_MAC_ADDRESS);
  2795. if (num_mac > 1) {
  2796. memcpy(priv->addresses[1].addr, priv->addresses[0].addr,
  2797. ETH_ALEN);
  2798. priv->addresses[1].addr[5]++;
  2799. priv->hw->wiphy->n_addresses++;
  2800. }
  2801. /************************
  2802. * 5. Setup HW constants
  2803. ************************/
  2804. if (iwl_set_hw_params(priv)) {
  2805. err = -ENOENT;
  2806. IWL_ERR(priv, "failed to set hw parameters\n");
  2807. goto out_free_eeprom;
  2808. }
  2809. /*******************
  2810. * 6. Setup priv
  2811. *******************/
  2812. err = iwl_init_drv(priv);
  2813. if (err)
  2814. goto out_free_eeprom;
  2815. /* At this point both hw and priv are initialized. */
  2816. /********************
  2817. * 7. Setup services
  2818. ********************/
  2819. iwl_setup_deferred_work(priv);
  2820. iwl_setup_rx_handlers(priv);
  2821. iwl_testmode_init(priv);
  2822. /*********************************************
  2823. * 8. Enable interrupts
  2824. *********************************************/
  2825. iwl_enable_rfkill_int(priv);
  2826. /* If platform's RF_KILL switch is NOT set to KILL */
  2827. if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2828. clear_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
  2829. else
  2830. set_bit(STATUS_RF_KILL_HW, &priv->shrd->status);
  2831. wiphy_rfkill_set_hw_state(priv->hw->wiphy,
  2832. test_bit(STATUS_RF_KILL_HW, &priv->shrd->status));
  2833. iwl_power_initialize(priv);
  2834. iwl_tt_initialize(priv);
  2835. init_completion(&priv->firmware_loading_complete);
  2836. err = iwl_request_firmware(priv, true);
  2837. if (err)
  2838. goto out_destroy_workqueue;
  2839. return 0;
  2840. out_destroy_workqueue:
  2841. destroy_workqueue(priv->shrd->workqueue);
  2842. priv->shrd->workqueue = NULL;
  2843. iwl_uninit_drv(priv);
  2844. out_free_eeprom:
  2845. iwl_eeprom_free(priv);
  2846. out_free_trans:
  2847. iwl_trans_free(trans(priv));
  2848. out_free_traffic_mem:
  2849. iwl_free_traffic_mem(priv);
  2850. ieee80211_free_hw(priv->hw);
  2851. out:
  2852. return err;
  2853. }
  2854. void __devexit iwl_remove(struct iwl_priv * priv)
  2855. {
  2856. wait_for_completion(&priv->firmware_loading_complete);
  2857. IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
  2858. iwl_dbgfs_unregister(priv);
  2859. sysfs_remove_group(&priv->bus->dev->kobj,
  2860. &iwl_attribute_group);
  2861. /* ieee80211_unregister_hw call wil cause iwl_mac_stop to
  2862. * to be called and iwl_down since we are removing the device
  2863. * we need to set STATUS_EXIT_PENDING bit.
  2864. */
  2865. set_bit(STATUS_EXIT_PENDING, &priv->shrd->status);
  2866. iwl_testmode_cleanup(priv);
  2867. iwl_leds_exit(priv);
  2868. if (priv->mac80211_registered) {
  2869. ieee80211_unregister_hw(priv->hw);
  2870. priv->mac80211_registered = 0;
  2871. }
  2872. /* Reset to low power before unloading driver. */
  2873. iwl_apm_stop(priv);
  2874. iwl_tt_exit(priv);
  2875. /* make sure we flush any pending irq or
  2876. * tasklet for the driver */
  2877. iwl_trans_disable_sync_irq(trans(priv));
  2878. iwl_dealloc_ucode(priv);
  2879. iwl_trans_rx_free(trans(priv));
  2880. iwl_trans_tx_free(trans(priv));
  2881. iwl_eeprom_free(priv);
  2882. /*netif_stop_queue(dev); */
  2883. flush_workqueue(priv->shrd->workqueue);
  2884. /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
  2885. * priv->shrd->workqueue... so we can't take down the workqueue
  2886. * until now... */
  2887. destroy_workqueue(priv->shrd->workqueue);
  2888. priv->shrd->workqueue = NULL;
  2889. iwl_free_traffic_mem(priv);
  2890. iwl_trans_free(trans(priv));
  2891. bus_set_drv_data(priv->bus, NULL);
  2892. iwl_uninit_drv(priv);
  2893. dev_kfree_skb(priv->beacon_skb);
  2894. ieee80211_free_hw(priv->hw);
  2895. }
  2896. /*****************************************************************************
  2897. *
  2898. * driver and module entry point
  2899. *
  2900. *****************************************************************************/
  2901. static int __init iwl_init(void)
  2902. {
  2903. int ret;
  2904. pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
  2905. pr_info(DRV_COPYRIGHT "\n");
  2906. ret = iwlagn_rate_control_register();
  2907. if (ret) {
  2908. pr_err("Unable to register rate control algorithm: %d\n", ret);
  2909. return ret;
  2910. }
  2911. ret = iwl_pci_register_driver();
  2912. if (ret)
  2913. goto error_register;
  2914. return ret;
  2915. error_register:
  2916. iwlagn_rate_control_unregister();
  2917. return ret;
  2918. }
  2919. static void __exit iwl_exit(void)
  2920. {
  2921. iwl_pci_unregister_driver();
  2922. iwlagn_rate_control_unregister();
  2923. }
  2924. module_exit(iwl_exit);
  2925. module_init(iwl_init);
  2926. #ifdef CONFIG_IWLWIFI_DEBUG
  2927. module_param_named(debug, iwlagn_mod_params.debug_level, uint,
  2928. S_IRUGO | S_IWUSR);
  2929. MODULE_PARM_DESC(debug, "debug output mask");
  2930. #endif
  2931. module_param_named(swcrypto, iwlagn_mod_params.sw_crypto, int, S_IRUGO);
  2932. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  2933. module_param_named(queues_num, iwlagn_mod_params.num_of_queues, int, S_IRUGO);
  2934. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  2935. module_param_named(11n_disable, iwlagn_mod_params.disable_11n, int, S_IRUGO);
  2936. MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
  2937. module_param_named(amsdu_size_8K, iwlagn_mod_params.amsdu_size_8K,
  2938. int, S_IRUGO);
  2939. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
  2940. module_param_named(fw_restart, iwlagn_mod_params.restart_fw, int, S_IRUGO);
  2941. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");
  2942. module_param_named(ucode_alternative,
  2943. iwlagn_mod_params.wanted_ucode_alternative,
  2944. int, S_IRUGO);
  2945. MODULE_PARM_DESC(ucode_alternative,
  2946. "specify ucode alternative to use from ucode file");
  2947. module_param_named(antenna_coupling, iwlagn_mod_params.ant_coupling,
  2948. int, S_IRUGO);
  2949. MODULE_PARM_DESC(antenna_coupling,
  2950. "specify antenna coupling in dB (defualt: 0 dB)");
  2951. module_param_named(bt_ch_inhibition, iwlagn_mod_params.bt_ch_announce,
  2952. bool, S_IRUGO);
  2953. MODULE_PARM_DESC(bt_ch_inhibition,
  2954. "Enable BT channel inhibition (default: enable)");
  2955. module_param_named(plcp_check, iwlagn_mod_params.plcp_check, bool, S_IRUGO);
  2956. MODULE_PARM_DESC(plcp_check, "Check plcp health (default: 1 [enabled])");
  2957. module_param_named(ack_check, iwlagn_mod_params.ack_check, bool, S_IRUGO);
  2958. MODULE_PARM_DESC(ack_check, "Check ack health (default: 0 [disabled])");
  2959. module_param_named(wd_disable, iwlagn_mod_params.wd_disable, bool, S_IRUGO);
  2960. MODULE_PARM_DESC(wd_disable,
  2961. "Disable stuck queue watchdog timer (default: 0 [enabled])");
  2962. /*
  2963. * set bt_coex_active to true, uCode will do kill/defer
  2964. * every time the priority line is asserted (BT is sending signals on the
  2965. * priority line in the PCIx).
  2966. * set bt_coex_active to false, uCode will ignore the BT activity and
  2967. * perform the normal operation
  2968. *
  2969. * User might experience transmit issue on some platform due to WiFi/BT
  2970. * co-exist problem. The possible behaviors are:
  2971. * Able to scan and finding all the available AP
  2972. * Not able to associate with any AP
  2973. * On those platforms, WiFi communication can be restored by set
  2974. * "bt_coex_active" module parameter to "false"
  2975. *
  2976. * default: bt_coex_active = true (BT_COEX_ENABLE)
  2977. */
  2978. module_param_named(bt_coex_active, iwlagn_mod_params.bt_coex_active,
  2979. bool, S_IRUGO);
  2980. MODULE_PARM_DESC(bt_coex_active, "enable wifi/bt co-exist (default: enable)");
  2981. module_param_named(led_mode, iwlagn_mod_params.led_mode, int, S_IRUGO);
  2982. MODULE_PARM_DESC(led_mode, "0=system default, "
  2983. "1=On(RF On)/Off(RF Off), 2=blinking (default: 0)");
  2984. module_param_named(power_save, iwlagn_mod_params.power_save,
  2985. bool, S_IRUGO);
  2986. MODULE_PARM_DESC(power_save,
  2987. "enable WiFi power management (default: disable)");
  2988. module_param_named(power_level, iwlagn_mod_params.power_level,
  2989. int, S_IRUGO);
  2990. MODULE_PARM_DESC(power_level,
  2991. "default power save level (range from 1 - 5, default: 1)");
  2992. module_param_named(auto_agg, iwlagn_mod_params.auto_agg,
  2993. bool, S_IRUGO);
  2994. MODULE_PARM_DESC(auto_agg,
  2995. "enable agg w/o check traffic load (default: enable)");
  2996. /*
  2997. * For now, keep using power level 1 instead of automatically
  2998. * adjusting ...
  2999. */
  3000. module_param_named(no_sleep_autoadjust, iwlagn_mod_params.no_sleep_autoadjust,
  3001. bool, S_IRUGO);
  3002. MODULE_PARM_DESC(no_sleep_autoadjust,
  3003. "don't automatically adjust sleep level "
  3004. "according to maximum network latency (default: true)");