iwl3945-base.c 217 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 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-3945.h"
  50. #include "iwl-helpers.h"
  51. #include "iwl-core.h"
  52. #include "iwl-dev.h"
  53. static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
  54. struct iwl_tx_queue *txq);
  55. /*
  56. * module name, copyright, version, etc.
  57. */
  58. #define DRV_DESCRIPTION \
  59. "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
  60. #ifdef CONFIG_IWL3945_DEBUG
  61. #define VD "d"
  62. #else
  63. #define VD
  64. #endif
  65. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  66. #define VS "s"
  67. #else
  68. #define VS
  69. #endif
  70. #define IWL39_VERSION "1.2.26k" VD VS
  71. #define DRV_COPYRIGHT "Copyright(c) 2003-2008 Intel Corporation"
  72. #define DRV_AUTHOR "<ilw@linux.intel.com>"
  73. #define DRV_VERSION IWL39_VERSION
  74. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  75. MODULE_VERSION(DRV_VERSION);
  76. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  77. MODULE_LICENSE("GPL");
  78. /* module parameters */
  79. struct iwl_mod_params iwl3945_mod_params = {
  80. .num_of_queues = IWL39_MAX_NUM_QUEUES,
  81. /* the rest are 0 by default */
  82. };
  83. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  84. * DMA services
  85. *
  86. * Theory of operation
  87. *
  88. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  89. * of buffer descriptors, each of which points to one or more data buffers for
  90. * the device to read from or fill. Driver and device exchange status of each
  91. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  92. * entries in each circular buffer, to protect against confusing empty and full
  93. * queue states.
  94. *
  95. * The device reads or writes the data in the queues via the device's several
  96. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  97. *
  98. * For Tx queue, there are low mark and high mark limits. If, after queuing
  99. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  100. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  101. * Tx queue resumed.
  102. *
  103. * The 3945 operates with six queues: One receive queue, one transmit queue
  104. * (#4) for sending commands to the device firmware, and four transmit queues
  105. * (#0-3) for data tx via EDCA. An additional 2 HCCA queues are unused.
  106. ***************************************************/
  107. int iwl3945_x2_queue_used(const struct iwl_queue *q, int i)
  108. {
  109. return q->write_ptr > q->read_ptr ?
  110. (i >= q->read_ptr && i < q->write_ptr) :
  111. !(i < q->read_ptr && i >= q->write_ptr);
  112. }
  113. /**
  114. * iwl3945_queue_init - Initialize queue's high/low-water and read/write indexes
  115. */
  116. static int iwl3945_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
  117. int count, int slots_num, u32 id)
  118. {
  119. q->n_bd = count;
  120. q->n_window = slots_num;
  121. q->id = id;
  122. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  123. * and iwl_queue_dec_wrap are broken. */
  124. BUG_ON(!is_power_of_2(count));
  125. /* slots_num must be power-of-two size, otherwise
  126. * get_cmd_index is broken. */
  127. BUG_ON(!is_power_of_2(slots_num));
  128. q->low_mark = q->n_window / 4;
  129. if (q->low_mark < 4)
  130. q->low_mark = 4;
  131. q->high_mark = q->n_window / 8;
  132. if (q->high_mark < 2)
  133. q->high_mark = 2;
  134. q->write_ptr = q->read_ptr = 0;
  135. return 0;
  136. }
  137. /**
  138. * iwl3945_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  139. */
  140. static int iwl3945_tx_queue_alloc(struct iwl_priv *priv,
  141. struct iwl_tx_queue *txq, u32 id)
  142. {
  143. struct pci_dev *dev = priv->pci_dev;
  144. /* Driver private data, only for Tx (not command) queues,
  145. * not shared with device. */
  146. if (id != IWL_CMD_QUEUE_NUM) {
  147. txq->txb = kmalloc(sizeof(txq->txb[0]) *
  148. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  149. if (!txq->txb) {
  150. IWL_ERR(priv, "kmalloc for auxiliary BD "
  151. "structures failed\n");
  152. goto error;
  153. }
  154. } else
  155. txq->txb = NULL;
  156. /* Circular buffer of transmit frame descriptors (TFDs),
  157. * shared with device */
  158. txq->tfds39 = pci_alloc_consistent(dev,
  159. sizeof(txq->tfds39[0]) * TFD_QUEUE_SIZE_MAX,
  160. &txq->q.dma_addr);
  161. if (!txq->tfds39) {
  162. IWL_ERR(priv, "pci_alloc_consistent(%zd) failed\n",
  163. sizeof(txq->tfds39[0]) * TFD_QUEUE_SIZE_MAX);
  164. goto error;
  165. }
  166. txq->q.id = id;
  167. return 0;
  168. error:
  169. kfree(txq->txb);
  170. txq->txb = NULL;
  171. return -ENOMEM;
  172. }
  173. /**
  174. * iwl3945_tx_queue_init - Allocate and initialize one tx/cmd queue
  175. */
  176. int iwl3945_tx_queue_init(struct iwl_priv *priv,
  177. struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
  178. {
  179. int len, i;
  180. int rc = 0;
  181. /*
  182. * Alloc buffer array for commands (Tx or other types of commands).
  183. * For the command queue (#4), allocate command space + one big
  184. * command for scan, since scan command is very huge; the system will
  185. * not have two scans at the same time, so only one is needed.
  186. * For data Tx queues (all other queues), no super-size command
  187. * space is needed.
  188. */
  189. len = sizeof(struct iwl_cmd);
  190. for (i = 0; i <= slots_num; i++) {
  191. if (i == slots_num) {
  192. if (txq_id == IWL_CMD_QUEUE_NUM)
  193. len += IWL_MAX_SCAN_SIZE;
  194. else
  195. continue;
  196. }
  197. txq->cmd[i] = kmalloc(len, GFP_KERNEL);
  198. if (!txq->cmd[i])
  199. goto err;
  200. }
  201. /* Alloc driver data array and TFD circular buffer */
  202. rc = iwl3945_tx_queue_alloc(priv, txq, txq_id);
  203. if (rc)
  204. goto err;
  205. txq->need_update = 0;
  206. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  207. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  208. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  209. /* Initialize queue high/low-water, head/tail indexes */
  210. iwl3945_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  211. /* Tell device where to find queue, enable DMA channel. */
  212. iwl3945_hw_tx_queue_init(priv, txq);
  213. return 0;
  214. err:
  215. for (i = 0; i < slots_num; i++) {
  216. kfree(txq->cmd[i]);
  217. txq->cmd[i] = NULL;
  218. }
  219. if (txq_id == IWL_CMD_QUEUE_NUM) {
  220. kfree(txq->cmd[slots_num]);
  221. txq->cmd[slots_num] = NULL;
  222. }
  223. return -ENOMEM;
  224. }
  225. /**
  226. * iwl3945_tx_queue_free - Deallocate DMA queue.
  227. * @txq: Transmit queue to deallocate.
  228. *
  229. * Empty queue by removing and destroying all BD's.
  230. * Free all buffers.
  231. * 0-fill, but do not free "txq" descriptor structure.
  232. */
  233. void iwl3945_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  234. {
  235. struct iwl_queue *q = &txq->q;
  236. struct pci_dev *dev = priv->pci_dev;
  237. int len, i;
  238. if (q->n_bd == 0)
  239. return;
  240. /* first, empty all BD's */
  241. for (; q->write_ptr != q->read_ptr;
  242. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
  243. iwl3945_hw_txq_free_tfd(priv, txq);
  244. len = sizeof(struct iwl_cmd) * q->n_window;
  245. if (q->id == IWL_CMD_QUEUE_NUM)
  246. len += IWL_MAX_SCAN_SIZE;
  247. /* De-alloc array of command/tx buffers */
  248. for (i = 0; i < TFD_TX_CMD_SLOTS; i++)
  249. kfree(txq->cmd[i]);
  250. /* De-alloc circular buffer of TFDs */
  251. if (txq->q.n_bd)
  252. pci_free_consistent(dev, sizeof(struct iwl3945_tfd) *
  253. txq->q.n_bd, txq->tfds39, txq->q.dma_addr);
  254. /* De-alloc array of per-TFD driver data */
  255. kfree(txq->txb);
  256. txq->txb = NULL;
  257. /* 0-fill queue descriptor structure */
  258. memset(txq, 0, sizeof(*txq));
  259. }
  260. /*************** STATION TABLE MANAGEMENT ****
  261. * mac80211 should be examined to determine if sta_info is duplicating
  262. * the functionality provided here
  263. */
  264. /**************************************************************/
  265. #if 0 /* temporary disable till we add real remove station */
  266. /**
  267. * iwl3945_remove_station - Remove driver's knowledge of station.
  268. *
  269. * NOTE: This does not remove station from device's station table.
  270. */
  271. static u8 iwl3945_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
  272. {
  273. int index = IWL_INVALID_STATION;
  274. int i;
  275. unsigned long flags;
  276. spin_lock_irqsave(&priv->sta_lock, flags);
  277. if (is_ap)
  278. index = IWL_AP_ID;
  279. else if (is_broadcast_ether_addr(addr))
  280. index = priv->hw_params.bcast_sta_id;
  281. else
  282. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
  283. if (priv->stations_39[i].used &&
  284. !compare_ether_addr(priv->stations_39[i].sta.sta.addr,
  285. addr)) {
  286. index = i;
  287. break;
  288. }
  289. if (unlikely(index == IWL_INVALID_STATION))
  290. goto out;
  291. if (priv->stations_39[index].used) {
  292. priv->stations_39[index].used = 0;
  293. priv->num_stations--;
  294. }
  295. BUG_ON(priv->num_stations < 0);
  296. out:
  297. spin_unlock_irqrestore(&priv->sta_lock, flags);
  298. return 0;
  299. }
  300. #endif
  301. /**
  302. * iwl3945_clear_stations_table - Clear the driver's station table
  303. *
  304. * NOTE: This does not clear or otherwise alter the device's station table.
  305. */
  306. static void iwl3945_clear_stations_table(struct iwl_priv *priv)
  307. {
  308. unsigned long flags;
  309. spin_lock_irqsave(&priv->sta_lock, flags);
  310. priv->num_stations = 0;
  311. memset(priv->stations_39, 0, sizeof(priv->stations_39));
  312. spin_unlock_irqrestore(&priv->sta_lock, flags);
  313. }
  314. /**
  315. * iwl3945_add_station - Add station to station tables in driver and device
  316. */
  317. u8 iwl3945_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
  318. {
  319. int i;
  320. int index = IWL_INVALID_STATION;
  321. struct iwl3945_station_entry *station;
  322. unsigned long flags_spin;
  323. u8 rate;
  324. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  325. if (is_ap)
  326. index = IWL_AP_ID;
  327. else if (is_broadcast_ether_addr(addr))
  328. index = priv->hw_params.bcast_sta_id;
  329. else
  330. for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
  331. if (!compare_ether_addr(priv->stations_39[i].sta.sta.addr,
  332. addr)) {
  333. index = i;
  334. break;
  335. }
  336. if (!priv->stations_39[i].used &&
  337. index == IWL_INVALID_STATION)
  338. index = i;
  339. }
  340. /* These two conditions has the same outcome but keep them separate
  341. since they have different meaning */
  342. if (unlikely(index == IWL_INVALID_STATION)) {
  343. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  344. return index;
  345. }
  346. if (priv->stations_39[index].used &&
  347. !compare_ether_addr(priv->stations_39[index].sta.sta.addr, addr)) {
  348. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  349. return index;
  350. }
  351. IWL_DEBUG_ASSOC("Add STA ID %d: %pM\n", index, addr);
  352. station = &priv->stations_39[index];
  353. station->used = 1;
  354. priv->num_stations++;
  355. /* Set up the REPLY_ADD_STA command to send to device */
  356. memset(&station->sta, 0, sizeof(struct iwl3945_addsta_cmd));
  357. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  358. station->sta.mode = 0;
  359. station->sta.sta.sta_id = index;
  360. station->sta.station_flags = 0;
  361. if (priv->band == IEEE80211_BAND_5GHZ)
  362. rate = IWL_RATE_6M_PLCP;
  363. else
  364. rate = IWL_RATE_1M_PLCP;
  365. /* Turn on both antennas for the station... */
  366. station->sta.rate_n_flags =
  367. iwl3945_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
  368. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  369. /* Add station to device's station table */
  370. iwl3945_send_add_station(priv, &station->sta, flags);
  371. return index;
  372. }
  373. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  374. #define IWL_CMD(x) case x: return #x
  375. #define HOST_COMPLETE_TIMEOUT (HZ / 2)
  376. /**
  377. * iwl3945_enqueue_hcmd - enqueue a uCode command
  378. * @priv: device private data point
  379. * @cmd: a point to the ucode command structure
  380. *
  381. * The function returns < 0 values to indicate the operation is
  382. * failed. On success, it turns the index (> 0) of command in the
  383. * command queue.
  384. */
  385. static int iwl3945_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  386. {
  387. struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
  388. struct iwl_queue *q = &txq->q;
  389. struct iwl3945_tfd *tfd;
  390. struct iwl_cmd *out_cmd;
  391. u32 idx;
  392. u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
  393. dma_addr_t phys_addr;
  394. int pad;
  395. int ret, len;
  396. unsigned long flags;
  397. /* If any of the command structures end up being larger than
  398. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  399. * we will need to increase the size of the TFD entries */
  400. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  401. !(cmd->meta.flags & CMD_SIZE_HUGE));
  402. if (iwl_is_rfkill(priv)) {
  403. IWL_DEBUG_INFO("Not sending command - RF KILL");
  404. return -EIO;
  405. }
  406. if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
  407. IWL_ERR(priv, "No space for Tx\n");
  408. return -ENOSPC;
  409. }
  410. spin_lock_irqsave(&priv->hcmd_lock, flags);
  411. tfd = &txq->tfds39[q->write_ptr];
  412. memset(tfd, 0, sizeof(*tfd));
  413. idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
  414. out_cmd = txq->cmd[idx];
  415. out_cmd->hdr.cmd = cmd->id;
  416. memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
  417. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  418. /* At this point, the out_cmd now has all of the incoming cmd
  419. * information */
  420. out_cmd->hdr.flags = 0;
  421. out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
  422. INDEX_TO_SEQ(q->write_ptr));
  423. if (out_cmd->meta.flags & CMD_SIZE_HUGE)
  424. out_cmd->hdr.sequence |= SEQ_HUGE_FRAME;
  425. len = (idx == TFD_CMD_SLOTS) ?
  426. IWL_MAX_SCAN_SIZE : sizeof(struct iwl_cmd);
  427. phys_addr = pci_map_single(priv->pci_dev, out_cmd,
  428. len, PCI_DMA_TODEVICE);
  429. pci_unmap_addr_set(&out_cmd->meta, mapping, phys_addr);
  430. pci_unmap_len_set(&out_cmd->meta, len, len);
  431. phys_addr += offsetof(struct iwl_cmd, hdr);
  432. iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
  433. pad = U32_PAD(cmd->len);
  434. tfd->control_flags |= cpu_to_le32(TFD_CTL_PAD_SET(pad));
  435. IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
  436. "%d bytes at %d[%d]:%d\n",
  437. get_cmd_string(out_cmd->hdr.cmd),
  438. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  439. fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
  440. txq->need_update = 1;
  441. /* Increment and update queue's write index */
  442. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  443. ret = iwl3945_tx_queue_update_write_ptr(priv, txq);
  444. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  445. return ret ? ret : idx;
  446. }
  447. static int iwl3945_send_cmd_async(struct iwl_priv *priv,
  448. struct iwl_host_cmd *cmd)
  449. {
  450. int ret;
  451. BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
  452. /* An asynchronous command can not expect an SKB to be set. */
  453. BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
  454. /* An asynchronous command MUST have a callback. */
  455. BUG_ON(!cmd->meta.u.callback);
  456. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  457. return -EBUSY;
  458. ret = iwl3945_enqueue_hcmd(priv, cmd);
  459. if (ret < 0) {
  460. IWL_ERR(priv,
  461. "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
  462. get_cmd_string(cmd->id), ret);
  463. return ret;
  464. }
  465. return 0;
  466. }
  467. static int iwl3945_send_cmd_sync(struct iwl_priv *priv,
  468. struct iwl_host_cmd *cmd)
  469. {
  470. int cmd_idx;
  471. int ret;
  472. BUG_ON(cmd->meta.flags & CMD_ASYNC);
  473. /* A synchronous command can not have a callback set. */
  474. BUG_ON(cmd->meta.u.callback != NULL);
  475. if (test_and_set_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status)) {
  476. IWL_ERR(priv,
  477. "Error sending %s: Already sending a host command\n",
  478. get_cmd_string(cmd->id));
  479. ret = -EBUSY;
  480. goto out;
  481. }
  482. set_bit(STATUS_HCMD_ACTIVE, &priv->status);
  483. if (cmd->meta.flags & CMD_WANT_SKB)
  484. cmd->meta.source = &cmd->meta;
  485. cmd_idx = iwl3945_enqueue_hcmd(priv, cmd);
  486. if (cmd_idx < 0) {
  487. ret = cmd_idx;
  488. IWL_ERR(priv,
  489. "Error sending %s: iwl3945_enqueue_hcmd failed: %d\n",
  490. get_cmd_string(cmd->id), ret);
  491. goto out;
  492. }
  493. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  494. !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
  495. HOST_COMPLETE_TIMEOUT);
  496. if (!ret) {
  497. if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
  498. IWL_ERR(priv, "Error sending %s: time out after %dms\n",
  499. get_cmd_string(cmd->id),
  500. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  501. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  502. ret = -ETIMEDOUT;
  503. goto cancel;
  504. }
  505. }
  506. if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
  507. IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
  508. get_cmd_string(cmd->id));
  509. ret = -ECANCELED;
  510. goto fail;
  511. }
  512. if (test_bit(STATUS_FW_ERROR, &priv->status)) {
  513. IWL_DEBUG_INFO("Command %s failed: FW Error\n",
  514. get_cmd_string(cmd->id));
  515. ret = -EIO;
  516. goto fail;
  517. }
  518. if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
  519. IWL_ERR(priv, "Error: Response NULL in '%s'\n",
  520. get_cmd_string(cmd->id));
  521. ret = -EIO;
  522. goto cancel;
  523. }
  524. ret = 0;
  525. goto out;
  526. cancel:
  527. if (cmd->meta.flags & CMD_WANT_SKB) {
  528. struct iwl_cmd *qcmd;
  529. /* Cancel the CMD_WANT_SKB flag for the cmd in the
  530. * TX cmd queue. Otherwise in case the cmd comes
  531. * in later, it will possibly set an invalid
  532. * address (cmd->meta.source). */
  533. qcmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
  534. qcmd->meta.flags &= ~CMD_WANT_SKB;
  535. }
  536. fail:
  537. if (cmd->meta.u.skb) {
  538. dev_kfree_skb_any(cmd->meta.u.skb);
  539. cmd->meta.u.skb = NULL;
  540. }
  541. out:
  542. clear_bit(STATUS_HCMD_SYNC_ACTIVE, &priv->status);
  543. return ret;
  544. }
  545. int iwl3945_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  546. {
  547. if (cmd->meta.flags & CMD_ASYNC)
  548. return iwl3945_send_cmd_async(priv, cmd);
  549. return iwl3945_send_cmd_sync(priv, cmd);
  550. }
  551. int iwl3945_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
  552. {
  553. struct iwl_host_cmd cmd = {
  554. .id = id,
  555. .len = len,
  556. .data = data,
  557. };
  558. return iwl3945_send_cmd_sync(priv, &cmd);
  559. }
  560. static int __must_check iwl3945_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
  561. {
  562. struct iwl_host_cmd cmd = {
  563. .id = id,
  564. .len = sizeof(val),
  565. .data = &val,
  566. };
  567. return iwl3945_send_cmd_sync(priv, &cmd);
  568. }
  569. int iwl3945_send_statistics_request(struct iwl_priv *priv)
  570. {
  571. return iwl3945_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
  572. }
  573. /**
  574. * iwl3945_set_rxon_channel - Set the phymode and channel values in staging RXON
  575. * @band: 2.4 or 5 GHz band
  576. * @channel: Any channel valid for the requested band
  577. * In addition to setting the staging RXON, priv->band is also set.
  578. *
  579. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  580. * in the staging RXON flag structure based on the band
  581. */
  582. static int iwl3945_set_rxon_channel(struct iwl_priv *priv,
  583. enum ieee80211_band band,
  584. u16 channel)
  585. {
  586. if (!iwl3945_get_channel_info(priv, band, channel)) {
  587. IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
  588. channel, band);
  589. return -EINVAL;
  590. }
  591. if ((le16_to_cpu(priv->staging39_rxon.channel) == channel) &&
  592. (priv->band == band))
  593. return 0;
  594. priv->staging39_rxon.channel = cpu_to_le16(channel);
  595. if (band == IEEE80211_BAND_5GHZ)
  596. priv->staging39_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
  597. else
  598. priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
  599. priv->band = band;
  600. IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, band);
  601. return 0;
  602. }
  603. /**
  604. * iwl3945_check_rxon_cmd - validate RXON structure is valid
  605. *
  606. * NOTE: This is really only useful during development and can eventually
  607. * be #ifdef'd out once the driver is stable and folks aren't actively
  608. * making changes
  609. */
  610. static int iwl3945_check_rxon_cmd(struct iwl_priv *priv)
  611. {
  612. int error = 0;
  613. int counter = 1;
  614. struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
  615. if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
  616. error |= le32_to_cpu(rxon->flags &
  617. (RXON_FLG_TGJ_NARROW_BAND_MSK |
  618. RXON_FLG_RADAR_DETECT_MSK));
  619. if (error)
  620. IWL_WARN(priv, "check 24G fields %d | %d\n",
  621. counter++, error);
  622. } else {
  623. error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
  624. 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
  625. if (error)
  626. IWL_WARN(priv, "check 52 fields %d | %d\n",
  627. counter++, error);
  628. error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
  629. if (error)
  630. IWL_WARN(priv, "check 52 CCK %d | %d\n",
  631. counter++, error);
  632. }
  633. error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
  634. if (error)
  635. IWL_WARN(priv, "check mac addr %d | %d\n", counter++, error);
  636. /* make sure basic rates 6Mbps and 1Mbps are supported */
  637. error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
  638. ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
  639. if (error)
  640. IWL_WARN(priv, "check basic rate %d | %d\n", counter++, error);
  641. error |= (le16_to_cpu(rxon->assoc_id) > 2007);
  642. if (error)
  643. IWL_WARN(priv, "check assoc id %d | %d\n", counter++, error);
  644. error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
  645. == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
  646. if (error)
  647. IWL_WARN(priv, "check CCK and short slot %d | %d\n",
  648. counter++, error);
  649. error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
  650. == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
  651. if (error)
  652. IWL_WARN(priv, "check CCK & auto detect %d | %d\n",
  653. counter++, error);
  654. error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
  655. RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
  656. if (error)
  657. IWL_WARN(priv, "check TGG and auto detect %d | %d\n",
  658. counter++, error);
  659. if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
  660. error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
  661. RXON_FLG_ANT_A_MSK)) == 0);
  662. if (error)
  663. IWL_WARN(priv, "check antenna %d %d\n", counter++, error);
  664. if (error)
  665. IWL_WARN(priv, "Tuning to channel %d\n",
  666. le16_to_cpu(rxon->channel));
  667. if (error) {
  668. IWL_ERR(priv, "Not a valid rxon_assoc_cmd field values\n");
  669. return -1;
  670. }
  671. return 0;
  672. }
  673. /**
  674. * iwl3945_full_rxon_required - check if full RXON (vs RXON_ASSOC) cmd is needed
  675. * @priv: staging_rxon is compared to active_rxon
  676. *
  677. * If the RXON structure is changing enough to require a new tune,
  678. * or is clearing the RXON_FILTER_ASSOC_MSK, then return 1 to indicate that
  679. * a new tune (full RXON command, rather than RXON_ASSOC cmd) is required.
  680. */
  681. static int iwl3945_full_rxon_required(struct iwl_priv *priv)
  682. {
  683. /* These items are only settable from the full RXON command */
  684. if (!(iwl3945_is_associated(priv)) ||
  685. compare_ether_addr(priv->staging39_rxon.bssid_addr,
  686. priv->active39_rxon.bssid_addr) ||
  687. compare_ether_addr(priv->staging39_rxon.node_addr,
  688. priv->active39_rxon.node_addr) ||
  689. compare_ether_addr(priv->staging39_rxon.wlap_bssid_addr,
  690. priv->active39_rxon.wlap_bssid_addr) ||
  691. (priv->staging39_rxon.dev_type != priv->active39_rxon.dev_type) ||
  692. (priv->staging39_rxon.channel != priv->active39_rxon.channel) ||
  693. (priv->staging39_rxon.air_propagation !=
  694. priv->active39_rxon.air_propagation) ||
  695. (priv->staging39_rxon.assoc_id != priv->active39_rxon.assoc_id))
  696. return 1;
  697. /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
  698. * be updated with the RXON_ASSOC command -- however only some
  699. * flag transitions are allowed using RXON_ASSOC */
  700. /* Check if we are not switching bands */
  701. if ((priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
  702. (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK))
  703. return 1;
  704. /* Check if we are switching association toggle */
  705. if ((priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
  706. (priv->active39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
  707. return 1;
  708. return 0;
  709. }
  710. static int iwl3945_send_rxon_assoc(struct iwl_priv *priv)
  711. {
  712. int rc = 0;
  713. struct iwl_rx_packet *res = NULL;
  714. struct iwl3945_rxon_assoc_cmd rxon_assoc;
  715. struct iwl_host_cmd cmd = {
  716. .id = REPLY_RXON_ASSOC,
  717. .len = sizeof(rxon_assoc),
  718. .meta.flags = CMD_WANT_SKB,
  719. .data = &rxon_assoc,
  720. };
  721. const struct iwl3945_rxon_cmd *rxon1 = &priv->staging39_rxon;
  722. const struct iwl3945_rxon_cmd *rxon2 = &priv->active39_rxon;
  723. if ((rxon1->flags == rxon2->flags) &&
  724. (rxon1->filter_flags == rxon2->filter_flags) &&
  725. (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
  726. (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
  727. IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
  728. return 0;
  729. }
  730. rxon_assoc.flags = priv->staging39_rxon.flags;
  731. rxon_assoc.filter_flags = priv->staging39_rxon.filter_flags;
  732. rxon_assoc.ofdm_basic_rates = priv->staging39_rxon.ofdm_basic_rates;
  733. rxon_assoc.cck_basic_rates = priv->staging39_rxon.cck_basic_rates;
  734. rxon_assoc.reserved = 0;
  735. rc = iwl3945_send_cmd_sync(priv, &cmd);
  736. if (rc)
  737. return rc;
  738. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  739. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  740. IWL_ERR(priv, "Bad return from REPLY_RXON_ASSOC command\n");
  741. rc = -EIO;
  742. }
  743. priv->alloc_rxb_skb--;
  744. dev_kfree_skb_any(cmd.meta.u.skb);
  745. return rc;
  746. }
  747. /**
  748. * iwl3945_commit_rxon - commit staging_rxon to hardware
  749. *
  750. * The RXON command in staging_rxon is committed to the hardware and
  751. * the active_rxon structure is updated with the new data. This
  752. * function correctly transitions out of the RXON_ASSOC_MSK state if
  753. * a HW tune is required based on the RXON structure changes.
  754. */
  755. static int iwl3945_commit_rxon(struct iwl_priv *priv)
  756. {
  757. /* cast away the const for active_rxon in this function */
  758. struct iwl3945_rxon_cmd *active_rxon = (void *)&priv->active39_rxon;
  759. int rc = 0;
  760. if (!iwl_is_alive(priv))
  761. return -1;
  762. /* always get timestamp with Rx frame */
  763. priv->staging39_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
  764. /* select antenna */
  765. priv->staging39_rxon.flags &=
  766. ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
  767. priv->staging39_rxon.flags |= iwl3945_get_antenna_flags(priv);
  768. rc = iwl3945_check_rxon_cmd(priv);
  769. if (rc) {
  770. IWL_ERR(priv, "Invalid RXON configuration. Not committing.\n");
  771. return -EINVAL;
  772. }
  773. /* If we don't need to send a full RXON, we can use
  774. * iwl3945_rxon_assoc_cmd which is used to reconfigure filter
  775. * and other flags for the current radio configuration. */
  776. if (!iwl3945_full_rxon_required(priv)) {
  777. rc = iwl3945_send_rxon_assoc(priv);
  778. if (rc) {
  779. IWL_ERR(priv, "Error setting RXON_ASSOC "
  780. "configuration (%d).\n", rc);
  781. return rc;
  782. }
  783. memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
  784. return 0;
  785. }
  786. /* If we are currently associated and the new config requires
  787. * an RXON_ASSOC and the new config wants the associated mask enabled,
  788. * we must clear the associated from the active configuration
  789. * before we apply the new config */
  790. if (iwl3945_is_associated(priv) &&
  791. (priv->staging39_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
  792. IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
  793. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  794. rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
  795. sizeof(struct iwl3945_rxon_cmd),
  796. &priv->active39_rxon);
  797. /* If the mask clearing failed then we set
  798. * active_rxon back to what it was previously */
  799. if (rc) {
  800. active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
  801. IWL_ERR(priv, "Error clearing ASSOC_MSK on current "
  802. "configuration (%d).\n", rc);
  803. return rc;
  804. }
  805. }
  806. IWL_DEBUG_INFO("Sending RXON\n"
  807. "* with%s RXON_FILTER_ASSOC_MSK\n"
  808. "* channel = %d\n"
  809. "* bssid = %pM\n",
  810. ((priv->staging39_rxon.filter_flags &
  811. RXON_FILTER_ASSOC_MSK) ? "" : "out"),
  812. le16_to_cpu(priv->staging39_rxon.channel),
  813. priv->staging_rxon.bssid_addr);
  814. /* Apply the new configuration */
  815. rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON,
  816. sizeof(struct iwl3945_rxon_cmd), &priv->staging39_rxon);
  817. if (rc) {
  818. IWL_ERR(priv, "Error setting new configuration (%d).\n", rc);
  819. return rc;
  820. }
  821. memcpy(active_rxon, &priv->staging39_rxon, sizeof(*active_rxon));
  822. iwl3945_clear_stations_table(priv);
  823. /* If we issue a new RXON command which required a tune then we must
  824. * send a new TXPOWER command or we won't be able to Tx any frames */
  825. rc = iwl3945_hw_reg_send_txpower(priv);
  826. if (rc) {
  827. IWL_ERR(priv, "Error setting Tx power (%d).\n", rc);
  828. return rc;
  829. }
  830. /* Add the broadcast address so we can send broadcast frames */
  831. if (iwl3945_add_station(priv, iwl_bcast_addr, 0, 0) ==
  832. IWL_INVALID_STATION) {
  833. IWL_ERR(priv, "Error adding BROADCAST address for transmit.\n");
  834. return -EIO;
  835. }
  836. /* If we have set the ASSOC_MSK and we are in BSS mode then
  837. * add the IWL_AP_ID to the station rate table */
  838. if (iwl3945_is_associated(priv) &&
  839. (priv->iw_mode == NL80211_IFTYPE_STATION))
  840. if (iwl3945_add_station(priv, priv->active39_rxon.bssid_addr, 1, 0)
  841. == IWL_INVALID_STATION) {
  842. IWL_ERR(priv, "Error adding AP address for transmit\n");
  843. return -EIO;
  844. }
  845. /* Init the hardware's rate fallback order based on the band */
  846. rc = iwl3945_init_hw_rate_table(priv);
  847. if (rc) {
  848. IWL_ERR(priv, "Error setting HW rate table: %02X\n", rc);
  849. return -EIO;
  850. }
  851. return 0;
  852. }
  853. static int iwl3945_send_bt_config(struct iwl_priv *priv)
  854. {
  855. struct iwl_bt_cmd bt_cmd = {
  856. .flags = 3,
  857. .lead_time = 0xAA,
  858. .max_kill = 1,
  859. .kill_ack_mask = 0,
  860. .kill_cts_mask = 0,
  861. };
  862. return iwl3945_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  863. sizeof(bt_cmd), &bt_cmd);
  864. }
  865. static int iwl3945_send_scan_abort(struct iwl_priv *priv)
  866. {
  867. int rc = 0;
  868. struct iwl_rx_packet *res;
  869. struct iwl_host_cmd cmd = {
  870. .id = REPLY_SCAN_ABORT_CMD,
  871. .meta.flags = CMD_WANT_SKB,
  872. };
  873. /* If there isn't a scan actively going on in the hardware
  874. * then we are in between scan bands and not actually
  875. * actively scanning, so don't send the abort command */
  876. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  877. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  878. return 0;
  879. }
  880. rc = iwl3945_send_cmd_sync(priv, &cmd);
  881. if (rc) {
  882. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  883. return rc;
  884. }
  885. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  886. if (res->u.status != CAN_ABORT_STATUS) {
  887. /* The scan abort will return 1 for success or
  888. * 2 for "failure". A failure condition can be
  889. * due to simply not being in an active scan which
  890. * can occur if we send the scan abort before we
  891. * the microcode has notified us that a scan is
  892. * completed. */
  893. IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
  894. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  895. clear_bit(STATUS_SCAN_HW, &priv->status);
  896. }
  897. dev_kfree_skb_any(cmd.meta.u.skb);
  898. return rc;
  899. }
  900. static int iwl3945_add_sta_sync_callback(struct iwl_priv *priv,
  901. struct iwl_cmd *cmd, struct sk_buff *skb)
  902. {
  903. struct iwl_rx_packet *res = NULL;
  904. if (!skb) {
  905. IWL_ERR(priv, "Error: Response NULL in REPLY_ADD_STA.\n");
  906. return 1;
  907. }
  908. res = (struct iwl_rx_packet *)skb->data;
  909. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  910. IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
  911. res->hdr.flags);
  912. return 1;
  913. }
  914. switch (res->u.add_sta.status) {
  915. case ADD_STA_SUCCESS_MSK:
  916. break;
  917. default:
  918. break;
  919. }
  920. /* We didn't cache the SKB; let the caller free it */
  921. return 1;
  922. }
  923. int iwl3945_send_add_station(struct iwl_priv *priv,
  924. struct iwl3945_addsta_cmd *sta, u8 flags)
  925. {
  926. struct iwl_rx_packet *res = NULL;
  927. int rc = 0;
  928. struct iwl_host_cmd cmd = {
  929. .id = REPLY_ADD_STA,
  930. .len = sizeof(struct iwl3945_addsta_cmd),
  931. .meta.flags = flags,
  932. .data = sta,
  933. };
  934. if (flags & CMD_ASYNC)
  935. cmd.meta.u.callback = iwl3945_add_sta_sync_callback;
  936. else
  937. cmd.meta.flags |= CMD_WANT_SKB;
  938. rc = iwl3945_send_cmd(priv, &cmd);
  939. if (rc || (flags & CMD_ASYNC))
  940. return rc;
  941. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  942. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  943. IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
  944. res->hdr.flags);
  945. rc = -EIO;
  946. }
  947. if (rc == 0) {
  948. switch (res->u.add_sta.status) {
  949. case ADD_STA_SUCCESS_MSK:
  950. IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
  951. break;
  952. default:
  953. rc = -EIO;
  954. IWL_WARN(priv, "REPLY_ADD_STA failed\n");
  955. break;
  956. }
  957. }
  958. priv->alloc_rxb_skb--;
  959. dev_kfree_skb_any(cmd.meta.u.skb);
  960. return rc;
  961. }
  962. static int iwl3945_update_sta_key_info(struct iwl_priv *priv,
  963. struct ieee80211_key_conf *keyconf,
  964. u8 sta_id)
  965. {
  966. unsigned long flags;
  967. __le16 key_flags = 0;
  968. switch (keyconf->alg) {
  969. case ALG_CCMP:
  970. key_flags |= STA_KEY_FLG_CCMP;
  971. key_flags |= cpu_to_le16(
  972. keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  973. key_flags &= ~STA_KEY_FLG_INVALID;
  974. break;
  975. case ALG_TKIP:
  976. case ALG_WEP:
  977. default:
  978. return -EINVAL;
  979. }
  980. spin_lock_irqsave(&priv->sta_lock, flags);
  981. priv->stations_39[sta_id].keyinfo.alg = keyconf->alg;
  982. priv->stations_39[sta_id].keyinfo.keylen = keyconf->keylen;
  983. memcpy(priv->stations_39[sta_id].keyinfo.key, keyconf->key,
  984. keyconf->keylen);
  985. memcpy(priv->stations_39[sta_id].sta.key.key, keyconf->key,
  986. keyconf->keylen);
  987. priv->stations_39[sta_id].sta.key.key_flags = key_flags;
  988. priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  989. priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  990. spin_unlock_irqrestore(&priv->sta_lock, flags);
  991. IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
  992. iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
  993. return 0;
  994. }
  995. static int iwl3945_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
  996. {
  997. unsigned long flags;
  998. spin_lock_irqsave(&priv->sta_lock, flags);
  999. memset(&priv->stations_39[sta_id].keyinfo, 0, sizeof(struct iwl3945_hw_key));
  1000. memset(&priv->stations_39[sta_id].sta.key, 0,
  1001. sizeof(struct iwl4965_keyinfo));
  1002. priv->stations_39[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
  1003. priv->stations_39[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1004. priv->stations_39[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1005. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1006. IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
  1007. iwl3945_send_add_station(priv, &priv->stations_39[sta_id].sta, 0);
  1008. return 0;
  1009. }
  1010. static void iwl3945_clear_free_frames(struct iwl_priv *priv)
  1011. {
  1012. struct list_head *element;
  1013. IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
  1014. priv->frames_count);
  1015. while (!list_empty(&priv->free_frames)) {
  1016. element = priv->free_frames.next;
  1017. list_del(element);
  1018. kfree(list_entry(element, struct iwl3945_frame, list));
  1019. priv->frames_count--;
  1020. }
  1021. if (priv->frames_count) {
  1022. IWL_WARN(priv, "%d frames still in use. Did we lose one?\n",
  1023. priv->frames_count);
  1024. priv->frames_count = 0;
  1025. }
  1026. }
  1027. static struct iwl3945_frame *iwl3945_get_free_frame(struct iwl_priv *priv)
  1028. {
  1029. struct iwl3945_frame *frame;
  1030. struct list_head *element;
  1031. if (list_empty(&priv->free_frames)) {
  1032. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  1033. if (!frame) {
  1034. IWL_ERR(priv, "Could not allocate frame!\n");
  1035. return NULL;
  1036. }
  1037. priv->frames_count++;
  1038. return frame;
  1039. }
  1040. element = priv->free_frames.next;
  1041. list_del(element);
  1042. return list_entry(element, struct iwl3945_frame, list);
  1043. }
  1044. static void iwl3945_free_frame(struct iwl_priv *priv, struct iwl3945_frame *frame)
  1045. {
  1046. memset(frame, 0, sizeof(*frame));
  1047. list_add(&frame->list, &priv->free_frames);
  1048. }
  1049. unsigned int iwl3945_fill_beacon_frame(struct iwl_priv *priv,
  1050. struct ieee80211_hdr *hdr,
  1051. int left)
  1052. {
  1053. if (!iwl3945_is_associated(priv) || !priv->ibss_beacon ||
  1054. ((priv->iw_mode != NL80211_IFTYPE_ADHOC) &&
  1055. (priv->iw_mode != NL80211_IFTYPE_AP)))
  1056. return 0;
  1057. if (priv->ibss_beacon->len > left)
  1058. return 0;
  1059. memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
  1060. return priv->ibss_beacon->len;
  1061. }
  1062. static u8 iwl3945_rate_get_lowest_plcp(struct iwl_priv *priv)
  1063. {
  1064. u8 i;
  1065. int rate_mask;
  1066. /* Set rate mask*/
  1067. if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
  1068. rate_mask = priv->active_rate_basic & IWL_CCK_RATES_MASK;
  1069. else
  1070. rate_mask = priv->active_rate_basic & IWL_OFDM_RATES_MASK;
  1071. for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
  1072. i = iwl3945_rates[i].next_ieee) {
  1073. if (rate_mask & (1 << i))
  1074. return iwl3945_rates[i].plcp;
  1075. }
  1076. /* No valid rate was found. Assign the lowest one */
  1077. if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK)
  1078. return IWL_RATE_1M_PLCP;
  1079. else
  1080. return IWL_RATE_6M_PLCP;
  1081. }
  1082. static int iwl3945_send_beacon_cmd(struct iwl_priv *priv)
  1083. {
  1084. struct iwl3945_frame *frame;
  1085. unsigned int frame_size;
  1086. int rc;
  1087. u8 rate;
  1088. frame = iwl3945_get_free_frame(priv);
  1089. if (!frame) {
  1090. IWL_ERR(priv, "Could not obtain free frame buffer for beacon "
  1091. "command.\n");
  1092. return -ENOMEM;
  1093. }
  1094. rate = iwl3945_rate_get_lowest_plcp(priv);
  1095. frame_size = iwl3945_hw_get_beacon_cmd(priv, frame, rate);
  1096. rc = iwl3945_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
  1097. &frame->u.cmd[0]);
  1098. iwl3945_free_frame(priv, frame);
  1099. return rc;
  1100. }
  1101. /******************************************************************************
  1102. *
  1103. * EEPROM related functions
  1104. *
  1105. ******************************************************************************/
  1106. static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
  1107. {
  1108. memcpy(mac, priv->eeprom39.mac_address, 6);
  1109. }
  1110. /*
  1111. * Clear the OWNER_MSK, to establish driver (instead of uCode running on
  1112. * embedded controller) as EEPROM reader; each read is a series of pulses
  1113. * to/from the EEPROM chip, not a single event, so even reads could conflict
  1114. * if they weren't arbitrated by some ownership mechanism. Here, the driver
  1115. * simply claims ownership, which should be safe when this function is called
  1116. * (i.e. before loading uCode!).
  1117. */
  1118. static inline int iwl3945_eeprom_acquire_semaphore(struct iwl_priv *priv)
  1119. {
  1120. _iwl_clear_bit(priv, CSR_EEPROM_GP, CSR_EEPROM_GP_IF_OWNER_MSK);
  1121. return 0;
  1122. }
  1123. /**
  1124. * iwl3945_eeprom_init - read EEPROM contents
  1125. *
  1126. * Load the EEPROM contents from adapter into priv->eeprom39
  1127. *
  1128. * NOTE: This routine uses the non-debug IO access functions.
  1129. */
  1130. int iwl3945_eeprom_init(struct iwl_priv *priv)
  1131. {
  1132. u16 *e = (u16 *)&priv->eeprom39;
  1133. u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
  1134. int sz = sizeof(priv->eeprom39);
  1135. int ret;
  1136. u16 addr;
  1137. /* The EEPROM structure has several padding buffers within it
  1138. * and when adding new EEPROM maps is subject to programmer errors
  1139. * which may be very difficult to identify without explicitly
  1140. * checking the resulting size of the eeprom map. */
  1141. BUILD_BUG_ON(sizeof(priv->eeprom39) != IWL_EEPROM_IMAGE_SIZE);
  1142. if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
  1143. IWL_ERR(priv, "EEPROM not found, EEPROM_GP=0x%08x\n", gp);
  1144. return -ENOENT;
  1145. }
  1146. /* Make sure driver (instead of uCode) is allowed to read EEPROM */
  1147. ret = iwl3945_eeprom_acquire_semaphore(priv);
  1148. if (ret < 0) {
  1149. IWL_ERR(priv, "Failed to acquire EEPROM semaphore.\n");
  1150. return -ENOENT;
  1151. }
  1152. /* eeprom is an array of 16bit values */
  1153. for (addr = 0; addr < sz; addr += sizeof(u16)) {
  1154. u32 r;
  1155. _iwl_write32(priv, CSR_EEPROM_REG,
  1156. CSR_EEPROM_REG_MSK_ADDR & (addr << 1));
  1157. _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
  1158. ret = iwl_poll_direct_bit(priv, CSR_EEPROM_REG,
  1159. CSR_EEPROM_REG_READ_VALID_MSK,
  1160. IWL_EEPROM_ACCESS_TIMEOUT);
  1161. if (ret < 0) {
  1162. IWL_ERR(priv, "Time out reading EEPROM[%d]\n", addr);
  1163. return ret;
  1164. }
  1165. r = _iwl_read_direct32(priv, CSR_EEPROM_REG);
  1166. e[addr / 2] = le16_to_cpu((__force __le16)(r >> 16));
  1167. }
  1168. return 0;
  1169. }
  1170. static void iwl3945_unset_hw_params(struct iwl_priv *priv)
  1171. {
  1172. if (priv->shared_virt)
  1173. pci_free_consistent(priv->pci_dev,
  1174. sizeof(struct iwl3945_shared),
  1175. priv->shared_virt,
  1176. priv->shared_phys);
  1177. }
  1178. /**
  1179. * iwl3945_supported_rate_to_ie - fill in the supported rate in IE field
  1180. *
  1181. * return : set the bit for each supported rate insert in ie
  1182. */
  1183. static u16 iwl3945_supported_rate_to_ie(u8 *ie, u16 supported_rate,
  1184. u16 basic_rate, int *left)
  1185. {
  1186. u16 ret_rates = 0, bit;
  1187. int i;
  1188. u8 *cnt = ie;
  1189. u8 *rates = ie + 1;
  1190. for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
  1191. if (bit & supported_rate) {
  1192. ret_rates |= bit;
  1193. rates[*cnt] = iwl3945_rates[i].ieee |
  1194. ((bit & basic_rate) ? 0x80 : 0x00);
  1195. (*cnt)++;
  1196. (*left)--;
  1197. if ((*left <= 0) ||
  1198. (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
  1199. break;
  1200. }
  1201. }
  1202. return ret_rates;
  1203. }
  1204. /**
  1205. * iwl3945_fill_probe_req - fill in all required fields and IE for probe request
  1206. */
  1207. static u16 iwl3945_fill_probe_req(struct iwl_priv *priv,
  1208. struct ieee80211_mgmt *frame,
  1209. int left)
  1210. {
  1211. int len = 0;
  1212. u8 *pos = NULL;
  1213. u16 active_rates, ret_rates, cck_rates;
  1214. /* Make sure there is enough space for the probe request,
  1215. * two mandatory IEs and the data */
  1216. left -= 24;
  1217. if (left < 0)
  1218. return 0;
  1219. len += 24;
  1220. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  1221. memcpy(frame->da, iwl_bcast_addr, ETH_ALEN);
  1222. memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
  1223. memcpy(frame->bssid, iwl_bcast_addr, ETH_ALEN);
  1224. frame->seq_ctrl = 0;
  1225. /* fill in our indirect SSID IE */
  1226. /* ...next IE... */
  1227. left -= 2;
  1228. if (left < 0)
  1229. return 0;
  1230. len += 2;
  1231. pos = &(frame->u.probe_req.variable[0]);
  1232. *pos++ = WLAN_EID_SSID;
  1233. *pos++ = 0;
  1234. /* fill in supported rate */
  1235. /* ...next IE... */
  1236. left -= 2;
  1237. if (left < 0)
  1238. return 0;
  1239. /* ... fill it in... */
  1240. *pos++ = WLAN_EID_SUPP_RATES;
  1241. *pos = 0;
  1242. priv->active_rate = priv->rates_mask;
  1243. active_rates = priv->active_rate;
  1244. priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
  1245. cck_rates = IWL_CCK_RATES_MASK & active_rates;
  1246. ret_rates = iwl3945_supported_rate_to_ie(pos, cck_rates,
  1247. priv->active_rate_basic, &left);
  1248. active_rates &= ~ret_rates;
  1249. ret_rates = iwl3945_supported_rate_to_ie(pos, active_rates,
  1250. priv->active_rate_basic, &left);
  1251. active_rates &= ~ret_rates;
  1252. len += 2 + *pos;
  1253. pos += (*pos) + 1;
  1254. if (active_rates == 0)
  1255. goto fill_end;
  1256. /* fill in supported extended rate */
  1257. /* ...next IE... */
  1258. left -= 2;
  1259. if (left < 0)
  1260. return 0;
  1261. /* ... fill it in... */
  1262. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1263. *pos = 0;
  1264. iwl3945_supported_rate_to_ie(pos, active_rates,
  1265. priv->active_rate_basic, &left);
  1266. if (*pos > 0)
  1267. len += 2 + *pos;
  1268. fill_end:
  1269. return (u16)len;
  1270. }
  1271. /*
  1272. * QoS support
  1273. */
  1274. static int iwl3945_send_qos_params_command(struct iwl_priv *priv,
  1275. struct iwl_qosparam_cmd *qos)
  1276. {
  1277. return iwl3945_send_cmd_pdu(priv, REPLY_QOS_PARAM,
  1278. sizeof(struct iwl_qosparam_cmd), qos);
  1279. }
  1280. static void iwl3945_activate_qos(struct iwl_priv *priv, u8 force)
  1281. {
  1282. unsigned long flags;
  1283. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1284. return;
  1285. spin_lock_irqsave(&priv->lock, flags);
  1286. priv->qos_data.def_qos_parm.qos_flags = 0;
  1287. if (priv->qos_data.qos_cap.q_AP.queue_request &&
  1288. !priv->qos_data.qos_cap.q_AP.txop_request)
  1289. priv->qos_data.def_qos_parm.qos_flags |=
  1290. QOS_PARAM_FLG_TXOP_TYPE_MSK;
  1291. if (priv->qos_data.qos_active)
  1292. priv->qos_data.def_qos_parm.qos_flags |=
  1293. QOS_PARAM_FLG_UPDATE_EDCA_MSK;
  1294. spin_unlock_irqrestore(&priv->lock, flags);
  1295. if (force || iwl3945_is_associated(priv)) {
  1296. IWL_DEBUG_QOS("send QoS cmd with QoS active %d \n",
  1297. priv->qos_data.qos_active);
  1298. iwl3945_send_qos_params_command(priv,
  1299. &(priv->qos_data.def_qos_parm));
  1300. }
  1301. }
  1302. /*
  1303. * Power management (not Tx power!) functions
  1304. */
  1305. #define MSEC_TO_USEC 1024
  1306. #define NOSLP __constant_cpu_to_le16(0), 0, 0
  1307. #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK, 0, 0
  1308. #define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
  1309. #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
  1310. __constant_cpu_to_le32(X1), \
  1311. __constant_cpu_to_le32(X2), \
  1312. __constant_cpu_to_le32(X3), \
  1313. __constant_cpu_to_le32(X4)}
  1314. /* default power management (not Tx power) table values */
  1315. /* for TIM 0-10 */
  1316. static struct iwl_power_vec_entry range_0[IWL39_POWER_AC] = {
  1317. {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  1318. {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
  1319. {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
  1320. {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
  1321. {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
  1322. {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
  1323. };
  1324. /* for TIM > 10 */
  1325. static struct iwl_power_vec_entry range_1[IWL39_POWER_AC] = {
  1326. {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  1327. {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
  1328. SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
  1329. {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
  1330. SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
  1331. {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
  1332. SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
  1333. {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  1334. {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
  1335. SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
  1336. };
  1337. int iwl3945_power_init_handle(struct iwl_priv *priv)
  1338. {
  1339. int rc = 0, i;
  1340. struct iwl3945_power_mgr *pow_data;
  1341. int size = sizeof(struct iwl_power_vec_entry) * IWL39_POWER_AC;
  1342. u16 pci_pm;
  1343. IWL_DEBUG_POWER("Initialize power \n");
  1344. pow_data = &(priv->power_data_39);
  1345. memset(pow_data, 0, sizeof(*pow_data));
  1346. pow_data->active_index = IWL_POWER_RANGE_0;
  1347. pow_data->dtim_val = 0xffff;
  1348. memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
  1349. memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
  1350. rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
  1351. if (rc != 0)
  1352. return 0;
  1353. else {
  1354. struct iwl_powertable_cmd *cmd;
  1355. IWL_DEBUG_POWER("adjust power command flags\n");
  1356. for (i = 0; i < IWL39_POWER_AC; i++) {
  1357. cmd = &pow_data->pwr_range_0[i].cmd;
  1358. if (pci_pm & 0x1)
  1359. cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
  1360. else
  1361. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  1362. }
  1363. }
  1364. return rc;
  1365. }
  1366. static int iwl3945_update_power_cmd(struct iwl_priv *priv,
  1367. struct iwl_powertable_cmd *cmd, u32 mode)
  1368. {
  1369. int rc = 0, i;
  1370. u8 skip;
  1371. u32 max_sleep = 0;
  1372. struct iwl_power_vec_entry *range;
  1373. u8 period = 0;
  1374. struct iwl3945_power_mgr *pow_data;
  1375. if (mode > IWL_POWER_INDEX_5) {
  1376. IWL_DEBUG_POWER("Error invalid power mode \n");
  1377. return -1;
  1378. }
  1379. pow_data = &(priv->power_data_39);
  1380. if (pow_data->active_index == IWL_POWER_RANGE_0)
  1381. range = &pow_data->pwr_range_0[0];
  1382. else
  1383. range = &pow_data->pwr_range_1[1];
  1384. memcpy(cmd, &range[mode].cmd, sizeof(struct iwl3945_powertable_cmd));
  1385. #ifdef IWL_MAC80211_DISABLE
  1386. if (priv->assoc_network != NULL) {
  1387. unsigned long flags;
  1388. period = priv->assoc_network->tim.tim_period;
  1389. }
  1390. #endif /*IWL_MAC80211_DISABLE */
  1391. skip = range[mode].no_dtim;
  1392. if (period == 0) {
  1393. period = 1;
  1394. skip = 0;
  1395. }
  1396. if (skip == 0) {
  1397. max_sleep = period;
  1398. cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
  1399. } else {
  1400. __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
  1401. max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
  1402. cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
  1403. }
  1404. for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
  1405. if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
  1406. cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
  1407. }
  1408. IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
  1409. IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
  1410. IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
  1411. IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
  1412. le32_to_cpu(cmd->sleep_interval[0]),
  1413. le32_to_cpu(cmd->sleep_interval[1]),
  1414. le32_to_cpu(cmd->sleep_interval[2]),
  1415. le32_to_cpu(cmd->sleep_interval[3]),
  1416. le32_to_cpu(cmd->sleep_interval[4]));
  1417. return rc;
  1418. }
  1419. static int iwl3945_send_power_mode(struct iwl_priv *priv, u32 mode)
  1420. {
  1421. u32 uninitialized_var(final_mode);
  1422. int rc;
  1423. struct iwl_powertable_cmd cmd;
  1424. /* If on battery, set to 3,
  1425. * if plugged into AC power, set to CAM ("continuously aware mode"),
  1426. * else user level */
  1427. switch (mode) {
  1428. case IWL39_POWER_BATTERY:
  1429. final_mode = IWL_POWER_INDEX_3;
  1430. break;
  1431. case IWL39_POWER_AC:
  1432. final_mode = IWL_POWER_MODE_CAM;
  1433. break;
  1434. default:
  1435. final_mode = mode;
  1436. break;
  1437. }
  1438. iwl3945_update_power_cmd(priv, &cmd, final_mode);
  1439. /* FIXME use get_hcmd_size 3945 command is 4 bytes shorter */
  1440. rc = iwl3945_send_cmd_pdu(priv, POWER_TABLE_CMD,
  1441. sizeof(struct iwl3945_powertable_cmd), &cmd);
  1442. if (final_mode == IWL_POWER_MODE_CAM)
  1443. clear_bit(STATUS_POWER_PMI, &priv->status);
  1444. else
  1445. set_bit(STATUS_POWER_PMI, &priv->status);
  1446. return rc;
  1447. }
  1448. /**
  1449. * iwl3945_scan_cancel - Cancel any currently executing HW scan
  1450. *
  1451. * NOTE: priv->mutex is not required before calling this function
  1452. */
  1453. static int iwl3945_scan_cancel(struct iwl_priv *priv)
  1454. {
  1455. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  1456. clear_bit(STATUS_SCANNING, &priv->status);
  1457. return 0;
  1458. }
  1459. if (test_bit(STATUS_SCANNING, &priv->status)) {
  1460. if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1461. IWL_DEBUG_SCAN("Queuing scan abort.\n");
  1462. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  1463. queue_work(priv->workqueue, &priv->abort_scan);
  1464. } else
  1465. IWL_DEBUG_SCAN("Scan abort already in progress.\n");
  1466. return test_bit(STATUS_SCANNING, &priv->status);
  1467. }
  1468. return 0;
  1469. }
  1470. /**
  1471. * iwl3945_scan_cancel_timeout - Cancel any currently executing HW scan
  1472. * @ms: amount of time to wait (in milliseconds) for scan to abort
  1473. *
  1474. * NOTE: priv->mutex must be held before calling this function
  1475. */
  1476. static int iwl3945_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
  1477. {
  1478. unsigned long now = jiffies;
  1479. int ret;
  1480. ret = iwl3945_scan_cancel(priv);
  1481. if (ret && ms) {
  1482. mutex_unlock(&priv->mutex);
  1483. while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
  1484. test_bit(STATUS_SCANNING, &priv->status))
  1485. msleep(1);
  1486. mutex_lock(&priv->mutex);
  1487. return test_bit(STATUS_SCANNING, &priv->status);
  1488. }
  1489. return ret;
  1490. }
  1491. #define MAX_UCODE_BEACON_INTERVAL 1024
  1492. #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
  1493. static __le16 iwl3945_adjust_beacon_interval(u16 beacon_val)
  1494. {
  1495. u16 new_val = 0;
  1496. u16 beacon_factor = 0;
  1497. beacon_factor =
  1498. (beacon_val + MAX_UCODE_BEACON_INTERVAL)
  1499. / MAX_UCODE_BEACON_INTERVAL;
  1500. new_val = beacon_val / beacon_factor;
  1501. return cpu_to_le16(new_val);
  1502. }
  1503. static void iwl3945_setup_rxon_timing(struct iwl_priv *priv)
  1504. {
  1505. u64 interval_tm_unit;
  1506. u64 tsf, result;
  1507. unsigned long flags;
  1508. struct ieee80211_conf *conf = NULL;
  1509. u16 beacon_int = 0;
  1510. conf = ieee80211_get_hw_conf(priv->hw);
  1511. spin_lock_irqsave(&priv->lock, flags);
  1512. priv->rxon_timing.timestamp = cpu_to_le64(priv->timestamp);
  1513. priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
  1514. tsf = priv->timestamp;
  1515. beacon_int = priv->beacon_int;
  1516. spin_unlock_irqrestore(&priv->lock, flags);
  1517. if (priv->iw_mode == NL80211_IFTYPE_STATION) {
  1518. if (beacon_int == 0) {
  1519. priv->rxon_timing.beacon_interval = cpu_to_le16(100);
  1520. priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
  1521. } else {
  1522. priv->rxon_timing.beacon_interval =
  1523. cpu_to_le16(beacon_int);
  1524. priv->rxon_timing.beacon_interval =
  1525. iwl3945_adjust_beacon_interval(
  1526. le16_to_cpu(priv->rxon_timing.beacon_interval));
  1527. }
  1528. priv->rxon_timing.atim_window = 0;
  1529. } else {
  1530. priv->rxon_timing.beacon_interval =
  1531. iwl3945_adjust_beacon_interval(conf->beacon_int);
  1532. /* TODO: we need to get atim_window from upper stack
  1533. * for now we set to 0 */
  1534. priv->rxon_timing.atim_window = 0;
  1535. }
  1536. interval_tm_unit =
  1537. (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
  1538. result = do_div(tsf, interval_tm_unit);
  1539. priv->rxon_timing.beacon_init_val =
  1540. cpu_to_le32((u32) ((u64) interval_tm_unit - result));
  1541. IWL_DEBUG_ASSOC
  1542. ("beacon interval %d beacon timer %d beacon tim %d\n",
  1543. le16_to_cpu(priv->rxon_timing.beacon_interval),
  1544. le32_to_cpu(priv->rxon_timing.beacon_init_val),
  1545. le16_to_cpu(priv->rxon_timing.atim_window));
  1546. }
  1547. static int iwl3945_scan_initiate(struct iwl_priv *priv)
  1548. {
  1549. if (!iwl_is_ready_rf(priv)) {
  1550. IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
  1551. return -EIO;
  1552. }
  1553. if (test_bit(STATUS_SCANNING, &priv->status)) {
  1554. IWL_DEBUG_SCAN("Scan already in progress.\n");
  1555. return -EAGAIN;
  1556. }
  1557. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1558. IWL_DEBUG_SCAN("Scan request while abort pending. "
  1559. "Queuing.\n");
  1560. return -EAGAIN;
  1561. }
  1562. IWL_DEBUG_INFO("Starting scan...\n");
  1563. if (priv->cfg->sku & IWL_SKU_G)
  1564. priv->scan_bands |= BIT(IEEE80211_BAND_2GHZ);
  1565. if (priv->cfg->sku & IWL_SKU_A)
  1566. priv->scan_bands |= BIT(IEEE80211_BAND_5GHZ);
  1567. set_bit(STATUS_SCANNING, &priv->status);
  1568. priv->scan_start = jiffies;
  1569. priv->scan_pass_start = priv->scan_start;
  1570. queue_work(priv->workqueue, &priv->request_scan);
  1571. return 0;
  1572. }
  1573. static int iwl3945_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
  1574. {
  1575. struct iwl3945_rxon_cmd *rxon = &priv->staging39_rxon;
  1576. if (hw_decrypt)
  1577. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  1578. else
  1579. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  1580. return 0;
  1581. }
  1582. static void iwl3945_set_flags_for_phymode(struct iwl_priv *priv,
  1583. enum ieee80211_band band)
  1584. {
  1585. if (band == IEEE80211_BAND_5GHZ) {
  1586. priv->staging39_rxon.flags &=
  1587. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
  1588. | RXON_FLG_CCK_MSK);
  1589. priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  1590. } else {
  1591. /* Copied from iwl3945_bg_post_associate() */
  1592. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
  1593. priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  1594. else
  1595. priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  1596. if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
  1597. priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  1598. priv->staging39_rxon.flags |= RXON_FLG_BAND_24G_MSK;
  1599. priv->staging39_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
  1600. priv->staging39_rxon.flags &= ~RXON_FLG_CCK_MSK;
  1601. }
  1602. }
  1603. /*
  1604. * initialize rxon structure with default values from eeprom
  1605. */
  1606. static void iwl3945_connection_init_rx_config(struct iwl_priv *priv,
  1607. int mode)
  1608. {
  1609. const struct iwl_channel_info *ch_info;
  1610. memset(&priv->staging39_rxon, 0, sizeof(priv->staging39_rxon));
  1611. switch (mode) {
  1612. case NL80211_IFTYPE_AP:
  1613. priv->staging39_rxon.dev_type = RXON_DEV_TYPE_AP;
  1614. break;
  1615. case NL80211_IFTYPE_STATION:
  1616. priv->staging39_rxon.dev_type = RXON_DEV_TYPE_ESS;
  1617. priv->staging39_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  1618. break;
  1619. case NL80211_IFTYPE_ADHOC:
  1620. priv->staging39_rxon.dev_type = RXON_DEV_TYPE_IBSS;
  1621. priv->staging39_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  1622. priv->staging39_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
  1623. RXON_FILTER_ACCEPT_GRP_MSK;
  1624. break;
  1625. case NL80211_IFTYPE_MONITOR:
  1626. priv->staging39_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
  1627. priv->staging39_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
  1628. RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
  1629. break;
  1630. default:
  1631. IWL_ERR(priv, "Unsupported interface type %d\n", mode);
  1632. break;
  1633. }
  1634. #if 0
  1635. /* TODO: Figure out when short_preamble would be set and cache from
  1636. * that */
  1637. if (!hw_to_local(priv->hw)->short_preamble)
  1638. priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  1639. else
  1640. priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  1641. #endif
  1642. ch_info = iwl3945_get_channel_info(priv, priv->band,
  1643. le16_to_cpu(priv->active39_rxon.channel));
  1644. if (!ch_info)
  1645. ch_info = &priv->channel_info[0];
  1646. /*
  1647. * in some case A channels are all non IBSS
  1648. * in this case force B/G channel
  1649. */
  1650. if ((mode == NL80211_IFTYPE_ADHOC) && !(is_channel_ibss(ch_info)))
  1651. ch_info = &priv->channel_info[0];
  1652. priv->staging39_rxon.channel = cpu_to_le16(ch_info->channel);
  1653. if (is_channel_a_band(ch_info))
  1654. priv->band = IEEE80211_BAND_5GHZ;
  1655. else
  1656. priv->band = IEEE80211_BAND_2GHZ;
  1657. iwl3945_set_flags_for_phymode(priv, priv->band);
  1658. priv->staging39_rxon.ofdm_basic_rates =
  1659. (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  1660. priv->staging39_rxon.cck_basic_rates =
  1661. (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  1662. }
  1663. static int iwl3945_set_mode(struct iwl_priv *priv, int mode)
  1664. {
  1665. if (mode == NL80211_IFTYPE_ADHOC) {
  1666. const struct iwl_channel_info *ch_info;
  1667. ch_info = iwl3945_get_channel_info(priv,
  1668. priv->band,
  1669. le16_to_cpu(priv->staging39_rxon.channel));
  1670. if (!ch_info || !is_channel_ibss(ch_info)) {
  1671. IWL_ERR(priv, "channel %d not IBSS channel\n",
  1672. le16_to_cpu(priv->staging39_rxon.channel));
  1673. return -EINVAL;
  1674. }
  1675. }
  1676. iwl3945_connection_init_rx_config(priv, mode);
  1677. memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
  1678. iwl3945_clear_stations_table(priv);
  1679. /* don't commit rxon if rf-kill is on*/
  1680. if (!iwl_is_ready_rf(priv))
  1681. return -EAGAIN;
  1682. cancel_delayed_work(&priv->scan_check);
  1683. if (iwl3945_scan_cancel_timeout(priv, 100)) {
  1684. IWL_WARN(priv, "Aborted scan still in progress after 100ms\n");
  1685. IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
  1686. return -EAGAIN;
  1687. }
  1688. iwl3945_commit_rxon(priv);
  1689. return 0;
  1690. }
  1691. static void iwl3945_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
  1692. struct ieee80211_tx_info *info,
  1693. struct iwl_cmd *cmd,
  1694. struct sk_buff *skb_frag,
  1695. int last_frag)
  1696. {
  1697. struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  1698. struct iwl3945_hw_key *keyinfo =
  1699. &priv->stations_39[info->control.hw_key->hw_key_idx].keyinfo;
  1700. switch (keyinfo->alg) {
  1701. case ALG_CCMP:
  1702. tx->sec_ctl = TX_CMD_SEC_CCM;
  1703. memcpy(tx->key, keyinfo->key, keyinfo->keylen);
  1704. IWL_DEBUG_TX("tx_cmd with AES hwcrypto\n");
  1705. break;
  1706. case ALG_TKIP:
  1707. #if 0
  1708. tx->sec_ctl = TX_CMD_SEC_TKIP;
  1709. if (last_frag)
  1710. memcpy(tx->tkip_mic.byte, skb_frag->tail - 8,
  1711. 8);
  1712. else
  1713. memset(tx->tkip_mic.byte, 0, 8);
  1714. #endif
  1715. break;
  1716. case ALG_WEP:
  1717. tx->sec_ctl = TX_CMD_SEC_WEP |
  1718. (info->control.hw_key->hw_key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
  1719. if (keyinfo->keylen == 13)
  1720. tx->sec_ctl |= TX_CMD_SEC_KEY128;
  1721. memcpy(&tx->key[3], keyinfo->key, keyinfo->keylen);
  1722. IWL_DEBUG_TX("Configuring packet for WEP encryption "
  1723. "with key %d\n", info->control.hw_key->hw_key_idx);
  1724. break;
  1725. default:
  1726. IWL_ERR(priv, "Unknown encode alg %d\n", keyinfo->alg);
  1727. break;
  1728. }
  1729. }
  1730. /*
  1731. * handle build REPLY_TX command notification.
  1732. */
  1733. static void iwl3945_build_tx_cmd_basic(struct iwl_priv *priv,
  1734. struct iwl_cmd *cmd,
  1735. struct ieee80211_tx_info *info,
  1736. struct ieee80211_hdr *hdr, u8 std_id)
  1737. {
  1738. struct iwl3945_tx_cmd *tx = (struct iwl3945_tx_cmd *)cmd->cmd.payload;
  1739. __le32 tx_flags = tx->tx_flags;
  1740. __le16 fc = hdr->frame_control;
  1741. u8 rc_flags = info->control.rates[0].flags;
  1742. tx->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1743. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  1744. tx_flags |= TX_CMD_FLG_ACK_MSK;
  1745. if (ieee80211_is_mgmt(fc))
  1746. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  1747. if (ieee80211_is_probe_resp(fc) &&
  1748. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  1749. tx_flags |= TX_CMD_FLG_TSF_MSK;
  1750. } else {
  1751. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  1752. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  1753. }
  1754. tx->sta_id = std_id;
  1755. if (ieee80211_has_morefrags(fc))
  1756. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  1757. if (ieee80211_is_data_qos(fc)) {
  1758. u8 *qc = ieee80211_get_qos_ctl(hdr);
  1759. tx->tid_tspec = qc[0] & 0xf;
  1760. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  1761. } else {
  1762. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  1763. }
  1764. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  1765. tx_flags |= TX_CMD_FLG_RTS_MSK;
  1766. tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  1767. } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  1768. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  1769. tx_flags |= TX_CMD_FLG_CTS_MSK;
  1770. }
  1771. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  1772. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  1773. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  1774. if (ieee80211_is_mgmt(fc)) {
  1775. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  1776. tx->timeout.pm_frame_timeout = cpu_to_le16(3);
  1777. else
  1778. tx->timeout.pm_frame_timeout = cpu_to_le16(2);
  1779. } else {
  1780. tx->timeout.pm_frame_timeout = 0;
  1781. #ifdef CONFIG_IWL3945_LEDS
  1782. priv->rxtxpackets += le16_to_cpu(cmd->cmd.tx.len);
  1783. #endif
  1784. }
  1785. tx->driver_txop = 0;
  1786. tx->tx_flags = tx_flags;
  1787. tx->next_frame_len = 0;
  1788. }
  1789. /**
  1790. * iwl3945_get_sta_id - Find station's index within station table
  1791. */
  1792. static int iwl3945_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  1793. {
  1794. int sta_id;
  1795. u16 fc = le16_to_cpu(hdr->frame_control);
  1796. /* If this frame is broadcast or management, use broadcast station id */
  1797. if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
  1798. is_multicast_ether_addr(hdr->addr1))
  1799. return priv->hw_params.bcast_sta_id;
  1800. switch (priv->iw_mode) {
  1801. /* If we are a client station in a BSS network, use the special
  1802. * AP station entry (that's the only station we communicate with) */
  1803. case NL80211_IFTYPE_STATION:
  1804. return IWL_AP_ID;
  1805. /* If we are an AP, then find the station, or use BCAST */
  1806. case NL80211_IFTYPE_AP:
  1807. sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
  1808. if (sta_id != IWL_INVALID_STATION)
  1809. return sta_id;
  1810. return priv->hw_params.bcast_sta_id;
  1811. /* If this frame is going out to an IBSS network, find the station,
  1812. * or create a new station table entry */
  1813. case NL80211_IFTYPE_ADHOC: {
  1814. /* Create new station table entry */
  1815. sta_id = iwl3945_hw_find_station(priv, hdr->addr1);
  1816. if (sta_id != IWL_INVALID_STATION)
  1817. return sta_id;
  1818. sta_id = iwl3945_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
  1819. if (sta_id != IWL_INVALID_STATION)
  1820. return sta_id;
  1821. IWL_DEBUG_DROP("Station %pM not in station map. "
  1822. "Defaulting to broadcast...\n",
  1823. hdr->addr1);
  1824. iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  1825. return priv->hw_params.bcast_sta_id;
  1826. }
  1827. /* If we are in monitor mode, use BCAST. This is required for
  1828. * packet injection. */
  1829. case NL80211_IFTYPE_MONITOR:
  1830. return priv->hw_params.bcast_sta_id;
  1831. default:
  1832. IWL_WARN(priv, "Unknown mode of operation: %d\n",
  1833. priv->iw_mode);
  1834. return priv->hw_params.bcast_sta_id;
  1835. }
  1836. }
  1837. /*
  1838. * start REPLY_TX command process
  1839. */
  1840. static int iwl3945_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  1841. {
  1842. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1843. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1844. struct iwl3945_tfd *tfd;
  1845. struct iwl3945_tx_cmd *tx;
  1846. struct iwl_tx_queue *txq = NULL;
  1847. struct iwl_queue *q = NULL;
  1848. struct iwl_cmd *out_cmd = NULL;
  1849. dma_addr_t phys_addr;
  1850. dma_addr_t txcmd_phys;
  1851. int txq_id = skb_get_queue_mapping(skb);
  1852. u16 len, idx, len_org, hdr_len;
  1853. u8 id;
  1854. u8 unicast;
  1855. u8 sta_id;
  1856. u8 tid = 0;
  1857. u16 seq_number = 0;
  1858. __le16 fc;
  1859. u8 wait_write_ptr = 0;
  1860. u8 *qc = NULL;
  1861. unsigned long flags;
  1862. int rc;
  1863. spin_lock_irqsave(&priv->lock, flags);
  1864. if (iwl_is_rfkill(priv)) {
  1865. IWL_DEBUG_DROP("Dropping - RF KILL\n");
  1866. goto drop_unlock;
  1867. }
  1868. if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) == IWL_INVALID_RATE) {
  1869. IWL_ERR(priv, "ERROR: No TX rate available.\n");
  1870. goto drop_unlock;
  1871. }
  1872. unicast = !is_multicast_ether_addr(hdr->addr1);
  1873. id = 0;
  1874. fc = hdr->frame_control;
  1875. #ifdef CONFIG_IWL3945_DEBUG
  1876. if (ieee80211_is_auth(fc))
  1877. IWL_DEBUG_TX("Sending AUTH frame\n");
  1878. else if (ieee80211_is_assoc_req(fc))
  1879. IWL_DEBUG_TX("Sending ASSOC frame\n");
  1880. else if (ieee80211_is_reassoc_req(fc))
  1881. IWL_DEBUG_TX("Sending REASSOC frame\n");
  1882. #endif
  1883. /* drop all data frame if we are not associated */
  1884. if (ieee80211_is_data(fc) &&
  1885. (priv->iw_mode != NL80211_IFTYPE_MONITOR) && /* packet injection */
  1886. (!iwl3945_is_associated(priv) ||
  1887. ((priv->iw_mode == NL80211_IFTYPE_STATION) && !priv->assoc_id))) {
  1888. IWL_DEBUG_DROP("Dropping - !iwl3945_is_associated\n");
  1889. goto drop_unlock;
  1890. }
  1891. spin_unlock_irqrestore(&priv->lock, flags);
  1892. hdr_len = ieee80211_hdrlen(fc);
  1893. /* Find (or create) index into station table for destination station */
  1894. sta_id = iwl3945_get_sta_id(priv, hdr);
  1895. if (sta_id == IWL_INVALID_STATION) {
  1896. IWL_DEBUG_DROP("Dropping - INVALID STATION: %pM\n",
  1897. hdr->addr1);
  1898. goto drop;
  1899. }
  1900. IWL_DEBUG_RATE("station Id %d\n", sta_id);
  1901. if (ieee80211_is_data_qos(fc)) {
  1902. qc = ieee80211_get_qos_ctl(hdr);
  1903. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  1904. seq_number = priv->stations_39[sta_id].tid[tid].seq_number &
  1905. IEEE80211_SCTL_SEQ;
  1906. hdr->seq_ctrl = cpu_to_le16(seq_number) |
  1907. (hdr->seq_ctrl &
  1908. __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
  1909. seq_number += 0x10;
  1910. }
  1911. /* Descriptor for chosen Tx queue */
  1912. txq = &priv->txq[txq_id];
  1913. q = &txq->q;
  1914. spin_lock_irqsave(&priv->lock, flags);
  1915. /* Set up first empty TFD within this queue's circular TFD buffer */
  1916. tfd = &txq->tfds39[q->write_ptr];
  1917. memset(tfd, 0, sizeof(*tfd));
  1918. idx = get_cmd_index(q, q->write_ptr, 0);
  1919. /* Set up driver data for this TFD */
  1920. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  1921. txq->txb[q->write_ptr].skb[0] = skb;
  1922. /* Init first empty entry in queue's array of Tx/cmd buffers */
  1923. out_cmd = txq->cmd[idx];
  1924. tx = (struct iwl3945_tx_cmd *)out_cmd->cmd.payload;
  1925. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  1926. memset(tx, 0, sizeof(*tx));
  1927. /*
  1928. * Set up the Tx-command (not MAC!) header.
  1929. * Store the chosen Tx queue and TFD index within the sequence field;
  1930. * after Tx, uCode's Tx response will return this value so driver can
  1931. * locate the frame within the tx queue and do post-tx processing.
  1932. */
  1933. out_cmd->hdr.cmd = REPLY_TX;
  1934. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  1935. INDEX_TO_SEQ(q->write_ptr)));
  1936. /* Copy MAC header from skb into command buffer */
  1937. memcpy(tx->hdr, hdr, hdr_len);
  1938. /*
  1939. * Use the first empty entry in this queue's command buffer array
  1940. * to contain the Tx command and MAC header concatenated together
  1941. * (payload data will be in another buffer).
  1942. * Size of this varies, due to varying MAC header length.
  1943. * If end is not dword aligned, we'll have 2 extra bytes at the end
  1944. * of the MAC header (device reads on dword boundaries).
  1945. * We'll tell device about this padding later.
  1946. */
  1947. len = sizeof(struct iwl3945_tx_cmd) +
  1948. sizeof(struct iwl_cmd_header) + hdr_len;
  1949. len_org = len;
  1950. len = (len + 3) & ~3;
  1951. if (len_org != len)
  1952. len_org = 1;
  1953. else
  1954. len_org = 0;
  1955. /* Physical address of this Tx command's header (not MAC header!),
  1956. * within command buffer array. */
  1957. txcmd_phys = pci_map_single(priv->pci_dev,
  1958. out_cmd, sizeof(struct iwl_cmd),
  1959. PCI_DMA_TODEVICE);
  1960. pci_unmap_addr_set(&out_cmd->meta, mapping, txcmd_phys);
  1961. pci_unmap_len_set(&out_cmd->meta, len, sizeof(struct iwl_cmd));
  1962. /* Add buffer containing Tx command and MAC(!) header to TFD's
  1963. * first entry */
  1964. txcmd_phys += offsetof(struct iwl_cmd, hdr);
  1965. /* Add buffer containing Tx command and MAC(!) header to TFD's
  1966. * first entry */
  1967. iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
  1968. if (info->control.hw_key)
  1969. iwl3945_build_tx_cmd_hwcrypto(priv, info, out_cmd, skb, 0);
  1970. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  1971. * if any (802.11 null frames have no payload). */
  1972. len = skb->len - hdr_len;
  1973. if (len) {
  1974. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  1975. len, PCI_DMA_TODEVICE);
  1976. iwl3945_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
  1977. }
  1978. if (!len)
  1979. /* If there is no payload, then we use only one Tx buffer */
  1980. tfd->control_flags = cpu_to_le32(TFD_CTL_COUNT_SET(1));
  1981. else
  1982. /* Else use 2 buffers.
  1983. * Tell 3945 about any padding after MAC header */
  1984. tfd->control_flags = cpu_to_le32(TFD_CTL_COUNT_SET(2) |
  1985. TFD_CTL_PAD_SET(U32_PAD(len)));
  1986. /* Total # bytes to be transmitted */
  1987. len = (u16)skb->len;
  1988. tx->len = cpu_to_le16(len);
  1989. /* TODO need this for burst mode later on */
  1990. iwl3945_build_tx_cmd_basic(priv, out_cmd, info, hdr, sta_id);
  1991. /* set is_hcca to 0; it probably will never be implemented */
  1992. iwl3945_hw_build_tx_cmd_rate(priv, out_cmd, info, hdr, sta_id, 0);
  1993. tx->tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
  1994. tx->tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
  1995. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  1996. txq->need_update = 1;
  1997. if (qc)
  1998. priv->stations_39[sta_id].tid[tid].seq_number = seq_number;
  1999. } else {
  2000. wait_write_ptr = 1;
  2001. txq->need_update = 0;
  2002. }
  2003. iwl_print_hex_dump(priv, IWL_DL_TX, tx, sizeof(*tx));
  2004. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx->hdr,
  2005. ieee80211_hdrlen(fc));
  2006. /* Tell device the write index *just past* this latest filled TFD */
  2007. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  2008. rc = iwl3945_tx_queue_update_write_ptr(priv, txq);
  2009. spin_unlock_irqrestore(&priv->lock, flags);
  2010. if (rc)
  2011. return rc;
  2012. if ((iwl_queue_space(q) < q->high_mark)
  2013. && priv->mac80211_registered) {
  2014. if (wait_write_ptr) {
  2015. spin_lock_irqsave(&priv->lock, flags);
  2016. txq->need_update = 1;
  2017. iwl3945_tx_queue_update_write_ptr(priv, txq);
  2018. spin_unlock_irqrestore(&priv->lock, flags);
  2019. }
  2020. ieee80211_stop_queue(priv->hw, skb_get_queue_mapping(skb));
  2021. }
  2022. return 0;
  2023. drop_unlock:
  2024. spin_unlock_irqrestore(&priv->lock, flags);
  2025. drop:
  2026. return -1;
  2027. }
  2028. static void iwl3945_set_rate(struct iwl_priv *priv)
  2029. {
  2030. const struct ieee80211_supported_band *sband = NULL;
  2031. struct ieee80211_rate *rate;
  2032. int i;
  2033. sband = iwl_get_hw_mode(priv, priv->band);
  2034. if (!sband) {
  2035. IWL_ERR(priv, "Failed to set rate: unable to get hw mode\n");
  2036. return;
  2037. }
  2038. priv->active_rate = 0;
  2039. priv->active_rate_basic = 0;
  2040. IWL_DEBUG_RATE("Setting rates for %s GHz\n",
  2041. sband->band == IEEE80211_BAND_2GHZ ? "2.4" : "5");
  2042. for (i = 0; i < sband->n_bitrates; i++) {
  2043. rate = &sband->bitrates[i];
  2044. if ((rate->hw_value < IWL_RATE_COUNT) &&
  2045. !(rate->flags & IEEE80211_CHAN_DISABLED)) {
  2046. IWL_DEBUG_RATE("Adding rate index %d (plcp %d)\n",
  2047. rate->hw_value, iwl3945_rates[rate->hw_value].plcp);
  2048. priv->active_rate |= (1 << rate->hw_value);
  2049. }
  2050. }
  2051. IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
  2052. priv->active_rate, priv->active_rate_basic);
  2053. /*
  2054. * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
  2055. * otherwise set it to the default of all CCK rates and 6, 12, 24 for
  2056. * OFDM
  2057. */
  2058. if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
  2059. priv->staging39_rxon.cck_basic_rates =
  2060. ((priv->active_rate_basic &
  2061. IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
  2062. else
  2063. priv->staging39_rxon.cck_basic_rates =
  2064. (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  2065. if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
  2066. priv->staging39_rxon.ofdm_basic_rates =
  2067. ((priv->active_rate_basic &
  2068. (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
  2069. IWL_FIRST_OFDM_RATE) & 0xFF;
  2070. else
  2071. priv->staging39_rxon.ofdm_basic_rates =
  2072. (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  2073. }
  2074. static void iwl3945_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
  2075. {
  2076. unsigned long flags;
  2077. if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
  2078. return;
  2079. IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
  2080. disable_radio ? "OFF" : "ON");
  2081. if (disable_radio) {
  2082. iwl3945_scan_cancel(priv);
  2083. /* FIXME: This is a workaround for AP */
  2084. if (priv->iw_mode != NL80211_IFTYPE_AP) {
  2085. spin_lock_irqsave(&priv->lock, flags);
  2086. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  2087. CSR_UCODE_SW_BIT_RFKILL);
  2088. spin_unlock_irqrestore(&priv->lock, flags);
  2089. iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
  2090. set_bit(STATUS_RF_KILL_SW, &priv->status);
  2091. }
  2092. return;
  2093. }
  2094. spin_lock_irqsave(&priv->lock, flags);
  2095. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2096. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  2097. spin_unlock_irqrestore(&priv->lock, flags);
  2098. /* wake up ucode */
  2099. msleep(10);
  2100. spin_lock_irqsave(&priv->lock, flags);
  2101. iwl_read32(priv, CSR_UCODE_DRV_GP1);
  2102. if (!iwl_grab_nic_access(priv))
  2103. iwl_release_nic_access(priv);
  2104. spin_unlock_irqrestore(&priv->lock, flags);
  2105. if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
  2106. IWL_DEBUG_RF_KILL("Can not turn radio back on - "
  2107. "disabled by HW switch\n");
  2108. return;
  2109. }
  2110. if (priv->is_open)
  2111. queue_work(priv->workqueue, &priv->restart);
  2112. return;
  2113. }
  2114. void iwl3945_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
  2115. u32 decrypt_res, struct ieee80211_rx_status *stats)
  2116. {
  2117. u16 fc =
  2118. le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
  2119. if (priv->active39_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  2120. return;
  2121. if (!(fc & IEEE80211_FCTL_PROTECTED))
  2122. return;
  2123. IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
  2124. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  2125. case RX_RES_STATUS_SEC_TYPE_TKIP:
  2126. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2127. RX_RES_STATUS_BAD_ICV_MIC)
  2128. stats->flag |= RX_FLAG_MMIC_ERROR;
  2129. case RX_RES_STATUS_SEC_TYPE_WEP:
  2130. case RX_RES_STATUS_SEC_TYPE_CCMP:
  2131. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2132. RX_RES_STATUS_DECRYPT_OK) {
  2133. IWL_DEBUG_RX("hw decrypt successfully!!!\n");
  2134. stats->flag |= RX_FLAG_DECRYPTED;
  2135. }
  2136. break;
  2137. default:
  2138. break;
  2139. }
  2140. }
  2141. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  2142. #include "iwl-spectrum.h"
  2143. #define BEACON_TIME_MASK_LOW 0x00FFFFFF
  2144. #define BEACON_TIME_MASK_HIGH 0xFF000000
  2145. #define TIME_UNIT 1024
  2146. /*
  2147. * extended beacon time format
  2148. * time in usec will be changed into a 32-bit value in 8:24 format
  2149. * the high 1 byte is the beacon counts
  2150. * the lower 3 bytes is the time in usec within one beacon interval
  2151. */
  2152. static u32 iwl3945_usecs_to_beacons(u32 usec, u32 beacon_interval)
  2153. {
  2154. u32 quot;
  2155. u32 rem;
  2156. u32 interval = beacon_interval * 1024;
  2157. if (!interval || !usec)
  2158. return 0;
  2159. quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
  2160. rem = (usec % interval) & BEACON_TIME_MASK_LOW;
  2161. return (quot << 24) + rem;
  2162. }
  2163. /* base is usually what we get from ucode with each received frame,
  2164. * the same as HW timer counter counting down
  2165. */
  2166. static __le32 iwl3945_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
  2167. {
  2168. u32 base_low = base & BEACON_TIME_MASK_LOW;
  2169. u32 addon_low = addon & BEACON_TIME_MASK_LOW;
  2170. u32 interval = beacon_interval * TIME_UNIT;
  2171. u32 res = (base & BEACON_TIME_MASK_HIGH) +
  2172. (addon & BEACON_TIME_MASK_HIGH);
  2173. if (base_low > addon_low)
  2174. res += base_low - addon_low;
  2175. else if (base_low < addon_low) {
  2176. res += interval + base_low - addon_low;
  2177. res += (1 << 24);
  2178. } else
  2179. res += (1 << 24);
  2180. return cpu_to_le32(res);
  2181. }
  2182. static int iwl3945_get_measurement(struct iwl_priv *priv,
  2183. struct ieee80211_measurement_params *params,
  2184. u8 type)
  2185. {
  2186. struct iwl_spectrum_cmd spectrum;
  2187. struct iwl_rx_packet *res;
  2188. struct iwl_host_cmd cmd = {
  2189. .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
  2190. .data = (void *)&spectrum,
  2191. .meta.flags = CMD_WANT_SKB,
  2192. };
  2193. u32 add_time = le64_to_cpu(params->start_time);
  2194. int rc;
  2195. int spectrum_resp_status;
  2196. int duration = le16_to_cpu(params->duration);
  2197. if (iwl3945_is_associated(priv))
  2198. add_time =
  2199. iwl3945_usecs_to_beacons(
  2200. le64_to_cpu(params->start_time) - priv->last_tsf,
  2201. le16_to_cpu(priv->rxon_timing.beacon_interval));
  2202. memset(&spectrum, 0, sizeof(spectrum));
  2203. spectrum.channel_count = cpu_to_le16(1);
  2204. spectrum.flags =
  2205. RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
  2206. spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
  2207. cmd.len = sizeof(spectrum);
  2208. spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
  2209. if (iwl3945_is_associated(priv))
  2210. spectrum.start_time =
  2211. iwl3945_add_beacon_time(priv->last_beacon_time,
  2212. add_time,
  2213. le16_to_cpu(priv->rxon_timing.beacon_interval));
  2214. else
  2215. spectrum.start_time = 0;
  2216. spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
  2217. spectrum.channels[0].channel = params->channel;
  2218. spectrum.channels[0].type = type;
  2219. if (priv->active39_rxon.flags & RXON_FLG_BAND_24G_MSK)
  2220. spectrum.flags |= RXON_FLG_BAND_24G_MSK |
  2221. RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
  2222. rc = iwl3945_send_cmd_sync(priv, &cmd);
  2223. if (rc)
  2224. return rc;
  2225. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  2226. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  2227. IWL_ERR(priv, "Bad return from REPLY_RX_ON_ASSOC command\n");
  2228. rc = -EIO;
  2229. }
  2230. spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
  2231. switch (spectrum_resp_status) {
  2232. case 0: /* Command will be handled */
  2233. if (res->u.spectrum.id != 0xff) {
  2234. IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
  2235. res->u.spectrum.id);
  2236. priv->measurement_status &= ~MEASUREMENT_READY;
  2237. }
  2238. priv->measurement_status |= MEASUREMENT_ACTIVE;
  2239. rc = 0;
  2240. break;
  2241. case 1: /* Command will not be handled */
  2242. rc = -EAGAIN;
  2243. break;
  2244. }
  2245. dev_kfree_skb_any(cmd.meta.u.skb);
  2246. return rc;
  2247. }
  2248. #endif
  2249. static void iwl3945_rx_reply_alive(struct iwl_priv *priv,
  2250. struct iwl_rx_mem_buffer *rxb)
  2251. {
  2252. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2253. struct iwl_alive_resp *palive;
  2254. struct delayed_work *pwork;
  2255. palive = &pkt->u.alive_frame;
  2256. IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
  2257. "0x%01X 0x%01X\n",
  2258. palive->is_valid, palive->ver_type,
  2259. palive->ver_subtype);
  2260. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  2261. IWL_DEBUG_INFO("Initialization Alive received.\n");
  2262. memcpy(&priv->card_alive_init, &pkt->u.alive_frame,
  2263. sizeof(struct iwl_alive_resp));
  2264. pwork = &priv->init_alive_start;
  2265. } else {
  2266. IWL_DEBUG_INFO("Runtime Alive received.\n");
  2267. memcpy(&priv->card_alive, &pkt->u.alive_frame,
  2268. sizeof(struct iwl_alive_resp));
  2269. pwork = &priv->alive_start;
  2270. iwl3945_disable_events(priv);
  2271. }
  2272. /* We delay the ALIVE response by 5ms to
  2273. * give the HW RF Kill time to activate... */
  2274. if (palive->is_valid == UCODE_VALID_OK)
  2275. queue_delayed_work(priv->workqueue, pwork,
  2276. msecs_to_jiffies(5));
  2277. else
  2278. IWL_WARN(priv, "uCode did not respond OK.\n");
  2279. }
  2280. static void iwl3945_rx_reply_add_sta(struct iwl_priv *priv,
  2281. struct iwl_rx_mem_buffer *rxb)
  2282. {
  2283. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2284. IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
  2285. return;
  2286. }
  2287. static void iwl3945_rx_reply_error(struct iwl_priv *priv,
  2288. struct iwl_rx_mem_buffer *rxb)
  2289. {
  2290. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2291. IWL_ERR(priv, "Error Reply type 0x%08X cmd %s (0x%02X) "
  2292. "seq 0x%04X ser 0x%08X\n",
  2293. le32_to_cpu(pkt->u.err_resp.error_type),
  2294. get_cmd_string(pkt->u.err_resp.cmd_id),
  2295. pkt->u.err_resp.cmd_id,
  2296. le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
  2297. le32_to_cpu(pkt->u.err_resp.error_info));
  2298. }
  2299. #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
  2300. static void iwl3945_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
  2301. {
  2302. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2303. struct iwl3945_rxon_cmd *rxon = (void *)&priv->active39_rxon;
  2304. struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
  2305. IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
  2306. le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
  2307. rxon->channel = csa->channel;
  2308. priv->staging39_rxon.channel = csa->channel;
  2309. }
  2310. static void iwl3945_rx_spectrum_measure_notif(struct iwl_priv *priv,
  2311. struct iwl_rx_mem_buffer *rxb)
  2312. {
  2313. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  2314. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2315. struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
  2316. if (!report->state) {
  2317. IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
  2318. "Spectrum Measure Notification: Start\n");
  2319. return;
  2320. }
  2321. memcpy(&priv->measure_report, report, sizeof(*report));
  2322. priv->measurement_status |= MEASUREMENT_READY;
  2323. #endif
  2324. }
  2325. static void iwl3945_rx_pm_sleep_notif(struct iwl_priv *priv,
  2326. struct iwl_rx_mem_buffer *rxb)
  2327. {
  2328. #ifdef CONFIG_IWL3945_DEBUG
  2329. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2330. struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
  2331. IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
  2332. sleep->pm_sleep_mode, sleep->pm_wakeup_src);
  2333. #endif
  2334. }
  2335. static void iwl3945_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
  2336. struct iwl_rx_mem_buffer *rxb)
  2337. {
  2338. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2339. IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
  2340. "notification for %s:\n",
  2341. le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
  2342. iwl_print_hex_dump(priv, IWL_DL_RADIO, pkt->u.raw,
  2343. le32_to_cpu(pkt->len));
  2344. }
  2345. static void iwl3945_bg_beacon_update(struct work_struct *work)
  2346. {
  2347. struct iwl_priv *priv =
  2348. container_of(work, struct iwl_priv, beacon_update);
  2349. struct sk_buff *beacon;
  2350. /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
  2351. beacon = ieee80211_beacon_get(priv->hw, priv->vif);
  2352. if (!beacon) {
  2353. IWL_ERR(priv, "update beacon failed\n");
  2354. return;
  2355. }
  2356. mutex_lock(&priv->mutex);
  2357. /* new beacon skb is allocated every time; dispose previous.*/
  2358. if (priv->ibss_beacon)
  2359. dev_kfree_skb(priv->ibss_beacon);
  2360. priv->ibss_beacon = beacon;
  2361. mutex_unlock(&priv->mutex);
  2362. iwl3945_send_beacon_cmd(priv);
  2363. }
  2364. static void iwl3945_rx_beacon_notif(struct iwl_priv *priv,
  2365. struct iwl_rx_mem_buffer *rxb)
  2366. {
  2367. #ifdef CONFIG_IWL3945_DEBUG
  2368. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2369. struct iwl3945_beacon_notif *beacon = &(pkt->u.beacon_status);
  2370. u8 rate = beacon->beacon_notify_hdr.rate;
  2371. IWL_DEBUG_RX("beacon status %x retries %d iss %d "
  2372. "tsf %d %d rate %d\n",
  2373. le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
  2374. beacon->beacon_notify_hdr.failure_frame,
  2375. le32_to_cpu(beacon->ibss_mgr_status),
  2376. le32_to_cpu(beacon->high_tsf),
  2377. le32_to_cpu(beacon->low_tsf), rate);
  2378. #endif
  2379. if ((priv->iw_mode == NL80211_IFTYPE_AP) &&
  2380. (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
  2381. queue_work(priv->workqueue, &priv->beacon_update);
  2382. }
  2383. /* Service response to REPLY_SCAN_CMD (0x80) */
  2384. static void iwl3945_rx_reply_scan(struct iwl_priv *priv,
  2385. struct iwl_rx_mem_buffer *rxb)
  2386. {
  2387. #ifdef CONFIG_IWL3945_DEBUG
  2388. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2389. struct iwl_scanreq_notification *notif =
  2390. (struct iwl_scanreq_notification *)pkt->u.raw;
  2391. IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
  2392. #endif
  2393. }
  2394. /* Service SCAN_START_NOTIFICATION (0x82) */
  2395. static void iwl3945_rx_scan_start_notif(struct iwl_priv *priv,
  2396. struct iwl_rx_mem_buffer *rxb)
  2397. {
  2398. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2399. struct iwl_scanstart_notification *notif =
  2400. (struct iwl_scanstart_notification *)pkt->u.raw;
  2401. priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  2402. IWL_DEBUG_SCAN("Scan start: "
  2403. "%d [802.11%s] "
  2404. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
  2405. notif->channel,
  2406. notif->band ? "bg" : "a",
  2407. notif->tsf_high,
  2408. notif->tsf_low, notif->status, notif->beacon_timer);
  2409. }
  2410. /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
  2411. static void iwl3945_rx_scan_results_notif(struct iwl_priv *priv,
  2412. struct iwl_rx_mem_buffer *rxb)
  2413. {
  2414. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2415. struct iwl_scanresults_notification *notif =
  2416. (struct iwl_scanresults_notification *)pkt->u.raw;
  2417. IWL_DEBUG_SCAN("Scan ch.res: "
  2418. "%d [802.11%s] "
  2419. "(TSF: 0x%08X:%08X) - %d "
  2420. "elapsed=%lu usec (%dms since last)\n",
  2421. notif->channel,
  2422. notif->band ? "bg" : "a",
  2423. le32_to_cpu(notif->tsf_high),
  2424. le32_to_cpu(notif->tsf_low),
  2425. le32_to_cpu(notif->statistics[0]),
  2426. le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
  2427. jiffies_to_msecs(elapsed_jiffies
  2428. (priv->last_scan_jiffies, jiffies)));
  2429. priv->last_scan_jiffies = jiffies;
  2430. priv->next_scan_jiffies = 0;
  2431. }
  2432. /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
  2433. static void iwl3945_rx_scan_complete_notif(struct iwl_priv *priv,
  2434. struct iwl_rx_mem_buffer *rxb)
  2435. {
  2436. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2437. struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  2438. IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  2439. scan_notif->scanned_channels,
  2440. scan_notif->tsf_low,
  2441. scan_notif->tsf_high, scan_notif->status);
  2442. /* The HW is no longer scanning */
  2443. clear_bit(STATUS_SCAN_HW, &priv->status);
  2444. /* The scan completion notification came in, so kill that timer... */
  2445. cancel_delayed_work(&priv->scan_check);
  2446. IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
  2447. (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) ?
  2448. "2.4" : "5.2",
  2449. jiffies_to_msecs(elapsed_jiffies
  2450. (priv->scan_pass_start, jiffies)));
  2451. /* Remove this scanned band from the list of pending
  2452. * bands to scan, band G precedes A in order of scanning
  2453. * as seen in iwl3945_bg_request_scan */
  2454. if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ))
  2455. priv->scan_bands &= ~BIT(IEEE80211_BAND_2GHZ);
  2456. else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ))
  2457. priv->scan_bands &= ~BIT(IEEE80211_BAND_5GHZ);
  2458. /* If a request to abort was given, or the scan did not succeed
  2459. * then we reset the scan state machine and terminate,
  2460. * re-queuing another scan if one has been requested */
  2461. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  2462. IWL_DEBUG_INFO("Aborted scan completed.\n");
  2463. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  2464. } else {
  2465. /* If there are more bands on this scan pass reschedule */
  2466. if (priv->scan_bands > 0)
  2467. goto reschedule;
  2468. }
  2469. priv->last_scan_jiffies = jiffies;
  2470. priv->next_scan_jiffies = 0;
  2471. IWL_DEBUG_INFO("Setting scan to off\n");
  2472. clear_bit(STATUS_SCANNING, &priv->status);
  2473. IWL_DEBUG_INFO("Scan took %dms\n",
  2474. jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
  2475. queue_work(priv->workqueue, &priv->scan_completed);
  2476. return;
  2477. reschedule:
  2478. priv->scan_pass_start = jiffies;
  2479. queue_work(priv->workqueue, &priv->request_scan);
  2480. }
  2481. /* Handle notification from uCode that card's power state is changing
  2482. * due to software, hardware, or critical temperature RFKILL */
  2483. static void iwl3945_rx_card_state_notif(struct iwl_priv *priv,
  2484. struct iwl_rx_mem_buffer *rxb)
  2485. {
  2486. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2487. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  2488. unsigned long status = priv->status;
  2489. IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
  2490. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  2491. (flags & SW_CARD_DISABLED) ? "Kill" : "On");
  2492. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  2493. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  2494. if (flags & HW_CARD_DISABLED)
  2495. set_bit(STATUS_RF_KILL_HW, &priv->status);
  2496. else
  2497. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  2498. if (flags & SW_CARD_DISABLED)
  2499. set_bit(STATUS_RF_KILL_SW, &priv->status);
  2500. else
  2501. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  2502. iwl3945_scan_cancel(priv);
  2503. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  2504. test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
  2505. (test_bit(STATUS_RF_KILL_SW, &status) !=
  2506. test_bit(STATUS_RF_KILL_SW, &priv->status)))
  2507. queue_work(priv->workqueue, &priv->rf_kill);
  2508. else
  2509. wake_up_interruptible(&priv->wait_command_queue);
  2510. }
  2511. /**
  2512. * iwl3945_setup_rx_handlers - Initialize Rx handler callbacks
  2513. *
  2514. * Setup the RX handlers for each of the reply types sent from the uCode
  2515. * to the host.
  2516. *
  2517. * This function chains into the hardware specific files for them to setup
  2518. * any hardware specific handlers as well.
  2519. */
  2520. static void iwl3945_setup_rx_handlers(struct iwl_priv *priv)
  2521. {
  2522. priv->rx_handlers[REPLY_ALIVE] = iwl3945_rx_reply_alive;
  2523. priv->rx_handlers[REPLY_ADD_STA] = iwl3945_rx_reply_add_sta;
  2524. priv->rx_handlers[REPLY_ERROR] = iwl3945_rx_reply_error;
  2525. priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl3945_rx_csa;
  2526. priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
  2527. iwl3945_rx_spectrum_measure_notif;
  2528. priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl3945_rx_pm_sleep_notif;
  2529. priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  2530. iwl3945_rx_pm_debug_statistics_notif;
  2531. priv->rx_handlers[BEACON_NOTIFICATION] = iwl3945_rx_beacon_notif;
  2532. /*
  2533. * The same handler is used for both the REPLY to a discrete
  2534. * statistics request from the host as well as for the periodic
  2535. * statistics notifications (after received beacons) from the uCode.
  2536. */
  2537. priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl3945_hw_rx_statistics;
  2538. priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl3945_hw_rx_statistics;
  2539. priv->rx_handlers[REPLY_SCAN_CMD] = iwl3945_rx_reply_scan;
  2540. priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl3945_rx_scan_start_notif;
  2541. priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  2542. iwl3945_rx_scan_results_notif;
  2543. priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  2544. iwl3945_rx_scan_complete_notif;
  2545. priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl3945_rx_card_state_notif;
  2546. /* Set up hardware specific Rx handlers */
  2547. iwl3945_hw_rx_handler_setup(priv);
  2548. }
  2549. /**
  2550. * iwl3945_cmd_queue_reclaim - Reclaim CMD queue entries
  2551. * When FW advances 'R' index, all entries between old and new 'R' index
  2552. * need to be reclaimed.
  2553. */
  2554. static void iwl3945_cmd_queue_reclaim(struct iwl_priv *priv,
  2555. int txq_id, int index)
  2556. {
  2557. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  2558. struct iwl_queue *q = &txq->q;
  2559. int nfreed = 0;
  2560. if ((index >= q->n_bd) || (iwl3945_x2_queue_used(q, index) == 0)) {
  2561. IWL_ERR(priv, "Read index for DMA queue txq id (%d), index %d, "
  2562. "is out of range [0-%d] %d %d.\n", txq_id,
  2563. index, q->n_bd, q->write_ptr, q->read_ptr);
  2564. return;
  2565. }
  2566. for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
  2567. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  2568. if (nfreed > 1) {
  2569. IWL_ERR(priv, "HCMD skipped: index (%d) %d %d\n", index,
  2570. q->write_ptr, q->read_ptr);
  2571. queue_work(priv->workqueue, &priv->restart);
  2572. break;
  2573. }
  2574. nfreed++;
  2575. }
  2576. }
  2577. /**
  2578. * iwl3945_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  2579. * @rxb: Rx buffer to reclaim
  2580. *
  2581. * If an Rx buffer has an async callback associated with it the callback
  2582. * will be executed. The attached skb (if present) will only be freed
  2583. * if the callback returns 1
  2584. */
  2585. static void iwl3945_tx_cmd_complete(struct iwl_priv *priv,
  2586. struct iwl_rx_mem_buffer *rxb)
  2587. {
  2588. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  2589. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  2590. int txq_id = SEQ_TO_QUEUE(sequence);
  2591. int index = SEQ_TO_INDEX(sequence);
  2592. int huge = !!(pkt->hdr.sequence & SEQ_HUGE_FRAME);
  2593. int cmd_index;
  2594. struct iwl_cmd *cmd;
  2595. BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
  2596. cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
  2597. cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
  2598. /* Input error checking is done when commands are added to queue. */
  2599. if (cmd->meta.flags & CMD_WANT_SKB) {
  2600. cmd->meta.source->u.skb = rxb->skb;
  2601. rxb->skb = NULL;
  2602. } else if (cmd->meta.u.callback &&
  2603. !cmd->meta.u.callback(priv, cmd, rxb->skb))
  2604. rxb->skb = NULL;
  2605. iwl3945_cmd_queue_reclaim(priv, txq_id, index);
  2606. if (!(cmd->meta.flags & CMD_ASYNC)) {
  2607. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  2608. wake_up_interruptible(&priv->wait_command_queue);
  2609. }
  2610. }
  2611. /************************** RX-FUNCTIONS ****************************/
  2612. /*
  2613. * Rx theory of operation
  2614. *
  2615. * The host allocates 32 DMA target addresses and passes the host address
  2616. * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
  2617. * 0 to 31
  2618. *
  2619. * Rx Queue Indexes
  2620. * The host/firmware share two index registers for managing the Rx buffers.
  2621. *
  2622. * The READ index maps to the first position that the firmware may be writing
  2623. * to -- the driver can read up to (but not including) this position and get
  2624. * good data.
  2625. * The READ index is managed by the firmware once the card is enabled.
  2626. *
  2627. * The WRITE index maps to the last position the driver has read from -- the
  2628. * position preceding WRITE is the last slot the firmware can place a packet.
  2629. *
  2630. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  2631. * WRITE = READ.
  2632. *
  2633. * During initialization, the host sets up the READ queue position to the first
  2634. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  2635. *
  2636. * When the firmware places a packet in a buffer, it will advance the READ index
  2637. * and fire the RX interrupt. The driver can then query the READ index and
  2638. * process as many packets as possible, moving the WRITE index forward as it
  2639. * resets the Rx queue buffers with new memory.
  2640. *
  2641. * The management in the driver is as follows:
  2642. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  2643. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  2644. * to replenish the iwl->rxq->rx_free.
  2645. * + In iwl3945_rx_replenish (scheduled) if 'processed' != 'read' then the
  2646. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  2647. * 'processed' and 'read' driver indexes as well)
  2648. * + A received packet is processed and handed to the kernel network stack,
  2649. * detached from the iwl->rxq. The driver 'processed' index is updated.
  2650. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  2651. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  2652. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  2653. * were enough free buffers and RX_STALLED is set it is cleared.
  2654. *
  2655. *
  2656. * Driver sequence:
  2657. *
  2658. * iwl3945_rx_replenish() Replenishes rx_free list from rx_used, and calls
  2659. * iwl3945_rx_queue_restock
  2660. * iwl3945_rx_queue_restock() Moves available buffers from rx_free into Rx
  2661. * queue, updates firmware pointers, and updates
  2662. * the WRITE index. If insufficient rx_free buffers
  2663. * are available, schedules iwl3945_rx_replenish
  2664. *
  2665. * -- enable interrupts --
  2666. * ISR - iwl3945_rx() Detach iwl_rx_mem_buffers from pool up to the
  2667. * READ INDEX, detaching the SKB from the pool.
  2668. * Moves the packet buffer from queue to rx_used.
  2669. * Calls iwl3945_rx_queue_restock to refill any empty
  2670. * slots.
  2671. * ...
  2672. *
  2673. */
  2674. /**
  2675. * iwl3945_rx_queue_space - Return number of free slots available in queue.
  2676. */
  2677. static int iwl3945_rx_queue_space(const struct iwl_rx_queue *q)
  2678. {
  2679. int s = q->read - q->write;
  2680. if (s <= 0)
  2681. s += RX_QUEUE_SIZE;
  2682. /* keep some buffer to not confuse full and empty queue */
  2683. s -= 2;
  2684. if (s < 0)
  2685. s = 0;
  2686. return s;
  2687. }
  2688. /**
  2689. * iwl3945_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  2690. */
  2691. static inline __le32 iwl3945_dma_addr2rbd_ptr(struct iwl_priv *priv,
  2692. dma_addr_t dma_addr)
  2693. {
  2694. return cpu_to_le32((u32)dma_addr);
  2695. }
  2696. /**
  2697. * iwl3945_rx_queue_restock - refill RX queue from pre-allocated pool
  2698. *
  2699. * If there are slots in the RX queue that need to be restocked,
  2700. * and we have free pre-allocated buffers, fill the ranks as much
  2701. * as we can, pulling from rx_free.
  2702. *
  2703. * This moves the 'write' index forward to catch up with 'processed', and
  2704. * also updates the memory address in the firmware to reference the new
  2705. * target buffer.
  2706. */
  2707. static int iwl3945_rx_queue_restock(struct iwl_priv *priv)
  2708. {
  2709. struct iwl_rx_queue *rxq = &priv->rxq;
  2710. struct list_head *element;
  2711. struct iwl_rx_mem_buffer *rxb;
  2712. unsigned long flags;
  2713. int write, rc;
  2714. spin_lock_irqsave(&rxq->lock, flags);
  2715. write = rxq->write & ~0x7;
  2716. while ((iwl3945_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  2717. /* Get next free Rx buffer, remove from free list */
  2718. element = rxq->rx_free.next;
  2719. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  2720. list_del(element);
  2721. /* Point to Rx buffer via next RBD in circular buffer */
  2722. rxq->bd[rxq->write] = iwl3945_dma_addr2rbd_ptr(priv, rxb->real_dma_addr);
  2723. rxq->queue[rxq->write] = rxb;
  2724. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  2725. rxq->free_count--;
  2726. }
  2727. spin_unlock_irqrestore(&rxq->lock, flags);
  2728. /* If the pre-allocated buffer pool is dropping low, schedule to
  2729. * refill it */
  2730. if (rxq->free_count <= RX_LOW_WATERMARK)
  2731. queue_work(priv->workqueue, &priv->rx_replenish);
  2732. /* If we've added more space for the firmware to place data, tell it.
  2733. * Increment device's write pointer in multiples of 8. */
  2734. if ((write != (rxq->write & ~0x7))
  2735. || (abs(rxq->write - rxq->read) > 7)) {
  2736. spin_lock_irqsave(&rxq->lock, flags);
  2737. rxq->need_update = 1;
  2738. spin_unlock_irqrestore(&rxq->lock, flags);
  2739. rc = iwl_rx_queue_update_write_ptr(priv, rxq);
  2740. if (rc)
  2741. return rc;
  2742. }
  2743. return 0;
  2744. }
  2745. /**
  2746. * iwl3945_rx_replenish - Move all used packet from rx_used to rx_free
  2747. *
  2748. * When moving to rx_free an SKB is allocated for the slot.
  2749. *
  2750. * Also restock the Rx queue via iwl3945_rx_queue_restock.
  2751. * This is called as a scheduled work item (except for during initialization)
  2752. */
  2753. static void iwl3945_rx_allocate(struct iwl_priv *priv)
  2754. {
  2755. struct iwl_rx_queue *rxq = &priv->rxq;
  2756. struct list_head *element;
  2757. struct iwl_rx_mem_buffer *rxb;
  2758. unsigned long flags;
  2759. spin_lock_irqsave(&rxq->lock, flags);
  2760. while (!list_empty(&rxq->rx_used)) {
  2761. element = rxq->rx_used.next;
  2762. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  2763. /* Alloc a new receive buffer */
  2764. rxb->skb =
  2765. alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
  2766. if (!rxb->skb) {
  2767. if (net_ratelimit())
  2768. IWL_CRIT(priv, ": Can not allocate SKB buffers\n");
  2769. /* We don't reschedule replenish work here -- we will
  2770. * call the restock method and if it still needs
  2771. * more buffers it will schedule replenish */
  2772. break;
  2773. }
  2774. /* If radiotap head is required, reserve some headroom here.
  2775. * The physical head count is a variable rx_stats->phy_count.
  2776. * We reserve 4 bytes here. Plus these extra bytes, the
  2777. * headroom of the physical head should be enough for the
  2778. * radiotap head that iwl3945 supported. See iwl3945_rt.
  2779. */
  2780. skb_reserve(rxb->skb, 4);
  2781. priv->alloc_rxb_skb++;
  2782. list_del(element);
  2783. /* Get physical address of RB/SKB */
  2784. rxb->real_dma_addr =
  2785. pci_map_single(priv->pci_dev, rxb->skb->data,
  2786. IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  2787. list_add_tail(&rxb->list, &rxq->rx_free);
  2788. rxq->free_count++;
  2789. }
  2790. spin_unlock_irqrestore(&rxq->lock, flags);
  2791. }
  2792. /*
  2793. * this should be called while priv->lock is locked
  2794. */
  2795. static void __iwl3945_rx_replenish(void *data)
  2796. {
  2797. struct iwl_priv *priv = data;
  2798. iwl3945_rx_allocate(priv);
  2799. iwl3945_rx_queue_restock(priv);
  2800. }
  2801. void iwl3945_rx_replenish(void *data)
  2802. {
  2803. struct iwl_priv *priv = data;
  2804. unsigned long flags;
  2805. iwl3945_rx_allocate(priv);
  2806. spin_lock_irqsave(&priv->lock, flags);
  2807. iwl3945_rx_queue_restock(priv);
  2808. spin_unlock_irqrestore(&priv->lock, flags);
  2809. }
  2810. /* Convert linear signal-to-noise ratio into dB */
  2811. static u8 ratio2dB[100] = {
  2812. /* 0 1 2 3 4 5 6 7 8 9 */
  2813. 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
  2814. 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
  2815. 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
  2816. 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
  2817. 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
  2818. 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
  2819. 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
  2820. 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
  2821. 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
  2822. 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
  2823. };
  2824. /* Calculates a relative dB value from a ratio of linear
  2825. * (i.e. not dB) signal levels.
  2826. * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
  2827. int iwl3945_calc_db_from_ratio(int sig_ratio)
  2828. {
  2829. /* 1000:1 or higher just report as 60 dB */
  2830. if (sig_ratio >= 1000)
  2831. return 60;
  2832. /* 100:1 or higher, divide by 10 and use table,
  2833. * add 20 dB to make up for divide by 10 */
  2834. if (sig_ratio >= 100)
  2835. return 20 + (int)ratio2dB[sig_ratio/10];
  2836. /* We shouldn't see this */
  2837. if (sig_ratio < 1)
  2838. return 0;
  2839. /* Use table for ratios 1:1 - 99:1 */
  2840. return (int)ratio2dB[sig_ratio];
  2841. }
  2842. #define PERFECT_RSSI (-20) /* dBm */
  2843. #define WORST_RSSI (-95) /* dBm */
  2844. #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
  2845. /* Calculate an indication of rx signal quality (a percentage, not dBm!).
  2846. * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
  2847. * about formulas used below. */
  2848. int iwl3945_calc_sig_qual(int rssi_dbm, int noise_dbm)
  2849. {
  2850. int sig_qual;
  2851. int degradation = PERFECT_RSSI - rssi_dbm;
  2852. /* If we get a noise measurement, use signal-to-noise ratio (SNR)
  2853. * as indicator; formula is (signal dbm - noise dbm).
  2854. * SNR at or above 40 is a great signal (100%).
  2855. * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
  2856. * Weakest usable signal is usually 10 - 15 dB SNR. */
  2857. if (noise_dbm) {
  2858. if (rssi_dbm - noise_dbm >= 40)
  2859. return 100;
  2860. else if (rssi_dbm < noise_dbm)
  2861. return 0;
  2862. sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
  2863. /* Else use just the signal level.
  2864. * This formula is a least squares fit of data points collected and
  2865. * compared with a reference system that had a percentage (%) display
  2866. * for signal quality. */
  2867. } else
  2868. sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
  2869. (15 * RSSI_RANGE + 62 * degradation)) /
  2870. (RSSI_RANGE * RSSI_RANGE);
  2871. if (sig_qual > 100)
  2872. sig_qual = 100;
  2873. else if (sig_qual < 1)
  2874. sig_qual = 0;
  2875. return sig_qual;
  2876. }
  2877. /**
  2878. * iwl3945_rx_handle - Main entry function for receiving responses from uCode
  2879. *
  2880. * Uses the priv->rx_handlers callback function array to invoke
  2881. * the appropriate handlers, including command responses,
  2882. * frame-received notifications, and other notifications.
  2883. */
  2884. static void iwl3945_rx_handle(struct iwl_priv *priv)
  2885. {
  2886. struct iwl_rx_mem_buffer *rxb;
  2887. struct iwl_rx_packet *pkt;
  2888. struct iwl_rx_queue *rxq = &priv->rxq;
  2889. u32 r, i;
  2890. int reclaim;
  2891. unsigned long flags;
  2892. u8 fill_rx = 0;
  2893. u32 count = 8;
  2894. /* uCode's read index (stored in shared DRAM) indicates the last Rx
  2895. * buffer that the driver may process (last buffer filled by ucode). */
  2896. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  2897. i = rxq->read;
  2898. if (iwl3945_rx_queue_space(rxq) > (RX_QUEUE_SIZE / 2))
  2899. fill_rx = 1;
  2900. /* Rx interrupt, but nothing sent from uCode */
  2901. if (i == r)
  2902. IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
  2903. while (i != r) {
  2904. rxb = rxq->queue[i];
  2905. /* If an RXB doesn't have a Rx queue slot associated with it,
  2906. * then a bug has been introduced in the queue refilling
  2907. * routines -- catch it here */
  2908. BUG_ON(rxb == NULL);
  2909. rxq->queue[i] = NULL;
  2910. pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->real_dma_addr,
  2911. IWL_RX_BUF_SIZE,
  2912. PCI_DMA_FROMDEVICE);
  2913. pkt = (struct iwl_rx_packet *)rxb->skb->data;
  2914. /* Reclaim a command buffer only if this packet is a response
  2915. * to a (driver-originated) command.
  2916. * If the packet (e.g. Rx frame) originated from uCode,
  2917. * there is no command buffer to reclaim.
  2918. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  2919. * but apparently a few don't get set; catch them here. */
  2920. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  2921. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  2922. (pkt->hdr.cmd != REPLY_TX);
  2923. /* Based on type of command response or notification,
  2924. * handle those that need handling via function in
  2925. * rx_handlers table. See iwl3945_setup_rx_handlers() */
  2926. if (priv->rx_handlers[pkt->hdr.cmd]) {
  2927. IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
  2928. "r = %d, i = %d, %s, 0x%02x\n", r, i,
  2929. get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  2930. priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
  2931. } else {
  2932. /* No handling needed */
  2933. IWL_DEBUG(IWL_DL_HCMD | IWL_DL_RX | IWL_DL_ISR,
  2934. "r %d i %d No handler needed for %s, 0x%02x\n",
  2935. r, i, get_cmd_string(pkt->hdr.cmd),
  2936. pkt->hdr.cmd);
  2937. }
  2938. if (reclaim) {
  2939. /* Invoke any callbacks, transfer the skb to caller, and
  2940. * fire off the (possibly) blocking iwl3945_send_cmd()
  2941. * as we reclaim the driver command queue */
  2942. if (rxb && rxb->skb)
  2943. iwl3945_tx_cmd_complete(priv, rxb);
  2944. else
  2945. IWL_WARN(priv, "Claim null rxb?\n");
  2946. }
  2947. /* For now we just don't re-use anything. We can tweak this
  2948. * later to try and re-use notification packets and SKBs that
  2949. * fail to Rx correctly */
  2950. if (rxb->skb != NULL) {
  2951. priv->alloc_rxb_skb--;
  2952. dev_kfree_skb_any(rxb->skb);
  2953. rxb->skb = NULL;
  2954. }
  2955. pci_unmap_single(priv->pci_dev, rxb->real_dma_addr,
  2956. IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  2957. spin_lock_irqsave(&rxq->lock, flags);
  2958. list_add_tail(&rxb->list, &priv->rxq.rx_used);
  2959. spin_unlock_irqrestore(&rxq->lock, flags);
  2960. i = (i + 1) & RX_QUEUE_MASK;
  2961. /* If there are a lot of unused frames,
  2962. * restock the Rx queue so ucode won't assert. */
  2963. if (fill_rx) {
  2964. count++;
  2965. if (count >= 8) {
  2966. priv->rxq.read = i;
  2967. __iwl3945_rx_replenish(priv);
  2968. count = 0;
  2969. }
  2970. }
  2971. }
  2972. /* Backtrack one entry */
  2973. priv->rxq.read = i;
  2974. iwl3945_rx_queue_restock(priv);
  2975. }
  2976. /**
  2977. * iwl3945_tx_queue_update_write_ptr - Send new write index to hardware
  2978. */
  2979. static int iwl3945_tx_queue_update_write_ptr(struct iwl_priv *priv,
  2980. struct iwl_tx_queue *txq)
  2981. {
  2982. u32 reg = 0;
  2983. int rc = 0;
  2984. int txq_id = txq->q.id;
  2985. if (txq->need_update == 0)
  2986. return rc;
  2987. /* if we're trying to save power */
  2988. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  2989. /* wake up nic if it's powered down ...
  2990. * uCode will wake up, and interrupt us again, so next
  2991. * time we'll skip this part. */
  2992. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  2993. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  2994. IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
  2995. iwl_set_bit(priv, CSR_GP_CNTRL,
  2996. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  2997. return rc;
  2998. }
  2999. /* restore this queue's parameters in nic hardware. */
  3000. rc = iwl_grab_nic_access(priv);
  3001. if (rc)
  3002. return rc;
  3003. iwl_write_direct32(priv, HBUS_TARG_WRPTR,
  3004. txq->q.write_ptr | (txq_id << 8));
  3005. iwl_release_nic_access(priv);
  3006. /* else not in power-save mode, uCode will never sleep when we're
  3007. * trying to tx (during RFKILL, we're not trying to tx). */
  3008. } else
  3009. iwl_write32(priv, HBUS_TARG_WRPTR,
  3010. txq->q.write_ptr | (txq_id << 8));
  3011. txq->need_update = 0;
  3012. return rc;
  3013. }
  3014. #ifdef CONFIG_IWL3945_DEBUG
  3015. static void iwl3945_print_rx_config_cmd(struct iwl_priv *priv,
  3016. struct iwl3945_rxon_cmd *rxon)
  3017. {
  3018. IWL_DEBUG_RADIO("RX CONFIG:\n");
  3019. iwl_print_hex_dump(priv, IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  3020. IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  3021. IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  3022. IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
  3023. le32_to_cpu(rxon->filter_flags));
  3024. IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
  3025. IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
  3026. rxon->ofdm_basic_rates);
  3027. IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  3028. IWL_DEBUG_RADIO("u8[6] node_addr: %pM\n", rxon->node_addr);
  3029. IWL_DEBUG_RADIO("u8[6] bssid_addr: %pM\n", rxon->bssid_addr);
  3030. IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  3031. }
  3032. #endif
  3033. static void iwl3945_enable_interrupts(struct iwl_priv *priv)
  3034. {
  3035. IWL_DEBUG_ISR("Enabling interrupts\n");
  3036. set_bit(STATUS_INT_ENABLED, &priv->status);
  3037. iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
  3038. }
  3039. /* call this function to flush any scheduled tasklet */
  3040. static inline void iwl_synchronize_irq(struct iwl_priv *priv)
  3041. {
  3042. /* wait to make sure we flush pending tasklet*/
  3043. synchronize_irq(priv->pci_dev->irq);
  3044. tasklet_kill(&priv->irq_tasklet);
  3045. }
  3046. static inline void iwl3945_disable_interrupts(struct iwl_priv *priv)
  3047. {
  3048. clear_bit(STATUS_INT_ENABLED, &priv->status);
  3049. /* disable interrupts from uCode/NIC to host */
  3050. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  3051. /* acknowledge/clear/reset any interrupts still pending
  3052. * from uCode or flow handler (Rx/Tx DMA) */
  3053. iwl_write32(priv, CSR_INT, 0xffffffff);
  3054. iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
  3055. IWL_DEBUG_ISR("Disabled interrupts\n");
  3056. }
  3057. static const char *desc_lookup(int i)
  3058. {
  3059. switch (i) {
  3060. case 1:
  3061. return "FAIL";
  3062. case 2:
  3063. return "BAD_PARAM";
  3064. case 3:
  3065. return "BAD_CHECKSUM";
  3066. case 4:
  3067. return "NMI_INTERRUPT";
  3068. case 5:
  3069. return "SYSASSERT";
  3070. case 6:
  3071. return "FATAL_ERROR";
  3072. }
  3073. return "UNKNOWN";
  3074. }
  3075. #define ERROR_START_OFFSET (1 * sizeof(u32))
  3076. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  3077. static void iwl3945_dump_nic_error_log(struct iwl_priv *priv)
  3078. {
  3079. u32 i;
  3080. u32 desc, time, count, base, data1;
  3081. u32 blink1, blink2, ilink1, ilink2;
  3082. int rc;
  3083. base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
  3084. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  3085. IWL_ERR(priv, "Not valid error log pointer 0x%08X\n", base);
  3086. return;
  3087. }
  3088. rc = iwl_grab_nic_access(priv);
  3089. if (rc) {
  3090. IWL_WARN(priv, "Can not read from adapter at this time.\n");
  3091. return;
  3092. }
  3093. count = iwl_read_targ_mem(priv, base);
  3094. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  3095. IWL_ERR(priv, "Start IWL Error Log Dump:\n");
  3096. IWL_ERR(priv, "Status: 0x%08lX, count: %d\n",
  3097. priv->status, count);
  3098. }
  3099. IWL_ERR(priv, "Desc Time asrtPC blink2 "
  3100. "ilink1 nmiPC Line\n");
  3101. for (i = ERROR_START_OFFSET;
  3102. i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
  3103. i += ERROR_ELEM_SIZE) {
  3104. desc = iwl_read_targ_mem(priv, base + i);
  3105. time =
  3106. iwl_read_targ_mem(priv, base + i + 1 * sizeof(u32));
  3107. blink1 =
  3108. iwl_read_targ_mem(priv, base + i + 2 * sizeof(u32));
  3109. blink2 =
  3110. iwl_read_targ_mem(priv, base + i + 3 * sizeof(u32));
  3111. ilink1 =
  3112. iwl_read_targ_mem(priv, base + i + 4 * sizeof(u32));
  3113. ilink2 =
  3114. iwl_read_targ_mem(priv, base + i + 5 * sizeof(u32));
  3115. data1 =
  3116. iwl_read_targ_mem(priv, base + i + 6 * sizeof(u32));
  3117. IWL_ERR(priv,
  3118. "%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
  3119. desc_lookup(desc), desc, time, blink1, blink2,
  3120. ilink1, ilink2, data1);
  3121. }
  3122. iwl_release_nic_access(priv);
  3123. }
  3124. #define EVENT_START_OFFSET (6 * sizeof(u32))
  3125. /**
  3126. * iwl3945_print_event_log - Dump error event log to syslog
  3127. *
  3128. * NOTE: Must be called with iwl_grab_nic_access() already obtained!
  3129. */
  3130. static void iwl3945_print_event_log(struct iwl_priv *priv, u32 start_idx,
  3131. u32 num_events, u32 mode)
  3132. {
  3133. u32 i;
  3134. u32 base; /* SRAM byte address of event log header */
  3135. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  3136. u32 ptr; /* SRAM byte address of log data */
  3137. u32 ev, time, data; /* event log data */
  3138. if (num_events == 0)
  3139. return;
  3140. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  3141. if (mode == 0)
  3142. event_size = 2 * sizeof(u32);
  3143. else
  3144. event_size = 3 * sizeof(u32);
  3145. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  3146. /* "time" is actually "data" for mode 0 (no timestamp).
  3147. * place event id # at far right for easier visual parsing. */
  3148. for (i = 0; i < num_events; i++) {
  3149. ev = iwl_read_targ_mem(priv, ptr);
  3150. ptr += sizeof(u32);
  3151. time = iwl_read_targ_mem(priv, ptr);
  3152. ptr += sizeof(u32);
  3153. if (mode == 0) {
  3154. /* data, ev */
  3155. IWL_ERR(priv, "0x%08x\t%04u\n", time, ev);
  3156. } else {
  3157. data = iwl_read_targ_mem(priv, ptr);
  3158. ptr += sizeof(u32);
  3159. IWL_ERR(priv, "%010u\t0x%08x\t%04u\n", time, data, ev);
  3160. }
  3161. }
  3162. }
  3163. static void iwl3945_dump_nic_event_log(struct iwl_priv *priv)
  3164. {
  3165. int rc;
  3166. u32 base; /* SRAM byte address of event log header */
  3167. u32 capacity; /* event log capacity in # entries */
  3168. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  3169. u32 num_wraps; /* # times uCode wrapped to top of log */
  3170. u32 next_entry; /* index of next entry to be written by uCode */
  3171. u32 size; /* # entries that we'll print */
  3172. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  3173. if (!iwl3945_hw_valid_rtc_data_addr(base)) {
  3174. IWL_ERR(priv, "Invalid event log pointer 0x%08X\n", base);
  3175. return;
  3176. }
  3177. rc = iwl_grab_nic_access(priv);
  3178. if (rc) {
  3179. IWL_WARN(priv, "Can not read from adapter at this time.\n");
  3180. return;
  3181. }
  3182. /* event log header */
  3183. capacity = iwl_read_targ_mem(priv, base);
  3184. mode = iwl_read_targ_mem(priv, base + (1 * sizeof(u32)));
  3185. num_wraps = iwl_read_targ_mem(priv, base + (2 * sizeof(u32)));
  3186. next_entry = iwl_read_targ_mem(priv, base + (3 * sizeof(u32)));
  3187. size = num_wraps ? capacity : next_entry;
  3188. /* bail out if nothing in log */
  3189. if (size == 0) {
  3190. IWL_ERR(priv, "Start IWL Event Log Dump: nothing in log\n");
  3191. iwl_release_nic_access(priv);
  3192. return;
  3193. }
  3194. IWL_ERR(priv, "Start IWL Event Log Dump: display count %d, wraps %d\n",
  3195. size, num_wraps);
  3196. /* if uCode has wrapped back to top of log, start at the oldest entry,
  3197. * i.e the next one that uCode would fill. */
  3198. if (num_wraps)
  3199. iwl3945_print_event_log(priv, next_entry,
  3200. capacity - next_entry, mode);
  3201. /* (then/else) start at top of log */
  3202. iwl3945_print_event_log(priv, 0, next_entry, mode);
  3203. iwl_release_nic_access(priv);
  3204. }
  3205. /**
  3206. * iwl3945_irq_handle_error - called for HW or SW error interrupt from card
  3207. */
  3208. static void iwl3945_irq_handle_error(struct iwl_priv *priv)
  3209. {
  3210. /* Set the FW error flag -- cleared on iwl3945_down */
  3211. set_bit(STATUS_FW_ERROR, &priv->status);
  3212. /* Cancel currently queued command. */
  3213. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  3214. #ifdef CONFIG_IWL3945_DEBUG
  3215. if (priv->debug_level & IWL_DL_FW_ERRORS) {
  3216. iwl3945_dump_nic_error_log(priv);
  3217. iwl3945_dump_nic_event_log(priv);
  3218. iwl3945_print_rx_config_cmd(priv, &priv->staging39_rxon);
  3219. }
  3220. #endif
  3221. wake_up_interruptible(&priv->wait_command_queue);
  3222. /* Keep the restart process from trying to send host
  3223. * commands by clearing the INIT status bit */
  3224. clear_bit(STATUS_READY, &priv->status);
  3225. if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  3226. IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
  3227. "Restarting adapter due to uCode error.\n");
  3228. if (iwl3945_is_associated(priv)) {
  3229. memcpy(&priv->recovery39_rxon, &priv->active39_rxon,
  3230. sizeof(priv->recovery39_rxon));
  3231. priv->error_recovering = 1;
  3232. }
  3233. queue_work(priv->workqueue, &priv->restart);
  3234. }
  3235. }
  3236. static void iwl3945_error_recovery(struct iwl_priv *priv)
  3237. {
  3238. unsigned long flags;
  3239. memcpy(&priv->staging39_rxon, &priv->recovery39_rxon,
  3240. sizeof(priv->staging39_rxon));
  3241. priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3242. iwl3945_commit_rxon(priv);
  3243. iwl3945_add_station(priv, priv->bssid, 1, 0);
  3244. spin_lock_irqsave(&priv->lock, flags);
  3245. priv->assoc_id = le16_to_cpu(priv->staging39_rxon.assoc_id);
  3246. priv->error_recovering = 0;
  3247. spin_unlock_irqrestore(&priv->lock, flags);
  3248. }
  3249. static void iwl3945_irq_tasklet(struct iwl_priv *priv)
  3250. {
  3251. u32 inta, handled = 0;
  3252. u32 inta_fh;
  3253. unsigned long flags;
  3254. #ifdef CONFIG_IWL3945_DEBUG
  3255. u32 inta_mask;
  3256. #endif
  3257. spin_lock_irqsave(&priv->lock, flags);
  3258. /* Ack/clear/reset pending uCode interrupts.
  3259. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  3260. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  3261. inta = iwl_read32(priv, CSR_INT);
  3262. iwl_write32(priv, CSR_INT, inta);
  3263. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  3264. * Any new interrupts that happen after this, either while we're
  3265. * in this tasklet, or later, will show up in next ISR/tasklet. */
  3266. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  3267. iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
  3268. #ifdef CONFIG_IWL3945_DEBUG
  3269. if (priv->debug_level & IWL_DL_ISR) {
  3270. /* just for debug */
  3271. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  3272. IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  3273. inta, inta_mask, inta_fh);
  3274. }
  3275. #endif
  3276. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  3277. * atomic, make sure that inta covers all the interrupts that
  3278. * we've discovered, even if FH interrupt came in just after
  3279. * reading CSR_INT. */
  3280. if (inta_fh & CSR39_FH_INT_RX_MASK)
  3281. inta |= CSR_INT_BIT_FH_RX;
  3282. if (inta_fh & CSR39_FH_INT_TX_MASK)
  3283. inta |= CSR_INT_BIT_FH_TX;
  3284. /* Now service all interrupt bits discovered above. */
  3285. if (inta & CSR_INT_BIT_HW_ERR) {
  3286. IWL_ERR(priv, "Microcode HW error detected. Restarting.\n");
  3287. /* Tell the device to stop sending interrupts */
  3288. iwl3945_disable_interrupts(priv);
  3289. iwl3945_irq_handle_error(priv);
  3290. handled |= CSR_INT_BIT_HW_ERR;
  3291. spin_unlock_irqrestore(&priv->lock, flags);
  3292. return;
  3293. }
  3294. #ifdef CONFIG_IWL3945_DEBUG
  3295. if (priv->debug_level & (IWL_DL_ISR)) {
  3296. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  3297. if (inta & CSR_INT_BIT_SCD)
  3298. IWL_DEBUG_ISR("Scheduler finished to transmit "
  3299. "the frame/frames.\n");
  3300. /* Alive notification via Rx interrupt will do the real work */
  3301. if (inta & CSR_INT_BIT_ALIVE)
  3302. IWL_DEBUG_ISR("Alive interrupt\n");
  3303. }
  3304. #endif
  3305. /* Safely ignore these bits for debug checks below */
  3306. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  3307. /* Error detected by uCode */
  3308. if (inta & CSR_INT_BIT_SW_ERR) {
  3309. IWL_ERR(priv, "Microcode SW error detected. "
  3310. "Restarting 0x%X.\n", inta);
  3311. iwl3945_irq_handle_error(priv);
  3312. handled |= CSR_INT_BIT_SW_ERR;
  3313. }
  3314. /* uCode wakes up after power-down sleep */
  3315. if (inta & CSR_INT_BIT_WAKEUP) {
  3316. IWL_DEBUG_ISR("Wakeup interrupt\n");
  3317. iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
  3318. iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[0]);
  3319. iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[1]);
  3320. iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[2]);
  3321. iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[3]);
  3322. iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[4]);
  3323. iwl3945_tx_queue_update_write_ptr(priv, &priv->txq[5]);
  3324. handled |= CSR_INT_BIT_WAKEUP;
  3325. }
  3326. /* All uCode command responses, including Tx command responses,
  3327. * Rx "responses" (frame-received notification), and other
  3328. * notifications from uCode come through here*/
  3329. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  3330. iwl3945_rx_handle(priv);
  3331. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  3332. }
  3333. if (inta & CSR_INT_BIT_FH_TX) {
  3334. IWL_DEBUG_ISR("Tx interrupt\n");
  3335. iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
  3336. if (!iwl_grab_nic_access(priv)) {
  3337. iwl_write_direct32(priv, FH39_TCSR_CREDIT
  3338. (FH39_SRVC_CHNL), 0x0);
  3339. iwl_release_nic_access(priv);
  3340. }
  3341. handled |= CSR_INT_BIT_FH_TX;
  3342. }
  3343. if (inta & ~handled)
  3344. IWL_ERR(priv, "Unhandled INTA bits 0x%08x\n", inta & ~handled);
  3345. if (inta & ~CSR_INI_SET_MASK) {
  3346. IWL_WARN(priv, "Disabled INTA bits 0x%08x were pending\n",
  3347. inta & ~CSR_INI_SET_MASK);
  3348. IWL_WARN(priv, " with FH_INT = 0x%08x\n", inta_fh);
  3349. }
  3350. /* Re-enable all interrupts */
  3351. /* only Re-enable if disabled by irq */
  3352. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  3353. iwl3945_enable_interrupts(priv);
  3354. #ifdef CONFIG_IWL3945_DEBUG
  3355. if (priv->debug_level & (IWL_DL_ISR)) {
  3356. inta = iwl_read32(priv, CSR_INT);
  3357. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  3358. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  3359. IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  3360. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  3361. }
  3362. #endif
  3363. spin_unlock_irqrestore(&priv->lock, flags);
  3364. }
  3365. static irqreturn_t iwl3945_isr(int irq, void *data)
  3366. {
  3367. struct iwl_priv *priv = data;
  3368. u32 inta, inta_mask;
  3369. u32 inta_fh;
  3370. if (!priv)
  3371. return IRQ_NONE;
  3372. spin_lock(&priv->lock);
  3373. /* Disable (but don't clear!) interrupts here to avoid
  3374. * back-to-back ISRs and sporadic interrupts from our NIC.
  3375. * If we have something to service, the tasklet will re-enable ints.
  3376. * If we *don't* have something, we'll re-enable before leaving here. */
  3377. inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
  3378. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  3379. /* Discover which interrupts are active/pending */
  3380. inta = iwl_read32(priv, CSR_INT);
  3381. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  3382. /* Ignore interrupt if there's nothing in NIC to service.
  3383. * This may be due to IRQ shared with another device,
  3384. * or due to sporadic interrupts thrown from our NIC. */
  3385. if (!inta && !inta_fh) {
  3386. IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
  3387. goto none;
  3388. }
  3389. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  3390. /* Hardware disappeared */
  3391. IWL_WARN(priv, "HARDWARE GONE?? INTA == 0x%08x\n", inta);
  3392. goto unplugged;
  3393. }
  3394. IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  3395. inta, inta_mask, inta_fh);
  3396. inta &= ~CSR_INT_BIT_SCD;
  3397. /* iwl3945_irq_tasklet() will service interrupts and re-enable them */
  3398. if (likely(inta || inta_fh))
  3399. tasklet_schedule(&priv->irq_tasklet);
  3400. unplugged:
  3401. spin_unlock(&priv->lock);
  3402. return IRQ_HANDLED;
  3403. none:
  3404. /* re-enable interrupts here since we don't have anything to service. */
  3405. /* only Re-enable if disabled by irq */
  3406. if (test_bit(STATUS_INT_ENABLED, &priv->status))
  3407. iwl3945_enable_interrupts(priv);
  3408. spin_unlock(&priv->lock);
  3409. return IRQ_NONE;
  3410. }
  3411. /************************** EEPROM BANDS ****************************
  3412. *
  3413. * The iwl3945_eeprom_band definitions below provide the mapping from the
  3414. * EEPROM contents to the specific channel number supported for each
  3415. * band.
  3416. *
  3417. * For example, iwl3945_priv->eeprom39.band_3_channels[4] from the band_3
  3418. * definition below maps to physical channel 42 in the 5.2GHz spectrum.
  3419. * The specific geography and calibration information for that channel
  3420. * is contained in the eeprom map itself.
  3421. *
  3422. * During init, we copy the eeprom information and channel map
  3423. * information into priv->channel_info_24/52 and priv->channel_map_24/52
  3424. *
  3425. * channel_map_24/52 provides the index in the channel_info array for a
  3426. * given channel. We have to have two separate maps as there is channel
  3427. * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
  3428. * band_2
  3429. *
  3430. * A value of 0xff stored in the channel_map indicates that the channel
  3431. * is not supported by the hardware at all.
  3432. *
  3433. * A value of 0xfe in the channel_map indicates that the channel is not
  3434. * valid for Tx with the current hardware. This means that
  3435. * while the system can tune and receive on a given channel, it may not
  3436. * be able to associate or transmit any frames on that
  3437. * channel. There is no corresponding channel information for that
  3438. * entry.
  3439. *
  3440. *********************************************************************/
  3441. /* 2.4 GHz */
  3442. static const u8 iwl3945_eeprom_band_1[14] = {
  3443. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  3444. };
  3445. /* 5.2 GHz bands */
  3446. static const u8 iwl3945_eeprom_band_2[] = { /* 4915-5080MHz */
  3447. 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
  3448. };
  3449. static const u8 iwl3945_eeprom_band_3[] = { /* 5170-5320MHz */
  3450. 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
  3451. };
  3452. static const u8 iwl3945_eeprom_band_4[] = { /* 5500-5700MHz */
  3453. 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
  3454. };
  3455. static const u8 iwl3945_eeprom_band_5[] = { /* 5725-5825MHz */
  3456. 145, 149, 153, 157, 161, 165
  3457. };
  3458. static void iwl3945_init_band_reference(const struct iwl_priv *priv, int band,
  3459. int *eeprom_ch_count,
  3460. const struct iwl_eeprom_channel
  3461. **eeprom_ch_info,
  3462. const u8 **eeprom_ch_index)
  3463. {
  3464. switch (band) {
  3465. case 1: /* 2.4GHz band */
  3466. *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_1);
  3467. *eeprom_ch_info = priv->eeprom39.band_1_channels;
  3468. *eeprom_ch_index = iwl3945_eeprom_band_1;
  3469. break;
  3470. case 2: /* 4.9GHz band */
  3471. *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_2);
  3472. *eeprom_ch_info = priv->eeprom39.band_2_channels;
  3473. *eeprom_ch_index = iwl3945_eeprom_band_2;
  3474. break;
  3475. case 3: /* 5.2GHz band */
  3476. *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_3);
  3477. *eeprom_ch_info = priv->eeprom39.band_3_channels;
  3478. *eeprom_ch_index = iwl3945_eeprom_band_3;
  3479. break;
  3480. case 4: /* 5.5GHz band */
  3481. *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_4);
  3482. *eeprom_ch_info = priv->eeprom39.band_4_channels;
  3483. *eeprom_ch_index = iwl3945_eeprom_band_4;
  3484. break;
  3485. case 5: /* 5.7GHz band */
  3486. *eeprom_ch_count = ARRAY_SIZE(iwl3945_eeprom_band_5);
  3487. *eeprom_ch_info = priv->eeprom39.band_5_channels;
  3488. *eeprom_ch_index = iwl3945_eeprom_band_5;
  3489. break;
  3490. default:
  3491. BUG();
  3492. return;
  3493. }
  3494. }
  3495. /**
  3496. * iwl3945_get_channel_info - Find driver's private channel info
  3497. *
  3498. * Based on band and channel number.
  3499. */
  3500. const struct iwl_channel_info *
  3501. iwl3945_get_channel_info(const struct iwl_priv *priv,
  3502. enum ieee80211_band band, u16 channel)
  3503. {
  3504. int i;
  3505. switch (band) {
  3506. case IEEE80211_BAND_5GHZ:
  3507. for (i = 14; i < priv->channel_count; i++) {
  3508. if (priv->channel_info[i].channel == channel)
  3509. return &priv->channel_info[i];
  3510. }
  3511. break;
  3512. case IEEE80211_BAND_2GHZ:
  3513. if (channel >= 1 && channel <= 14)
  3514. return &priv->channel_info[channel - 1];
  3515. break;
  3516. case IEEE80211_NUM_BANDS:
  3517. WARN_ON(1);
  3518. }
  3519. return NULL;
  3520. }
  3521. #define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
  3522. ? # x " " : "")
  3523. /**
  3524. * iwl3945_init_channel_map - Set up driver's info for all possible channels
  3525. */
  3526. static int iwl3945_init_channel_map(struct iwl_priv *priv)
  3527. {
  3528. int eeprom_ch_count = 0;
  3529. const u8 *eeprom_ch_index = NULL;
  3530. const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
  3531. int band, ch;
  3532. struct iwl_channel_info *ch_info;
  3533. if (priv->channel_count) {
  3534. IWL_DEBUG_INFO("Channel map already initialized.\n");
  3535. return 0;
  3536. }
  3537. if (priv->eeprom39.version < 0x2f) {
  3538. IWL_WARN(priv, "Unsupported EEPROM version: 0x%04X\n",
  3539. priv->eeprom39.version);
  3540. return -EINVAL;
  3541. }
  3542. IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
  3543. priv->channel_count =
  3544. ARRAY_SIZE(iwl3945_eeprom_band_1) +
  3545. ARRAY_SIZE(iwl3945_eeprom_band_2) +
  3546. ARRAY_SIZE(iwl3945_eeprom_band_3) +
  3547. ARRAY_SIZE(iwl3945_eeprom_band_4) +
  3548. ARRAY_SIZE(iwl3945_eeprom_band_5);
  3549. IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
  3550. priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
  3551. priv->channel_count, GFP_KERNEL);
  3552. if (!priv->channel_info) {
  3553. IWL_ERR(priv, "Could not allocate channel_info\n");
  3554. priv->channel_count = 0;
  3555. return -ENOMEM;
  3556. }
  3557. ch_info = priv->channel_info;
  3558. /* Loop through the 5 EEPROM bands adding them in order to the
  3559. * channel map we maintain (that contains additional information than
  3560. * what just in the EEPROM) */
  3561. for (band = 1; band <= 5; band++) {
  3562. iwl3945_init_band_reference(priv, band, &eeprom_ch_count,
  3563. &eeprom_ch_info, &eeprom_ch_index);
  3564. /* Loop through each band adding each of the channels */
  3565. for (ch = 0; ch < eeprom_ch_count; ch++) {
  3566. ch_info->channel = eeprom_ch_index[ch];
  3567. ch_info->band = (band == 1) ? IEEE80211_BAND_2GHZ :
  3568. IEEE80211_BAND_5GHZ;
  3569. /* permanently store EEPROM's channel regulatory flags
  3570. * and max power in channel info database. */
  3571. ch_info->eeprom = eeprom_ch_info[ch];
  3572. /* Copy the run-time flags so they are there even on
  3573. * invalid channels */
  3574. ch_info->flags = eeprom_ch_info[ch].flags;
  3575. if (!(is_channel_valid(ch_info))) {
  3576. IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
  3577. "No traffic\n",
  3578. ch_info->channel,
  3579. ch_info->flags,
  3580. is_channel_a_band(ch_info) ?
  3581. "5.2" : "2.4");
  3582. ch_info++;
  3583. continue;
  3584. }
  3585. /* Initialize regulatory-based run-time data */
  3586. ch_info->max_power_avg = ch_info->curr_txpow =
  3587. eeprom_ch_info[ch].max_power_avg;
  3588. ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
  3589. ch_info->min_power = 0;
  3590. IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
  3591. " %ddBm): Ad-Hoc %ssupported\n",
  3592. ch_info->channel,
  3593. is_channel_a_band(ch_info) ?
  3594. "5.2" : "2.4",
  3595. CHECK_AND_PRINT(VALID),
  3596. CHECK_AND_PRINT(IBSS),
  3597. CHECK_AND_PRINT(ACTIVE),
  3598. CHECK_AND_PRINT(RADAR),
  3599. CHECK_AND_PRINT(WIDE),
  3600. CHECK_AND_PRINT(DFS),
  3601. eeprom_ch_info[ch].flags,
  3602. eeprom_ch_info[ch].max_power_avg,
  3603. ((eeprom_ch_info[ch].
  3604. flags & EEPROM_CHANNEL_IBSS)
  3605. && !(eeprom_ch_info[ch].
  3606. flags & EEPROM_CHANNEL_RADAR))
  3607. ? "" : "not ");
  3608. /* Set the user_txpower_limit to the highest power
  3609. * supported by any channel */
  3610. if (eeprom_ch_info[ch].max_power_avg >
  3611. priv->user_txpower_limit)
  3612. priv->user_txpower_limit =
  3613. eeprom_ch_info[ch].max_power_avg;
  3614. ch_info++;
  3615. }
  3616. }
  3617. /* Set up txpower settings in driver for all channels */
  3618. if (iwl3945_txpower_set_from_eeprom(priv))
  3619. return -EIO;
  3620. return 0;
  3621. }
  3622. /*
  3623. * iwl3945_free_channel_map - undo allocations in iwl3945_init_channel_map
  3624. */
  3625. static void iwl3945_free_channel_map(struct iwl_priv *priv)
  3626. {
  3627. kfree(priv->channel_info);
  3628. priv->channel_count = 0;
  3629. }
  3630. /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
  3631. * sending probe req. This should be set long enough to hear probe responses
  3632. * from more than one AP. */
  3633. #define IWL_ACTIVE_DWELL_TIME_24 (30) /* all times in msec */
  3634. #define IWL_ACTIVE_DWELL_TIME_52 (20)
  3635. #define IWL_ACTIVE_DWELL_FACTOR_24GHZ (3)
  3636. #define IWL_ACTIVE_DWELL_FACTOR_52GHZ (2)
  3637. /* For faster active scanning, scan will move to the next channel if fewer than
  3638. * PLCP_QUIET_THRESH packets are heard on this channel within
  3639. * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
  3640. * time if it's a quiet channel (nothing responded to our probe, and there's
  3641. * no other traffic).
  3642. * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
  3643. #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
  3644. #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(10) /* msec */
  3645. /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
  3646. * Must be set longer than active dwell time.
  3647. * For the most reliable scan, set > AP beacon interval (typically 100msec). */
  3648. #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  3649. #define IWL_PASSIVE_DWELL_TIME_52 (10)
  3650. #define IWL_PASSIVE_DWELL_BASE (100)
  3651. #define IWL_CHANNEL_TUNE_TIME 5
  3652. #define IWL_SCAN_PROBE_MASK(n) (BIT(n) | (BIT(n) - BIT(1)))
  3653. static inline u16 iwl3945_get_active_dwell_time(struct iwl_priv *priv,
  3654. enum ieee80211_band band,
  3655. u8 n_probes)
  3656. {
  3657. if (band == IEEE80211_BAND_5GHZ)
  3658. return IWL_ACTIVE_DWELL_TIME_52 +
  3659. IWL_ACTIVE_DWELL_FACTOR_52GHZ * (n_probes + 1);
  3660. else
  3661. return IWL_ACTIVE_DWELL_TIME_24 +
  3662. IWL_ACTIVE_DWELL_FACTOR_24GHZ * (n_probes + 1);
  3663. }
  3664. static u16 iwl3945_get_passive_dwell_time(struct iwl_priv *priv,
  3665. enum ieee80211_band band)
  3666. {
  3667. u16 passive = (band == IEEE80211_BAND_2GHZ) ?
  3668. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
  3669. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
  3670. if (iwl3945_is_associated(priv)) {
  3671. /* If we're associated, we clamp the maximum passive
  3672. * dwell time to be 98% of the beacon interval (minus
  3673. * 2 * channel tune time) */
  3674. passive = priv->beacon_int;
  3675. if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
  3676. passive = IWL_PASSIVE_DWELL_BASE;
  3677. passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
  3678. }
  3679. return passive;
  3680. }
  3681. static int iwl3945_get_channels_for_scan(struct iwl_priv *priv,
  3682. enum ieee80211_band band,
  3683. u8 is_active, u8 n_probes,
  3684. struct iwl3945_scan_channel *scan_ch)
  3685. {
  3686. const struct ieee80211_channel *channels = NULL;
  3687. const struct ieee80211_supported_band *sband;
  3688. const struct iwl_channel_info *ch_info;
  3689. u16 passive_dwell = 0;
  3690. u16 active_dwell = 0;
  3691. int added, i;
  3692. sband = iwl_get_hw_mode(priv, band);
  3693. if (!sband)
  3694. return 0;
  3695. channels = sband->channels;
  3696. active_dwell = iwl3945_get_active_dwell_time(priv, band, n_probes);
  3697. passive_dwell = iwl3945_get_passive_dwell_time(priv, band);
  3698. if (passive_dwell <= active_dwell)
  3699. passive_dwell = active_dwell + 1;
  3700. for (i = 0, added = 0; i < sband->n_channels; i++) {
  3701. if (channels[i].flags & IEEE80211_CHAN_DISABLED)
  3702. continue;
  3703. scan_ch->channel = channels[i].hw_value;
  3704. ch_info = iwl3945_get_channel_info(priv, band, scan_ch->channel);
  3705. if (!is_channel_valid(ch_info)) {
  3706. IWL_DEBUG_SCAN("Channel %d is INVALID for this band.\n",
  3707. scan_ch->channel);
  3708. continue;
  3709. }
  3710. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  3711. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  3712. /* If passive , set up for auto-switch
  3713. * and use long active_dwell time.
  3714. */
  3715. if (!is_active || is_channel_passive(ch_info) ||
  3716. (channels[i].flags & IEEE80211_CHAN_PASSIVE_SCAN)) {
  3717. scan_ch->type = 0; /* passive */
  3718. if (IWL_UCODE_API(priv->ucode_ver) == 1)
  3719. scan_ch->active_dwell = cpu_to_le16(passive_dwell - 1);
  3720. } else {
  3721. scan_ch->type = 1; /* active */
  3722. }
  3723. /* Set direct probe bits. These may be used both for active
  3724. * scan channels (probes gets sent right away),
  3725. * or for passive channels (probes get se sent only after
  3726. * hearing clear Rx packet).*/
  3727. if (IWL_UCODE_API(priv->ucode_ver) >= 2) {
  3728. if (n_probes)
  3729. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  3730. } else {
  3731. /* uCode v1 does not allow setting direct probe bits on
  3732. * passive channel. */
  3733. if ((scan_ch->type & 1) && n_probes)
  3734. scan_ch->type |= IWL_SCAN_PROBE_MASK(n_probes);
  3735. }
  3736. /* Set txpower levels to defaults */
  3737. scan_ch->tpc.dsp_atten = 110;
  3738. /* scan_pwr_info->tpc.dsp_atten; */
  3739. /*scan_pwr_info->tpc.tx_gain; */
  3740. if (band == IEEE80211_BAND_5GHZ)
  3741. scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
  3742. else {
  3743. scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
  3744. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  3745. * power level:
  3746. * scan_ch->tpc.tx_gain = ((1 << 5) | (2 << 3)) | 3;
  3747. */
  3748. }
  3749. IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
  3750. scan_ch->channel,
  3751. (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
  3752. (scan_ch->type & 1) ?
  3753. active_dwell : passive_dwell);
  3754. scan_ch++;
  3755. added++;
  3756. }
  3757. IWL_DEBUG_SCAN("total channels to scan %d \n", added);
  3758. return added;
  3759. }
  3760. static void iwl3945_init_hw_rates(struct iwl_priv *priv,
  3761. struct ieee80211_rate *rates)
  3762. {
  3763. int i;
  3764. for (i = 0; i < IWL_RATE_COUNT; i++) {
  3765. rates[i].bitrate = iwl3945_rates[i].ieee * 5;
  3766. rates[i].hw_value = i; /* Rate scaling will work on indexes */
  3767. rates[i].hw_value_short = i;
  3768. rates[i].flags = 0;
  3769. if ((i > IWL39_LAST_OFDM_RATE) || (i < IWL_FIRST_OFDM_RATE)) {
  3770. /*
  3771. * If CCK != 1M then set short preamble rate flag.
  3772. */
  3773. rates[i].flags |= (iwl3945_rates[i].plcp == 10) ?
  3774. 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  3775. }
  3776. }
  3777. }
  3778. /**
  3779. * iwl3945_init_geos - Initialize mac80211's geo/channel info based from eeprom
  3780. */
  3781. static int iwl3945_init_geos(struct iwl_priv *priv)
  3782. {
  3783. struct iwl_channel_info *ch;
  3784. struct ieee80211_supported_band *sband;
  3785. struct ieee80211_channel *channels;
  3786. struct ieee80211_channel *geo_ch;
  3787. struct ieee80211_rate *rates;
  3788. int i = 0;
  3789. if (priv->bands[IEEE80211_BAND_2GHZ].n_bitrates ||
  3790. priv->bands[IEEE80211_BAND_5GHZ].n_bitrates) {
  3791. IWL_DEBUG_INFO("Geography modes already initialized.\n");
  3792. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  3793. return 0;
  3794. }
  3795. channels = kzalloc(sizeof(struct ieee80211_channel) *
  3796. priv->channel_count, GFP_KERNEL);
  3797. if (!channels)
  3798. return -ENOMEM;
  3799. rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_RATE_COUNT + 1)),
  3800. GFP_KERNEL);
  3801. if (!rates) {
  3802. kfree(channels);
  3803. return -ENOMEM;
  3804. }
  3805. /* 5.2GHz channels start after the 2.4GHz channels */
  3806. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  3807. sband->channels = &channels[ARRAY_SIZE(iwl3945_eeprom_band_1)];
  3808. /* just OFDM */
  3809. sband->bitrates = &rates[IWL_FIRST_OFDM_RATE];
  3810. sband->n_bitrates = IWL_RATE_COUNT - IWL_FIRST_OFDM_RATE;
  3811. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  3812. sband->channels = channels;
  3813. /* OFDM & CCK */
  3814. sband->bitrates = rates;
  3815. sband->n_bitrates = IWL_RATE_COUNT;
  3816. priv->ieee_channels = channels;
  3817. priv->ieee_rates = rates;
  3818. iwl3945_init_hw_rates(priv, rates);
  3819. for (i = 0; i < priv->channel_count; i++) {
  3820. ch = &priv->channel_info[i];
  3821. /* FIXME: might be removed if scan is OK*/
  3822. if (!is_channel_valid(ch))
  3823. continue;
  3824. if (is_channel_a_band(ch))
  3825. sband = &priv->bands[IEEE80211_BAND_5GHZ];
  3826. else
  3827. sband = &priv->bands[IEEE80211_BAND_2GHZ];
  3828. geo_ch = &sband->channels[sband->n_channels++];
  3829. geo_ch->center_freq = ieee80211_channel_to_frequency(ch->channel);
  3830. geo_ch->max_power = ch->max_power_avg;
  3831. geo_ch->max_antenna_gain = 0xff;
  3832. geo_ch->hw_value = ch->channel;
  3833. if (is_channel_valid(ch)) {
  3834. if (!(ch->flags & EEPROM_CHANNEL_IBSS))
  3835. geo_ch->flags |= IEEE80211_CHAN_NO_IBSS;
  3836. if (!(ch->flags & EEPROM_CHANNEL_ACTIVE))
  3837. geo_ch->flags |= IEEE80211_CHAN_PASSIVE_SCAN;
  3838. if (ch->flags & EEPROM_CHANNEL_RADAR)
  3839. geo_ch->flags |= IEEE80211_CHAN_RADAR;
  3840. if (ch->max_power_avg > priv->max_channel_txpower_limit)
  3841. priv->max_channel_txpower_limit =
  3842. ch->max_power_avg;
  3843. } else {
  3844. geo_ch->flags |= IEEE80211_CHAN_DISABLED;
  3845. }
  3846. /* Save flags for reg domain usage */
  3847. geo_ch->orig_flags = geo_ch->flags;
  3848. IWL_DEBUG_INFO("Channel %d Freq=%d[%sGHz] %s flag=0%X\n",
  3849. ch->channel, geo_ch->center_freq,
  3850. is_channel_a_band(ch) ? "5.2" : "2.4",
  3851. geo_ch->flags & IEEE80211_CHAN_DISABLED ?
  3852. "restricted" : "valid",
  3853. geo_ch->flags);
  3854. }
  3855. if ((priv->bands[IEEE80211_BAND_5GHZ].n_channels == 0) &&
  3856. priv->cfg->sku & IWL_SKU_A) {
  3857. IWL_INFO(priv, "Incorrectly detected BG card as ABG. "
  3858. "Please send your PCI ID 0x%04X:0x%04X to maintainer.\n",
  3859. priv->pci_dev->device, priv->pci_dev->subsystem_device);
  3860. priv->cfg->sku &= ~IWL_SKU_A;
  3861. }
  3862. IWL_INFO(priv, "Tunable channels: %d 802.11bg, %d 802.11a channels\n",
  3863. priv->bands[IEEE80211_BAND_2GHZ].n_channels,
  3864. priv->bands[IEEE80211_BAND_5GHZ].n_channels);
  3865. if (priv->bands[IEEE80211_BAND_2GHZ].n_channels)
  3866. priv->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  3867. &priv->bands[IEEE80211_BAND_2GHZ];
  3868. if (priv->bands[IEEE80211_BAND_5GHZ].n_channels)
  3869. priv->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  3870. &priv->bands[IEEE80211_BAND_5GHZ];
  3871. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  3872. return 0;
  3873. }
  3874. /*
  3875. * iwl3945_free_geos - undo allocations in iwl3945_init_geos
  3876. */
  3877. static void iwl3945_free_geos(struct iwl_priv *priv)
  3878. {
  3879. kfree(priv->ieee_channels);
  3880. kfree(priv->ieee_rates);
  3881. clear_bit(STATUS_GEO_CONFIGURED, &priv->status);
  3882. }
  3883. /******************************************************************************
  3884. *
  3885. * uCode download functions
  3886. *
  3887. ******************************************************************************/
  3888. static void iwl3945_dealloc_ucode_pci(struct iwl_priv *priv)
  3889. {
  3890. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_code);
  3891. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data);
  3892. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  3893. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init);
  3894. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  3895. iwl_free_fw_desc(priv->pci_dev, &priv->ucode_boot);
  3896. }
  3897. /**
  3898. * iwl3945_verify_inst_full - verify runtime uCode image in card vs. host,
  3899. * looking at all data.
  3900. */
  3901. static int iwl3945_verify_inst_full(struct iwl_priv *priv, __le32 *image, u32 len)
  3902. {
  3903. u32 val;
  3904. u32 save_len = len;
  3905. int rc = 0;
  3906. u32 errcnt;
  3907. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  3908. rc = iwl_grab_nic_access(priv);
  3909. if (rc)
  3910. return rc;
  3911. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  3912. IWL39_RTC_INST_LOWER_BOUND);
  3913. errcnt = 0;
  3914. for (; len > 0; len -= sizeof(u32), image++) {
  3915. /* read data comes through single port, auto-incr addr */
  3916. /* NOTE: Use the debugless read so we don't flood kernel log
  3917. * if IWL_DL_IO is set */
  3918. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  3919. if (val != le32_to_cpu(*image)) {
  3920. IWL_ERR(priv, "uCode INST section is invalid at "
  3921. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  3922. save_len - len, val, le32_to_cpu(*image));
  3923. rc = -EIO;
  3924. errcnt++;
  3925. if (errcnt >= 20)
  3926. break;
  3927. }
  3928. }
  3929. iwl_release_nic_access(priv);
  3930. if (!errcnt)
  3931. IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
  3932. return rc;
  3933. }
  3934. /**
  3935. * iwl3945_verify_inst_sparse - verify runtime uCode image in card vs. host,
  3936. * using sample data 100 bytes apart. If these sample points are good,
  3937. * it's a pretty good bet that everything between them is good, too.
  3938. */
  3939. static int iwl3945_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
  3940. {
  3941. u32 val;
  3942. int rc = 0;
  3943. u32 errcnt = 0;
  3944. u32 i;
  3945. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  3946. rc = iwl_grab_nic_access(priv);
  3947. if (rc)
  3948. return rc;
  3949. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  3950. /* read data comes through single port, auto-incr addr */
  3951. /* NOTE: Use the debugless read so we don't flood kernel log
  3952. * if IWL_DL_IO is set */
  3953. iwl_write_direct32(priv, HBUS_TARG_MEM_RADDR,
  3954. i + IWL39_RTC_INST_LOWER_BOUND);
  3955. val = _iwl_read_direct32(priv, HBUS_TARG_MEM_RDAT);
  3956. if (val != le32_to_cpu(*image)) {
  3957. #if 0 /* Enable this if you want to see details */
  3958. IWL_ERR(priv, "uCode INST section is invalid at "
  3959. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  3960. i, val, *image);
  3961. #endif
  3962. rc = -EIO;
  3963. errcnt++;
  3964. if (errcnt >= 3)
  3965. break;
  3966. }
  3967. }
  3968. iwl_release_nic_access(priv);
  3969. return rc;
  3970. }
  3971. /**
  3972. * iwl3945_verify_ucode - determine which instruction image is in SRAM,
  3973. * and verify its contents
  3974. */
  3975. static int iwl3945_verify_ucode(struct iwl_priv *priv)
  3976. {
  3977. __le32 *image;
  3978. u32 len;
  3979. int rc = 0;
  3980. /* Try bootstrap */
  3981. image = (__le32 *)priv->ucode_boot.v_addr;
  3982. len = priv->ucode_boot.len;
  3983. rc = iwl3945_verify_inst_sparse(priv, image, len);
  3984. if (rc == 0) {
  3985. IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
  3986. return 0;
  3987. }
  3988. /* Try initialize */
  3989. image = (__le32 *)priv->ucode_init.v_addr;
  3990. len = priv->ucode_init.len;
  3991. rc = iwl3945_verify_inst_sparse(priv, image, len);
  3992. if (rc == 0) {
  3993. IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
  3994. return 0;
  3995. }
  3996. /* Try runtime/protocol */
  3997. image = (__le32 *)priv->ucode_code.v_addr;
  3998. len = priv->ucode_code.len;
  3999. rc = iwl3945_verify_inst_sparse(priv, image, len);
  4000. if (rc == 0) {
  4001. IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
  4002. return 0;
  4003. }
  4004. IWL_ERR(priv, "NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
  4005. /* Since nothing seems to match, show first several data entries in
  4006. * instruction SRAM, so maybe visual inspection will give a clue.
  4007. * Selection of bootstrap image (vs. other images) is arbitrary. */
  4008. image = (__le32 *)priv->ucode_boot.v_addr;
  4009. len = priv->ucode_boot.len;
  4010. rc = iwl3945_verify_inst_full(priv, image, len);
  4011. return rc;
  4012. }
  4013. static void iwl3945_nic_start(struct iwl_priv *priv)
  4014. {
  4015. /* Remove all resets to allow NIC to operate */
  4016. iwl_write32(priv, CSR_RESET, 0);
  4017. }
  4018. /**
  4019. * iwl3945_read_ucode - Read uCode images from disk file.
  4020. *
  4021. * Copy into buffers for card to fetch via bus-mastering
  4022. */
  4023. static int iwl3945_read_ucode(struct iwl_priv *priv)
  4024. {
  4025. struct iwl_ucode *ucode;
  4026. int ret = -EINVAL, index;
  4027. const struct firmware *ucode_raw;
  4028. /* firmware file name contains uCode/driver compatibility version */
  4029. const char *name_pre = priv->cfg->fw_name_pre;
  4030. const unsigned int api_max = priv->cfg->ucode_api_max;
  4031. const unsigned int api_min = priv->cfg->ucode_api_min;
  4032. char buf[25];
  4033. u8 *src;
  4034. size_t len;
  4035. u32 api_ver, inst_size, data_size, init_size, init_data_size, boot_size;
  4036. /* Ask kernel firmware_class module to get the boot firmware off disk.
  4037. * request_firmware() is synchronous, file is in memory on return. */
  4038. for (index = api_max; index >= api_min; index--) {
  4039. sprintf(buf, "%s%u%s", name_pre, index, ".ucode");
  4040. ret = request_firmware(&ucode_raw, buf, &priv->pci_dev->dev);
  4041. if (ret < 0) {
  4042. IWL_ERR(priv, "%s firmware file req failed: %d\n",
  4043. buf, ret);
  4044. if (ret == -ENOENT)
  4045. continue;
  4046. else
  4047. goto error;
  4048. } else {
  4049. if (index < api_max)
  4050. IWL_ERR(priv, "Loaded firmware %s, "
  4051. "which is deprecated. "
  4052. " Please use API v%u instead.\n",
  4053. buf, api_max);
  4054. IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
  4055. buf, ucode_raw->size);
  4056. break;
  4057. }
  4058. }
  4059. if (ret < 0)
  4060. goto error;
  4061. /* Make sure that we got at least our header! */
  4062. if (ucode_raw->size < sizeof(*ucode)) {
  4063. IWL_ERR(priv, "File size way too small!\n");
  4064. ret = -EINVAL;
  4065. goto err_release;
  4066. }
  4067. /* Data from ucode file: header followed by uCode images */
  4068. ucode = (void *)ucode_raw->data;
  4069. priv->ucode_ver = le32_to_cpu(ucode->ver);
  4070. api_ver = IWL_UCODE_API(priv->ucode_ver);
  4071. inst_size = le32_to_cpu(ucode->inst_size);
  4072. data_size = le32_to_cpu(ucode->data_size);
  4073. init_size = le32_to_cpu(ucode->init_size);
  4074. init_data_size = le32_to_cpu(ucode->init_data_size);
  4075. boot_size = le32_to_cpu(ucode->boot_size);
  4076. /* api_ver should match the api version forming part of the
  4077. * firmware filename ... but we don't check for that and only rely
  4078. * on the API version read from firware header from here on forward */
  4079. if (api_ver < api_min || api_ver > api_max) {
  4080. IWL_ERR(priv, "Driver unable to support your firmware API. "
  4081. "Driver supports v%u, firmware is v%u.\n",
  4082. api_max, api_ver);
  4083. priv->ucode_ver = 0;
  4084. ret = -EINVAL;
  4085. goto err_release;
  4086. }
  4087. if (api_ver != api_max)
  4088. IWL_ERR(priv, "Firmware has old API version. Expected %u, "
  4089. "got %u. New firmware can be obtained "
  4090. "from http://www.intellinuxwireless.org.\n",
  4091. api_max, api_ver);
  4092. IWL_INFO(priv, "loaded firmware version %u.%u.%u.%u\n",
  4093. IWL_UCODE_MAJOR(priv->ucode_ver),
  4094. IWL_UCODE_MINOR(priv->ucode_ver),
  4095. IWL_UCODE_API(priv->ucode_ver),
  4096. IWL_UCODE_SERIAL(priv->ucode_ver));
  4097. IWL_DEBUG_INFO("f/w package hdr ucode version raw = 0x%x\n",
  4098. priv->ucode_ver);
  4099. IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
  4100. IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
  4101. IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
  4102. IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
  4103. IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
  4104. /* Verify size of file vs. image size info in file's header */
  4105. if (ucode_raw->size < sizeof(*ucode) +
  4106. inst_size + data_size + init_size +
  4107. init_data_size + boot_size) {
  4108. IWL_DEBUG_INFO("uCode file size %d too small\n",
  4109. (int)ucode_raw->size);
  4110. ret = -EINVAL;
  4111. goto err_release;
  4112. }
  4113. /* Verify that uCode images will fit in card's SRAM */
  4114. if (inst_size > IWL39_MAX_INST_SIZE) {
  4115. IWL_DEBUG_INFO("uCode instr len %d too large to fit in\n",
  4116. inst_size);
  4117. ret = -EINVAL;
  4118. goto err_release;
  4119. }
  4120. if (data_size > IWL39_MAX_DATA_SIZE) {
  4121. IWL_DEBUG_INFO("uCode data len %d too large to fit in\n",
  4122. data_size);
  4123. ret = -EINVAL;
  4124. goto err_release;
  4125. }
  4126. if (init_size > IWL39_MAX_INST_SIZE) {
  4127. IWL_DEBUG_INFO("uCode init instr len %d too large to fit in\n",
  4128. init_size);
  4129. ret = -EINVAL;
  4130. goto err_release;
  4131. }
  4132. if (init_data_size > IWL39_MAX_DATA_SIZE) {
  4133. IWL_DEBUG_INFO("uCode init data len %d too large to fit in\n",
  4134. init_data_size);
  4135. ret = -EINVAL;
  4136. goto err_release;
  4137. }
  4138. if (boot_size > IWL39_MAX_BSM_SIZE) {
  4139. IWL_DEBUG_INFO("uCode boot instr len %d too large to fit in\n",
  4140. boot_size);
  4141. ret = -EINVAL;
  4142. goto err_release;
  4143. }
  4144. /* Allocate ucode buffers for card's bus-master loading ... */
  4145. /* Runtime instructions and 2 copies of data:
  4146. * 1) unmodified from disk
  4147. * 2) backup cache for save/restore during power-downs */
  4148. priv->ucode_code.len = inst_size;
  4149. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_code);
  4150. priv->ucode_data.len = data_size;
  4151. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data);
  4152. priv->ucode_data_backup.len = data_size;
  4153. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_data_backup);
  4154. if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
  4155. !priv->ucode_data_backup.v_addr)
  4156. goto err_pci_alloc;
  4157. /* Initialization instructions and data */
  4158. if (init_size && init_data_size) {
  4159. priv->ucode_init.len = init_size;
  4160. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init);
  4161. priv->ucode_init_data.len = init_data_size;
  4162. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_init_data);
  4163. if (!priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr)
  4164. goto err_pci_alloc;
  4165. }
  4166. /* Bootstrap (instructions only, no data) */
  4167. if (boot_size) {
  4168. priv->ucode_boot.len = boot_size;
  4169. iwl_alloc_fw_desc(priv->pci_dev, &priv->ucode_boot);
  4170. if (!priv->ucode_boot.v_addr)
  4171. goto err_pci_alloc;
  4172. }
  4173. /* Copy images into buffers for card's bus-master reads ... */
  4174. /* Runtime instructions (first block of data in file) */
  4175. src = &ucode->data[0];
  4176. len = priv->ucode_code.len;
  4177. IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %Zd\n", len);
  4178. memcpy(priv->ucode_code.v_addr, src, len);
  4179. IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  4180. priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
  4181. /* Runtime data (2nd block)
  4182. * NOTE: Copy into backup buffer will be done in iwl3945_up() */
  4183. src = &ucode->data[inst_size];
  4184. len = priv->ucode_data.len;
  4185. IWL_DEBUG_INFO("Copying (but not loading) uCode data len %Zd\n", len);
  4186. memcpy(priv->ucode_data.v_addr, src, len);
  4187. memcpy(priv->ucode_data_backup.v_addr, src, len);
  4188. /* Initialization instructions (3rd block) */
  4189. if (init_size) {
  4190. src = &ucode->data[inst_size + data_size];
  4191. len = priv->ucode_init.len;
  4192. IWL_DEBUG_INFO("Copying (but not loading) init instr len %Zd\n",
  4193. len);
  4194. memcpy(priv->ucode_init.v_addr, src, len);
  4195. }
  4196. /* Initialization data (4th block) */
  4197. if (init_data_size) {
  4198. src = &ucode->data[inst_size + data_size + init_size];
  4199. len = priv->ucode_init_data.len;
  4200. IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
  4201. (int)len);
  4202. memcpy(priv->ucode_init_data.v_addr, src, len);
  4203. }
  4204. /* Bootstrap instructions (5th block) */
  4205. src = &ucode->data[inst_size + data_size + init_size + init_data_size];
  4206. len = priv->ucode_boot.len;
  4207. IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
  4208. (int)len);
  4209. memcpy(priv->ucode_boot.v_addr, src, len);
  4210. /* We have our copies now, allow OS release its copies */
  4211. release_firmware(ucode_raw);
  4212. return 0;
  4213. err_pci_alloc:
  4214. IWL_ERR(priv, "failed to allocate pci memory\n");
  4215. ret = -ENOMEM;
  4216. iwl3945_dealloc_ucode_pci(priv);
  4217. err_release:
  4218. release_firmware(ucode_raw);
  4219. error:
  4220. return ret;
  4221. }
  4222. /**
  4223. * iwl3945_set_ucode_ptrs - Set uCode address location
  4224. *
  4225. * Tell initialization uCode where to find runtime uCode.
  4226. *
  4227. * BSM registers initially contain pointers to initialization uCode.
  4228. * We need to replace them to load runtime uCode inst and data,
  4229. * and to save runtime data when powering down.
  4230. */
  4231. static int iwl3945_set_ucode_ptrs(struct iwl_priv *priv)
  4232. {
  4233. dma_addr_t pinst;
  4234. dma_addr_t pdata;
  4235. int rc = 0;
  4236. unsigned long flags;
  4237. /* bits 31:0 for 3945 */
  4238. pinst = priv->ucode_code.p_addr;
  4239. pdata = priv->ucode_data_backup.p_addr;
  4240. spin_lock_irqsave(&priv->lock, flags);
  4241. rc = iwl_grab_nic_access(priv);
  4242. if (rc) {
  4243. spin_unlock_irqrestore(&priv->lock, flags);
  4244. return rc;
  4245. }
  4246. /* Tell bootstrap uCode where to find image to load */
  4247. iwl_write_prph(priv, BSM_DRAM_INST_PTR_REG, pinst);
  4248. iwl_write_prph(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  4249. iwl_write_prph(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
  4250. priv->ucode_data.len);
  4251. /* Inst byte count must be last to set up, bit 31 signals uCode
  4252. * that all new ptr/size info is in place */
  4253. iwl_write_prph(priv, BSM_DRAM_INST_BYTECOUNT_REG,
  4254. priv->ucode_code.len | BSM_DRAM_INST_LOAD);
  4255. iwl_release_nic_access(priv);
  4256. spin_unlock_irqrestore(&priv->lock, flags);
  4257. IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
  4258. return rc;
  4259. }
  4260. /**
  4261. * iwl3945_init_alive_start - Called after REPLY_ALIVE notification received
  4262. *
  4263. * Called after REPLY_ALIVE notification received from "initialize" uCode.
  4264. *
  4265. * Tell "initialize" uCode to go ahead and load the runtime uCode.
  4266. */
  4267. static void iwl3945_init_alive_start(struct iwl_priv *priv)
  4268. {
  4269. /* Check alive response for "valid" sign from uCode */
  4270. if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
  4271. /* We had an error bringing up the hardware, so take it
  4272. * all the way back down so we can try again */
  4273. IWL_DEBUG_INFO("Initialize Alive failed.\n");
  4274. goto restart;
  4275. }
  4276. /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
  4277. * This is a paranoid check, because we would not have gotten the
  4278. * "initialize" alive if code weren't properly loaded. */
  4279. if (iwl3945_verify_ucode(priv)) {
  4280. /* Runtime instruction load was bad;
  4281. * take it all the way back down so we can try again */
  4282. IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
  4283. goto restart;
  4284. }
  4285. /* Send pointers to protocol/runtime uCode image ... init code will
  4286. * load and launch runtime uCode, which will send us another "Alive"
  4287. * notification. */
  4288. IWL_DEBUG_INFO("Initialization Alive received.\n");
  4289. if (iwl3945_set_ucode_ptrs(priv)) {
  4290. /* Runtime instruction load won't happen;
  4291. * take it all the way back down so we can try again */
  4292. IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
  4293. goto restart;
  4294. }
  4295. return;
  4296. restart:
  4297. queue_work(priv->workqueue, &priv->restart);
  4298. }
  4299. /* temporary */
  4300. static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw,
  4301. struct sk_buff *skb);
  4302. /**
  4303. * iwl3945_alive_start - called after REPLY_ALIVE notification received
  4304. * from protocol/runtime uCode (initialization uCode's
  4305. * Alive gets handled by iwl3945_init_alive_start()).
  4306. */
  4307. static void iwl3945_alive_start(struct iwl_priv *priv)
  4308. {
  4309. int rc = 0;
  4310. int thermal_spin = 0;
  4311. u32 rfkill;
  4312. IWL_DEBUG_INFO("Runtime Alive received.\n");
  4313. if (priv->card_alive.is_valid != UCODE_VALID_OK) {
  4314. /* We had an error bringing up the hardware, so take it
  4315. * all the way back down so we can try again */
  4316. IWL_DEBUG_INFO("Alive failed.\n");
  4317. goto restart;
  4318. }
  4319. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  4320. * This is a paranoid check, because we would not have gotten the
  4321. * "runtime" alive if code weren't properly loaded. */
  4322. if (iwl3945_verify_ucode(priv)) {
  4323. /* Runtime instruction load was bad;
  4324. * take it all the way back down so we can try again */
  4325. IWL_DEBUG_INFO("Bad runtime uCode load.\n");
  4326. goto restart;
  4327. }
  4328. iwl3945_clear_stations_table(priv);
  4329. rc = iwl_grab_nic_access(priv);
  4330. if (rc) {
  4331. IWL_WARN(priv, "Can not read RFKILL status from adapter\n");
  4332. return;
  4333. }
  4334. rfkill = iwl_read_prph(priv, APMG_RFKILL_REG);
  4335. IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
  4336. iwl_release_nic_access(priv);
  4337. if (rfkill & 0x1) {
  4338. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  4339. /* if RFKILL is not on, then wait for thermal
  4340. * sensor in adapter to kick in */
  4341. while (iwl3945_hw_get_temperature(priv) == 0) {
  4342. thermal_spin++;
  4343. udelay(10);
  4344. }
  4345. if (thermal_spin)
  4346. IWL_DEBUG_INFO("Thermal calibration took %dus\n",
  4347. thermal_spin * 10);
  4348. } else
  4349. set_bit(STATUS_RF_KILL_HW, &priv->status);
  4350. /* After the ALIVE response, we can send commands to 3945 uCode */
  4351. set_bit(STATUS_ALIVE, &priv->status);
  4352. /* Clear out the uCode error bit if it is set */
  4353. clear_bit(STATUS_FW_ERROR, &priv->status);
  4354. if (iwl_is_rfkill(priv))
  4355. return;
  4356. ieee80211_wake_queues(priv->hw);
  4357. priv->active_rate = priv->rates_mask;
  4358. priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
  4359. iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
  4360. if (iwl3945_is_associated(priv)) {
  4361. struct iwl3945_rxon_cmd *active_rxon =
  4362. (struct iwl3945_rxon_cmd *)(&priv->active39_rxon);
  4363. memcpy(&priv->staging39_rxon, &priv->active39_rxon,
  4364. sizeof(priv->staging39_rxon));
  4365. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4366. } else {
  4367. /* Initialize our rx_config data */
  4368. iwl3945_connection_init_rx_config(priv, priv->iw_mode);
  4369. memcpy(priv->staging39_rxon.node_addr, priv->mac_addr, ETH_ALEN);
  4370. }
  4371. /* Configure Bluetooth device coexistence support */
  4372. iwl3945_send_bt_config(priv);
  4373. /* Configure the adapter for unassociated operation */
  4374. iwl3945_commit_rxon(priv);
  4375. iwl3945_reg_txpower_periodic(priv);
  4376. iwl3945_led_register(priv);
  4377. IWL_DEBUG_INFO("ALIVE processing complete.\n");
  4378. set_bit(STATUS_READY, &priv->status);
  4379. wake_up_interruptible(&priv->wait_command_queue);
  4380. if (priv->error_recovering)
  4381. iwl3945_error_recovery(priv);
  4382. /* reassociate for ADHOC mode */
  4383. if (priv->vif && (priv->iw_mode == NL80211_IFTYPE_ADHOC)) {
  4384. struct sk_buff *beacon = ieee80211_beacon_get(priv->hw,
  4385. priv->vif);
  4386. if (beacon)
  4387. iwl3945_mac_beacon_update(priv->hw, beacon);
  4388. }
  4389. return;
  4390. restart:
  4391. queue_work(priv->workqueue, &priv->restart);
  4392. }
  4393. static void iwl3945_cancel_deferred_work(struct iwl_priv *priv);
  4394. static void __iwl3945_down(struct iwl_priv *priv)
  4395. {
  4396. unsigned long flags;
  4397. int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
  4398. struct ieee80211_conf *conf = NULL;
  4399. IWL_DEBUG_INFO(DRV_NAME " is going down\n");
  4400. conf = ieee80211_get_hw_conf(priv->hw);
  4401. if (!exit_pending)
  4402. set_bit(STATUS_EXIT_PENDING, &priv->status);
  4403. iwl3945_led_unregister(priv);
  4404. iwl3945_clear_stations_table(priv);
  4405. /* Unblock any waiting calls */
  4406. wake_up_interruptible_all(&priv->wait_command_queue);
  4407. /* Wipe out the EXIT_PENDING status bit if we are not actually
  4408. * exiting the module */
  4409. if (!exit_pending)
  4410. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  4411. /* stop and reset the on-board processor */
  4412. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  4413. /* tell the device to stop sending interrupts */
  4414. spin_lock_irqsave(&priv->lock, flags);
  4415. iwl3945_disable_interrupts(priv);
  4416. spin_unlock_irqrestore(&priv->lock, flags);
  4417. iwl_synchronize_irq(priv);
  4418. if (priv->mac80211_registered)
  4419. ieee80211_stop_queues(priv->hw);
  4420. /* If we have not previously called iwl3945_init() then
  4421. * clear all bits but the RF Kill and SUSPEND bits and return */
  4422. if (!iwl_is_init(priv)) {
  4423. priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  4424. STATUS_RF_KILL_HW |
  4425. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  4426. STATUS_RF_KILL_SW |
  4427. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  4428. STATUS_GEO_CONFIGURED |
  4429. test_bit(STATUS_IN_SUSPEND, &priv->status) <<
  4430. STATUS_IN_SUSPEND |
  4431. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  4432. STATUS_EXIT_PENDING;
  4433. goto exit;
  4434. }
  4435. /* ...otherwise clear out all the status bits but the RF Kill and
  4436. * SUSPEND bits and continue taking the NIC down. */
  4437. priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  4438. STATUS_RF_KILL_HW |
  4439. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  4440. STATUS_RF_KILL_SW |
  4441. test_bit(STATUS_GEO_CONFIGURED, &priv->status) <<
  4442. STATUS_GEO_CONFIGURED |
  4443. test_bit(STATUS_IN_SUSPEND, &priv->status) <<
  4444. STATUS_IN_SUSPEND |
  4445. test_bit(STATUS_FW_ERROR, &priv->status) <<
  4446. STATUS_FW_ERROR |
  4447. test_bit(STATUS_EXIT_PENDING, &priv->status) <<
  4448. STATUS_EXIT_PENDING;
  4449. spin_lock_irqsave(&priv->lock, flags);
  4450. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  4451. spin_unlock_irqrestore(&priv->lock, flags);
  4452. iwl3945_hw_txq_ctx_stop(priv);
  4453. iwl3945_hw_rxq_stop(priv);
  4454. spin_lock_irqsave(&priv->lock, flags);
  4455. if (!iwl_grab_nic_access(priv)) {
  4456. iwl_write_prph(priv, APMG_CLK_DIS_REG,
  4457. APMG_CLK_VAL_DMA_CLK_RQT);
  4458. iwl_release_nic_access(priv);
  4459. }
  4460. spin_unlock_irqrestore(&priv->lock, flags);
  4461. udelay(5);
  4462. priv->cfg->ops->lib->apm_ops.reset(priv);
  4463. exit:
  4464. memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
  4465. if (priv->ibss_beacon)
  4466. dev_kfree_skb(priv->ibss_beacon);
  4467. priv->ibss_beacon = NULL;
  4468. /* clear out any free frames */
  4469. iwl3945_clear_free_frames(priv);
  4470. }
  4471. static void iwl3945_down(struct iwl_priv *priv)
  4472. {
  4473. mutex_lock(&priv->mutex);
  4474. __iwl3945_down(priv);
  4475. mutex_unlock(&priv->mutex);
  4476. iwl3945_cancel_deferred_work(priv);
  4477. }
  4478. #define MAX_HW_RESTARTS 5
  4479. static int __iwl3945_up(struct iwl_priv *priv)
  4480. {
  4481. int rc, i;
  4482. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  4483. IWL_WARN(priv, "Exit pending; will not bring the NIC up\n");
  4484. return -EIO;
  4485. }
  4486. if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
  4487. IWL_WARN(priv, "Radio disabled by SW RF kill (module "
  4488. "parameter)\n");
  4489. return -ENODEV;
  4490. }
  4491. if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
  4492. IWL_ERR(priv, "ucode not available for device bring up\n");
  4493. return -EIO;
  4494. }
  4495. /* If platform's RF_KILL switch is NOT set to KILL */
  4496. if (iwl_read32(priv, CSR_GP_CNTRL) &
  4497. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  4498. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  4499. else {
  4500. set_bit(STATUS_RF_KILL_HW, &priv->status);
  4501. if (!test_bit(STATUS_IN_SUSPEND, &priv->status)) {
  4502. IWL_WARN(priv, "Radio disabled by HW RF Kill switch\n");
  4503. return -ENODEV;
  4504. }
  4505. }
  4506. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  4507. rc = iwl3945_hw_nic_init(priv);
  4508. if (rc) {
  4509. IWL_ERR(priv, "Unable to int nic\n");
  4510. return rc;
  4511. }
  4512. /* make sure rfkill handshake bits are cleared */
  4513. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4514. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  4515. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  4516. /* clear (again), then enable host interrupts */
  4517. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  4518. iwl3945_enable_interrupts(priv);
  4519. /* really make sure rfkill handshake bits are cleared */
  4520. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4521. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4522. /* Copy original ucode data image from disk into backup cache.
  4523. * This will be used to initialize the on-board processor's
  4524. * data SRAM for a clean start when the runtime program first loads. */
  4525. memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
  4526. priv->ucode_data.len);
  4527. /* We return success when we resume from suspend and rf_kill is on. */
  4528. if (test_bit(STATUS_RF_KILL_HW, &priv->status))
  4529. return 0;
  4530. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  4531. iwl3945_clear_stations_table(priv);
  4532. /* load bootstrap state machine,
  4533. * load bootstrap program into processor's memory,
  4534. * prepare to load the "initialize" uCode */
  4535. priv->cfg->ops->lib->load_ucode(priv);
  4536. if (rc) {
  4537. IWL_ERR(priv,
  4538. "Unable to set up bootstrap uCode: %d\n", rc);
  4539. continue;
  4540. }
  4541. /* start card; "initialize" will load runtime ucode */
  4542. iwl3945_nic_start(priv);
  4543. IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
  4544. return 0;
  4545. }
  4546. set_bit(STATUS_EXIT_PENDING, &priv->status);
  4547. __iwl3945_down(priv);
  4548. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  4549. /* tried to restart and config the device for as long as our
  4550. * patience could withstand */
  4551. IWL_ERR(priv, "Unable to initialize device after %d attempts.\n", i);
  4552. return -EIO;
  4553. }
  4554. /*****************************************************************************
  4555. *
  4556. * Workqueue callbacks
  4557. *
  4558. *****************************************************************************/
  4559. static void iwl3945_bg_init_alive_start(struct work_struct *data)
  4560. {
  4561. struct iwl_priv *priv =
  4562. container_of(data, struct iwl_priv, init_alive_start.work);
  4563. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4564. return;
  4565. mutex_lock(&priv->mutex);
  4566. iwl3945_init_alive_start(priv);
  4567. mutex_unlock(&priv->mutex);
  4568. }
  4569. static void iwl3945_bg_alive_start(struct work_struct *data)
  4570. {
  4571. struct iwl_priv *priv =
  4572. container_of(data, struct iwl_priv, alive_start.work);
  4573. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4574. return;
  4575. mutex_lock(&priv->mutex);
  4576. iwl3945_alive_start(priv);
  4577. mutex_unlock(&priv->mutex);
  4578. }
  4579. static void iwl3945_bg_rf_kill(struct work_struct *work)
  4580. {
  4581. struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
  4582. wake_up_interruptible(&priv->wait_command_queue);
  4583. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4584. return;
  4585. mutex_lock(&priv->mutex);
  4586. if (!iwl_is_rfkill(priv)) {
  4587. IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
  4588. "HW and/or SW RF Kill no longer active, restarting "
  4589. "device\n");
  4590. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  4591. queue_work(priv->workqueue, &priv->restart);
  4592. } else {
  4593. if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
  4594. IWL_DEBUG_RF_KILL("Can not turn radio back on - "
  4595. "disabled by SW switch\n");
  4596. else
  4597. IWL_WARN(priv, "Radio Frequency Kill Switch is On:\n"
  4598. "Kill switch must be turned off for "
  4599. "wireless networking to work.\n");
  4600. }
  4601. mutex_unlock(&priv->mutex);
  4602. iwl3945_rfkill_set_hw_state(priv);
  4603. }
  4604. #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
  4605. static void iwl3945_bg_scan_check(struct work_struct *data)
  4606. {
  4607. struct iwl_priv *priv =
  4608. container_of(data, struct iwl_priv, scan_check.work);
  4609. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4610. return;
  4611. mutex_lock(&priv->mutex);
  4612. if (test_bit(STATUS_SCANNING, &priv->status) ||
  4613. test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  4614. IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
  4615. "Scan completion watchdog resetting adapter (%dms)\n",
  4616. jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
  4617. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  4618. iwl3945_send_scan_abort(priv);
  4619. }
  4620. mutex_unlock(&priv->mutex);
  4621. }
  4622. static void iwl3945_bg_request_scan(struct work_struct *data)
  4623. {
  4624. struct iwl_priv *priv =
  4625. container_of(data, struct iwl_priv, request_scan);
  4626. struct iwl_host_cmd cmd = {
  4627. .id = REPLY_SCAN_CMD,
  4628. .len = sizeof(struct iwl3945_scan_cmd),
  4629. .meta.flags = CMD_SIZE_HUGE,
  4630. };
  4631. int rc = 0;
  4632. struct iwl3945_scan_cmd *scan;
  4633. struct ieee80211_conf *conf = NULL;
  4634. u8 n_probes = 2;
  4635. enum ieee80211_band band;
  4636. DECLARE_SSID_BUF(ssid);
  4637. conf = ieee80211_get_hw_conf(priv->hw);
  4638. mutex_lock(&priv->mutex);
  4639. if (!iwl_is_ready(priv)) {
  4640. IWL_WARN(priv, "request scan called when driver not ready.\n");
  4641. goto done;
  4642. }
  4643. /* Make sure the scan wasn't canceled before this queued work
  4644. * was given the chance to run... */
  4645. if (!test_bit(STATUS_SCANNING, &priv->status))
  4646. goto done;
  4647. /* This should never be called or scheduled if there is currently
  4648. * a scan active in the hardware. */
  4649. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  4650. IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
  4651. "Ignoring second request.\n");
  4652. rc = -EIO;
  4653. goto done;
  4654. }
  4655. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  4656. IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
  4657. goto done;
  4658. }
  4659. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  4660. IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
  4661. goto done;
  4662. }
  4663. if (iwl_is_rfkill(priv)) {
  4664. IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
  4665. goto done;
  4666. }
  4667. if (!test_bit(STATUS_READY, &priv->status)) {
  4668. IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
  4669. goto done;
  4670. }
  4671. if (!priv->scan_bands) {
  4672. IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
  4673. goto done;
  4674. }
  4675. if (!priv->scan39) {
  4676. priv->scan39 = kmalloc(sizeof(struct iwl3945_scan_cmd) +
  4677. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  4678. if (!priv->scan39) {
  4679. rc = -ENOMEM;
  4680. goto done;
  4681. }
  4682. }
  4683. scan = priv->scan39;
  4684. memset(scan, 0, sizeof(struct iwl3945_scan_cmd) + IWL_MAX_SCAN_SIZE);
  4685. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  4686. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  4687. if (iwl3945_is_associated(priv)) {
  4688. u16 interval = 0;
  4689. u32 extra;
  4690. u32 suspend_time = 100;
  4691. u32 scan_suspend_time = 100;
  4692. unsigned long flags;
  4693. IWL_DEBUG_INFO("Scanning while associated...\n");
  4694. spin_lock_irqsave(&priv->lock, flags);
  4695. interval = priv->beacon_int;
  4696. spin_unlock_irqrestore(&priv->lock, flags);
  4697. scan->suspend_time = 0;
  4698. scan->max_out_time = cpu_to_le32(200 * 1024);
  4699. if (!interval)
  4700. interval = suspend_time;
  4701. /*
  4702. * suspend time format:
  4703. * 0-19: beacon interval in usec (time before exec.)
  4704. * 20-23: 0
  4705. * 24-31: number of beacons (suspend between channels)
  4706. */
  4707. extra = (suspend_time / interval) << 24;
  4708. scan_suspend_time = 0xFF0FFFFF &
  4709. (extra | ((suspend_time % interval) * 1024));
  4710. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  4711. IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
  4712. scan_suspend_time, interval);
  4713. }
  4714. /* We should add the ability for user to lock to PASSIVE ONLY */
  4715. if (priv->one_direct_scan) {
  4716. IWL_DEBUG_SCAN
  4717. ("Kicking off one direct scan for '%s'\n",
  4718. print_ssid(ssid, priv->direct_ssid,
  4719. priv->direct_ssid_len));
  4720. scan->direct_scan[0].id = WLAN_EID_SSID;
  4721. scan->direct_scan[0].len = priv->direct_ssid_len;
  4722. memcpy(scan->direct_scan[0].ssid,
  4723. priv->direct_ssid, priv->direct_ssid_len);
  4724. n_probes++;
  4725. } else
  4726. IWL_DEBUG_SCAN("Kicking off one indirect scan.\n");
  4727. /* We don't build a direct scan probe request; the uCode will do
  4728. * that based on the direct_mask added to each channel entry */
  4729. scan->tx_cmd.len = cpu_to_le16(
  4730. iwl3945_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
  4731. IWL_MAX_SCAN_SIZE - sizeof(*scan)));
  4732. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  4733. scan->tx_cmd.sta_id = priv->hw_params.bcast_sta_id;
  4734. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  4735. /* flags + rate selection */
  4736. if (priv->scan_bands & BIT(IEEE80211_BAND_2GHZ)) {
  4737. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  4738. scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
  4739. scan->good_CRC_th = 0;
  4740. band = IEEE80211_BAND_2GHZ;
  4741. } else if (priv->scan_bands & BIT(IEEE80211_BAND_5GHZ)) {
  4742. scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
  4743. scan->good_CRC_th = IWL_GOOD_CRC_TH;
  4744. band = IEEE80211_BAND_5GHZ;
  4745. } else {
  4746. IWL_WARN(priv, "Invalid scan band count\n");
  4747. goto done;
  4748. }
  4749. /* select Rx antennas */
  4750. scan->flags |= iwl3945_get_antenna_flags(priv);
  4751. if (priv->iw_mode == NL80211_IFTYPE_MONITOR)
  4752. scan->filter_flags = RXON_FILTER_PROMISC_MSK;
  4753. scan->channel_count =
  4754. iwl3945_get_channels_for_scan(priv, band, 1, /* active */
  4755. n_probes,
  4756. (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
  4757. if (scan->channel_count == 0) {
  4758. IWL_DEBUG_SCAN("channel count %d\n", scan->channel_count);
  4759. goto done;
  4760. }
  4761. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  4762. scan->channel_count * sizeof(struct iwl3945_scan_channel);
  4763. cmd.data = scan;
  4764. scan->len = cpu_to_le16(cmd.len);
  4765. set_bit(STATUS_SCAN_HW, &priv->status);
  4766. rc = iwl3945_send_cmd_sync(priv, &cmd);
  4767. if (rc)
  4768. goto done;
  4769. queue_delayed_work(priv->workqueue, &priv->scan_check,
  4770. IWL_SCAN_CHECK_WATCHDOG);
  4771. mutex_unlock(&priv->mutex);
  4772. return;
  4773. done:
  4774. /* can not perform scan make sure we clear scanning
  4775. * bits from status so next scan request can be performed.
  4776. * if we dont clear scanning status bit here all next scan
  4777. * will fail
  4778. */
  4779. clear_bit(STATUS_SCAN_HW, &priv->status);
  4780. clear_bit(STATUS_SCANNING, &priv->status);
  4781. /* inform mac80211 scan aborted */
  4782. queue_work(priv->workqueue, &priv->scan_completed);
  4783. mutex_unlock(&priv->mutex);
  4784. }
  4785. static void iwl3945_bg_up(struct work_struct *data)
  4786. {
  4787. struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
  4788. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4789. return;
  4790. mutex_lock(&priv->mutex);
  4791. __iwl3945_up(priv);
  4792. mutex_unlock(&priv->mutex);
  4793. iwl3945_rfkill_set_hw_state(priv);
  4794. }
  4795. static void iwl3945_bg_restart(struct work_struct *data)
  4796. {
  4797. struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
  4798. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4799. return;
  4800. iwl3945_down(priv);
  4801. queue_work(priv->workqueue, &priv->up);
  4802. }
  4803. static void iwl3945_bg_rx_replenish(struct work_struct *data)
  4804. {
  4805. struct iwl_priv *priv =
  4806. container_of(data, struct iwl_priv, rx_replenish);
  4807. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4808. return;
  4809. mutex_lock(&priv->mutex);
  4810. iwl3945_rx_replenish(priv);
  4811. mutex_unlock(&priv->mutex);
  4812. }
  4813. #define IWL_DELAY_NEXT_SCAN (HZ*2)
  4814. static void iwl3945_post_associate(struct iwl_priv *priv)
  4815. {
  4816. int rc = 0;
  4817. struct ieee80211_conf *conf = NULL;
  4818. if (priv->iw_mode == NL80211_IFTYPE_AP) {
  4819. IWL_ERR(priv, "%s Should not be called in AP mode\n", __func__);
  4820. return;
  4821. }
  4822. IWL_DEBUG_ASSOC("Associated as %d to: %pM\n",
  4823. priv->assoc_id, priv->active39_rxon.bssid_addr);
  4824. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4825. return;
  4826. if (!priv->vif || !priv->is_open)
  4827. return;
  4828. iwl3945_scan_cancel_timeout(priv, 200);
  4829. conf = ieee80211_get_hw_conf(priv->hw);
  4830. priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4831. iwl3945_commit_rxon(priv);
  4832. memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
  4833. iwl3945_setup_rxon_timing(priv);
  4834. rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  4835. sizeof(priv->rxon_timing), &priv->rxon_timing);
  4836. if (rc)
  4837. IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
  4838. "Attempting to continue.\n");
  4839. priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  4840. priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  4841. IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
  4842. priv->assoc_id, priv->beacon_int);
  4843. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  4844. priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  4845. else
  4846. priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  4847. if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  4848. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
  4849. priv->staging39_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  4850. else
  4851. priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  4852. if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
  4853. priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  4854. }
  4855. iwl3945_commit_rxon(priv);
  4856. switch (priv->iw_mode) {
  4857. case NL80211_IFTYPE_STATION:
  4858. iwl3945_rate_scale_init(priv->hw, IWL_AP_ID);
  4859. break;
  4860. case NL80211_IFTYPE_ADHOC:
  4861. priv->assoc_id = 1;
  4862. iwl3945_add_station(priv, priv->bssid, 0, 0);
  4863. iwl3945_sync_sta(priv, IWL_STA_ID,
  4864. (priv->band == IEEE80211_BAND_5GHZ) ?
  4865. IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
  4866. CMD_ASYNC);
  4867. iwl3945_rate_scale_init(priv->hw, IWL_STA_ID);
  4868. iwl3945_send_beacon_cmd(priv);
  4869. break;
  4870. default:
  4871. IWL_ERR(priv, "%s Should not be called in %d mode\n",
  4872. __func__, priv->iw_mode);
  4873. break;
  4874. }
  4875. iwl3945_activate_qos(priv, 0);
  4876. /* we have just associated, don't start scan too early */
  4877. priv->next_scan_jiffies = jiffies + IWL_DELAY_NEXT_SCAN;
  4878. }
  4879. static void iwl3945_bg_abort_scan(struct work_struct *work)
  4880. {
  4881. struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
  4882. if (!iwl_is_ready(priv))
  4883. return;
  4884. mutex_lock(&priv->mutex);
  4885. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  4886. iwl3945_send_scan_abort(priv);
  4887. mutex_unlock(&priv->mutex);
  4888. }
  4889. static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed);
  4890. static void iwl3945_bg_scan_completed(struct work_struct *work)
  4891. {
  4892. struct iwl_priv *priv =
  4893. container_of(work, struct iwl_priv, scan_completed);
  4894. IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
  4895. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  4896. return;
  4897. if (test_bit(STATUS_CONF_PENDING, &priv->status))
  4898. iwl3945_mac_config(priv->hw, 0);
  4899. ieee80211_scan_completed(priv->hw);
  4900. /* Since setting the TXPOWER may have been deferred while
  4901. * performing the scan, fire one off */
  4902. mutex_lock(&priv->mutex);
  4903. iwl3945_hw_reg_send_txpower(priv);
  4904. mutex_unlock(&priv->mutex);
  4905. }
  4906. /*****************************************************************************
  4907. *
  4908. * mac80211 entry point functions
  4909. *
  4910. *****************************************************************************/
  4911. #define UCODE_READY_TIMEOUT (2 * HZ)
  4912. static int iwl3945_mac_start(struct ieee80211_hw *hw)
  4913. {
  4914. struct iwl_priv *priv = hw->priv;
  4915. int ret;
  4916. IWL_DEBUG_MAC80211("enter\n");
  4917. if (pci_enable_device(priv->pci_dev)) {
  4918. IWL_ERR(priv, "Fail to pci_enable_device\n");
  4919. return -ENODEV;
  4920. }
  4921. pci_restore_state(priv->pci_dev);
  4922. pci_enable_msi(priv->pci_dev);
  4923. ret = request_irq(priv->pci_dev->irq, iwl3945_isr, IRQF_SHARED,
  4924. DRV_NAME, priv);
  4925. if (ret) {
  4926. IWL_ERR(priv, "Error allocating IRQ %d\n", priv->pci_dev->irq);
  4927. goto out_disable_msi;
  4928. }
  4929. /* we should be verifying the device is ready to be opened */
  4930. mutex_lock(&priv->mutex);
  4931. memset(&priv->staging39_rxon, 0, sizeof(struct iwl3945_rxon_cmd));
  4932. /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
  4933. * ucode filename and max sizes are card-specific. */
  4934. if (!priv->ucode_code.len) {
  4935. ret = iwl3945_read_ucode(priv);
  4936. if (ret) {
  4937. IWL_ERR(priv, "Could not read microcode: %d\n", ret);
  4938. mutex_unlock(&priv->mutex);
  4939. goto out_release_irq;
  4940. }
  4941. }
  4942. ret = __iwl3945_up(priv);
  4943. mutex_unlock(&priv->mutex);
  4944. iwl3945_rfkill_set_hw_state(priv);
  4945. if (ret)
  4946. goto out_release_irq;
  4947. IWL_DEBUG_INFO("Start UP work.\n");
  4948. if (test_bit(STATUS_IN_SUSPEND, &priv->status))
  4949. return 0;
  4950. /* Wait for START_ALIVE from ucode. Otherwise callbacks from
  4951. * mac80211 will not be run successfully. */
  4952. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  4953. test_bit(STATUS_READY, &priv->status),
  4954. UCODE_READY_TIMEOUT);
  4955. if (!ret) {
  4956. if (!test_bit(STATUS_READY, &priv->status)) {
  4957. IWL_ERR(priv,
  4958. "Wait for START_ALIVE timeout after %dms.\n",
  4959. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  4960. ret = -ETIMEDOUT;
  4961. goto out_release_irq;
  4962. }
  4963. }
  4964. priv->is_open = 1;
  4965. IWL_DEBUG_MAC80211("leave\n");
  4966. return 0;
  4967. out_release_irq:
  4968. free_irq(priv->pci_dev->irq, priv);
  4969. out_disable_msi:
  4970. pci_disable_msi(priv->pci_dev);
  4971. pci_disable_device(priv->pci_dev);
  4972. priv->is_open = 0;
  4973. IWL_DEBUG_MAC80211("leave - failed\n");
  4974. return ret;
  4975. }
  4976. static void iwl3945_mac_stop(struct ieee80211_hw *hw)
  4977. {
  4978. struct iwl_priv *priv = hw->priv;
  4979. IWL_DEBUG_MAC80211("enter\n");
  4980. if (!priv->is_open) {
  4981. IWL_DEBUG_MAC80211("leave - skip\n");
  4982. return;
  4983. }
  4984. priv->is_open = 0;
  4985. if (iwl_is_ready_rf(priv)) {
  4986. /* stop mac, cancel any scan request and clear
  4987. * RXON_FILTER_ASSOC_MSK BIT
  4988. */
  4989. mutex_lock(&priv->mutex);
  4990. iwl3945_scan_cancel_timeout(priv, 100);
  4991. mutex_unlock(&priv->mutex);
  4992. }
  4993. iwl3945_down(priv);
  4994. flush_workqueue(priv->workqueue);
  4995. free_irq(priv->pci_dev->irq, priv);
  4996. pci_disable_msi(priv->pci_dev);
  4997. pci_save_state(priv->pci_dev);
  4998. pci_disable_device(priv->pci_dev);
  4999. IWL_DEBUG_MAC80211("leave\n");
  5000. }
  5001. static int iwl3945_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb)
  5002. {
  5003. struct iwl_priv *priv = hw->priv;
  5004. IWL_DEBUG_MAC80211("enter\n");
  5005. IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  5006. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  5007. if (iwl3945_tx_skb(priv, skb))
  5008. dev_kfree_skb_any(skb);
  5009. IWL_DEBUG_MAC80211("leave\n");
  5010. return NETDEV_TX_OK;
  5011. }
  5012. static int iwl3945_mac_add_interface(struct ieee80211_hw *hw,
  5013. struct ieee80211_if_init_conf *conf)
  5014. {
  5015. struct iwl_priv *priv = hw->priv;
  5016. unsigned long flags;
  5017. IWL_DEBUG_MAC80211("enter: type %d\n", conf->type);
  5018. if (priv->vif) {
  5019. IWL_DEBUG_MAC80211("leave - vif != NULL\n");
  5020. return -EOPNOTSUPP;
  5021. }
  5022. spin_lock_irqsave(&priv->lock, flags);
  5023. priv->vif = conf->vif;
  5024. priv->iw_mode = conf->type;
  5025. spin_unlock_irqrestore(&priv->lock, flags);
  5026. mutex_lock(&priv->mutex);
  5027. if (conf->mac_addr) {
  5028. IWL_DEBUG_MAC80211("Set: %pM\n", conf->mac_addr);
  5029. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  5030. }
  5031. if (iwl_is_ready(priv))
  5032. iwl3945_set_mode(priv, conf->type);
  5033. mutex_unlock(&priv->mutex);
  5034. IWL_DEBUG_MAC80211("leave\n");
  5035. return 0;
  5036. }
  5037. /**
  5038. * iwl3945_mac_config - mac80211 config callback
  5039. *
  5040. * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
  5041. * be set inappropriately and the driver currently sets the hardware up to
  5042. * use it whenever needed.
  5043. */
  5044. static int iwl3945_mac_config(struct ieee80211_hw *hw, u32 changed)
  5045. {
  5046. struct iwl_priv *priv = hw->priv;
  5047. const struct iwl_channel_info *ch_info;
  5048. struct ieee80211_conf *conf = &hw->conf;
  5049. unsigned long flags;
  5050. int ret = 0;
  5051. mutex_lock(&priv->mutex);
  5052. IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel->hw_value);
  5053. if (!iwl_is_ready(priv)) {
  5054. IWL_DEBUG_MAC80211("leave - not ready\n");
  5055. ret = -EIO;
  5056. goto out;
  5057. }
  5058. if (unlikely(!iwl3945_mod_params.disable_hw_scan &&
  5059. test_bit(STATUS_SCANNING, &priv->status))) {
  5060. IWL_DEBUG_MAC80211("leave - scanning\n");
  5061. set_bit(STATUS_CONF_PENDING, &priv->status);
  5062. mutex_unlock(&priv->mutex);
  5063. return 0;
  5064. }
  5065. spin_lock_irqsave(&priv->lock, flags);
  5066. ch_info = iwl3945_get_channel_info(priv, conf->channel->band,
  5067. conf->channel->hw_value);
  5068. if (!is_channel_valid(ch_info)) {
  5069. IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this band.\n",
  5070. conf->channel->hw_value, conf->channel->band);
  5071. IWL_DEBUG_MAC80211("leave - invalid channel\n");
  5072. spin_unlock_irqrestore(&priv->lock, flags);
  5073. ret = -EINVAL;
  5074. goto out;
  5075. }
  5076. iwl3945_set_rxon_channel(priv, conf->channel->band, conf->channel->hw_value);
  5077. iwl3945_set_flags_for_phymode(priv, conf->channel->band);
  5078. /* The list of supported rates and rate mask can be different
  5079. * for each phymode; since the phymode may have changed, reset
  5080. * the rate mask to what mac80211 lists */
  5081. iwl3945_set_rate(priv);
  5082. spin_unlock_irqrestore(&priv->lock, flags);
  5083. #ifdef IEEE80211_CONF_CHANNEL_SWITCH
  5084. if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
  5085. iwl3945_hw_channel_switch(priv, conf->channel);
  5086. goto out;
  5087. }
  5088. #endif
  5089. iwl3945_radio_kill_sw(priv, !conf->radio_enabled);
  5090. if (!conf->radio_enabled) {
  5091. IWL_DEBUG_MAC80211("leave - radio disabled\n");
  5092. goto out;
  5093. }
  5094. if (iwl_is_rfkill(priv)) {
  5095. IWL_DEBUG_MAC80211("leave - RF kill\n");
  5096. ret = -EIO;
  5097. goto out;
  5098. }
  5099. iwl3945_set_rate(priv);
  5100. if (memcmp(&priv->active39_rxon,
  5101. &priv->staging39_rxon, sizeof(priv->staging39_rxon)))
  5102. iwl3945_commit_rxon(priv);
  5103. else
  5104. IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
  5105. IWL_DEBUG_MAC80211("leave\n");
  5106. out:
  5107. clear_bit(STATUS_CONF_PENDING, &priv->status);
  5108. mutex_unlock(&priv->mutex);
  5109. return ret;
  5110. }
  5111. static void iwl3945_config_ap(struct iwl_priv *priv)
  5112. {
  5113. int rc = 0;
  5114. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5115. return;
  5116. /* The following should be done only at AP bring up */
  5117. if (!(iwl3945_is_associated(priv))) {
  5118. /* RXON - unassoc (to set timing command) */
  5119. priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5120. iwl3945_commit_rxon(priv);
  5121. /* RXON Timing */
  5122. memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
  5123. iwl3945_setup_rxon_timing(priv);
  5124. rc = iwl3945_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  5125. sizeof(priv->rxon_timing), &priv->rxon_timing);
  5126. if (rc)
  5127. IWL_WARN(priv, "REPLY_RXON_TIMING failed - "
  5128. "Attempting to continue.\n");
  5129. /* FIXME: what should be the assoc_id for AP? */
  5130. priv->staging39_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  5131. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  5132. priv->staging39_rxon.flags |=
  5133. RXON_FLG_SHORT_PREAMBLE_MSK;
  5134. else
  5135. priv->staging39_rxon.flags &=
  5136. ~RXON_FLG_SHORT_PREAMBLE_MSK;
  5137. if (priv->staging39_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  5138. if (priv->assoc_capability &
  5139. WLAN_CAPABILITY_SHORT_SLOT_TIME)
  5140. priv->staging39_rxon.flags |=
  5141. RXON_FLG_SHORT_SLOT_MSK;
  5142. else
  5143. priv->staging39_rxon.flags &=
  5144. ~RXON_FLG_SHORT_SLOT_MSK;
  5145. if (priv->iw_mode == NL80211_IFTYPE_ADHOC)
  5146. priv->staging39_rxon.flags &=
  5147. ~RXON_FLG_SHORT_SLOT_MSK;
  5148. }
  5149. /* restore RXON assoc */
  5150. priv->staging39_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  5151. iwl3945_commit_rxon(priv);
  5152. iwl3945_add_station(priv, iwl_bcast_addr, 0, 0);
  5153. }
  5154. iwl3945_send_beacon_cmd(priv);
  5155. /* FIXME - we need to add code here to detect a totally new
  5156. * configuration, reset the AP, unassoc, rxon timing, assoc,
  5157. * clear sta table, add BCAST sta... */
  5158. }
  5159. static int iwl3945_mac_config_interface(struct ieee80211_hw *hw,
  5160. struct ieee80211_vif *vif,
  5161. struct ieee80211_if_conf *conf)
  5162. {
  5163. struct iwl_priv *priv = hw->priv;
  5164. int rc;
  5165. if (conf == NULL)
  5166. return -EIO;
  5167. if (priv->vif != vif) {
  5168. IWL_DEBUG_MAC80211("leave - priv->vif != vif\n");
  5169. return 0;
  5170. }
  5171. /* handle this temporarily here */
  5172. if (priv->iw_mode == NL80211_IFTYPE_ADHOC &&
  5173. conf->changed & IEEE80211_IFCC_BEACON) {
  5174. struct sk_buff *beacon = ieee80211_beacon_get(hw, vif);
  5175. if (!beacon)
  5176. return -ENOMEM;
  5177. mutex_lock(&priv->mutex);
  5178. rc = iwl3945_mac_beacon_update(hw, beacon);
  5179. mutex_unlock(&priv->mutex);
  5180. if (rc)
  5181. return rc;
  5182. }
  5183. if (!iwl_is_alive(priv))
  5184. return -EAGAIN;
  5185. mutex_lock(&priv->mutex);
  5186. if (conf->bssid)
  5187. IWL_DEBUG_MAC80211("bssid: %pM\n", conf->bssid);
  5188. /*
  5189. * very dubious code was here; the probe filtering flag is never set:
  5190. *
  5191. if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
  5192. !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
  5193. */
  5194. if (priv->iw_mode == NL80211_IFTYPE_AP) {
  5195. if (!conf->bssid) {
  5196. conf->bssid = priv->mac_addr;
  5197. memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
  5198. IWL_DEBUG_MAC80211("bssid was set to: %pM\n",
  5199. conf->bssid);
  5200. }
  5201. if (priv->ibss_beacon)
  5202. dev_kfree_skb(priv->ibss_beacon);
  5203. priv->ibss_beacon = ieee80211_beacon_get(hw, vif);
  5204. }
  5205. if (iwl_is_rfkill(priv))
  5206. goto done;
  5207. if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
  5208. !is_multicast_ether_addr(conf->bssid)) {
  5209. /* If there is currently a HW scan going on in the background
  5210. * then we need to cancel it else the RXON below will fail. */
  5211. if (iwl3945_scan_cancel_timeout(priv, 100)) {
  5212. IWL_WARN(priv, "Aborted scan still in progress "
  5213. "after 100ms\n");
  5214. IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
  5215. mutex_unlock(&priv->mutex);
  5216. return -EAGAIN;
  5217. }
  5218. memcpy(priv->staging39_rxon.bssid_addr, conf->bssid, ETH_ALEN);
  5219. /* TODO: Audit driver for usage of these members and see
  5220. * if mac80211 deprecates them (priv->bssid looks like it
  5221. * shouldn't be there, but I haven't scanned the IBSS code
  5222. * to verify) - jpk */
  5223. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  5224. if (priv->iw_mode == NL80211_IFTYPE_AP)
  5225. iwl3945_config_ap(priv);
  5226. else {
  5227. rc = iwl3945_commit_rxon(priv);
  5228. if ((priv->iw_mode == NL80211_IFTYPE_STATION) && rc)
  5229. iwl3945_add_station(priv,
  5230. priv->active39_rxon.bssid_addr, 1, 0);
  5231. }
  5232. } else {
  5233. iwl3945_scan_cancel_timeout(priv, 100);
  5234. priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5235. iwl3945_commit_rxon(priv);
  5236. }
  5237. done:
  5238. IWL_DEBUG_MAC80211("leave\n");
  5239. mutex_unlock(&priv->mutex);
  5240. return 0;
  5241. }
  5242. static void iwl3945_configure_filter(struct ieee80211_hw *hw,
  5243. unsigned int changed_flags,
  5244. unsigned int *total_flags,
  5245. int mc_count, struct dev_addr_list *mc_list)
  5246. {
  5247. struct iwl_priv *priv = hw->priv;
  5248. __le32 *filter_flags = &priv->staging39_rxon.filter_flags;
  5249. IWL_DEBUG_MAC80211("Enter: changed: 0x%x, total: 0x%x\n",
  5250. changed_flags, *total_flags);
  5251. if (changed_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS)) {
  5252. if (*total_flags & (FIF_OTHER_BSS | FIF_PROMISC_IN_BSS))
  5253. *filter_flags |= RXON_FILTER_PROMISC_MSK;
  5254. else
  5255. *filter_flags &= ~RXON_FILTER_PROMISC_MSK;
  5256. }
  5257. if (changed_flags & FIF_ALLMULTI) {
  5258. if (*total_flags & FIF_ALLMULTI)
  5259. *filter_flags |= RXON_FILTER_ACCEPT_GRP_MSK;
  5260. else
  5261. *filter_flags &= ~RXON_FILTER_ACCEPT_GRP_MSK;
  5262. }
  5263. if (changed_flags & FIF_CONTROL) {
  5264. if (*total_flags & FIF_CONTROL)
  5265. *filter_flags |= RXON_FILTER_CTL2HOST_MSK;
  5266. else
  5267. *filter_flags &= ~RXON_FILTER_CTL2HOST_MSK;
  5268. }
  5269. if (changed_flags & FIF_BCN_PRBRESP_PROMISC) {
  5270. if (*total_flags & FIF_BCN_PRBRESP_PROMISC)
  5271. *filter_flags |= RXON_FILTER_BCON_AWARE_MSK;
  5272. else
  5273. *filter_flags &= ~RXON_FILTER_BCON_AWARE_MSK;
  5274. }
  5275. /* We avoid iwl_commit_rxon here to commit the new filter flags
  5276. * since mac80211 will call ieee80211_hw_config immediately.
  5277. * (mc_list is not supported at this time). Otherwise, we need to
  5278. * queue a background iwl_commit_rxon work.
  5279. */
  5280. *total_flags &= FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
  5281. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  5282. }
  5283. static void iwl3945_mac_remove_interface(struct ieee80211_hw *hw,
  5284. struct ieee80211_if_init_conf *conf)
  5285. {
  5286. struct iwl_priv *priv = hw->priv;
  5287. IWL_DEBUG_MAC80211("enter\n");
  5288. mutex_lock(&priv->mutex);
  5289. if (iwl_is_ready_rf(priv)) {
  5290. iwl3945_scan_cancel_timeout(priv, 100);
  5291. priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5292. iwl3945_commit_rxon(priv);
  5293. }
  5294. if (priv->vif == conf->vif) {
  5295. priv->vif = NULL;
  5296. memset(priv->bssid, 0, ETH_ALEN);
  5297. }
  5298. mutex_unlock(&priv->mutex);
  5299. IWL_DEBUG_MAC80211("leave\n");
  5300. }
  5301. #define IWL_DELAY_NEXT_SCAN_AFTER_ASSOC (HZ*6)
  5302. static void iwl3945_bss_info_changed(struct ieee80211_hw *hw,
  5303. struct ieee80211_vif *vif,
  5304. struct ieee80211_bss_conf *bss_conf,
  5305. u32 changes)
  5306. {
  5307. struct iwl_priv *priv = hw->priv;
  5308. IWL_DEBUG_MAC80211("changes = 0x%X\n", changes);
  5309. if (changes & BSS_CHANGED_ERP_PREAMBLE) {
  5310. IWL_DEBUG_MAC80211("ERP_PREAMBLE %d\n",
  5311. bss_conf->use_short_preamble);
  5312. if (bss_conf->use_short_preamble)
  5313. priv->staging39_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  5314. else
  5315. priv->staging39_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  5316. }
  5317. if (changes & BSS_CHANGED_ERP_CTS_PROT) {
  5318. IWL_DEBUG_MAC80211("ERP_CTS %d\n", bss_conf->use_cts_prot);
  5319. if (bss_conf->use_cts_prot && (priv->band != IEEE80211_BAND_5GHZ))
  5320. priv->staging39_rxon.flags |= RXON_FLG_TGG_PROTECT_MSK;
  5321. else
  5322. priv->staging39_rxon.flags &= ~RXON_FLG_TGG_PROTECT_MSK;
  5323. }
  5324. if (changes & BSS_CHANGED_ASSOC) {
  5325. IWL_DEBUG_MAC80211("ASSOC %d\n", bss_conf->assoc);
  5326. /* This should never happen as this function should
  5327. * never be called from interrupt context. */
  5328. if (WARN_ON_ONCE(in_interrupt()))
  5329. return;
  5330. if (bss_conf->assoc) {
  5331. priv->assoc_id = bss_conf->aid;
  5332. priv->beacon_int = bss_conf->beacon_int;
  5333. priv->timestamp = bss_conf->timestamp;
  5334. priv->assoc_capability = bss_conf->assoc_capability;
  5335. priv->next_scan_jiffies = jiffies +
  5336. IWL_DELAY_NEXT_SCAN_AFTER_ASSOC;
  5337. mutex_lock(&priv->mutex);
  5338. iwl3945_post_associate(priv);
  5339. mutex_unlock(&priv->mutex);
  5340. } else {
  5341. priv->assoc_id = 0;
  5342. IWL_DEBUG_MAC80211("DISASSOC %d\n", bss_conf->assoc);
  5343. }
  5344. } else if (changes && iwl3945_is_associated(priv) && priv->assoc_id) {
  5345. IWL_DEBUG_MAC80211("Associated Changes %d\n", changes);
  5346. iwl3945_send_rxon_assoc(priv);
  5347. }
  5348. }
  5349. static int iwl3945_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
  5350. {
  5351. int rc = 0;
  5352. unsigned long flags;
  5353. struct iwl_priv *priv = hw->priv;
  5354. DECLARE_SSID_BUF(ssid_buf);
  5355. IWL_DEBUG_MAC80211("enter\n");
  5356. mutex_lock(&priv->mutex);
  5357. spin_lock_irqsave(&priv->lock, flags);
  5358. if (!iwl_is_ready_rf(priv)) {
  5359. rc = -EIO;
  5360. IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
  5361. goto out_unlock;
  5362. }
  5363. /* we don't schedule scan within next_scan_jiffies period */
  5364. if (priv->next_scan_jiffies &&
  5365. time_after(priv->next_scan_jiffies, jiffies)) {
  5366. rc = -EAGAIN;
  5367. goto out_unlock;
  5368. }
  5369. /* if we just finished scan ask for delay for a broadcast scan */
  5370. if ((len == 0) && priv->last_scan_jiffies &&
  5371. time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
  5372. jiffies)) {
  5373. rc = -EAGAIN;
  5374. goto out_unlock;
  5375. }
  5376. if (len) {
  5377. IWL_DEBUG_SCAN("direct scan for %s [%d]\n ",
  5378. print_ssid(ssid_buf, ssid, len), (int)len);
  5379. priv->one_direct_scan = 1;
  5380. priv->direct_ssid_len = (u8)
  5381. min((u8) len, (u8) IW_ESSID_MAX_SIZE);
  5382. memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
  5383. } else
  5384. priv->one_direct_scan = 0;
  5385. rc = iwl3945_scan_initiate(priv);
  5386. IWL_DEBUG_MAC80211("leave\n");
  5387. out_unlock:
  5388. spin_unlock_irqrestore(&priv->lock, flags);
  5389. mutex_unlock(&priv->mutex);
  5390. return rc;
  5391. }
  5392. static int iwl3945_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  5393. struct ieee80211_vif *vif,
  5394. struct ieee80211_sta *sta,
  5395. struct ieee80211_key_conf *key)
  5396. {
  5397. struct iwl_priv *priv = hw->priv;
  5398. const u8 *addr;
  5399. int rc = 0;
  5400. u8 sta_id;
  5401. static const u8 bcast_addr[ETH_ALEN] =
  5402. { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  5403. IWL_DEBUG_MAC80211("enter\n");
  5404. if (iwl3945_mod_params.sw_crypto) {
  5405. IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
  5406. return -EOPNOTSUPP;
  5407. }
  5408. addr = sta ? sta->addr : bcast_addr;
  5409. sta_id = iwl3945_hw_find_station(priv, addr);
  5410. if (sta_id == IWL_INVALID_STATION) {
  5411. IWL_DEBUG_MAC80211("leave - %pM not in station map.\n",
  5412. addr);
  5413. return -EINVAL;
  5414. }
  5415. mutex_lock(&priv->mutex);
  5416. iwl3945_scan_cancel_timeout(priv, 100);
  5417. switch (cmd) {
  5418. case SET_KEY:
  5419. rc = iwl3945_update_sta_key_info(priv, key, sta_id);
  5420. if (!rc) {
  5421. iwl3945_set_rxon_hwcrypto(priv, 1);
  5422. iwl3945_commit_rxon(priv);
  5423. key->hw_key_idx = sta_id;
  5424. IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
  5425. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  5426. }
  5427. break;
  5428. case DISABLE_KEY:
  5429. rc = iwl3945_clear_sta_key_info(priv, sta_id);
  5430. if (!rc) {
  5431. iwl3945_set_rxon_hwcrypto(priv, 0);
  5432. iwl3945_commit_rxon(priv);
  5433. IWL_DEBUG_MAC80211("disable hwcrypto key\n");
  5434. }
  5435. break;
  5436. default:
  5437. rc = -EINVAL;
  5438. }
  5439. IWL_DEBUG_MAC80211("leave\n");
  5440. mutex_unlock(&priv->mutex);
  5441. return rc;
  5442. }
  5443. static int iwl3945_mac_conf_tx(struct ieee80211_hw *hw, u16 queue,
  5444. const struct ieee80211_tx_queue_params *params)
  5445. {
  5446. struct iwl_priv *priv = hw->priv;
  5447. unsigned long flags;
  5448. int q;
  5449. IWL_DEBUG_MAC80211("enter\n");
  5450. if (!iwl_is_ready_rf(priv)) {
  5451. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  5452. return -EIO;
  5453. }
  5454. if (queue >= AC_NUM) {
  5455. IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
  5456. return 0;
  5457. }
  5458. q = AC_NUM - 1 - queue;
  5459. spin_lock_irqsave(&priv->lock, flags);
  5460. priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
  5461. priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
  5462. priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  5463. priv->qos_data.def_qos_parm.ac[q].edca_txop =
  5464. cpu_to_le16((params->txop * 32));
  5465. priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  5466. priv->qos_data.qos_active = 1;
  5467. spin_unlock_irqrestore(&priv->lock, flags);
  5468. mutex_lock(&priv->mutex);
  5469. if (priv->iw_mode == NL80211_IFTYPE_AP)
  5470. iwl3945_activate_qos(priv, 1);
  5471. else if (priv->assoc_id && iwl3945_is_associated(priv))
  5472. iwl3945_activate_qos(priv, 0);
  5473. mutex_unlock(&priv->mutex);
  5474. IWL_DEBUG_MAC80211("leave\n");
  5475. return 0;
  5476. }
  5477. static int iwl3945_mac_get_tx_stats(struct ieee80211_hw *hw,
  5478. struct ieee80211_tx_queue_stats *stats)
  5479. {
  5480. struct iwl_priv *priv = hw->priv;
  5481. int i, avail;
  5482. struct iwl_tx_queue *txq;
  5483. struct iwl_queue *q;
  5484. unsigned long flags;
  5485. IWL_DEBUG_MAC80211("enter\n");
  5486. if (!iwl_is_ready_rf(priv)) {
  5487. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  5488. return -EIO;
  5489. }
  5490. spin_lock_irqsave(&priv->lock, flags);
  5491. for (i = 0; i < AC_NUM; i++) {
  5492. txq = &priv->txq[i];
  5493. q = &txq->q;
  5494. avail = iwl_queue_space(q);
  5495. stats[i].len = q->n_window - avail;
  5496. stats[i].limit = q->n_window - q->high_mark;
  5497. stats[i].count = q->n_window;
  5498. }
  5499. spin_unlock_irqrestore(&priv->lock, flags);
  5500. IWL_DEBUG_MAC80211("leave\n");
  5501. return 0;
  5502. }
  5503. static void iwl3945_mac_reset_tsf(struct ieee80211_hw *hw)
  5504. {
  5505. struct iwl_priv *priv = hw->priv;
  5506. unsigned long flags;
  5507. mutex_lock(&priv->mutex);
  5508. IWL_DEBUG_MAC80211("enter\n");
  5509. iwl_reset_qos(priv);
  5510. spin_lock_irqsave(&priv->lock, flags);
  5511. priv->assoc_id = 0;
  5512. priv->assoc_capability = 0;
  5513. priv->call_post_assoc_from_beacon = 0;
  5514. /* new association get rid of ibss beacon skb */
  5515. if (priv->ibss_beacon)
  5516. dev_kfree_skb(priv->ibss_beacon);
  5517. priv->ibss_beacon = NULL;
  5518. priv->beacon_int = priv->hw->conf.beacon_int;
  5519. priv->timestamp = 0;
  5520. if ((priv->iw_mode == NL80211_IFTYPE_STATION))
  5521. priv->beacon_int = 0;
  5522. spin_unlock_irqrestore(&priv->lock, flags);
  5523. if (!iwl_is_ready_rf(priv)) {
  5524. IWL_DEBUG_MAC80211("leave - not ready\n");
  5525. mutex_unlock(&priv->mutex);
  5526. return;
  5527. }
  5528. /* we are restarting association process
  5529. * clear RXON_FILTER_ASSOC_MSK bit
  5530. */
  5531. if (priv->iw_mode != NL80211_IFTYPE_AP) {
  5532. iwl3945_scan_cancel_timeout(priv, 100);
  5533. priv->staging39_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5534. iwl3945_commit_rxon(priv);
  5535. }
  5536. /* Per mac80211.h: This is only used in IBSS mode... */
  5537. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  5538. IWL_DEBUG_MAC80211("leave - not in IBSS\n");
  5539. mutex_unlock(&priv->mutex);
  5540. return;
  5541. }
  5542. iwl3945_set_rate(priv);
  5543. mutex_unlock(&priv->mutex);
  5544. IWL_DEBUG_MAC80211("leave\n");
  5545. }
  5546. static int iwl3945_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb)
  5547. {
  5548. struct iwl_priv *priv = hw->priv;
  5549. unsigned long flags;
  5550. IWL_DEBUG_MAC80211("enter\n");
  5551. if (!iwl_is_ready_rf(priv)) {
  5552. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  5553. return -EIO;
  5554. }
  5555. if (priv->iw_mode != NL80211_IFTYPE_ADHOC) {
  5556. IWL_DEBUG_MAC80211("leave - not IBSS\n");
  5557. return -EIO;
  5558. }
  5559. spin_lock_irqsave(&priv->lock, flags);
  5560. if (priv->ibss_beacon)
  5561. dev_kfree_skb(priv->ibss_beacon);
  5562. priv->ibss_beacon = skb;
  5563. priv->assoc_id = 0;
  5564. IWL_DEBUG_MAC80211("leave\n");
  5565. spin_unlock_irqrestore(&priv->lock, flags);
  5566. iwl_reset_qos(priv);
  5567. iwl3945_post_associate(priv);
  5568. return 0;
  5569. }
  5570. /*****************************************************************************
  5571. *
  5572. * sysfs attributes
  5573. *
  5574. *****************************************************************************/
  5575. #ifdef CONFIG_IWL3945_DEBUG
  5576. /*
  5577. * The following adds a new attribute to the sysfs representation
  5578. * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
  5579. * used for controlling the debug level.
  5580. *
  5581. * See the level definitions in iwl for details.
  5582. */
  5583. static ssize_t show_debug_level(struct device *d,
  5584. struct device_attribute *attr, char *buf)
  5585. {
  5586. struct iwl_priv *priv = d->driver_data;
  5587. return sprintf(buf, "0x%08X\n", priv->debug_level);
  5588. }
  5589. static ssize_t store_debug_level(struct device *d,
  5590. struct device_attribute *attr,
  5591. const char *buf, size_t count)
  5592. {
  5593. struct iwl_priv *priv = d->driver_data;
  5594. unsigned long val;
  5595. int ret;
  5596. ret = strict_strtoul(buf, 0, &val);
  5597. if (ret)
  5598. IWL_INFO(priv, "%s is not in hex or decimal form.\n", buf);
  5599. else
  5600. priv->debug_level = val;
  5601. return strnlen(buf, count);
  5602. }
  5603. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO,
  5604. show_debug_level, store_debug_level);
  5605. #endif /* CONFIG_IWL3945_DEBUG */
  5606. static ssize_t show_temperature(struct device *d,
  5607. struct device_attribute *attr, char *buf)
  5608. {
  5609. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5610. if (!iwl_is_alive(priv))
  5611. return -EAGAIN;
  5612. return sprintf(buf, "%d\n", iwl3945_hw_get_temperature(priv));
  5613. }
  5614. static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
  5615. static ssize_t show_tx_power(struct device *d,
  5616. struct device_attribute *attr, char *buf)
  5617. {
  5618. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5619. return sprintf(buf, "%d\n", priv->user_txpower_limit);
  5620. }
  5621. static ssize_t store_tx_power(struct device *d,
  5622. struct device_attribute *attr,
  5623. const char *buf, size_t count)
  5624. {
  5625. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5626. char *p = (char *)buf;
  5627. u32 val;
  5628. val = simple_strtoul(p, &p, 10);
  5629. if (p == buf)
  5630. IWL_INFO(priv, ": %s is not in decimal form.\n", buf);
  5631. else
  5632. iwl3945_hw_reg_set_txpower(priv, val);
  5633. return count;
  5634. }
  5635. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
  5636. static ssize_t show_flags(struct device *d,
  5637. struct device_attribute *attr, char *buf)
  5638. {
  5639. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5640. return sprintf(buf, "0x%04X\n", priv->active39_rxon.flags);
  5641. }
  5642. static ssize_t store_flags(struct device *d,
  5643. struct device_attribute *attr,
  5644. const char *buf, size_t count)
  5645. {
  5646. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5647. u32 flags = simple_strtoul(buf, NULL, 0);
  5648. mutex_lock(&priv->mutex);
  5649. if (le32_to_cpu(priv->staging39_rxon.flags) != flags) {
  5650. /* Cancel any currently running scans... */
  5651. if (iwl3945_scan_cancel_timeout(priv, 100))
  5652. IWL_WARN(priv, "Could not cancel scan.\n");
  5653. else {
  5654. IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
  5655. flags);
  5656. priv->staging39_rxon.flags = cpu_to_le32(flags);
  5657. iwl3945_commit_rxon(priv);
  5658. }
  5659. }
  5660. mutex_unlock(&priv->mutex);
  5661. return count;
  5662. }
  5663. static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
  5664. static ssize_t show_filter_flags(struct device *d,
  5665. struct device_attribute *attr, char *buf)
  5666. {
  5667. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5668. return sprintf(buf, "0x%04X\n",
  5669. le32_to_cpu(priv->active39_rxon.filter_flags));
  5670. }
  5671. static ssize_t store_filter_flags(struct device *d,
  5672. struct device_attribute *attr,
  5673. const char *buf, size_t count)
  5674. {
  5675. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5676. u32 filter_flags = simple_strtoul(buf, NULL, 0);
  5677. mutex_lock(&priv->mutex);
  5678. if (le32_to_cpu(priv->staging39_rxon.filter_flags) != filter_flags) {
  5679. /* Cancel any currently running scans... */
  5680. if (iwl3945_scan_cancel_timeout(priv, 100))
  5681. IWL_WARN(priv, "Could not cancel scan.\n");
  5682. else {
  5683. IWL_DEBUG_INFO("Committing rxon.filter_flags = "
  5684. "0x%04X\n", filter_flags);
  5685. priv->staging39_rxon.filter_flags =
  5686. cpu_to_le32(filter_flags);
  5687. iwl3945_commit_rxon(priv);
  5688. }
  5689. }
  5690. mutex_unlock(&priv->mutex);
  5691. return count;
  5692. }
  5693. static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
  5694. store_filter_flags);
  5695. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  5696. static ssize_t show_measurement(struct device *d,
  5697. struct device_attribute *attr, char *buf)
  5698. {
  5699. struct iwl_priv *priv = dev_get_drvdata(d);
  5700. struct iwl_spectrum_notification measure_report;
  5701. u32 size = sizeof(measure_report), len = 0, ofs = 0;
  5702. u8 *data = (u8 *)&measure_report;
  5703. unsigned long flags;
  5704. spin_lock_irqsave(&priv->lock, flags);
  5705. if (!(priv->measurement_status & MEASUREMENT_READY)) {
  5706. spin_unlock_irqrestore(&priv->lock, flags);
  5707. return 0;
  5708. }
  5709. memcpy(&measure_report, &priv->measure_report, size);
  5710. priv->measurement_status = 0;
  5711. spin_unlock_irqrestore(&priv->lock, flags);
  5712. while (size && (PAGE_SIZE - len)) {
  5713. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  5714. PAGE_SIZE - len, 1);
  5715. len = strlen(buf);
  5716. if (PAGE_SIZE - len)
  5717. buf[len++] = '\n';
  5718. ofs += 16;
  5719. size -= min(size, 16U);
  5720. }
  5721. return len;
  5722. }
  5723. static ssize_t store_measurement(struct device *d,
  5724. struct device_attribute *attr,
  5725. const char *buf, size_t count)
  5726. {
  5727. struct iwl_priv *priv = dev_get_drvdata(d);
  5728. struct ieee80211_measurement_params params = {
  5729. .channel = le16_to_cpu(priv->active39_rxon.channel),
  5730. .start_time = cpu_to_le64(priv->last_tsf),
  5731. .duration = cpu_to_le16(1),
  5732. };
  5733. u8 type = IWL_MEASURE_BASIC;
  5734. u8 buffer[32];
  5735. u8 channel;
  5736. if (count) {
  5737. char *p = buffer;
  5738. strncpy(buffer, buf, min(sizeof(buffer), count));
  5739. channel = simple_strtoul(p, NULL, 0);
  5740. if (channel)
  5741. params.channel = channel;
  5742. p = buffer;
  5743. while (*p && *p != ' ')
  5744. p++;
  5745. if (*p)
  5746. type = simple_strtoul(p + 1, NULL, 0);
  5747. }
  5748. IWL_DEBUG_INFO("Invoking measurement of type %d on "
  5749. "channel %d (for '%s')\n", type, params.channel, buf);
  5750. iwl3945_get_measurement(priv, &params, type);
  5751. return count;
  5752. }
  5753. static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
  5754. show_measurement, store_measurement);
  5755. #endif /* CONFIG_IWL3945_SPECTRUM_MEASUREMENT */
  5756. static ssize_t store_retry_rate(struct device *d,
  5757. struct device_attribute *attr,
  5758. const char *buf, size_t count)
  5759. {
  5760. struct iwl_priv *priv = dev_get_drvdata(d);
  5761. priv->retry_rate = simple_strtoul(buf, NULL, 0);
  5762. if (priv->retry_rate <= 0)
  5763. priv->retry_rate = 1;
  5764. return count;
  5765. }
  5766. static ssize_t show_retry_rate(struct device *d,
  5767. struct device_attribute *attr, char *buf)
  5768. {
  5769. struct iwl_priv *priv = dev_get_drvdata(d);
  5770. return sprintf(buf, "%d", priv->retry_rate);
  5771. }
  5772. static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
  5773. store_retry_rate);
  5774. static ssize_t store_power_level(struct device *d,
  5775. struct device_attribute *attr,
  5776. const char *buf, size_t count)
  5777. {
  5778. struct iwl_priv *priv = dev_get_drvdata(d);
  5779. int rc;
  5780. int mode;
  5781. mode = simple_strtoul(buf, NULL, 0);
  5782. mutex_lock(&priv->mutex);
  5783. if (!iwl_is_ready(priv)) {
  5784. rc = -EAGAIN;
  5785. goto out;
  5786. }
  5787. if ((mode < 1) || (mode > IWL39_POWER_LIMIT) ||
  5788. (mode == IWL39_POWER_AC))
  5789. mode = IWL39_POWER_AC;
  5790. else
  5791. mode |= IWL_POWER_ENABLED;
  5792. if (mode != priv->power_mode) {
  5793. rc = iwl3945_send_power_mode(priv, IWL_POWER_LEVEL(mode));
  5794. if (rc) {
  5795. IWL_DEBUG_MAC80211("failed setting power mode.\n");
  5796. goto out;
  5797. }
  5798. priv->power_mode = mode;
  5799. }
  5800. rc = count;
  5801. out:
  5802. mutex_unlock(&priv->mutex);
  5803. return rc;
  5804. }
  5805. #define MAX_WX_STRING 80
  5806. /* Values are in microsecond */
  5807. static const s32 timeout_duration[] = {
  5808. 350000,
  5809. 250000,
  5810. 75000,
  5811. 37000,
  5812. 25000,
  5813. };
  5814. static const s32 period_duration[] = {
  5815. 400000,
  5816. 700000,
  5817. 1000000,
  5818. 1000000,
  5819. 1000000
  5820. };
  5821. static ssize_t show_power_level(struct device *d,
  5822. struct device_attribute *attr, char *buf)
  5823. {
  5824. struct iwl_priv *priv = dev_get_drvdata(d);
  5825. int level = IWL_POWER_LEVEL(priv->power_mode);
  5826. char *p = buf;
  5827. p += sprintf(p, "%d ", level);
  5828. switch (level) {
  5829. case IWL_POWER_MODE_CAM:
  5830. case IWL39_POWER_AC:
  5831. p += sprintf(p, "(AC)");
  5832. break;
  5833. case IWL39_POWER_BATTERY:
  5834. p += sprintf(p, "(BATTERY)");
  5835. break;
  5836. default:
  5837. p += sprintf(p,
  5838. "(Timeout %dms, Period %dms)",
  5839. timeout_duration[level - 1] / 1000,
  5840. period_duration[level - 1] / 1000);
  5841. }
  5842. if (!(priv->power_mode & IWL_POWER_ENABLED))
  5843. p += sprintf(p, " OFF\n");
  5844. else
  5845. p += sprintf(p, " \n");
  5846. return p - buf + 1;
  5847. }
  5848. static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
  5849. store_power_level);
  5850. static ssize_t show_channels(struct device *d,
  5851. struct device_attribute *attr, char *buf)
  5852. {
  5853. /* all this shit doesn't belong into sysfs anyway */
  5854. return 0;
  5855. }
  5856. static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
  5857. static ssize_t show_statistics(struct device *d,
  5858. struct device_attribute *attr, char *buf)
  5859. {
  5860. struct iwl_priv *priv = dev_get_drvdata(d);
  5861. u32 size = sizeof(struct iwl3945_notif_statistics);
  5862. u32 len = 0, ofs = 0;
  5863. u8 *data = (u8 *)&priv->statistics_39;
  5864. int rc = 0;
  5865. if (!iwl_is_alive(priv))
  5866. return -EAGAIN;
  5867. mutex_lock(&priv->mutex);
  5868. rc = iwl3945_send_statistics_request(priv);
  5869. mutex_unlock(&priv->mutex);
  5870. if (rc) {
  5871. len = sprintf(buf,
  5872. "Error sending statistics request: 0x%08X\n", rc);
  5873. return len;
  5874. }
  5875. while (size && (PAGE_SIZE - len)) {
  5876. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  5877. PAGE_SIZE - len, 1);
  5878. len = strlen(buf);
  5879. if (PAGE_SIZE - len)
  5880. buf[len++] = '\n';
  5881. ofs += 16;
  5882. size -= min(size, 16U);
  5883. }
  5884. return len;
  5885. }
  5886. static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
  5887. static ssize_t show_antenna(struct device *d,
  5888. struct device_attribute *attr, char *buf)
  5889. {
  5890. struct iwl_priv *priv = dev_get_drvdata(d);
  5891. if (!iwl_is_alive(priv))
  5892. return -EAGAIN;
  5893. return sprintf(buf, "%d\n", priv->antenna);
  5894. }
  5895. static ssize_t store_antenna(struct device *d,
  5896. struct device_attribute *attr,
  5897. const char *buf, size_t count)
  5898. {
  5899. int ant;
  5900. struct iwl_priv *priv = dev_get_drvdata(d);
  5901. if (count == 0)
  5902. return 0;
  5903. if (sscanf(buf, "%1i", &ant) != 1) {
  5904. IWL_DEBUG_INFO("not in hex or decimal form.\n");
  5905. return count;
  5906. }
  5907. if ((ant >= 0) && (ant <= 2)) {
  5908. IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
  5909. priv->antenna = (enum iwl3945_antenna)ant;
  5910. } else
  5911. IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
  5912. return count;
  5913. }
  5914. static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
  5915. static ssize_t show_status(struct device *d,
  5916. struct device_attribute *attr, char *buf)
  5917. {
  5918. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  5919. if (!iwl_is_alive(priv))
  5920. return -EAGAIN;
  5921. return sprintf(buf, "0x%08x\n", (int)priv->status);
  5922. }
  5923. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  5924. static ssize_t dump_error_log(struct device *d,
  5925. struct device_attribute *attr,
  5926. const char *buf, size_t count)
  5927. {
  5928. char *p = (char *)buf;
  5929. if (p[0] == '1')
  5930. iwl3945_dump_nic_error_log((struct iwl_priv *)d->driver_data);
  5931. return strnlen(buf, count);
  5932. }
  5933. static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
  5934. static ssize_t dump_event_log(struct device *d,
  5935. struct device_attribute *attr,
  5936. const char *buf, size_t count)
  5937. {
  5938. char *p = (char *)buf;
  5939. if (p[0] == '1')
  5940. iwl3945_dump_nic_event_log((struct iwl_priv *)d->driver_data);
  5941. return strnlen(buf, count);
  5942. }
  5943. static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
  5944. /*****************************************************************************
  5945. *
  5946. * driver setup and tear down
  5947. *
  5948. *****************************************************************************/
  5949. static void iwl3945_setup_deferred_work(struct iwl_priv *priv)
  5950. {
  5951. priv->workqueue = create_workqueue(DRV_NAME);
  5952. init_waitqueue_head(&priv->wait_command_queue);
  5953. INIT_WORK(&priv->up, iwl3945_bg_up);
  5954. INIT_WORK(&priv->restart, iwl3945_bg_restart);
  5955. INIT_WORK(&priv->rx_replenish, iwl3945_bg_rx_replenish);
  5956. INIT_WORK(&priv->scan_completed, iwl3945_bg_scan_completed);
  5957. INIT_WORK(&priv->request_scan, iwl3945_bg_request_scan);
  5958. INIT_WORK(&priv->abort_scan, iwl3945_bg_abort_scan);
  5959. INIT_WORK(&priv->rf_kill, iwl3945_bg_rf_kill);
  5960. INIT_WORK(&priv->beacon_update, iwl3945_bg_beacon_update);
  5961. INIT_DELAYED_WORK(&priv->init_alive_start, iwl3945_bg_init_alive_start);
  5962. INIT_DELAYED_WORK(&priv->alive_start, iwl3945_bg_alive_start);
  5963. INIT_DELAYED_WORK(&priv->scan_check, iwl3945_bg_scan_check);
  5964. iwl3945_hw_setup_deferred_work(priv);
  5965. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  5966. iwl3945_irq_tasklet, (unsigned long)priv);
  5967. }
  5968. static void iwl3945_cancel_deferred_work(struct iwl_priv *priv)
  5969. {
  5970. iwl3945_hw_cancel_deferred_work(priv);
  5971. cancel_delayed_work_sync(&priv->init_alive_start);
  5972. cancel_delayed_work(&priv->scan_check);
  5973. cancel_delayed_work(&priv->alive_start);
  5974. cancel_work_sync(&priv->beacon_update);
  5975. }
  5976. static struct attribute *iwl3945_sysfs_entries[] = {
  5977. &dev_attr_antenna.attr,
  5978. &dev_attr_channels.attr,
  5979. &dev_attr_dump_errors.attr,
  5980. &dev_attr_dump_events.attr,
  5981. &dev_attr_flags.attr,
  5982. &dev_attr_filter_flags.attr,
  5983. #ifdef CONFIG_IWL3945_SPECTRUM_MEASUREMENT
  5984. &dev_attr_measurement.attr,
  5985. #endif
  5986. &dev_attr_power_level.attr,
  5987. &dev_attr_retry_rate.attr,
  5988. &dev_attr_statistics.attr,
  5989. &dev_attr_status.attr,
  5990. &dev_attr_temperature.attr,
  5991. &dev_attr_tx_power.attr,
  5992. #ifdef CONFIG_IWL3945_DEBUG
  5993. &dev_attr_debug_level.attr,
  5994. #endif
  5995. NULL
  5996. };
  5997. static struct attribute_group iwl3945_attribute_group = {
  5998. .name = NULL, /* put in device directory */
  5999. .attrs = iwl3945_sysfs_entries,
  6000. };
  6001. static struct ieee80211_ops iwl3945_hw_ops = {
  6002. .tx = iwl3945_mac_tx,
  6003. .start = iwl3945_mac_start,
  6004. .stop = iwl3945_mac_stop,
  6005. .add_interface = iwl3945_mac_add_interface,
  6006. .remove_interface = iwl3945_mac_remove_interface,
  6007. .config = iwl3945_mac_config,
  6008. .config_interface = iwl3945_mac_config_interface,
  6009. .configure_filter = iwl3945_configure_filter,
  6010. .set_key = iwl3945_mac_set_key,
  6011. .get_tx_stats = iwl3945_mac_get_tx_stats,
  6012. .conf_tx = iwl3945_mac_conf_tx,
  6013. .reset_tsf = iwl3945_mac_reset_tsf,
  6014. .bss_info_changed = iwl3945_bss_info_changed,
  6015. .hw_scan = iwl3945_mac_hw_scan
  6016. };
  6017. static int iwl3945_init_drv(struct iwl_priv *priv)
  6018. {
  6019. int ret;
  6020. priv->retry_rate = 1;
  6021. priv->ibss_beacon = NULL;
  6022. spin_lock_init(&priv->lock);
  6023. spin_lock_init(&priv->power_data.lock);
  6024. spin_lock_init(&priv->sta_lock);
  6025. spin_lock_init(&priv->hcmd_lock);
  6026. INIT_LIST_HEAD(&priv->free_frames);
  6027. mutex_init(&priv->mutex);
  6028. /* Clear the driver's (not device's) station table */
  6029. iwl3945_clear_stations_table(priv);
  6030. priv->data_retry_limit = -1;
  6031. priv->ieee_channels = NULL;
  6032. priv->ieee_rates = NULL;
  6033. priv->band = IEEE80211_BAND_2GHZ;
  6034. priv->iw_mode = NL80211_IFTYPE_STATION;
  6035. iwl_reset_qos(priv);
  6036. priv->qos_data.qos_active = 0;
  6037. priv->qos_data.qos_cap.val = 0;
  6038. priv->rates_mask = IWL_RATES_MASK;
  6039. /* If power management is turned on, default to AC mode */
  6040. priv->power_mode = IWL_POWER_AC;
  6041. priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
  6042. ret = iwl3945_init_channel_map(priv);
  6043. if (ret) {
  6044. IWL_ERR(priv, "initializing regulatory failed: %d\n", ret);
  6045. goto err;
  6046. }
  6047. ret = iwl3945_init_geos(priv);
  6048. if (ret) {
  6049. IWL_ERR(priv, "initializing geos failed: %d\n", ret);
  6050. goto err_free_channel_map;
  6051. }
  6052. return 0;
  6053. err_free_channel_map:
  6054. iwl3945_free_channel_map(priv);
  6055. err:
  6056. return ret;
  6057. }
  6058. static int iwl3945_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  6059. {
  6060. int err = 0;
  6061. struct iwl_priv *priv;
  6062. struct ieee80211_hw *hw;
  6063. struct iwl_cfg *cfg = (struct iwl_cfg *)(ent->driver_data);
  6064. unsigned long flags;
  6065. /***********************
  6066. * 1. Allocating HW data
  6067. * ********************/
  6068. /* mac80211 allocates memory for this device instance, including
  6069. * space for this driver's private structure */
  6070. hw = iwl_alloc_all(cfg, &iwl3945_hw_ops);
  6071. if (hw == NULL) {
  6072. printk(KERN_ERR DRV_NAME "Can not allocate network device\n");
  6073. err = -ENOMEM;
  6074. goto out;
  6075. }
  6076. priv = hw->priv;
  6077. SET_IEEE80211_DEV(hw, &pdev->dev);
  6078. if ((iwl3945_mod_params.num_of_queues > IWL39_MAX_NUM_QUEUES) ||
  6079. (iwl3945_mod_params.num_of_queues < IWL_MIN_NUM_QUEUES)) {
  6080. IWL_ERR(priv,
  6081. "invalid queues_num, should be between %d and %d\n",
  6082. IWL_MIN_NUM_QUEUES, IWL39_MAX_NUM_QUEUES);
  6083. err = -EINVAL;
  6084. goto out;
  6085. }
  6086. /*
  6087. * Disabling hardware scan means that mac80211 will perform scans
  6088. * "the hard way", rather than using device's scan.
  6089. */
  6090. if (iwl3945_mod_params.disable_hw_scan) {
  6091. IWL_DEBUG_INFO("Disabling hw_scan\n");
  6092. iwl3945_hw_ops.hw_scan = NULL;
  6093. }
  6094. IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
  6095. priv->cfg = cfg;
  6096. priv->pci_dev = pdev;
  6097. #ifdef CONFIG_IWL3945_DEBUG
  6098. priv->debug_level = iwl3945_mod_params.debug;
  6099. atomic_set(&priv->restrict_refcnt, 0);
  6100. #endif
  6101. hw->rate_control_algorithm = "iwl-3945-rs";
  6102. hw->sta_data_size = sizeof(struct iwl3945_sta_priv);
  6103. /* Select antenna (may be helpful if only one antenna is connected) */
  6104. priv->antenna = (enum iwl3945_antenna)iwl3945_mod_params.antenna;
  6105. /* Tell mac80211 our characteristics */
  6106. hw->flags = IEEE80211_HW_SIGNAL_DBM |
  6107. IEEE80211_HW_NOISE_DBM;
  6108. hw->wiphy->interface_modes =
  6109. BIT(NL80211_IFTYPE_STATION) |
  6110. BIT(NL80211_IFTYPE_ADHOC);
  6111. hw->wiphy->fw_handles_regulatory = true;
  6112. /* 4 EDCA QOS priorities */
  6113. hw->queues = 4;
  6114. /***************************
  6115. * 2. Initializing PCI bus
  6116. * *************************/
  6117. if (pci_enable_device(pdev)) {
  6118. err = -ENODEV;
  6119. goto out_ieee80211_free_hw;
  6120. }
  6121. pci_set_master(pdev);
  6122. err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  6123. if (!err)
  6124. err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  6125. if (err) {
  6126. IWL_WARN(priv, "No suitable DMA available.\n");
  6127. goto out_pci_disable_device;
  6128. }
  6129. pci_set_drvdata(pdev, priv);
  6130. err = pci_request_regions(pdev, DRV_NAME);
  6131. if (err)
  6132. goto out_pci_disable_device;
  6133. /***********************
  6134. * 3. Read REV Register
  6135. * ********************/
  6136. priv->hw_base = pci_iomap(pdev, 0, 0);
  6137. if (!priv->hw_base) {
  6138. err = -ENODEV;
  6139. goto out_pci_release_regions;
  6140. }
  6141. IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
  6142. (unsigned long long) pci_resource_len(pdev, 0));
  6143. IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
  6144. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  6145. * PCI Tx retries from interfering with C3 CPU state */
  6146. pci_write_config_byte(pdev, 0x41, 0x00);
  6147. /* amp init */
  6148. err = priv->cfg->ops->lib->apm_ops.init(priv);
  6149. if (err < 0) {
  6150. IWL_DEBUG_INFO("Failed to init APMG\n");
  6151. goto out_iounmap;
  6152. }
  6153. /***********************
  6154. * 4. Read EEPROM
  6155. * ********************/
  6156. /* Read the EEPROM */
  6157. err = iwl3945_eeprom_init(priv);
  6158. if (err) {
  6159. IWL_ERR(priv, "Unable to init EEPROM\n");
  6160. goto out_remove_sysfs;
  6161. }
  6162. /* MAC Address location in EEPROM same for 3945/4965 */
  6163. get_eeprom_mac(priv, priv->mac_addr);
  6164. IWL_DEBUG_INFO("MAC address: %pM\n", priv->mac_addr);
  6165. SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
  6166. /***********************
  6167. * 5. Setup HW Constants
  6168. * ********************/
  6169. /* Device-specific setup */
  6170. if (iwl3945_hw_set_hw_params(priv)) {
  6171. IWL_ERR(priv, "failed to set hw settings\n");
  6172. goto out_iounmap;
  6173. }
  6174. /***********************
  6175. * 6. Setup priv
  6176. * ********************/
  6177. err = iwl3945_init_drv(priv);
  6178. if (err) {
  6179. IWL_ERR(priv, "initializing driver failed\n");
  6180. goto out_free_geos;
  6181. }
  6182. IWL_INFO(priv, "Detected Intel Wireless WiFi Link %s\n",
  6183. priv->cfg->name);
  6184. /***********************************
  6185. * 7. Initialize Module Parameters
  6186. * **********************************/
  6187. /* Initialize module parameter values here */
  6188. /* Disable radio (SW RF KILL) via parameter when loading driver */
  6189. if (iwl3945_mod_params.disable) {
  6190. set_bit(STATUS_RF_KILL_SW, &priv->status);
  6191. IWL_DEBUG_INFO("Radio disabled.\n");
  6192. }
  6193. /***********************
  6194. * 8. Setup Services
  6195. * ********************/
  6196. spin_lock_irqsave(&priv->lock, flags);
  6197. iwl3945_disable_interrupts(priv);
  6198. spin_unlock_irqrestore(&priv->lock, flags);
  6199. err = sysfs_create_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  6200. if (err) {
  6201. IWL_ERR(priv, "failed to create sysfs device attributes\n");
  6202. goto out_release_irq;
  6203. }
  6204. iwl3945_set_rxon_channel(priv, IEEE80211_BAND_2GHZ, 6);
  6205. iwl3945_setup_deferred_work(priv);
  6206. iwl3945_setup_rx_handlers(priv);
  6207. /***********************
  6208. * 9. Conclude
  6209. * ********************/
  6210. pci_save_state(pdev);
  6211. pci_disable_device(pdev);
  6212. /*********************************
  6213. * 10. Setup and Register mac80211
  6214. * *******************************/
  6215. err = ieee80211_register_hw(priv->hw);
  6216. if (err) {
  6217. IWL_ERR(priv, "Failed to register network device: %d\n", err);
  6218. goto out_remove_sysfs;
  6219. }
  6220. priv->hw->conf.beacon_int = 100;
  6221. priv->mac80211_registered = 1;
  6222. err = iwl3945_rfkill_init(priv);
  6223. if (err)
  6224. IWL_ERR(priv, "Unable to initialize RFKILL system. "
  6225. "Ignoring error: %d\n", err);
  6226. return 0;
  6227. out_remove_sysfs:
  6228. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  6229. out_free_geos:
  6230. iwl3945_free_geos(priv);
  6231. out_release_irq:
  6232. destroy_workqueue(priv->workqueue);
  6233. priv->workqueue = NULL;
  6234. iwl3945_unset_hw_params(priv);
  6235. out_iounmap:
  6236. pci_iounmap(pdev, priv->hw_base);
  6237. out_pci_release_regions:
  6238. pci_release_regions(pdev);
  6239. out_pci_disable_device:
  6240. pci_disable_device(pdev);
  6241. pci_set_drvdata(pdev, NULL);
  6242. out_ieee80211_free_hw:
  6243. ieee80211_free_hw(priv->hw);
  6244. out:
  6245. return err;
  6246. }
  6247. static void __devexit iwl3945_pci_remove(struct pci_dev *pdev)
  6248. {
  6249. struct iwl_priv *priv = pci_get_drvdata(pdev);
  6250. unsigned long flags;
  6251. if (!priv)
  6252. return;
  6253. IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
  6254. set_bit(STATUS_EXIT_PENDING, &priv->status);
  6255. if (priv->mac80211_registered) {
  6256. ieee80211_unregister_hw(priv->hw);
  6257. priv->mac80211_registered = 0;
  6258. } else {
  6259. iwl3945_down(priv);
  6260. }
  6261. /* make sure we flush any pending irq or
  6262. * tasklet for the driver
  6263. */
  6264. spin_lock_irqsave(&priv->lock, flags);
  6265. iwl3945_disable_interrupts(priv);
  6266. spin_unlock_irqrestore(&priv->lock, flags);
  6267. iwl_synchronize_irq(priv);
  6268. sysfs_remove_group(&pdev->dev.kobj, &iwl3945_attribute_group);
  6269. iwl3945_rfkill_unregister(priv);
  6270. iwl3945_dealloc_ucode_pci(priv);
  6271. if (priv->rxq.bd)
  6272. iwl_rx_queue_free(priv, &priv->rxq);
  6273. iwl3945_hw_txq_ctx_free(priv);
  6274. iwl3945_unset_hw_params(priv);
  6275. iwl3945_clear_stations_table(priv);
  6276. /*netif_stop_queue(dev); */
  6277. flush_workqueue(priv->workqueue);
  6278. /* ieee80211_unregister_hw calls iwl3945_mac_stop, which flushes
  6279. * priv->workqueue... so we can't take down the workqueue
  6280. * until now... */
  6281. destroy_workqueue(priv->workqueue);
  6282. priv->workqueue = NULL;
  6283. pci_iounmap(pdev, priv->hw_base);
  6284. pci_release_regions(pdev);
  6285. pci_disable_device(pdev);
  6286. pci_set_drvdata(pdev, NULL);
  6287. iwl3945_free_channel_map(priv);
  6288. iwl3945_free_geos(priv);
  6289. kfree(priv->scan39);
  6290. if (priv->ibss_beacon)
  6291. dev_kfree_skb(priv->ibss_beacon);
  6292. ieee80211_free_hw(priv->hw);
  6293. }
  6294. #ifdef CONFIG_PM
  6295. static int iwl3945_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  6296. {
  6297. struct iwl_priv *priv = pci_get_drvdata(pdev);
  6298. if (priv->is_open) {
  6299. set_bit(STATUS_IN_SUSPEND, &priv->status);
  6300. iwl3945_mac_stop(priv->hw);
  6301. priv->is_open = 1;
  6302. }
  6303. pci_set_power_state(pdev, PCI_D3hot);
  6304. return 0;
  6305. }
  6306. static int iwl3945_pci_resume(struct pci_dev *pdev)
  6307. {
  6308. struct iwl_priv *priv = pci_get_drvdata(pdev);
  6309. pci_set_power_state(pdev, PCI_D0);
  6310. if (priv->is_open)
  6311. iwl3945_mac_start(priv->hw);
  6312. clear_bit(STATUS_IN_SUSPEND, &priv->status);
  6313. return 0;
  6314. }
  6315. #endif /* CONFIG_PM */
  6316. /*************** RFKILL FUNCTIONS **********/
  6317. #ifdef CONFIG_IWL3945_RFKILL
  6318. /* software rf-kill from user */
  6319. static int iwl3945_rfkill_soft_rf_kill(void *data, enum rfkill_state state)
  6320. {
  6321. struct iwl_priv *priv = data;
  6322. int err = 0;
  6323. if (!priv->rfkill)
  6324. return 0;
  6325. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  6326. return 0;
  6327. IWL_DEBUG_RF_KILL("we received soft RFKILL set to state %d\n", state);
  6328. mutex_lock(&priv->mutex);
  6329. switch (state) {
  6330. case RFKILL_STATE_UNBLOCKED:
  6331. if (iwl_is_rfkill_hw(priv)) {
  6332. err = -EBUSY;
  6333. goto out_unlock;
  6334. }
  6335. iwl3945_radio_kill_sw(priv, 0);
  6336. break;
  6337. case RFKILL_STATE_SOFT_BLOCKED:
  6338. iwl3945_radio_kill_sw(priv, 1);
  6339. break;
  6340. default:
  6341. IWL_WARN(priv, "received unexpected RFKILL state %d\n", state);
  6342. break;
  6343. }
  6344. out_unlock:
  6345. mutex_unlock(&priv->mutex);
  6346. return err;
  6347. }
  6348. int iwl3945_rfkill_init(struct iwl_priv *priv)
  6349. {
  6350. struct device *device = wiphy_dev(priv->hw->wiphy);
  6351. int ret = 0;
  6352. BUG_ON(device == NULL);
  6353. IWL_DEBUG_RF_KILL("Initializing RFKILL.\n");
  6354. priv->rfkill = rfkill_allocate(device, RFKILL_TYPE_WLAN);
  6355. if (!priv->rfkill) {
  6356. IWL_ERR(priv, "Unable to allocate rfkill device.\n");
  6357. ret = -ENOMEM;
  6358. goto error;
  6359. }
  6360. priv->rfkill->name = priv->cfg->name;
  6361. priv->rfkill->data = priv;
  6362. priv->rfkill->state = RFKILL_STATE_UNBLOCKED;
  6363. priv->rfkill->toggle_radio = iwl3945_rfkill_soft_rf_kill;
  6364. priv->rfkill->user_claim_unsupported = 1;
  6365. priv->rfkill->dev.class->suspend = NULL;
  6366. priv->rfkill->dev.class->resume = NULL;
  6367. ret = rfkill_register(priv->rfkill);
  6368. if (ret) {
  6369. IWL_ERR(priv, "Unable to register rfkill: %d\n", ret);
  6370. goto freed_rfkill;
  6371. }
  6372. IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
  6373. return ret;
  6374. freed_rfkill:
  6375. if (priv->rfkill != NULL)
  6376. rfkill_free(priv->rfkill);
  6377. priv->rfkill = NULL;
  6378. error:
  6379. IWL_DEBUG_RF_KILL("RFKILL initialization complete.\n");
  6380. return ret;
  6381. }
  6382. void iwl3945_rfkill_unregister(struct iwl_priv *priv)
  6383. {
  6384. if (priv->rfkill)
  6385. rfkill_unregister(priv->rfkill);
  6386. priv->rfkill = NULL;
  6387. }
  6388. /* set rf-kill to the right state. */
  6389. void iwl3945_rfkill_set_hw_state(struct iwl_priv *priv)
  6390. {
  6391. if (!priv->rfkill)
  6392. return;
  6393. if (iwl_is_rfkill_hw(priv)) {
  6394. rfkill_force_state(priv->rfkill, RFKILL_STATE_HARD_BLOCKED);
  6395. return;
  6396. }
  6397. if (!iwl_is_rfkill_sw(priv))
  6398. rfkill_force_state(priv->rfkill, RFKILL_STATE_UNBLOCKED);
  6399. else
  6400. rfkill_force_state(priv->rfkill, RFKILL_STATE_SOFT_BLOCKED);
  6401. }
  6402. #endif
  6403. /*****************************************************************************
  6404. *
  6405. * driver and module entry point
  6406. *
  6407. *****************************************************************************/
  6408. static struct pci_driver iwl3945_driver = {
  6409. .name = DRV_NAME,
  6410. .id_table = iwl3945_hw_card_ids,
  6411. .probe = iwl3945_pci_probe,
  6412. .remove = __devexit_p(iwl3945_pci_remove),
  6413. #ifdef CONFIG_PM
  6414. .suspend = iwl3945_pci_suspend,
  6415. .resume = iwl3945_pci_resume,
  6416. #endif
  6417. };
  6418. static int __init iwl3945_init(void)
  6419. {
  6420. int ret;
  6421. printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
  6422. printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
  6423. ret = iwl3945_rate_control_register();
  6424. if (ret) {
  6425. printk(KERN_ERR DRV_NAME
  6426. "Unable to register rate control algorithm: %d\n", ret);
  6427. return ret;
  6428. }
  6429. ret = pci_register_driver(&iwl3945_driver);
  6430. if (ret) {
  6431. printk(KERN_ERR DRV_NAME "Unable to initialize PCI module\n");
  6432. goto error_register;
  6433. }
  6434. return ret;
  6435. error_register:
  6436. iwl3945_rate_control_unregister();
  6437. return ret;
  6438. }
  6439. static void __exit iwl3945_exit(void)
  6440. {
  6441. pci_unregister_driver(&iwl3945_driver);
  6442. iwl3945_rate_control_unregister();
  6443. }
  6444. MODULE_FIRMWARE(IWL3945_MODULE_FIRMWARE(IWL3945_UCODE_API_MAX));
  6445. module_param_named(antenna, iwl3945_mod_params.antenna, int, 0444);
  6446. MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
  6447. module_param_named(disable, iwl3945_mod_params.disable, int, 0444);
  6448. MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
  6449. module_param_named(hwcrypto, iwl3945_mod_params.sw_crypto, int, 0444);
  6450. MODULE_PARM_DESC(hwcrypto,
  6451. "using hardware crypto engine (default 0 [software])\n");
  6452. module_param_named(debug, iwl3945_mod_params.debug, uint, 0444);
  6453. MODULE_PARM_DESC(debug, "debug output mask");
  6454. module_param_named(disable_hw_scan, iwl3945_mod_params.disable_hw_scan, int, 0444);
  6455. MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
  6456. module_param_named(queues_num, iwl3945_mod_params.num_of_queues, int, 0444);
  6457. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  6458. module_exit(iwl3945_exit);
  6459. module_init(iwl3945_init);