iwl3945-base.c 216 KB

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