iwl3945-base.c 147 KB

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