iwl-agn.c 97 KB

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