iwl3945-base.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2009 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/pci.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/delay.h>
  35. #include <linux/skbuff.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/wireless.h>
  38. #include <linux/firmware.h>
  39. #include <linux/etherdevice.h>
  40. #include <linux/if_arp.h>
  41. #include <net/ieee80211_radiotap.h>
  42. #include <net/lib80211.h>
  43. #include <net/mac80211.h>
  44. #include <asm/div64.h>
  45. #define DRV_NAME "iwl3945"
  46. #include "iwl-fh.h"
  47. #include "iwl-3945-fh.h"
  48. #include "iwl-commands.h"
  49. #include "iwl-sta.h"
  50. #include "iwl-3945.h"
  51. #include "iwl-helpers.h"
  52. #include "iwl-core.h"
  53. #include "iwl-dev.h"
  54. /*
  55. * module name, copyright, version, etc.
  56. */
  57. #define DRV_DESCRIPTION \
  58. "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
  59. #ifdef CONFIG_IWLWIFI_DEBUG
  60. #define VD "d"
  61. #else
  62. #define VD
  63. #endif
  64. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  65. #define VS "s"
  66. #else
  67. #define VS
  68. #endif
  69. #define IWL39_VERSION "1.2.26k" VD VS
  70. #define DRV_COPYRIGHT "Copyright(c) 2003-2009 Intel Corporation"
  71. #define DRV_AUTHOR "<ilw@linux.intel.com>"
  72. #define DRV_VERSION IWL39_VERSION
  73. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  74. MODULE_VERSION(DRV_VERSION);
  75. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  76. MODULE_LICENSE("GPL");
  77. /* module parameters */
  78. struct iwl_mod_params iwl3945_mod_params = {
  79. .num_of_queues = IWL39_MAX_NUM_QUEUES,
  80. .sw_crypto = 1,
  81. .restart_fw = 1,
  82. /* the rest are 0 by default */
  83. };
  84. /*************** STATION TABLE MANAGEMENT ****
  85. * mac80211 should be examined to determine if sta_info is duplicating
  86. * the functionality provided here
  87. */
  88. /**************************************************************/
  89. #if 0 /* temporary disable till we add real remove station */
  90. /**
  91. * iwl3945_remove_station - Remove driver's knowledge of station.
  92. *
  93. * NOTE: This does not remove station from device's station table.
  94. */
  95. static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
  96. {
  97. int index = IWL_INVALID_STATION;
  98. int i;
  99. unsigned long flags;
  100. spin_lock_irqsave(&priv->sta_lock, flags);
  101. if (is_ap)
  102. index = IWL_AP_ID;
  103. else if (is_broadcast_ether_addr(addr))
  104. index = priv->hw_params.bcast_sta_id;
  105. else
  106. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
  107. if (priv->stations_39[i].used &&
  108. !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
  109. addr)) {
  110. index = i;
  111. break;
  112. }
  113. if (unlikely(index == IWL_INVALID_STATION))
  114. goto out;
  115. if (priv->stations_39[index].used) {
  116. priv->stations_39[index].used = 0;
  117. priv->num_stations--;
  118. }
  119. BUG_ON(priv->num_stations < 0);
  120. out:
  121. spin_unlock_irqrestore(&priv->sta_lock, flags);
  122. return 0;
  123. }
  124. #endif
  125. /**
  126. * iwl3945_clear_stations_table - Clear the driver's station table
  127. *
  128. * NOTE: This does not clear or otherwise alter the device's station table.
  129. */
  130. void iwl3945_clear_stations_table(struct iwl_priv *priv)
  131. {
  132. unsigned long flags;
  133. spin_lock_irqsave(&priv->sta_lock, flags);
  134. priv->num_stations = 0;
  135. memset(priv->stations_39, 0, sizeof(priv->stations_39));
  136. spin_unlock_irqrestore(&priv->sta_lock, flags);
  137. }
  138. /**
  139. * iwl3945_add_station - Add station to station tables in driver and device
  140. */
  141. u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags, struct ieee80211_sta_ht_cap *ht_info)
  142. {
  143. int i;
  144. int index = IWL_INVALID_STATION;
  145. struct iwl3945_station_entry *station;
  146. unsigned long flags_spin;
  147. u8 rate;
  148. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  149. if (is_ap)
  150. index = IWL_AP_ID;
  151. else if (is_broadcast_ether_addr(addr))
  152. index = priv->hw_params.bcast_sta_id;
  153. else
  154. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  155. if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
  156. addr)) {
  157. index = i;
  158. break;
  159. }
  160. if (!priv->stations_39[i].used &&
  161. index == IWL_INVALID_STATION)
  162. index = i;
  163. }
  164. /* These two conditions has the same outcome but keep them separate
  165. since they have different meaning */
  166. if (unlikely(index == IWL_INVALID_STATION)) {
  167. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  168. return index;
  169. }
  170. if (priv->stations_39[index].used &&
  171. !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
  172. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  173. return index;
  174. }
  175. IWL_DEBUG_ASSOC(priv, "Add STA ID %d: %pM\n", index, addr);
  176. station = &priv->stations_39[index];
  177. station->used = 1;
  178. priv->num_stations++;
  179. /* Set up the REPLY_ADD_STA command to send to device */
  180. memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
  181. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  182. station->sta.mode = 0;
  183. station->sta.sta.sta_id = index;
  184. station->sta.station_flags = 0;
  185. if (priv->band == IEEE80211_BAND_5GHZ)
  186. rate = IWL_RATE_6M_PLCP;
  187. else
  188. rate = IWL_RATE_1M_PLCP;
  189. /* Turn on both antennas for the station... */
  190. station->sta.rate_n_flags =
  191. iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
  192. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  193. /* Add station to device's station table */
  194. iwl_send_add_sta(priv,
  195. (struct iwl_addsta_cmd *)&station->sta, flags);
  196. return index;
  197. }
  198. /**
  199. * iwl3945_get_antenna_flags - Get antenna flags for RXON command
  200. * @priv: eeprom and antenna fields are used to determine antenna flags
  201. *
  202. * priv->eeprom39 is used to determine if antenna AUX/MAIN are reversed
  203. * iwl3945_mod_params.antenna specifies the antenna diversity mode:
  204. *
  205. * IWL_ANTENNA_DIVERSITY - NIC selects best antenna by itself
  206. * IWL_ANTENNA_MAIN - Force MAIN antenna
  207. * IWL_ANTENNA_AUX - Force AUX antenna
  208. */
  209. __le32 iwl3945_get_antenna_flags(const struct iwl_priv *priv)
  210. {
  211. struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  212. switch (iwl3945_mod_params.antenna) {
  213. case IWL_ANTENNA_DIVERSITY:
  214. return 0;
  215. case IWL_ANTENNA_MAIN:
  216. if (eeprom->antenna_switch_type)
  217. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  218. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  219. case IWL_ANTENNA_AUX:
  220. if (eeprom->antenna_switch_type)
  221. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_A_MSK;
  222. return RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_B_MSK;
  223. }
  224. /* bad antenna selector value */
  225. IWL_ERR(priv, "Bad antenna selector value (0x%x)\n",
  226. iwl3945_mod_params.antenna);
  227. return 0; /* "diversity" is default if error */
  228. }
  229. static int iwl3945_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
  230. struct ieee80211_key_conf *keyconf,
  231. u8 sta_id)
  232. {
  233. unsigned long flags;
  234. __le16 key_flags = 0;
  235. int ret;
  236. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  237. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  238. if (sta_id == priv->hw_params.bcast_sta_id)
  239. key_flags |= STA_KEY_MULTICAST_MSK;
  240. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  241. keyconf->hw_key_idx = keyconf->keyidx;
  242. key_flags &= ~STA_KEY_FLG_INVALID;
  243. spin_lock_irqsave(&priv->sta_lock, flags);
  244. priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
  245. priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
  246. memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
  247. keyconf->keylen);
  248. memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
  249. keyconf->keylen);
  250. if ((priv->stations_39[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
  251. == STA_KEY_FLG_NO_ENC)
  252. priv->stations_39[sta_id].sta.key.key_offset =
  253. iwl_get_free_ucode_key_index(priv);
  254. /* else, we are overriding an existing key => no need to allocated room
  255. * in uCode. */
  256. WARN(priv->stations_39[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  257. "no space for a new key");
  258. priv->stations_39[sta_id].sta.key.key_flags = key_flags;
  259. priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  260. priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  261. IWL_DEBUG_INFO(priv, "hwcrypto: modify ucode station key info\n");
  262. ret = iwl_send_add_sta(priv,
  263. (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, CMD_ASYNC);
  264. spin_unlock_irqrestore(&priv->sta_lock, flags);
  265. return ret;
  266. }
  267. static int iwl3945_set_tkip_dynamic_key_info(struct iwl_priv *priv,
  268. struct ieee80211_key_conf *keyconf,
  269. u8 sta_id)
  270. {
  271. return -EOPNOTSUPP;
  272. }
  273. static int iwl3945_set_wep_dynamic_key_info(struct iwl_priv *priv,
  274. struct ieee80211_key_conf *keyconf,
  275. u8 sta_id)
  276. {
  277. return -EOPNOTSUPP;
  278. }
  279. static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
  280. {
  281. unsigned long flags;
  282. spin_lock_irqsave(&priv->sta_lock, flags);
  283. memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
  284. memset(&priv->stations_39[sta_id].sta.key, 0,
  285. sizeof(struct iwl4965_keyinfo));
  286. priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
  287. priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  288. priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  289. spin_unlock_irqrestore(&priv->sta_lock, flags);
  290. IWL_DEBUG_INFO(priv, "hwcrypto: clear ucode station key info\n");
  291. iwl_send_add_sta(priv,
  292. (struct iwl_addsta_cmd *)&priv->stations_39[sta_id].sta, 0);
  293. return 0;
  294. }
  295. static int iwl3945_set_dynamic_key(struct iwl_priv *priv,
  296. struct ieee80211_key_conf *keyconf, u8 sta_id)
  297. {
  298. int ret = 0;
  299. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  300. switch (keyconf->alg) {
  301. case ALG_CCMP:
  302. ret = iwl3945_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
  303. break;
  304. case ALG_TKIP:
  305. ret = iwl3945_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
  306. break;
  307. case ALG_WEP:
  308. ret = iwl3945_set_wep_dynamic_key_info(priv, keyconf, sta_id);
  309. break;
  310. default:
  311. IWL_ERR(priv, "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
  312. ret = -EINVAL;
  313. }
  314. IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
  315. keyconf->alg, keyconf->keylen, keyconf->keyidx,
  316. sta_id, ret);
  317. return ret;
  318. }
  319. static int iwl3945_remove_static_key(struct iwl_priv *priv)
  320. {
  321. int ret = -EOPNOTSUPP;
  322. return ret;
  323. }
  324. static int iwl3945_set_static_key(struct iwl_priv *priv,
  325. struct ieee80211_key_conf *key)
  326. {
  327. if (key->alg == ALG_WEP)
  328. return -EOPNOTSUPP;
  329. IWL_ERR(priv, "Static key invalid: alg %d\n", key->alg);
  330. return -EINVAL;
  331. }
  332. static void iwl3945_clear_free_frames(struct iwl_priv *priv)
  333. {
  334. struct list_head *element;
  335. IWL_DEBUG_INFO(priv, "%d frames on pre-allocated heap on clear.\n",
  336. priv->frames_count);
  337. while (!list_empty(&priv->free_frames)) {
  338. element = priv->free_frames.next;
  339. list_del(element);
  340. kfree(list_entry(element, struct iwl3945_frame, list));
  341. priv->frames_count--;
  342. }
  343. if (priv->frames_count) {
  344. IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
  345. priv->frames_count);
  346. priv->frames_count = 0;
  347. }
  348. }
  349. static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
  350. {
  351. struct iwl3945_frame *frame;
  352. struct list_head *element;
  353. if (list_empty(&priv->free_frames)) {
  354. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  355. if (!frame) {
  356. IWL_ERR(priv, "Could not allocate frame!\n");
  357. return NULL;
  358. }
  359. priv->frames_count++;
  360. return frame;
  361. }
  362. element = priv->free_frames.next;
  363. list_del(element);
  364. return list_entry(element, struct iwl3945_frame, list);
  365. }
  366. static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
  367. {
  368. memset(frame, 0, sizeof(*frame));
  369. list_add(&frame->list, &priv->free_frames);
  370. }
  371. unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
  372. struct ieee80211_hdr *hdr,
  373. int left)
  374. {
  375. if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
  376. ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
  377. (priv->iw_mode != NL80211_IFTYPE_AP)))
  378. return 0;
  379. if (priv->ibss_beacon->len > left)
  380. return 0;
  381. memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
  382. return priv->ibss_beacon->len;
  383. }
  384. static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
  385. {
  386. struct iwl3945_frame *frame;
  387. unsigned int frame_size;
  388. int rc;
  389. u8 rate;
  390. frame = iwl3945_get_free_frame(priv);
  391. if (!frame) {
  392. IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
  393. "command.\n");
  394. return -ENOMEM;
  395. }
  396. rate = iwl_rate_get_lowest_plcp(priv);
  397. frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
  398. rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
  399. &frame->u.cmd[0]);
  400. iwl3945_free_frame(priv, frame);
  401. return rc;
  402. }
  403. static void iwl3945_unset_hw_params(struct iwl_priv *priv)
  404. {
  405. if (priv->shared_virt)
  406. pci_free_consistent(priv->pci_dev,
  407. sizeof(struct iwl3945_shared),
  408. priv->shared_virt,
  409. priv->shared_phys);
  410. }
  411. #define MAX_UCODE_BEACON_INTERVAL 1024
  412. #define INTEL_CONN_LISTEN_INTERVAL cpu_to_le16(0xA)
  413. static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
  414. {
  415. u16 new_val = 0;
  416. u16 beacon_factor = 0;
  417. beacon_factor =
  418. (beacon_val + MAX_UCODE_BEACON_INTERVAL)
  419. / MAX_UCODE_BEACON_INTERVAL;
  420. new_val = beacon_val / beacon_factor;
  421. return cpu_to_le16(new_val);
  422. }
  423. static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
  424. {
  425. u64 interval_tm_unit;
  426. u64 tsf, result;
  427. unsigned long flags;
  428. struct ieee80211_conf *conf = NULL;
  429. u16 beacon_int = 0;
  430. conf = ieee80211_get_hw_conf(priv->hw);
  431. spin_lock_irqsave(&priv->lock, flags);
  432. priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
  433. priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
  434. tsf = priv->timestamp;
  435. beacon_int = priv->beacon_int;
  436. spin_unlock_irqrestore(&priv->lock, flags);
  437. if (priv->iw_mode == NL80211_IFTYPE_STATION) {
  438. if (beacon_int == 0) {
  439. priv->rxon_timing.beacon_interval = cpu_to_le16(100);
  440. priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
  441. } else {
  442. priv->rxon_timing.beacon_interval =
  443. cpu_to_le16(beacon_int);
  444. priv->rxon_timing.beacon_interval =
  445. iwl3945_adjust_beacon_interval(
  446. le16_to_cpu(priv->rxon_timing.beacon_interval));
  447. }
  448. priv->rxon_timing.atim_window = 0;
  449. } else {
  450. priv->rxon_timing.beacon_interval =
  451. iwl3945_adjust_beacon_interval(
  452. priv->vif->bss_conf.beacon_int);
  453. /* TODO: we need to get atim_window from upper stack
  454. * for now we set to 0 */
  455. priv->rxon_timing.atim_window = 0;
  456. }
  457. interval_tm_unit =
  458. (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
  459. result = do_div(tsf, interval_tm_unit);
  460. priv->rxon_timing.beacon_init_val =
  461. cpu_to_le32((u32) ((u64) interval_tm_unit - result));
  462. IWL_DEBUG_ASSOC(priv,
  463. "beacon interval %d beacon timer %d beacon tim %d\n",
  464. le16_to_cpu(priv->rxon_timing.beacon_interval),
  465. le32_to_cpu(priv->rxon_timing.beacon_init_val),
  466. le16_to_cpu(priv->rxon_timing.atim_window));
  467. }
  468. static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
  469. struct ieee80211_tx_info *info,
  470. struct iwl_cmd *cmd,
  471. struct sk_buff *skb_frag,
  472. int sta_id)
  473. {
  474. struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  475. struct iwl3945_hw_key *keyinfo =
  476. &priv->stations_39[sta_id].keyinfo;
  477. switch (keyinfo->alg) {
  478. case ALG_CCMP:
  479. tx->sec_ctl = TX_CMD_SEC_CCM;
  480. memcpy(tx->key, keyinfo->key, keyinfo->keylen);
  481. IWL_DEBUG_TX(priv, "tx_cmd with AES hwcrypto\n");
  482. break;
  483. case ALG_TKIP:
  484. break;
  485. case ALG_WEP:
  486. tx->sec_ctl = TX_CMD_SEC_WEP |
  487. (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
  488. if (keyinfo->keylen == 13)
  489. tx->sec_ctl |= TX_CMD_SEC_KEY128;
  490. memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
  491. IWL_DEBUG_TX(priv, "Configuring packet for WEP encryption "
  492. "with key %d\n", info->control.hw_key->hw_key_idx);
  493. break;
  494. default:
  495. IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
  496. break;
  497. }
  498. }
  499. /*
  500. * handle build REPLY_TX command notification.
  501. */
  502. static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
  503. struct iwl_cmd *cmd,
  504. struct ieee80211_tx_info *info,
  505. struct ieee80211_hdr *hdr, u8 std_id)
  506. {
  507. struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  508. __le32 tx_flags = tx->tx_flags;
  509. __le16 fc = hdr->frame_control;
  510. u8 rc_flags = info->control.rates[0].flags;
  511. tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  512. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  513. tx_flags |= TX_CMD_FLG_ACK_MSK;
  514. if (ieee80211_is_mgmt(fc))
  515. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  516. if (ieee80211_is_probe_resp(fc) &&
  517. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  518. tx_flags |= TX_CMD_FLG_TSF_MSK;
  519. } else {
  520. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  521. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  522. }
  523. tx->sta_id = std_id;
  524. if (ieee80211_has_morefrags(fc))
  525. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  526. if (ieee80211_is_data_qos(fc)) {
  527. u8 *qc = ieee80211_get_qos_ctl(hdr);
  528. tx->tid_tspec = qc[0] & 0xf;
  529. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  530. } else {
  531. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  532. }
  533. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  534. tx_flags |= TX_CMD_FLG_RTS_MSK;
  535. tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  536. } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  537. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  538. tx_flags |= TX_CMD_FLG_CTS_MSK;
  539. }
  540. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  541. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  542. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  543. if (ieee80211_is_mgmt(fc)) {
  544. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  545. tx->timeout.pm_frame_timeout = cpu_to_le16(3);
  546. else
  547. tx->timeout.pm_frame_timeout = cpu_to_le16(2);
  548. } else {
  549. tx->timeout.pm_frame_timeout = 0;
  550. #ifdef CONFIG_IWLWIFI_LEDS
  551. priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
  552. #endif
  553. }
  554. tx->driver_txop = 0;
  555. tx->tx_flags = tx_flags;
  556. tx->next_frame_len = 0;
  557. }
  558. /*
  559. * start REPLY_TX command process
  560. */
  561. static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  562. {
  563. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  564. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  565. struct iwl3945_tx_cmd *tx;
  566. struct iwl_tx_queue *txq = NULL;
  567. struct iwl_queue *q = NULL;
  568. struct iwl_cmd *out_cmd = NULL;
  569. dma_addr_t phys_addr;
  570. dma_addr_t txcmd_phys;
  571. int txq_id = skb_get_queue_mapping(skb);
  572. u16 len, idx, len_org, hdr_len; /* TODO: len_org is not used */
  573. u8 id;
  574. u8 unicast;
  575. u8 sta_id;
  576. u8 tid = 0;
  577. u16 seq_number = 0;
  578. __le16 fc;
  579. u8 wait_write_ptr = 0;
  580. u8 *qc = NULL;
  581. unsigned long flags;
  582. int rc;
  583. spin_lock_irqsave(&priv->lock, flags);
  584. if (iwl_is_rfkill(priv)) {
  585. IWL_DEBUG_DROP(priv, "Dropping - RF KILL\n");
  586. goto drop_unlock;
  587. }
  588. if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
  589. IWL_ERR(priv, "ERROR: No TX rate available.\n");
  590. goto drop_unlock;
  591. }
  592. unicast = !is_multicast_ether_addr(hdr->addr1);
  593. id = 0;
  594. fc = hdr->frame_control;
  595. #ifdef CONFIG_IWLWIFI_DEBUG
  596. if (ieee80211_is_auth(fc))
  597. IWL_DEBUG_TX(priv, "Sending AUTH frame\n");
  598. else if (ieee80211_is_assoc_req(fc))
  599. IWL_DEBUG_TX(priv, "Sending ASSOC frame\n");
  600. else if (ieee80211_is_reassoc_req(fc))
  601. IWL_DEBUG_TX(priv, "Sending REASSOC frame\n");
  602. #endif
  603. /* drop all data frame if we are not associated */
  604. if (ieee80211_is_data(fc) &&
  605. (!iwl_is_monitor_mode(priv)) && /* packet injection */
  606. (!iwl_is_associated(priv) ||
  607. ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
  608. IWL_DEBUG_DROP(priv, "Dropping - !iwl_is_associated\n");
  609. goto drop_unlock;
  610. }
  611. spin_unlock_irqrestore(&priv->lock, flags);
  612. hdr_len = ieee80211_hdrlen(fc);
  613. /* Find (or create) index into station table for destination station */
  614. sta_id = iwl_get_sta_id(priv, hdr);
  615. if (sta_id == IWL_INVALID_STATION) {
  616. IWL_DEBUG_DROP(priv, "Dropping - INVALID STATION: %pM\n",
  617. hdr->addr1);
  618. goto drop;
  619. }
  620. IWL_DEBUG_RATE(priv, "station Id %d\n", sta_id);
  621. if (ieee80211_is_data_qos(fc)) {
  622. qc = ieee80211_get_qos_ctl(hdr);
  623. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  624. seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
  625. IEEE80211_SCTL_SEQ;
  626. hdr->seq_ctrl = cpu_to_le16(seq_number) |
  627. (hdr->seq_ctrl &
  628. cpu_to_le16(IEEE80211_SCTL_FRAG));
  629. seq_number += 0x10;
  630. }
  631. /* Descriptor for chosen Tx queue */
  632. txq = &priv->txq[txq_id];
  633. q = &txq->q;
  634. spin_lock_irqsave(&priv->lock, flags);
  635. idx = get_cmd_index(q, q->write_ptr, 0);
  636. /* Set up driver data for this TFD */
  637. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  638. txq->txb[q->write_ptr].skb[0] = skb;
  639. /* Init first empty entry in queue's array of Tx/cmd buffers */
  640. out_cmd = txq->cmd[idx];
  641. tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
  642. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  643. memset(tx, 0, sizeof(*tx));
  644. /*
  645. * Set up the Tx-command (not MAC!) header.
  646. * Store the chosen Tx queue and TFD index within the sequence field;
  647. * after Tx, uCode's Tx response will return this value so driver can
  648. * locate the frame within the tx queue and do post-tx processing.
  649. */
  650. out_cmd->hdr.cmd = REPLY_TX;
  651. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  652. INDEX_TO_SEQ(q->write_ptr)));
  653. /* Copy MAC header from skb into command buffer */
  654. memcpy(tx->hdr, hdr, hdr_len);
  655. if (info->control.hw_key)
  656. iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, sta_id);
  657. /* TODO need this for burst mode later on */
  658. iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
  659. /* set is_hcca to 0; it probably will never be implemented */
  660. iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
  661. /* Total # bytes to be transmitted */
  662. len = (u16)skb->len;
  663. tx->len = cpu_to_le16(len);
  664. tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
  665. tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
  666. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  667. txq->need_update = 1;
  668. if (qc)
  669. priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
  670. } else {
  671. wait_write_ptr = 1;
  672. txq->need_update = 0;
  673. }
  674. IWL_DEBUG_TX(priv, "sequence nr = 0X%x \n",
  675. le16_to_cpu(out_cmd->hdr.sequence));
  676. IWL_DEBUG_TX(priv, "tx_flags = 0X%x \n", le32_to_cpu(tx->tx_flags));
  677. iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
  678. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
  679. ieee80211_hdrlen(fc));
  680. /*
  681. * Use the first empty entry in this queue's command buffer array
  682. * to contain the Tx command and MAC header concatenated together
  683. * (payload data will be in another buffer).
  684. * Size of this varies, due to varying MAC header length.
  685. * If end is not dword aligned, we'll have 2 extra bytes at the end
  686. * of the MAC header (device reads on dword boundaries).
  687. * We'll tell device about this padding later.
  688. */
  689. len = sizeof(struct iwl3945_tx_cmd) +
  690. sizeof(struct iwl_cmd_header) + hdr_len;
  691. len_org = len;
  692. len = (len + 3) & ~3;
  693. if (len_org != len)
  694. len_org = 1;
  695. else
  696. len_org = 0;
  697. /* Physical address of this Tx command's header (not MAC header!),
  698. * within command buffer array. */
  699. txcmd_phys = pci_map_single(priv->pci_dev, &out_cmd->hdr,
  700. len, PCI_DMA_TODEVICE);
  701. /* we do not map meta data ... so we can safely access address to
  702. * provide to unmap command*/
  703. pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
  704. pci_unmap_len_set(&out_cmd->meta, len, len);
  705. /* Add buffer containing Tx command and MAC(!) header to TFD's
  706. * first entry */
  707. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  708. txcmd_phys, len, 1, 0);
  709. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  710. * if any (802.11 null frames have no payload). */
  711. len = skb->len - hdr_len;
  712. if (len) {
  713. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  714. len, PCI_DMA_TODEVICE);
  715. priv->cfg->ops->lib->txq_attach_buf_to_tfd(priv, txq,
  716. phys_addr, len,
  717. 0, U32_PAD(len));
  718. }
  719. /* Tell device the write index *just past* this latest filled TFD */
  720. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  721. rc = iwl_txq_update_write_ptr(priv, txq);
  722. spin_unlock_irqrestore(&priv->lock, flags);
  723. if (rc)
  724. return rc;
  725. if ((iwl_queue_space(q) < q->high_mark)
  726. && priv->mac80211_registered) {
  727. if (wait_write_ptr) {
  728. spin_lock_irqsave(&priv->lock, flags);
  729. txq->need_update = 1;
  730. iwl_txq_update_write_ptr(priv, txq);
  731. spin_unlock_irqrestore(&priv->lock, flags);
  732. }
  733. iwl_stop_queue(priv, skb_get_queue_mapping(skb));
  734. }
  735. return 0;
  736. drop_unlock:
  737. spin_unlock_irqrestore(&priv->lock, flags);
  738. drop:
  739. return -1;
  740. }
  741. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  742. #include "iwl-spectrum.h"
  743. #define BEACON_TIME_MASK_LOW 0x00FFFFFF
  744. #define BEACON_TIME_MASK_HIGH 0xFF000000
  745. #define TIME_UNIT 1024
  746. /*
  747. * extended beacon time format
  748. * time in usec will be changed into a 32-bit value in 8:24 format
  749. * the high 1 byte is the beacon counts
  750. * the lower 3 bytes is the time in usec within one beacon interval
  751. */
  752. static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
  753. {
  754. u32 quot;
  755. u32 rem;
  756. u32 interval = beacon_interval * 1024;
  757. if (!interval || !usec)
  758. return 0;
  759. quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
  760. rem = (usec % interval) & BEACON_TIME_MASK_LOW;
  761. return (quot << 24) + rem;
  762. }
  763. /* base is usually what we get from ucode with each received frame,
  764. * the same as HW timer counter counting down
  765. */
  766. static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
  767. {
  768. u32 base_low = base & BEACON_TIME_MASK_LOW;
  769. u32 addon_low = addon & BEACON_TIME_MASK_LOW;
  770. u32 interval = beacon_interval * TIME_UNIT;
  771. u32 res = (base & BEACON_TIME_MASK_HIGH) +
  772. (addon & BEACON_TIME_MASK_HIGH);
  773. if (base_low > addon_low)
  774. res += base_low - addon_low;
  775. else if (base_low < addon_low) {
  776. res += interval + base_low - addon_low;
  777. res += (1 << 24);
  778. } else
  779. res += (1 << 24);
  780. return cpu_to_le32(res);
  781. }
  782. static int iwl3945_get_measurement(struct iwl_priv *priv,
  783. struct ieee80211_measurement_params *params,
  784. u8 type)
  785. {
  786. struct iwl_spectrum_cmd spectrum;
  787. struct iwl_rx_packet *res;
  788. struct iwl_host_cmd cmd = {
  789. .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
  790. .data = (void *)&spectrum,
  791. .meta.flags = CMD_WANT_SKB,
  792. };
  793. u32 add_time = le64_to_cpu(params->start_time);
  794. int rc;
  795. int spectrum_resp_status;
  796. int duration = le16_to_cpu(params->duration);
  797. if (iwl_is_associated(priv))
  798. add_time =
  799. iwl3945_usecs_to_beacons(
  800. le64_to_cpu(params->start_time) - priv->last_tsf,
  801. le16_to_cpu(priv->rxon_timing.beacon_interval));
  802. memset(&spectrum, 0, sizeof(spectrum));
  803. spectrum.channel_count = cpu_to_le16(1);
  804. spectrum.flags =
  805. RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
  806. spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
  807. cmd.len = sizeof(spectrum);
  808. spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
  809. if (iwl_is_associated(priv))
  810. spectrum.start_time =
  811. iwl3945_add_beacon_time(priv->last_beacon_time,
  812. add_time,
  813. le16_to_cpu(priv->rxon_timing.beacon_interval));
  814. else
  815. spectrum.start_time = 0;
  816. spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
  817. spectrum.channels[0].channel = params->channel;
  818. spectrum.channels[0].type = type;
  819. if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
  820. spectrum.flags |= RXON_FLG_BAND_24G_MSK |
  821. RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
  822. rc = iwl_send_cmd_sync(priv, &cmd);
  823. if (rc)
  824. return rc;
  825. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  826. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  827. IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
  828. rc = -EIO;
  829. }
  830. spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
  831. switch (spectrum_resp_status) {
  832. case 0: /* Command will be handled */
  833. if (res->u.spectrum.id != 0xff) {
  834. IWL_DEBUG_INFO(priv, "Replaced existing measurement: %d\n",
  835. res->u.spectrum.id);
  836. priv->measurement_status &= ~MEASUREMENT_READY;
  837. }
  838. priv->measurement_status |= MEASUREMENT_ACTIVE;
  839. rc = 0;
  840. break;
  841. case 1: /* Command will not be handled */
  842. rc = -EAGAIN;
  843. break;
  844. }
  845. dev_kfree_skb_any(cmd.meta.u.skb);
  846. return rc;
  847. }
  848. #endif
  849. static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
  850. struct iwl_rx_mem_buffer *rxb)
  851. {
  852. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  853. struct iwl_alive_resp *palive;
  854. struct delayed_work *pwork;
  855. palive = &pkt->u.alive_frame;
  856. IWL_DEBUG_INFO(priv, "Alive ucode status 0x%08X revision "
  857. "0x%01X 0x%01X\n",
  858. palive->is_valid, palive->ver_type,
  859. palive->ver_subtype);
  860. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  861. IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
  862. memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
  863. sizeof(struct iwl_alive_resp));
  864. pwork = &priv->init_alive_start;
  865. } else {
  866. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  867. memcpy(&priv->card_alive, &pkt->u.alive_frame,
  868. sizeof(struct iwl_alive_resp));
  869. pwork = &priv->alive_start;
  870. iwl3945_disable_events(priv);
  871. }
  872. /* We delay the ALIVE response by 5ms to
  873. * give the HW RF Kill time to activate... */
  874. if (palive->is_valid == UCODE_VALID_OK)
  875. queue_delayed_work(priv->workqueue, pwork,
  876. msecs_to_jiffies(5));
  877. else
  878. IWL_WARN(priv, "uCode did not respond OK.\n");
  879. }
  880. static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
  881. struct iwl_rx_mem_buffer *rxb)
  882. {
  883. #ifdef CONFIG_IWLWIFI_DEBUG
  884. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  885. #endif
  886. IWL_DEBUG_RX(priv, "Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
  887. return;
  888. }
  889. static void iwl3945_bg_beacon_update(struct work_struct *work)
  890. {
  891. struct iwl_priv *priv =
  892. container_of(work, struct iwl_priv, beacon_update);
  893. struct sk_buff *beacon;
  894. /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
  895. beacon = ieee80211_beacon_get(priv->hw, priv->vif);
  896. if (!beacon) {
  897. IWL_ERR(priv, "update beacon failed\n");
  898. return;
  899. }
  900. mutex_lock(&priv->mutex);
  901. /* new beacon skb is allocated every time; dispose previous.*/
  902. if (priv->ibss_beacon)
  903. dev_kfree_skb(priv->ibss_beacon);
  904. priv->ibss_beacon = beacon;
  905. mutex_unlock(&priv->mutex);
  906. iwl3945_send_beacon_cmd(priv);
  907. }
  908. static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
  909. struct iwl_rx_mem_buffer *rxb)
  910. {
  911. #ifdef CONFIG_IWLWIFI_DEBUG
  912. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  913. struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
  914. u8 rate = beacon->beacon_notify_hdr.rate;
  915. IWL_DEBUG_RX(priv, "beacon status %x retries %d iss %d "
  916. "tsf %d %d rate %d\n",
  917. le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
  918. beacon->beacon_notify_hdr.failure_frame,
  919. le32_to_cpu(beacon->ibss_mgr_status),
  920. le32_to_cpu(beacon->high_tsf),
  921. le32_to_cpu(beacon->low_tsf), rate);
  922. #endif
  923. if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
  924. (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
  925. queue_work(priv->workqueue, &priv->beacon_update);
  926. }
  927. /* Handle notification from uCode that card's power state is changing
  928. * due to software, hardware, or critical temperature RFKILL */
  929. static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
  930. struct iwl_rx_mem_buffer *rxb)
  931. {
  932. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  933. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  934. unsigned long status = priv->status;
  935. IWL_DEBUG_RF_KILL(priv, "Card state received: HW:%s SW:%s\n",
  936. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  937. (flags & SW_CARD_DISABLED) ? "Kill" : "On");
  938. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  939. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  940. if (flags & HW_CARD_DISABLED)
  941. set_bit(STATUS_RF_KILL_HW, &priv->status);
  942. else
  943. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  944. if (flags & SW_CARD_DISABLED)
  945. set_bit(STATUS_RF_KILL_SW, &priv->status);
  946. else
  947. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  948. iwl_scan_cancel(priv);
  949. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  950. test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
  951. (test_bit(STATUS_RF_KILL_SW, &status) !=
  952. test_bit(STATUS_RF_KILL_SW, &priv->status)))
  953. queue_work(priv->workqueue, &priv->rf_kill);
  954. else
  955. wake_up_interruptible(&priv->wait_command_queue);
  956. }
  957. /**
  958. * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
  959. *
  960. * Setup the RX handlers for each of the reply types sent from the uCode
  961. * to the host.
  962. *
  963. * This function chains into the hardware specific files for them to setup
  964. * any hardware specific handlers as well.
  965. */
  966. static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
  967. {
  968. priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
  969. priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
  970. priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
  971. priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
  972. priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
  973. priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  974. iwl_rx_pm_debug_statistics_notif;
  975. priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
  976. /*
  977. * The same handler is used for both the REPLY to a discrete
  978. * statistics request from the host as well as for the periodic
  979. * statistics notifications (after received beacons) from the uCode.
  980. */
  981. priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
  982. priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
  983. iwl_setup_spectrum_handlers(priv);
  984. iwl_setup_rx_scan_handlers(priv);
  985. priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
  986. /* Set up hardware specific Rx handlers */
  987. iwl3945_hw_rx_handler_setup(priv);
  988. }
  989. /************************** RX-FUNCTIONS ****************************/
  990. /*
  991. * Rx theory of operation
  992. *
  993. * The host allocates 32 DMA target addresses and passes the host address
  994. * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
  995. * 0 to 31
  996. *
  997. * Rx Queue Indexes
  998. * The host/firmware share two index registers for managing the Rx buffers.
  999. *
  1000. * The READ index maps to the first position that the firmware may be writing
  1001. * to -- the driver can read up to (but not including) this position and get
  1002. * good data.
  1003. * The READ index is managed by the firmware once the card is enabled.
  1004. *
  1005. * The WRITE index maps to the last position the driver has read from -- the
  1006. * position preceding WRITE is the last slot the firmware can place a packet.
  1007. *
  1008. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  1009. * WRITE = READ.
  1010. *
  1011. * During initialization, the host sets up the READ queue position to the first
  1012. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  1013. *
  1014. * When the firmware places a packet in a buffer, it will advance the READ index
  1015. * and fire the RX interrupt. The driver can then query the READ index and
  1016. * process as many packets as possible, moving the WRITE index forward as it
  1017. * resets the Rx queue buffers with new memory.
  1018. *
  1019. * The management in the driver is as follows:
  1020. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  1021. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  1022. * to replenish the iwl->rxq->rx_free.
  1023. * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
  1024. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  1025. * 'processed' and 'read' driver indexes as well)
  1026. * + A received packet is processed and handed to the kernel network stack,
  1027. * detached from the iwl->rxq. The driver 'processed' index is updated.
  1028. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  1029. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  1030. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  1031. * were enough free buffers and RX_STALLED is set it is cleared.
  1032. *
  1033. *
  1034. * Driver sequence:
  1035. *
  1036. * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
  1037. * iwl3945_rx_queue_restock
  1038. * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
  1039. * queue, updates firmware pointers, and updates
  1040. * the WRITE index. If insufficient rx_free buffers
  1041. * are available, schedules iwl3945_rx_replenish
  1042. *
  1043. * -- enable interrupts --
  1044. * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
  1045. * READ INDEX, detaching the SKB from the pool.
  1046. * Moves the packet buffer from queue to rx_used.
  1047. * Calls iwl3945_rx_queue_restock to refill any empty
  1048. * slots.
  1049. * ...
  1050. *
  1051. */
  1052. /**
  1053. * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  1054. */
  1055. static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
  1056. dma_addr_t dma_addr)
  1057. {
  1058. return cpu_to_le32((u32)dma_addr);
  1059. }
  1060. /**
  1061. * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
  1062. *
  1063. * If there are slots in the RX queue that need to be restocked,
  1064. * and we have free pre-allocated buffers, fill the ranks as much
  1065. * as we can, pulling from rx_free.
  1066. *
  1067. * This moves the 'write' index forward to catch up with 'processed', and
  1068. * also updates the memory address in the firmware to reference the new
  1069. * target buffer.
  1070. */
  1071. static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
  1072. {
  1073. struct iwl_rx_queue *rxq = &priv->rxq;
  1074. struct list_head *element;
  1075. struct iwl_rx_mem_buffer *rxb;
  1076. unsigned long flags;
  1077. int write, rc;
  1078. spin_lock_irqsave(&rxq->lock, flags);
  1079. write = rxq->write & ~0x7;
  1080. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  1081. /* Get next free Rx buffer, remove from free list */
  1082. element = rxq->rx_free.next;
  1083. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  1084. list_del(element);
  1085. /* Point to Rx buffer via next RBD in circular buffer */
  1086. rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
  1087. rxq->queue[rxq->write] = rxb;
  1088. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  1089. rxq->free_count--;
  1090. }
  1091. spin_unlock_irqrestore(&rxq->lock, flags);
  1092. /* If the pre-allocated buffer pool is dropping low, schedule to
  1093. * refill it */
  1094. if (rxq->free_count <= RX_LOW_WATERMARK)
  1095. queue_work(priv->workqueue, &priv->rx_replenish);
  1096. /* If we've added more space for the firmware to place data, tell it.
  1097. * Increment device's write pointer in multiples of 8. */
  1098. if ((write != (rxq->write & ~0x7))
  1099. || (abs(rxq->write - rxq->read) > 7)) {
  1100. spin_lock_irqsave(&rxq->lock, flags);
  1101. rxq->need_update = 1;
  1102. spin_unlock_irqrestore(&rxq->lock, flags);
  1103. rc = iwl_rx_queue_update_write_ptr(priv, rxq);
  1104. if (rc)
  1105. return rc;
  1106. }
  1107. return 0;
  1108. }
  1109. /**
  1110. * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
  1111. *
  1112. * When moving to rx_free an SKB is allocated for the slot.
  1113. *
  1114. * Also restock the Rx queue via iwl3945_rx_queue_restock.
  1115. * This is called as a scheduled work item (except for during initialization)
  1116. */
  1117. static void iwl3945_rx_allocate(struct iwl_priv *priv)
  1118. {
  1119. struct iwl_rx_queue *rxq = &priv->rxq;
  1120. struct list_head *element;
  1121. struct iwl_rx_mem_buffer *rxb;
  1122. unsigned long flags;
  1123. while (1) {
  1124. spin_lock_irqsave(&rxq->lock, flags);
  1125. if (list_empty(&rxq->rx_used)) {
  1126. spin_unlock_irqrestore(&rxq->lock, flags);
  1127. return;
  1128. }
  1129. element = rxq->rx_used.next;
  1130. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  1131. list_del(element);
  1132. spin_unlock_irqrestore(&rxq->lock, flags);
  1133. /* Alloc a new receive buffer */
  1134. rxb->skb =
  1135. alloc_skb(priv->hw_params.rx_buf_size,
  1136. GFP_KERNEL);
  1137. if (!rxb->skb) {
  1138. if (net_ratelimit())
  1139. IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
  1140. /* We don't reschedule replenish work here -- we will
  1141. * call the restock method and if it still needs
  1142. * more buffers it will schedule replenish */
  1143. break;
  1144. }
  1145. /* If radiotap head is required, reserve some headroom here.
  1146. * The physical head count is a variable rx_stats->phy_count.
  1147. * We reserve 4 bytes here. Plus these extra bytes, the
  1148. * headroom of the physical head should be enough for the
  1149. * radiotap head that iwl3945 supported. See iwl3945_rt.
  1150. */
  1151. skb_reserve(rxb->skb, 4);
  1152. /* Get physical address of RB/SKB */
  1153. rxb->real_dma_addr = pci_map_single(priv->pci_dev,
  1154. rxb->skb->data,
  1155. priv->hw_params.rx_buf_size,
  1156. PCI_DMA_FROMDEVICE);
  1157. spin_lock_irqsave(&rxq->lock, flags);
  1158. list_add_tail(&rxb->list, &rxq->rx_free);
  1159. priv->alloc_rxb_skb++;
  1160. rxq->free_count++;
  1161. spin_unlock_irqrestore(&rxq->lock, flags);
  1162. }
  1163. }
  1164. void iwl3945_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  1165. {
  1166. unsigned long flags;
  1167. int i;
  1168. spin_lock_irqsave(&rxq->lock, flags);
  1169. INIT_LIST_HEAD(&rxq->rx_free);
  1170. INIT_LIST_HEAD(&rxq->rx_used);
  1171. /* Fill the rx_used queue with _all_ of the Rx buffers */
  1172. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  1173. /* In the reset function, these buffers may have been allocated
  1174. * to an SKB, so we need to unmap and free potential storage */
  1175. if (rxq->pool[i].skb != NULL) {
  1176. pci_unmap_single(priv->pci_dev,
  1177. rxq->pool[i].real_dma_addr,
  1178. priv->hw_params.rx_buf_size,
  1179. PCI_DMA_FROMDEVICE);
  1180. priv->alloc_rxb_skb--;
  1181. dev_kfree_skb(rxq->pool[i].skb);
  1182. rxq->pool[i].skb = NULL;
  1183. }
  1184. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  1185. }
  1186. /* Set us so that we have processed and used all buffers, but have
  1187. * not restocked the Rx queue with fresh buffers */
  1188. rxq->read = rxq->write = 0;
  1189. rxq->free_count = 0;
  1190. spin_unlock_irqrestore(&rxq->lock, flags);
  1191. }
  1192. void iwl3945_rx_replenish(void *data)
  1193. {
  1194. struct iwl_priv *priv = data;
  1195. unsigned long flags;
  1196. iwl3945_rx_allocate(priv);
  1197. spin_lock_irqsave(&priv->lock, flags);
  1198. iwl3945_rx_queue_restock(priv);
  1199. spin_unlock_irqrestore(&priv->lock, flags);
  1200. }
  1201. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  1202. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  1203. * This free routine walks the list of POOL entries and if SKB is set to
  1204. * non NULL it is unmapped and freed
  1205. */
  1206. static void iwl3945_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  1207. {
  1208. int i;
  1209. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  1210. if (rxq->pool[i].skb != NULL) {
  1211. pci_unmap_single(priv->pci_dev,
  1212. rxq->pool[i].real_dma_addr,
  1213. priv->hw_params.rx_buf_size,
  1214. PCI_DMA_FROMDEVICE);
  1215. dev_kfree_skb(rxq->pool[i].skb);
  1216. }
  1217. }
  1218. pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  1219. rxq->dma_addr);
  1220. pci_free_consistent(priv->pci_dev, sizeof(struct iwl_rb_status),
  1221. rxq->rb_stts, rxq->rb_stts_dma);
  1222. rxq->bd = NULL;
  1223. rxq->rb_stts = NULL;
  1224. }
  1225. /* Convert linear signal-to-noise ratio into dB */
  1226. static u8 ratio2dB[100] = {
  1227. /* 0 1 2 3 4 5 6 7 8 9 */
  1228. 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
  1229. 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
  1230. 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
  1231. 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
  1232. 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
  1233. 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
  1234. 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
  1235. 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
  1236. 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
  1237. 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
  1238. };
  1239. /* Calculates a relative dB value from a ratio of linear
  1240. * (i.e. not dB) signal levels.
  1241. * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
  1242. int iwl3945_calc_db_from_ratio(int sig_ratio)
  1243. {
  1244. /* 1000:1 or higher just report as 60 dB */
  1245. if (sig_ratio >= 1000)
  1246. return 60;
  1247. /* 100:1 or higher, divide by 10 and use table,
  1248. * add 20 dB to make up for divide by 10 */
  1249. if (sig_ratio >= 100)
  1250. return 20 + (int)ratio2dB[sig_ratio/10];
  1251. /* We shouldn't see this */
  1252. if (sig_ratio < 1)
  1253. return 0;
  1254. /* Use table for ratios 1:1 - 99:1 */
  1255. return (int)ratio2dB[sig_ratio];
  1256. }
  1257. #define PERFECT_RSSI (-20) /* dBm */
  1258. #define WORST_RSSI (-95) /* dBm */
  1259. #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
  1260. /* Calculate an indication of rx signal quality (a percentage, not dBm!).
  1261. * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
  1262. * about formulas used below. */
  1263. int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
  1264. {
  1265. int sig_qual;
  1266. int degradation = PERFECT_RSSI - rssi_dbm;
  1267. /* If we get a noise measurement, use signal-to-noise ratio (SNR)
  1268. * as indicator; formula is (signal dbm - noise dbm).
  1269. * SNR at or above 40 is a great signal (100%).
  1270. * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
  1271. * Weakest usable signal is usually 10 - 15 dB SNR. */
  1272. if (noise_dbm) {
  1273. if (rssi_dbm - noise_dbm >= 40)
  1274. return 100;
  1275. else if (rssi_dbm < noise_dbm)
  1276. return 0;
  1277. sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
  1278. /* Else use just the signal level.
  1279. * This formula is a least squares fit of data points collected and
  1280. * compared with a reference system that had a percentage (%) display
  1281. * for signal quality. */
  1282. } else
  1283. sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
  1284. (15 * RSSI_RANGE + 62 * degradation)) /
  1285. (RSSI_RANGE * RSSI_RANGE);
  1286. if (sig_qual > 100)
  1287. sig_qual = 100;
  1288. else if (sig_qual < 1)
  1289. sig_qual = 0;
  1290. return sig_qual;
  1291. }
  1292. /**
  1293. * iwl3945_rx_handle - Main entry function for receiving responses from uCode
  1294. *
  1295. * Uses the priv->rx_handlers callback function array to invoke
  1296. * the appropriate handlers, including command responses,
  1297. * frame-received notifications, and other notifications.
  1298. */
  1299. static void iwl3945_rx_handle(struct iwl_priv *priv)
  1300. {
  1301. struct iwl_rx_mem_buffer *rxb;
  1302. struct iwl_rx_packet *pkt;
  1303. struct iwl_rx_queue *rxq = &priv->rxq;
  1304. u32 r, i;
  1305. int reclaim;
  1306. unsigned long flags;
  1307. u8 fill_rx = 0;
  1308. u32 count = 8;
  1309. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  1310. * buffer that the driver may process (last buffer filled by ucode). */
  1311. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  1312. i = rxq->read;
  1313. if (iwl_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
  1314. fill_rx = 1;
  1315. /* Rx interrupt, but nothing sent from uCode */
  1316. if (i == r)
  1317. IWL_DEBUG(priv, IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
  1318. while (i != r) {
  1319. rxb = rxq->queue[i];
  1320. /* If an RXB doesn't have a Rx queue slot associated with it,
  1321. * then a bug has been introduced in the queue refilling
  1322. * routines -- catch it here */
  1323. BUG_ON(rxb == NULL);
  1324. rxq->queue[i] = NULL;
  1325. pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
  1326. priv->hw_params.rx_buf_size,
  1327. PCI_DMA_FROMDEVICE);
  1328. pkt = (struct iwl_rx_packet *)rxb->skb->data;
  1329. /* Reclaim a command buffer only if this packet is a response
  1330. * to a (driver-originated) command.
  1331. * If the packet (e.g. Rx frame) originated from uCode,
  1332. * there is no command buffer to reclaim.
  1333. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  1334. * but apparently a few don't get set; catch them here. */
  1335. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  1336. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  1337. (pkt->hdr.cmd != REPLY_TX);
  1338. /* Based on type of command response or notification,
  1339. * handle those that need handling via function in
  1340. * rx_handlers table. See iwl3945_setup_rx_handlers() */
  1341. if (priv->rx_handlers[pkt->hdr.cmd]) {
  1342. IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
  1343. "r = %d, i = %d, %s, 0x%02x\n", r, i,
  1344. get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  1345. priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
  1346. priv->isr_stats.rx_handlers[pkt->hdr.cmd]++;
  1347. } else {
  1348. /* No handling needed */
  1349. IWL_DEBUG(priv, IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
  1350. "r %d i %d No handler needed for %s, 0x%02x\n",
  1351. r, i, get_cmd_string(pkt->hdr.cmd),
  1352. pkt->hdr.cmd);
  1353. }
  1354. if (reclaim) {
  1355. /* Invoke any callbacks, transfer the skb to caller, and
  1356. * fire off the (possibly) blocking iwl_send_cmd()
  1357. * as we reclaim the driver command queue */
  1358. if (rxb && rxb->skb)
  1359. iwl_tx_cmd_complete(priv, rxb);
  1360. else
  1361. IWL_WARN(priv, "Claim null rxb?\n");
  1362. }
  1363. /* For now we just don't re-use anything. We can tweak this
  1364. * later to try and re-use notification packets and SKBs that
  1365. * fail to Rx correctly */
  1366. if (rxb->skb != NULL) {
  1367. priv->alloc_rxb_skb--;
  1368. dev_kfree_skb_any(rxb->skb);
  1369. rxb->skb = NULL;
  1370. }
  1371. spin_lock_irqsave(&rxq->lock, flags);
  1372. list_add_tail(&rxb->list, &priv->rxq.rx_used);
  1373. spin_unlock_irqrestore(&rxq->lock, flags);
  1374. i = (i + 1) & RX_QUEUE_MASK;
  1375. /* If there are a lot of unused frames,
  1376. * restock the Rx queue so ucode won't assert. */
  1377. if (fill_rx) {
  1378. count++;
  1379. if (count >= 8) {
  1380. priv->rxq.read = i;
  1381. iwl3945_rx_queue_restock(priv);
  1382. count = 0;
  1383. }
  1384. }
  1385. }
  1386. /* Backtrack one entry */
  1387. priv->rxq.read = i;
  1388. iwl3945_rx_queue_restock(priv);
  1389. }
  1390. /* call this function to flush any scheduled tasklet */
  1391. static inline void iwl_synchronize_irq(struct iwl_priv *priv)
  1392. {
  1393. /* wait to make sure we flush pending tasklet*/
  1394. synchronize_irq(priv->pci_dev->irq);
  1395. tasklet_kill(&priv->irq_tasklet);
  1396. }
  1397. static const char *desc_lookup(int i)
  1398. {
  1399. switch (i) {
  1400. case 1:
  1401. return "FAIL";
  1402. case 2:
  1403. return "BAD_PARAM";
  1404. case 3:
  1405. return "BAD_CHECKSUM";
  1406. case 4:
  1407. return "NMI_INTERRUPT";
  1408. case 5:
  1409. return "SYSASSERT";
  1410. case 6:
  1411. return "FATAL_ERROR";
  1412. }
  1413. return "UNKNOWN";
  1414. }
  1415. #define ERROR_START_OFFSET (1 * sizeof(u32))
  1416. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  1417. static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
  1418. {
  1419. u32 i;
  1420. u32 desc, time, count, base, data1;
  1421. u32 blink1, blink2, ilink1, ilink2;
  1422. int rc;
  1423. base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
  1424. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  1425. IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
  1426. return;
  1427. }
  1428. rc = iwl_grab_nic_access(priv);
  1429. if (rc) {
  1430. IWL_WARN(priv, "Can not read from adapter at this time.\n");
  1431. return;
  1432. }
  1433. count = iwl_read_targ_mem(priv, base);
  1434. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  1435. IWL_ERR(priv, "Start IWL Error Log Dump:\n");
  1436. IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
  1437. priv->status, count);
  1438. }
  1439. IWL_ERR(priv, "Desc Time asrtPC blink2 "
  1440. "ilink1 nmiPC Line\n");
  1441. for (i = ERROR_START_OFFSET;
  1442. i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
  1443. i += ERROR_ELEM_SIZE) {
  1444. desc = iwl_read_targ_mem(priv, base + i);
  1445. time =
  1446. iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
  1447. blink1 =
  1448. iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
  1449. blink2 =
  1450. iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
  1451. ilink1 =
  1452. iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
  1453. ilink2 =
  1454. iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
  1455. data1 =
  1456. iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
  1457. IWL_ERR(priv,
  1458. "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
  1459. desc_lookup(desc), desc, time, blink1, blink2,
  1460. ilink1, ilink2, data1);
  1461. }
  1462. iwl_release_nic_access(priv);
  1463. }
  1464. #define EVENT_START_OFFSET (6 * sizeof(u32))
  1465. /**
  1466. * iwl3945_print_event_log - Dump error event log to syslog
  1467. *
  1468. * NOTE: Must be called with iwl_grab_nic_access() already obtained!
  1469. */
  1470. static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
  1471. u32 num_events, u32 mode)
  1472. {
  1473. u32 i;
  1474. u32 base; /* SRAM byte address of event log header */
  1475. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  1476. u32 ptr; /* SRAM byte address of log data */
  1477. u32 ev, time, data; /* event log data */
  1478. if (num_events == 0)
  1479. return;
  1480. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  1481. if (mode == 0)
  1482. event_size = 2 * sizeof(u32);
  1483. else
  1484. event_size = 3 * sizeof(u32);
  1485. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  1486. /* "time" is actually "data" for mode 0 (no timestamp).
  1487. * place event id # at far right for easier visual parsing. */
  1488. for (i = 0; i < num_events; i++) {
  1489. ev = iwl_read_targ_mem(priv, ptr);
  1490. ptr += sizeof(u32);
  1491. time = iwl_read_targ_mem(priv, ptr);
  1492. ptr += sizeof(u32);
  1493. if (mode == 0) {
  1494. /* data, ev */
  1495. IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
  1496. } else {
  1497. data = iwl_read_targ_mem(priv, ptr);
  1498. ptr += sizeof(u32);
  1499. IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
  1500. }
  1501. }
  1502. }
  1503. static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
  1504. {
  1505. int rc;
  1506. u32 base; /* SRAM byte address of event log header */
  1507. u32 capacity; /* event log capacity in # entries */
  1508. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  1509. u32 num_wraps; /* # times uCode wrapped to top of log */
  1510. u32 next_entry; /* index of next entry to be written by uCode */
  1511. u32 size; /* # entries that we'll print */
  1512. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  1513. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  1514. IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
  1515. return;
  1516. }
  1517. rc = iwl_grab_nic_access(priv);
  1518. if (rc) {
  1519. IWL_WARN(priv, "Can not read from adapter at this time.\n");
  1520. return;
  1521. }
  1522. /* event log header */
  1523. capacity = iwl_read_targ_mem(priv, base);
  1524. mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
  1525. num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
  1526. next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
  1527. size = num_wraps ? capacity : next_entry;
  1528. /* bail out if nothing in log */
  1529. if (size == 0) {
  1530. IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
  1531. iwl_release_nic_access(priv);
  1532. return;
  1533. }
  1534. IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
  1535. size, num_wraps);
  1536. /* if uCode has wrapped back to top of log, start at the oldest entry,
  1537. * i.e the next one that uCode would fill. */
  1538. if (num_wraps)
  1539. iwl3945_print_event_log(priv, next_entry,
  1540. capacity - next_entry, mode);
  1541. /* (then/else) start at top of log */
  1542. iwl3945_print_event_log(priv, 0, next_entry, mode);
  1543. iwl_release_nic_access(priv);
  1544. }
  1545. static void iwl3945_irq_tasklet(struct iwl_priv *priv)
  1546. {
  1547. u32 inta, handled = 0;
  1548. u32 inta_fh;
  1549. unsigned long flags;
  1550. #ifdef CONFIG_IWLWIFI_DEBUG
  1551. u32 inta_mask;
  1552. #endif
  1553. spin_lock_irqsave(&priv->lock, flags);
  1554. /* Ack/clear/reset pending uCode interrupts.
  1555. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  1556. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  1557. inta = iwl_read32(priv, CSR_INT);
  1558. iwl_write32(priv, CSR_INT, inta);
  1559. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  1560. * Any new interrupts that happen after this, either while we're
  1561. * in this tasklet, or later, will show up in next ISR/tasklet. */
  1562. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  1563. iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
  1564. #ifdef CONFIG_IWLWIFI_DEBUG
  1565. if (priv->debug_level & IWL_DL_ISR) {
  1566. /* just for debug */
  1567. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  1568. IWL_DEBUG_ISR(priv, "inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  1569. inta, inta_mask, inta_fh);
  1570. }
  1571. #endif
  1572. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  1573. * atomic, make sure that inta covers all the interrupts that
  1574. * we've discovered, even if FH interrupt came in just after
  1575. * reading CSR_INT. */
  1576. if (inta_fh & CSR39_FH_INT_RX_MASK)
  1577. inta |= CSR_INT_BIT_FH_RX;
  1578. if (inta_fh & CSR39_FH_INT_TX_MASK)
  1579. inta |= CSR_INT_BIT_FH_TX;
  1580. /* Now service all interrupt bits discovered above. */
  1581. if (inta & CSR_INT_BIT_HW_ERR) {
  1582. IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
  1583. /* Tell the device to stop sending interrupts */
  1584. iwl_disable_interrupts(priv);
  1585. priv->isr_stats.hw++;
  1586. iwl_irq_handle_error(priv);
  1587. handled |= CSR_INT_BIT_HW_ERR;
  1588. spin_unlock_irqrestore(&priv->lock, flags);
  1589. return;
  1590. }
  1591. #ifdef CONFIG_IWLWIFI_DEBUG
  1592. if (priv->debug_level & (IWL_DL_ISR)) {
  1593. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  1594. if (inta & CSR_INT_BIT_SCD) {
  1595. IWL_DEBUG_ISR(priv, "Scheduler finished to transmit "
  1596. "the frame/frames.\n");
  1597. priv->isr_stats.sch++;
  1598. }
  1599. /* Alive notification via Rx interrupt will do the real work */
  1600. if (inta & CSR_INT_BIT_ALIVE) {
  1601. IWL_DEBUG_ISR(priv, "Alive interrupt\n");
  1602. priv->isr_stats.alive++;
  1603. }
  1604. }
  1605. #endif
  1606. /* Safely ignore these bits for debug checks below */
  1607. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  1608. /* Error detected by uCode */
  1609. if (inta & CSR_INT_BIT_SW_ERR) {
  1610. IWL_ERR(priv, "Microcode SW error detected. "
  1611. "Restarting 0x%X.\n", inta);
  1612. priv->isr_stats.sw++;
  1613. priv->isr_stats.sw_err = inta;
  1614. iwl_irq_handle_error(priv);
  1615. handled |= CSR_INT_BIT_SW_ERR;
  1616. }
  1617. /* uCode wakes up after power-down sleep */
  1618. if (inta & CSR_INT_BIT_WAKEUP) {
  1619. IWL_DEBUG_ISR(priv, "Wakeup interrupt\n");
  1620. iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
  1621. iwl_txq_update_write_ptr(priv, &priv->txq[0]);
  1622. iwl_txq_update_write_ptr(priv, &priv->txq[1]);
  1623. iwl_txq_update_write_ptr(priv, &priv->txq[2]);
  1624. iwl_txq_update_write_ptr(priv, &priv->txq[3]);
  1625. iwl_txq_update_write_ptr(priv, &priv->txq[4]);
  1626. iwl_txq_update_write_ptr(priv, &priv->txq[5]);
  1627. priv->isr_stats.wakeup++;
  1628. handled |= CSR_INT_BIT_WAKEUP;
  1629. }
  1630. /* All uCode command responses, including Tx command responses,
  1631. * Rx "responses" (frame-received notification), and other
  1632. * notifications from uCode come through here*/
  1633. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  1634. iwl3945_rx_handle(priv);
  1635. priv->isr_stats.rx++;
  1636. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  1637. }
  1638. if (inta & CSR_INT_BIT_FH_TX) {
  1639. IWL_DEBUG_ISR(priv, "Tx interrupt\n");
  1640. priv->isr_stats.tx++;
  1641. iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
  1642. if (!iwl_grab_nic_access(priv)) {
  1643. iwl_write_direct32(priv, FH39_TCSR_CREDIT
  1644. (FH39_SRVC_CHNL), 0x0);
  1645. iwl_release_nic_access(priv);
  1646. }
  1647. handled |= CSR_INT_BIT_FH_TX;
  1648. }
  1649. if (inta & ~handled) {
  1650. IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  1651. priv->isr_stats.unhandled++;
  1652. }
  1653. if (inta & ~CSR_INI_SET_MASK) {
  1654. IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
  1655. inta & ~CSR_INI_SET_MASK);
  1656. IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
  1657. }
  1658. /* Re-enable all interrupts */
  1659. /* only Re-enable if disabled by irq */
  1660. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  1661. iwl_enable_interrupts(priv);
  1662. #ifdef CONFIG_IWLWIFI_DEBUG
  1663. if (priv->debug_level & (IWL_DL_ISR)) {
  1664. inta = iwl_read32(priv, CSR_INT);
  1665. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  1666. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  1667. IWL_DEBUG_ISR(priv, "End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  1668. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  1669. }
  1670. #endif
  1671. spin_unlock_irqrestore(&priv->lock, flags);
  1672. }
  1673. static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
  1674. enum ieee80211_band band,
  1675. u8 is_active, u8 n_probes,
  1676. struct iwl3945_scan_channel *scan_ch)
  1677. {
  1678. const struct ieee80211_channel *channels = NULL;
  1679. const struct ieee80211_supported_band *sband;
  1680. const struct iwl_channel_info *ch_info;
  1681. u16 passive_dwell = 0;
  1682. u16 active_dwell = 0;
  1683. int added, i;
  1684. sband = iwl_get_hw_mode(priv, band);
  1685. if (!sband)
  1686. return 0;
  1687. channels = sband->channels;
  1688. active_dwell = iwl_get_active_dwell_time(priv, band, n_probes);
  1689. passive_dwell = iwl_get_passive_dwell_time(priv, band);
  1690. if (passive_dwell <= active_dwell)
  1691. passive_dwell = active_dwell + 1;
  1692. for (i = 0, added = 0; i < sband->n_channels; i++) {
  1693. if (channels[i].flags & IEEE80211_CHAN_DISABLED)
  1694. continue;
  1695. scan_ch->channel = channels[i].hw_value;
  1696. ch_info = iwl_get_channel_info(priv, band, scan_ch->channel);
  1697. if (!is_channel_valid(ch_info)) {
  1698. IWL_DEBUG_SCAN(priv, "Channel %d is INVALID for this band.\n",
  1699. scan_ch->channel);
  1700. continue;
  1701. }
  1702. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  1703. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  1704. /* If passive , set up for auto-switch
  1705. * and use long active_dwell time.
  1706. */
  1707. if (!is_active || is_channel_passive(ch_info) ||
  1708. (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
  1709. scan_ch->type = 0; /* passive */
  1710. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  1711. scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
  1712. } else {
  1713. scan_ch->type = 1; /* active */
  1714. }
  1715. /* Set direct probe bits. These may be used both for active
  1716. * scan channels (probes gets sent right away),
  1717. * or for passive channels (probes get se sent only after
  1718. * hearing clear Rx packet).*/
  1719. if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
  1720. if (n_probes)
  1721. scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
  1722. } else {
  1723. /* uCode v1 does not allow setting direct probe bits on
  1724. * passive channel. */
  1725. if ((scan_ch->type & 1) && n_probes)
  1726. scan_ch->type |= IWL39_SCAN_PROBE_MASK(n_probes);
  1727. }
  1728. /* Set txpower levels to defaults */
  1729. scan_ch->tpc.dsp_atten = 110;
  1730. /* scan_pwr_info->tpc.dsp_atten; */
  1731. /*scan_pwr_info->tpc.tx_gain; */
  1732. if (band == IEEE80211_BAND_5GHZ)
  1733. scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
  1734. else {
  1735. scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
  1736. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  1737. * power level:
  1738. * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
  1739. */
  1740. }
  1741. IWL_DEBUG_SCAN(priv, "Scanning %d [%s %d]\n",
  1742. scan_ch->channel,
  1743. (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
  1744. (scan_ch->type & 1) ?
  1745. active_dwell : passive_dwell);
  1746. scan_ch++;
  1747. added++;
  1748. }
  1749. IWL_DEBUG_SCAN(priv, "total channels to scan %d \n", added);
  1750. return added;
  1751. }
  1752. static void iwl3945_init_hw_rates(struct iwl_priv *priv,
  1753. struct ieee80211_rate *rates)
  1754. {
  1755. int i;
  1756. for (i = 0; i < IWL_RATE_COUNT; i++) {
  1757. rates[i].bitrate = iwl3945_rates[i].ieee * 5;
  1758. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  1759. rates[i].hw_value_short = i;
  1760. rates[i].flags = 0;
  1761. if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
  1762. /*
  1763. * If CCK != 1M then set short preamble rate flag.
  1764. */
  1765. rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
  1766. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  1767. }
  1768. }
  1769. }
  1770. /******************************************************************************
  1771. *
  1772. * uCode download functions
  1773. *
  1774. ******************************************************************************/
  1775. static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
  1776. {
  1777. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
  1778. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
  1779. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  1780. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
  1781. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  1782. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
  1783. }
  1784. /**
  1785. * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
  1786. * looking at all data.
  1787. */
  1788. static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
  1789. {
  1790. u32 val;
  1791. u32 save_len = len;
  1792. int rc = 0;
  1793. u32 errcnt;
  1794. IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
  1795. rc = iwl_grab_nic_access(priv);
  1796. if (rc)
  1797. return rc;
  1798. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  1799. IWL39_RTC_INST_LOWER_BOUND);
  1800. errcnt = 0;
  1801. for (; len > 0; len -= sizeof(u32), image++) {
  1802. /* read data comes through single port, auto-incr addr */
  1803. /* NOTE: Use the debugless read so we don't flood kernel log
  1804. * if IWL_DL_IO is set */
  1805. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  1806. if (val != le32_to_cpu(*image)) {
  1807. IWL_ERR(priv, "uCode INST section is invalid at "
  1808. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  1809. save_len - len, val, le32_to_cpu(*image));
  1810. rc = -EIO;
  1811. errcnt++;
  1812. if (errcnt >= 20)
  1813. break;
  1814. }
  1815. }
  1816. iwl_release_nic_access(priv);
  1817. if (!errcnt)
  1818. IWL_DEBUG_INFO(priv,
  1819. "ucode image in INSTRUCTION memory is good\n");
  1820. return rc;
  1821. }
  1822. /**
  1823. * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
  1824. * using sample data 100 bytes apart. If these sample points are good,
  1825. * it's a pretty good bet that everything between them is good, too.
  1826. */
  1827. static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
  1828. {
  1829. u32 val;
  1830. int rc = 0;
  1831. u32 errcnt = 0;
  1832. u32 i;
  1833. IWL_DEBUG_INFO(priv, "ucode inst image size is %u\n", len);
  1834. rc = iwl_grab_nic_access(priv);
  1835. if (rc)
  1836. return rc;
  1837. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  1838. /* read data comes through single port, auto-incr addr */
  1839. /* NOTE: Use the debugless read so we don't flood kernel log
  1840. * if IWL_DL_IO is set */
  1841. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  1842. i + IWL39_RTC_INST_LOWER_BOUND);
  1843. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  1844. if (val != le32_to_cpu(*image)) {
  1845. #if 0 /* Enable this if you want to see details */
  1846. IWL_ERR(priv, "uCode INST section is invalid at "
  1847. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  1848. i, val, *image);
  1849. #endif
  1850. rc = -EIO;
  1851. errcnt++;
  1852. if (errcnt >= 3)
  1853. break;
  1854. }
  1855. }
  1856. iwl_release_nic_access(priv);
  1857. return rc;
  1858. }
  1859. /**
  1860. * iwl3945_verify_ucode - determine which instruction image is in SRAM,
  1861. * and verify its contents
  1862. */
  1863. static int iwl3945_verify_ucode(struct iwl_priv *priv)
  1864. {
  1865. __le32 *image;
  1866. u32 len;
  1867. int rc = 0;
  1868. /* Try bootstrap */
  1869. image = (__le32 *)priv->ucode_boot.v_addr;
  1870. len = priv->ucode_boot.len;
  1871. rc = iwl3945_verify_inst_sparse(priv, image, len);
  1872. if (rc == 0) {
  1873. IWL_DEBUG_INFO(priv, "Bootstrap uCode is good in inst SRAM\n");
  1874. return 0;
  1875. }
  1876. /* Try initialize */
  1877. image = (__le32 *)priv->ucode_init.v_addr;
  1878. len = priv->ucode_init.len;
  1879. rc = iwl3945_verify_inst_sparse(priv, image, len);
  1880. if (rc == 0) {
  1881. IWL_DEBUG_INFO(priv, "Initialize uCode is good in inst SRAM\n");
  1882. return 0;
  1883. }
  1884. /* Try runtime/protocol */
  1885. image = (__le32 *)priv->ucode_code.v_addr;
  1886. len = priv->ucode_code.len;
  1887. rc = iwl3945_verify_inst_sparse(priv, image, len);
  1888. if (rc == 0) {
  1889. IWL_DEBUG_INFO(priv, "Runtime uCode is good in inst SRAM\n");
  1890. return 0;
  1891. }
  1892. IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
  1893. /* Since nothing seems to match, show first several data entries in
  1894. * instruction SRAM, so maybe visual inspection will give a clue.
  1895. * Selection of bootstrap image (vs. other images) is arbitrary. */
  1896. image = (__le32 *)priv->ucode_boot.v_addr;
  1897. len = priv->ucode_boot.len;
  1898. rc = iwl3945_verify_inst_full(priv, image, len);
  1899. return rc;
  1900. }
  1901. static void iwl3945_nic_start(struct iwl_priv *priv)
  1902. {
  1903. /* Remove all resets to allow NIC to operate */
  1904. iwl_write32(priv, CSR_RESET, 0);
  1905. }
  1906. /**
  1907. * iwl3945_read_ucode - Read uCode images from disk file.
  1908. *
  1909. * Copy into buffers for card to fetch via bus-mastering
  1910. */
  1911. static int iwl3945_read_ucode(struct iwl_priv *priv)
  1912. {
  1913. struct iwl_ucode *ucode;
  1914. int ret = -EINVAL, index;
  1915. const struct firmware *ucode_raw;
  1916. /* firmware file name contains uCode/driver compatibility version */
  1917. const char *name_pre = priv->cfg->fw_name_pre;
  1918. const unsigned int api_max = priv->cfg->ucode_api_max;
  1919. const unsigned int api_min = priv->cfg->ucode_api_min;
  1920. char buf[25];
  1921. u8 *src;
  1922. size_t len;
  1923. u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
  1924. /* Ask kernel firmware_class module to get the boot firmware off disk.
  1925. * request_firmware() is synchronous, file is in memory on return. */
  1926. for (index = api_max; index >= api_min; index--) {
  1927. sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
  1928. ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
  1929. if (ret < 0) {
  1930. IWL_ERR(priv, "%s firmware file req failed: %d\n",
  1931. buf, ret);
  1932. if (ret == -ENOENT)
  1933. continue;
  1934. else
  1935. goto error;
  1936. } else {
  1937. if (index < api_max)
  1938. IWL_ERR(priv, "Loaded firmware %s, "
  1939. "which is deprecated. "
  1940. " Please use API v%u instead.\n",
  1941. buf, api_max);
  1942. IWL_DEBUG_INFO(priv, "Got firmware '%s' file "
  1943. "(%zd bytes) from disk\n",
  1944. buf, ucode_raw->size);
  1945. break;
  1946. }
  1947. }
  1948. if (ret < 0)
  1949. goto error;
  1950. /* Make sure that we got at least our header! */
  1951. if (ucode_raw->size < sizeof(*ucode)) {
  1952. IWL_ERR(priv, "File size way too small!\n");
  1953. ret = -EINVAL;
  1954. goto err_release;
  1955. }
  1956. /* Data from ucode file: header followed by uCode images */
  1957. ucode = (void *)ucode_raw->data;
  1958. priv->ucode_ver = le32_to_cpu(ucode->ver);
  1959. api_ver = IWL_UCODE_API(priv->ucode_ver);
  1960. inst_size = le32_to_cpu(ucode->inst_size);
  1961. data_size = le32_to_cpu(ucode->data_size);
  1962. init_size = le32_to_cpu(ucode->init_size);
  1963. init_data_size = le32_to_cpu(ucode->init_data_size);
  1964. boot_size = le32_to_cpu(ucode->boot_size);
  1965. /* api_ver should match the api version forming part of the
  1966. * firmware filename ... but we don't check for that and only rely
  1967. * on the API version read from firmware header from here on forward */
  1968. if (api_ver < api_min || api_ver > api_max) {
  1969. IWL_ERR(priv, "Driver unable to support your firmware API. "
  1970. "Driver supports v%u, firmware is v%u.\n",
  1971. api_max, api_ver);
  1972. priv->ucode_ver = 0;
  1973. ret = -EINVAL;
  1974. goto err_release;
  1975. }
  1976. if (api_ver != api_max)
  1977. IWL_ERR(priv, "Firmware has old API version. Expected %u, "
  1978. "got %u. New firmware can be obtained "
  1979. "from http://www.intellinuxwireless.org.\n",
  1980. api_max, api_ver);
  1981. IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
  1982. IWL_UCODE_MAJOR(priv->ucode_ver),
  1983. IWL_UCODE_MINOR(priv->ucode_ver),
  1984. IWL_UCODE_API(priv->ucode_ver),
  1985. IWL_UCODE_SERIAL(priv->ucode_ver));
  1986. IWL_DEBUG_INFO(priv, "f/w package hdr ucode version raw = 0x%x\n",
  1987. priv->ucode_ver);
  1988. IWL_DEBUG_INFO(priv, "f/w package hdr runtime inst size = %u\n",
  1989. inst_size);
  1990. IWL_DEBUG_INFO(priv, "f/w package hdr runtime data size = %u\n",
  1991. data_size);
  1992. IWL_DEBUG_INFO(priv, "f/w package hdr init inst size = %u\n",
  1993. init_size);
  1994. IWL_DEBUG_INFO(priv, "f/w package hdr init data size = %u\n",
  1995. init_data_size);
  1996. IWL_DEBUG_INFO(priv, "f/w package hdr boot inst size = %u\n",
  1997. boot_size);
  1998. /* Verify size of file vs. image size info in file's header */
  1999. if (ucode_raw->size < sizeof(*ucode) +
  2000. inst_size + data_size + init_size +
  2001. init_data_size + boot_size) {
  2002. IWL_DEBUG_INFO(priv, "uCode file size %zd too small\n",
  2003. ucode_raw->size);
  2004. ret = -EINVAL;
  2005. goto err_release;
  2006. }
  2007. /* Verify that uCode images will fit in card's SRAM */
  2008. if (inst_size > IWL39_MAX_INST_SIZE) {
  2009. IWL_DEBUG_INFO(priv, "uCode instr len %d too large to fit in\n",
  2010. inst_size);
  2011. ret = -EINVAL;
  2012. goto err_release;
  2013. }
  2014. if (data_size > IWL39_MAX_DATA_SIZE) {
  2015. IWL_DEBUG_INFO(priv, "uCode data len %d too large to fit in\n",
  2016. data_size);
  2017. ret = -EINVAL;
  2018. goto err_release;
  2019. }
  2020. if (init_size > IWL39_MAX_INST_SIZE) {
  2021. IWL_DEBUG_INFO(priv,
  2022. "uCode init instr len %d too large to fit in\n",
  2023. init_size);
  2024. ret = -EINVAL;
  2025. goto err_release;
  2026. }
  2027. if (init_data_size > IWL39_MAX_DATA_SIZE) {
  2028. IWL_DEBUG_INFO(priv,
  2029. "uCode init data len %d too large to fit in\n",
  2030. init_data_size);
  2031. ret = -EINVAL;
  2032. goto err_release;
  2033. }
  2034. if (boot_size > IWL39_MAX_BSM_SIZE) {
  2035. IWL_DEBUG_INFO(priv,
  2036. "uCode boot instr len %d too large to fit in\n",
  2037. boot_size);
  2038. ret = -EINVAL;
  2039. goto err_release;
  2040. }
  2041. /* Allocate ucode buffers for card's bus-master loading ... */
  2042. /* Runtime instructions and 2 copies of data:
  2043. * 1) unmodified from disk
  2044. * 2) backup cache for save/restore during power-downs */
  2045. priv->ucode_code.len = inst_size;
  2046. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
  2047. priv->ucode_data.len = data_size;
  2048. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
  2049. priv->ucode_data_backup.len = data_size;
  2050. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  2051. if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
  2052. !priv->ucode_data_backup.v_addr)
  2053. goto err_pci_alloc;
  2054. /* Initialization instructions and data */
  2055. if (init_size && init_data_size) {
  2056. priv->ucode_init.len = init_size;
  2057. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
  2058. priv->ucode_init_data.len = init_data_size;
  2059. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  2060. if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
  2061. goto err_pci_alloc;
  2062. }
  2063. /* Bootstrap (instructions only, no data) */
  2064. if (boot_size) {
  2065. priv->ucode_boot.len = boot_size;
  2066. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
  2067. if (!priv->ucode_boot.v_addr)
  2068. goto err_pci_alloc;
  2069. }
  2070. /* Copy images into buffers for card's bus-master reads ... */
  2071. /* Runtime instructions (first block of data in file) */
  2072. src = &ucode->data[0];
  2073. len = priv->ucode_code.len;
  2074. IWL_DEBUG_INFO(priv,
  2075. "Copying (but not loading) uCode instr len %zd\n", len);
  2076. memcpy(priv->ucode_code.v_addr, src, len);
  2077. IWL_DEBUG_INFO(priv, "uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  2078. priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
  2079. /* Runtime data (2nd block)
  2080. * NOTE: Copy into backup buffer will be done in iwl3945_up() */
  2081. src = &ucode->data[inst_size];
  2082. len = priv->ucode_data.len;
  2083. IWL_DEBUG_INFO(priv,
  2084. "Copying (but not loading) uCode data len %zd\n", len);
  2085. memcpy(priv->ucode_data.v_addr, src, len);
  2086. memcpy(priv->ucode_data_backup.v_addr, src, len);
  2087. /* Initialization instructions (3rd block) */
  2088. if (init_size) {
  2089. src = &ucode->data[inst_size + data_size];
  2090. len = priv->ucode_init.len;
  2091. IWL_DEBUG_INFO(priv,
  2092. "Copying (but not loading) init instr len %zd\n", len);
  2093. memcpy(priv->ucode_init.v_addr, src, len);
  2094. }
  2095. /* Initialization data (4th block) */
  2096. if (init_data_size) {
  2097. src = &ucode->data[inst_size + data_size + init_size];
  2098. len = priv->ucode_init_data.len;
  2099. IWL_DEBUG_INFO(priv,
  2100. "Copying (but not loading) init data len %zd\n", len);
  2101. memcpy(priv->ucode_init_data.v_addr, src, len);
  2102. }
  2103. /* Bootstrap instructions (5th block) */
  2104. src = &ucode->data[inst_size + data_size + init_size + init_data_size];
  2105. len = priv->ucode_boot.len;
  2106. IWL_DEBUG_INFO(priv,
  2107. "Copying (but not loading) boot instr len %zd\n", len);
  2108. memcpy(priv->ucode_boot.v_addr, src, len);
  2109. /* We have our copies now, allow OS release its copies */
  2110. release_firmware(ucode_raw);
  2111. return 0;
  2112. err_pci_alloc:
  2113. IWL_ERR(priv, "failed to allocate pci memory\n");
  2114. ret = -ENOMEM;
  2115. iwl3945_dealloc_ucode_pci(priv);
  2116. err_release:
  2117. release_firmware(ucode_raw);
  2118. error:
  2119. return ret;
  2120. }
  2121. /**
  2122. * iwl3945_set_ucode_ptrs - Set uCode address location
  2123. *
  2124. * Tell initialization uCode where to find runtime uCode.
  2125. *
  2126. * BSM registers initially contain pointers to initialization uCode.
  2127. * We need to replace them to load runtime uCode inst and data,
  2128. * and to save runtime data when powering down.
  2129. */
  2130. static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
  2131. {
  2132. dma_addr_t pinst;
  2133. dma_addr_t pdata;
  2134. int rc = 0;
  2135. unsigned long flags;
  2136. /* bits 31:0 for 3945 */
  2137. pinst = priv->ucode_code.p_addr;
  2138. pdata = priv->ucode_data_backup.p_addr;
  2139. spin_lock_irqsave(&priv->lock, flags);
  2140. rc = iwl_grab_nic_access(priv);
  2141. if (rc) {
  2142. spin_unlock_irqrestore(&priv->lock, flags);
  2143. return rc;
  2144. }
  2145. /* Tell bootstrap uCode where to find image to load */
  2146. iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  2147. iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  2148. iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
  2149. priv->ucode_data.len);
  2150. /* Inst byte count must be last to set up, bit 31 signals uCode
  2151. * that all new ptr/size info is in place */
  2152. iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
  2153. priv->ucode_code.len | BSM_DRAM_INST_LOAD);
  2154. iwl_release_nic_access(priv);
  2155. spin_unlock_irqrestore(&priv->lock, flags);
  2156. IWL_DEBUG_INFO(priv, "Runtime uCode pointers are set.\n");
  2157. return rc;
  2158. }
  2159. /**
  2160. * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
  2161. *
  2162. * Called after REPLY_ALIVE notification received from "initialize" uCode.
  2163. *
  2164. * Tell "initialize" uCode to go ahead and load the runtime uCode.
  2165. */
  2166. static void iwl3945_init_alive_start(struct iwl_priv *priv)
  2167. {
  2168. /* Check alive response for "valid" sign from uCode */
  2169. if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
  2170. /* We had an error bringing up the hardware, so take it
  2171. * all the way back down so we can try again */
  2172. IWL_DEBUG_INFO(priv, "Initialize Alive failed.\n");
  2173. goto restart;
  2174. }
  2175. /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
  2176. * This is a paranoid check, because we would not have gotten the
  2177. * "initialize" alive if code weren't properly loaded. */
  2178. if (iwl3945_verify_ucode(priv)) {
  2179. /* Runtime instruction load was bad;
  2180. * take it all the way back down so we can try again */
  2181. IWL_DEBUG_INFO(priv, "Bad \"initialize\" uCode load.\n");
  2182. goto restart;
  2183. }
  2184. /* Send pointers to protocol/runtime uCode image ... init code will
  2185. * load and launch runtime uCode, which will send us another "Alive"
  2186. * notification. */
  2187. IWL_DEBUG_INFO(priv, "Initialization Alive received.\n");
  2188. if (iwl3945_set_ucode_ptrs(priv)) {
  2189. /* Runtime instruction load won't happen;
  2190. * take it all the way back down so we can try again */
  2191. IWL_DEBUG_INFO(priv, "Couldn't set up uCode pointers.\n");
  2192. goto restart;
  2193. }
  2194. return;
  2195. restart:
  2196. queue_work(priv->workqueue, &priv->restart);
  2197. }
  2198. /**
  2199. * iwl3945_alive_start - called after REPLY_ALIVE notification received
  2200. * from protocol/runtime uCode (initialization uCode's
  2201. * Alive gets handled by iwl3945_init_alive_start()).
  2202. */
  2203. static void iwl3945_alive_start(struct iwl_priv *priv)
  2204. {
  2205. int rc = 0;
  2206. int thermal_spin = 0;
  2207. u32 rfkill;
  2208. IWL_DEBUG_INFO(priv, "Runtime Alive received.\n");
  2209. if (priv->card_alive.is_valid != UCODE_VALID_OK) {
  2210. /* We had an error bringing up the hardware, so take it
  2211. * all the way back down so we can try again */
  2212. IWL_DEBUG_INFO(priv, "Alive failed.\n");
  2213. goto restart;
  2214. }
  2215. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  2216. * This is a paranoid check, because we would not have gotten the
  2217. * "runtime" alive if code weren't properly loaded. */
  2218. if (iwl3945_verify_ucode(priv)) {
  2219. /* Runtime instruction load was bad;
  2220. * take it all the way back down so we can try again */
  2221. IWL_DEBUG_INFO(priv, "Bad runtime uCode load.\n");
  2222. goto restart;
  2223. }
  2224. priv->cfg->ops->smgmt->clear_station_table(priv);
  2225. rc = iwl_grab_nic_access(priv);
  2226. if (rc) {
  2227. IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
  2228. return;
  2229. }
  2230. rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
  2231. IWL_DEBUG_INFO(priv, "RFKILL status: 0x%x\n", rfkill);
  2232. iwl_release_nic_access(priv);
  2233. if (rfkill & 0x1) {
  2234. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2235. /* if RFKILL is not on, then wait for thermal
  2236. * sensor in adapter to kick in */
  2237. while (iwl3945_hw_get_temperature(priv) == 0) {
  2238. thermal_spin++;
  2239. udelay(10);
  2240. }
  2241. if (thermal_spin)
  2242. IWL_DEBUG_INFO(priv, "Thermal calibration took %dus\n",
  2243. thermal_spin * 10);
  2244. } else
  2245. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2246. /* After the ALIVE response, we can send commands to 3945 uCode */
  2247. set_bit(STATUS_ALIVE, &priv->status);
  2248. if (iwl_is_rfkill(priv))
  2249. return;
  2250. ieee80211_wake_queues(priv->hw);
  2251. priv->active_rate = priv->rates_mask;
  2252. priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
  2253. iwl_power_update_mode(priv, false);
  2254. if (iwl_is_associated(priv)) {
  2255. struct iwl3945_rxon_cmd *active_rxon =
  2256. (struct iwl3945_rxon_cmd *)(&priv->active_rxon);
  2257. memcpy(&priv->staging_rxon, &priv->active_rxon,
  2258. sizeof(priv->staging_rxon));
  2259. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2260. } else {
  2261. /* Initialize our rx_config data */
  2262. iwl_connection_init_rx_config(priv, priv->iw_mode);
  2263. }
  2264. /* Configure Bluetooth device coexistence support */
  2265. iwl_send_bt_config(priv);
  2266. /* Configure the adapter for unassociated operation */
  2267. iwlcore_commit_rxon(priv);
  2268. iwl3945_reg_txpower_periodic(priv);
  2269. iwl3945_led_register(priv);
  2270. IWL_DEBUG_INFO(priv, "ALIVE processing complete.\n");
  2271. set_bit(STATUS_READY, &priv->status);
  2272. wake_up_interruptible(&priv->wait_command_queue);
  2273. /* reassociate for ADHOC mode */
  2274. if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
  2275. struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
  2276. priv->vif);
  2277. if (beacon)
  2278. iwl_mac_beacon_update(priv->hw, beacon);
  2279. }
  2280. if (test_and_clear_bit(STATUS_MODE_PENDING, &priv->status))
  2281. iwl_set_mode(priv, priv->iw_mode);
  2282. return;
  2283. restart:
  2284. queue_work(priv->workqueue, &priv->restart);
  2285. }
  2286. static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
  2287. static void __iwl3945_down(struct iwl_priv *priv)
  2288. {
  2289. unsigned long flags;
  2290. int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
  2291. struct ieee80211_conf *conf = NULL;
  2292. IWL_DEBUG_INFO(priv, DRV_NAME " is going down\n");
  2293. conf = ieee80211_get_hw_conf(priv->hw);
  2294. if (!exit_pending)
  2295. set_bit(STATUS_EXIT_PENDING, &priv->status);
  2296. iwl3945_led_unregister(priv);
  2297. priv->cfg->ops->smgmt->clear_station_table(priv);
  2298. /* Unblock any waiting calls */
  2299. wake_up_interruptible_all(&priv->wait_command_queue);
  2300. /* Wipe out the EXIT_PENDING status bit if we are not actually
  2301. * exiting the module */
  2302. if (!exit_pending)
  2303. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  2304. /* stop and reset the on-board processor */
  2305. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  2306. /* tell the device to stop sending interrupts */
  2307. spin_lock_irqsave(&priv->lock, flags);
  2308. iwl_disable_interrupts(priv);
  2309. spin_unlock_irqrestore(&priv->lock, flags);
  2310. iwl_synchronize_irq(priv);
  2311. if (priv->mac80211_registered)
  2312. ieee80211_stop_queues(priv->hw);
  2313. /* If we have not previously called iwl3945_init() then
  2314. * clear all bits but the RF Kill bits and return */
  2315. if (!iwl_is_init(priv)) {
  2316. priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  2317. STATUS_RF_KILL_HW |
  2318. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  2319. STATUS_RF_KILL_SW |
  2320. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  2321. STATUS_GEO_CONFIGURED |
  2322. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  2323. STATUS_EXIT_PENDING;
  2324. goto exit;
  2325. }
  2326. /* ...otherwise clear out all the status bits but the RF Kill
  2327. * bits and continue taking the NIC down. */
  2328. priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  2329. STATUS_RF_KILL_HW |
  2330. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  2331. STATUS_RF_KILL_SW |
  2332. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  2333. STATUS_GEO_CONFIGURED |
  2334. test_bit(STATUS_FW_ERROR, &priv->status) <<
  2335. STATUS_FW_ERROR |
  2336. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  2337. STATUS_EXIT_PENDING;
  2338. priv->cfg->ops->lib->apm_ops.reset(priv);
  2339. spin_lock_irqsave(&priv->lock, flags);
  2340. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2341. spin_unlock_irqrestore(&priv->lock, flags);
  2342. iwl3945_hw_txq_ctx_stop(priv);
  2343. iwl3945_hw_rxq_stop(priv);
  2344. spin_lock_irqsave(&priv->lock, flags);
  2345. if (!iwl_grab_nic_access(priv)) {
  2346. iwl_write_prph(priv, APMG_CLK_DIS_REG,
  2347. APMG_CLK_VAL_DMA_CLK_RQT);
  2348. iwl_release_nic_access(priv);
  2349. }
  2350. spin_unlock_irqrestore(&priv->lock, flags);
  2351. udelay(5);
  2352. if (exit_pending)
  2353. priv->cfg->ops->lib->apm_ops.stop(priv);
  2354. else
  2355. priv->cfg->ops->lib->apm_ops.reset(priv);
  2356. exit:
  2357. memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
  2358. if (priv->ibss_beacon)
  2359. dev_kfree_skb(priv->ibss_beacon);
  2360. priv->ibss_beacon = NULL;
  2361. /* clear out any free frames */
  2362. iwl3945_clear_free_frames(priv);
  2363. }
  2364. static void iwl3945_down(struct iwl_priv *priv)
  2365. {
  2366. mutex_lock(&priv->mutex);
  2367. __iwl3945_down(priv);
  2368. mutex_unlock(&priv->mutex);
  2369. iwl3945_cancel_deferred_work(priv);
  2370. }
  2371. #define MAX_HW_RESTARTS 5
  2372. static int __iwl3945_up(struct iwl_priv *priv)
  2373. {
  2374. int rc, i;
  2375. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  2376. IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
  2377. return -EIO;
  2378. }
  2379. if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
  2380. IWL_WARN(priv, "Radio disabled by SW RF kill (module "
  2381. "parameter)\n");
  2382. return -ENODEV;
  2383. }
  2384. if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
  2385. IWL_ERR(priv, "ucode not available for device bring up\n");
  2386. return -EIO;
  2387. }
  2388. /* If platform's RF_KILL switch is NOT set to KILL */
  2389. if (iwl_read32(priv, CSR_GP_CNTRL) &
  2390. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2391. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2392. else {
  2393. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2394. IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
  2395. return -ENODEV;
  2396. }
  2397. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2398. rc = iwl3945_hw_nic_init(priv);
  2399. if (rc) {
  2400. IWL_ERR(priv, "Unable to int nic\n");
  2401. return rc;
  2402. }
  2403. /* make sure rfkill handshake bits are cleared */
  2404. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2405. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  2406. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2407. /* clear (again), then enable host interrupts */
  2408. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  2409. iwl_enable_interrupts(priv);
  2410. /* really make sure rfkill handshake bits are cleared */
  2411. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2412. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2413. /* Copy original ucode data image from disk into backup cache.
  2414. * This will be used to initialize the on-board processor's
  2415. * data SRAM for a clean start when the runtime program first loads. */
  2416. memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
  2417. priv->ucode_data.len);
  2418. /* We return success when we resume from suspend and rf_kill is on. */
  2419. if (test_bit(STATUS_RF_KILL_HW, &priv->status))
  2420. return 0;
  2421. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  2422. priv->cfg->ops->smgmt->clear_station_table(priv);
  2423. /* load bootstrap state machine,
  2424. * load bootstrap program into processor's memory,
  2425. * prepare to load the "initialize" uCode */
  2426. priv->cfg->ops->lib->load_ucode(priv);
  2427. if (rc) {
  2428. IWL_ERR(priv,
  2429. "Unable to set up bootstrap uCode: %d\n", rc);
  2430. continue;
  2431. }
  2432. /* start card; "initialize" will load runtime ucode */
  2433. iwl3945_nic_start(priv);
  2434. IWL_DEBUG_INFO(priv, DRV_NAME " is coming up\n");
  2435. return 0;
  2436. }
  2437. set_bit(STATUS_EXIT_PENDING, &priv->status);
  2438. __iwl3945_down(priv);
  2439. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  2440. /* tried to restart and config the device for as long as our
  2441. * patience could withstand */
  2442. IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
  2443. return -EIO;
  2444. }
  2445. /*****************************************************************************
  2446. *
  2447. * Workqueue callbacks
  2448. *
  2449. *****************************************************************************/
  2450. static void iwl3945_bg_init_alive_start(struct work_struct *data)
  2451. {
  2452. struct iwl_priv *priv =
  2453. container_of(data, struct iwl_priv, init_alive_start.work);
  2454. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2455. return;
  2456. mutex_lock(&priv->mutex);
  2457. iwl3945_init_alive_start(priv);
  2458. mutex_unlock(&priv->mutex);
  2459. }
  2460. static void iwl3945_bg_alive_start(struct work_struct *data)
  2461. {
  2462. struct iwl_priv *priv =
  2463. container_of(data, struct iwl_priv, alive_start.work);
  2464. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2465. return;
  2466. mutex_lock(&priv->mutex);
  2467. iwl3945_alive_start(priv);
  2468. mutex_unlock(&priv->mutex);
  2469. }
  2470. static void iwl3945_rfkill_poll(struct work_struct *data)
  2471. {
  2472. struct iwl_priv *priv =
  2473. container_of(data, struct iwl_priv, rfkill_poll.work);
  2474. unsigned long status = priv->status;
  2475. if (iwl_read32(priv, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  2476. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2477. else
  2478. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2479. if (test_bit(STATUS_RF_KILL_HW, &status) != test_bit(STATUS_RF_KILL_HW, &priv->status))
  2480. queue_work(priv->workqueue, &priv->rf_kill);
  2481. queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
  2482. round_jiffies_relative(2 * HZ));
  2483. }
  2484. #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
  2485. static void iwl3945_bg_request_scan(struct work_struct *data)
  2486. {
  2487. struct iwl_priv *priv =
  2488. container_of(data, struct iwl_priv, request_scan);
  2489. struct iwl_host_cmd cmd = {
  2490. .id = REPLY_SCAN_CMD,
  2491. .len = sizeof(struct iwl3945_scan_cmd),
  2492. .meta.flags = CMD_SIZE_HUGE,
  2493. };
  2494. int rc = 0;
  2495. struct iwl3945_scan_cmd *scan;
  2496. struct ieee80211_conf *conf = NULL;
  2497. u8 n_probes = 0;
  2498. enum ieee80211_band band;
  2499. bool is_active = false;
  2500. conf = ieee80211_get_hw_conf(priv->hw);
  2501. mutex_lock(&priv->mutex);
  2502. if (!iwl_is_ready(priv)) {
  2503. IWL_WARN(priv, "request scan called when driver not ready.\n");
  2504. goto done;
  2505. }
  2506. /* Make sure the scan wasn't canceled before this queued work
  2507. * was given the chance to run... */
  2508. if (!test_bit(STATUS_SCANNING, &priv->status))
  2509. goto done;
  2510. /* This should never be called or scheduled if there is currently
  2511. * a scan active in the hardware. */
  2512. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  2513. IWL_DEBUG_INFO(priv, "Multiple concurrent scan requests "
  2514. "Ignoring second request.\n");
  2515. rc = -EIO;
  2516. goto done;
  2517. }
  2518. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  2519. IWL_DEBUG_SCAN(priv, "Aborting scan due to device shutdown\n");
  2520. goto done;
  2521. }
  2522. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  2523. IWL_DEBUG_HC(priv,
  2524. "Scan request while abort pending. Queuing.\n");
  2525. goto done;
  2526. }
  2527. if (iwl_is_rfkill(priv)) {
  2528. IWL_DEBUG_HC(priv, "Aborting scan due to RF Kill activation\n");
  2529. goto done;
  2530. }
  2531. if (!test_bit(STATUS_READY, &priv->status)) {
  2532. IWL_DEBUG_HC(priv,
  2533. "Scan request while uninitialized. Queuing.\n");
  2534. goto done;
  2535. }
  2536. if (!priv->scan_bands) {
  2537. IWL_DEBUG_HC(priv, "Aborting scan due to no requested bands\n");
  2538. goto done;
  2539. }
  2540. if (!priv->scan) {
  2541. priv->scan = kmalloc(sizeof(struct iwl3945_scan_cmd) +
  2542. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  2543. if (!priv->scan) {
  2544. rc = -ENOMEM;
  2545. goto done;
  2546. }
  2547. }
  2548. scan = priv->scan;
  2549. memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
  2550. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  2551. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  2552. if (iwl_is_associated(priv)) {
  2553. u16 interval = 0;
  2554. u32 extra;
  2555. u32 suspend_time = 100;
  2556. u32 scan_suspend_time = 100;
  2557. unsigned long flags;
  2558. IWL_DEBUG_INFO(priv, "Scanning while associated...\n");
  2559. spin_lock_irqsave(&priv->lock, flags);
  2560. interval = priv->beacon_int;
  2561. spin_unlock_irqrestore(&priv->lock, flags);
  2562. scan->suspend_time = 0;
  2563. scan->max_out_time = cpu_to_le32(200 * 1024);
  2564. if (!interval)
  2565. interval = suspend_time;
  2566. /*
  2567. * suspend time format:
  2568. * 0-19: beacon interval in usec (time before exec.)
  2569. * 20-23: 0
  2570. * 24-31: number of beacons (suspend between channels)
  2571. */
  2572. extra = (suspend_time / interval) << 24;
  2573. scan_suspend_time = 0xFF0FFFFF &
  2574. (extra | ((suspend_time % interval) * 1024));
  2575. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  2576. IWL_DEBUG_SCAN(priv, "suspend_time 0x%X beacon interval %d\n",
  2577. scan_suspend_time, interval);
  2578. }
  2579. if (priv->scan_request->n_ssids) {
  2580. int i, p = 0;
  2581. IWL_DEBUG_SCAN(priv, "Kicking off active scan\n");
  2582. for (i = 0; i < priv->scan_request->n_ssids; i++) {
  2583. /* always does wildcard anyway */
  2584. if (!priv->scan_request->ssids[i].ssid_len)
  2585. continue;
  2586. scan->direct_scan[p].id = WLAN_EID_SSID;
  2587. scan->direct_scan[p].len =
  2588. priv->scan_request->ssids[i].ssid_len;
  2589. memcpy(scan->direct_scan[p].ssid,
  2590. priv->scan_request->ssids[i].ssid,
  2591. priv->scan_request->ssids[i].ssid_len);
  2592. n_probes++;
  2593. p++;
  2594. }
  2595. is_active = true;
  2596. } else
  2597. IWL_DEBUG_SCAN(priv, "Kicking off passive scan.\n");
  2598. /* We don't build a direct scan probe request; the uCode will do
  2599. * that based on the direct_mask added to each channel entry */
  2600. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  2601. scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
  2602. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  2603. /* flags + rate selection */
  2604. if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
  2605. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  2606. scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
  2607. scan->good_CRC_th = 0;
  2608. band = IEEE80211_BAND_2GHZ;
  2609. } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
  2610. scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
  2611. /*
  2612. * If active scaning is requested but a certain channel
  2613. * is marked passive, we can do active scanning if we
  2614. * detect transmissions.
  2615. */
  2616. scan->good_CRC_th = is_active ? IWL_GOOD_CRC_TH : 0;
  2617. band = IEEE80211_BAND_5GHZ;
  2618. } else {
  2619. IWL_WARN(priv, "Invalid scan band count\n");
  2620. goto done;
  2621. }
  2622. scan->tx_cmd.len = cpu_to_le16(
  2623. iwl_fill_probe_req(priv,
  2624. (struct ieee80211_mgmt *)scan->data,
  2625. priv->scan_request->ie,
  2626. priv->scan_request->ie_len,
  2627. IWL_MAX_SCAN_SIZE - sizeof(*scan)));
  2628. /* select Rx antennas */
  2629. scan->flags |= iwl3945_get_antenna_flags(priv);
  2630. if (iwl_is_monitor_mode(priv))
  2631. scan->filter_flags = RXON_FILTER_PROMISC_MSK;
  2632. scan->channel_count =
  2633. iwl3945_get_channels_for_scan(priv, band, is_active, n_probes,
  2634. (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
  2635. if (scan->channel_count == 0) {
  2636. IWL_DEBUG_SCAN(priv, "channel count %d\n", scan->channel_count);
  2637. goto done;
  2638. }
  2639. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  2640. scan->channel_count * sizeof(struct iwl3945_scan_channel);
  2641. cmd.data = scan;
  2642. scan->len = cpu_to_le16(cmd.len);
  2643. set_bit(STATUS_SCAN_HW, &priv->status);
  2644. rc = iwl_send_cmd_sync(priv, &cmd);
  2645. if (rc)
  2646. goto done;
  2647. queue_delayed_work(priv->workqueue, &priv->scan_check,
  2648. IWL_SCAN_CHECK_WATCHDOG);
  2649. mutex_unlock(&priv->mutex);
  2650. return;
  2651. done:
  2652. /* can not perform scan make sure we clear scanning
  2653. * bits from status so next scan request can be performed.
  2654. * if we dont clear scanning status bit here all next scan
  2655. * will fail
  2656. */
  2657. clear_bit(STATUS_SCAN_HW, &priv->status);
  2658. clear_bit(STATUS_SCANNING, &priv->status);
  2659. /* inform mac80211 scan aborted */
  2660. queue_work(priv->workqueue, &priv->scan_completed);
  2661. mutex_unlock(&priv->mutex);
  2662. }
  2663. static void iwl3945_bg_up(struct work_struct *data)
  2664. {
  2665. struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
  2666. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2667. return;
  2668. mutex_lock(&priv->mutex);
  2669. __iwl3945_up(priv);
  2670. mutex_unlock(&priv->mutex);
  2671. iwl_rfkill_set_hw_state(priv);
  2672. }
  2673. static void iwl3945_bg_restart(struct work_struct *data)
  2674. {
  2675. struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
  2676. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2677. return;
  2678. if (test_and_clear_bit(STATUS_FW_ERROR, &priv->status)) {
  2679. mutex_lock(&priv->mutex);
  2680. priv->vif = NULL;
  2681. priv->is_open = 0;
  2682. mutex_unlock(&priv->mutex);
  2683. iwl3945_down(priv);
  2684. ieee80211_restart_hw(priv->hw);
  2685. } else {
  2686. iwl3945_down(priv);
  2687. queue_work(priv->workqueue, &priv->up);
  2688. }
  2689. }
  2690. static void iwl3945_bg_rx_replenish(struct work_struct *data)
  2691. {
  2692. struct iwl_priv *priv =
  2693. container_of(data, struct iwl_priv, rx_replenish);
  2694. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2695. return;
  2696. mutex_lock(&priv->mutex);
  2697. iwl3945_rx_replenish(priv);
  2698. mutex_unlock(&priv->mutex);
  2699. }
  2700. #define IWL_DELAY_NEXT_SCAN (HZ*2)
  2701. void iwl3945_post_associate(struct iwl_priv *priv)
  2702. {
  2703. int rc = 0;
  2704. struct ieee80211_conf *conf = NULL;
  2705. if (priv->iw_mode == NL80211_IFTYPE_AP) {
  2706. IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
  2707. return;
  2708. }
  2709. IWL_DEBUG_ASSOC(priv, "Associated as %d to: %pM\n",
  2710. priv->assoc_id, priv->active_rxon.bssid_addr);
  2711. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2712. return;
  2713. if (!priv->vif || !priv->is_open)
  2714. return;
  2715. iwl_scan_cancel_timeout(priv, 200);
  2716. conf = ieee80211_get_hw_conf(priv->hw);
  2717. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2718. iwlcore_commit_rxon(priv);
  2719. memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
  2720. iwl3945_setup_rxon_timing(priv);
  2721. rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  2722. sizeof(priv->rxon_timing), &priv->rxon_timing);
  2723. if (rc)
  2724. IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
  2725. "Attempting to continue.\n");
  2726. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  2727. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  2728. IWL_DEBUG_ASSOC(priv, "assoc id %d beacon interval %d\n",
  2729. priv->assoc_id, priv->beacon_int);
  2730. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  2731. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  2732. else
  2733. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  2734. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  2735. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
  2736. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  2737. else
  2738. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  2739. if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
  2740. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  2741. }
  2742. iwlcore_commit_rxon(priv);
  2743. switch (priv->iw_mode) {
  2744. case NL80211_IFTYPE_STATION:
  2745. iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
  2746. break;
  2747. case NL80211_IFTYPE_ADHOC:
  2748. priv->assoc_id = 1;
  2749. priv->cfg->ops->smgmt->add_station(priv, priv->bssid, 0, 0, NULL);
  2750. iwl3945_sync_sta(priv, IWL_STA_ID,
  2751. (priv->band == IEEE80211_BAND_5GHZ) ?
  2752. IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
  2753. CMD_ASYNC);
  2754. iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
  2755. iwl3945_send_beacon_cmd(priv);
  2756. break;
  2757. default:
  2758. IWL_ERR(priv, "%s Should not be called in %d mode\n",
  2759. __func__, priv->iw_mode);
  2760. break;
  2761. }
  2762. iwl_activate_qos(priv, 0);
  2763. /* we have just associated, don't start scan too early */
  2764. priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
  2765. }
  2766. /*****************************************************************************
  2767. *
  2768. * mac80211 entry point functions
  2769. *
  2770. *****************************************************************************/
  2771. #define UCODE_READY_TIMEOUT (2 * HZ)
  2772. static int iwl3945_mac_start(struct ieee80211_hw *hw)
  2773. {
  2774. struct iwl_priv *priv = hw->priv;
  2775. int ret;
  2776. IWL_DEBUG_MAC80211(priv, "enter\n");
  2777. /* we should be verifying the device is ready to be opened */
  2778. mutex_lock(&priv->mutex);
  2779. memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
  2780. /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
  2781. * ucode filename and max sizes are card-specific. */
  2782. if (!priv->ucode_code.len) {
  2783. ret = iwl3945_read_ucode(priv);
  2784. if (ret) {
  2785. IWL_ERR(priv, "Could not read microcode: %d\n", ret);
  2786. mutex_unlock(&priv->mutex);
  2787. goto out_release_irq;
  2788. }
  2789. }
  2790. ret = __iwl3945_up(priv);
  2791. mutex_unlock(&priv->mutex);
  2792. iwl_rfkill_set_hw_state(priv);
  2793. if (ret)
  2794. goto out_release_irq;
  2795. IWL_DEBUG_INFO(priv, "Start UP work.\n");
  2796. /* Wait for START_ALIVE from ucode. Otherwise callbacks from
  2797. * mac80211 will not be run successfully. */
  2798. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  2799. test_bit(STATUS_READY, &priv->status),
  2800. UCODE_READY_TIMEOUT);
  2801. if (!ret) {
  2802. if (!test_bit(STATUS_READY, &priv->status)) {
  2803. IWL_ERR(priv,
  2804. "Wait for START_ALIVE timeout after %dms.\n",
  2805. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  2806. ret = -ETIMEDOUT;
  2807. goto out_release_irq;
  2808. }
  2809. }
  2810. /* ucode is running and will send rfkill notifications,
  2811. * no need to poll the killswitch state anymore */
  2812. cancel_delayed_work(&priv->rfkill_poll);
  2813. priv->is_open = 1;
  2814. IWL_DEBUG_MAC80211(priv, "leave\n");
  2815. return 0;
  2816. out_release_irq:
  2817. priv->is_open = 0;
  2818. IWL_DEBUG_MAC80211(priv, "leave - failed\n");
  2819. return ret;
  2820. }
  2821. static void iwl3945_mac_stop(struct ieee80211_hw *hw)
  2822. {
  2823. struct iwl_priv *priv = hw->priv;
  2824. IWL_DEBUG_MAC80211(priv, "enter\n");
  2825. if (!priv->is_open) {
  2826. IWL_DEBUG_MAC80211(priv, "leave - skip\n");
  2827. return;
  2828. }
  2829. priv->is_open = 0;
  2830. if (iwl_is_ready_rf(priv)) {
  2831. /* stop mac, cancel any scan request and clear
  2832. * RXON_FILTER_ASSOC_MSK BIT
  2833. */
  2834. mutex_lock(&priv->mutex);
  2835. iwl_scan_cancel_timeout(priv, 100);
  2836. mutex_unlock(&priv->mutex);
  2837. }
  2838. iwl3945_down(priv);
  2839. flush_workqueue(priv->workqueue);
  2840. /* start polling the killswitch state again */
  2841. queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
  2842. round_jiffies_relative(2 * HZ));
  2843. IWL_DEBUG_MAC80211(priv, "leave\n");
  2844. }
  2845. static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  2846. {
  2847. struct iwl_priv *priv = hw->priv;
  2848. IWL_DEBUG_MAC80211(priv, "enter\n");
  2849. IWL_DEBUG_TX(priv, "dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  2850. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  2851. if (iwl3945_tx_skb(priv, skb))
  2852. dev_kfree_skb_any(skb);
  2853. IWL_DEBUG_MAC80211(priv, "leave\n");
  2854. return NETDEV_TX_OK;
  2855. }
  2856. void iwl3945_config_ap(struct iwl_priv *priv)
  2857. {
  2858. int rc = 0;
  2859. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  2860. return;
  2861. /* The following should be done only at AP bring up */
  2862. if (!(iwl_is_associated(priv))) {
  2863. /* RXON - unassoc (to set timing command) */
  2864. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  2865. iwlcore_commit_rxon(priv);
  2866. /* RXON Timing */
  2867. memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
  2868. iwl3945_setup_rxon_timing(priv);
  2869. rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  2870. sizeof(priv->rxon_timing),
  2871. &priv->rxon_timing);
  2872. if (rc)
  2873. IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
  2874. "Attempting to continue.\n");
  2875. /* FIXME: what should be the assoc_id for AP? */
  2876. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  2877. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  2878. priv->staging_rxon.flags |=
  2879. RXON_FLG_SHORT_PREAMBLE_MSK;
  2880. else
  2881. priv->staging_rxon.flags &=
  2882. ~RXON_FLG_SHORT_PREAMBLE_MSK;
  2883. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  2884. if (priv->assoc_capability &
  2885. WLAN_CAPABILITY_SHORT_SLOT_TIME)
  2886. priv->staging_rxon.flags |=
  2887. RXON_FLG_SHORT_SLOT_MSK;
  2888. else
  2889. priv->staging_rxon.flags &=
  2890. ~RXON_FLG_SHORT_SLOT_MSK;
  2891. if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
  2892. priv->staging_rxon.flags &=
  2893. ~RXON_FLG_SHORT_SLOT_MSK;
  2894. }
  2895. /* restore RXON assoc */
  2896. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  2897. iwlcore_commit_rxon(priv);
  2898. priv->cfg->ops->smgmt->add_station(priv, iwl_bcast_addr, 0, 0, NULL);
  2899. }
  2900. iwl3945_send_beacon_cmd(priv);
  2901. /* FIXME - we need to add code here to detect a totally new
  2902. * configuration, reset the AP, unassoc, rxon timing, assoc,
  2903. * clear sta table, add BCAST sta... */
  2904. }
  2905. static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  2906. struct ieee80211_vif *vif,
  2907. struct ieee80211_sta *sta,
  2908. struct ieee80211_key_conf *key)
  2909. {
  2910. struct iwl_priv *priv = hw->priv;
  2911. const u8 *addr;
  2912. int ret = 0;
  2913. u8 sta_id = IWL_INVALID_STATION;
  2914. u8 static_key;
  2915. IWL_DEBUG_MAC80211(priv, "enter\n");
  2916. if (iwl3945_mod_params.sw_crypto) {
  2917. IWL_DEBUG_MAC80211(priv, "leave - hwcrypto disabled\n");
  2918. return -EOPNOTSUPP;
  2919. }
  2920. addr = sta ? sta->addr : iwl_bcast_addr;
  2921. static_key = !iwl_is_associated(priv);
  2922. if (!static_key) {
  2923. sta_id = priv->cfg->ops->smgmt->find_station(priv, addr);
  2924. if (sta_id == IWL_INVALID_STATION) {
  2925. IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
  2926. addr);
  2927. return -EINVAL;
  2928. }
  2929. }
  2930. mutex_lock(&priv->mutex);
  2931. iwl_scan_cancel_timeout(priv, 100);
  2932. mutex_unlock(&priv->mutex);
  2933. switch (cmd) {
  2934. case SET_KEY:
  2935. if (static_key)
  2936. ret = iwl3945_set_static_key(priv, key);
  2937. else
  2938. ret = iwl3945_set_dynamic_key(priv, key, sta_id);
  2939. IWL_DEBUG_MAC80211(priv, "enable hwcrypto key\n");
  2940. break;
  2941. case DISABLE_KEY:
  2942. if (static_key)
  2943. ret = iwl3945_remove_static_key(priv);
  2944. else
  2945. ret = iwl3945_clear_sta_key_info(priv, sta_id);
  2946. IWL_DEBUG_MAC80211(priv, "disable hwcrypto key\n");
  2947. break;
  2948. default:
  2949. ret = -EINVAL;
  2950. }
  2951. IWL_DEBUG_MAC80211(priv, "leave\n");
  2952. return ret;
  2953. }
  2954. /*****************************************************************************
  2955. *
  2956. * sysfs attributes
  2957. *
  2958. *****************************************************************************/
  2959. #ifdef CONFIG_IWLWIFI_DEBUG
  2960. /*
  2961. * The following adds a new attribute to the sysfs representation
  2962. * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
  2963. * used for controlling the debug level.
  2964. *
  2965. * See the level definitions in iwl for details.
  2966. */
  2967. static ssize_t show_debug_level(struct device *d,
  2968. struct device_attribute *attr, char *buf)
  2969. {
  2970. struct iwl_priv *priv = dev_get_drvdata(d);
  2971. return sprintf(buf, "0x%08X\n", priv->debug_level);
  2972. }
  2973. static ssize_t store_debug_level(struct device *d,
  2974. struct device_attribute *attr,
  2975. const char *buf, size_t count)
  2976. {
  2977. struct iwl_priv *priv = dev_get_drvdata(d);
  2978. unsigned long val;
  2979. int ret;
  2980. ret = strict_strtoul(buf, 0, &val);
  2981. if (ret)
  2982. IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
  2983. else
  2984. priv->debug_level = val;
  2985. return strnlen(buf, count);
  2986. }
  2987. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  2988. show_debug_level, store_debug_level);
  2989. #endif /* CONFIG_IWLWIFI_DEBUG */
  2990. static ssize_t show_temperature(struct device *d,
  2991. struct device_attribute *attr, char *buf)
  2992. {
  2993. struct iwl_priv *priv = dev_get_drvdata(d);
  2994. if (!iwl_is_alive(priv))
  2995. return -EAGAIN;
  2996. return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
  2997. }
  2998. static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
  2999. static ssize_t show_tx_power(struct device *d,
  3000. struct device_attribute *attr, char *buf)
  3001. {
  3002. struct iwl_priv *priv = dev_get_drvdata(d);
  3003. return sprintf(buf, "%d\n", priv->tx_power_user_lmt);
  3004. }
  3005. static ssize_t store_tx_power(struct device *d,
  3006. struct device_attribute *attr,
  3007. const char *buf, size_t count)
  3008. {
  3009. struct iwl_priv *priv = dev_get_drvdata(d);
  3010. char *p = (char *)buf;
  3011. u32 val;
  3012. val = simple_strtoul(p, &p, 10);
  3013. if (p == buf)
  3014. IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
  3015. else
  3016. iwl3945_hw_reg_set_txpower(priv, val);
  3017. return count;
  3018. }
  3019. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
  3020. static ssize_t show_flags(struct device *d,
  3021. struct device_attribute *attr, char *buf)
  3022. {
  3023. struct iwl_priv *priv = dev_get_drvdata(d);
  3024. return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
  3025. }
  3026. static ssize_t store_flags(struct device *d,
  3027. struct device_attribute *attr,
  3028. const char *buf, size_t count)
  3029. {
  3030. struct iwl_priv *priv = dev_get_drvdata(d);
  3031. u32 flags = simple_strtoul(buf, NULL, 0);
  3032. mutex_lock(&priv->mutex);
  3033. if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
  3034. /* Cancel any currently running scans... */
  3035. if (iwl_scan_cancel_timeout(priv, 100))
  3036. IWL_WARN(priv, "Could not cancel scan.\n");
  3037. else {
  3038. IWL_DEBUG_INFO(priv, "Committing rxon.flags = 0x%04X\n",
  3039. flags);
  3040. priv->staging_rxon.flags = cpu_to_le32(flags);
  3041. iwlcore_commit_rxon(priv);
  3042. }
  3043. }
  3044. mutex_unlock(&priv->mutex);
  3045. return count;
  3046. }
  3047. static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
  3048. static ssize_t show_filter_flags(struct device *d,
  3049. struct device_attribute *attr, char *buf)
  3050. {
  3051. struct iwl_priv *priv = dev_get_drvdata(d);
  3052. return sprintf(buf, "0x%04X\n",
  3053. le32_to_cpu(priv->active_rxon.filter_flags));
  3054. }
  3055. static ssize_t store_filter_flags(struct device *d,
  3056. struct device_attribute *attr,
  3057. const char *buf, size_t count)
  3058. {
  3059. struct iwl_priv *priv = dev_get_drvdata(d);
  3060. u32 filter_flags = simple_strtoul(buf, NULL, 0);
  3061. mutex_lock(&priv->mutex);
  3062. if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
  3063. /* Cancel any currently running scans... */
  3064. if (iwl_scan_cancel_timeout(priv, 100))
  3065. IWL_WARN(priv, "Could not cancel scan.\n");
  3066. else {
  3067. IWL_DEBUG_INFO(priv, "Committing rxon.filter_flags = "
  3068. "0x%04X\n", filter_flags);
  3069. priv->staging_rxon.filter_flags =
  3070. cpu_to_le32(filter_flags);
  3071. iwlcore_commit_rxon(priv);
  3072. }
  3073. }
  3074. mutex_unlock(&priv->mutex);
  3075. return count;
  3076. }
  3077. static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
  3078. store_filter_flags);
  3079. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  3080. static ssize_t show_measurement(struct device *d,
  3081. struct device_attribute *attr, char *buf)
  3082. {
  3083. struct iwl_priv *priv = dev_get_drvdata(d);
  3084. struct iwl_spectrum_notification measure_report;
  3085. u32 size = sizeof(measure_report), len = 0, ofs = 0;
  3086. u8 *data = (u8 *)&measure_report;
  3087. unsigned long flags;
  3088. spin_lock_irqsave(&priv->lock, flags);
  3089. if (!(priv->measurement_status & MEASUREMENT_READY)) {
  3090. spin_unlock_irqrestore(&priv->lock, flags);
  3091. return 0;
  3092. }
  3093. memcpy(&measure_report, &priv->measure_report, size);
  3094. priv->measurement_status = 0;
  3095. spin_unlock_irqrestore(&priv->lock, flags);
  3096. while (size && (PAGE_SIZE - len)) {
  3097. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  3098. PAGE_SIZE - len, 1);
  3099. len = strlen(buf);
  3100. if (PAGE_SIZE - len)
  3101. buf[len++] = '\n';
  3102. ofs += 16;
  3103. size -= min(size, 16U);
  3104. }
  3105. return len;
  3106. }
  3107. static ssize_t store_measurement(struct device *d,
  3108. struct device_attribute *attr,
  3109. const char *buf, size_t count)
  3110. {
  3111. struct iwl_priv *priv = dev_get_drvdata(d);
  3112. struct ieee80211_measurement_params params = {
  3113. .channel = le16_to_cpu(priv->active_rxon.channel),
  3114. .start_time = cpu_to_le64(priv->last_tsf),
  3115. .duration = cpu_to_le16(1),
  3116. };
  3117. u8 type = IWL_MEASURE_BASIC;
  3118. u8 buffer[32];
  3119. u8 channel;
  3120. if (count) {
  3121. char *p = buffer;
  3122. strncpy(buffer, buf, min(sizeof(buffer), count));
  3123. channel = simple_strtoul(p, NULL, 0);
  3124. if (channel)
  3125. params.channel = channel;
  3126. p = buffer;
  3127. while (*p && *p != ' ')
  3128. p++;
  3129. if (*p)
  3130. type = simple_strtoul(p + 1, NULL, 0);
  3131. }
  3132. IWL_DEBUG_INFO(priv, "Invoking measurement of type %d on "
  3133. "channel %d (for '%s')\n", type, params.channel, buf);
  3134. iwl3945_get_measurement(priv, &params, type);
  3135. return count;
  3136. }
  3137. static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
  3138. show_measurement, store_measurement);
  3139. #endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
  3140. static ssize_t store_retry_rate(struct device *d,
  3141. struct device_attribute *attr,
  3142. const char *buf, size_t count)
  3143. {
  3144. struct iwl_priv *priv = dev_get_drvdata(d);
  3145. priv->retry_rate = simple_strtoul(buf, NULL, 0);
  3146. if (priv->retry_rate <= 0)
  3147. priv->retry_rate = 1;
  3148. return count;
  3149. }
  3150. static ssize_t show_retry_rate(struct device *d,
  3151. struct device_attribute *attr, char *buf)
  3152. {
  3153. struct iwl_priv *priv = dev_get_drvdata(d);
  3154. return sprintf(buf, "%d", priv->retry_rate);
  3155. }
  3156. static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
  3157. store_retry_rate);
  3158. static ssize_t store_power_level(struct device *d,
  3159. struct device_attribute *attr,
  3160. const char *buf, size_t count)
  3161. {
  3162. struct iwl_priv *priv = dev_get_drvdata(d);
  3163. int ret;
  3164. unsigned long mode;
  3165. mutex_lock(&priv->mutex);
  3166. ret = strict_strtoul(buf, 10, &mode);
  3167. if (ret)
  3168. goto out;
  3169. ret = iwl_power_set_user_mode(priv, mode);
  3170. if (ret) {
  3171. IWL_DEBUG_MAC80211(priv, "failed setting power mode.\n");
  3172. goto out;
  3173. }
  3174. ret = count;
  3175. out:
  3176. mutex_unlock(&priv->mutex);
  3177. return ret;
  3178. }
  3179. static ssize_t show_power_level(struct device *d,
  3180. struct device_attribute *attr, char *buf)
  3181. {
  3182. struct iwl_priv *priv = dev_get_drvdata(d);
  3183. int mode = priv->power_data.user_power_setting;
  3184. int system = priv->power_data.system_power_setting;
  3185. int level = priv->power_data.power_mode;
  3186. char *p = buf;
  3187. switch (system) {
  3188. case IWL_POWER_SYS_AUTO:
  3189. p += sprintf(p, "SYSTEM:auto");
  3190. break;
  3191. case IWL_POWER_SYS_AC:
  3192. p += sprintf(p, "SYSTEM:ac");
  3193. break;
  3194. case IWL_POWER_SYS_BATTERY:
  3195. p += sprintf(p, "SYSTEM:battery");
  3196. break;
  3197. }
  3198. p += sprintf(p, "\tMODE:%s", (mode < IWL_POWER_AUTO) ?
  3199. "fixed" : "auto");
  3200. p += sprintf(p, "\tINDEX:%d", level);
  3201. p += sprintf(p, "\n");
  3202. return p - buf + 1;
  3203. }
  3204. static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR,
  3205. show_power_level, store_power_level);
  3206. #define MAX_WX_STRING 80
  3207. /* Values are in microsecond */
  3208. static const s32 timeout_duration[] = {
  3209. 350000,
  3210. 250000,
  3211. 75000,
  3212. 37000,
  3213. 25000,
  3214. };
  3215. static const s32 period_duration[] = {
  3216. 400000,
  3217. 700000,
  3218. 1000000,
  3219. 1000000,
  3220. 1000000
  3221. };
  3222. static ssize_t show_channels(struct device *d,
  3223. struct device_attribute *attr, char *buf)
  3224. {
  3225. /* all this shit doesn't belong into sysfs anyway */
  3226. return 0;
  3227. }
  3228. static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
  3229. static ssize_t show_statistics(struct device *d,
  3230. struct device_attribute *attr, char *buf)
  3231. {
  3232. struct iwl_priv *priv = dev_get_drvdata(d);
  3233. u32 size = sizeof(struct iwl3945_notif_statistics);
  3234. u32 len = 0, ofs = 0;
  3235. u8 *data = (u8 *)&priv->statistics_39;
  3236. int rc = 0;
  3237. if (!iwl_is_alive(priv))
  3238. return -EAGAIN;
  3239. mutex_lock(&priv->mutex);
  3240. rc = iwl_send_statistics_request(priv, 0);
  3241. mutex_unlock(&priv->mutex);
  3242. if (rc) {
  3243. len = sprintf(buf,
  3244. "Error sending statistics request: 0x%08X\n", rc);
  3245. return len;
  3246. }
  3247. while (size && (PAGE_SIZE - len)) {
  3248. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  3249. PAGE_SIZE - len, 1);
  3250. len = strlen(buf);
  3251. if (PAGE_SIZE - len)
  3252. buf[len++] = '\n';
  3253. ofs += 16;
  3254. size -= min(size, 16U);
  3255. }
  3256. return len;
  3257. }
  3258. static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
  3259. static ssize_t show_antenna(struct device *d,
  3260. struct device_attribute *attr, char *buf)
  3261. {
  3262. struct iwl_priv *priv = dev_get_drvdata(d);
  3263. if (!iwl_is_alive(priv))
  3264. return -EAGAIN;
  3265. return sprintf(buf, "%d\n", iwl3945_mod_params.antenna);
  3266. }
  3267. static ssize_t store_antenna(struct device *d,
  3268. struct device_attribute *attr,
  3269. const char *buf, size_t count)
  3270. {
  3271. struct iwl_priv *priv __maybe_unused = dev_get_drvdata(d);
  3272. int ant;
  3273. if (count == 0)
  3274. return 0;
  3275. if (sscanf(buf, "%1i", &ant) != 1) {
  3276. IWL_DEBUG_INFO(priv, "not in hex or decimal form.\n");
  3277. return count;
  3278. }
  3279. if ((ant >= 0) && (ant <= 2)) {
  3280. IWL_DEBUG_INFO(priv, "Setting antenna select to %d.\n", ant);
  3281. iwl3945_mod_params.antenna = (enum iwl3945_antenna)ant;
  3282. } else
  3283. IWL_DEBUG_INFO(priv, "Bad antenna select value %d.\n", ant);
  3284. return count;
  3285. }
  3286. static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
  3287. static ssize_t show_status(struct device *d,
  3288. struct device_attribute *attr, char *buf)
  3289. {
  3290. struct iwl_priv *priv = dev_get_drvdata(d);
  3291. if (!iwl_is_alive(priv))
  3292. return -EAGAIN;
  3293. return sprintf(buf, "0x%08x\n", (int)priv->status);
  3294. }
  3295. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  3296. static ssize_t dump_error_log(struct device *d,
  3297. struct device_attribute *attr,
  3298. const char *buf, size_t count)
  3299. {
  3300. struct iwl_priv *priv = dev_get_drvdata(d);
  3301. char *p = (char *)buf;
  3302. if (p[0] == '1')
  3303. iwl3945_dump_nic_error_log(priv);
  3304. return strnlen(buf, count);
  3305. }
  3306. static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
  3307. static ssize_t dump_event_log(struct device *d,
  3308. struct device_attribute *attr,
  3309. const char *buf, size_t count)
  3310. {
  3311. struct iwl_priv *priv = dev_get_drvdata(d);
  3312. char *p = (char *)buf;
  3313. if (p[0] == '1')
  3314. iwl3945_dump_nic_event_log(priv);
  3315. return strnlen(buf, count);
  3316. }
  3317. static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
  3318. /*****************************************************************************
  3319. *
  3320. * driver setup and tear down
  3321. *
  3322. *****************************************************************************/
  3323. static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
  3324. {
  3325. priv->workqueue = create_singlethread_workqueue(DRV_NAME);
  3326. init_waitqueue_head(&priv->wait_command_queue);
  3327. INIT_WORK(&priv->up, iwl3945_bg_up);
  3328. INIT_WORK(&priv->restart, iwl3945_bg_restart);
  3329. INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
  3330. INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
  3331. INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
  3332. INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
  3333. INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
  3334. INIT_DELAYED_WORK(&priv->rfkill_poll, iwl3945_rfkill_poll);
  3335. INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
  3336. INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
  3337. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  3338. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  3339. iwl3945_hw_setup_deferred_work(priv);
  3340. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  3341. iwl3945_irq_tasklet, (unsigned long)priv);
  3342. }
  3343. static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
  3344. {
  3345. iwl3945_hw_cancel_deferred_work(priv);
  3346. cancel_delayed_work_sync(&priv->init_alive_start);
  3347. cancel_delayed_work(&priv->scan_check);
  3348. cancel_delayed_work(&priv->alive_start);
  3349. cancel_work_sync(&priv->beacon_update);
  3350. }
  3351. static struct attribute *iwl3945_sysfs_entries[] = {
  3352. &dev_attr_antenna.attr,
  3353. &dev_attr_channels.attr,
  3354. &dev_attr_dump_errors.attr,
  3355. &dev_attr_dump_events.attr,
  3356. &dev_attr_flags.attr,
  3357. &dev_attr_filter_flags.attr,
  3358. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  3359. &dev_attr_measurement.attr,
  3360. #endif
  3361. &dev_attr_power_level.attr,
  3362. &dev_attr_retry_rate.attr,
  3363. &dev_attr_statistics.attr,
  3364. &dev_attr_status.attr,
  3365. &dev_attr_temperature.attr,
  3366. &dev_attr_tx_power.attr,
  3367. #ifdef CONFIG_IWLWIFI_DEBUG
  3368. &dev_attr_debug_level.attr,
  3369. #endif
  3370. NULL
  3371. };
  3372. static struct attribute_group iwl3945_attribute_group = {
  3373. .name = NULL, /* put in device directory */
  3374. .attrs = iwl3945_sysfs_entries,
  3375. };
  3376. static struct ieee80211_ops iwl3945_hw_ops = {
  3377. .tx = iwl3945_mac_tx,
  3378. .start = iwl3945_mac_start,
  3379. .stop = iwl3945_mac_stop,
  3380. .add_interface = iwl_mac_add_interface,
  3381. .remove_interface = iwl_mac_remove_interface,
  3382. .config = iwl_mac_config,
  3383. .configure_filter = iwl_configure_filter,
  3384. .set_key = iwl3945_mac_set_key,
  3385. .get_tx_stats = iwl_mac_get_tx_stats,
  3386. .conf_tx = iwl_mac_conf_tx,
  3387. .reset_tsf = iwl_mac_reset_tsf,
  3388. .bss_info_changed = iwl_bss_info_changed,
  3389. .hw_scan = iwl_mac_hw_scan
  3390. };
  3391. static int iwl3945_init_drv(struct iwl_priv *priv)
  3392. {
  3393. int ret;
  3394. struct iwl3945_eeprom *eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  3395. priv->retry_rate = 1;
  3396. priv->ibss_beacon = NULL;
  3397. spin_lock_init(&priv->lock);
  3398. spin_lock_init(&priv->power_data.lock);
  3399. spin_lock_init(&priv->sta_lock);
  3400. spin_lock_init(&priv->hcmd_lock);
  3401. INIT_LIST_HEAD(&priv->free_frames);
  3402. mutex_init(&priv->mutex);
  3403. /* Clear the driver's (not device's) station table */
  3404. priv->cfg->ops->smgmt->clear_station_table(priv);
  3405. priv->data_retry_limit = -1;
  3406. priv->ieee_channels = NULL;
  3407. priv->ieee_rates = NULL;
  3408. priv->band = IEEE80211_BAND_2GHZ;
  3409. priv->iw_mode = NL80211_IFTYPE_STATION;
  3410. iwl_reset_qos(priv);
  3411. priv->qos_data.qos_active = 0;
  3412. priv->qos_data.qos_cap.val = 0;
  3413. priv->rates_mask = IWL_RATES_MASK;
  3414. /* If power management is turned on, default to CAM mode */
  3415. priv->power_mode = IWL_POWER_MODE_CAM;
  3416. priv->tx_power_user_lmt = IWL_DEFAULT_TX_POWER;
  3417. if (eeprom->version < EEPROM_3945_EEPROM_VERSION) {
  3418. IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
  3419. eeprom->version);
  3420. ret = -EINVAL;
  3421. goto err;
  3422. }
  3423. ret = iwl_init_channel_map(priv);
  3424. if (ret) {
  3425. IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
  3426. goto err;
  3427. }
  3428. /* Set up txpower settings in driver for all channels */
  3429. if (iwl3945_txpower_set_from_eeprom(priv)) {
  3430. ret = -EIO;
  3431. goto err_free_channel_map;
  3432. }
  3433. ret = iwlcore_init_geos(priv);
  3434. if (ret) {
  3435. IWL_ERR(priv, "initializing geos failed: %d\n", ret);
  3436. goto err_free_channel_map;
  3437. }
  3438. iwl3945_init_hw_rates(priv, priv->ieee_rates);
  3439. return 0;
  3440. err_free_channel_map:
  3441. iwl_free_channel_map(priv);
  3442. err:
  3443. return ret;
  3444. }
  3445. static int iwl3945_setup_mac(struct iwl_priv *priv)
  3446. {
  3447. int ret;
  3448. struct ieee80211_hw *hw = priv->hw;
  3449. hw->rate_control_algorithm = "iwl-3945-rs";
  3450. hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
  3451. /* Tell mac80211 our characteristics */
  3452. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  3453. IEEE80211_HW_NOISE_DBM |
  3454. IEEE80211_HW_SPECTRUM_MGMT;
  3455. hw->wiphy->interface_modes =
  3456. BIT(NL80211_IFTYPE_STATION) |
  3457. BIT(NL80211_IFTYPE_ADHOC);
  3458. hw->wiphy->custom_regulatory = true;
  3459. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX_3945;
  3460. /* we create the 802.11 header and a zero-length SSID element */
  3461. hw->wiphy->max_scan_ie_len = IWL_MAX_PROBE_REQUEST - 24 - 2;
  3462. /* Default value; 4 EDCA QOS priorities */
  3463. hw->queues = 4;
  3464. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  3465. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  3466. &priv->bands[IEEE80211_BAND_2GHZ];
  3467. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  3468. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  3469. &priv->bands[IEEE80211_BAND_5GHZ];
  3470. ret = ieee80211_register_hw(priv->hw);
  3471. if (ret) {
  3472. IWL_ERR(priv, "Failed to register hw (error %d)\n", ret);
  3473. return ret;
  3474. }
  3475. priv->mac80211_registered = 1;
  3476. return 0;
  3477. }
  3478. static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  3479. {
  3480. int err = 0;
  3481. struct iwl_priv *priv;
  3482. struct ieee80211_hw *hw;
  3483. struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
  3484. struct iwl3945_eeprom *eeprom;
  3485. unsigned long flags;
  3486. /***********************
  3487. * 1. Allocating HW data
  3488. * ********************/
  3489. /* mac80211 allocates memory for this device instance, including
  3490. * space for this driver's private structure */
  3491. hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
  3492. if (hw == NULL) {
  3493. printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
  3494. err = -ENOMEM;
  3495. goto out;
  3496. }
  3497. priv = hw->priv;
  3498. SET_IEEE80211_DEV(hw, &pdev->dev);
  3499. if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
  3500. (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
  3501. IWL_ERR(priv,
  3502. "invalid queues_num, should be between %d and %d\n",
  3503. IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
  3504. err = -EINVAL;
  3505. goto out_ieee80211_free_hw;
  3506. }
  3507. /*
  3508. * Disabling hardware scan means that mac80211 will perform scans
  3509. * "the hard way", rather than using device's scan.
  3510. */
  3511. if (iwl3945_mod_params.disable_hw_scan) {
  3512. IWL_DEBUG_INFO(priv, "Disabling hw_scan\n");
  3513. iwl3945_hw_ops.hw_scan = NULL;
  3514. }
  3515. IWL_DEBUG_INFO(priv, "*** LOAD DRIVER ***\n");
  3516. priv->cfg = cfg;
  3517. priv->pci_dev = pdev;
  3518. #ifdef CONFIG_IWLWIFI_DEBUG
  3519. priv->debug_level = iwl3945_mod_params.debug;
  3520. atomic_set(&priv->restrict_refcnt, 0);
  3521. #endif
  3522. /***************************
  3523. * 2. Initializing PCI bus
  3524. * *************************/
  3525. if (pci_enable_device(pdev)) {
  3526. err = -ENODEV;
  3527. goto out_ieee80211_free_hw;
  3528. }
  3529. pci_set_master(pdev);
  3530. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  3531. if (!err)
  3532. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  3533. if (err) {
  3534. IWL_WARN(priv, "No suitable DMA available.\n");
  3535. goto out_pci_disable_device;
  3536. }
  3537. pci_set_drvdata(pdev, priv);
  3538. err = pci_request_regions(pdev, DRV_NAME);
  3539. if (err)
  3540. goto out_pci_disable_device;
  3541. /***********************
  3542. * 3. Read REV Register
  3543. * ********************/
  3544. priv->hw_base = pci_iomap(pdev, 0, 0);
  3545. if (!priv->hw_base) {
  3546. err = -ENODEV;
  3547. goto out_pci_release_regions;
  3548. }
  3549. IWL_DEBUG_INFO(priv, "pci_resource_len = 0x%08llx\n",
  3550. (unsigned long long) pci_resource_len(pdev, 0));
  3551. IWL_DEBUG_INFO(priv, "pci_resource_base = %p\n", priv->hw_base);
  3552. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  3553. * PCI Tx retries from interfering with C3 CPU state */
  3554. pci_write_config_byte(pdev, 0x41, 0x00);
  3555. /* amp init */
  3556. err = priv->cfg->ops->lib->apm_ops.init(priv);
  3557. if (err < 0) {
  3558. IWL_DEBUG_INFO(priv, "Failed to init the card\n");
  3559. goto out_iounmap;
  3560. }
  3561. /***********************
  3562. * 4. Read EEPROM
  3563. * ********************/
  3564. /* Read the EEPROM */
  3565. err = iwl_eeprom_init(priv);
  3566. if (err) {
  3567. IWL_ERR(priv, "Unable to init EEPROM\n");
  3568. goto out_iounmap;
  3569. }
  3570. /* MAC Address location in EEPROM same for 3945/4965 */
  3571. eeprom = (struct iwl3945_eeprom *)priv->eeprom;
  3572. memcpy(priv->mac_addr, eeprom->mac_address, ETH_ALEN);
  3573. IWL_DEBUG_INFO(priv, "MAC address: %pM\n", priv->mac_addr);
  3574. SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
  3575. /***********************
  3576. * 5. Setup HW Constants
  3577. * ********************/
  3578. /* Device-specific setup */
  3579. if (iwl3945_hw_set_hw_params(priv)) {
  3580. IWL_ERR(priv, "failed to set hw settings\n");
  3581. goto out_eeprom_free;
  3582. }
  3583. /***********************
  3584. * 6. Setup priv
  3585. * ********************/
  3586. err = iwl3945_init_drv(priv);
  3587. if (err) {
  3588. IWL_ERR(priv, "initializing driver failed\n");
  3589. goto out_unset_hw_params;
  3590. }
  3591. IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
  3592. priv->cfg->name);
  3593. /***********************
  3594. * 7. Setup Services
  3595. * ********************/
  3596. spin_lock_irqsave(&priv->lock, flags);
  3597. iwl_disable_interrupts(priv);
  3598. spin_unlock_irqrestore(&priv->lock, flags);
  3599. pci_enable_msi(priv->pci_dev);
  3600. err = request_irq(priv->pci_dev->irq, iwl_isr, IRQF_SHARED,
  3601. DRV_NAME, priv);
  3602. if (err) {
  3603. IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
  3604. goto out_disable_msi;
  3605. }
  3606. err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  3607. if (err) {
  3608. IWL_ERR(priv, "failed to create sysfs device attributes\n");
  3609. goto out_release_irq;
  3610. }
  3611. iwl_set_rxon_channel(priv,
  3612. &priv->bands[IEEE80211_BAND_2GHZ].channels[5]);
  3613. iwl3945_setup_deferred_work(priv);
  3614. iwl3945_setup_rx_handlers(priv);
  3615. /*********************************
  3616. * 8. Setup and Register mac80211
  3617. * *******************************/
  3618. iwl_enable_interrupts(priv);
  3619. err = iwl3945_setup_mac(priv);
  3620. if (err)
  3621. goto out_remove_sysfs;
  3622. err = iwl_dbgfs_register(priv, DRV_NAME);
  3623. if (err)
  3624. IWL_ERR(priv, "failed to create debugfs files. Ignoring error: %d\n", err);
  3625. err = iwl_rfkill_init(priv);
  3626. if (err)
  3627. IWL_ERR(priv, "Unable to initialize RFKILL system. "
  3628. "Ignoring error: %d\n", err);
  3629. else
  3630. iwl_rfkill_set_hw_state(priv);
  3631. /* Start monitoring the killswitch */
  3632. queue_delayed_work(priv->workqueue, &priv->rfkill_poll,
  3633. 2 * HZ);
  3634. return 0;
  3635. out_remove_sysfs:
  3636. destroy_workqueue(priv->workqueue);
  3637. priv->workqueue = NULL;
  3638. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  3639. out_release_irq:
  3640. free_irq(priv->pci_dev->irq, priv);
  3641. out_disable_msi:
  3642. pci_disable_msi(priv->pci_dev);
  3643. iwlcore_free_geos(priv);
  3644. iwl_free_channel_map(priv);
  3645. out_unset_hw_params:
  3646. iwl3945_unset_hw_params(priv);
  3647. out_eeprom_free:
  3648. iwl_eeprom_free(priv);
  3649. out_iounmap:
  3650. pci_iounmap(pdev, priv->hw_base);
  3651. out_pci_release_regions:
  3652. pci_release_regions(pdev);
  3653. out_pci_disable_device:
  3654. pci_set_drvdata(pdev, NULL);
  3655. pci_disable_device(pdev);
  3656. out_ieee80211_free_hw:
  3657. ieee80211_free_hw(priv->hw);
  3658. out:
  3659. return err;
  3660. }
  3661. static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
  3662. {
  3663. struct iwl_priv *priv = pci_get_drvdata(pdev);
  3664. unsigned long flags;
  3665. if (!priv)
  3666. return;
  3667. IWL_DEBUG_INFO(priv, "*** UNLOAD DRIVER ***\n");
  3668. iwl_dbgfs_unregister(priv);
  3669. set_bit(STATUS_EXIT_PENDING, &priv->status);
  3670. if (priv->mac80211_registered) {
  3671. ieee80211_unregister_hw(priv->hw);
  3672. priv->mac80211_registered = 0;
  3673. } else {
  3674. iwl3945_down(priv);
  3675. }
  3676. /* make sure we flush any pending irq or
  3677. * tasklet for the driver
  3678. */
  3679. spin_lock_irqsave(&priv->lock, flags);
  3680. iwl_disable_interrupts(priv);
  3681. spin_unlock_irqrestore(&priv->lock, flags);
  3682. iwl_synchronize_irq(priv);
  3683. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  3684. iwl_rfkill_unregister(priv);
  3685. cancel_delayed_work_sync(&priv->rfkill_poll);
  3686. iwl3945_dealloc_ucode_pci(priv);
  3687. if (priv->rxq.bd)
  3688. iwl3945_rx_queue_free(priv, &priv->rxq);
  3689. iwl3945_hw_txq_ctx_free(priv);
  3690. iwl3945_unset_hw_params(priv);
  3691. priv->cfg->ops->smgmt->clear_station_table(priv);
  3692. /*netif_stop_queue(dev); */
  3693. flush_workqueue(priv->workqueue);
  3694. /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
  3695. * priv->workqueue... so we can't take down the workqueue
  3696. * until now... */
  3697. destroy_workqueue(priv->workqueue);
  3698. priv->workqueue = NULL;
  3699. free_irq(pdev->irq, priv);
  3700. pci_disable_msi(pdev);
  3701. pci_iounmap(pdev, priv->hw_base);
  3702. pci_release_regions(pdev);
  3703. pci_disable_device(pdev);
  3704. pci_set_drvdata(pdev, NULL);
  3705. iwl_free_channel_map(priv);
  3706. iwlcore_free_geos(priv);
  3707. kfree(priv->scan);
  3708. if (priv->ibss_beacon)
  3709. dev_kfree_skb(priv->ibss_beacon);
  3710. ieee80211_free_hw(priv->hw);
  3711. }
  3712. /*****************************************************************************
  3713. *
  3714. * driver and module entry point
  3715. *
  3716. *****************************************************************************/
  3717. static struct pci_driver iwl3945_driver = {
  3718. .name = DRV_NAME,
  3719. .id_table = iwl3945_hw_card_ids,
  3720. .probe = iwl3945_pci_probe,
  3721. .remove = __devexit_p(iwl3945_pci_remove),
  3722. #ifdef CONFIG_PM
  3723. .suspend = iwl_pci_suspend,
  3724. .resume = iwl_pci_resume,
  3725. #endif
  3726. };
  3727. static int __init iwl3945_init(void)
  3728. {
  3729. int ret;
  3730. printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
  3731. printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
  3732. ret = iwl3945_rate_control_register();
  3733. if (ret) {
  3734. printk(KERN_ERR DRV_NAME
  3735. "Unable to register rate control algorithm: %d\n", ret);
  3736. return ret;
  3737. }
  3738. ret = pci_register_driver(&iwl3945_driver);
  3739. if (ret) {
  3740. printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
  3741. goto error_register;
  3742. }
  3743. return ret;
  3744. error_register:
  3745. iwl3945_rate_control_unregister();
  3746. return ret;
  3747. }
  3748. static void __exit iwl3945_exit(void)
  3749. {
  3750. pci_unregister_driver(&iwl3945_driver);
  3751. iwl3945_rate_control_unregister();
  3752. }
  3753. MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
  3754. module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
  3755. MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
  3756. module_param_named(swcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
  3757. MODULE_PARM_DESC(swcrypto,
  3758. "using software crypto (default 1 [software])\n");
  3759. module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
  3760. MODULE_PARM_DESC(debug, "debug output mask");
  3761. module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
  3762. MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
  3763. module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
  3764. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  3765. module_param_named(fw_restart3945, iwl3945_mod_params.restart_fw, int, 0444);
  3766. MODULE_PARM_DESC(fw_restart3945, "restart firmware in case of error");
  3767. module_exit(iwl3945_exit);
  3768. module_init(iwl3945_init);