iwl-agn.c 98 KB

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