iwl3945-base.c 240 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2007 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. * James P. Ketrenos <ipw2100-admin@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. /*
  30. * NOTE: This file (iwl-base.c) is used to build to multiple hardware targets
  31. * by defining IWL to either 3945 or 4965. The Makefile used when building
  32. * the base targets will create base-3945.o and base-4965.o
  33. *
  34. * The eventual goal is to move as many of the #if IWL / #endif blocks out of
  35. * this file and into the hardware specific implementation files (iwl-XXXX.c)
  36. * and leave only the common (non #ifdef sprinkled) code in this file
  37. */
  38. #include <linux/kernel.h>
  39. #include <linux/module.h>
  40. #include <linux/version.h>
  41. #include <linux/init.h>
  42. #include <linux/pci.h>
  43. #include <linux/dma-mapping.h>
  44. #include <linux/delay.h>
  45. #include <linux/skbuff.h>
  46. #include <linux/netdevice.h>
  47. #include <linux/wireless.h>
  48. #include <linux/firmware.h>
  49. #include <linux/etherdevice.h>
  50. #include <linux/if_arp.h>
  51. #include <net/ieee80211_radiotap.h>
  52. #include <net/mac80211.h>
  53. #include <asm/div64.h>
  54. #define IWL 3945
  55. #include "iwlwifi.h"
  56. #include "iwl-3945.h"
  57. #include "iwl-helpers.h"
  58. #ifdef CONFIG_IWLWIFI_DEBUG
  59. u32 iwl_debug_level;
  60. #endif
  61. /******************************************************************************
  62. *
  63. * module boiler plate
  64. *
  65. ******************************************************************************/
  66. /* module parameters */
  67. int iwl_param_disable_hw_scan;
  68. int iwl_param_debug;
  69. int iwl_param_disable; /* def: enable radio */
  70. int iwl_param_antenna; /* def: 0 = both antennas (use diversity) */
  71. int iwl_param_hwcrypto; /* def: using software encryption */
  72. int iwl_param_qos_enable = 1;
  73. int iwl_param_queues_num = IWL_MAX_NUM_QUEUES;
  74. /*
  75. * module name, copyright, version, etc.
  76. * NOTE: DRV_NAME is defined in iwlwifi.h for use by iwl-debug.h and printk
  77. */
  78. #define DRV_DESCRIPTION \
  79. "Intel(R) PRO/Wireless 3945ABG/BG Network Connection driver for Linux"
  80. #ifdef CONFIG_IWLWIFI_DEBUG
  81. #define VD "d"
  82. #else
  83. #define VD
  84. #endif
  85. #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
  86. #define VS "s"
  87. #else
  88. #define VS
  89. #endif
  90. #define IWLWIFI_VERSION "1.1.17k" VD VS
  91. #define DRV_COPYRIGHT "Copyright(c) 2003-2007 Intel Corporation"
  92. #define DRV_VERSION IWLWIFI_VERSION
  93. /* Change firmware file name, using "-" and incrementing number,
  94. * *only* when uCode interface or architecture changes so that it
  95. * is not compatible with earlier drivers.
  96. * This number will also appear in << 8 position of 1st dword of uCode file */
  97. #define IWL3945_UCODE_API "-1"
  98. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  99. MODULE_VERSION(DRV_VERSION);
  100. MODULE_AUTHOR(DRV_COPYRIGHT);
  101. MODULE_LICENSE("GPL");
  102. __le16 *ieee80211_get_qos_ctrl(struct ieee80211_hdr *hdr)
  103. {
  104. u16 fc = le16_to_cpu(hdr->frame_control);
  105. int hdr_len = ieee80211_get_hdrlen(fc);
  106. if ((fc & 0x00cc) == (IEEE80211_STYPE_QOS_DATA | IEEE80211_FTYPE_DATA))
  107. return (__le16 *) ((u8 *) hdr + hdr_len - QOS_CONTROL_LEN);
  108. return NULL;
  109. }
  110. static const struct ieee80211_hw_mode *iwl_get_hw_mode(
  111. struct iwl_priv *priv, int mode)
  112. {
  113. int i;
  114. for (i = 0; i < 3; i++)
  115. if (priv->modes[i].mode == mode)
  116. return &priv->modes[i];
  117. return NULL;
  118. }
  119. static int iwl_is_empty_essid(const char *essid, int essid_len)
  120. {
  121. /* Single white space is for Linksys APs */
  122. if (essid_len == 1 && essid[0] == ' ')
  123. return 1;
  124. /* Otherwise, if the entire essid is 0, we assume it is hidden */
  125. while (essid_len) {
  126. essid_len--;
  127. if (essid[essid_len] != '\0')
  128. return 0;
  129. }
  130. return 1;
  131. }
  132. static const char *iwl_escape_essid(const char *essid, u8 essid_len)
  133. {
  134. static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
  135. const char *s = essid;
  136. char *d = escaped;
  137. if (iwl_is_empty_essid(essid, essid_len)) {
  138. memcpy(escaped, "<hidden>", sizeof("<hidden>"));
  139. return escaped;
  140. }
  141. essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
  142. while (essid_len--) {
  143. if (*s == '\0') {
  144. *d++ = '\\';
  145. *d++ = '0';
  146. s++;
  147. } else
  148. *d++ = *s++;
  149. }
  150. *d = '\0';
  151. return escaped;
  152. }
  153. static void iwl_print_hex_dump(int level, void *p, u32 len)
  154. {
  155. #ifdef CONFIG_IWLWIFI_DEBUG
  156. if (!(iwl_debug_level & level))
  157. return;
  158. print_hex_dump(KERN_DEBUG, "iwl data: ", DUMP_PREFIX_OFFSET, 16, 1,
  159. p, len, 1);
  160. #endif
  161. }
  162. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  163. * DMA services
  164. *
  165. * Theory of operation
  166. *
  167. * A queue is a circular buffers with 'Read' and 'Write' pointers.
  168. * 2 empty entries always kept in the buffer to protect from overflow.
  169. *
  170. * For Tx queue, there are low mark and high mark limits. If, after queuing
  171. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  172. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  173. * Tx queue resumed.
  174. *
  175. * The IWL operates with six queues, one receive queue in the device's
  176. * sram, one transmit queue for sending commands to the device firmware,
  177. * and four transmit queues for data.
  178. ***************************************************/
  179. static int iwl_queue_space(const struct iwl_queue *q)
  180. {
  181. int s = q->read_ptr - q->write_ptr;
  182. if (q->read_ptr > q->write_ptr)
  183. s -= q->n_bd;
  184. if (s <= 0)
  185. s += q->n_window;
  186. /* keep some reserve to not confuse empty and full situations */
  187. s -= 2;
  188. if (s < 0)
  189. s = 0;
  190. return s;
  191. }
  192. /* XXX: n_bd must be power-of-two size */
  193. static inline int iwl_queue_inc_wrap(int index, int n_bd)
  194. {
  195. return ++index & (n_bd - 1);
  196. }
  197. /* XXX: n_bd must be power-of-two size */
  198. static inline int iwl_queue_dec_wrap(int index, int n_bd)
  199. {
  200. return --index & (n_bd - 1);
  201. }
  202. static inline int x2_queue_used(const struct iwl_queue *q, int i)
  203. {
  204. return q->write_ptr > q->read_ptr ?
  205. (i >= q->read_ptr && i < q->write_ptr) :
  206. !(i < q->read_ptr && i >= q->write_ptr);
  207. }
  208. static inline u8 get_cmd_index(struct iwl_queue *q, u32 index, int is_huge)
  209. {
  210. if (is_huge)
  211. return q->n_window;
  212. return index & (q->n_window - 1);
  213. }
  214. static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
  215. int count, int slots_num, u32 id)
  216. {
  217. q->n_bd = count;
  218. q->n_window = slots_num;
  219. q->id = id;
  220. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  221. * and iwl_queue_dec_wrap are broken. */
  222. BUG_ON(!is_power_of_2(count));
  223. /* slots_num must be power-of-two size, otherwise
  224. * get_cmd_index is broken. */
  225. BUG_ON(!is_power_of_2(slots_num));
  226. q->low_mark = q->n_window / 4;
  227. if (q->low_mark < 4)
  228. q->low_mark = 4;
  229. q->high_mark = q->n_window / 8;
  230. if (q->high_mark < 2)
  231. q->high_mark = 2;
  232. q->write_ptr = q->read_ptr = 0;
  233. return 0;
  234. }
  235. static int iwl_tx_queue_alloc(struct iwl_priv *priv,
  236. struct iwl_tx_queue *txq, u32 id)
  237. {
  238. struct pci_dev *dev = priv->pci_dev;
  239. if (id != IWL_CMD_QUEUE_NUM) {
  240. txq->txb = kmalloc(sizeof(txq->txb[0]) *
  241. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  242. if (!txq->txb) {
  243. IWL_ERROR("kmalloc for auxiliary BD "
  244. "structures failed\n");
  245. goto error;
  246. }
  247. } else
  248. txq->txb = NULL;
  249. txq->bd = pci_alloc_consistent(dev,
  250. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
  251. &txq->q.dma_addr);
  252. if (!txq->bd) {
  253. IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
  254. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
  255. goto error;
  256. }
  257. txq->q.id = id;
  258. return 0;
  259. error:
  260. if (txq->txb) {
  261. kfree(txq->txb);
  262. txq->txb = NULL;
  263. }
  264. return -ENOMEM;
  265. }
  266. int iwl_tx_queue_init(struct iwl_priv *priv,
  267. struct iwl_tx_queue *txq, int slots_num, u32 txq_id)
  268. {
  269. struct pci_dev *dev = priv->pci_dev;
  270. int len;
  271. int rc = 0;
  272. /* allocate command space + one big command for scan since scan
  273. * command is very huge the system will not have two scan at the
  274. * same time */
  275. len = sizeof(struct iwl_cmd) * slots_num;
  276. if (txq_id == IWL_CMD_QUEUE_NUM)
  277. len += IWL_MAX_SCAN_SIZE;
  278. txq->cmd = pci_alloc_consistent(dev, len, &txq->dma_addr_cmd);
  279. if (!txq->cmd)
  280. return -ENOMEM;
  281. rc = iwl_tx_queue_alloc(priv, txq, txq_id);
  282. if (rc) {
  283. pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
  284. return -ENOMEM;
  285. }
  286. txq->need_update = 0;
  287. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  288. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  289. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  290. iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  291. iwl_hw_tx_queue_init(priv, txq);
  292. return 0;
  293. }
  294. /**
  295. * iwl_tx_queue_free - Deallocate DMA queue.
  296. * @txq: Transmit queue to deallocate.
  297. *
  298. * Empty queue by removing and destroying all BD's.
  299. * Free all buffers. txq itself is not freed.
  300. *
  301. */
  302. void iwl_tx_queue_free(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  303. {
  304. struct iwl_queue *q = &txq->q;
  305. struct pci_dev *dev = priv->pci_dev;
  306. int len;
  307. if (q->n_bd == 0)
  308. return;
  309. /* first, empty all BD's */
  310. for (; q->write_ptr != q->read_ptr;
  311. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
  312. iwl_hw_txq_free_tfd(priv, txq);
  313. len = sizeof(struct iwl_cmd) * q->n_window;
  314. if (q->id == IWL_CMD_QUEUE_NUM)
  315. len += IWL_MAX_SCAN_SIZE;
  316. pci_free_consistent(dev, len, txq->cmd, txq->dma_addr_cmd);
  317. /* free buffers belonging to queue itself */
  318. if (txq->q.n_bd)
  319. pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
  320. txq->q.n_bd, txq->bd, txq->q.dma_addr);
  321. if (txq->txb) {
  322. kfree(txq->txb);
  323. txq->txb = NULL;
  324. }
  325. /* 0 fill whole structure */
  326. memset(txq, 0, sizeof(*txq));
  327. }
  328. const u8 BROADCAST_ADDR[ETH_ALEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
  329. /*************** STATION TABLE MANAGEMENT ****
  330. *
  331. * NOTE: This needs to be overhauled to better synchronize between
  332. * how the iwl-4965.c is using iwl_hw_find_station vs. iwl-3945.c
  333. *
  334. * mac80211 should also be examined to determine if sta_info is duplicating
  335. * the functionality provided here
  336. */
  337. /**************************************************************/
  338. #if 0 /* temporary disable till we add real remove station */
  339. static u8 iwl_remove_station(struct iwl_priv *priv, const u8 *addr, int is_ap)
  340. {
  341. int index = IWL_INVALID_STATION;
  342. int i;
  343. unsigned long flags;
  344. spin_lock_irqsave(&priv->sta_lock, flags);
  345. if (is_ap)
  346. index = IWL_AP_ID;
  347. else if (is_broadcast_ether_addr(addr))
  348. index = priv->hw_setting.bcast_sta_id;
  349. else
  350. for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++)
  351. if (priv->stations[i].used &&
  352. !compare_ether_addr(priv->stations[i].sta.sta.addr,
  353. addr)) {
  354. index = i;
  355. break;
  356. }
  357. if (unlikely(index == IWL_INVALID_STATION))
  358. goto out;
  359. if (priv->stations[index].used) {
  360. priv->stations[index].used = 0;
  361. priv->num_stations--;
  362. }
  363. BUG_ON(priv->num_stations < 0);
  364. out:
  365. spin_unlock_irqrestore(&priv->sta_lock, flags);
  366. return 0;
  367. }
  368. #endif
  369. static void iwl_clear_stations_table(struct iwl_priv *priv)
  370. {
  371. unsigned long flags;
  372. spin_lock_irqsave(&priv->sta_lock, flags);
  373. priv->num_stations = 0;
  374. memset(priv->stations, 0, sizeof(priv->stations));
  375. spin_unlock_irqrestore(&priv->sta_lock, flags);
  376. }
  377. u8 iwl_add_station(struct iwl_priv *priv, const u8 *addr, int is_ap, u8 flags)
  378. {
  379. int i;
  380. int index = IWL_INVALID_STATION;
  381. struct iwl_station_entry *station;
  382. unsigned long flags_spin;
  383. DECLARE_MAC_BUF(mac);
  384. u8 rate;
  385. spin_lock_irqsave(&priv->sta_lock, flags_spin);
  386. if (is_ap)
  387. index = IWL_AP_ID;
  388. else if (is_broadcast_ether_addr(addr))
  389. index = priv->hw_setting.bcast_sta_id;
  390. else
  391. for (i = IWL_STA_ID; i < priv->hw_setting.max_stations; i++) {
  392. if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
  393. addr)) {
  394. index = i;
  395. break;
  396. }
  397. if (!priv->stations[i].used &&
  398. index == IWL_INVALID_STATION)
  399. index = i;
  400. }
  401. /* These two conditions has the same outcome but keep them separate
  402. since they have different meaning */
  403. if (unlikely(index == IWL_INVALID_STATION)) {
  404. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  405. return index;
  406. }
  407. if (priv->stations[index].used &&
  408. !compare_ether_addr(priv->stations[index].sta.sta.addr, addr)) {
  409. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  410. return index;
  411. }
  412. IWL_DEBUG_ASSOC("Add STA ID %d: %s\n", index, print_mac(mac, addr));
  413. station = &priv->stations[index];
  414. station->used = 1;
  415. priv->num_stations++;
  416. memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
  417. memcpy(station->sta.sta.addr, addr, ETH_ALEN);
  418. station->sta.mode = 0;
  419. station->sta.sta.sta_id = index;
  420. station->sta.station_flags = 0;
  421. if (priv->phymode == MODE_IEEE80211A)
  422. rate = IWL_RATE_6M_PLCP;
  423. else
  424. rate = IWL_RATE_1M_PLCP;
  425. /* Turn on both antennas for the station... */
  426. station->sta.rate_n_flags =
  427. iwl_hw_set_rate_n_flags(rate, RATE_MCS_ANT_AB_MSK);
  428. station->current_rate.rate_n_flags =
  429. le16_to_cpu(station->sta.rate_n_flags);
  430. spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
  431. iwl_send_add_station(priv, &station->sta, flags);
  432. return index;
  433. }
  434. /*************** DRIVER STATUS FUNCTIONS *****/
  435. static inline int iwl_is_ready(struct iwl_priv *priv)
  436. {
  437. /* The adapter is 'ready' if READY and GEO_CONFIGURED bits are
  438. * set but EXIT_PENDING is not */
  439. return test_bit(STATUS_READY, &priv->status) &&
  440. test_bit(STATUS_GEO_CONFIGURED, &priv->status) &&
  441. !test_bit(STATUS_EXIT_PENDING, &priv->status);
  442. }
  443. static inline int iwl_is_alive(struct iwl_priv *priv)
  444. {
  445. return test_bit(STATUS_ALIVE, &priv->status);
  446. }
  447. static inline int iwl_is_init(struct iwl_priv *priv)
  448. {
  449. return test_bit(STATUS_INIT, &priv->status);
  450. }
  451. static inline int iwl_is_rfkill(struct iwl_priv *priv)
  452. {
  453. return test_bit(STATUS_RF_KILL_HW, &priv->status) ||
  454. test_bit(STATUS_RF_KILL_SW, &priv->status);
  455. }
  456. static inline int iwl_is_ready_rf(struct iwl_priv *priv)
  457. {
  458. if (iwl_is_rfkill(priv))
  459. return 0;
  460. return iwl_is_ready(priv);
  461. }
  462. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  463. #define IWL_CMD(x) case x : return #x
  464. static const char *get_cmd_string(u8 cmd)
  465. {
  466. switch (cmd) {
  467. IWL_CMD(REPLY_ALIVE);
  468. IWL_CMD(REPLY_ERROR);
  469. IWL_CMD(REPLY_RXON);
  470. IWL_CMD(REPLY_RXON_ASSOC);
  471. IWL_CMD(REPLY_QOS_PARAM);
  472. IWL_CMD(REPLY_RXON_TIMING);
  473. IWL_CMD(REPLY_ADD_STA);
  474. IWL_CMD(REPLY_REMOVE_STA);
  475. IWL_CMD(REPLY_REMOVE_ALL_STA);
  476. IWL_CMD(REPLY_3945_RX);
  477. IWL_CMD(REPLY_TX);
  478. IWL_CMD(REPLY_RATE_SCALE);
  479. IWL_CMD(REPLY_LEDS_CMD);
  480. IWL_CMD(REPLY_TX_LINK_QUALITY_CMD);
  481. IWL_CMD(RADAR_NOTIFICATION);
  482. IWL_CMD(REPLY_QUIET_CMD);
  483. IWL_CMD(REPLY_CHANNEL_SWITCH);
  484. IWL_CMD(CHANNEL_SWITCH_NOTIFICATION);
  485. IWL_CMD(REPLY_SPECTRUM_MEASUREMENT_CMD);
  486. IWL_CMD(SPECTRUM_MEASURE_NOTIFICATION);
  487. IWL_CMD(POWER_TABLE_CMD);
  488. IWL_CMD(PM_SLEEP_NOTIFICATION);
  489. IWL_CMD(PM_DEBUG_STATISTIC_NOTIFIC);
  490. IWL_CMD(REPLY_SCAN_CMD);
  491. IWL_CMD(REPLY_SCAN_ABORT_CMD);
  492. IWL_CMD(SCAN_START_NOTIFICATION);
  493. IWL_CMD(SCAN_RESULTS_NOTIFICATION);
  494. IWL_CMD(SCAN_COMPLETE_NOTIFICATION);
  495. IWL_CMD(BEACON_NOTIFICATION);
  496. IWL_CMD(REPLY_TX_BEACON);
  497. IWL_CMD(WHO_IS_AWAKE_NOTIFICATION);
  498. IWL_CMD(QUIET_NOTIFICATION);
  499. IWL_CMD(REPLY_TX_PWR_TABLE_CMD);
  500. IWL_CMD(MEASURE_ABORT_NOTIFICATION);
  501. IWL_CMD(REPLY_BT_CONFIG);
  502. IWL_CMD(REPLY_STATISTICS_CMD);
  503. IWL_CMD(STATISTICS_NOTIFICATION);
  504. IWL_CMD(REPLY_CARD_STATE_CMD);
  505. IWL_CMD(CARD_STATE_NOTIFICATION);
  506. IWL_CMD(MISSED_BEACONS_NOTIFICATION);
  507. default:
  508. return "UNKNOWN";
  509. }
  510. }
  511. #define HOST_COMPLETE_TIMEOUT (HZ / 2)
  512. /**
  513. * iwl_enqueue_hcmd - enqueue a uCode command
  514. * @priv: device private data point
  515. * @cmd: a point to the ucode command structure
  516. *
  517. * The function returns < 0 values to indicate the operation is
  518. * failed. On success, it turns the index (> 0) of command in the
  519. * command queue.
  520. */
  521. static int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  522. {
  523. struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
  524. struct iwl_queue *q = &txq->q;
  525. struct iwl_tfd_frame *tfd;
  526. u32 *control_flags;
  527. struct iwl_cmd *out_cmd;
  528. u32 idx;
  529. u16 fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
  530. dma_addr_t phys_addr;
  531. int pad;
  532. u16 count;
  533. int ret;
  534. unsigned long flags;
  535. /* If any of the command structures end up being larger than
  536. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  537. * we will need to increase the size of the TFD entries */
  538. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  539. !(cmd->meta.flags & CMD_SIZE_HUGE));
  540. if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
  541. IWL_ERROR("No space for Tx\n");
  542. return -ENOSPC;
  543. }
  544. spin_lock_irqsave(&priv->hcmd_lock, flags);
  545. tfd = &txq->bd[q->write_ptr];
  546. memset(tfd, 0, sizeof(*tfd));
  547. control_flags = (u32 *) tfd;
  548. idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
  549. out_cmd = &txq->cmd[idx];
  550. out_cmd->hdr.cmd = cmd->id;
  551. memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
  552. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  553. /* At this point, the out_cmd now has all of the incoming cmd
  554. * information */
  555. out_cmd->hdr.flags = 0;
  556. out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
  557. INDEX_TO_SEQ(q->write_ptr));
  558. if (out_cmd->meta.flags & CMD_SIZE_HUGE)
  559. out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
  560. phys_addr = txq->dma_addr_cmd + sizeof(txq->cmd[0]) * idx +
  561. offsetof(struct iwl_cmd, hdr);
  562. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
  563. pad = U32_PAD(cmd->len);
  564. count = TFD_CTL_COUNT_GET(*control_flags);
  565. *control_flags = TFD_CTL_COUNT_SET(count) | TFD_CTL_PAD_SET(pad);
  566. IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
  567. "%d bytes at %d[%d]:%d\n",
  568. get_cmd_string(out_cmd->hdr.cmd),
  569. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  570. fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
  571. txq->need_update = 1;
  572. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  573. ret = iwl_tx_queue_update_write_ptr(priv, txq);
  574. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  575. return ret ? ret : idx;
  576. }
  577. int iwl_send_cmd_async(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  578. {
  579. int ret;
  580. BUG_ON(!(cmd->meta.flags & CMD_ASYNC));
  581. /* An asynchronous command can not expect an SKB to be set. */
  582. BUG_ON(cmd->meta.flags & CMD_WANT_SKB);
  583. /* An asynchronous command MUST have a callback. */
  584. BUG_ON(!cmd->meta.u.callback);
  585. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  586. return -EBUSY;
  587. ret = iwl_enqueue_hcmd(priv, cmd);
  588. if (ret < 0) {
  589. IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
  590. get_cmd_string(cmd->id), ret);
  591. return ret;
  592. }
  593. return 0;
  594. }
  595. int iwl_send_cmd_sync(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  596. {
  597. int cmd_idx;
  598. int ret;
  599. static atomic_t entry = ATOMIC_INIT(0); /* reentrance protection */
  600. BUG_ON(cmd->meta.flags & CMD_ASYNC);
  601. /* A synchronous command can not have a callback set. */
  602. BUG_ON(cmd->meta.u.callback != NULL);
  603. if (atomic_xchg(&entry, 1)) {
  604. IWL_ERROR("Error sending %s: Already sending a host command\n",
  605. get_cmd_string(cmd->id));
  606. return -EBUSY;
  607. }
  608. set_bit(STATUS_HCMD_ACTIVE, &priv->status);
  609. if (cmd->meta.flags & CMD_WANT_SKB)
  610. cmd->meta.source = &cmd->meta;
  611. cmd_idx = iwl_enqueue_hcmd(priv, cmd);
  612. if (cmd_idx < 0) {
  613. ret = cmd_idx;
  614. IWL_ERROR("Error sending %s: iwl_enqueue_hcmd failed: %d\n",
  615. get_cmd_string(cmd->id), ret);
  616. goto out;
  617. }
  618. ret = wait_event_interruptible_timeout(priv->wait_command_queue,
  619. !test_bit(STATUS_HCMD_ACTIVE, &priv->status),
  620. HOST_COMPLETE_TIMEOUT);
  621. if (!ret) {
  622. if (test_bit(STATUS_HCMD_ACTIVE, &priv->status)) {
  623. IWL_ERROR("Error sending %s: time out after %dms.\n",
  624. get_cmd_string(cmd->id),
  625. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  626. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  627. ret = -ETIMEDOUT;
  628. goto cancel;
  629. }
  630. }
  631. if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
  632. IWL_DEBUG_INFO("Command %s aborted: RF KILL Switch\n",
  633. get_cmd_string(cmd->id));
  634. ret = -ECANCELED;
  635. goto fail;
  636. }
  637. if (test_bit(STATUS_FW_ERROR, &priv->status)) {
  638. IWL_DEBUG_INFO("Command %s failed: FW Error\n",
  639. get_cmd_string(cmd->id));
  640. ret = -EIO;
  641. goto fail;
  642. }
  643. if ((cmd->meta.flags & CMD_WANT_SKB) && !cmd->meta.u.skb) {
  644. IWL_ERROR("Error: Response NULL in '%s'\n",
  645. get_cmd_string(cmd->id));
  646. ret = -EIO;
  647. goto out;
  648. }
  649. ret = 0;
  650. goto out;
  651. cancel:
  652. if (cmd->meta.flags & CMD_WANT_SKB) {
  653. struct iwl_cmd *qcmd;
  654. /* Cancel the CMD_WANT_SKB flag for the cmd in the
  655. * TX cmd queue. Otherwise in case the cmd comes
  656. * in later, it will possibly set an invalid
  657. * address (cmd->meta.source). */
  658. qcmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_idx];
  659. qcmd->meta.flags &= ~CMD_WANT_SKB;
  660. }
  661. fail:
  662. if (cmd->meta.u.skb) {
  663. dev_kfree_skb_any(cmd->meta.u.skb);
  664. cmd->meta.u.skb = NULL;
  665. }
  666. out:
  667. atomic_set(&entry, 0);
  668. return ret;
  669. }
  670. int iwl_send_cmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  671. {
  672. /* A command can not be asynchronous AND expect an SKB to be set. */
  673. BUG_ON((cmd->meta.flags & CMD_ASYNC) &&
  674. (cmd->meta.flags & CMD_WANT_SKB));
  675. if (cmd->meta.flags & CMD_ASYNC)
  676. return iwl_send_cmd_async(priv, cmd);
  677. return iwl_send_cmd_sync(priv, cmd);
  678. }
  679. int iwl_send_cmd_pdu(struct iwl_priv *priv, u8 id, u16 len, const void *data)
  680. {
  681. struct iwl_host_cmd cmd = {
  682. .id = id,
  683. .len = len,
  684. .data = data,
  685. };
  686. return iwl_send_cmd_sync(priv, &cmd);
  687. }
  688. static int __must_check iwl_send_cmd_u32(struct iwl_priv *priv, u8 id, u32 val)
  689. {
  690. struct iwl_host_cmd cmd = {
  691. .id = id,
  692. .len = sizeof(val),
  693. .data = &val,
  694. };
  695. return iwl_send_cmd_sync(priv, &cmd);
  696. }
  697. int iwl_send_statistics_request(struct iwl_priv *priv)
  698. {
  699. return iwl_send_cmd_u32(priv, REPLY_STATISTICS_CMD, 0);
  700. }
  701. /**
  702. * iwl_set_rxon_channel - Set the phymode and channel values in staging RXON
  703. * @phymode: MODE_IEEE80211A sets to 5.2GHz; all else set to 2.4GHz
  704. * @channel: Any channel valid for the requested phymode
  705. * In addition to setting the staging RXON, priv->phymode is also set.
  706. *
  707. * NOTE: Does not commit to the hardware; it sets appropriate bit fields
  708. * in the staging RXON flag structure based on the phymode
  709. */
  710. static int iwl_set_rxon_channel(struct iwl_priv *priv, u8 phymode, u16 channel)
  711. {
  712. if (!iwl_get_channel_info(priv, phymode, channel)) {
  713. IWL_DEBUG_INFO("Could not set channel to %d [%d]\n",
  714. channel, phymode);
  715. return -EINVAL;
  716. }
  717. if ((le16_to_cpu(priv->staging_rxon.channel) == channel) &&
  718. (priv->phymode == phymode))
  719. return 0;
  720. priv->staging_rxon.channel = cpu_to_le16(channel);
  721. if (phymode == MODE_IEEE80211A)
  722. priv->staging_rxon.flags &= ~RXON_FLG_BAND_24G_MSK;
  723. else
  724. priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
  725. priv->phymode = phymode;
  726. IWL_DEBUG_INFO("Staging channel set to %d [%d]\n", channel, phymode);
  727. return 0;
  728. }
  729. /**
  730. * iwl_check_rxon_cmd - validate RXON structure is valid
  731. *
  732. * NOTE: This is really only useful during development and can eventually
  733. * be #ifdef'd out once the driver is stable and folks aren't actively
  734. * making changes
  735. */
  736. static int iwl_check_rxon_cmd(struct iwl_rxon_cmd *rxon)
  737. {
  738. int error = 0;
  739. int counter = 1;
  740. if (rxon->flags & RXON_FLG_BAND_24G_MSK) {
  741. error |= le32_to_cpu(rxon->flags &
  742. (RXON_FLG_TGJ_NARROW_BAND_MSK |
  743. RXON_FLG_RADAR_DETECT_MSK));
  744. if (error)
  745. IWL_WARNING("check 24G fields %d | %d\n",
  746. counter++, error);
  747. } else {
  748. error |= (rxon->flags & RXON_FLG_SHORT_SLOT_MSK) ?
  749. 0 : le32_to_cpu(RXON_FLG_SHORT_SLOT_MSK);
  750. if (error)
  751. IWL_WARNING("check 52 fields %d | %d\n",
  752. counter++, error);
  753. error |= le32_to_cpu(rxon->flags & RXON_FLG_CCK_MSK);
  754. if (error)
  755. IWL_WARNING("check 52 CCK %d | %d\n",
  756. counter++, error);
  757. }
  758. error |= (rxon->node_addr[0] | rxon->bssid_addr[0]) & 0x1;
  759. if (error)
  760. IWL_WARNING("check mac addr %d | %d\n", counter++, error);
  761. /* make sure basic rates 6Mbps and 1Mbps are supported */
  762. error |= (((rxon->ofdm_basic_rates & IWL_RATE_6M_MASK) == 0) &&
  763. ((rxon->cck_basic_rates & IWL_RATE_1M_MASK) == 0));
  764. if (error)
  765. IWL_WARNING("check basic rate %d | %d\n", counter++, error);
  766. error |= (le16_to_cpu(rxon->assoc_id) > 2007);
  767. if (error)
  768. IWL_WARNING("check assoc id %d | %d\n", counter++, error);
  769. error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK))
  770. == (RXON_FLG_CCK_MSK | RXON_FLG_SHORT_SLOT_MSK));
  771. if (error)
  772. IWL_WARNING("check CCK and short slot %d | %d\n",
  773. counter++, error);
  774. error |= ((rxon->flags & (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK))
  775. == (RXON_FLG_CCK_MSK | RXON_FLG_AUTO_DETECT_MSK));
  776. if (error)
  777. IWL_WARNING("check CCK & auto detect %d | %d\n",
  778. counter++, error);
  779. error |= ((rxon->flags & (RXON_FLG_AUTO_DETECT_MSK |
  780. RXON_FLG_TGG_PROTECT_MSK)) == RXON_FLG_TGG_PROTECT_MSK);
  781. if (error)
  782. IWL_WARNING("check TGG and auto detect %d | %d\n",
  783. counter++, error);
  784. if ((rxon->flags & RXON_FLG_DIS_DIV_MSK))
  785. error |= ((rxon->flags & (RXON_FLG_ANT_B_MSK |
  786. RXON_FLG_ANT_A_MSK)) == 0);
  787. if (error)
  788. IWL_WARNING("check antenna %d %d\n", counter++, error);
  789. if (error)
  790. IWL_WARNING("Tuning to channel %d\n",
  791. le16_to_cpu(rxon->channel));
  792. if (error) {
  793. IWL_ERROR("Not a valid iwl_rxon_assoc_cmd field values\n");
  794. return -1;
  795. }
  796. return 0;
  797. }
  798. /**
  799. * iwl_full_rxon_required - determine if RXON_ASSOC can be used in RXON commit
  800. * @priv: staging_rxon is compared to active_rxon
  801. *
  802. * If the RXON structure is changing sufficient to require a new
  803. * tune or to clear and reset the RXON_FILTER_ASSOC_MSK then return 1
  804. * to indicate a new tune is required.
  805. */
  806. static int iwl_full_rxon_required(struct iwl_priv *priv)
  807. {
  808. /* These items are only settable from the full RXON command */
  809. if (!(priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) ||
  810. compare_ether_addr(priv->staging_rxon.bssid_addr,
  811. priv->active_rxon.bssid_addr) ||
  812. compare_ether_addr(priv->staging_rxon.node_addr,
  813. priv->active_rxon.node_addr) ||
  814. compare_ether_addr(priv->staging_rxon.wlap_bssid_addr,
  815. priv->active_rxon.wlap_bssid_addr) ||
  816. (priv->staging_rxon.dev_type != priv->active_rxon.dev_type) ||
  817. (priv->staging_rxon.channel != priv->active_rxon.channel) ||
  818. (priv->staging_rxon.air_propagation !=
  819. priv->active_rxon.air_propagation) ||
  820. (priv->staging_rxon.assoc_id != priv->active_rxon.assoc_id))
  821. return 1;
  822. /* flags, filter_flags, ofdm_basic_rates, and cck_basic_rates can
  823. * be updated with the RXON_ASSOC command -- however only some
  824. * flag transitions are allowed using RXON_ASSOC */
  825. /* Check if we are not switching bands */
  826. if ((priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) !=
  827. (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK))
  828. return 1;
  829. /* Check if we are switching association toggle */
  830. if ((priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) !=
  831. (priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK))
  832. return 1;
  833. return 0;
  834. }
  835. static int iwl_send_rxon_assoc(struct iwl_priv *priv)
  836. {
  837. int rc = 0;
  838. struct iwl_rx_packet *res = NULL;
  839. struct iwl_rxon_assoc_cmd rxon_assoc;
  840. struct iwl_host_cmd cmd = {
  841. .id = REPLY_RXON_ASSOC,
  842. .len = sizeof(rxon_assoc),
  843. .meta.flags = CMD_WANT_SKB,
  844. .data = &rxon_assoc,
  845. };
  846. const struct iwl_rxon_cmd *rxon1 = &priv->staging_rxon;
  847. const struct iwl_rxon_cmd *rxon2 = &priv->active_rxon;
  848. if ((rxon1->flags == rxon2->flags) &&
  849. (rxon1->filter_flags == rxon2->filter_flags) &&
  850. (rxon1->cck_basic_rates == rxon2->cck_basic_rates) &&
  851. (rxon1->ofdm_basic_rates == rxon2->ofdm_basic_rates)) {
  852. IWL_DEBUG_INFO("Using current RXON_ASSOC. Not resending.\n");
  853. return 0;
  854. }
  855. rxon_assoc.flags = priv->staging_rxon.flags;
  856. rxon_assoc.filter_flags = priv->staging_rxon.filter_flags;
  857. rxon_assoc.ofdm_basic_rates = priv->staging_rxon.ofdm_basic_rates;
  858. rxon_assoc.cck_basic_rates = priv->staging_rxon.cck_basic_rates;
  859. rxon_assoc.reserved = 0;
  860. rc = iwl_send_cmd_sync(priv, &cmd);
  861. if (rc)
  862. return rc;
  863. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  864. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  865. IWL_ERROR("Bad return from REPLY_RXON_ASSOC command\n");
  866. rc = -EIO;
  867. }
  868. priv->alloc_rxb_skb--;
  869. dev_kfree_skb_any(cmd.meta.u.skb);
  870. return rc;
  871. }
  872. /**
  873. * iwl_commit_rxon - commit staging_rxon to hardware
  874. *
  875. * The RXON command in staging_rxon is committed to the hardware and
  876. * the active_rxon structure is updated with the new data. This
  877. * function correctly transitions out of the RXON_ASSOC_MSK state if
  878. * a HW tune is required based on the RXON structure changes.
  879. */
  880. static int iwl_commit_rxon(struct iwl_priv *priv)
  881. {
  882. /* cast away the const for active_rxon in this function */
  883. struct iwl_rxon_cmd *active_rxon = (void *)&priv->active_rxon;
  884. int rc = 0;
  885. DECLARE_MAC_BUF(mac);
  886. if (!iwl_is_alive(priv))
  887. return -1;
  888. /* always get timestamp with Rx frame */
  889. priv->staging_rxon.flags |= RXON_FLG_TSF2HOST_MSK;
  890. /* select antenna */
  891. priv->staging_rxon.flags &=
  892. ~(RXON_FLG_DIS_DIV_MSK | RXON_FLG_ANT_SEL_MSK);
  893. priv->staging_rxon.flags |= iwl3945_get_antenna_flags(priv);
  894. rc = iwl_check_rxon_cmd(&priv->staging_rxon);
  895. if (rc) {
  896. IWL_ERROR("Invalid RXON configuration. Not committing.\n");
  897. return -EINVAL;
  898. }
  899. /* If we don't need to send a full RXON, we can use
  900. * iwl_rxon_assoc_cmd which is used to reconfigure filter
  901. * and other flags for the current radio configuration. */
  902. if (!iwl_full_rxon_required(priv)) {
  903. rc = iwl_send_rxon_assoc(priv);
  904. if (rc) {
  905. IWL_ERROR("Error setting RXON_ASSOC "
  906. "configuration (%d).\n", rc);
  907. return rc;
  908. }
  909. memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
  910. return 0;
  911. }
  912. /* If we are currently associated and the new config requires
  913. * an RXON_ASSOC and the new config wants the associated mask enabled,
  914. * we must clear the associated from the active configuration
  915. * before we apply the new config */
  916. if (iwl_is_associated(priv) &&
  917. (priv->staging_rxon.filter_flags & RXON_FILTER_ASSOC_MSK)) {
  918. IWL_DEBUG_INFO("Toggling associated bit on current RXON\n");
  919. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  920. rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
  921. sizeof(struct iwl_rxon_cmd),
  922. &priv->active_rxon);
  923. /* If the mask clearing failed then we set
  924. * active_rxon back to what it was previously */
  925. if (rc) {
  926. active_rxon->filter_flags |= RXON_FILTER_ASSOC_MSK;
  927. IWL_ERROR("Error clearing ASSOC_MSK on current "
  928. "configuration (%d).\n", rc);
  929. return rc;
  930. }
  931. }
  932. IWL_DEBUG_INFO("Sending RXON\n"
  933. "* with%s RXON_FILTER_ASSOC_MSK\n"
  934. "* channel = %d\n"
  935. "* bssid = %s\n",
  936. ((priv->staging_rxon.filter_flags &
  937. RXON_FILTER_ASSOC_MSK) ? "" : "out"),
  938. le16_to_cpu(priv->staging_rxon.channel),
  939. print_mac(mac, priv->staging_rxon.bssid_addr));
  940. /* Apply the new configuration */
  941. rc = iwl_send_cmd_pdu(priv, REPLY_RXON,
  942. sizeof(struct iwl_rxon_cmd), &priv->staging_rxon);
  943. if (rc) {
  944. IWL_ERROR("Error setting new configuration (%d).\n", rc);
  945. return rc;
  946. }
  947. memcpy(active_rxon, &priv->staging_rxon, sizeof(*active_rxon));
  948. iwl_clear_stations_table(priv);
  949. /* If we issue a new RXON command which required a tune then we must
  950. * send a new TXPOWER command or we won't be able to Tx any frames */
  951. rc = iwl_hw_reg_send_txpower(priv);
  952. if (rc) {
  953. IWL_ERROR("Error setting Tx power (%d).\n", rc);
  954. return rc;
  955. }
  956. /* Add the broadcast address so we can send broadcast frames */
  957. if (iwl_add_station(priv, BROADCAST_ADDR, 0, 0) ==
  958. IWL_INVALID_STATION) {
  959. IWL_ERROR("Error adding BROADCAST address for transmit.\n");
  960. return -EIO;
  961. }
  962. /* If we have set the ASSOC_MSK and we are in BSS mode then
  963. * add the IWL_AP_ID to the station rate table */
  964. if (iwl_is_associated(priv) &&
  965. (priv->iw_mode == IEEE80211_IF_TYPE_STA))
  966. if (iwl_add_station(priv, priv->active_rxon.bssid_addr, 1, 0)
  967. == IWL_INVALID_STATION) {
  968. IWL_ERROR("Error adding AP address for transmit.\n");
  969. return -EIO;
  970. }
  971. /* Init the hardware's rate fallback order based on the
  972. * phymode */
  973. rc = iwl3945_init_hw_rate_table(priv);
  974. if (rc) {
  975. IWL_ERROR("Error setting HW rate table: %02X\n", rc);
  976. return -EIO;
  977. }
  978. return 0;
  979. }
  980. static int iwl_send_bt_config(struct iwl_priv *priv)
  981. {
  982. struct iwl_bt_cmd bt_cmd = {
  983. .flags = 3,
  984. .lead_time = 0xAA,
  985. .max_kill = 1,
  986. .kill_ack_mask = 0,
  987. .kill_cts_mask = 0,
  988. };
  989. return iwl_send_cmd_pdu(priv, REPLY_BT_CONFIG,
  990. sizeof(struct iwl_bt_cmd), &bt_cmd);
  991. }
  992. static int iwl_send_scan_abort(struct iwl_priv *priv)
  993. {
  994. int rc = 0;
  995. struct iwl_rx_packet *res;
  996. struct iwl_host_cmd cmd = {
  997. .id = REPLY_SCAN_ABORT_CMD,
  998. .meta.flags = CMD_WANT_SKB,
  999. };
  1000. /* If there isn't a scan actively going on in the hardware
  1001. * then we are in between scan bands and not actually
  1002. * actively scanning, so don't send the abort command */
  1003. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  1004. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  1005. return 0;
  1006. }
  1007. rc = iwl_send_cmd_sync(priv, &cmd);
  1008. if (rc) {
  1009. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  1010. return rc;
  1011. }
  1012. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  1013. if (res->u.status != CAN_ABORT_STATUS) {
  1014. /* The scan abort will return 1 for success or
  1015. * 2 for "failure". A failure condition can be
  1016. * due to simply not being in an active scan which
  1017. * can occur if we send the scan abort before we
  1018. * the microcode has notified us that a scan is
  1019. * completed. */
  1020. IWL_DEBUG_INFO("SCAN_ABORT returned %d.\n", res->u.status);
  1021. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  1022. clear_bit(STATUS_SCAN_HW, &priv->status);
  1023. }
  1024. dev_kfree_skb_any(cmd.meta.u.skb);
  1025. return rc;
  1026. }
  1027. static int iwl_card_state_sync_callback(struct iwl_priv *priv,
  1028. struct iwl_cmd *cmd,
  1029. struct sk_buff *skb)
  1030. {
  1031. return 1;
  1032. }
  1033. /*
  1034. * CARD_STATE_CMD
  1035. *
  1036. * Use: Sets the internal card state to enable, disable, or halt
  1037. *
  1038. * When in the 'enable' state the card operates as normal.
  1039. * When in the 'disable' state, the card enters into a low power mode.
  1040. * When in the 'halt' state, the card is shut down and must be fully
  1041. * restarted to come back on.
  1042. */
  1043. static int iwl_send_card_state(struct iwl_priv *priv, u32 flags, u8 meta_flag)
  1044. {
  1045. struct iwl_host_cmd cmd = {
  1046. .id = REPLY_CARD_STATE_CMD,
  1047. .len = sizeof(u32),
  1048. .data = &flags,
  1049. .meta.flags = meta_flag,
  1050. };
  1051. if (meta_flag & CMD_ASYNC)
  1052. cmd.meta.u.callback = iwl_card_state_sync_callback;
  1053. return iwl_send_cmd(priv, &cmd);
  1054. }
  1055. static int iwl_add_sta_sync_callback(struct iwl_priv *priv,
  1056. struct iwl_cmd *cmd, struct sk_buff *skb)
  1057. {
  1058. struct iwl_rx_packet *res = NULL;
  1059. if (!skb) {
  1060. IWL_ERROR("Error: Response NULL in REPLY_ADD_STA.\n");
  1061. return 1;
  1062. }
  1063. res = (struct iwl_rx_packet *)skb->data;
  1064. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  1065. IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
  1066. res->hdr.flags);
  1067. return 1;
  1068. }
  1069. switch (res->u.add_sta.status) {
  1070. case ADD_STA_SUCCESS_MSK:
  1071. break;
  1072. default:
  1073. break;
  1074. }
  1075. /* We didn't cache the SKB; let the caller free it */
  1076. return 1;
  1077. }
  1078. int iwl_send_add_station(struct iwl_priv *priv,
  1079. struct iwl_addsta_cmd *sta, u8 flags)
  1080. {
  1081. struct iwl_rx_packet *res = NULL;
  1082. int rc = 0;
  1083. struct iwl_host_cmd cmd = {
  1084. .id = REPLY_ADD_STA,
  1085. .len = sizeof(struct iwl_addsta_cmd),
  1086. .meta.flags = flags,
  1087. .data = sta,
  1088. };
  1089. if (flags & CMD_ASYNC)
  1090. cmd.meta.u.callback = iwl_add_sta_sync_callback;
  1091. else
  1092. cmd.meta.flags |= CMD_WANT_SKB;
  1093. rc = iwl_send_cmd(priv, &cmd);
  1094. if (rc || (flags & CMD_ASYNC))
  1095. return rc;
  1096. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  1097. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  1098. IWL_ERROR("Bad return from REPLY_ADD_STA (0x%08X)\n",
  1099. res->hdr.flags);
  1100. rc = -EIO;
  1101. }
  1102. if (rc == 0) {
  1103. switch (res->u.add_sta.status) {
  1104. case ADD_STA_SUCCESS_MSK:
  1105. IWL_DEBUG_INFO("REPLY_ADD_STA PASSED\n");
  1106. break;
  1107. default:
  1108. rc = -EIO;
  1109. IWL_WARNING("REPLY_ADD_STA failed\n");
  1110. break;
  1111. }
  1112. }
  1113. priv->alloc_rxb_skb--;
  1114. dev_kfree_skb_any(cmd.meta.u.skb);
  1115. return rc;
  1116. }
  1117. static int iwl_update_sta_key_info(struct iwl_priv *priv,
  1118. struct ieee80211_key_conf *keyconf,
  1119. u8 sta_id)
  1120. {
  1121. unsigned long flags;
  1122. __le16 key_flags = 0;
  1123. switch (keyconf->alg) {
  1124. case ALG_CCMP:
  1125. key_flags |= STA_KEY_FLG_CCMP;
  1126. key_flags |= cpu_to_le16(
  1127. keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  1128. key_flags &= ~STA_KEY_FLG_INVALID;
  1129. break;
  1130. case ALG_TKIP:
  1131. case ALG_WEP:
  1132. default:
  1133. return -EINVAL;
  1134. }
  1135. spin_lock_irqsave(&priv->sta_lock, flags);
  1136. priv->stations[sta_id].keyinfo.alg = keyconf->alg;
  1137. priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  1138. memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
  1139. keyconf->keylen);
  1140. memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
  1141. keyconf->keylen);
  1142. priv->stations[sta_id].sta.key.key_flags = key_flags;
  1143. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1144. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1145. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1146. IWL_DEBUG_INFO("hwcrypto: modify ucode station key info\n");
  1147. iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
  1148. return 0;
  1149. }
  1150. static int iwl_clear_sta_key_info(struct iwl_priv *priv, u8 sta_id)
  1151. {
  1152. unsigned long flags;
  1153. spin_lock_irqsave(&priv->sta_lock, flags);
  1154. memset(&priv->stations[sta_id].keyinfo, 0, sizeof(struct iwl_hw_key));
  1155. memset(&priv->stations[sta_id].sta.key, 0, sizeof(struct iwl_keyinfo));
  1156. priv->stations[sta_id].sta.key.key_flags = STA_KEY_FLG_NO_ENC;
  1157. priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  1158. priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  1159. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1160. IWL_DEBUG_INFO("hwcrypto: clear ucode station key info\n");
  1161. iwl_send_add_station(priv, &priv->stations[sta_id].sta, 0);
  1162. return 0;
  1163. }
  1164. static void iwl_clear_free_frames(struct iwl_priv *priv)
  1165. {
  1166. struct list_head *element;
  1167. IWL_DEBUG_INFO("%d frames on pre-allocated heap on clear.\n",
  1168. priv->frames_count);
  1169. while (!list_empty(&priv->free_frames)) {
  1170. element = priv->free_frames.next;
  1171. list_del(element);
  1172. kfree(list_entry(element, struct iwl_frame, list));
  1173. priv->frames_count--;
  1174. }
  1175. if (priv->frames_count) {
  1176. IWL_WARNING("%d frames still in use. Did we lose one?\n",
  1177. priv->frames_count);
  1178. priv->frames_count = 0;
  1179. }
  1180. }
  1181. static struct iwl_frame *iwl_get_free_frame(struct iwl_priv *priv)
  1182. {
  1183. struct iwl_frame *frame;
  1184. struct list_head *element;
  1185. if (list_empty(&priv->free_frames)) {
  1186. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  1187. if (!frame) {
  1188. IWL_ERROR("Could not allocate frame!\n");
  1189. return NULL;
  1190. }
  1191. priv->frames_count++;
  1192. return frame;
  1193. }
  1194. element = priv->free_frames.next;
  1195. list_del(element);
  1196. return list_entry(element, struct iwl_frame, list);
  1197. }
  1198. static void iwl_free_frame(struct iwl_priv *priv, struct iwl_frame *frame)
  1199. {
  1200. memset(frame, 0, sizeof(*frame));
  1201. list_add(&frame->list, &priv->free_frames);
  1202. }
  1203. unsigned int iwl_fill_beacon_frame(struct iwl_priv *priv,
  1204. struct ieee80211_hdr *hdr,
  1205. const u8 *dest, int left)
  1206. {
  1207. if (!iwl_is_associated(priv) || !priv->ibss_beacon ||
  1208. ((priv->iw_mode != IEEE80211_IF_TYPE_IBSS) &&
  1209. (priv->iw_mode != IEEE80211_IF_TYPE_AP)))
  1210. return 0;
  1211. if (priv->ibss_beacon->len > left)
  1212. return 0;
  1213. memcpy(hdr, priv->ibss_beacon->data, priv->ibss_beacon->len);
  1214. return priv->ibss_beacon->len;
  1215. }
  1216. static int iwl_rate_index_from_plcp(int plcp)
  1217. {
  1218. int i = 0;
  1219. for (i = 0; i < IWL_RATE_COUNT; i++)
  1220. if (iwl_rates[i].plcp == plcp)
  1221. return i;
  1222. return -1;
  1223. }
  1224. static u8 iwl_rate_get_lowest_plcp(int rate_mask)
  1225. {
  1226. u8 i;
  1227. for (i = IWL_RATE_1M_INDEX; i != IWL_RATE_INVALID;
  1228. i = iwl_rates[i].next_ieee) {
  1229. if (rate_mask & (1 << i))
  1230. return iwl_rates[i].plcp;
  1231. }
  1232. return IWL_RATE_INVALID;
  1233. }
  1234. static int iwl_send_beacon_cmd(struct iwl_priv *priv)
  1235. {
  1236. struct iwl_frame *frame;
  1237. unsigned int frame_size;
  1238. int rc;
  1239. u8 rate;
  1240. frame = iwl_get_free_frame(priv);
  1241. if (!frame) {
  1242. IWL_ERROR("Could not obtain free frame buffer for beacon "
  1243. "command.\n");
  1244. return -ENOMEM;
  1245. }
  1246. if (!(priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK)) {
  1247. rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic &
  1248. 0xFF0);
  1249. if (rate == IWL_INVALID_RATE)
  1250. rate = IWL_RATE_6M_PLCP;
  1251. } else {
  1252. rate = iwl_rate_get_lowest_plcp(priv->active_rate_basic & 0xF);
  1253. if (rate == IWL_INVALID_RATE)
  1254. rate = IWL_RATE_1M_PLCP;
  1255. }
  1256. frame_size = iwl_hw_get_beacon_cmd(priv, frame, rate);
  1257. rc = iwl_send_cmd_pdu(priv, REPLY_TX_BEACON, frame_size,
  1258. &frame->u.cmd[0]);
  1259. iwl_free_frame(priv, frame);
  1260. return rc;
  1261. }
  1262. /******************************************************************************
  1263. *
  1264. * EEPROM related functions
  1265. *
  1266. ******************************************************************************/
  1267. static void get_eeprom_mac(struct iwl_priv *priv, u8 *mac)
  1268. {
  1269. memcpy(mac, priv->eeprom.mac_address, 6);
  1270. }
  1271. /**
  1272. * iwl_eeprom_init - read EEPROM contents
  1273. *
  1274. * Load the EEPROM from adapter into priv->eeprom
  1275. *
  1276. * NOTE: This routine uses the non-debug IO access functions.
  1277. */
  1278. int iwl_eeprom_init(struct iwl_priv *priv)
  1279. {
  1280. u16 *e = (u16 *)&priv->eeprom;
  1281. u32 gp = iwl_read32(priv, CSR_EEPROM_GP);
  1282. u32 r;
  1283. int sz = sizeof(priv->eeprom);
  1284. int rc;
  1285. int i;
  1286. u16 addr;
  1287. /* The EEPROM structure has several padding buffers within it
  1288. * and when adding new EEPROM maps is subject to programmer errors
  1289. * which may be very difficult to identify without explicitly
  1290. * checking the resulting size of the eeprom map. */
  1291. BUILD_BUG_ON(sizeof(priv->eeprom) != IWL_EEPROM_IMAGE_SIZE);
  1292. if ((gp & CSR_EEPROM_GP_VALID_MSK) == CSR_EEPROM_GP_BAD_SIGNATURE) {
  1293. IWL_ERROR("EEPROM not found, EEPROM_GP=0x%08x", gp);
  1294. return -ENOENT;
  1295. }
  1296. rc = iwl_eeprom_acquire_semaphore(priv);
  1297. if (rc < 0) {
  1298. IWL_ERROR("Failed to acquire EEPROM semaphore.\n");
  1299. return -ENOENT;
  1300. }
  1301. /* eeprom is an array of 16bit values */
  1302. for (addr = 0; addr < sz; addr += sizeof(u16)) {
  1303. _iwl_write32(priv, CSR_EEPROM_REG, addr << 1);
  1304. _iwl_clear_bit(priv, CSR_EEPROM_REG, CSR_EEPROM_REG_BIT_CMD);
  1305. for (i = 0; i < IWL_EEPROM_ACCESS_TIMEOUT;
  1306. i += IWL_EEPROM_ACCESS_DELAY) {
  1307. r = _iwl_read_restricted(priv, CSR_EEPROM_REG);
  1308. if (r & CSR_EEPROM_REG_READ_VALID_MSK)
  1309. break;
  1310. udelay(IWL_EEPROM_ACCESS_DELAY);
  1311. }
  1312. if (!(r & CSR_EEPROM_REG_READ_VALID_MSK)) {
  1313. IWL_ERROR("Time out reading EEPROM[%d]", addr);
  1314. return -ETIMEDOUT;
  1315. }
  1316. e[addr / 2] = le16_to_cpu(r >> 16);
  1317. }
  1318. return 0;
  1319. }
  1320. /******************************************************************************
  1321. *
  1322. * Misc. internal state and helper functions
  1323. *
  1324. ******************************************************************************/
  1325. #ifdef CONFIG_IWLWIFI_DEBUG
  1326. /**
  1327. * iwl_report_frame - dump frame to syslog during debug sessions
  1328. *
  1329. * hack this function to show different aspects of received frames,
  1330. * including selective frame dumps.
  1331. * group100 parameter selects whether to show 1 out of 100 good frames.
  1332. *
  1333. * TODO: ieee80211_hdr stuff is common to 3945 and 4965, so frame type
  1334. * info output is okay, but some of this stuff (e.g. iwl_rx_frame_stats)
  1335. * is 3945-specific and gives bad output for 4965. Need to split the
  1336. * functionality, keep common stuff here.
  1337. */
  1338. void iwl_report_frame(struct iwl_priv *priv,
  1339. struct iwl_rx_packet *pkt,
  1340. struct ieee80211_hdr *header, int group100)
  1341. {
  1342. u32 to_us;
  1343. u32 print_summary = 0;
  1344. u32 print_dump = 0; /* set to 1 to dump all frames' contents */
  1345. u32 hundred = 0;
  1346. u32 dataframe = 0;
  1347. u16 fc;
  1348. u16 seq_ctl;
  1349. u16 channel;
  1350. u16 phy_flags;
  1351. int rate_sym;
  1352. u16 length;
  1353. u16 status;
  1354. u16 bcn_tmr;
  1355. u32 tsf_low;
  1356. u64 tsf;
  1357. u8 rssi;
  1358. u8 agc;
  1359. u16 sig_avg;
  1360. u16 noise_diff;
  1361. struct iwl_rx_frame_stats *rx_stats = IWL_RX_STATS(pkt);
  1362. struct iwl_rx_frame_hdr *rx_hdr = IWL_RX_HDR(pkt);
  1363. struct iwl_rx_frame_end *rx_end = IWL_RX_END(pkt);
  1364. u8 *data = IWL_RX_DATA(pkt);
  1365. /* MAC header */
  1366. fc = le16_to_cpu(header->frame_control);
  1367. seq_ctl = le16_to_cpu(header->seq_ctrl);
  1368. /* metadata */
  1369. channel = le16_to_cpu(rx_hdr->channel);
  1370. phy_flags = le16_to_cpu(rx_hdr->phy_flags);
  1371. rate_sym = rx_hdr->rate;
  1372. length = le16_to_cpu(rx_hdr->len);
  1373. /* end-of-frame status and timestamp */
  1374. status = le32_to_cpu(rx_end->status);
  1375. bcn_tmr = le32_to_cpu(rx_end->beacon_timestamp);
  1376. tsf_low = le64_to_cpu(rx_end->timestamp) & 0x0ffffffff;
  1377. tsf = le64_to_cpu(rx_end->timestamp);
  1378. /* signal statistics */
  1379. rssi = rx_stats->rssi;
  1380. agc = rx_stats->agc;
  1381. sig_avg = le16_to_cpu(rx_stats->sig_avg);
  1382. noise_diff = le16_to_cpu(rx_stats->noise_diff);
  1383. to_us = !compare_ether_addr(header->addr1, priv->mac_addr);
  1384. /* if data frame is to us and all is good,
  1385. * (optionally) print summary for only 1 out of every 100 */
  1386. if (to_us && (fc & ~IEEE80211_FCTL_PROTECTED) ==
  1387. (IEEE80211_FCTL_FROMDS | IEEE80211_FTYPE_DATA)) {
  1388. dataframe = 1;
  1389. if (!group100)
  1390. print_summary = 1; /* print each frame */
  1391. else if (priv->framecnt_to_us < 100) {
  1392. priv->framecnt_to_us++;
  1393. print_summary = 0;
  1394. } else {
  1395. priv->framecnt_to_us = 0;
  1396. print_summary = 1;
  1397. hundred = 1;
  1398. }
  1399. } else {
  1400. /* print summary for all other frames */
  1401. print_summary = 1;
  1402. }
  1403. if (print_summary) {
  1404. char *title;
  1405. u32 rate;
  1406. if (hundred)
  1407. title = "100Frames";
  1408. else if (fc & IEEE80211_FCTL_RETRY)
  1409. title = "Retry";
  1410. else if (ieee80211_is_assoc_response(fc))
  1411. title = "AscRsp";
  1412. else if (ieee80211_is_reassoc_response(fc))
  1413. title = "RasRsp";
  1414. else if (ieee80211_is_probe_response(fc)) {
  1415. title = "PrbRsp";
  1416. print_dump = 1; /* dump frame contents */
  1417. } else if (ieee80211_is_beacon(fc)) {
  1418. title = "Beacon";
  1419. print_dump = 1; /* dump frame contents */
  1420. } else if (ieee80211_is_atim(fc))
  1421. title = "ATIM";
  1422. else if (ieee80211_is_auth(fc))
  1423. title = "Auth";
  1424. else if (ieee80211_is_deauth(fc))
  1425. title = "DeAuth";
  1426. else if (ieee80211_is_disassoc(fc))
  1427. title = "DisAssoc";
  1428. else
  1429. title = "Frame";
  1430. rate = iwl_rate_index_from_plcp(rate_sym);
  1431. if (rate == -1)
  1432. rate = 0;
  1433. else
  1434. rate = iwl_rates[rate].ieee / 2;
  1435. /* print frame summary.
  1436. * MAC addresses show just the last byte (for brevity),
  1437. * but you can hack it to show more, if you'd like to. */
  1438. if (dataframe)
  1439. IWL_DEBUG_RX("%s: mhd=0x%04x, dst=0x%02x, "
  1440. "len=%u, rssi=%d, chnl=%d, rate=%u, \n",
  1441. title, fc, header->addr1[5],
  1442. length, rssi, channel, rate);
  1443. else {
  1444. /* src/dst addresses assume managed mode */
  1445. IWL_DEBUG_RX("%s: 0x%04x, dst=0x%02x, "
  1446. "src=0x%02x, rssi=%u, tim=%lu usec, "
  1447. "phy=0x%02x, chnl=%d\n",
  1448. title, fc, header->addr1[5],
  1449. header->addr3[5], rssi,
  1450. tsf_low - priv->scan_start_tsf,
  1451. phy_flags, channel);
  1452. }
  1453. }
  1454. if (print_dump)
  1455. iwl_print_hex_dump(IWL_DL_RX, data, length);
  1456. }
  1457. #endif
  1458. static void iwl_unset_hw_setting(struct iwl_priv *priv)
  1459. {
  1460. if (priv->hw_setting.shared_virt)
  1461. pci_free_consistent(priv->pci_dev,
  1462. sizeof(struct iwl_shared),
  1463. priv->hw_setting.shared_virt,
  1464. priv->hw_setting.shared_phys);
  1465. }
  1466. /**
  1467. * iwl_supported_rate_to_ie - fill in the supported rate in IE field
  1468. *
  1469. * return : set the bit for each supported rate insert in ie
  1470. */
  1471. static u16 iwl_supported_rate_to_ie(u8 *ie, u16 supported_rate,
  1472. u16 basic_rate, int *left)
  1473. {
  1474. u16 ret_rates = 0, bit;
  1475. int i;
  1476. u8 *cnt = ie;
  1477. u8 *rates = ie + 1;
  1478. for (bit = 1, i = 0; i < IWL_RATE_COUNT; i++, bit <<= 1) {
  1479. if (bit & supported_rate) {
  1480. ret_rates |= bit;
  1481. rates[*cnt] = iwl_rates[i].ieee |
  1482. ((bit & basic_rate) ? 0x80 : 0x00);
  1483. (*cnt)++;
  1484. (*left)--;
  1485. if ((*left <= 0) ||
  1486. (*cnt >= IWL_SUPPORTED_RATES_IE_LEN))
  1487. break;
  1488. }
  1489. }
  1490. return ret_rates;
  1491. }
  1492. /**
  1493. * iwl_fill_probe_req - fill in all required fields and IE for probe request
  1494. */
  1495. static u16 iwl_fill_probe_req(struct iwl_priv *priv,
  1496. struct ieee80211_mgmt *frame,
  1497. int left, int is_direct)
  1498. {
  1499. int len = 0;
  1500. u8 *pos = NULL;
  1501. u16 active_rates, ret_rates, cck_rates;
  1502. /* Make sure there is enough space for the probe request,
  1503. * two mandatory IEs and the data */
  1504. left -= 24;
  1505. if (left < 0)
  1506. return 0;
  1507. len += 24;
  1508. frame->frame_control = cpu_to_le16(IEEE80211_STYPE_PROBE_REQ);
  1509. memcpy(frame->da, BROADCAST_ADDR, ETH_ALEN);
  1510. memcpy(frame->sa, priv->mac_addr, ETH_ALEN);
  1511. memcpy(frame->bssid, BROADCAST_ADDR, ETH_ALEN);
  1512. frame->seq_ctrl = 0;
  1513. /* fill in our indirect SSID IE */
  1514. /* ...next IE... */
  1515. left -= 2;
  1516. if (left < 0)
  1517. return 0;
  1518. len += 2;
  1519. pos = &(frame->u.probe_req.variable[0]);
  1520. *pos++ = WLAN_EID_SSID;
  1521. *pos++ = 0;
  1522. /* fill in our direct SSID IE... */
  1523. if (is_direct) {
  1524. /* ...next IE... */
  1525. left -= 2 + priv->essid_len;
  1526. if (left < 0)
  1527. return 0;
  1528. /* ... fill it in... */
  1529. *pos++ = WLAN_EID_SSID;
  1530. *pos++ = priv->essid_len;
  1531. memcpy(pos, priv->essid, priv->essid_len);
  1532. pos += priv->essid_len;
  1533. len += 2 + priv->essid_len;
  1534. }
  1535. /* fill in supported rate */
  1536. /* ...next IE... */
  1537. left -= 2;
  1538. if (left < 0)
  1539. return 0;
  1540. /* ... fill it in... */
  1541. *pos++ = WLAN_EID_SUPP_RATES;
  1542. *pos = 0;
  1543. priv->active_rate = priv->rates_mask;
  1544. active_rates = priv->active_rate;
  1545. priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
  1546. cck_rates = IWL_CCK_RATES_MASK & active_rates;
  1547. ret_rates = iwl_supported_rate_to_ie(pos, cck_rates,
  1548. priv->active_rate_basic, &left);
  1549. active_rates &= ~ret_rates;
  1550. ret_rates = iwl_supported_rate_to_ie(pos, active_rates,
  1551. priv->active_rate_basic, &left);
  1552. active_rates &= ~ret_rates;
  1553. len += 2 + *pos;
  1554. pos += (*pos) + 1;
  1555. if (active_rates == 0)
  1556. goto fill_end;
  1557. /* fill in supported extended rate */
  1558. /* ...next IE... */
  1559. left -= 2;
  1560. if (left < 0)
  1561. return 0;
  1562. /* ... fill it in... */
  1563. *pos++ = WLAN_EID_EXT_SUPP_RATES;
  1564. *pos = 0;
  1565. iwl_supported_rate_to_ie(pos, active_rates,
  1566. priv->active_rate_basic, &left);
  1567. if (*pos > 0)
  1568. len += 2 + *pos;
  1569. fill_end:
  1570. return (u16)len;
  1571. }
  1572. /*
  1573. * QoS support
  1574. */
  1575. #ifdef CONFIG_IWLWIFI_QOS
  1576. static int iwl_send_qos_params_command(struct iwl_priv *priv,
  1577. struct iwl_qosparam_cmd *qos)
  1578. {
  1579. return iwl_send_cmd_pdu(priv, REPLY_QOS_PARAM,
  1580. sizeof(struct iwl_qosparam_cmd), qos);
  1581. }
  1582. static void iwl_reset_qos(struct iwl_priv *priv)
  1583. {
  1584. u16 cw_min = 15;
  1585. u16 cw_max = 1023;
  1586. u8 aifs = 2;
  1587. u8 is_legacy = 0;
  1588. unsigned long flags;
  1589. int i;
  1590. spin_lock_irqsave(&priv->lock, flags);
  1591. priv->qos_data.qos_active = 0;
  1592. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS) {
  1593. if (priv->qos_data.qos_enable)
  1594. priv->qos_data.qos_active = 1;
  1595. if (!(priv->active_rate & 0xfff0)) {
  1596. cw_min = 31;
  1597. is_legacy = 1;
  1598. }
  1599. } else if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  1600. if (priv->qos_data.qos_enable)
  1601. priv->qos_data.qos_active = 1;
  1602. } else if (!(priv->staging_rxon.flags & RXON_FLG_SHORT_SLOT_MSK)) {
  1603. cw_min = 31;
  1604. is_legacy = 1;
  1605. }
  1606. if (priv->qos_data.qos_active)
  1607. aifs = 3;
  1608. priv->qos_data.def_qos_parm.ac[0].cw_min = cpu_to_le16(cw_min);
  1609. priv->qos_data.def_qos_parm.ac[0].cw_max = cpu_to_le16(cw_max);
  1610. priv->qos_data.def_qos_parm.ac[0].aifsn = aifs;
  1611. priv->qos_data.def_qos_parm.ac[0].edca_txop = 0;
  1612. priv->qos_data.def_qos_parm.ac[0].reserved1 = 0;
  1613. if (priv->qos_data.qos_active) {
  1614. i = 1;
  1615. priv->qos_data.def_qos_parm.ac[i].cw_min = cpu_to_le16(cw_min);
  1616. priv->qos_data.def_qos_parm.ac[i].cw_max = cpu_to_le16(cw_max);
  1617. priv->qos_data.def_qos_parm.ac[i].aifsn = 7;
  1618. priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
  1619. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  1620. i = 2;
  1621. priv->qos_data.def_qos_parm.ac[i].cw_min =
  1622. cpu_to_le16((cw_min + 1) / 2 - 1);
  1623. priv->qos_data.def_qos_parm.ac[i].cw_max =
  1624. cpu_to_le16(cw_max);
  1625. priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
  1626. if (is_legacy)
  1627. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  1628. cpu_to_le16(6016);
  1629. else
  1630. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  1631. cpu_to_le16(3008);
  1632. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  1633. i = 3;
  1634. priv->qos_data.def_qos_parm.ac[i].cw_min =
  1635. cpu_to_le16((cw_min + 1) / 4 - 1);
  1636. priv->qos_data.def_qos_parm.ac[i].cw_max =
  1637. cpu_to_le16((cw_max + 1) / 2 - 1);
  1638. priv->qos_data.def_qos_parm.ac[i].aifsn = 2;
  1639. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  1640. if (is_legacy)
  1641. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  1642. cpu_to_le16(3264);
  1643. else
  1644. priv->qos_data.def_qos_parm.ac[i].edca_txop =
  1645. cpu_to_le16(1504);
  1646. } else {
  1647. for (i = 1; i < 4; i++) {
  1648. priv->qos_data.def_qos_parm.ac[i].cw_min =
  1649. cpu_to_le16(cw_min);
  1650. priv->qos_data.def_qos_parm.ac[i].cw_max =
  1651. cpu_to_le16(cw_max);
  1652. priv->qos_data.def_qos_parm.ac[i].aifsn = aifs;
  1653. priv->qos_data.def_qos_parm.ac[i].edca_txop = 0;
  1654. priv->qos_data.def_qos_parm.ac[i].reserved1 = 0;
  1655. }
  1656. }
  1657. IWL_DEBUG_QOS("set QoS to default \n");
  1658. spin_unlock_irqrestore(&priv->lock, flags);
  1659. }
  1660. static void iwl_activate_qos(struct iwl_priv *priv, u8 force)
  1661. {
  1662. unsigned long flags;
  1663. if (priv == NULL)
  1664. return;
  1665. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  1666. return;
  1667. if (!priv->qos_data.qos_enable)
  1668. return;
  1669. spin_lock_irqsave(&priv->lock, flags);
  1670. priv->qos_data.def_qos_parm.qos_flags = 0;
  1671. if (priv->qos_data.qos_cap.q_AP.queue_request &&
  1672. !priv->qos_data.qos_cap.q_AP.txop_request)
  1673. priv->qos_data.def_qos_parm.qos_flags |=
  1674. QOS_PARAM_FLG_TXOP_TYPE_MSK;
  1675. if (priv->qos_data.qos_active)
  1676. priv->qos_data.def_qos_parm.qos_flags |=
  1677. QOS_PARAM_FLG_UPDATE_EDCA_MSK;
  1678. spin_unlock_irqrestore(&priv->lock, flags);
  1679. if (force || iwl_is_associated(priv)) {
  1680. IWL_DEBUG_QOS("send QoS cmd with Qos active %d \n",
  1681. priv->qos_data.qos_active);
  1682. iwl_send_qos_params_command(priv,
  1683. &(priv->qos_data.def_qos_parm));
  1684. }
  1685. }
  1686. #endif /* CONFIG_IWLWIFI_QOS */
  1687. /*
  1688. * Power management (not Tx power!) functions
  1689. */
  1690. #define MSEC_TO_USEC 1024
  1691. #define NOSLP __constant_cpu_to_le32(0)
  1692. #define SLP IWL_POWER_DRIVER_ALLOW_SLEEP_MSK
  1693. #define SLP_TIMEOUT(T) __constant_cpu_to_le32((T) * MSEC_TO_USEC)
  1694. #define SLP_VEC(X0, X1, X2, X3, X4) {__constant_cpu_to_le32(X0), \
  1695. __constant_cpu_to_le32(X1), \
  1696. __constant_cpu_to_le32(X2), \
  1697. __constant_cpu_to_le32(X3), \
  1698. __constant_cpu_to_le32(X4)}
  1699. /* default power management (not Tx power) table values */
  1700. /* for tim 0-10 */
  1701. static struct iwl_power_vec_entry range_0[IWL_POWER_AC] = {
  1702. {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  1703. {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500), SLP_VEC(1, 2, 3, 4, 4)}, 0},
  1704. {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300), SLP_VEC(2, 4, 6, 7, 7)}, 0},
  1705. {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100), SLP_VEC(2, 6, 9, 9, 10)}, 0},
  1706. {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 10)}, 1},
  1707. {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25), SLP_VEC(4, 7, 10, 10, 10)}, 1}
  1708. };
  1709. /* for tim > 10 */
  1710. static struct iwl_power_vec_entry range_1[IWL_POWER_AC] = {
  1711. {{NOSLP, SLP_TIMEOUT(0), SLP_TIMEOUT(0), SLP_VEC(0, 0, 0, 0, 0)}, 0},
  1712. {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(500),
  1713. SLP_VEC(1, 2, 3, 4, 0xFF)}, 0},
  1714. {{SLP, SLP_TIMEOUT(200), SLP_TIMEOUT(300),
  1715. SLP_VEC(2, 4, 6, 7, 0xFF)}, 0},
  1716. {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(100),
  1717. SLP_VEC(2, 6, 9, 9, 0xFF)}, 0},
  1718. {{SLP, SLP_TIMEOUT(50), SLP_TIMEOUT(25), SLP_VEC(2, 7, 9, 9, 0xFF)}, 0},
  1719. {{SLP, SLP_TIMEOUT(25), SLP_TIMEOUT(25),
  1720. SLP_VEC(4, 7, 10, 10, 0xFF)}, 0}
  1721. };
  1722. int iwl_power_init_handle(struct iwl_priv *priv)
  1723. {
  1724. int rc = 0, i;
  1725. struct iwl_power_mgr *pow_data;
  1726. int size = sizeof(struct iwl_power_vec_entry) * IWL_POWER_AC;
  1727. u16 pci_pm;
  1728. IWL_DEBUG_POWER("Initialize power \n");
  1729. pow_data = &(priv->power_data);
  1730. memset(pow_data, 0, sizeof(*pow_data));
  1731. pow_data->active_index = IWL_POWER_RANGE_0;
  1732. pow_data->dtim_val = 0xffff;
  1733. memcpy(&pow_data->pwr_range_0[0], &range_0[0], size);
  1734. memcpy(&pow_data->pwr_range_1[0], &range_1[0], size);
  1735. rc = pci_read_config_word(priv->pci_dev, PCI_LINK_CTRL, &pci_pm);
  1736. if (rc != 0)
  1737. return 0;
  1738. else {
  1739. struct iwl_powertable_cmd *cmd;
  1740. IWL_DEBUG_POWER("adjust power command flags\n");
  1741. for (i = 0; i < IWL_POWER_AC; i++) {
  1742. cmd = &pow_data->pwr_range_0[i].cmd;
  1743. if (pci_pm & 0x1)
  1744. cmd->flags &= ~IWL_POWER_PCI_PM_MSK;
  1745. else
  1746. cmd->flags |= IWL_POWER_PCI_PM_MSK;
  1747. }
  1748. }
  1749. return rc;
  1750. }
  1751. static int iwl_update_power_cmd(struct iwl_priv *priv,
  1752. struct iwl_powertable_cmd *cmd, u32 mode)
  1753. {
  1754. int rc = 0, i;
  1755. u8 skip;
  1756. u32 max_sleep = 0;
  1757. struct iwl_power_vec_entry *range;
  1758. u8 period = 0;
  1759. struct iwl_power_mgr *pow_data;
  1760. if (mode > IWL_POWER_INDEX_5) {
  1761. IWL_DEBUG_POWER("Error invalid power mode \n");
  1762. return -1;
  1763. }
  1764. pow_data = &(priv->power_data);
  1765. if (pow_data->active_index == IWL_POWER_RANGE_0)
  1766. range = &pow_data->pwr_range_0[0];
  1767. else
  1768. range = &pow_data->pwr_range_1[1];
  1769. memcpy(cmd, &range[mode].cmd, sizeof(struct iwl_powertable_cmd));
  1770. #ifdef IWL_MAC80211_DISABLE
  1771. if (priv->assoc_network != NULL) {
  1772. unsigned long flags;
  1773. period = priv->assoc_network->tim.tim_period;
  1774. }
  1775. #endif /*IWL_MAC80211_DISABLE */
  1776. skip = range[mode].no_dtim;
  1777. if (period == 0) {
  1778. period = 1;
  1779. skip = 0;
  1780. }
  1781. if (skip == 0) {
  1782. max_sleep = period;
  1783. cmd->flags &= ~IWL_POWER_SLEEP_OVER_DTIM_MSK;
  1784. } else {
  1785. __le32 slp_itrvl = cmd->sleep_interval[IWL_POWER_VEC_SIZE - 1];
  1786. max_sleep = (le32_to_cpu(slp_itrvl) / period) * period;
  1787. cmd->flags |= IWL_POWER_SLEEP_OVER_DTIM_MSK;
  1788. }
  1789. for (i = 0; i < IWL_POWER_VEC_SIZE; i++) {
  1790. if (le32_to_cpu(cmd->sleep_interval[i]) > max_sleep)
  1791. cmd->sleep_interval[i] = cpu_to_le32(max_sleep);
  1792. }
  1793. IWL_DEBUG_POWER("Flags value = 0x%08X\n", cmd->flags);
  1794. IWL_DEBUG_POWER("Tx timeout = %u\n", le32_to_cpu(cmd->tx_data_timeout));
  1795. IWL_DEBUG_POWER("Rx timeout = %u\n", le32_to_cpu(cmd->rx_data_timeout));
  1796. IWL_DEBUG_POWER("Sleep interval vector = { %d , %d , %d , %d , %d }\n",
  1797. le32_to_cpu(cmd->sleep_interval[0]),
  1798. le32_to_cpu(cmd->sleep_interval[1]),
  1799. le32_to_cpu(cmd->sleep_interval[2]),
  1800. le32_to_cpu(cmd->sleep_interval[3]),
  1801. le32_to_cpu(cmd->sleep_interval[4]));
  1802. return rc;
  1803. }
  1804. static int iwl_send_power_mode(struct iwl_priv *priv, u32 mode)
  1805. {
  1806. u32 final_mode = mode;
  1807. int rc;
  1808. struct iwl_powertable_cmd cmd;
  1809. /* If on battery, set to 3,
  1810. * if plugged into AC power, set to CAM ("continuously aware mode"),
  1811. * else user level */
  1812. switch (mode) {
  1813. case IWL_POWER_BATTERY:
  1814. final_mode = IWL_POWER_INDEX_3;
  1815. break;
  1816. case IWL_POWER_AC:
  1817. final_mode = IWL_POWER_MODE_CAM;
  1818. break;
  1819. default:
  1820. final_mode = mode;
  1821. break;
  1822. }
  1823. iwl_update_power_cmd(priv, &cmd, final_mode);
  1824. rc = iwl_send_cmd_pdu(priv, POWER_TABLE_CMD, sizeof(cmd), &cmd);
  1825. if (final_mode == IWL_POWER_MODE_CAM)
  1826. clear_bit(STATUS_POWER_PMI, &priv->status);
  1827. else
  1828. set_bit(STATUS_POWER_PMI, &priv->status);
  1829. return rc;
  1830. }
  1831. int iwl_is_network_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
  1832. {
  1833. /* Filter incoming packets to determine if they are targeted toward
  1834. * this network, discarding packets coming from ourselves */
  1835. switch (priv->iw_mode) {
  1836. case IEEE80211_IF_TYPE_IBSS: /* Header: Dest. | Source | BSSID */
  1837. /* packets from our adapter are dropped (echo) */
  1838. if (!compare_ether_addr(header->addr2, priv->mac_addr))
  1839. return 0;
  1840. /* {broad,multi}cast packets to our IBSS go through */
  1841. if (is_multicast_ether_addr(header->addr1))
  1842. return !compare_ether_addr(header->addr3, priv->bssid);
  1843. /* packets to our adapter go through */
  1844. return !compare_ether_addr(header->addr1, priv->mac_addr);
  1845. case IEEE80211_IF_TYPE_STA: /* Header: Dest. | AP{BSSID} | Source */
  1846. /* packets from our adapter are dropped (echo) */
  1847. if (!compare_ether_addr(header->addr3, priv->mac_addr))
  1848. return 0;
  1849. /* {broad,multi}cast packets to our BSS go through */
  1850. if (is_multicast_ether_addr(header->addr1))
  1851. return !compare_ether_addr(header->addr2, priv->bssid);
  1852. /* packets to our adapter go through */
  1853. return !compare_ether_addr(header->addr1, priv->mac_addr);
  1854. }
  1855. return 1;
  1856. }
  1857. #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
  1858. const char *iwl_get_tx_fail_reason(u32 status)
  1859. {
  1860. switch (status & TX_STATUS_MSK) {
  1861. case TX_STATUS_SUCCESS:
  1862. return "SUCCESS";
  1863. TX_STATUS_ENTRY(SHORT_LIMIT);
  1864. TX_STATUS_ENTRY(LONG_LIMIT);
  1865. TX_STATUS_ENTRY(FIFO_UNDERRUN);
  1866. TX_STATUS_ENTRY(MGMNT_ABORT);
  1867. TX_STATUS_ENTRY(NEXT_FRAG);
  1868. TX_STATUS_ENTRY(LIFE_EXPIRE);
  1869. TX_STATUS_ENTRY(DEST_PS);
  1870. TX_STATUS_ENTRY(ABORTED);
  1871. TX_STATUS_ENTRY(BT_RETRY);
  1872. TX_STATUS_ENTRY(STA_INVALID);
  1873. TX_STATUS_ENTRY(FRAG_DROPPED);
  1874. TX_STATUS_ENTRY(TID_DISABLE);
  1875. TX_STATUS_ENTRY(FRAME_FLUSHED);
  1876. TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
  1877. TX_STATUS_ENTRY(TX_LOCKED);
  1878. TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
  1879. }
  1880. return "UNKNOWN";
  1881. }
  1882. /**
  1883. * iwl_scan_cancel - Cancel any currently executing HW scan
  1884. *
  1885. * NOTE: priv->mutex is not required before calling this function
  1886. */
  1887. static int iwl_scan_cancel(struct iwl_priv *priv)
  1888. {
  1889. if (!test_bit(STATUS_SCAN_HW, &priv->status)) {
  1890. clear_bit(STATUS_SCANNING, &priv->status);
  1891. return 0;
  1892. }
  1893. if (test_bit(STATUS_SCANNING, &priv->status)) {
  1894. if (!test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  1895. IWL_DEBUG_SCAN("Queuing scan abort.\n");
  1896. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  1897. queue_work(priv->workqueue, &priv->abort_scan);
  1898. } else
  1899. IWL_DEBUG_SCAN("Scan abort already in progress.\n");
  1900. return test_bit(STATUS_SCANNING, &priv->status);
  1901. }
  1902. return 0;
  1903. }
  1904. /**
  1905. * iwl_scan_cancel_timeout - Cancel any currently executing HW scan
  1906. * @ms: amount of time to wait (in milliseconds) for scan to abort
  1907. *
  1908. * NOTE: priv->mutex must be held before calling this function
  1909. */
  1910. static int iwl_scan_cancel_timeout(struct iwl_priv *priv, unsigned long ms)
  1911. {
  1912. unsigned long now = jiffies;
  1913. int ret;
  1914. ret = iwl_scan_cancel(priv);
  1915. if (ret && ms) {
  1916. mutex_unlock(&priv->mutex);
  1917. while (!time_after(jiffies, now + msecs_to_jiffies(ms)) &&
  1918. test_bit(STATUS_SCANNING, &priv->status))
  1919. msleep(1);
  1920. mutex_lock(&priv->mutex);
  1921. return test_bit(STATUS_SCANNING, &priv->status);
  1922. }
  1923. return ret;
  1924. }
  1925. static void iwl_sequence_reset(struct iwl_priv *priv)
  1926. {
  1927. /* Reset ieee stats */
  1928. /* We don't reset the net_device_stats (ieee->stats) on
  1929. * re-association */
  1930. priv->last_seq_num = -1;
  1931. priv->last_frag_num = -1;
  1932. priv->last_packet_time = 0;
  1933. iwl_scan_cancel(priv);
  1934. }
  1935. #define MAX_UCODE_BEACON_INTERVAL 1024
  1936. #define INTEL_CONN_LISTEN_INTERVAL __constant_cpu_to_le16(0xA)
  1937. static __le16 iwl_adjust_beacon_interval(u16 beacon_val)
  1938. {
  1939. u16 new_val = 0;
  1940. u16 beacon_factor = 0;
  1941. beacon_factor =
  1942. (beacon_val + MAX_UCODE_BEACON_INTERVAL)
  1943. / MAX_UCODE_BEACON_INTERVAL;
  1944. new_val = beacon_val / beacon_factor;
  1945. return cpu_to_le16(new_val);
  1946. }
  1947. static void iwl_setup_rxon_timing(struct iwl_priv *priv)
  1948. {
  1949. u64 interval_tm_unit;
  1950. u64 tsf, result;
  1951. unsigned long flags;
  1952. struct ieee80211_conf *conf = NULL;
  1953. u16 beacon_int = 0;
  1954. conf = ieee80211_get_hw_conf(priv->hw);
  1955. spin_lock_irqsave(&priv->lock, flags);
  1956. priv->rxon_timing.timestamp.dw[1] = cpu_to_le32(priv->timestamp1);
  1957. priv->rxon_timing.timestamp.dw[0] = cpu_to_le32(priv->timestamp0);
  1958. priv->rxon_timing.listen_interval = INTEL_CONN_LISTEN_INTERVAL;
  1959. tsf = priv->timestamp1;
  1960. tsf = ((tsf << 32) | priv->timestamp0);
  1961. beacon_int = priv->beacon_int;
  1962. spin_unlock_irqrestore(&priv->lock, flags);
  1963. if (priv->iw_mode == IEEE80211_IF_TYPE_STA) {
  1964. if (beacon_int == 0) {
  1965. priv->rxon_timing.beacon_interval = cpu_to_le16(100);
  1966. priv->rxon_timing.beacon_init_val = cpu_to_le32(102400);
  1967. } else {
  1968. priv->rxon_timing.beacon_interval =
  1969. cpu_to_le16(beacon_int);
  1970. priv->rxon_timing.beacon_interval =
  1971. iwl_adjust_beacon_interval(
  1972. le16_to_cpu(priv->rxon_timing.beacon_interval));
  1973. }
  1974. priv->rxon_timing.atim_window = 0;
  1975. } else {
  1976. priv->rxon_timing.beacon_interval =
  1977. iwl_adjust_beacon_interval(conf->beacon_int);
  1978. /* TODO: we need to get atim_window from upper stack
  1979. * for now we set to 0 */
  1980. priv->rxon_timing.atim_window = 0;
  1981. }
  1982. interval_tm_unit =
  1983. (le16_to_cpu(priv->rxon_timing.beacon_interval) * 1024);
  1984. result = do_div(tsf, interval_tm_unit);
  1985. priv->rxon_timing.beacon_init_val =
  1986. cpu_to_le32((u32) ((u64) interval_tm_unit - result));
  1987. IWL_DEBUG_ASSOC
  1988. ("beacon interval %d beacon timer %d beacon tim %d\n",
  1989. le16_to_cpu(priv->rxon_timing.beacon_interval),
  1990. le32_to_cpu(priv->rxon_timing.beacon_init_val),
  1991. le16_to_cpu(priv->rxon_timing.atim_window));
  1992. }
  1993. static int iwl_scan_initiate(struct iwl_priv *priv)
  1994. {
  1995. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  1996. IWL_ERROR("APs don't scan.\n");
  1997. return 0;
  1998. }
  1999. if (!iwl_is_ready_rf(priv)) {
  2000. IWL_DEBUG_SCAN("Aborting scan due to not ready.\n");
  2001. return -EIO;
  2002. }
  2003. if (test_bit(STATUS_SCANNING, &priv->status)) {
  2004. IWL_DEBUG_SCAN("Scan already in progress.\n");
  2005. return -EAGAIN;
  2006. }
  2007. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  2008. IWL_DEBUG_SCAN("Scan request while abort pending. "
  2009. "Queuing.\n");
  2010. return -EAGAIN;
  2011. }
  2012. IWL_DEBUG_INFO("Starting scan...\n");
  2013. priv->scan_bands = 2;
  2014. set_bit(STATUS_SCANNING, &priv->status);
  2015. priv->scan_start = jiffies;
  2016. priv->scan_pass_start = priv->scan_start;
  2017. queue_work(priv->workqueue, &priv->request_scan);
  2018. return 0;
  2019. }
  2020. static int iwl_set_rxon_hwcrypto(struct iwl_priv *priv, int hw_decrypt)
  2021. {
  2022. struct iwl_rxon_cmd *rxon = &priv->staging_rxon;
  2023. if (hw_decrypt)
  2024. rxon->filter_flags &= ~RXON_FILTER_DIS_DECRYPT_MSK;
  2025. else
  2026. rxon->filter_flags |= RXON_FILTER_DIS_DECRYPT_MSK;
  2027. return 0;
  2028. }
  2029. static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode)
  2030. {
  2031. if (phymode == MODE_IEEE80211A) {
  2032. priv->staging_rxon.flags &=
  2033. ~(RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK
  2034. | RXON_FLG_CCK_MSK);
  2035. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  2036. } else {
  2037. /* Copied from iwl_bg_post_associate() */
  2038. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
  2039. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  2040. else
  2041. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  2042. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
  2043. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  2044. priv->staging_rxon.flags |= RXON_FLG_BAND_24G_MSK;
  2045. priv->staging_rxon.flags |= RXON_FLG_AUTO_DETECT_MSK;
  2046. priv->staging_rxon.flags &= ~RXON_FLG_CCK_MSK;
  2047. }
  2048. }
  2049. /*
  2050. * initialize rxon structure with default values from eeprom
  2051. */
  2052. static void iwl_connection_init_rx_config(struct iwl_priv *priv)
  2053. {
  2054. const struct iwl_channel_info *ch_info;
  2055. memset(&priv->staging_rxon, 0, sizeof(priv->staging_rxon));
  2056. switch (priv->iw_mode) {
  2057. case IEEE80211_IF_TYPE_AP:
  2058. priv->staging_rxon.dev_type = RXON_DEV_TYPE_AP;
  2059. break;
  2060. case IEEE80211_IF_TYPE_STA:
  2061. priv->staging_rxon.dev_type = RXON_DEV_TYPE_ESS;
  2062. priv->staging_rxon.filter_flags = RXON_FILTER_ACCEPT_GRP_MSK;
  2063. break;
  2064. case IEEE80211_IF_TYPE_IBSS:
  2065. priv->staging_rxon.dev_type = RXON_DEV_TYPE_IBSS;
  2066. priv->staging_rxon.flags = RXON_FLG_SHORT_PREAMBLE_MSK;
  2067. priv->staging_rxon.filter_flags = RXON_FILTER_BCON_AWARE_MSK |
  2068. RXON_FILTER_ACCEPT_GRP_MSK;
  2069. break;
  2070. case IEEE80211_IF_TYPE_MNTR:
  2071. priv->staging_rxon.dev_type = RXON_DEV_TYPE_SNIFFER;
  2072. priv->staging_rxon.filter_flags = RXON_FILTER_PROMISC_MSK |
  2073. RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_ACCEPT_GRP_MSK;
  2074. break;
  2075. }
  2076. #if 0
  2077. /* TODO: Figure out when short_preamble would be set and cache from
  2078. * that */
  2079. if (!hw_to_local(priv->hw)->short_preamble)
  2080. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  2081. else
  2082. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  2083. #endif
  2084. ch_info = iwl_get_channel_info(priv, priv->phymode,
  2085. le16_to_cpu(priv->staging_rxon.channel));
  2086. if (!ch_info)
  2087. ch_info = &priv->channel_info[0];
  2088. /*
  2089. * in some case A channels are all non IBSS
  2090. * in this case force B/G channel
  2091. */
  2092. if ((priv->iw_mode == IEEE80211_IF_TYPE_IBSS) &&
  2093. !(is_channel_ibss(ch_info)))
  2094. ch_info = &priv->channel_info[0];
  2095. priv->staging_rxon.channel = cpu_to_le16(ch_info->channel);
  2096. if (is_channel_a_band(ch_info))
  2097. priv->phymode = MODE_IEEE80211A;
  2098. else
  2099. priv->phymode = MODE_IEEE80211G;
  2100. iwl_set_flags_for_phymode(priv, priv->phymode);
  2101. priv->staging_rxon.ofdm_basic_rates =
  2102. (IWL_OFDM_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  2103. priv->staging_rxon.cck_basic_rates =
  2104. (IWL_CCK_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  2105. }
  2106. static int iwl_set_mode(struct iwl_priv *priv, int mode)
  2107. {
  2108. if (!iwl_is_ready_rf(priv))
  2109. return -EAGAIN;
  2110. if (mode == IEEE80211_IF_TYPE_IBSS) {
  2111. const struct iwl_channel_info *ch_info;
  2112. ch_info = iwl_get_channel_info(priv,
  2113. priv->phymode,
  2114. le16_to_cpu(priv->staging_rxon.channel));
  2115. if (!ch_info || !is_channel_ibss(ch_info)) {
  2116. IWL_ERROR("channel %d not IBSS channel\n",
  2117. le16_to_cpu(priv->staging_rxon.channel));
  2118. return -EINVAL;
  2119. }
  2120. }
  2121. cancel_delayed_work(&priv->scan_check);
  2122. if (iwl_scan_cancel_timeout(priv, 100)) {
  2123. IWL_WARNING("Aborted scan still in progress after 100ms\n");
  2124. IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
  2125. return -EAGAIN;
  2126. }
  2127. priv->iw_mode = mode;
  2128. iwl_connection_init_rx_config(priv);
  2129. memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
  2130. iwl_clear_stations_table(priv);
  2131. iwl_commit_rxon(priv);
  2132. return 0;
  2133. }
  2134. static void iwl_build_tx_cmd_hwcrypto(struct iwl_priv *priv,
  2135. struct ieee80211_tx_control *ctl,
  2136. struct iwl_cmd *cmd,
  2137. struct sk_buff *skb_frag,
  2138. int last_frag)
  2139. {
  2140. struct iwl_hw_key *keyinfo = &priv->stations[ctl->key_idx].keyinfo;
  2141. switch (keyinfo->alg) {
  2142. case ALG_CCMP:
  2143. cmd->cmd.tx.sec_ctl = TX_CMD_SEC_CCM;
  2144. memcpy(cmd->cmd.tx.key, keyinfo->key, keyinfo->keylen);
  2145. IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
  2146. break;
  2147. case ALG_TKIP:
  2148. #if 0
  2149. cmd->cmd.tx.sec_ctl = TX_CMD_SEC_TKIP;
  2150. if (last_frag)
  2151. memcpy(cmd->cmd.tx.tkip_mic.byte, skb_frag->tail - 8,
  2152. 8);
  2153. else
  2154. memset(cmd->cmd.tx.tkip_mic.byte, 0, 8);
  2155. #endif
  2156. break;
  2157. case ALG_WEP:
  2158. cmd->cmd.tx.sec_ctl = TX_CMD_SEC_WEP |
  2159. (ctl->key_idx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT;
  2160. if (keyinfo->keylen == 13)
  2161. cmd->cmd.tx.sec_ctl |= TX_CMD_SEC_KEY128;
  2162. memcpy(&cmd->cmd.tx.key[3], keyinfo->key, keyinfo->keylen);
  2163. IWL_DEBUG_TX("Configuring packet for WEP encryption "
  2164. "with key %d\n", ctl->key_idx);
  2165. break;
  2166. default:
  2167. printk(KERN_ERR "Unknown encode alg %d\n", keyinfo->alg);
  2168. break;
  2169. }
  2170. }
  2171. /*
  2172. * handle build REPLY_TX command notification.
  2173. */
  2174. static void iwl_build_tx_cmd_basic(struct iwl_priv *priv,
  2175. struct iwl_cmd *cmd,
  2176. struct ieee80211_tx_control *ctrl,
  2177. struct ieee80211_hdr *hdr,
  2178. int is_unicast, u8 std_id)
  2179. {
  2180. __le16 *qc;
  2181. u16 fc = le16_to_cpu(hdr->frame_control);
  2182. __le32 tx_flags = cmd->cmd.tx.tx_flags;
  2183. cmd->cmd.tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  2184. if (!(ctrl->flags & IEEE80211_TXCTL_NO_ACK)) {
  2185. tx_flags |= TX_CMD_FLG_ACK_MSK;
  2186. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT)
  2187. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  2188. if (ieee80211_is_probe_response(fc) &&
  2189. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  2190. tx_flags |= TX_CMD_FLG_TSF_MSK;
  2191. } else {
  2192. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  2193. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  2194. }
  2195. cmd->cmd.tx.sta_id = std_id;
  2196. if (ieee80211_get_morefrag(hdr))
  2197. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  2198. qc = ieee80211_get_qos_ctrl(hdr);
  2199. if (qc) {
  2200. cmd->cmd.tx.tid_tspec = (u8) (le16_to_cpu(*qc) & 0xf);
  2201. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  2202. } else
  2203. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  2204. if (ctrl->flags & IEEE80211_TXCTL_USE_RTS_CTS) {
  2205. tx_flags |= TX_CMD_FLG_RTS_MSK;
  2206. tx_flags &= ~TX_CMD_FLG_CTS_MSK;
  2207. } else if (ctrl->flags & IEEE80211_TXCTL_USE_CTS_PROTECT) {
  2208. tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  2209. tx_flags |= TX_CMD_FLG_CTS_MSK;
  2210. }
  2211. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  2212. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  2213. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  2214. if ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_MGMT) {
  2215. if ((fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_ASSOC_REQ ||
  2216. (fc & IEEE80211_FCTL_STYPE) == IEEE80211_STYPE_REASSOC_REQ)
  2217. cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(3);
  2218. else
  2219. cmd->cmd.tx.timeout.pm_frame_timeout = cpu_to_le16(2);
  2220. } else
  2221. cmd->cmd.tx.timeout.pm_frame_timeout = 0;
  2222. cmd->cmd.tx.driver_txop = 0;
  2223. cmd->cmd.tx.tx_flags = tx_flags;
  2224. cmd->cmd.tx.next_frame_len = 0;
  2225. }
  2226. static int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
  2227. {
  2228. int sta_id;
  2229. u16 fc = le16_to_cpu(hdr->frame_control);
  2230. /* If this frame is broadcast or not data then use the broadcast
  2231. * station id */
  2232. if (((fc & IEEE80211_FCTL_FTYPE) != IEEE80211_FTYPE_DATA) ||
  2233. is_multicast_ether_addr(hdr->addr1))
  2234. return priv->hw_setting.bcast_sta_id;
  2235. switch (priv->iw_mode) {
  2236. /* If this frame is part of a BSS network (we're a station), then
  2237. * we use the AP's station id */
  2238. case IEEE80211_IF_TYPE_STA:
  2239. return IWL_AP_ID;
  2240. /* If we are an AP, then find the station, or use BCAST */
  2241. case IEEE80211_IF_TYPE_AP:
  2242. sta_id = iwl_hw_find_station(priv, hdr->addr1);
  2243. if (sta_id != IWL_INVALID_STATION)
  2244. return sta_id;
  2245. return priv->hw_setting.bcast_sta_id;
  2246. /* If this frame is part of a IBSS network, then we use the
  2247. * target specific station id */
  2248. case IEEE80211_IF_TYPE_IBSS: {
  2249. DECLARE_MAC_BUF(mac);
  2250. sta_id = iwl_hw_find_station(priv, hdr->addr1);
  2251. if (sta_id != IWL_INVALID_STATION)
  2252. return sta_id;
  2253. sta_id = iwl_add_station(priv, hdr->addr1, 0, CMD_ASYNC);
  2254. if (sta_id != IWL_INVALID_STATION)
  2255. return sta_id;
  2256. IWL_DEBUG_DROP("Station %s not in station map. "
  2257. "Defaulting to broadcast...\n",
  2258. print_mac(mac, hdr->addr1));
  2259. iwl_print_hex_dump(IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
  2260. return priv->hw_setting.bcast_sta_id;
  2261. }
  2262. default:
  2263. IWL_WARNING("Unknown mode of operation: %d", priv->iw_mode);
  2264. return priv->hw_setting.bcast_sta_id;
  2265. }
  2266. }
  2267. /*
  2268. * start REPLY_TX command process
  2269. */
  2270. static int iwl_tx_skb(struct iwl_priv *priv,
  2271. struct sk_buff *skb, struct ieee80211_tx_control *ctl)
  2272. {
  2273. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  2274. struct iwl_tfd_frame *tfd;
  2275. u32 *control_flags;
  2276. int txq_id = ctl->queue;
  2277. struct iwl_tx_queue *txq = NULL;
  2278. struct iwl_queue *q = NULL;
  2279. dma_addr_t phys_addr;
  2280. dma_addr_t txcmd_phys;
  2281. struct iwl_cmd *out_cmd = NULL;
  2282. u16 len, idx, len_org;
  2283. u8 id, hdr_len, unicast;
  2284. u8 sta_id;
  2285. u16 seq_number = 0;
  2286. u16 fc;
  2287. __le16 *qc;
  2288. u8 wait_write_ptr = 0;
  2289. unsigned long flags;
  2290. int rc;
  2291. spin_lock_irqsave(&priv->lock, flags);
  2292. if (iwl_is_rfkill(priv)) {
  2293. IWL_DEBUG_DROP("Dropping - RF KILL\n");
  2294. goto drop_unlock;
  2295. }
  2296. if (!priv->interface_id) {
  2297. IWL_DEBUG_DROP("Dropping - !priv->interface_id\n");
  2298. goto drop_unlock;
  2299. }
  2300. if ((ctl->tx_rate & 0xFF) == IWL_INVALID_RATE) {
  2301. IWL_ERROR("ERROR: No TX rate available.\n");
  2302. goto drop_unlock;
  2303. }
  2304. unicast = !is_multicast_ether_addr(hdr->addr1);
  2305. id = 0;
  2306. fc = le16_to_cpu(hdr->frame_control);
  2307. #ifdef CONFIG_IWLWIFI_DEBUG
  2308. if (ieee80211_is_auth(fc))
  2309. IWL_DEBUG_TX("Sending AUTH frame\n");
  2310. else if (ieee80211_is_assoc_request(fc))
  2311. IWL_DEBUG_TX("Sending ASSOC frame\n");
  2312. else if (ieee80211_is_reassoc_request(fc))
  2313. IWL_DEBUG_TX("Sending REASSOC frame\n");
  2314. #endif
  2315. if (!iwl_is_associated(priv) &&
  2316. ((fc & IEEE80211_FCTL_FTYPE) == IEEE80211_FTYPE_DATA)) {
  2317. IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
  2318. goto drop_unlock;
  2319. }
  2320. spin_unlock_irqrestore(&priv->lock, flags);
  2321. hdr_len = ieee80211_get_hdrlen(fc);
  2322. sta_id = iwl_get_sta_id(priv, hdr);
  2323. if (sta_id == IWL_INVALID_STATION) {
  2324. DECLARE_MAC_BUF(mac);
  2325. IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
  2326. print_mac(mac, hdr->addr1));
  2327. goto drop;
  2328. }
  2329. IWL_DEBUG_RATE("station Id %d\n", sta_id);
  2330. qc = ieee80211_get_qos_ctrl(hdr);
  2331. if (qc) {
  2332. u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
  2333. seq_number = priv->stations[sta_id].tid[tid].seq_number &
  2334. IEEE80211_SCTL_SEQ;
  2335. hdr->seq_ctrl = cpu_to_le16(seq_number) |
  2336. (hdr->seq_ctrl &
  2337. __constant_cpu_to_le16(IEEE80211_SCTL_FRAG));
  2338. seq_number += 0x10;
  2339. }
  2340. txq = &priv->txq[txq_id];
  2341. q = &txq->q;
  2342. spin_lock_irqsave(&priv->lock, flags);
  2343. tfd = &txq->bd[q->write_ptr];
  2344. memset(tfd, 0, sizeof(*tfd));
  2345. control_flags = (u32 *) tfd;
  2346. idx = get_cmd_index(q, q->write_ptr, 0);
  2347. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  2348. txq->txb[q->write_ptr].skb[0] = skb;
  2349. memcpy(&(txq->txb[q->write_ptr].status.control),
  2350. ctl, sizeof(struct ieee80211_tx_control));
  2351. out_cmd = &txq->cmd[idx];
  2352. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  2353. memset(&out_cmd->cmd.tx, 0, sizeof(out_cmd->cmd.tx));
  2354. out_cmd->hdr.cmd = REPLY_TX;
  2355. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  2356. INDEX_TO_SEQ(q->write_ptr)));
  2357. /* copy frags header */
  2358. memcpy(out_cmd->cmd.tx.hdr, hdr, hdr_len);
  2359. /* hdr = (struct ieee80211_hdr *)out_cmd->cmd.tx.hdr; */
  2360. len = priv->hw_setting.tx_cmd_len +
  2361. sizeof(struct iwl_cmd_header) + hdr_len;
  2362. len_org = len;
  2363. len = (len + 3) & ~3;
  2364. if (len_org != len)
  2365. len_org = 1;
  2366. else
  2367. len_org = 0;
  2368. txcmd_phys = txq->dma_addr_cmd + sizeof(struct iwl_cmd) * idx +
  2369. offsetof(struct iwl_cmd, hdr);
  2370. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
  2371. if (!(ctl->flags & IEEE80211_TXCTL_DO_NOT_ENCRYPT))
  2372. iwl_build_tx_cmd_hwcrypto(priv, ctl, out_cmd, skb, 0);
  2373. /* 802.11 null functions have no payload... */
  2374. len = skb->len - hdr_len;
  2375. if (len) {
  2376. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  2377. len, PCI_DMA_TODEVICE);
  2378. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
  2379. }
  2380. /* If there is no payload, then only one TFD is used */
  2381. if (!len)
  2382. *control_flags = TFD_CTL_COUNT_SET(1);
  2383. else
  2384. *control_flags = TFD_CTL_COUNT_SET(2) |
  2385. TFD_CTL_PAD_SET(U32_PAD(len));
  2386. len = (u16)skb->len;
  2387. out_cmd->cmd.tx.len = cpu_to_le16(len);
  2388. /* TODO need this for burst mode later on */
  2389. iwl_build_tx_cmd_basic(priv, out_cmd, ctl, hdr, unicast, sta_id);
  2390. /* set is_hcca to 0; it probably will never be implemented */
  2391. iwl_hw_build_tx_cmd_rate(priv, out_cmd, ctl, hdr, sta_id, 0);
  2392. out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_A_MSK;
  2393. out_cmd->cmd.tx.tx_flags &= ~TX_CMD_FLG_ANT_B_MSK;
  2394. if (!ieee80211_get_morefrag(hdr)) {
  2395. txq->need_update = 1;
  2396. if (qc) {
  2397. u8 tid = (u8)(le16_to_cpu(*qc) & 0xf);
  2398. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  2399. }
  2400. } else {
  2401. wait_write_ptr = 1;
  2402. txq->need_update = 0;
  2403. }
  2404. iwl_print_hex_dump(IWL_DL_TX, out_cmd->cmd.payload,
  2405. sizeof(out_cmd->cmd.tx));
  2406. iwl_print_hex_dump(IWL_DL_TX, (u8 *)out_cmd->cmd.tx.hdr,
  2407. ieee80211_get_hdrlen(fc));
  2408. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  2409. rc = iwl_tx_queue_update_write_ptr(priv, txq);
  2410. spin_unlock_irqrestore(&priv->lock, flags);
  2411. if (rc)
  2412. return rc;
  2413. if ((iwl_queue_space(q) < q->high_mark)
  2414. && priv->mac80211_registered) {
  2415. if (wait_write_ptr) {
  2416. spin_lock_irqsave(&priv->lock, flags);
  2417. txq->need_update = 1;
  2418. iwl_tx_queue_update_write_ptr(priv, txq);
  2419. spin_unlock_irqrestore(&priv->lock, flags);
  2420. }
  2421. ieee80211_stop_queue(priv->hw, ctl->queue);
  2422. }
  2423. return 0;
  2424. drop_unlock:
  2425. spin_unlock_irqrestore(&priv->lock, flags);
  2426. drop:
  2427. return -1;
  2428. }
  2429. static void iwl_set_rate(struct iwl_priv *priv)
  2430. {
  2431. const struct ieee80211_hw_mode *hw = NULL;
  2432. struct ieee80211_rate *rate;
  2433. int i;
  2434. hw = iwl_get_hw_mode(priv, priv->phymode);
  2435. if (!hw) {
  2436. IWL_ERROR("Failed to set rate: unable to get hw mode\n");
  2437. return;
  2438. }
  2439. priv->active_rate = 0;
  2440. priv->active_rate_basic = 0;
  2441. IWL_DEBUG_RATE("Setting rates for 802.11%c\n",
  2442. hw->mode == MODE_IEEE80211A ?
  2443. 'a' : ((hw->mode == MODE_IEEE80211B) ? 'b' : 'g'));
  2444. for (i = 0; i < hw->num_rates; i++) {
  2445. rate = &(hw->rates[i]);
  2446. if ((rate->val < IWL_RATE_COUNT) &&
  2447. (rate->flags & IEEE80211_RATE_SUPPORTED)) {
  2448. IWL_DEBUG_RATE("Adding rate index %d (plcp %d)%s\n",
  2449. rate->val, iwl_rates[rate->val].plcp,
  2450. (rate->flags & IEEE80211_RATE_BASIC) ?
  2451. "*" : "");
  2452. priv->active_rate |= (1 << rate->val);
  2453. if (rate->flags & IEEE80211_RATE_BASIC)
  2454. priv->active_rate_basic |= (1 << rate->val);
  2455. } else
  2456. IWL_DEBUG_RATE("Not adding rate %d (plcp %d)\n",
  2457. rate->val, iwl_rates[rate->val].plcp);
  2458. }
  2459. IWL_DEBUG_RATE("Set active_rate = %0x, active_rate_basic = %0x\n",
  2460. priv->active_rate, priv->active_rate_basic);
  2461. /*
  2462. * If a basic rate is configured, then use it (adding IWL_RATE_1M_MASK)
  2463. * otherwise set it to the default of all CCK rates and 6, 12, 24 for
  2464. * OFDM
  2465. */
  2466. if (priv->active_rate_basic & IWL_CCK_BASIC_RATES_MASK)
  2467. priv->staging_rxon.cck_basic_rates =
  2468. ((priv->active_rate_basic &
  2469. IWL_CCK_RATES_MASK) >> IWL_FIRST_CCK_RATE) & 0xF;
  2470. else
  2471. priv->staging_rxon.cck_basic_rates =
  2472. (IWL_CCK_BASIC_RATES_MASK >> IWL_FIRST_CCK_RATE) & 0xF;
  2473. if (priv->active_rate_basic & IWL_OFDM_BASIC_RATES_MASK)
  2474. priv->staging_rxon.ofdm_basic_rates =
  2475. ((priv->active_rate_basic &
  2476. (IWL_OFDM_BASIC_RATES_MASK | IWL_RATE_6M_MASK)) >>
  2477. IWL_FIRST_OFDM_RATE) & 0xFF;
  2478. else
  2479. priv->staging_rxon.ofdm_basic_rates =
  2480. (IWL_OFDM_BASIC_RATES_MASK >> IWL_FIRST_OFDM_RATE) & 0xFF;
  2481. }
  2482. static void iwl_radio_kill_sw(struct iwl_priv *priv, int disable_radio)
  2483. {
  2484. unsigned long flags;
  2485. if (!!disable_radio == test_bit(STATUS_RF_KILL_SW, &priv->status))
  2486. return;
  2487. IWL_DEBUG_RF_KILL("Manual SW RF KILL set to: RADIO %s\n",
  2488. disable_radio ? "OFF" : "ON");
  2489. if (disable_radio) {
  2490. iwl_scan_cancel(priv);
  2491. /* FIXME: This is a workaround for AP */
  2492. if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
  2493. spin_lock_irqsave(&priv->lock, flags);
  2494. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  2495. CSR_UCODE_SW_BIT_RFKILL);
  2496. spin_unlock_irqrestore(&priv->lock, flags);
  2497. iwl_send_card_state(priv, CARD_STATE_CMD_DISABLE, 0);
  2498. set_bit(STATUS_RF_KILL_SW, &priv->status);
  2499. }
  2500. return;
  2501. }
  2502. spin_lock_irqsave(&priv->lock, flags);
  2503. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  2504. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  2505. spin_unlock_irqrestore(&priv->lock, flags);
  2506. /* wake up ucode */
  2507. msleep(10);
  2508. spin_lock_irqsave(&priv->lock, flags);
  2509. iwl_read32(priv, CSR_UCODE_DRV_GP1);
  2510. if (!iwl_grab_restricted_access(priv))
  2511. iwl_release_restricted_access(priv);
  2512. spin_unlock_irqrestore(&priv->lock, flags);
  2513. if (test_bit(STATUS_RF_KILL_HW, &priv->status)) {
  2514. IWL_DEBUG_RF_KILL("Can not turn radio back on - "
  2515. "disabled by HW switch\n");
  2516. return;
  2517. }
  2518. queue_work(priv->workqueue, &priv->restart);
  2519. return;
  2520. }
  2521. void iwl_set_decrypted_flag(struct iwl_priv *priv, struct sk_buff *skb,
  2522. u32 decrypt_res, struct ieee80211_rx_status *stats)
  2523. {
  2524. u16 fc =
  2525. le16_to_cpu(((struct ieee80211_hdr *)skb->data)->frame_control);
  2526. if (priv->active_rxon.filter_flags & RXON_FILTER_DIS_DECRYPT_MSK)
  2527. return;
  2528. if (!(fc & IEEE80211_FCTL_PROTECTED))
  2529. return;
  2530. IWL_DEBUG_RX("decrypt_res:0x%x\n", decrypt_res);
  2531. switch (decrypt_res & RX_RES_STATUS_SEC_TYPE_MSK) {
  2532. case RX_RES_STATUS_SEC_TYPE_TKIP:
  2533. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2534. RX_RES_STATUS_BAD_ICV_MIC)
  2535. stats->flag |= RX_FLAG_MMIC_ERROR;
  2536. case RX_RES_STATUS_SEC_TYPE_WEP:
  2537. case RX_RES_STATUS_SEC_TYPE_CCMP:
  2538. if ((decrypt_res & RX_RES_STATUS_DECRYPT_TYPE_MSK) ==
  2539. RX_RES_STATUS_DECRYPT_OK) {
  2540. IWL_DEBUG_RX("hw decrypt successfully!!!\n");
  2541. stats->flag |= RX_FLAG_DECRYPTED;
  2542. }
  2543. break;
  2544. default:
  2545. break;
  2546. }
  2547. }
  2548. void iwl_handle_data_packet_monitor(struct iwl_priv *priv,
  2549. struct iwl_rx_mem_buffer *rxb,
  2550. void *data, short len,
  2551. struct ieee80211_rx_status *stats,
  2552. u16 phy_flags)
  2553. {
  2554. struct iwl_rt_rx_hdr *iwl_rt;
  2555. /* First cache any information we need before we overwrite
  2556. * the information provided in the skb from the hardware */
  2557. s8 signal = stats->ssi;
  2558. s8 noise = 0;
  2559. int rate = stats->rate;
  2560. u64 tsf = stats->mactime;
  2561. __le16 phy_flags_hw = cpu_to_le16(phy_flags);
  2562. /* We received data from the HW, so stop the watchdog */
  2563. if (len > IWL_RX_BUF_SIZE - sizeof(*iwl_rt)) {
  2564. IWL_DEBUG_DROP("Dropping too large packet in monitor\n");
  2565. return;
  2566. }
  2567. /* copy the frame data to write after where the radiotap header goes */
  2568. iwl_rt = (void *)rxb->skb->data;
  2569. memmove(iwl_rt->payload, data, len);
  2570. iwl_rt->rt_hdr.it_version = PKTHDR_RADIOTAP_VERSION;
  2571. iwl_rt->rt_hdr.it_pad = 0; /* always good to zero */
  2572. /* total header + data */
  2573. iwl_rt->rt_hdr.it_len = cpu_to_le16(sizeof(*iwl_rt));
  2574. /* Set the size of the skb to the size of the frame */
  2575. skb_put(rxb->skb, sizeof(*iwl_rt) + len);
  2576. /* Big bitfield of all the fields we provide in radiotap */
  2577. iwl_rt->rt_hdr.it_present =
  2578. cpu_to_le32((1 << IEEE80211_RADIOTAP_TSFT) |
  2579. (1 << IEEE80211_RADIOTAP_FLAGS) |
  2580. (1 << IEEE80211_RADIOTAP_RATE) |
  2581. (1 << IEEE80211_RADIOTAP_CHANNEL) |
  2582. (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |
  2583. (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE) |
  2584. (1 << IEEE80211_RADIOTAP_ANTENNA));
  2585. /* Zero the flags, we'll add to them as we go */
  2586. iwl_rt->rt_flags = 0;
  2587. iwl_rt->rt_tsf = cpu_to_le64(tsf);
  2588. /* Convert to dBm */
  2589. iwl_rt->rt_dbmsignal = signal;
  2590. iwl_rt->rt_dbmnoise = noise;
  2591. /* Convert the channel frequency and set the flags */
  2592. iwl_rt->rt_channelMHz = cpu_to_le16(stats->freq);
  2593. if (!(phy_flags_hw & RX_RES_PHY_FLAGS_BAND_24_MSK))
  2594. iwl_rt->rt_chbitmask =
  2595. cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_5GHZ));
  2596. else if (phy_flags_hw & RX_RES_PHY_FLAGS_MOD_CCK_MSK)
  2597. iwl_rt->rt_chbitmask =
  2598. cpu_to_le16((IEEE80211_CHAN_CCK | IEEE80211_CHAN_2GHZ));
  2599. else /* 802.11g */
  2600. iwl_rt->rt_chbitmask =
  2601. cpu_to_le16((IEEE80211_CHAN_OFDM | IEEE80211_CHAN_2GHZ));
  2602. rate = iwl_rate_index_from_plcp(rate);
  2603. if (rate == -1)
  2604. iwl_rt->rt_rate = 0;
  2605. else
  2606. iwl_rt->rt_rate = iwl_rates[rate].ieee;
  2607. /* antenna number */
  2608. iwl_rt->rt_antenna =
  2609. le16_to_cpu(phy_flags_hw & RX_RES_PHY_FLAGS_ANTENNA_MSK) >> 4;
  2610. /* set the preamble flag if we have it */
  2611. if (phy_flags_hw & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  2612. iwl_rt->rt_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
  2613. IWL_DEBUG_RX("Rx packet of %d bytes.\n", rxb->skb->len);
  2614. stats->flag |= RX_FLAG_RADIOTAP;
  2615. ieee80211_rx_irqsafe(priv->hw, rxb->skb, stats);
  2616. rxb->skb = NULL;
  2617. }
  2618. #define IWL_PACKET_RETRY_TIME HZ
  2619. int is_duplicate_packet(struct iwl_priv *priv, struct ieee80211_hdr *header)
  2620. {
  2621. u16 sc = le16_to_cpu(header->seq_ctrl);
  2622. u16 seq = (sc & IEEE80211_SCTL_SEQ) >> 4;
  2623. u16 frag = sc & IEEE80211_SCTL_FRAG;
  2624. u16 *last_seq, *last_frag;
  2625. unsigned long *last_time;
  2626. switch (priv->iw_mode) {
  2627. case IEEE80211_IF_TYPE_IBSS:{
  2628. struct list_head *p;
  2629. struct iwl_ibss_seq *entry = NULL;
  2630. u8 *mac = header->addr2;
  2631. int index = mac[5] & (IWL_IBSS_MAC_HASH_SIZE - 1);
  2632. __list_for_each(p, &priv->ibss_mac_hash[index]) {
  2633. entry = list_entry(p, struct iwl_ibss_seq, list);
  2634. if (!compare_ether_addr(entry->mac, mac))
  2635. break;
  2636. }
  2637. if (p == &priv->ibss_mac_hash[index]) {
  2638. entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
  2639. if (!entry) {
  2640. IWL_ERROR("Cannot malloc new mac entry\n");
  2641. return 0;
  2642. }
  2643. memcpy(entry->mac, mac, ETH_ALEN);
  2644. entry->seq_num = seq;
  2645. entry->frag_num = frag;
  2646. entry->packet_time = jiffies;
  2647. list_add(&entry->list, &priv->ibss_mac_hash[index]);
  2648. return 0;
  2649. }
  2650. last_seq = &entry->seq_num;
  2651. last_frag = &entry->frag_num;
  2652. last_time = &entry->packet_time;
  2653. break;
  2654. }
  2655. case IEEE80211_IF_TYPE_STA:
  2656. last_seq = &priv->last_seq_num;
  2657. last_frag = &priv->last_frag_num;
  2658. last_time = &priv->last_packet_time;
  2659. break;
  2660. default:
  2661. return 0;
  2662. }
  2663. if ((*last_seq == seq) &&
  2664. time_after(*last_time + IWL_PACKET_RETRY_TIME, jiffies)) {
  2665. if (*last_frag == frag)
  2666. goto drop;
  2667. if (*last_frag + 1 != frag)
  2668. /* out-of-order fragment */
  2669. goto drop;
  2670. } else
  2671. *last_seq = seq;
  2672. *last_frag = frag;
  2673. *last_time = jiffies;
  2674. return 0;
  2675. drop:
  2676. return 1;
  2677. }
  2678. #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
  2679. #include "iwl-spectrum.h"
  2680. #define BEACON_TIME_MASK_LOW 0x00FFFFFF
  2681. #define BEACON_TIME_MASK_HIGH 0xFF000000
  2682. #define TIME_UNIT 1024
  2683. /*
  2684. * extended beacon time format
  2685. * time in usec will be changed into a 32-bit value in 8:24 format
  2686. * the high 1 byte is the beacon counts
  2687. * the lower 3 bytes is the time in usec within one beacon interval
  2688. */
  2689. static u32 iwl_usecs_to_beacons(u32 usec, u32 beacon_interval)
  2690. {
  2691. u32 quot;
  2692. u32 rem;
  2693. u32 interval = beacon_interval * 1024;
  2694. if (!interval || !usec)
  2695. return 0;
  2696. quot = (usec / interval) & (BEACON_TIME_MASK_HIGH >> 24);
  2697. rem = (usec % interval) & BEACON_TIME_MASK_LOW;
  2698. return (quot << 24) + rem;
  2699. }
  2700. /* base is usually what we get from ucode with each received frame,
  2701. * the same as HW timer counter counting down
  2702. */
  2703. static __le32 iwl_add_beacon_time(u32 base, u32 addon, u32 beacon_interval)
  2704. {
  2705. u32 base_low = base & BEACON_TIME_MASK_LOW;
  2706. u32 addon_low = addon & BEACON_TIME_MASK_LOW;
  2707. u32 interval = beacon_interval * TIME_UNIT;
  2708. u32 res = (base & BEACON_TIME_MASK_HIGH) +
  2709. (addon & BEACON_TIME_MASK_HIGH);
  2710. if (base_low > addon_low)
  2711. res += base_low - addon_low;
  2712. else if (base_low < addon_low) {
  2713. res += interval + base_low - addon_low;
  2714. res += (1 << 24);
  2715. } else
  2716. res += (1 << 24);
  2717. return cpu_to_le32(res);
  2718. }
  2719. static int iwl_get_measurement(struct iwl_priv *priv,
  2720. struct ieee80211_measurement_params *params,
  2721. u8 type)
  2722. {
  2723. struct iwl_spectrum_cmd spectrum;
  2724. struct iwl_rx_packet *res;
  2725. struct iwl_host_cmd cmd = {
  2726. .id = REPLY_SPECTRUM_MEASUREMENT_CMD,
  2727. .data = (void *)&spectrum,
  2728. .meta.flags = CMD_WANT_SKB,
  2729. };
  2730. u32 add_time = le64_to_cpu(params->start_time);
  2731. int rc;
  2732. int spectrum_resp_status;
  2733. int duration = le16_to_cpu(params->duration);
  2734. if (iwl_is_associated(priv))
  2735. add_time =
  2736. iwl_usecs_to_beacons(
  2737. le64_to_cpu(params->start_time) - priv->last_tsf,
  2738. le16_to_cpu(priv->rxon_timing.beacon_interval));
  2739. memset(&spectrum, 0, sizeof(spectrum));
  2740. spectrum.channel_count = cpu_to_le16(1);
  2741. spectrum.flags =
  2742. RXON_FLG_TSF2HOST_MSK | RXON_FLG_ANT_A_MSK | RXON_FLG_DIS_DIV_MSK;
  2743. spectrum.filter_flags = MEASUREMENT_FILTER_FLAG;
  2744. cmd.len = sizeof(spectrum);
  2745. spectrum.len = cpu_to_le16(cmd.len - sizeof(spectrum.len));
  2746. if (iwl_is_associated(priv))
  2747. spectrum.start_time =
  2748. iwl_add_beacon_time(priv->last_beacon_time,
  2749. add_time,
  2750. le16_to_cpu(priv->rxon_timing.beacon_interval));
  2751. else
  2752. spectrum.start_time = 0;
  2753. spectrum.channels[0].duration = cpu_to_le32(duration * TIME_UNIT);
  2754. spectrum.channels[0].channel = params->channel;
  2755. spectrum.channels[0].type = type;
  2756. if (priv->active_rxon.flags & RXON_FLG_BAND_24G_MSK)
  2757. spectrum.flags |= RXON_FLG_BAND_24G_MSK |
  2758. RXON_FLG_AUTO_DETECT_MSK | RXON_FLG_TGG_PROTECT_MSK;
  2759. rc = iwl_send_cmd_sync(priv, &cmd);
  2760. if (rc)
  2761. return rc;
  2762. res = (struct iwl_rx_packet *)cmd.meta.u.skb->data;
  2763. if (res->hdr.flags & IWL_CMD_FAILED_MSK) {
  2764. IWL_ERROR("Bad return from REPLY_RX_ON_ASSOC command\n");
  2765. rc = -EIO;
  2766. }
  2767. spectrum_resp_status = le16_to_cpu(res->u.spectrum.status);
  2768. switch (spectrum_resp_status) {
  2769. case 0: /* Command will be handled */
  2770. if (res->u.spectrum.id != 0xff) {
  2771. IWL_DEBUG_INFO("Replaced existing measurement: %d\n",
  2772. res->u.spectrum.id);
  2773. priv->measurement_status &= ~MEASUREMENT_READY;
  2774. }
  2775. priv->measurement_status |= MEASUREMENT_ACTIVE;
  2776. rc = 0;
  2777. break;
  2778. case 1: /* Command will not be handled */
  2779. rc = -EAGAIN;
  2780. break;
  2781. }
  2782. dev_kfree_skb_any(cmd.meta.u.skb);
  2783. return rc;
  2784. }
  2785. #endif
  2786. static void iwl_txstatus_to_ieee(struct iwl_priv *priv,
  2787. struct iwl_tx_info *tx_sta)
  2788. {
  2789. tx_sta->status.ack_signal = 0;
  2790. tx_sta->status.excessive_retries = 0;
  2791. tx_sta->status.queue_length = 0;
  2792. tx_sta->status.queue_number = 0;
  2793. if (in_interrupt())
  2794. ieee80211_tx_status_irqsafe(priv->hw,
  2795. tx_sta->skb[0], &(tx_sta->status));
  2796. else
  2797. ieee80211_tx_status(priv->hw,
  2798. tx_sta->skb[0], &(tx_sta->status));
  2799. tx_sta->skb[0] = NULL;
  2800. }
  2801. /**
  2802. * iwl_tx_queue_reclaim - Reclaim Tx queue entries no more used by NIC.
  2803. *
  2804. * When FW advances 'R' index, all entries between old and
  2805. * new 'R' index need to be reclaimed. As result, some free space
  2806. * forms. If there is enough free space (> low mark), wake Tx queue.
  2807. */
  2808. int iwl_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
  2809. {
  2810. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  2811. struct iwl_queue *q = &txq->q;
  2812. int nfreed = 0;
  2813. if ((index >= q->n_bd) || (x2_queue_used(q, index) == 0)) {
  2814. IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
  2815. "is out of range [0-%d] %d %d.\n", txq_id,
  2816. index, q->n_bd, q->write_ptr, q->read_ptr);
  2817. return 0;
  2818. }
  2819. for (index = iwl_queue_inc_wrap(index, q->n_bd);
  2820. q->read_ptr != index;
  2821. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  2822. if (txq_id != IWL_CMD_QUEUE_NUM) {
  2823. iwl_txstatus_to_ieee(priv,
  2824. &(txq->txb[txq->q.read_ptr]));
  2825. iwl_hw_txq_free_tfd(priv, txq);
  2826. } else if (nfreed > 1) {
  2827. IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
  2828. q->write_ptr, q->read_ptr);
  2829. queue_work(priv->workqueue, &priv->restart);
  2830. }
  2831. nfreed++;
  2832. }
  2833. if (iwl_queue_space(q) > q->low_mark && (txq_id >= 0) &&
  2834. (txq_id != IWL_CMD_QUEUE_NUM) &&
  2835. priv->mac80211_registered)
  2836. ieee80211_wake_queue(priv->hw, txq_id);
  2837. return nfreed;
  2838. }
  2839. static int iwl_is_tx_success(u32 status)
  2840. {
  2841. return (status & 0xFF) == 0x1;
  2842. }
  2843. /******************************************************************************
  2844. *
  2845. * Generic RX handler implementations
  2846. *
  2847. ******************************************************************************/
  2848. static void iwl_rx_reply_tx(struct iwl_priv *priv,
  2849. struct iwl_rx_mem_buffer *rxb)
  2850. {
  2851. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2852. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  2853. int txq_id = SEQ_TO_QUEUE(sequence);
  2854. int index = SEQ_TO_INDEX(sequence);
  2855. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  2856. struct ieee80211_tx_status *tx_status;
  2857. struct iwl_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  2858. u32 status = le32_to_cpu(tx_resp->status);
  2859. if ((index >= txq->q.n_bd) || (x2_queue_used(&txq->q, index) == 0)) {
  2860. IWL_ERROR("Read index for DMA queue txq_id (%d) index %d "
  2861. "is out of range [0-%d] %d %d\n", txq_id,
  2862. index, txq->q.n_bd, txq->q.write_ptr,
  2863. txq->q.read_ptr);
  2864. return;
  2865. }
  2866. tx_status = &(txq->txb[txq->q.read_ptr].status);
  2867. tx_status->retry_count = tx_resp->failure_frame;
  2868. tx_status->queue_number = status;
  2869. tx_status->queue_length = tx_resp->bt_kill_count;
  2870. tx_status->queue_length |= tx_resp->failure_rts;
  2871. tx_status->flags =
  2872. iwl_is_tx_success(status) ? IEEE80211_TX_STATUS_ACK : 0;
  2873. tx_status->control.tx_rate = iwl_rate_index_from_plcp(tx_resp->rate);
  2874. IWL_DEBUG_TX("Tx queue %d Status %s (0x%08x) plcp rate %d retries %d\n",
  2875. txq_id, iwl_get_tx_fail_reason(status), status,
  2876. tx_resp->rate, tx_resp->failure_frame);
  2877. IWL_DEBUG_TX_REPLY("Tx queue reclaim %d\n", index);
  2878. if (index != -1)
  2879. iwl_tx_queue_reclaim(priv, txq_id, index);
  2880. if (iwl_check_bits(status, TX_ABORT_REQUIRED_MSK))
  2881. IWL_ERROR("TODO: Implement Tx ABORT REQUIRED!!!\n");
  2882. }
  2883. static void iwl_rx_reply_alive(struct iwl_priv *priv,
  2884. struct iwl_rx_mem_buffer *rxb)
  2885. {
  2886. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2887. struct iwl_alive_resp *palive;
  2888. struct delayed_work *pwork;
  2889. palive = &pkt->u.alive_frame;
  2890. IWL_DEBUG_INFO("Alive ucode status 0x%08X revision "
  2891. "0x%01X 0x%01X\n",
  2892. palive->is_valid, palive->ver_type,
  2893. palive->ver_subtype);
  2894. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  2895. IWL_DEBUG_INFO("Initialization Alive received.\n");
  2896. memcpy(&priv->card_alive_init,
  2897. &pkt->u.alive_frame,
  2898. sizeof(struct iwl_init_alive_resp));
  2899. pwork = &priv->init_alive_start;
  2900. } else {
  2901. IWL_DEBUG_INFO("Runtime Alive received.\n");
  2902. memcpy(&priv->card_alive, &pkt->u.alive_frame,
  2903. sizeof(struct iwl_alive_resp));
  2904. pwork = &priv->alive_start;
  2905. iwl_disable_events(priv);
  2906. }
  2907. /* We delay the ALIVE response by 5ms to
  2908. * give the HW RF Kill time to activate... */
  2909. if (palive->is_valid == UCODE_VALID_OK)
  2910. queue_delayed_work(priv->workqueue, pwork,
  2911. msecs_to_jiffies(5));
  2912. else
  2913. IWL_WARNING("uCode did not respond OK.\n");
  2914. }
  2915. static void iwl_rx_reply_add_sta(struct iwl_priv *priv,
  2916. struct iwl_rx_mem_buffer *rxb)
  2917. {
  2918. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2919. IWL_DEBUG_RX("Received REPLY_ADD_STA: 0x%02X\n", pkt->u.status);
  2920. return;
  2921. }
  2922. static void iwl_rx_reply_error(struct iwl_priv *priv,
  2923. struct iwl_rx_mem_buffer *rxb)
  2924. {
  2925. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2926. IWL_ERROR("Error Reply type 0x%08X cmd %s (0x%02X) "
  2927. "seq 0x%04X ser 0x%08X\n",
  2928. le32_to_cpu(pkt->u.err_resp.error_type),
  2929. get_cmd_string(pkt->u.err_resp.cmd_id),
  2930. pkt->u.err_resp.cmd_id,
  2931. le16_to_cpu(pkt->u.err_resp.bad_cmd_seq_num),
  2932. le32_to_cpu(pkt->u.err_resp.error_info));
  2933. }
  2934. #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
  2935. static void iwl_rx_csa(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
  2936. {
  2937. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2938. struct iwl_rxon_cmd *rxon = (void *)&priv->active_rxon;
  2939. struct iwl_csa_notification *csa = &(pkt->u.csa_notif);
  2940. IWL_DEBUG_11H("CSA notif: channel %d, status %d\n",
  2941. le16_to_cpu(csa->channel), le32_to_cpu(csa->status));
  2942. rxon->channel = csa->channel;
  2943. priv->staging_rxon.channel = csa->channel;
  2944. }
  2945. static void iwl_rx_spectrum_measure_notif(struct iwl_priv *priv,
  2946. struct iwl_rx_mem_buffer *rxb)
  2947. {
  2948. #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
  2949. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2950. struct iwl_spectrum_notification *report = &(pkt->u.spectrum_notif);
  2951. if (!report->state) {
  2952. IWL_DEBUG(IWL_DL_11H | IWL_DL_INFO,
  2953. "Spectrum Measure Notification: Start\n");
  2954. return;
  2955. }
  2956. memcpy(&priv->measure_report, report, sizeof(*report));
  2957. priv->measurement_status |= MEASUREMENT_READY;
  2958. #endif
  2959. }
  2960. static void iwl_rx_pm_sleep_notif(struct iwl_priv *priv,
  2961. struct iwl_rx_mem_buffer *rxb)
  2962. {
  2963. #ifdef CONFIG_IWLWIFI_DEBUG
  2964. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2965. struct iwl_sleep_notification *sleep = &(pkt->u.sleep_notif);
  2966. IWL_DEBUG_RX("sleep mode: %d, src: %d\n",
  2967. sleep->pm_sleep_mode, sleep->pm_wakeup_src);
  2968. #endif
  2969. }
  2970. static void iwl_rx_pm_debug_statistics_notif(struct iwl_priv *priv,
  2971. struct iwl_rx_mem_buffer *rxb)
  2972. {
  2973. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  2974. IWL_DEBUG_RADIO("Dumping %d bytes of unhandled "
  2975. "notification for %s:\n",
  2976. le32_to_cpu(pkt->len), get_cmd_string(pkt->hdr.cmd));
  2977. iwl_print_hex_dump(IWL_DL_RADIO, pkt->u.raw, le32_to_cpu(pkt->len));
  2978. }
  2979. static void iwl_bg_beacon_update(struct work_struct *work)
  2980. {
  2981. struct iwl_priv *priv =
  2982. container_of(work, struct iwl_priv, beacon_update);
  2983. struct sk_buff *beacon;
  2984. /* Pull updated AP beacon from mac80211. will fail if not in AP mode */
  2985. beacon = ieee80211_beacon_get(priv->hw, priv->interface_id, NULL);
  2986. if (!beacon) {
  2987. IWL_ERROR("update beacon failed\n");
  2988. return;
  2989. }
  2990. mutex_lock(&priv->mutex);
  2991. /* new beacon skb is allocated every time; dispose previous.*/
  2992. if (priv->ibss_beacon)
  2993. dev_kfree_skb(priv->ibss_beacon);
  2994. priv->ibss_beacon = beacon;
  2995. mutex_unlock(&priv->mutex);
  2996. iwl_send_beacon_cmd(priv);
  2997. }
  2998. static void iwl_rx_beacon_notif(struct iwl_priv *priv,
  2999. struct iwl_rx_mem_buffer *rxb)
  3000. {
  3001. #ifdef CONFIG_IWLWIFI_DEBUG
  3002. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  3003. struct iwl_beacon_notif *beacon = &(pkt->u.beacon_status);
  3004. u8 rate = beacon->beacon_notify_hdr.rate;
  3005. IWL_DEBUG_RX("beacon status %x retries %d iss %d "
  3006. "tsf %d %d rate %d\n",
  3007. le32_to_cpu(beacon->beacon_notify_hdr.status) & TX_STATUS_MSK,
  3008. beacon->beacon_notify_hdr.failure_frame,
  3009. le32_to_cpu(beacon->ibss_mgr_status),
  3010. le32_to_cpu(beacon->high_tsf),
  3011. le32_to_cpu(beacon->low_tsf), rate);
  3012. #endif
  3013. if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
  3014. (!test_bit(STATUS_EXIT_PENDING, &priv->status)))
  3015. queue_work(priv->workqueue, &priv->beacon_update);
  3016. }
  3017. /* Service response to REPLY_SCAN_CMD (0x80) */
  3018. static void iwl_rx_reply_scan(struct iwl_priv *priv,
  3019. struct iwl_rx_mem_buffer *rxb)
  3020. {
  3021. #ifdef CONFIG_IWLWIFI_DEBUG
  3022. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  3023. struct iwl_scanreq_notification *notif =
  3024. (struct iwl_scanreq_notification *)pkt->u.raw;
  3025. IWL_DEBUG_RX("Scan request status = 0x%x\n", notif->status);
  3026. #endif
  3027. }
  3028. /* Service SCAN_START_NOTIFICATION (0x82) */
  3029. static void iwl_rx_scan_start_notif(struct iwl_priv *priv,
  3030. struct iwl_rx_mem_buffer *rxb)
  3031. {
  3032. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  3033. struct iwl_scanstart_notification *notif =
  3034. (struct iwl_scanstart_notification *)pkt->u.raw;
  3035. priv->scan_start_tsf = le32_to_cpu(notif->tsf_low);
  3036. IWL_DEBUG_SCAN("Scan start: "
  3037. "%d [802.11%s] "
  3038. "(TSF: 0x%08X:%08X) - %d (beacon timer %u)\n",
  3039. notif->channel,
  3040. notif->band ? "bg" : "a",
  3041. notif->tsf_high,
  3042. notif->tsf_low, notif->status, notif->beacon_timer);
  3043. }
  3044. /* Service SCAN_RESULTS_NOTIFICATION (0x83) */
  3045. static void iwl_rx_scan_results_notif(struct iwl_priv *priv,
  3046. struct iwl_rx_mem_buffer *rxb)
  3047. {
  3048. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  3049. struct iwl_scanresults_notification *notif =
  3050. (struct iwl_scanresults_notification *)pkt->u.raw;
  3051. IWL_DEBUG_SCAN("Scan ch.res: "
  3052. "%d [802.11%s] "
  3053. "(TSF: 0x%08X:%08X) - %d "
  3054. "elapsed=%lu usec (%dms since last)\n",
  3055. notif->channel,
  3056. notif->band ? "bg" : "a",
  3057. le32_to_cpu(notif->tsf_high),
  3058. le32_to_cpu(notif->tsf_low),
  3059. le32_to_cpu(notif->statistics[0]),
  3060. le32_to_cpu(notif->tsf_low) - priv->scan_start_tsf,
  3061. jiffies_to_msecs(elapsed_jiffies
  3062. (priv->last_scan_jiffies, jiffies)));
  3063. priv->last_scan_jiffies = jiffies;
  3064. }
  3065. /* Service SCAN_COMPLETE_NOTIFICATION (0x84) */
  3066. static void iwl_rx_scan_complete_notif(struct iwl_priv *priv,
  3067. struct iwl_rx_mem_buffer *rxb)
  3068. {
  3069. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  3070. struct iwl_scancomplete_notification *scan_notif = (void *)pkt->u.raw;
  3071. IWL_DEBUG_SCAN("Scan complete: %d channels (TSF 0x%08X:%08X) - %d\n",
  3072. scan_notif->scanned_channels,
  3073. scan_notif->tsf_low,
  3074. scan_notif->tsf_high, scan_notif->status);
  3075. /* The HW is no longer scanning */
  3076. clear_bit(STATUS_SCAN_HW, &priv->status);
  3077. /* The scan completion notification came in, so kill that timer... */
  3078. cancel_delayed_work(&priv->scan_check);
  3079. IWL_DEBUG_INFO("Scan pass on %sGHz took %dms\n",
  3080. (priv->scan_bands == 2) ? "2.4" : "5.2",
  3081. jiffies_to_msecs(elapsed_jiffies
  3082. (priv->scan_pass_start, jiffies)));
  3083. /* Remove this scanned band from the list
  3084. * of pending bands to scan */
  3085. priv->scan_bands--;
  3086. /* If a request to abort was given, or the scan did not succeed
  3087. * then we reset the scan state machine and terminate,
  3088. * re-queuing another scan if one has been requested */
  3089. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  3090. IWL_DEBUG_INFO("Aborted scan completed.\n");
  3091. clear_bit(STATUS_SCAN_ABORTING, &priv->status);
  3092. } else {
  3093. /* If there are more bands on this scan pass reschedule */
  3094. if (priv->scan_bands > 0)
  3095. goto reschedule;
  3096. }
  3097. priv->last_scan_jiffies = jiffies;
  3098. IWL_DEBUG_INFO("Setting scan to off\n");
  3099. clear_bit(STATUS_SCANNING, &priv->status);
  3100. IWL_DEBUG_INFO("Scan took %dms\n",
  3101. jiffies_to_msecs(elapsed_jiffies(priv->scan_start, jiffies)));
  3102. queue_work(priv->workqueue, &priv->scan_completed);
  3103. return;
  3104. reschedule:
  3105. priv->scan_pass_start = jiffies;
  3106. queue_work(priv->workqueue, &priv->request_scan);
  3107. }
  3108. /* Handle notification from uCode that card's power state is changing
  3109. * due to software, hardware, or critical temperature RFKILL */
  3110. static void iwl_rx_card_state_notif(struct iwl_priv *priv,
  3111. struct iwl_rx_mem_buffer *rxb)
  3112. {
  3113. struct iwl_rx_packet *pkt = (void *)rxb->skb->data;
  3114. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  3115. unsigned long status = priv->status;
  3116. IWL_DEBUG_RF_KILL("Card state received: HW:%s SW:%s\n",
  3117. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  3118. (flags & SW_CARD_DISABLED) ? "Kill" : "On");
  3119. iwl_write32(priv, CSR_UCODE_DRV_GP1_SET,
  3120. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  3121. if (flags & HW_CARD_DISABLED)
  3122. set_bit(STATUS_RF_KILL_HW, &priv->status);
  3123. else
  3124. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  3125. if (flags & SW_CARD_DISABLED)
  3126. set_bit(STATUS_RF_KILL_SW, &priv->status);
  3127. else
  3128. clear_bit(STATUS_RF_KILL_SW, &priv->status);
  3129. iwl_scan_cancel(priv);
  3130. if ((test_bit(STATUS_RF_KILL_HW, &status) !=
  3131. test_bit(STATUS_RF_KILL_HW, &priv->status)) ||
  3132. (test_bit(STATUS_RF_KILL_SW, &status) !=
  3133. test_bit(STATUS_RF_KILL_SW, &priv->status)))
  3134. queue_work(priv->workqueue, &priv->rf_kill);
  3135. else
  3136. wake_up_interruptible(&priv->wait_command_queue);
  3137. }
  3138. /**
  3139. * iwl_setup_rx_handlers - Initialize Rx handler callbacks
  3140. *
  3141. * Setup the RX handlers for each of the reply types sent from the uCode
  3142. * to the host.
  3143. *
  3144. * This function chains into the hardware specific files for them to setup
  3145. * any hardware specific handlers as well.
  3146. */
  3147. static void iwl_setup_rx_handlers(struct iwl_priv *priv)
  3148. {
  3149. priv->rx_handlers[REPLY_ALIVE] = iwl_rx_reply_alive;
  3150. priv->rx_handlers[REPLY_ADD_STA] = iwl_rx_reply_add_sta;
  3151. priv->rx_handlers[REPLY_ERROR] = iwl_rx_reply_error;
  3152. priv->rx_handlers[CHANNEL_SWITCH_NOTIFICATION] = iwl_rx_csa;
  3153. priv->rx_handlers[SPECTRUM_MEASURE_NOTIFICATION] =
  3154. iwl_rx_spectrum_measure_notif;
  3155. priv->rx_handlers[PM_SLEEP_NOTIFICATION] = iwl_rx_pm_sleep_notif;
  3156. priv->rx_handlers[PM_DEBUG_STATISTIC_NOTIFIC] =
  3157. iwl_rx_pm_debug_statistics_notif;
  3158. priv->rx_handlers[BEACON_NOTIFICATION] = iwl_rx_beacon_notif;
  3159. /* NOTE: iwl_rx_statistics is different based on whether
  3160. * the build is for the 3945 or the 4965. See the
  3161. * corresponding implementation in iwl-XXXX.c
  3162. *
  3163. * The same handler is used for both the REPLY to a
  3164. * discrete statistics request from the host as well as
  3165. * for the periodic statistics notification from the uCode
  3166. */
  3167. priv->rx_handlers[REPLY_STATISTICS_CMD] = iwl_hw_rx_statistics;
  3168. priv->rx_handlers[STATISTICS_NOTIFICATION] = iwl_hw_rx_statistics;
  3169. priv->rx_handlers[REPLY_SCAN_CMD] = iwl_rx_reply_scan;
  3170. priv->rx_handlers[SCAN_START_NOTIFICATION] = iwl_rx_scan_start_notif;
  3171. priv->rx_handlers[SCAN_RESULTS_NOTIFICATION] =
  3172. iwl_rx_scan_results_notif;
  3173. priv->rx_handlers[SCAN_COMPLETE_NOTIFICATION] =
  3174. iwl_rx_scan_complete_notif;
  3175. priv->rx_handlers[CARD_STATE_NOTIFICATION] = iwl_rx_card_state_notif;
  3176. priv->rx_handlers[REPLY_TX] = iwl_rx_reply_tx;
  3177. /* Setup hardware specific Rx handlers */
  3178. iwl_hw_rx_handler_setup(priv);
  3179. }
  3180. /**
  3181. * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  3182. * @rxb: Rx buffer to reclaim
  3183. *
  3184. * If an Rx buffer has an async callback associated with it the callback
  3185. * will be executed. The attached skb (if present) will only be freed
  3186. * if the callback returns 1
  3187. */
  3188. static void iwl_tx_cmd_complete(struct iwl_priv *priv,
  3189. struct iwl_rx_mem_buffer *rxb)
  3190. {
  3191. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  3192. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  3193. int txq_id = SEQ_TO_QUEUE(sequence);
  3194. int index = SEQ_TO_INDEX(sequence);
  3195. int huge = sequence & SEQ_HUGE_FRAME;
  3196. int cmd_index;
  3197. struct iwl_cmd *cmd;
  3198. /* If a Tx command is being handled and it isn't in the actual
  3199. * command queue then there a command routing bug has been introduced
  3200. * in the queue management code. */
  3201. if (txq_id != IWL_CMD_QUEUE_NUM)
  3202. IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
  3203. txq_id, pkt->hdr.cmd);
  3204. BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
  3205. cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
  3206. cmd = &priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
  3207. /* Input error checking is done when commands are added to queue. */
  3208. if (cmd->meta.flags & CMD_WANT_SKB) {
  3209. cmd->meta.source->u.skb = rxb->skb;
  3210. rxb->skb = NULL;
  3211. } else if (cmd->meta.u.callback &&
  3212. !cmd->meta.u.callback(priv, cmd, rxb->skb))
  3213. rxb->skb = NULL;
  3214. iwl_tx_queue_reclaim(priv, txq_id, index);
  3215. if (!(cmd->meta.flags & CMD_ASYNC)) {
  3216. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  3217. wake_up_interruptible(&priv->wait_command_queue);
  3218. }
  3219. }
  3220. /************************** RX-FUNCTIONS ****************************/
  3221. /*
  3222. * Rx theory of operation
  3223. *
  3224. * The host allocates 32 DMA target addresses and passes the host address
  3225. * to the firmware at register IWL_RFDS_TABLE_LOWER + N * RFD_SIZE where N is
  3226. * 0 to 31
  3227. *
  3228. * Rx Queue Indexes
  3229. * The host/firmware share two index registers for managing the Rx buffers.
  3230. *
  3231. * The READ index maps to the first position that the firmware may be writing
  3232. * to -- the driver can read up to (but not including) this position and get
  3233. * good data.
  3234. * The READ index is managed by the firmware once the card is enabled.
  3235. *
  3236. * The WRITE index maps to the last position the driver has read from -- the
  3237. * position preceding WRITE is the last slot the firmware can place a packet.
  3238. *
  3239. * The queue is empty (no good data) if WRITE = READ - 1, and is full if
  3240. * WRITE = READ.
  3241. *
  3242. * During initialization the host sets up the READ queue position to the first
  3243. * INDEX position, and WRITE to the last (READ - 1 wrapped)
  3244. *
  3245. * When the firmware places a packet in a buffer it will advance the READ index
  3246. * and fire the RX interrupt. The driver can then query the READ index and
  3247. * process as many packets as possible, moving the WRITE index forward as it
  3248. * resets the Rx queue buffers with new memory.
  3249. *
  3250. * The management in the driver is as follows:
  3251. * + A list of pre-allocated SKBs is stored in iwl->rxq->rx_free. When
  3252. * iwl->rxq->free_count drops to or below RX_LOW_WATERMARK, work is scheduled
  3253. * to replenish the iwl->rxq->rx_free.
  3254. * + In iwl_rx_replenish (scheduled) if 'processed' != 'read' then the
  3255. * iwl->rxq is replenished and the READ INDEX is updated (updating the
  3256. * 'processed' and 'read' driver indexes as well)
  3257. * + A received packet is processed and handed to the kernel network stack,
  3258. * detached from the iwl->rxq. The driver 'processed' index is updated.
  3259. * + The Host/Firmware iwl->rxq is replenished at tasklet time from the rx_free
  3260. * list. If there are no allocated buffers in iwl->rxq->rx_free, the READ
  3261. * INDEX is not incremented and iwl->status(RX_STALLED) is set. If there
  3262. * were enough free buffers and RX_STALLED is set it is cleared.
  3263. *
  3264. *
  3265. * Driver sequence:
  3266. *
  3267. * iwl_rx_queue_alloc() Allocates rx_free
  3268. * iwl_rx_replenish() Replenishes rx_free list from rx_used, and calls
  3269. * iwl_rx_queue_restock
  3270. * iwl_rx_queue_restock() Moves available buffers from rx_free into Rx
  3271. * queue, updates firmware pointers, and updates
  3272. * the WRITE index. If insufficient rx_free buffers
  3273. * are available, schedules iwl_rx_replenish
  3274. *
  3275. * -- enable interrupts --
  3276. * ISR - iwl_rx() Detach iwl_rx_mem_buffers from pool up to the
  3277. * READ INDEX, detaching the SKB from the pool.
  3278. * Moves the packet buffer from queue to rx_used.
  3279. * Calls iwl_rx_queue_restock to refill any empty
  3280. * slots.
  3281. * ...
  3282. *
  3283. */
  3284. /**
  3285. * iwl_rx_queue_space - Return number of free slots available in queue.
  3286. */
  3287. static int iwl_rx_queue_space(const struct iwl_rx_queue *q)
  3288. {
  3289. int s = q->read - q->write;
  3290. if (s <= 0)
  3291. s += RX_QUEUE_SIZE;
  3292. /* keep some buffer to not confuse full and empty queue */
  3293. s -= 2;
  3294. if (s < 0)
  3295. s = 0;
  3296. return s;
  3297. }
  3298. /**
  3299. * iwl_rx_queue_update_write_ptr - Update the write pointer for the RX queue
  3300. *
  3301. * NOTE: This function has 3945 and 4965 specific code sections
  3302. * but is declared in base due to the majority of the
  3303. * implementation being the same (only a numeric constant is
  3304. * different)
  3305. *
  3306. */
  3307. int iwl_rx_queue_update_write_ptr(struct iwl_priv *priv, struct iwl_rx_queue *q)
  3308. {
  3309. u32 reg = 0;
  3310. int rc = 0;
  3311. unsigned long flags;
  3312. spin_lock_irqsave(&q->lock, flags);
  3313. if (q->need_update == 0)
  3314. goto exit_unlock;
  3315. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  3316. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  3317. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  3318. iwl_set_bit(priv, CSR_GP_CNTRL,
  3319. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  3320. goto exit_unlock;
  3321. }
  3322. rc = iwl_grab_restricted_access(priv);
  3323. if (rc)
  3324. goto exit_unlock;
  3325. iwl_write_restricted(priv, FH_RSCSR_CHNL0_WPTR,
  3326. q->write & ~0x7);
  3327. iwl_release_restricted_access(priv);
  3328. } else
  3329. iwl_write32(priv, FH_RSCSR_CHNL0_WPTR, q->write & ~0x7);
  3330. q->need_update = 0;
  3331. exit_unlock:
  3332. spin_unlock_irqrestore(&q->lock, flags);
  3333. return rc;
  3334. }
  3335. /**
  3336. * iwl_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer pointer.
  3337. *
  3338. * NOTE: This function has 3945 and 4965 specific code paths in it.
  3339. */
  3340. static inline __le32 iwl_dma_addr2rbd_ptr(struct iwl_priv *priv,
  3341. dma_addr_t dma_addr)
  3342. {
  3343. return cpu_to_le32((u32)dma_addr);
  3344. }
  3345. /**
  3346. * iwl_rx_queue_restock - refill RX queue from pre-allocated pool
  3347. *
  3348. * If there are slots in the RX queue that need to be restocked,
  3349. * and we have free pre-allocated buffers, fill the ranks as much
  3350. * as we can pulling from rx_free.
  3351. *
  3352. * This moves the 'write' index forward to catch up with 'processed', and
  3353. * also updates the memory address in the firmware to reference the new
  3354. * target buffer.
  3355. */
  3356. int iwl_rx_queue_restock(struct iwl_priv *priv)
  3357. {
  3358. struct iwl_rx_queue *rxq = &priv->rxq;
  3359. struct list_head *element;
  3360. struct iwl_rx_mem_buffer *rxb;
  3361. unsigned long flags;
  3362. int write, rc;
  3363. spin_lock_irqsave(&rxq->lock, flags);
  3364. write = rxq->write & ~0x7;
  3365. while ((iwl_rx_queue_space(rxq) > 0) && (rxq->free_count)) {
  3366. element = rxq->rx_free.next;
  3367. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  3368. list_del(element);
  3369. rxq->bd[rxq->write] = iwl_dma_addr2rbd_ptr(priv, rxb->dma_addr);
  3370. rxq->queue[rxq->write] = rxb;
  3371. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  3372. rxq->free_count--;
  3373. }
  3374. spin_unlock_irqrestore(&rxq->lock, flags);
  3375. /* If the pre-allocated buffer pool is dropping low, schedule to
  3376. * refill it */
  3377. if (rxq->free_count <= RX_LOW_WATERMARK)
  3378. queue_work(priv->workqueue, &priv->rx_replenish);
  3379. /* If we've added more space for the firmware to place data, tell it */
  3380. if ((write != (rxq->write & ~0x7))
  3381. || (abs(rxq->write - rxq->read) > 7)) {
  3382. spin_lock_irqsave(&rxq->lock, flags);
  3383. rxq->need_update = 1;
  3384. spin_unlock_irqrestore(&rxq->lock, flags);
  3385. rc = iwl_rx_queue_update_write_ptr(priv, rxq);
  3386. if (rc)
  3387. return rc;
  3388. }
  3389. return 0;
  3390. }
  3391. /**
  3392. * iwl_rx_replenish - Move all used packet from rx_used to rx_free
  3393. *
  3394. * When moving to rx_free an SKB is allocated for the slot.
  3395. *
  3396. * Also restock the Rx queue via iwl_rx_queue_restock.
  3397. * This is called as a scheduled work item (except for during initialization)
  3398. */
  3399. void iwl_rx_replenish(void *data)
  3400. {
  3401. struct iwl_priv *priv = data;
  3402. struct iwl_rx_queue *rxq = &priv->rxq;
  3403. struct list_head *element;
  3404. struct iwl_rx_mem_buffer *rxb;
  3405. unsigned long flags;
  3406. spin_lock_irqsave(&rxq->lock, flags);
  3407. while (!list_empty(&rxq->rx_used)) {
  3408. element = rxq->rx_used.next;
  3409. rxb = list_entry(element, struct iwl_rx_mem_buffer, list);
  3410. rxb->skb =
  3411. alloc_skb(IWL_RX_BUF_SIZE, __GFP_NOWARN | GFP_ATOMIC);
  3412. if (!rxb->skb) {
  3413. if (net_ratelimit())
  3414. printk(KERN_CRIT DRV_NAME
  3415. ": Can not allocate SKB buffers\n");
  3416. /* We don't reschedule replenish work here -- we will
  3417. * call the restock method and if it still needs
  3418. * more buffers it will schedule replenish */
  3419. break;
  3420. }
  3421. priv->alloc_rxb_skb++;
  3422. list_del(element);
  3423. rxb->dma_addr =
  3424. pci_map_single(priv->pci_dev, rxb->skb->data,
  3425. IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  3426. list_add_tail(&rxb->list, &rxq->rx_free);
  3427. rxq->free_count++;
  3428. }
  3429. spin_unlock_irqrestore(&rxq->lock, flags);
  3430. spin_lock_irqsave(&priv->lock, flags);
  3431. iwl_rx_queue_restock(priv);
  3432. spin_unlock_irqrestore(&priv->lock, flags);
  3433. }
  3434. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  3435. * If an SKB has been detached, the POOL needs to have it's SKB set to NULL
  3436. * This free routine walks the list of POOL entries and if SKB is set to
  3437. * non NULL it is unmapped and freed
  3438. */
  3439. void iwl_rx_queue_free(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  3440. {
  3441. int i;
  3442. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  3443. if (rxq->pool[i].skb != NULL) {
  3444. pci_unmap_single(priv->pci_dev,
  3445. rxq->pool[i].dma_addr,
  3446. IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  3447. dev_kfree_skb(rxq->pool[i].skb);
  3448. }
  3449. }
  3450. pci_free_consistent(priv->pci_dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  3451. rxq->dma_addr);
  3452. rxq->bd = NULL;
  3453. }
  3454. int iwl_rx_queue_alloc(struct iwl_priv *priv)
  3455. {
  3456. struct iwl_rx_queue *rxq = &priv->rxq;
  3457. struct pci_dev *dev = priv->pci_dev;
  3458. int i;
  3459. spin_lock_init(&rxq->lock);
  3460. INIT_LIST_HEAD(&rxq->rx_free);
  3461. INIT_LIST_HEAD(&rxq->rx_used);
  3462. rxq->bd = pci_alloc_consistent(dev, 4 * RX_QUEUE_SIZE, &rxq->dma_addr);
  3463. if (!rxq->bd)
  3464. return -ENOMEM;
  3465. /* Fill the rx_used queue with _all_ of the Rx buffers */
  3466. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++)
  3467. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  3468. /* Set us so that we have processed and used all buffers, but have
  3469. * not restocked the Rx queue with fresh buffers */
  3470. rxq->read = rxq->write = 0;
  3471. rxq->free_count = 0;
  3472. rxq->need_update = 0;
  3473. return 0;
  3474. }
  3475. void iwl_rx_queue_reset(struct iwl_priv *priv, struct iwl_rx_queue *rxq)
  3476. {
  3477. unsigned long flags;
  3478. int i;
  3479. spin_lock_irqsave(&rxq->lock, flags);
  3480. INIT_LIST_HEAD(&rxq->rx_free);
  3481. INIT_LIST_HEAD(&rxq->rx_used);
  3482. /* Fill the rx_used queue with _all_ of the Rx buffers */
  3483. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  3484. /* In the reset function, these buffers may have been allocated
  3485. * to an SKB, so we need to unmap and free potential storage */
  3486. if (rxq->pool[i].skb != NULL) {
  3487. pci_unmap_single(priv->pci_dev,
  3488. rxq->pool[i].dma_addr,
  3489. IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  3490. priv->alloc_rxb_skb--;
  3491. dev_kfree_skb(rxq->pool[i].skb);
  3492. rxq->pool[i].skb = NULL;
  3493. }
  3494. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  3495. }
  3496. /* Set us so that we have processed and used all buffers, but have
  3497. * not restocked the Rx queue with fresh buffers */
  3498. rxq->read = rxq->write = 0;
  3499. rxq->free_count = 0;
  3500. spin_unlock_irqrestore(&rxq->lock, flags);
  3501. }
  3502. /* Convert linear signal-to-noise ratio into dB */
  3503. static u8 ratio2dB[100] = {
  3504. /* 0 1 2 3 4 5 6 7 8 9 */
  3505. 0, 0, 6, 10, 12, 14, 16, 17, 18, 19, /* 00 - 09 */
  3506. 20, 21, 22, 22, 23, 23, 24, 25, 26, 26, /* 10 - 19 */
  3507. 26, 26, 26, 27, 27, 28, 28, 28, 29, 29, /* 20 - 29 */
  3508. 29, 30, 30, 30, 31, 31, 31, 31, 32, 32, /* 30 - 39 */
  3509. 32, 32, 32, 33, 33, 33, 33, 33, 34, 34, /* 40 - 49 */
  3510. 34, 34, 34, 34, 35, 35, 35, 35, 35, 35, /* 50 - 59 */
  3511. 36, 36, 36, 36, 36, 36, 36, 37, 37, 37, /* 60 - 69 */
  3512. 37, 37, 37, 37, 37, 38, 38, 38, 38, 38, /* 70 - 79 */
  3513. 38, 38, 38, 38, 38, 39, 39, 39, 39, 39, /* 80 - 89 */
  3514. 39, 39, 39, 39, 39, 40, 40, 40, 40, 40 /* 90 - 99 */
  3515. };
  3516. /* Calculates a relative dB value from a ratio of linear
  3517. * (i.e. not dB) signal levels.
  3518. * Conversion assumes that levels are voltages (20*log), not powers (10*log). */
  3519. int iwl_calc_db_from_ratio(int sig_ratio)
  3520. {
  3521. /* Anything above 1000:1 just report as 60 dB */
  3522. if (sig_ratio > 1000)
  3523. return 60;
  3524. /* Above 100:1, divide by 10 and use table,
  3525. * add 20 dB to make up for divide by 10 */
  3526. if (sig_ratio > 100)
  3527. return (20 + (int)ratio2dB[sig_ratio/10]);
  3528. /* We shouldn't see this */
  3529. if (sig_ratio < 1)
  3530. return 0;
  3531. /* Use table for ratios 1:1 - 99:1 */
  3532. return (int)ratio2dB[sig_ratio];
  3533. }
  3534. #define PERFECT_RSSI (-20) /* dBm */
  3535. #define WORST_RSSI (-95) /* dBm */
  3536. #define RSSI_RANGE (PERFECT_RSSI - WORST_RSSI)
  3537. /* Calculate an indication of rx signal quality (a percentage, not dBm!).
  3538. * See http://www.ces.clemson.edu/linux/signal_quality.shtml for info
  3539. * about formulas used below. */
  3540. int iwl_calc_sig_qual(int rssi_dbm, int noise_dbm)
  3541. {
  3542. int sig_qual;
  3543. int degradation = PERFECT_RSSI - rssi_dbm;
  3544. /* If we get a noise measurement, use signal-to-noise ratio (SNR)
  3545. * as indicator; formula is (signal dbm - noise dbm).
  3546. * SNR at or above 40 is a great signal (100%).
  3547. * Below that, scale to fit SNR of 0 - 40 dB within 0 - 100% indicator.
  3548. * Weakest usable signal is usually 10 - 15 dB SNR. */
  3549. if (noise_dbm) {
  3550. if (rssi_dbm - noise_dbm >= 40)
  3551. return 100;
  3552. else if (rssi_dbm < noise_dbm)
  3553. return 0;
  3554. sig_qual = ((rssi_dbm - noise_dbm) * 5) / 2;
  3555. /* Else use just the signal level.
  3556. * This formula is a least squares fit of data points collected and
  3557. * compared with a reference system that had a percentage (%) display
  3558. * for signal quality. */
  3559. } else
  3560. sig_qual = (100 * (RSSI_RANGE * RSSI_RANGE) - degradation *
  3561. (15 * RSSI_RANGE + 62 * degradation)) /
  3562. (RSSI_RANGE * RSSI_RANGE);
  3563. if (sig_qual > 100)
  3564. sig_qual = 100;
  3565. else if (sig_qual < 1)
  3566. sig_qual = 0;
  3567. return sig_qual;
  3568. }
  3569. /**
  3570. * iwl_rx_handle - Main entry function for receiving responses from the uCode
  3571. *
  3572. * Uses the priv->rx_handlers callback function array to invoke
  3573. * the appropriate handlers, including command responses,
  3574. * frame-received notifications, and other notifications.
  3575. */
  3576. static void iwl_rx_handle(struct iwl_priv *priv)
  3577. {
  3578. struct iwl_rx_mem_buffer *rxb;
  3579. struct iwl_rx_packet *pkt;
  3580. struct iwl_rx_queue *rxq = &priv->rxq;
  3581. u32 r, i;
  3582. int reclaim;
  3583. unsigned long flags;
  3584. r = iwl_hw_get_rx_read(priv);
  3585. i = rxq->read;
  3586. /* Rx interrupt, but nothing sent from uCode */
  3587. if (i == r)
  3588. IWL_DEBUG(IWL_DL_RX | IWL_DL_ISR, "r = %d, i = %d\n", r, i);
  3589. while (i != r) {
  3590. rxb = rxq->queue[i];
  3591. /* If an RXB doesn't have a queue slot associated with it
  3592. * then a bug has been introduced in the queue refilling
  3593. * routines -- catch it here */
  3594. BUG_ON(rxb == NULL);
  3595. rxq->queue[i] = NULL;
  3596. pci_dma_sync_single_for_cpu(priv->pci_dev, rxb->dma_addr,
  3597. IWL_RX_BUF_SIZE,
  3598. PCI_DMA_FROMDEVICE);
  3599. pkt = (struct iwl_rx_packet *)rxb->skb->data;
  3600. /* Reclaim a command buffer only if this packet is a response
  3601. * to a (driver-originated) command.
  3602. * If the packet (e.g. Rx frame) originated from uCode,
  3603. * there is no command buffer to reclaim.
  3604. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  3605. * but apparently a few don't get set; catch them here. */
  3606. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  3607. (pkt->hdr.cmd != STATISTICS_NOTIFICATION) &&
  3608. (pkt->hdr.cmd != REPLY_TX);
  3609. /* Based on type of command response or notification,
  3610. * handle those that need handling via function in
  3611. * rx_handlers table. See iwl_setup_rx_handlers() */
  3612. if (priv->rx_handlers[pkt->hdr.cmd]) {
  3613. IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
  3614. "r = %d, i = %d, %s, 0x%02x\n", r, i,
  3615. get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  3616. priv->rx_handlers[pkt->hdr.cmd] (priv, rxb);
  3617. } else {
  3618. /* No handling needed */
  3619. IWL_DEBUG(IWL_DL_HOST_COMMAND | IWL_DL_RX | IWL_DL_ISR,
  3620. "r %d i %d No handler needed for %s, 0x%02x\n",
  3621. r, i, get_cmd_string(pkt->hdr.cmd),
  3622. pkt->hdr.cmd);
  3623. }
  3624. if (reclaim) {
  3625. /* Invoke any callbacks, transfer the skb to caller,
  3626. * and fire off the (possibly) blocking iwl_send_cmd()
  3627. * as we reclaim the driver command queue */
  3628. if (rxb && rxb->skb)
  3629. iwl_tx_cmd_complete(priv, rxb);
  3630. else
  3631. IWL_WARNING("Claim null rxb?\n");
  3632. }
  3633. /* For now we just don't re-use anything. We can tweak this
  3634. * later to try and re-use notification packets and SKBs that
  3635. * fail to Rx correctly */
  3636. if (rxb->skb != NULL) {
  3637. priv->alloc_rxb_skb--;
  3638. dev_kfree_skb_any(rxb->skb);
  3639. rxb->skb = NULL;
  3640. }
  3641. pci_unmap_single(priv->pci_dev, rxb->dma_addr,
  3642. IWL_RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
  3643. spin_lock_irqsave(&rxq->lock, flags);
  3644. list_add_tail(&rxb->list, &priv->rxq.rx_used);
  3645. spin_unlock_irqrestore(&rxq->lock, flags);
  3646. i = (i + 1) & RX_QUEUE_MASK;
  3647. }
  3648. /* Backtrack one entry */
  3649. priv->rxq.read = i;
  3650. iwl_rx_queue_restock(priv);
  3651. }
  3652. int iwl_tx_queue_update_write_ptr(struct iwl_priv *priv,
  3653. struct iwl_tx_queue *txq)
  3654. {
  3655. u32 reg = 0;
  3656. int rc = 0;
  3657. int txq_id = txq->q.id;
  3658. if (txq->need_update == 0)
  3659. return rc;
  3660. /* if we're trying to save power */
  3661. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  3662. /* wake up nic if it's powered down ...
  3663. * uCode will wake up, and interrupt us again, so next
  3664. * time we'll skip this part. */
  3665. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  3666. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  3667. IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
  3668. iwl_set_bit(priv, CSR_GP_CNTRL,
  3669. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  3670. return rc;
  3671. }
  3672. /* restore this queue's parameters in nic hardware. */
  3673. rc = iwl_grab_restricted_access(priv);
  3674. if (rc)
  3675. return rc;
  3676. iwl_write_restricted(priv, HBUS_TARG_WRPTR,
  3677. txq->q.write_ptr | (txq_id << 8));
  3678. iwl_release_restricted_access(priv);
  3679. /* else not in power-save mode, uCode will never sleep when we're
  3680. * trying to tx (during RFKILL, we're not trying to tx). */
  3681. } else
  3682. iwl_write32(priv, HBUS_TARG_WRPTR,
  3683. txq->q.write_ptr | (txq_id << 8));
  3684. txq->need_update = 0;
  3685. return rc;
  3686. }
  3687. #ifdef CONFIG_IWLWIFI_DEBUG
  3688. static void iwl_print_rx_config_cmd(struct iwl_rxon_cmd *rxon)
  3689. {
  3690. DECLARE_MAC_BUF(mac);
  3691. IWL_DEBUG_RADIO("RX CONFIG:\n");
  3692. iwl_print_hex_dump(IWL_DL_RADIO, (u8 *) rxon, sizeof(*rxon));
  3693. IWL_DEBUG_RADIO("u16 channel: 0x%x\n", le16_to_cpu(rxon->channel));
  3694. IWL_DEBUG_RADIO("u32 flags: 0x%08X\n", le32_to_cpu(rxon->flags));
  3695. IWL_DEBUG_RADIO("u32 filter_flags: 0x%08x\n",
  3696. le32_to_cpu(rxon->filter_flags));
  3697. IWL_DEBUG_RADIO("u8 dev_type: 0x%x\n", rxon->dev_type);
  3698. IWL_DEBUG_RADIO("u8 ofdm_basic_rates: 0x%02x\n",
  3699. rxon->ofdm_basic_rates);
  3700. IWL_DEBUG_RADIO("u8 cck_basic_rates: 0x%02x\n", rxon->cck_basic_rates);
  3701. IWL_DEBUG_RADIO("u8[6] node_addr: %s\n",
  3702. print_mac(mac, rxon->node_addr));
  3703. IWL_DEBUG_RADIO("u8[6] bssid_addr: %s\n",
  3704. print_mac(mac, rxon->bssid_addr));
  3705. IWL_DEBUG_RADIO("u16 assoc_id: 0x%x\n", le16_to_cpu(rxon->assoc_id));
  3706. }
  3707. #endif
  3708. static void iwl_enable_interrupts(struct iwl_priv *priv)
  3709. {
  3710. IWL_DEBUG_ISR("Enabling interrupts\n");
  3711. set_bit(STATUS_INT_ENABLED, &priv->status);
  3712. iwl_write32(priv, CSR_INT_MASK, CSR_INI_SET_MASK);
  3713. }
  3714. static inline void iwl_disable_interrupts(struct iwl_priv *priv)
  3715. {
  3716. clear_bit(STATUS_INT_ENABLED, &priv->status);
  3717. /* disable interrupts from uCode/NIC to host */
  3718. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  3719. /* acknowledge/clear/reset any interrupts still pending
  3720. * from uCode or flow handler (Rx/Tx DMA) */
  3721. iwl_write32(priv, CSR_INT, 0xffffffff);
  3722. iwl_write32(priv, CSR_FH_INT_STATUS, 0xffffffff);
  3723. IWL_DEBUG_ISR("Disabled interrupts\n");
  3724. }
  3725. static const char *desc_lookup(int i)
  3726. {
  3727. switch (i) {
  3728. case 1:
  3729. return "FAIL";
  3730. case 2:
  3731. return "BAD_PARAM";
  3732. case 3:
  3733. return "BAD_CHECKSUM";
  3734. case 4:
  3735. return "NMI_INTERRUPT";
  3736. case 5:
  3737. return "SYSASSERT";
  3738. case 6:
  3739. return "FATAL_ERROR";
  3740. }
  3741. return "UNKNOWN";
  3742. }
  3743. #define ERROR_START_OFFSET (1 * sizeof(u32))
  3744. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  3745. static void iwl_dump_nic_error_log(struct iwl_priv *priv)
  3746. {
  3747. u32 i;
  3748. u32 desc, time, count, base, data1;
  3749. u32 blink1, blink2, ilink1, ilink2;
  3750. int rc;
  3751. base = le32_to_cpu(priv->card_alive.error_event_table_ptr);
  3752. if (!iwl_hw_valid_rtc_data_addr(base)) {
  3753. IWL_ERROR("Not valid error log pointer 0x%08X\n", base);
  3754. return;
  3755. }
  3756. rc = iwl_grab_restricted_access(priv);
  3757. if (rc) {
  3758. IWL_WARNING("Can not read from adapter at this time.\n");
  3759. return;
  3760. }
  3761. count = iwl_read_restricted_mem(priv, base);
  3762. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  3763. IWL_ERROR("Start IWL Error Log Dump:\n");
  3764. IWL_ERROR("Status: 0x%08lX, Config: %08X count: %d\n",
  3765. priv->status, priv->config, count);
  3766. }
  3767. IWL_ERROR("Desc Time asrtPC blink2 "
  3768. "ilink1 nmiPC Line\n");
  3769. for (i = ERROR_START_OFFSET;
  3770. i < (count * ERROR_ELEM_SIZE) + ERROR_START_OFFSET;
  3771. i += ERROR_ELEM_SIZE) {
  3772. desc = iwl_read_restricted_mem(priv, base + i);
  3773. time =
  3774. iwl_read_restricted_mem(priv, base + i + 1 * sizeof(u32));
  3775. blink1 =
  3776. iwl_read_restricted_mem(priv, base + i + 2 * sizeof(u32));
  3777. blink2 =
  3778. iwl_read_restricted_mem(priv, base + i + 3 * sizeof(u32));
  3779. ilink1 =
  3780. iwl_read_restricted_mem(priv, base + i + 4 * sizeof(u32));
  3781. ilink2 =
  3782. iwl_read_restricted_mem(priv, base + i + 5 * sizeof(u32));
  3783. data1 =
  3784. iwl_read_restricted_mem(priv, base + i + 6 * sizeof(u32));
  3785. IWL_ERROR
  3786. ("%-13s (#%d) %010u 0x%05X 0x%05X 0x%05X 0x%05X %u\n\n",
  3787. desc_lookup(desc), desc, time, blink1, blink2,
  3788. ilink1, ilink2, data1);
  3789. }
  3790. iwl_release_restricted_access(priv);
  3791. }
  3792. #define EVENT_START_OFFSET (4 * sizeof(u32))
  3793. /**
  3794. * iwl_print_event_log - Dump error event log to syslog
  3795. *
  3796. * NOTE: Must be called with iwl_grab_restricted_access() already obtained!
  3797. */
  3798. static void iwl_print_event_log(struct iwl_priv *priv, u32 start_idx,
  3799. u32 num_events, u32 mode)
  3800. {
  3801. u32 i;
  3802. u32 base; /* SRAM byte address of event log header */
  3803. u32 event_size; /* 2 u32s, or 3 u32s if timestamp recorded */
  3804. u32 ptr; /* SRAM byte address of log data */
  3805. u32 ev, time, data; /* event log data */
  3806. if (num_events == 0)
  3807. return;
  3808. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  3809. if (mode == 0)
  3810. event_size = 2 * sizeof(u32);
  3811. else
  3812. event_size = 3 * sizeof(u32);
  3813. ptr = base + EVENT_START_OFFSET + (start_idx * event_size);
  3814. /* "time" is actually "data" for mode 0 (no timestamp).
  3815. * place event id # at far right for easier visual parsing. */
  3816. for (i = 0; i < num_events; i++) {
  3817. ev = iwl_read_restricted_mem(priv, ptr);
  3818. ptr += sizeof(u32);
  3819. time = iwl_read_restricted_mem(priv, ptr);
  3820. ptr += sizeof(u32);
  3821. if (mode == 0)
  3822. IWL_ERROR("0x%08x\t%04u\n", time, ev); /* data, ev */
  3823. else {
  3824. data = iwl_read_restricted_mem(priv, ptr);
  3825. ptr += sizeof(u32);
  3826. IWL_ERROR("%010u\t0x%08x\t%04u\n", time, data, ev);
  3827. }
  3828. }
  3829. }
  3830. static void iwl_dump_nic_event_log(struct iwl_priv *priv)
  3831. {
  3832. int rc;
  3833. u32 base; /* SRAM byte address of event log header */
  3834. u32 capacity; /* event log capacity in # entries */
  3835. u32 mode; /* 0 - no timestamp, 1 - timestamp recorded */
  3836. u32 num_wraps; /* # times uCode wrapped to top of log */
  3837. u32 next_entry; /* index of next entry to be written by uCode */
  3838. u32 size; /* # entries that we'll print */
  3839. base = le32_to_cpu(priv->card_alive.log_event_table_ptr);
  3840. if (!iwl_hw_valid_rtc_data_addr(base)) {
  3841. IWL_ERROR("Invalid event log pointer 0x%08X\n", base);
  3842. return;
  3843. }
  3844. rc = iwl_grab_restricted_access(priv);
  3845. if (rc) {
  3846. IWL_WARNING("Can not read from adapter at this time.\n");
  3847. return;
  3848. }
  3849. /* event log header */
  3850. capacity = iwl_read_restricted_mem(priv, base);
  3851. mode = iwl_read_restricted_mem(priv, base + (1 * sizeof(u32)));
  3852. num_wraps = iwl_read_restricted_mem(priv, base + (2 * sizeof(u32)));
  3853. next_entry = iwl_read_restricted_mem(priv, base + (3 * sizeof(u32)));
  3854. size = num_wraps ? capacity : next_entry;
  3855. /* bail out if nothing in log */
  3856. if (size == 0) {
  3857. IWL_ERROR("Start IWL Event Log Dump: nothing in log\n");
  3858. iwl_release_restricted_access(priv);
  3859. return;
  3860. }
  3861. IWL_ERROR("Start IWL Event Log Dump: display count %d, wraps %d\n",
  3862. size, num_wraps);
  3863. /* if uCode has wrapped back to top of log, start at the oldest entry,
  3864. * i.e the next one that uCode would fill. */
  3865. if (num_wraps)
  3866. iwl_print_event_log(priv, next_entry,
  3867. capacity - next_entry, mode);
  3868. /* (then/else) start at top of log */
  3869. iwl_print_event_log(priv, 0, next_entry, mode);
  3870. iwl_release_restricted_access(priv);
  3871. }
  3872. /**
  3873. * iwl_irq_handle_error - called for HW or SW error interrupt from card
  3874. */
  3875. static void iwl_irq_handle_error(struct iwl_priv *priv)
  3876. {
  3877. /* Set the FW error flag -- cleared on iwl_down */
  3878. set_bit(STATUS_FW_ERROR, &priv->status);
  3879. /* Cancel currently queued command. */
  3880. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  3881. #ifdef CONFIG_IWLWIFI_DEBUG
  3882. if (iwl_debug_level & IWL_DL_FW_ERRORS) {
  3883. iwl_dump_nic_error_log(priv);
  3884. iwl_dump_nic_event_log(priv);
  3885. iwl_print_rx_config_cmd(&priv->staging_rxon);
  3886. }
  3887. #endif
  3888. wake_up_interruptible(&priv->wait_command_queue);
  3889. /* Keep the restart process from trying to send host
  3890. * commands by clearing the INIT status bit */
  3891. clear_bit(STATUS_READY, &priv->status);
  3892. if (!test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  3893. IWL_DEBUG(IWL_DL_INFO | IWL_DL_FW_ERRORS,
  3894. "Restarting adapter due to uCode error.\n");
  3895. if (iwl_is_associated(priv)) {
  3896. memcpy(&priv->recovery_rxon, &priv->active_rxon,
  3897. sizeof(priv->recovery_rxon));
  3898. priv->error_recovering = 1;
  3899. }
  3900. queue_work(priv->workqueue, &priv->restart);
  3901. }
  3902. }
  3903. static void iwl_error_recovery(struct iwl_priv *priv)
  3904. {
  3905. unsigned long flags;
  3906. memcpy(&priv->staging_rxon, &priv->recovery_rxon,
  3907. sizeof(priv->staging_rxon));
  3908. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  3909. iwl_commit_rxon(priv);
  3910. iwl_add_station(priv, priv->bssid, 1, 0);
  3911. spin_lock_irqsave(&priv->lock, flags);
  3912. priv->assoc_id = le16_to_cpu(priv->staging_rxon.assoc_id);
  3913. priv->error_recovering = 0;
  3914. spin_unlock_irqrestore(&priv->lock, flags);
  3915. }
  3916. static void iwl_irq_tasklet(struct iwl_priv *priv)
  3917. {
  3918. u32 inta, handled = 0;
  3919. u32 inta_fh;
  3920. unsigned long flags;
  3921. #ifdef CONFIG_IWLWIFI_DEBUG
  3922. u32 inta_mask;
  3923. #endif
  3924. spin_lock_irqsave(&priv->lock, flags);
  3925. /* Ack/clear/reset pending uCode interrupts.
  3926. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  3927. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  3928. inta = iwl_read32(priv, CSR_INT);
  3929. iwl_write32(priv, CSR_INT, inta);
  3930. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  3931. * Any new interrupts that happen after this, either while we're
  3932. * in this tasklet, or later, will show up in next ISR/tasklet. */
  3933. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  3934. iwl_write32(priv, CSR_FH_INT_STATUS, inta_fh);
  3935. #ifdef CONFIG_IWLWIFI_DEBUG
  3936. if (iwl_debug_level & IWL_DL_ISR) {
  3937. inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
  3938. IWL_DEBUG_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  3939. inta, inta_mask, inta_fh);
  3940. }
  3941. #endif
  3942. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  3943. * atomic, make sure that inta covers all the interrupts that
  3944. * we've discovered, even if FH interrupt came in just after
  3945. * reading CSR_INT. */
  3946. if (inta_fh & CSR_FH_INT_RX_MASK)
  3947. inta |= CSR_INT_BIT_FH_RX;
  3948. if (inta_fh & CSR_FH_INT_TX_MASK)
  3949. inta |= CSR_INT_BIT_FH_TX;
  3950. /* Now service all interrupt bits discovered above. */
  3951. if (inta & CSR_INT_BIT_HW_ERR) {
  3952. IWL_ERROR("Microcode HW error detected. Restarting.\n");
  3953. /* Tell the device to stop sending interrupts */
  3954. iwl_disable_interrupts(priv);
  3955. iwl_irq_handle_error(priv);
  3956. handled |= CSR_INT_BIT_HW_ERR;
  3957. spin_unlock_irqrestore(&priv->lock, flags);
  3958. return;
  3959. }
  3960. #ifdef CONFIG_IWLWIFI_DEBUG
  3961. if (iwl_debug_level & (IWL_DL_ISR)) {
  3962. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  3963. if (inta & CSR_INT_BIT_MAC_CLK_ACTV)
  3964. IWL_DEBUG_ISR("Microcode started or stopped.\n");
  3965. /* Alive notification via Rx interrupt will do the real work */
  3966. if (inta & CSR_INT_BIT_ALIVE)
  3967. IWL_DEBUG_ISR("Alive interrupt\n");
  3968. }
  3969. #endif
  3970. /* Safely ignore these bits for debug checks below */
  3971. inta &= ~(CSR_INT_BIT_MAC_CLK_ACTV | CSR_INT_BIT_ALIVE);
  3972. /* HW RF KILL switch toggled (4965 only) */
  3973. if (inta & CSR_INT_BIT_RF_KILL) {
  3974. int hw_rf_kill = 0;
  3975. if (!(iwl_read32(priv, CSR_GP_CNTRL) &
  3976. CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  3977. hw_rf_kill = 1;
  3978. IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL | IWL_DL_ISR,
  3979. "RF_KILL bit toggled to %s.\n",
  3980. hw_rf_kill ? "disable radio":"enable radio");
  3981. /* Queue restart only if RF_KILL switch was set to "kill"
  3982. * when we loaded driver, and is now set to "enable".
  3983. * After we're Alive, RF_KILL gets handled by
  3984. * iwl_rx_card_state_notif() */
  3985. if (!hw_rf_kill && !test_bit(STATUS_ALIVE, &priv->status)) {
  3986. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  3987. queue_work(priv->workqueue, &priv->restart);
  3988. }
  3989. handled |= CSR_INT_BIT_RF_KILL;
  3990. }
  3991. /* Chip got too hot and stopped itself (4965 only) */
  3992. if (inta & CSR_INT_BIT_CT_KILL) {
  3993. IWL_ERROR("Microcode CT kill error detected.\n");
  3994. handled |= CSR_INT_BIT_CT_KILL;
  3995. }
  3996. /* Error detected by uCode */
  3997. if (inta & CSR_INT_BIT_SW_ERR) {
  3998. IWL_ERROR("Microcode SW error detected. Restarting 0x%X.\n",
  3999. inta);
  4000. iwl_irq_handle_error(priv);
  4001. handled |= CSR_INT_BIT_SW_ERR;
  4002. }
  4003. /* uCode wakes up after power-down sleep */
  4004. if (inta & CSR_INT_BIT_WAKEUP) {
  4005. IWL_DEBUG_ISR("Wakeup interrupt\n");
  4006. iwl_rx_queue_update_write_ptr(priv, &priv->rxq);
  4007. iwl_tx_queue_update_write_ptr(priv, &priv->txq[0]);
  4008. iwl_tx_queue_update_write_ptr(priv, &priv->txq[1]);
  4009. iwl_tx_queue_update_write_ptr(priv, &priv->txq[2]);
  4010. iwl_tx_queue_update_write_ptr(priv, &priv->txq[3]);
  4011. iwl_tx_queue_update_write_ptr(priv, &priv->txq[4]);
  4012. iwl_tx_queue_update_write_ptr(priv, &priv->txq[5]);
  4013. handled |= CSR_INT_BIT_WAKEUP;
  4014. }
  4015. /* All uCode command responses, including Tx command responses,
  4016. * Rx "responses" (frame-received notification), and other
  4017. * notifications from uCode come through here*/
  4018. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  4019. iwl_rx_handle(priv);
  4020. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  4021. }
  4022. if (inta & CSR_INT_BIT_FH_TX) {
  4023. IWL_DEBUG_ISR("Tx interrupt\n");
  4024. iwl_write32(priv, CSR_FH_INT_STATUS, (1 << 6));
  4025. if (!iwl_grab_restricted_access(priv)) {
  4026. iwl_write_restricted(priv,
  4027. FH_TCSR_CREDIT
  4028. (ALM_FH_SRVC_CHNL), 0x0);
  4029. iwl_release_restricted_access(priv);
  4030. }
  4031. handled |= CSR_INT_BIT_FH_TX;
  4032. }
  4033. if (inta & ~handled)
  4034. IWL_ERROR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
  4035. if (inta & ~CSR_INI_SET_MASK) {
  4036. IWL_WARNING("Disabled INTA bits 0x%08x were pending\n",
  4037. inta & ~CSR_INI_SET_MASK);
  4038. IWL_WARNING(" with FH_INT = 0x%08x\n", inta_fh);
  4039. }
  4040. /* Re-enable all interrupts */
  4041. iwl_enable_interrupts(priv);
  4042. #ifdef CONFIG_IWLWIFI_DEBUG
  4043. if (iwl_debug_level & (IWL_DL_ISR)) {
  4044. inta = iwl_read32(priv, CSR_INT);
  4045. inta_mask = iwl_read32(priv, CSR_INT_MASK);
  4046. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  4047. IWL_DEBUG_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  4048. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  4049. }
  4050. #endif
  4051. spin_unlock_irqrestore(&priv->lock, flags);
  4052. }
  4053. static irqreturn_t iwl_isr(int irq, void *data)
  4054. {
  4055. struct iwl_priv *priv = data;
  4056. u32 inta, inta_mask;
  4057. u32 inta_fh;
  4058. if (!priv)
  4059. return IRQ_NONE;
  4060. spin_lock(&priv->lock);
  4061. /* Disable (but don't clear!) interrupts here to avoid
  4062. * back-to-back ISRs and sporadic interrupts from our NIC.
  4063. * If we have something to service, the tasklet will re-enable ints.
  4064. * If we *don't* have something, we'll re-enable before leaving here. */
  4065. inta_mask = iwl_read32(priv, CSR_INT_MASK); /* just for debug */
  4066. iwl_write32(priv, CSR_INT_MASK, 0x00000000);
  4067. /* Discover which interrupts are active/pending */
  4068. inta = iwl_read32(priv, CSR_INT);
  4069. inta_fh = iwl_read32(priv, CSR_FH_INT_STATUS);
  4070. /* Ignore interrupt if there's nothing in NIC to service.
  4071. * This may be due to IRQ shared with another device,
  4072. * or due to sporadic interrupts thrown from our NIC. */
  4073. if (!inta && !inta_fh) {
  4074. IWL_DEBUG_ISR("Ignore interrupt, inta == 0, inta_fh == 0\n");
  4075. goto none;
  4076. }
  4077. if ((inta == 0xFFFFFFFF) || ((inta & 0xFFFFFFF0) == 0xa5a5a5a0)) {
  4078. /* Hardware disappeared */
  4079. IWL_WARNING("HARDWARE GONE?? INTA == 0x%080x\n", inta);
  4080. goto unplugged;
  4081. }
  4082. IWL_DEBUG_ISR("ISR inta 0x%08x, enabled 0x%08x, fh 0x%08x\n",
  4083. inta, inta_mask, inta_fh);
  4084. /* iwl_irq_tasklet() will service interrupts and re-enable them */
  4085. tasklet_schedule(&priv->irq_tasklet);
  4086. unplugged:
  4087. spin_unlock(&priv->lock);
  4088. return IRQ_HANDLED;
  4089. none:
  4090. /* re-enable interrupts here since we don't have anything to service. */
  4091. iwl_enable_interrupts(priv);
  4092. spin_unlock(&priv->lock);
  4093. return IRQ_NONE;
  4094. }
  4095. /************************** EEPROM BANDS ****************************
  4096. *
  4097. * The iwl_eeprom_band definitions below provide the mapping from the
  4098. * EEPROM contents to the specific channel number supported for each
  4099. * band.
  4100. *
  4101. * For example, iwl_priv->eeprom.band_3_channels[4] from the band_3
  4102. * definition below maps to physical channel 42 in the 5.2GHz spectrum.
  4103. * The specific geography and calibration information for that channel
  4104. * is contained in the eeprom map itself.
  4105. *
  4106. * During init, we copy the eeprom information and channel map
  4107. * information into priv->channel_info_24/52 and priv->channel_map_24/52
  4108. *
  4109. * channel_map_24/52 provides the index in the channel_info array for a
  4110. * given channel. We have to have two separate maps as there is channel
  4111. * overlap with the 2.4GHz and 5.2GHz spectrum as seen in band_1 and
  4112. * band_2
  4113. *
  4114. * A value of 0xff stored in the channel_map indicates that the channel
  4115. * is not supported by the hardware at all.
  4116. *
  4117. * A value of 0xfe in the channel_map indicates that the channel is not
  4118. * valid for Tx with the current hardware. This means that
  4119. * while the system can tune and receive on a given channel, it may not
  4120. * be able to associate or transmit any frames on that
  4121. * channel. There is no corresponding channel information for that
  4122. * entry.
  4123. *
  4124. *********************************************************************/
  4125. /* 2.4 GHz */
  4126. static const u8 iwl_eeprom_band_1[14] = {
  4127. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  4128. };
  4129. /* 5.2 GHz bands */
  4130. static const u8 iwl_eeprom_band_2[] = {
  4131. 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16
  4132. };
  4133. static const u8 iwl_eeprom_band_3[] = { /* 5205-5320MHz */
  4134. 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64
  4135. };
  4136. static const u8 iwl_eeprom_band_4[] = { /* 5500-5700MHz */
  4137. 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140
  4138. };
  4139. static const u8 iwl_eeprom_band_5[] = { /* 5725-5825MHz */
  4140. 145, 149, 153, 157, 161, 165
  4141. };
  4142. static void iwl_init_band_reference(const struct iwl_priv *priv, int band,
  4143. int *eeprom_ch_count,
  4144. const struct iwl_eeprom_channel
  4145. **eeprom_ch_info,
  4146. const u8 **eeprom_ch_index)
  4147. {
  4148. switch (band) {
  4149. case 1: /* 2.4GHz band */
  4150. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_1);
  4151. *eeprom_ch_info = priv->eeprom.band_1_channels;
  4152. *eeprom_ch_index = iwl_eeprom_band_1;
  4153. break;
  4154. case 2: /* 5.2GHz band */
  4155. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_2);
  4156. *eeprom_ch_info = priv->eeprom.band_2_channels;
  4157. *eeprom_ch_index = iwl_eeprom_band_2;
  4158. break;
  4159. case 3: /* 5.2GHz band */
  4160. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_3);
  4161. *eeprom_ch_info = priv->eeprom.band_3_channels;
  4162. *eeprom_ch_index = iwl_eeprom_band_3;
  4163. break;
  4164. case 4: /* 5.2GHz band */
  4165. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_4);
  4166. *eeprom_ch_info = priv->eeprom.band_4_channels;
  4167. *eeprom_ch_index = iwl_eeprom_band_4;
  4168. break;
  4169. case 5: /* 5.2GHz band */
  4170. *eeprom_ch_count = ARRAY_SIZE(iwl_eeprom_band_5);
  4171. *eeprom_ch_info = priv->eeprom.band_5_channels;
  4172. *eeprom_ch_index = iwl_eeprom_band_5;
  4173. break;
  4174. default:
  4175. BUG();
  4176. return;
  4177. }
  4178. }
  4179. const struct iwl_channel_info *iwl_get_channel_info(const struct iwl_priv *priv,
  4180. int phymode, u16 channel)
  4181. {
  4182. int i;
  4183. switch (phymode) {
  4184. case MODE_IEEE80211A:
  4185. for (i = 14; i < priv->channel_count; i++) {
  4186. if (priv->channel_info[i].channel == channel)
  4187. return &priv->channel_info[i];
  4188. }
  4189. break;
  4190. case MODE_IEEE80211B:
  4191. case MODE_IEEE80211G:
  4192. if (channel >= 1 && channel <= 14)
  4193. return &priv->channel_info[channel - 1];
  4194. break;
  4195. }
  4196. return NULL;
  4197. }
  4198. #define CHECK_AND_PRINT(x) ((eeprom_ch_info[ch].flags & EEPROM_CHANNEL_##x) \
  4199. ? # x " " : "")
  4200. static int iwl_init_channel_map(struct iwl_priv *priv)
  4201. {
  4202. int eeprom_ch_count = 0;
  4203. const u8 *eeprom_ch_index = NULL;
  4204. const struct iwl_eeprom_channel *eeprom_ch_info = NULL;
  4205. int band, ch;
  4206. struct iwl_channel_info *ch_info;
  4207. if (priv->channel_count) {
  4208. IWL_DEBUG_INFO("Channel map already initialized.\n");
  4209. return 0;
  4210. }
  4211. if (priv->eeprom.version < 0x2f) {
  4212. IWL_WARNING("Unsupported EEPROM version: 0x%04X\n",
  4213. priv->eeprom.version);
  4214. return -EINVAL;
  4215. }
  4216. IWL_DEBUG_INFO("Initializing regulatory info from EEPROM\n");
  4217. priv->channel_count =
  4218. ARRAY_SIZE(iwl_eeprom_band_1) +
  4219. ARRAY_SIZE(iwl_eeprom_band_2) +
  4220. ARRAY_SIZE(iwl_eeprom_band_3) +
  4221. ARRAY_SIZE(iwl_eeprom_band_4) +
  4222. ARRAY_SIZE(iwl_eeprom_band_5);
  4223. IWL_DEBUG_INFO("Parsing data for %d channels.\n", priv->channel_count);
  4224. priv->channel_info = kzalloc(sizeof(struct iwl_channel_info) *
  4225. priv->channel_count, GFP_KERNEL);
  4226. if (!priv->channel_info) {
  4227. IWL_ERROR("Could not allocate channel_info\n");
  4228. priv->channel_count = 0;
  4229. return -ENOMEM;
  4230. }
  4231. ch_info = priv->channel_info;
  4232. /* Loop through the 5 EEPROM bands adding them in order to the
  4233. * channel map we maintain (that contains additional information than
  4234. * what just in the EEPROM) */
  4235. for (band = 1; band <= 5; band++) {
  4236. iwl_init_band_reference(priv, band, &eeprom_ch_count,
  4237. &eeprom_ch_info, &eeprom_ch_index);
  4238. /* Loop through each band adding each of the channels */
  4239. for (ch = 0; ch < eeprom_ch_count; ch++) {
  4240. ch_info->channel = eeprom_ch_index[ch];
  4241. ch_info->phymode = (band == 1) ? MODE_IEEE80211B :
  4242. MODE_IEEE80211A;
  4243. /* permanently store EEPROM's channel regulatory flags
  4244. * and max power in channel info database. */
  4245. ch_info->eeprom = eeprom_ch_info[ch];
  4246. /* Copy the run-time flags so they are there even on
  4247. * invalid channels */
  4248. ch_info->flags = eeprom_ch_info[ch].flags;
  4249. if (!(is_channel_valid(ch_info))) {
  4250. IWL_DEBUG_INFO("Ch. %d Flags %x [%sGHz] - "
  4251. "No traffic\n",
  4252. ch_info->channel,
  4253. ch_info->flags,
  4254. is_channel_a_band(ch_info) ?
  4255. "5.2" : "2.4");
  4256. ch_info++;
  4257. continue;
  4258. }
  4259. /* Initialize regulatory-based run-time data */
  4260. ch_info->max_power_avg = ch_info->curr_txpow =
  4261. eeprom_ch_info[ch].max_power_avg;
  4262. ch_info->scan_power = eeprom_ch_info[ch].max_power_avg;
  4263. ch_info->min_power = 0;
  4264. IWL_DEBUG_INFO("Ch. %d [%sGHz] %s%s%s%s%s%s(0x%02x"
  4265. " %ddBm): Ad-Hoc %ssupported\n",
  4266. ch_info->channel,
  4267. is_channel_a_band(ch_info) ?
  4268. "5.2" : "2.4",
  4269. CHECK_AND_PRINT(IBSS),
  4270. CHECK_AND_PRINT(ACTIVE),
  4271. CHECK_AND_PRINT(RADAR),
  4272. CHECK_AND_PRINT(WIDE),
  4273. CHECK_AND_PRINT(NARROW),
  4274. CHECK_AND_PRINT(DFS),
  4275. eeprom_ch_info[ch].flags,
  4276. eeprom_ch_info[ch].max_power_avg,
  4277. ((eeprom_ch_info[ch].
  4278. flags & EEPROM_CHANNEL_IBSS)
  4279. && !(eeprom_ch_info[ch].
  4280. flags & EEPROM_CHANNEL_RADAR))
  4281. ? "" : "not ");
  4282. /* Set the user_txpower_limit to the highest power
  4283. * supported by any channel */
  4284. if (eeprom_ch_info[ch].max_power_avg >
  4285. priv->user_txpower_limit)
  4286. priv->user_txpower_limit =
  4287. eeprom_ch_info[ch].max_power_avg;
  4288. ch_info++;
  4289. }
  4290. }
  4291. if (iwl3945_txpower_set_from_eeprom(priv))
  4292. return -EIO;
  4293. return 0;
  4294. }
  4295. /* For active scan, listen ACTIVE_DWELL_TIME (msec) on each channel after
  4296. * sending probe req. This should be set long enough to hear probe responses
  4297. * from more than one AP. */
  4298. #define IWL_ACTIVE_DWELL_TIME_24 (20) /* all times in msec */
  4299. #define IWL_ACTIVE_DWELL_TIME_52 (10)
  4300. /* For faster active scanning, scan will move to the next channel if fewer than
  4301. * PLCP_QUIET_THRESH packets are heard on this channel within
  4302. * ACTIVE_QUIET_TIME after sending probe request. This shortens the dwell
  4303. * time if it's a quiet channel (nothing responded to our probe, and there's
  4304. * no other traffic).
  4305. * Disable "quiet" feature by setting PLCP_QUIET_THRESH to 0. */
  4306. #define IWL_PLCP_QUIET_THRESH __constant_cpu_to_le16(1) /* packets */
  4307. #define IWL_ACTIVE_QUIET_TIME __constant_cpu_to_le16(5) /* msec */
  4308. /* For passive scan, listen PASSIVE_DWELL_TIME (msec) on each channel.
  4309. * Must be set longer than active dwell time.
  4310. * For the most reliable scan, set > AP beacon interval (typically 100msec). */
  4311. #define IWL_PASSIVE_DWELL_TIME_24 (20) /* all times in msec */
  4312. #define IWL_PASSIVE_DWELL_TIME_52 (10)
  4313. #define IWL_PASSIVE_DWELL_BASE (100)
  4314. #define IWL_CHANNEL_TUNE_TIME 5
  4315. static inline u16 iwl_get_active_dwell_time(struct iwl_priv *priv, int phymode)
  4316. {
  4317. if (phymode == MODE_IEEE80211A)
  4318. return IWL_ACTIVE_DWELL_TIME_52;
  4319. else
  4320. return IWL_ACTIVE_DWELL_TIME_24;
  4321. }
  4322. static u16 iwl_get_passive_dwell_time(struct iwl_priv *priv, int phymode)
  4323. {
  4324. u16 active = iwl_get_active_dwell_time(priv, phymode);
  4325. u16 passive = (phymode != MODE_IEEE80211A) ?
  4326. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_24 :
  4327. IWL_PASSIVE_DWELL_BASE + IWL_PASSIVE_DWELL_TIME_52;
  4328. if (iwl_is_associated(priv)) {
  4329. /* If we're associated, we clamp the maximum passive
  4330. * dwell time to be 98% of the beacon interval (minus
  4331. * 2 * channel tune time) */
  4332. passive = priv->beacon_int;
  4333. if ((passive > IWL_PASSIVE_DWELL_BASE) || !passive)
  4334. passive = IWL_PASSIVE_DWELL_BASE;
  4335. passive = (passive * 98) / 100 - IWL_CHANNEL_TUNE_TIME * 2;
  4336. }
  4337. if (passive <= active)
  4338. passive = active + 1;
  4339. return passive;
  4340. }
  4341. static int iwl_get_channels_for_scan(struct iwl_priv *priv, int phymode,
  4342. u8 is_active, u8 direct_mask,
  4343. struct iwl_scan_channel *scan_ch)
  4344. {
  4345. const struct ieee80211_channel *channels = NULL;
  4346. const struct ieee80211_hw_mode *hw_mode;
  4347. const struct iwl_channel_info *ch_info;
  4348. u16 passive_dwell = 0;
  4349. u16 active_dwell = 0;
  4350. int added, i;
  4351. hw_mode = iwl_get_hw_mode(priv, phymode);
  4352. if (!hw_mode)
  4353. return 0;
  4354. channels = hw_mode->channels;
  4355. active_dwell = iwl_get_active_dwell_time(priv, phymode);
  4356. passive_dwell = iwl_get_passive_dwell_time(priv, phymode);
  4357. for (i = 0, added = 0; i < hw_mode->num_channels; i++) {
  4358. if (channels[i].chan ==
  4359. le16_to_cpu(priv->active_rxon.channel)) {
  4360. if (iwl_is_associated(priv)) {
  4361. IWL_DEBUG_SCAN
  4362. ("Skipping current channel %d\n",
  4363. le16_to_cpu(priv->active_rxon.channel));
  4364. continue;
  4365. }
  4366. } else if (priv->only_active_channel)
  4367. continue;
  4368. scan_ch->channel = channels[i].chan;
  4369. ch_info = iwl_get_channel_info(priv, phymode, scan_ch->channel);
  4370. if (!is_channel_valid(ch_info)) {
  4371. IWL_DEBUG_SCAN("Channel %d is INVALID for this SKU.\n",
  4372. scan_ch->channel);
  4373. continue;
  4374. }
  4375. if (!is_active || is_channel_passive(ch_info) ||
  4376. !(channels[i].flag & IEEE80211_CHAN_W_ACTIVE_SCAN))
  4377. scan_ch->type = 0; /* passive */
  4378. else
  4379. scan_ch->type = 1; /* active */
  4380. if (scan_ch->type & 1)
  4381. scan_ch->type |= (direct_mask << 1);
  4382. if (is_channel_narrow(ch_info))
  4383. scan_ch->type |= (1 << 7);
  4384. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  4385. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  4386. /* Set power levels to defaults */
  4387. scan_ch->tpc.dsp_atten = 110;
  4388. /* scan_pwr_info->tpc.dsp_atten; */
  4389. /*scan_pwr_info->tpc.tx_gain; */
  4390. if (phymode == MODE_IEEE80211A)
  4391. scan_ch->tpc.tx_gain = ((1 << 5) | (3 << 3)) | 3;
  4392. else {
  4393. scan_ch->tpc.tx_gain = ((1 << 5) | (5 << 3));
  4394. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  4395. * power level
  4396. scan_ch->tpc.tx_gain = ((1<<5) | (2 << 3)) | 3;
  4397. */
  4398. }
  4399. IWL_DEBUG_SCAN("Scanning %d [%s %d]\n",
  4400. scan_ch->channel,
  4401. (scan_ch->type & 1) ? "ACTIVE" : "PASSIVE",
  4402. (scan_ch->type & 1) ?
  4403. active_dwell : passive_dwell);
  4404. scan_ch++;
  4405. added++;
  4406. }
  4407. IWL_DEBUG_SCAN("total channels to scan %d \n", added);
  4408. return added;
  4409. }
  4410. static void iwl_reset_channel_flag(struct iwl_priv *priv)
  4411. {
  4412. int i, j;
  4413. for (i = 0; i < 3; i++) {
  4414. struct ieee80211_hw_mode *hw_mode = (void *)&priv->modes[i];
  4415. for (j = 0; j < hw_mode->num_channels; j++)
  4416. hw_mode->channels[j].flag = hw_mode->channels[j].val;
  4417. }
  4418. }
  4419. static void iwl_init_hw_rates(struct iwl_priv *priv,
  4420. struct ieee80211_rate *rates)
  4421. {
  4422. int i;
  4423. for (i = 0; i < IWL_RATE_COUNT; i++) {
  4424. rates[i].rate = iwl_rates[i].ieee * 5;
  4425. rates[i].val = i; /* Rate scaling will work on indexes */
  4426. rates[i].val2 = i;
  4427. rates[i].flags = IEEE80211_RATE_SUPPORTED;
  4428. /* Only OFDM have the bits-per-symbol set */
  4429. if ((i <= IWL_LAST_OFDM_RATE) && (i >= IWL_FIRST_OFDM_RATE))
  4430. rates[i].flags |= IEEE80211_RATE_OFDM;
  4431. else {
  4432. /*
  4433. * If CCK 1M then set rate flag to CCK else CCK_2
  4434. * which is CCK | PREAMBLE2
  4435. */
  4436. rates[i].flags |= (iwl_rates[i].plcp == 10) ?
  4437. IEEE80211_RATE_CCK : IEEE80211_RATE_CCK_2;
  4438. }
  4439. /* Set up which ones are basic rates... */
  4440. if (IWL_BASIC_RATES_MASK & (1 << i))
  4441. rates[i].flags |= IEEE80211_RATE_BASIC;
  4442. }
  4443. }
  4444. /**
  4445. * iwl_init_geos - Initialize mac80211's geo/channel info based from eeprom
  4446. */
  4447. static int iwl_init_geos(struct iwl_priv *priv)
  4448. {
  4449. struct iwl_channel_info *ch;
  4450. struct ieee80211_hw_mode *modes;
  4451. struct ieee80211_channel *channels;
  4452. struct ieee80211_channel *geo_ch;
  4453. struct ieee80211_rate *rates;
  4454. int i = 0;
  4455. enum {
  4456. A = 0,
  4457. B = 1,
  4458. G = 2,
  4459. };
  4460. int mode_count = 3;
  4461. if (priv->modes) {
  4462. IWL_DEBUG_INFO("Geography modes already initialized.\n");
  4463. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  4464. return 0;
  4465. }
  4466. modes = kzalloc(sizeof(struct ieee80211_hw_mode) * mode_count,
  4467. GFP_KERNEL);
  4468. if (!modes)
  4469. return -ENOMEM;
  4470. channels = kzalloc(sizeof(struct ieee80211_channel) *
  4471. priv->channel_count, GFP_KERNEL);
  4472. if (!channels) {
  4473. kfree(modes);
  4474. return -ENOMEM;
  4475. }
  4476. rates = kzalloc((sizeof(struct ieee80211_rate) * (IWL_MAX_RATES + 1)),
  4477. GFP_KERNEL);
  4478. if (!rates) {
  4479. kfree(modes);
  4480. kfree(channels);
  4481. return -ENOMEM;
  4482. }
  4483. /* 0 = 802.11a
  4484. * 1 = 802.11b
  4485. * 2 = 802.11g
  4486. */
  4487. /* 5.2GHz channels start after the 2.4GHz channels */
  4488. modes[A].mode = MODE_IEEE80211A;
  4489. modes[A].channels = &channels[ARRAY_SIZE(iwl_eeprom_band_1)];
  4490. modes[A].rates = &rates[4];
  4491. modes[A].num_rates = 8; /* just OFDM */
  4492. modes[A].num_channels = 0;
  4493. modes[B].mode = MODE_IEEE80211B;
  4494. modes[B].channels = channels;
  4495. modes[B].rates = rates;
  4496. modes[B].num_rates = 4; /* just CCK */
  4497. modes[B].num_channels = 0;
  4498. modes[G].mode = MODE_IEEE80211G;
  4499. modes[G].channels = channels;
  4500. modes[G].rates = rates;
  4501. modes[G].num_rates = 12; /* OFDM & CCK */
  4502. modes[G].num_channels = 0;
  4503. priv->ieee_channels = channels;
  4504. priv->ieee_rates = rates;
  4505. iwl_init_hw_rates(priv, rates);
  4506. for (i = 0, geo_ch = channels; i < priv->channel_count; i++) {
  4507. ch = &priv->channel_info[i];
  4508. if (!is_channel_valid(ch)) {
  4509. IWL_DEBUG_INFO("Channel %d [%sGHz] is restricted -- "
  4510. "skipping.\n",
  4511. ch->channel, is_channel_a_band(ch) ?
  4512. "5.2" : "2.4");
  4513. continue;
  4514. }
  4515. if (is_channel_a_band(ch))
  4516. geo_ch = &modes[A].channels[modes[A].num_channels++];
  4517. else {
  4518. geo_ch = &modes[B].channels[modes[B].num_channels++];
  4519. modes[G].num_channels++;
  4520. }
  4521. geo_ch->freq = ieee80211chan2mhz(ch->channel);
  4522. geo_ch->chan = ch->channel;
  4523. geo_ch->power_level = ch->max_power_avg;
  4524. geo_ch->antenna_max = 0xff;
  4525. if (is_channel_valid(ch)) {
  4526. geo_ch->flag = IEEE80211_CHAN_W_SCAN;
  4527. if (ch->flags & EEPROM_CHANNEL_IBSS)
  4528. geo_ch->flag |= IEEE80211_CHAN_W_IBSS;
  4529. if (ch->flags & EEPROM_CHANNEL_ACTIVE)
  4530. geo_ch->flag |= IEEE80211_CHAN_W_ACTIVE_SCAN;
  4531. if (ch->flags & EEPROM_CHANNEL_RADAR)
  4532. geo_ch->flag |= IEEE80211_CHAN_W_RADAR_DETECT;
  4533. if (ch->max_power_avg > priv->max_channel_txpower_limit)
  4534. priv->max_channel_txpower_limit =
  4535. ch->max_power_avg;
  4536. }
  4537. geo_ch->val = geo_ch->flag;
  4538. }
  4539. if ((modes[A].num_channels == 0) && priv->is_abg) {
  4540. printk(KERN_INFO DRV_NAME
  4541. ": Incorrectly detected BG card as ABG. Please send "
  4542. "your PCI ID 0x%04X:0x%04X to maintainer.\n",
  4543. priv->pci_dev->device, priv->pci_dev->subsystem_device);
  4544. priv->is_abg = 0;
  4545. }
  4546. printk(KERN_INFO DRV_NAME
  4547. ": Tunable channels: %d 802.11bg, %d 802.11a channels\n",
  4548. modes[G].num_channels, modes[A].num_channels);
  4549. /*
  4550. * NOTE: We register these in preference of order -- the
  4551. * stack doesn't currently (as of 7.0.6 / Apr 24 '07) pick
  4552. * a phymode based on rates or AP capabilities but seems to
  4553. * configure it purely on if the channel being configured
  4554. * is supported by a mode -- and the first match is taken
  4555. */
  4556. if (modes[G].num_channels)
  4557. ieee80211_register_hwmode(priv->hw, &modes[G]);
  4558. if (modes[B].num_channels)
  4559. ieee80211_register_hwmode(priv->hw, &modes[B]);
  4560. if (modes[A].num_channels)
  4561. ieee80211_register_hwmode(priv->hw, &modes[A]);
  4562. priv->modes = modes;
  4563. set_bit(STATUS_GEO_CONFIGURED, &priv->status);
  4564. return 0;
  4565. }
  4566. /******************************************************************************
  4567. *
  4568. * uCode download functions
  4569. *
  4570. ******************************************************************************/
  4571. static void iwl_dealloc_ucode_pci(struct iwl_priv *priv)
  4572. {
  4573. if (priv->ucode_code.v_addr != NULL) {
  4574. pci_free_consistent(priv->pci_dev,
  4575. priv->ucode_code.len,
  4576. priv->ucode_code.v_addr,
  4577. priv->ucode_code.p_addr);
  4578. priv->ucode_code.v_addr = NULL;
  4579. }
  4580. if (priv->ucode_data.v_addr != NULL) {
  4581. pci_free_consistent(priv->pci_dev,
  4582. priv->ucode_data.len,
  4583. priv->ucode_data.v_addr,
  4584. priv->ucode_data.p_addr);
  4585. priv->ucode_data.v_addr = NULL;
  4586. }
  4587. if (priv->ucode_data_backup.v_addr != NULL) {
  4588. pci_free_consistent(priv->pci_dev,
  4589. priv->ucode_data_backup.len,
  4590. priv->ucode_data_backup.v_addr,
  4591. priv->ucode_data_backup.p_addr);
  4592. priv->ucode_data_backup.v_addr = NULL;
  4593. }
  4594. if (priv->ucode_init.v_addr != NULL) {
  4595. pci_free_consistent(priv->pci_dev,
  4596. priv->ucode_init.len,
  4597. priv->ucode_init.v_addr,
  4598. priv->ucode_init.p_addr);
  4599. priv->ucode_init.v_addr = NULL;
  4600. }
  4601. if (priv->ucode_init_data.v_addr != NULL) {
  4602. pci_free_consistent(priv->pci_dev,
  4603. priv->ucode_init_data.len,
  4604. priv->ucode_init_data.v_addr,
  4605. priv->ucode_init_data.p_addr);
  4606. priv->ucode_init_data.v_addr = NULL;
  4607. }
  4608. if (priv->ucode_boot.v_addr != NULL) {
  4609. pci_free_consistent(priv->pci_dev,
  4610. priv->ucode_boot.len,
  4611. priv->ucode_boot.v_addr,
  4612. priv->ucode_boot.p_addr);
  4613. priv->ucode_boot.v_addr = NULL;
  4614. }
  4615. }
  4616. /**
  4617. * iwl_verify_inst_full - verify runtime uCode image in card vs. host,
  4618. * looking at all data.
  4619. */
  4620. static int iwl_verify_inst_full(struct iwl_priv *priv, __le32 * image, u32 len)
  4621. {
  4622. u32 val;
  4623. u32 save_len = len;
  4624. int rc = 0;
  4625. u32 errcnt;
  4626. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  4627. rc = iwl_grab_restricted_access(priv);
  4628. if (rc)
  4629. return rc;
  4630. iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR, RTC_INST_LOWER_BOUND);
  4631. errcnt = 0;
  4632. for (; len > 0; len -= sizeof(u32), image++) {
  4633. /* read data comes through single port, auto-incr addr */
  4634. /* NOTE: Use the debugless read so we don't flood kernel log
  4635. * if IWL_DL_IO is set */
  4636. val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
  4637. if (val != le32_to_cpu(*image)) {
  4638. IWL_ERROR("uCode INST section is invalid at "
  4639. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  4640. save_len - len, val, le32_to_cpu(*image));
  4641. rc = -EIO;
  4642. errcnt++;
  4643. if (errcnt >= 20)
  4644. break;
  4645. }
  4646. }
  4647. iwl_release_restricted_access(priv);
  4648. if (!errcnt)
  4649. IWL_DEBUG_INFO("ucode image in INSTRUCTION memory is good\n");
  4650. return rc;
  4651. }
  4652. /**
  4653. * iwl_verify_inst_sparse - verify runtime uCode image in card vs. host,
  4654. * using sample data 100 bytes apart. If these sample points are good,
  4655. * it's a pretty good bet that everything between them is good, too.
  4656. */
  4657. static int iwl_verify_inst_sparse(struct iwl_priv *priv, __le32 *image, u32 len)
  4658. {
  4659. u32 val;
  4660. int rc = 0;
  4661. u32 errcnt = 0;
  4662. u32 i;
  4663. IWL_DEBUG_INFO("ucode inst image size is %u\n", len);
  4664. rc = iwl_grab_restricted_access(priv);
  4665. if (rc)
  4666. return rc;
  4667. for (i = 0; i < len; i += 100, image += 100/sizeof(u32)) {
  4668. /* read data comes through single port, auto-incr addr */
  4669. /* NOTE: Use the debugless read so we don't flood kernel log
  4670. * if IWL_DL_IO is set */
  4671. iwl_write_restricted(priv, HBUS_TARG_MEM_RADDR,
  4672. i + RTC_INST_LOWER_BOUND);
  4673. val = _iwl_read_restricted(priv, HBUS_TARG_MEM_RDAT);
  4674. if (val != le32_to_cpu(*image)) {
  4675. #if 0 /* Enable this if you want to see details */
  4676. IWL_ERROR("uCode INST section is invalid at "
  4677. "offset 0x%x, is 0x%x, s/b 0x%x\n",
  4678. i, val, *image);
  4679. #endif
  4680. rc = -EIO;
  4681. errcnt++;
  4682. if (errcnt >= 3)
  4683. break;
  4684. }
  4685. }
  4686. iwl_release_restricted_access(priv);
  4687. return rc;
  4688. }
  4689. /**
  4690. * iwl_verify_ucode - determine which instruction image is in SRAM,
  4691. * and verify its contents
  4692. */
  4693. static int iwl_verify_ucode(struct iwl_priv *priv)
  4694. {
  4695. __le32 *image;
  4696. u32 len;
  4697. int rc = 0;
  4698. /* Try bootstrap */
  4699. image = (__le32 *)priv->ucode_boot.v_addr;
  4700. len = priv->ucode_boot.len;
  4701. rc = iwl_verify_inst_sparse(priv, image, len);
  4702. if (rc == 0) {
  4703. IWL_DEBUG_INFO("Bootstrap uCode is good in inst SRAM\n");
  4704. return 0;
  4705. }
  4706. /* Try initialize */
  4707. image = (__le32 *)priv->ucode_init.v_addr;
  4708. len = priv->ucode_init.len;
  4709. rc = iwl_verify_inst_sparse(priv, image, len);
  4710. if (rc == 0) {
  4711. IWL_DEBUG_INFO("Initialize uCode is good in inst SRAM\n");
  4712. return 0;
  4713. }
  4714. /* Try runtime/protocol */
  4715. image = (__le32 *)priv->ucode_code.v_addr;
  4716. len = priv->ucode_code.len;
  4717. rc = iwl_verify_inst_sparse(priv, image, len);
  4718. if (rc == 0) {
  4719. IWL_DEBUG_INFO("Runtime uCode is good in inst SRAM\n");
  4720. return 0;
  4721. }
  4722. IWL_ERROR("NO VALID UCODE IMAGE IN INSTRUCTION SRAM!!\n");
  4723. /* Show first several data entries in instruction SRAM.
  4724. * Selection of bootstrap image is arbitrary. */
  4725. image = (__le32 *)priv->ucode_boot.v_addr;
  4726. len = priv->ucode_boot.len;
  4727. rc = iwl_verify_inst_full(priv, image, len);
  4728. return rc;
  4729. }
  4730. /* check contents of special bootstrap uCode SRAM */
  4731. static int iwl_verify_bsm(struct iwl_priv *priv)
  4732. {
  4733. __le32 *image = priv->ucode_boot.v_addr;
  4734. u32 len = priv->ucode_boot.len;
  4735. u32 reg;
  4736. u32 val;
  4737. IWL_DEBUG_INFO("Begin verify bsm\n");
  4738. /* verify BSM SRAM contents */
  4739. val = iwl_read_restricted_reg(priv, BSM_WR_DWCOUNT_REG);
  4740. for (reg = BSM_SRAM_LOWER_BOUND;
  4741. reg < BSM_SRAM_LOWER_BOUND + len;
  4742. reg += sizeof(u32), image ++) {
  4743. val = iwl_read_restricted_reg(priv, reg);
  4744. if (val != le32_to_cpu(*image)) {
  4745. IWL_ERROR("BSM uCode verification failed at "
  4746. "addr 0x%08X+%u (of %u), is 0x%x, s/b 0x%x\n",
  4747. BSM_SRAM_LOWER_BOUND,
  4748. reg - BSM_SRAM_LOWER_BOUND, len,
  4749. val, le32_to_cpu(*image));
  4750. return -EIO;
  4751. }
  4752. }
  4753. IWL_DEBUG_INFO("BSM bootstrap uCode image OK\n");
  4754. return 0;
  4755. }
  4756. /**
  4757. * iwl_load_bsm - Load bootstrap instructions
  4758. *
  4759. * BSM operation:
  4760. *
  4761. * The Bootstrap State Machine (BSM) stores a short bootstrap uCode program
  4762. * in special SRAM that does not power down during RFKILL. When powering back
  4763. * up after power-saving sleeps (or during initial uCode load), the BSM loads
  4764. * the bootstrap program into the on-board processor, and starts it.
  4765. *
  4766. * The bootstrap program loads (via DMA) instructions and data for a new
  4767. * program from host DRAM locations indicated by the host driver in the
  4768. * BSM_DRAM_* registers. Once the new program is loaded, it starts
  4769. * automatically.
  4770. *
  4771. * When initializing the NIC, the host driver points the BSM to the
  4772. * "initialize" uCode image. This uCode sets up some internal data, then
  4773. * notifies host via "initialize alive" that it is complete.
  4774. *
  4775. * The host then replaces the BSM_DRAM_* pointer values to point to the
  4776. * normal runtime uCode instructions and a backup uCode data cache buffer
  4777. * (filled initially with starting data values for the on-board processor),
  4778. * then triggers the "initialize" uCode to load and launch the runtime uCode,
  4779. * which begins normal operation.
  4780. *
  4781. * When doing a power-save shutdown, runtime uCode saves data SRAM into
  4782. * the backup data cache in DRAM before SRAM is powered down.
  4783. *
  4784. * When powering back up, the BSM loads the bootstrap program. This reloads
  4785. * the runtime uCode instructions and the backup data cache into SRAM,
  4786. * and re-launches the runtime uCode from where it left off.
  4787. */
  4788. static int iwl_load_bsm(struct iwl_priv *priv)
  4789. {
  4790. __le32 *image = priv->ucode_boot.v_addr;
  4791. u32 len = priv->ucode_boot.len;
  4792. dma_addr_t pinst;
  4793. dma_addr_t pdata;
  4794. u32 inst_len;
  4795. u32 data_len;
  4796. int rc;
  4797. int i;
  4798. u32 done;
  4799. u32 reg_offset;
  4800. IWL_DEBUG_INFO("Begin load bsm\n");
  4801. /* make sure bootstrap program is no larger than BSM's SRAM size */
  4802. if (len > IWL_MAX_BSM_SIZE)
  4803. return -EINVAL;
  4804. /* Tell bootstrap uCode where to find the "Initialize" uCode
  4805. * in host DRAM ... bits 31:0 for 3945, bits 35:4 for 4965.
  4806. * NOTE: iwl_initialize_alive_start() will replace these values,
  4807. * after the "initialize" uCode has run, to point to
  4808. * runtime/protocol instructions and backup data cache. */
  4809. pinst = priv->ucode_init.p_addr;
  4810. pdata = priv->ucode_init_data.p_addr;
  4811. inst_len = priv->ucode_init.len;
  4812. data_len = priv->ucode_init_data.len;
  4813. rc = iwl_grab_restricted_access(priv);
  4814. if (rc)
  4815. return rc;
  4816. iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
  4817. iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  4818. iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG, inst_len);
  4819. iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG, data_len);
  4820. /* Fill BSM memory with bootstrap instructions */
  4821. for (reg_offset = BSM_SRAM_LOWER_BOUND;
  4822. reg_offset < BSM_SRAM_LOWER_BOUND + len;
  4823. reg_offset += sizeof(u32), image++)
  4824. _iwl_write_restricted_reg(priv, reg_offset,
  4825. le32_to_cpu(*image));
  4826. rc = iwl_verify_bsm(priv);
  4827. if (rc) {
  4828. iwl_release_restricted_access(priv);
  4829. return rc;
  4830. }
  4831. /* Tell BSM to copy from BSM SRAM into instruction SRAM, when asked */
  4832. iwl_write_restricted_reg(priv, BSM_WR_MEM_SRC_REG, 0x0);
  4833. iwl_write_restricted_reg(priv, BSM_WR_MEM_DST_REG,
  4834. RTC_INST_LOWER_BOUND);
  4835. iwl_write_restricted_reg(priv, BSM_WR_DWCOUNT_REG, len / sizeof(u32));
  4836. /* Load bootstrap code into instruction SRAM now,
  4837. * to prepare to load "initialize" uCode */
  4838. iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
  4839. BSM_WR_CTRL_REG_BIT_START);
  4840. /* Wait for load of bootstrap uCode to finish */
  4841. for (i = 0; i < 100; i++) {
  4842. done = iwl_read_restricted_reg(priv, BSM_WR_CTRL_REG);
  4843. if (!(done & BSM_WR_CTRL_REG_BIT_START))
  4844. break;
  4845. udelay(10);
  4846. }
  4847. if (i < 100)
  4848. IWL_DEBUG_INFO("BSM write complete, poll %d iterations\n", i);
  4849. else {
  4850. IWL_ERROR("BSM write did not complete!\n");
  4851. return -EIO;
  4852. }
  4853. /* Enable future boot loads whenever power management unit triggers it
  4854. * (e.g. when powering back up after power-save shutdown) */
  4855. iwl_write_restricted_reg(priv, BSM_WR_CTRL_REG,
  4856. BSM_WR_CTRL_REG_BIT_START_EN);
  4857. iwl_release_restricted_access(priv);
  4858. return 0;
  4859. }
  4860. static void iwl_nic_start(struct iwl_priv *priv)
  4861. {
  4862. /* Remove all resets to allow NIC to operate */
  4863. iwl_write32(priv, CSR_RESET, 0);
  4864. }
  4865. /**
  4866. * iwl_read_ucode - Read uCode images from disk file.
  4867. *
  4868. * Copy into buffers for card to fetch via bus-mastering
  4869. */
  4870. static int iwl_read_ucode(struct iwl_priv *priv)
  4871. {
  4872. struct iwl_ucode *ucode;
  4873. int rc = 0;
  4874. const struct firmware *ucode_raw;
  4875. /* firmware file name contains uCode/driver compatibility version */
  4876. const char *name = "iwlwifi-3945" IWL3945_UCODE_API ".ucode";
  4877. u8 *src;
  4878. size_t len;
  4879. u32 ver, inst_size, data_size, init_size, init_data_size, boot_size;
  4880. /* Ask kernel firmware_class module to get the boot firmware off disk.
  4881. * request_firmware() is synchronous, file is in memory on return. */
  4882. rc = request_firmware(&ucode_raw, name, &priv->pci_dev->dev);
  4883. if (rc < 0) {
  4884. IWL_ERROR("%s firmware file req failed: Reason %d\n", name, rc);
  4885. goto error;
  4886. }
  4887. IWL_DEBUG_INFO("Got firmware '%s' file (%zd bytes) from disk\n",
  4888. name, ucode_raw->size);
  4889. /* Make sure that we got at least our header! */
  4890. if (ucode_raw->size < sizeof(*ucode)) {
  4891. IWL_ERROR("File size way too small!\n");
  4892. rc = -EINVAL;
  4893. goto err_release;
  4894. }
  4895. /* Data from ucode file: header followed by uCode images */
  4896. ucode = (void *)ucode_raw->data;
  4897. ver = le32_to_cpu(ucode->ver);
  4898. inst_size = le32_to_cpu(ucode->inst_size);
  4899. data_size = le32_to_cpu(ucode->data_size);
  4900. init_size = le32_to_cpu(ucode->init_size);
  4901. init_data_size = le32_to_cpu(ucode->init_data_size);
  4902. boot_size = le32_to_cpu(ucode->boot_size);
  4903. IWL_DEBUG_INFO("f/w package hdr ucode version = 0x%x\n", ver);
  4904. IWL_DEBUG_INFO("f/w package hdr runtime inst size = %u\n", inst_size);
  4905. IWL_DEBUG_INFO("f/w package hdr runtime data size = %u\n", data_size);
  4906. IWL_DEBUG_INFO("f/w package hdr init inst size = %u\n", init_size);
  4907. IWL_DEBUG_INFO("f/w package hdr init data size = %u\n", init_data_size);
  4908. IWL_DEBUG_INFO("f/w package hdr boot inst size = %u\n", boot_size);
  4909. /* Verify size of file vs. image size info in file's header */
  4910. if (ucode_raw->size < sizeof(*ucode) +
  4911. inst_size + data_size + init_size +
  4912. init_data_size + boot_size) {
  4913. IWL_DEBUG_INFO("uCode file size %d too small\n",
  4914. (int)ucode_raw->size);
  4915. rc = -EINVAL;
  4916. goto err_release;
  4917. }
  4918. /* Verify that uCode images will fit in card's SRAM */
  4919. if (inst_size > IWL_MAX_INST_SIZE) {
  4920. IWL_DEBUG_INFO("uCode instr len %d too large to fit in card\n",
  4921. (int)inst_size);
  4922. rc = -EINVAL;
  4923. goto err_release;
  4924. }
  4925. if (data_size > IWL_MAX_DATA_SIZE) {
  4926. IWL_DEBUG_INFO("uCode data len %d too large to fit in card\n",
  4927. (int)data_size);
  4928. rc = -EINVAL;
  4929. goto err_release;
  4930. }
  4931. if (init_size > IWL_MAX_INST_SIZE) {
  4932. IWL_DEBUG_INFO
  4933. ("uCode init instr len %d too large to fit in card\n",
  4934. (int)init_size);
  4935. rc = -EINVAL;
  4936. goto err_release;
  4937. }
  4938. if (init_data_size > IWL_MAX_DATA_SIZE) {
  4939. IWL_DEBUG_INFO
  4940. ("uCode init data len %d too large to fit in card\n",
  4941. (int)init_data_size);
  4942. rc = -EINVAL;
  4943. goto err_release;
  4944. }
  4945. if (boot_size > IWL_MAX_BSM_SIZE) {
  4946. IWL_DEBUG_INFO
  4947. ("uCode boot instr len %d too large to fit in bsm\n",
  4948. (int)boot_size);
  4949. rc = -EINVAL;
  4950. goto err_release;
  4951. }
  4952. /* Allocate ucode buffers for card's bus-master loading ... */
  4953. /* Runtime instructions and 2 copies of data:
  4954. * 1) unmodified from disk
  4955. * 2) backup cache for save/restore during power-downs */
  4956. priv->ucode_code.len = inst_size;
  4957. priv->ucode_code.v_addr =
  4958. pci_alloc_consistent(priv->pci_dev,
  4959. priv->ucode_code.len,
  4960. &(priv->ucode_code.p_addr));
  4961. priv->ucode_data.len = data_size;
  4962. priv->ucode_data.v_addr =
  4963. pci_alloc_consistent(priv->pci_dev,
  4964. priv->ucode_data.len,
  4965. &(priv->ucode_data.p_addr));
  4966. priv->ucode_data_backup.len = data_size;
  4967. priv->ucode_data_backup.v_addr =
  4968. pci_alloc_consistent(priv->pci_dev,
  4969. priv->ucode_data_backup.len,
  4970. &(priv->ucode_data_backup.p_addr));
  4971. /* Initialization instructions and data */
  4972. priv->ucode_init.len = init_size;
  4973. priv->ucode_init.v_addr =
  4974. pci_alloc_consistent(priv->pci_dev,
  4975. priv->ucode_init.len,
  4976. &(priv->ucode_init.p_addr));
  4977. priv->ucode_init_data.len = init_data_size;
  4978. priv->ucode_init_data.v_addr =
  4979. pci_alloc_consistent(priv->pci_dev,
  4980. priv->ucode_init_data.len,
  4981. &(priv->ucode_init_data.p_addr));
  4982. /* Bootstrap (instructions only, no data) */
  4983. priv->ucode_boot.len = boot_size;
  4984. priv->ucode_boot.v_addr =
  4985. pci_alloc_consistent(priv->pci_dev,
  4986. priv->ucode_boot.len,
  4987. &(priv->ucode_boot.p_addr));
  4988. if (!priv->ucode_code.v_addr || !priv->ucode_data.v_addr ||
  4989. !priv->ucode_init.v_addr || !priv->ucode_init_data.v_addr ||
  4990. !priv->ucode_boot.v_addr || !priv->ucode_data_backup.v_addr)
  4991. goto err_pci_alloc;
  4992. /* Copy images into buffers for card's bus-master reads ... */
  4993. /* Runtime instructions (first block of data in file) */
  4994. src = &ucode->data[0];
  4995. len = priv->ucode_code.len;
  4996. IWL_DEBUG_INFO("Copying (but not loading) uCode instr len %d\n",
  4997. (int)len);
  4998. memcpy(priv->ucode_code.v_addr, src, len);
  4999. IWL_DEBUG_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  5000. priv->ucode_code.v_addr, (u32)priv->ucode_code.p_addr);
  5001. /* Runtime data (2nd block)
  5002. * NOTE: Copy into backup buffer will be done in iwl_up() */
  5003. src = &ucode->data[inst_size];
  5004. len = priv->ucode_data.len;
  5005. IWL_DEBUG_INFO("Copying (but not loading) uCode data len %d\n",
  5006. (int)len);
  5007. memcpy(priv->ucode_data.v_addr, src, len);
  5008. memcpy(priv->ucode_data_backup.v_addr, src, len);
  5009. /* Initialization instructions (3rd block) */
  5010. if (init_size) {
  5011. src = &ucode->data[inst_size + data_size];
  5012. len = priv->ucode_init.len;
  5013. IWL_DEBUG_INFO("Copying (but not loading) init instr len %d\n",
  5014. (int)len);
  5015. memcpy(priv->ucode_init.v_addr, src, len);
  5016. }
  5017. /* Initialization data (4th block) */
  5018. if (init_data_size) {
  5019. src = &ucode->data[inst_size + data_size + init_size];
  5020. len = priv->ucode_init_data.len;
  5021. IWL_DEBUG_INFO("Copying (but not loading) init data len %d\n",
  5022. (int)len);
  5023. memcpy(priv->ucode_init_data.v_addr, src, len);
  5024. }
  5025. /* Bootstrap instructions (5th block) */
  5026. src = &ucode->data[inst_size + data_size + init_size + init_data_size];
  5027. len = priv->ucode_boot.len;
  5028. IWL_DEBUG_INFO("Copying (but not loading) boot instr len %d\n",
  5029. (int)len);
  5030. memcpy(priv->ucode_boot.v_addr, src, len);
  5031. /* We have our copies now, allow OS release its copies */
  5032. release_firmware(ucode_raw);
  5033. return 0;
  5034. err_pci_alloc:
  5035. IWL_ERROR("failed to allocate pci memory\n");
  5036. rc = -ENOMEM;
  5037. iwl_dealloc_ucode_pci(priv);
  5038. err_release:
  5039. release_firmware(ucode_raw);
  5040. error:
  5041. return rc;
  5042. }
  5043. /**
  5044. * iwl_set_ucode_ptrs - Set uCode address location
  5045. *
  5046. * Tell initialization uCode where to find runtime uCode.
  5047. *
  5048. * BSM registers initially contain pointers to initialization uCode.
  5049. * We need to replace them to load runtime uCode inst and data,
  5050. * and to save runtime data when powering down.
  5051. */
  5052. static int iwl_set_ucode_ptrs(struct iwl_priv *priv)
  5053. {
  5054. dma_addr_t pinst;
  5055. dma_addr_t pdata;
  5056. int rc = 0;
  5057. unsigned long flags;
  5058. /* bits 31:0 for 3945 */
  5059. pinst = priv->ucode_code.p_addr;
  5060. pdata = priv->ucode_data_backup.p_addr;
  5061. spin_lock_irqsave(&priv->lock, flags);
  5062. rc = iwl_grab_restricted_access(priv);
  5063. if (rc) {
  5064. spin_unlock_irqrestore(&priv->lock, flags);
  5065. return rc;
  5066. }
  5067. /* Tell bootstrap uCode where to find image to load */
  5068. iwl_write_restricted_reg(priv, BSM_DRAM_INST_PTR_REG, pinst);
  5069. iwl_write_restricted_reg(priv, BSM_DRAM_DATA_PTR_REG, pdata);
  5070. iwl_write_restricted_reg(priv, BSM_DRAM_DATA_BYTECOUNT_REG,
  5071. priv->ucode_data.len);
  5072. /* Inst bytecount must be last to set up, bit 31 signals uCode
  5073. * that all new ptr/size info is in place */
  5074. iwl_write_restricted_reg(priv, BSM_DRAM_INST_BYTECOUNT_REG,
  5075. priv->ucode_code.len | BSM_DRAM_INST_LOAD);
  5076. iwl_release_restricted_access(priv);
  5077. spin_unlock_irqrestore(&priv->lock, flags);
  5078. IWL_DEBUG_INFO("Runtime uCode pointers are set.\n");
  5079. return rc;
  5080. }
  5081. /**
  5082. * iwl_init_alive_start - Called after REPLY_ALIVE notification received
  5083. *
  5084. * Called after REPLY_ALIVE notification received from "initialize" uCode.
  5085. *
  5086. * The 4965 "initialize" ALIVE reply contains calibration data for:
  5087. * Voltage, temperature, and MIMO tx gain correction, now stored in priv
  5088. * (3945 does not contain this data).
  5089. *
  5090. * Tell "initialize" uCode to go ahead and load the runtime uCode.
  5091. */
  5092. static void iwl_init_alive_start(struct iwl_priv *priv)
  5093. {
  5094. /* Check alive response for "valid" sign from uCode */
  5095. if (priv->card_alive_init.is_valid != UCODE_VALID_OK) {
  5096. /* We had an error bringing up the hardware, so take it
  5097. * all the way back down so we can try again */
  5098. IWL_DEBUG_INFO("Initialize Alive failed.\n");
  5099. goto restart;
  5100. }
  5101. /* Bootstrap uCode has loaded initialize uCode ... verify inst image.
  5102. * This is a paranoid check, because we would not have gotten the
  5103. * "initialize" alive if code weren't properly loaded. */
  5104. if (iwl_verify_ucode(priv)) {
  5105. /* Runtime instruction load was bad;
  5106. * take it all the way back down so we can try again */
  5107. IWL_DEBUG_INFO("Bad \"initialize\" uCode load.\n");
  5108. goto restart;
  5109. }
  5110. /* Send pointers to protocol/runtime uCode image ... init code will
  5111. * load and launch runtime uCode, which will send us another "Alive"
  5112. * notification. */
  5113. IWL_DEBUG_INFO("Initialization Alive received.\n");
  5114. if (iwl_set_ucode_ptrs(priv)) {
  5115. /* Runtime instruction load won't happen;
  5116. * take it all the way back down so we can try again */
  5117. IWL_DEBUG_INFO("Couldn't set up uCode pointers.\n");
  5118. goto restart;
  5119. }
  5120. return;
  5121. restart:
  5122. queue_work(priv->workqueue, &priv->restart);
  5123. }
  5124. /**
  5125. * iwl_alive_start - called after REPLY_ALIVE notification received
  5126. * from protocol/runtime uCode (initialization uCode's
  5127. * Alive gets handled by iwl_init_alive_start()).
  5128. */
  5129. static void iwl_alive_start(struct iwl_priv *priv)
  5130. {
  5131. int rc = 0;
  5132. int thermal_spin = 0;
  5133. u32 rfkill;
  5134. IWL_DEBUG_INFO("Runtime Alive received.\n");
  5135. if (priv->card_alive.is_valid != UCODE_VALID_OK) {
  5136. /* We had an error bringing up the hardware, so take it
  5137. * all the way back down so we can try again */
  5138. IWL_DEBUG_INFO("Alive failed.\n");
  5139. goto restart;
  5140. }
  5141. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  5142. * This is a paranoid check, because we would not have gotten the
  5143. * "runtime" alive if code weren't properly loaded. */
  5144. if (iwl_verify_ucode(priv)) {
  5145. /* Runtime instruction load was bad;
  5146. * take it all the way back down so we can try again */
  5147. IWL_DEBUG_INFO("Bad runtime uCode load.\n");
  5148. goto restart;
  5149. }
  5150. iwl_clear_stations_table(priv);
  5151. rc = iwl_grab_restricted_access(priv);
  5152. if (rc) {
  5153. IWL_WARNING("Can not read rfkill status from adapter\n");
  5154. return;
  5155. }
  5156. rfkill = iwl_read_restricted_reg(priv, APMG_RFKILL_REG);
  5157. IWL_DEBUG_INFO("RFKILL status: 0x%x\n", rfkill);
  5158. iwl_release_restricted_access(priv);
  5159. if (rfkill & 0x1) {
  5160. clear_bit(STATUS_RF_KILL_HW, &priv->status);
  5161. /* if rfkill is not on, then wait for thermal
  5162. * sensor in adapter to kick in */
  5163. while (iwl_hw_get_temperature(priv) == 0) {
  5164. thermal_spin++;
  5165. udelay(10);
  5166. }
  5167. if (thermal_spin)
  5168. IWL_DEBUG_INFO("Thermal calibration took %dus\n",
  5169. thermal_spin * 10);
  5170. } else
  5171. set_bit(STATUS_RF_KILL_HW, &priv->status);
  5172. /* After the ALIVE response, we can process host commands */
  5173. set_bit(STATUS_ALIVE, &priv->status);
  5174. /* Clear out the uCode error bit if it is set */
  5175. clear_bit(STATUS_FW_ERROR, &priv->status);
  5176. rc = iwl_init_channel_map(priv);
  5177. if (rc) {
  5178. IWL_ERROR("initializing regulatory failed: %d\n", rc);
  5179. return;
  5180. }
  5181. iwl_init_geos(priv);
  5182. if (iwl_is_rfkill(priv))
  5183. return;
  5184. if (!priv->mac80211_registered) {
  5185. /* Unlock so any user space entry points can call back into
  5186. * the driver without a deadlock... */
  5187. mutex_unlock(&priv->mutex);
  5188. iwl_rate_control_register(priv->hw);
  5189. rc = ieee80211_register_hw(priv->hw);
  5190. priv->hw->conf.beacon_int = 100;
  5191. mutex_lock(&priv->mutex);
  5192. if (rc) {
  5193. iwl_rate_control_unregister(priv->hw);
  5194. IWL_ERROR("Failed to register network "
  5195. "device (error %d)\n", rc);
  5196. return;
  5197. }
  5198. priv->mac80211_registered = 1;
  5199. iwl_reset_channel_flag(priv);
  5200. } else
  5201. ieee80211_start_queues(priv->hw);
  5202. priv->active_rate = priv->rates_mask;
  5203. priv->active_rate_basic = priv->rates_mask & IWL_BASIC_RATES_MASK;
  5204. iwl_send_power_mode(priv, IWL_POWER_LEVEL(priv->power_mode));
  5205. if (iwl_is_associated(priv)) {
  5206. struct iwl_rxon_cmd *active_rxon =
  5207. (struct iwl_rxon_cmd *)(&priv->active_rxon);
  5208. memcpy(&priv->staging_rxon, &priv->active_rxon,
  5209. sizeof(priv->staging_rxon));
  5210. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5211. } else {
  5212. /* Initialize our rx_config data */
  5213. iwl_connection_init_rx_config(priv);
  5214. memcpy(priv->staging_rxon.node_addr, priv->mac_addr, ETH_ALEN);
  5215. }
  5216. /* Configure BT coexistence */
  5217. iwl_send_bt_config(priv);
  5218. /* Configure the adapter for unassociated operation */
  5219. iwl_commit_rxon(priv);
  5220. /* At this point, the NIC is initialized and operational */
  5221. priv->notif_missed_beacons = 0;
  5222. set_bit(STATUS_READY, &priv->status);
  5223. iwl3945_reg_txpower_periodic(priv);
  5224. IWL_DEBUG_INFO("ALIVE processing complete.\n");
  5225. if (priv->error_recovering)
  5226. iwl_error_recovery(priv);
  5227. return;
  5228. restart:
  5229. queue_work(priv->workqueue, &priv->restart);
  5230. }
  5231. static void iwl_cancel_deferred_work(struct iwl_priv *priv);
  5232. static void __iwl_down(struct iwl_priv *priv)
  5233. {
  5234. unsigned long flags;
  5235. int exit_pending = test_bit(STATUS_EXIT_PENDING, &priv->status);
  5236. struct ieee80211_conf *conf = NULL;
  5237. IWL_DEBUG_INFO(DRV_NAME " is going down\n");
  5238. conf = ieee80211_get_hw_conf(priv->hw);
  5239. if (!exit_pending)
  5240. set_bit(STATUS_EXIT_PENDING, &priv->status);
  5241. iwl_clear_stations_table(priv);
  5242. /* Unblock any waiting calls */
  5243. wake_up_interruptible_all(&priv->wait_command_queue);
  5244. /* Wipe out the EXIT_PENDING status bit if we are not actually
  5245. * exiting the module */
  5246. if (!exit_pending)
  5247. clear_bit(STATUS_EXIT_PENDING, &priv->status);
  5248. /* stop and reset the on-board processor */
  5249. iwl_write32(priv, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  5250. /* tell the device to stop sending interrupts */
  5251. iwl_disable_interrupts(priv);
  5252. if (priv->mac80211_registered)
  5253. ieee80211_stop_queues(priv->hw);
  5254. /* If we have not previously called iwl_init() then
  5255. * clear all bits but the RF Kill and SUSPEND bits and return */
  5256. if (!iwl_is_init(priv)) {
  5257. priv->status = test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  5258. STATUS_RF_KILL_HW |
  5259. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  5260. STATUS_RF_KILL_SW |
  5261. test_bit(STATUS_IN_SUSPEND, &priv->status) <<
  5262. STATUS_IN_SUSPEND;
  5263. goto exit;
  5264. }
  5265. /* ...otherwise clear out all the status bits but the RF Kill and
  5266. * SUSPEND bits and continue taking the NIC down. */
  5267. priv->status &= test_bit(STATUS_RF_KILL_HW, &priv->status) <<
  5268. STATUS_RF_KILL_HW |
  5269. test_bit(STATUS_RF_KILL_SW, &priv->status) <<
  5270. STATUS_RF_KILL_SW |
  5271. test_bit(STATUS_IN_SUSPEND, &priv->status) <<
  5272. STATUS_IN_SUSPEND |
  5273. test_bit(STATUS_FW_ERROR, &priv->status) <<
  5274. STATUS_FW_ERROR;
  5275. spin_lock_irqsave(&priv->lock, flags);
  5276. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  5277. spin_unlock_irqrestore(&priv->lock, flags);
  5278. iwl_hw_txq_ctx_stop(priv);
  5279. iwl_hw_rxq_stop(priv);
  5280. spin_lock_irqsave(&priv->lock, flags);
  5281. if (!iwl_grab_restricted_access(priv)) {
  5282. iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
  5283. APMG_CLK_VAL_DMA_CLK_RQT);
  5284. iwl_release_restricted_access(priv);
  5285. }
  5286. spin_unlock_irqrestore(&priv->lock, flags);
  5287. udelay(5);
  5288. iwl_hw_nic_stop_master(priv);
  5289. iwl_set_bit(priv, CSR_RESET, CSR_RESET_REG_FLAG_SW_RESET);
  5290. iwl_hw_nic_reset(priv);
  5291. exit:
  5292. memset(&priv->card_alive, 0, sizeof(struct iwl_alive_resp));
  5293. if (priv->ibss_beacon)
  5294. dev_kfree_skb(priv->ibss_beacon);
  5295. priv->ibss_beacon = NULL;
  5296. /* clear out any free frames */
  5297. iwl_clear_free_frames(priv);
  5298. }
  5299. static void iwl_down(struct iwl_priv *priv)
  5300. {
  5301. mutex_lock(&priv->mutex);
  5302. __iwl_down(priv);
  5303. mutex_unlock(&priv->mutex);
  5304. iwl_cancel_deferred_work(priv);
  5305. }
  5306. #define MAX_HW_RESTARTS 5
  5307. static int __iwl_up(struct iwl_priv *priv)
  5308. {
  5309. DECLARE_MAC_BUF(mac);
  5310. int rc, i;
  5311. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  5312. IWL_WARNING("Exit pending; will not bring the NIC up\n");
  5313. return -EIO;
  5314. }
  5315. if (test_bit(STATUS_RF_KILL_SW, &priv->status)) {
  5316. IWL_WARNING("Radio disabled by SW RF kill (module "
  5317. "parameter)\n");
  5318. return 0;
  5319. }
  5320. if (!priv->ucode_data_backup.v_addr || !priv->ucode_data.v_addr) {
  5321. IWL_ERROR("ucode not available for device bringup\n");
  5322. return -EIO;
  5323. }
  5324. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  5325. rc = iwl_hw_nic_init(priv);
  5326. if (rc) {
  5327. IWL_ERROR("Unable to int nic\n");
  5328. return rc;
  5329. }
  5330. /* make sure rfkill handshake bits are cleared */
  5331. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  5332. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  5333. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  5334. /* clear (again), then enable host interrupts */
  5335. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  5336. iwl_enable_interrupts(priv);
  5337. /* really make sure rfkill handshake bits are cleared */
  5338. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  5339. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  5340. /* Copy original ucode data image from disk into backup cache.
  5341. * This will be used to initialize the on-board processor's
  5342. * data SRAM for a clean start when the runtime program first loads. */
  5343. memcpy(priv->ucode_data_backup.v_addr, priv->ucode_data.v_addr,
  5344. priv->ucode_data.len);
  5345. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  5346. iwl_clear_stations_table(priv);
  5347. /* load bootstrap state machine,
  5348. * load bootstrap program into processor's memory,
  5349. * prepare to load the "initialize" uCode */
  5350. rc = iwl_load_bsm(priv);
  5351. if (rc) {
  5352. IWL_ERROR("Unable to set up bootstrap uCode: %d\n", rc);
  5353. continue;
  5354. }
  5355. /* start card; "initialize" will load runtime ucode */
  5356. iwl_nic_start(priv);
  5357. /* MAC Address location in EEPROM same for 3945/4965 */
  5358. get_eeprom_mac(priv, priv->mac_addr);
  5359. IWL_DEBUG_INFO("MAC address: %s\n",
  5360. print_mac(mac, priv->mac_addr));
  5361. SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
  5362. IWL_DEBUG_INFO(DRV_NAME " is coming up\n");
  5363. return 0;
  5364. }
  5365. set_bit(STATUS_EXIT_PENDING, &priv->status);
  5366. __iwl_down(priv);
  5367. /* tried to restart and config the device for as long as our
  5368. * patience could withstand */
  5369. IWL_ERROR("Unable to initialize device after %d attempts.\n", i);
  5370. return -EIO;
  5371. }
  5372. /*****************************************************************************
  5373. *
  5374. * Workqueue callbacks
  5375. *
  5376. *****************************************************************************/
  5377. static void iwl_bg_init_alive_start(struct work_struct *data)
  5378. {
  5379. struct iwl_priv *priv =
  5380. container_of(data, struct iwl_priv, init_alive_start.work);
  5381. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5382. return;
  5383. mutex_lock(&priv->mutex);
  5384. iwl_init_alive_start(priv);
  5385. mutex_unlock(&priv->mutex);
  5386. }
  5387. static void iwl_bg_alive_start(struct work_struct *data)
  5388. {
  5389. struct iwl_priv *priv =
  5390. container_of(data, struct iwl_priv, alive_start.work);
  5391. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5392. return;
  5393. mutex_lock(&priv->mutex);
  5394. iwl_alive_start(priv);
  5395. mutex_unlock(&priv->mutex);
  5396. }
  5397. static void iwl_bg_rf_kill(struct work_struct *work)
  5398. {
  5399. struct iwl_priv *priv = container_of(work, struct iwl_priv, rf_kill);
  5400. wake_up_interruptible(&priv->wait_command_queue);
  5401. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5402. return;
  5403. mutex_lock(&priv->mutex);
  5404. if (!iwl_is_rfkill(priv)) {
  5405. IWL_DEBUG(IWL_DL_INFO | IWL_DL_RF_KILL,
  5406. "HW and/or SW RF Kill no longer active, restarting "
  5407. "device\n");
  5408. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  5409. queue_work(priv->workqueue, &priv->restart);
  5410. } else {
  5411. if (!test_bit(STATUS_RF_KILL_HW, &priv->status))
  5412. IWL_DEBUG_RF_KILL("Can not turn radio back on - "
  5413. "disabled by SW switch\n");
  5414. else
  5415. IWL_WARNING("Radio Frequency Kill Switch is On:\n"
  5416. "Kill switch must be turned off for "
  5417. "wireless networking to work.\n");
  5418. }
  5419. mutex_unlock(&priv->mutex);
  5420. }
  5421. #define IWL_SCAN_CHECK_WATCHDOG (7 * HZ)
  5422. static void iwl_bg_scan_check(struct work_struct *data)
  5423. {
  5424. struct iwl_priv *priv =
  5425. container_of(data, struct iwl_priv, scan_check.work);
  5426. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5427. return;
  5428. mutex_lock(&priv->mutex);
  5429. if (test_bit(STATUS_SCANNING, &priv->status) ||
  5430. test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  5431. IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN,
  5432. "Scan completion watchdog resetting adapter (%dms)\n",
  5433. jiffies_to_msecs(IWL_SCAN_CHECK_WATCHDOG));
  5434. if (!test_bit(STATUS_EXIT_PENDING, &priv->status))
  5435. iwl_send_scan_abort(priv);
  5436. }
  5437. mutex_unlock(&priv->mutex);
  5438. }
  5439. static void iwl_bg_request_scan(struct work_struct *data)
  5440. {
  5441. struct iwl_priv *priv =
  5442. container_of(data, struct iwl_priv, request_scan);
  5443. struct iwl_host_cmd cmd = {
  5444. .id = REPLY_SCAN_CMD,
  5445. .len = sizeof(struct iwl_scan_cmd),
  5446. .meta.flags = CMD_SIZE_HUGE,
  5447. };
  5448. int rc = 0;
  5449. struct iwl_scan_cmd *scan;
  5450. struct ieee80211_conf *conf = NULL;
  5451. u8 direct_mask;
  5452. int phymode;
  5453. conf = ieee80211_get_hw_conf(priv->hw);
  5454. mutex_lock(&priv->mutex);
  5455. if (!iwl_is_ready(priv)) {
  5456. IWL_WARNING("request scan called when driver not ready.\n");
  5457. goto done;
  5458. }
  5459. /* Make sure the scan wasn't cancelled before this queued work
  5460. * was given the chance to run... */
  5461. if (!test_bit(STATUS_SCANNING, &priv->status))
  5462. goto done;
  5463. /* This should never be called or scheduled if there is currently
  5464. * a scan active in the hardware. */
  5465. if (test_bit(STATUS_SCAN_HW, &priv->status)) {
  5466. IWL_DEBUG_INFO("Multiple concurrent scan requests in parallel. "
  5467. "Ignoring second request.\n");
  5468. rc = -EIO;
  5469. goto done;
  5470. }
  5471. if (test_bit(STATUS_EXIT_PENDING, &priv->status)) {
  5472. IWL_DEBUG_SCAN("Aborting scan due to device shutdown\n");
  5473. goto done;
  5474. }
  5475. if (test_bit(STATUS_SCAN_ABORTING, &priv->status)) {
  5476. IWL_DEBUG_HC("Scan request while abort pending. Queuing.\n");
  5477. goto done;
  5478. }
  5479. if (iwl_is_rfkill(priv)) {
  5480. IWL_DEBUG_HC("Aborting scan due to RF Kill activation\n");
  5481. goto done;
  5482. }
  5483. if (!test_bit(STATUS_READY, &priv->status)) {
  5484. IWL_DEBUG_HC("Scan request while uninitialized. Queuing.\n");
  5485. goto done;
  5486. }
  5487. if (!priv->scan_bands) {
  5488. IWL_DEBUG_HC("Aborting scan due to no requested bands\n");
  5489. goto done;
  5490. }
  5491. if (!priv->scan) {
  5492. priv->scan = kmalloc(sizeof(struct iwl_scan_cmd) +
  5493. IWL_MAX_SCAN_SIZE, GFP_KERNEL);
  5494. if (!priv->scan) {
  5495. rc = -ENOMEM;
  5496. goto done;
  5497. }
  5498. }
  5499. scan = priv->scan;
  5500. memset(scan, 0, sizeof(struct iwl_scan_cmd) + IWL_MAX_SCAN_SIZE);
  5501. scan->quiet_plcp_th = IWL_PLCP_QUIET_THRESH;
  5502. scan->quiet_time = IWL_ACTIVE_QUIET_TIME;
  5503. if (iwl_is_associated(priv)) {
  5504. u16 interval = 0;
  5505. u32 extra;
  5506. u32 suspend_time = 100;
  5507. u32 scan_suspend_time = 100;
  5508. unsigned long flags;
  5509. IWL_DEBUG_INFO("Scanning while associated...\n");
  5510. spin_lock_irqsave(&priv->lock, flags);
  5511. interval = priv->beacon_int;
  5512. spin_unlock_irqrestore(&priv->lock, flags);
  5513. scan->suspend_time = 0;
  5514. scan->max_out_time = cpu_to_le32(200 * 1024);
  5515. if (!interval)
  5516. interval = suspend_time;
  5517. /*
  5518. * suspend time format:
  5519. * 0-19: beacon interval in usec (time before exec.)
  5520. * 20-23: 0
  5521. * 24-31: number of beacons (suspend between channels)
  5522. */
  5523. extra = (suspend_time / interval) << 24;
  5524. scan_suspend_time = 0xFF0FFFFF &
  5525. (extra | ((suspend_time % interval) * 1024));
  5526. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  5527. IWL_DEBUG_SCAN("suspend_time 0x%X beacon interval %d\n",
  5528. scan_suspend_time, interval);
  5529. }
  5530. /* We should add the ability for user to lock to PASSIVE ONLY */
  5531. if (priv->one_direct_scan) {
  5532. IWL_DEBUG_SCAN
  5533. ("Kicking off one direct scan for '%s'\n",
  5534. iwl_escape_essid(priv->direct_ssid,
  5535. priv->direct_ssid_len));
  5536. scan->direct_scan[0].id = WLAN_EID_SSID;
  5537. scan->direct_scan[0].len = priv->direct_ssid_len;
  5538. memcpy(scan->direct_scan[0].ssid,
  5539. priv->direct_ssid, priv->direct_ssid_len);
  5540. direct_mask = 1;
  5541. } else if (!iwl_is_associated(priv) && priv->essid_len) {
  5542. scan->direct_scan[0].id = WLAN_EID_SSID;
  5543. scan->direct_scan[0].len = priv->essid_len;
  5544. memcpy(scan->direct_scan[0].ssid, priv->essid, priv->essid_len);
  5545. direct_mask = 1;
  5546. } else
  5547. direct_mask = 0;
  5548. /* We don't build a direct scan probe request; the uCode will do
  5549. * that based on the direct_mask added to each channel entry */
  5550. scan->tx_cmd.len = cpu_to_le16(
  5551. iwl_fill_probe_req(priv, (struct ieee80211_mgmt *)scan->data,
  5552. IWL_MAX_SCAN_SIZE - sizeof(scan), 0));
  5553. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  5554. scan->tx_cmd.sta_id = priv->hw_setting.bcast_sta_id;
  5555. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  5556. /* flags + rate selection */
  5557. switch (priv->scan_bands) {
  5558. case 2:
  5559. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  5560. scan->tx_cmd.rate = IWL_RATE_1M_PLCP;
  5561. scan->good_CRC_th = 0;
  5562. phymode = MODE_IEEE80211G;
  5563. break;
  5564. case 1:
  5565. scan->tx_cmd.rate = IWL_RATE_6M_PLCP;
  5566. scan->good_CRC_th = IWL_GOOD_CRC_TH;
  5567. phymode = MODE_IEEE80211A;
  5568. break;
  5569. default:
  5570. IWL_WARNING("Invalid scan band count\n");
  5571. goto done;
  5572. }
  5573. /* select Rx antennas */
  5574. scan->flags |= iwl3945_get_antenna_flags(priv);
  5575. if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR)
  5576. scan->filter_flags = RXON_FILTER_PROMISC_MSK;
  5577. if (direct_mask)
  5578. IWL_DEBUG_SCAN
  5579. ("Initiating direct scan for %s.\n",
  5580. iwl_escape_essid(priv->essid, priv->essid_len));
  5581. else
  5582. IWL_DEBUG_SCAN("Initiating indirect scan.\n");
  5583. scan->channel_count =
  5584. iwl_get_channels_for_scan(
  5585. priv, phymode, 1, /* active */
  5586. direct_mask,
  5587. (void *)&scan->data[le16_to_cpu(scan->tx_cmd.len)]);
  5588. cmd.len += le16_to_cpu(scan->tx_cmd.len) +
  5589. scan->channel_count * sizeof(struct iwl_scan_channel);
  5590. cmd.data = scan;
  5591. scan->len = cpu_to_le16(cmd.len);
  5592. set_bit(STATUS_SCAN_HW, &priv->status);
  5593. rc = iwl_send_cmd_sync(priv, &cmd);
  5594. if (rc)
  5595. goto done;
  5596. queue_delayed_work(priv->workqueue, &priv->scan_check,
  5597. IWL_SCAN_CHECK_WATCHDOG);
  5598. mutex_unlock(&priv->mutex);
  5599. return;
  5600. done:
  5601. /* inform mac80211 scan aborted */
  5602. queue_work(priv->workqueue, &priv->scan_completed);
  5603. mutex_unlock(&priv->mutex);
  5604. }
  5605. static void iwl_bg_up(struct work_struct *data)
  5606. {
  5607. struct iwl_priv *priv = container_of(data, struct iwl_priv, up);
  5608. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5609. return;
  5610. mutex_lock(&priv->mutex);
  5611. __iwl_up(priv);
  5612. mutex_unlock(&priv->mutex);
  5613. }
  5614. static void iwl_bg_restart(struct work_struct *data)
  5615. {
  5616. struct iwl_priv *priv = container_of(data, struct iwl_priv, restart);
  5617. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5618. return;
  5619. iwl_down(priv);
  5620. queue_work(priv->workqueue, &priv->up);
  5621. }
  5622. static void iwl_bg_rx_replenish(struct work_struct *data)
  5623. {
  5624. struct iwl_priv *priv =
  5625. container_of(data, struct iwl_priv, rx_replenish);
  5626. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5627. return;
  5628. mutex_lock(&priv->mutex);
  5629. iwl_rx_replenish(priv);
  5630. mutex_unlock(&priv->mutex);
  5631. }
  5632. static void iwl_bg_post_associate(struct work_struct *data)
  5633. {
  5634. struct iwl_priv *priv = container_of(data, struct iwl_priv,
  5635. post_associate.work);
  5636. int rc = 0;
  5637. struct ieee80211_conf *conf = NULL;
  5638. DECLARE_MAC_BUF(mac);
  5639. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  5640. IWL_ERROR("%s Should not be called in AP mode\n", __FUNCTION__);
  5641. return;
  5642. }
  5643. IWL_DEBUG_ASSOC("Associated as %d to: %s\n",
  5644. priv->assoc_id,
  5645. print_mac(mac, priv->active_rxon.bssid_addr));
  5646. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5647. return;
  5648. mutex_lock(&priv->mutex);
  5649. if (!priv->interface_id || !priv->is_open) {
  5650. mutex_unlock(&priv->mutex);
  5651. return;
  5652. }
  5653. iwl_scan_cancel_timeout(priv, 200);
  5654. conf = ieee80211_get_hw_conf(priv->hw);
  5655. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5656. iwl_commit_rxon(priv);
  5657. memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
  5658. iwl_setup_rxon_timing(priv);
  5659. rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  5660. sizeof(priv->rxon_timing), &priv->rxon_timing);
  5661. if (rc)
  5662. IWL_WARNING("REPLY_RXON_TIMING failed - "
  5663. "Attempting to continue.\n");
  5664. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  5665. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  5666. IWL_DEBUG_ASSOC("assoc id %d beacon interval %d\n",
  5667. priv->assoc_id, priv->beacon_int);
  5668. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  5669. priv->staging_rxon.flags |= RXON_FLG_SHORT_PREAMBLE_MSK;
  5670. else
  5671. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_PREAMBLE_MSK;
  5672. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  5673. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_SLOT_TIME)
  5674. priv->staging_rxon.flags |= RXON_FLG_SHORT_SLOT_MSK;
  5675. else
  5676. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  5677. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
  5678. priv->staging_rxon.flags &= ~RXON_FLG_SHORT_SLOT_MSK;
  5679. }
  5680. iwl_commit_rxon(priv);
  5681. switch (priv->iw_mode) {
  5682. case IEEE80211_IF_TYPE_STA:
  5683. iwl_rate_scale_init(priv->hw, IWL_AP_ID);
  5684. break;
  5685. case IEEE80211_IF_TYPE_IBSS:
  5686. /* clear out the station table */
  5687. iwl_clear_stations_table(priv);
  5688. iwl_add_station(priv, BROADCAST_ADDR, 0, 0);
  5689. iwl_add_station(priv, priv->bssid, 0, 0);
  5690. iwl3945_sync_sta(priv, IWL_STA_ID,
  5691. (priv->phymode == MODE_IEEE80211A)?
  5692. IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP,
  5693. CMD_ASYNC);
  5694. iwl_rate_scale_init(priv->hw, IWL_STA_ID);
  5695. iwl_send_beacon_cmd(priv);
  5696. break;
  5697. default:
  5698. IWL_ERROR("%s Should not be called in %d mode\n",
  5699. __FUNCTION__, priv->iw_mode);
  5700. break;
  5701. }
  5702. iwl_sequence_reset(priv);
  5703. #ifdef CONFIG_IWLWIFI_QOS
  5704. iwl_activate_qos(priv, 0);
  5705. #endif /* CONFIG_IWLWIFI_QOS */
  5706. mutex_unlock(&priv->mutex);
  5707. }
  5708. static void iwl_bg_abort_scan(struct work_struct *work)
  5709. {
  5710. struct iwl_priv *priv = container_of(work, struct iwl_priv, abort_scan);
  5711. if (!iwl_is_ready(priv))
  5712. return;
  5713. mutex_lock(&priv->mutex);
  5714. set_bit(STATUS_SCAN_ABORTING, &priv->status);
  5715. iwl_send_scan_abort(priv);
  5716. mutex_unlock(&priv->mutex);
  5717. }
  5718. static void iwl_bg_scan_completed(struct work_struct *work)
  5719. {
  5720. struct iwl_priv *priv =
  5721. container_of(work, struct iwl_priv, scan_completed);
  5722. IWL_DEBUG(IWL_DL_INFO | IWL_DL_SCAN, "SCAN complete scan\n");
  5723. if (test_bit(STATUS_EXIT_PENDING, &priv->status))
  5724. return;
  5725. ieee80211_scan_completed(priv->hw);
  5726. /* Since setting the TXPOWER may have been deferred while
  5727. * performing the scan, fire one off */
  5728. mutex_lock(&priv->mutex);
  5729. iwl_hw_reg_send_txpower(priv);
  5730. mutex_unlock(&priv->mutex);
  5731. }
  5732. /*****************************************************************************
  5733. *
  5734. * mac80211 entry point functions
  5735. *
  5736. *****************************************************************************/
  5737. static int iwl_mac_start(struct ieee80211_hw *hw)
  5738. {
  5739. struct iwl_priv *priv = hw->priv;
  5740. IWL_DEBUG_MAC80211("enter\n");
  5741. /* we should be verifying the device is ready to be opened */
  5742. mutex_lock(&priv->mutex);
  5743. priv->is_open = 1;
  5744. if (!iwl_is_rfkill(priv))
  5745. ieee80211_start_queues(priv->hw);
  5746. mutex_unlock(&priv->mutex);
  5747. IWL_DEBUG_MAC80211("leave\n");
  5748. return 0;
  5749. }
  5750. static void iwl_mac_stop(struct ieee80211_hw *hw)
  5751. {
  5752. struct iwl_priv *priv = hw->priv;
  5753. IWL_DEBUG_MAC80211("enter\n");
  5754. mutex_lock(&priv->mutex);
  5755. /* stop mac, cancel any scan request and clear
  5756. * RXON_FILTER_ASSOC_MSK BIT
  5757. */
  5758. priv->is_open = 0;
  5759. iwl_scan_cancel_timeout(priv, 100);
  5760. cancel_delayed_work(&priv->post_associate);
  5761. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5762. iwl_commit_rxon(priv);
  5763. mutex_unlock(&priv->mutex);
  5764. IWL_DEBUG_MAC80211("leave\n");
  5765. }
  5766. static int iwl_mac_tx(struct ieee80211_hw *hw, struct sk_buff *skb,
  5767. struct ieee80211_tx_control *ctl)
  5768. {
  5769. struct iwl_priv *priv = hw->priv;
  5770. IWL_DEBUG_MAC80211("enter\n");
  5771. if (priv->iw_mode == IEEE80211_IF_TYPE_MNTR) {
  5772. IWL_DEBUG_MAC80211("leave - monitor\n");
  5773. return -1;
  5774. }
  5775. IWL_DEBUG_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  5776. ctl->tx_rate);
  5777. if (iwl_tx_skb(priv, skb, ctl))
  5778. dev_kfree_skb_any(skb);
  5779. IWL_DEBUG_MAC80211("leave\n");
  5780. return 0;
  5781. }
  5782. static int iwl_mac_add_interface(struct ieee80211_hw *hw,
  5783. struct ieee80211_if_init_conf *conf)
  5784. {
  5785. struct iwl_priv *priv = hw->priv;
  5786. unsigned long flags;
  5787. DECLARE_MAC_BUF(mac);
  5788. IWL_DEBUG_MAC80211("enter: id %d, type %d\n", conf->if_id, conf->type);
  5789. if (priv->interface_id) {
  5790. IWL_DEBUG_MAC80211("leave - interface_id != 0\n");
  5791. return -EOPNOTSUPP;
  5792. }
  5793. spin_lock_irqsave(&priv->lock, flags);
  5794. priv->interface_id = conf->if_id;
  5795. spin_unlock_irqrestore(&priv->lock, flags);
  5796. mutex_lock(&priv->mutex);
  5797. if (conf->mac_addr) {
  5798. IWL_DEBUG_MAC80211("Set: %s\n", print_mac(mac, conf->mac_addr));
  5799. memcpy(priv->mac_addr, conf->mac_addr, ETH_ALEN);
  5800. }
  5801. iwl_set_mode(priv, conf->type);
  5802. IWL_DEBUG_MAC80211("leave\n");
  5803. mutex_unlock(&priv->mutex);
  5804. return 0;
  5805. }
  5806. /**
  5807. * iwl_mac_config - mac80211 config callback
  5808. *
  5809. * We ignore conf->flags & IEEE80211_CONF_SHORT_SLOT_TIME since it seems to
  5810. * be set inappropriately and the driver currently sets the hardware up to
  5811. * use it whenever needed.
  5812. */
  5813. static int iwl_mac_config(struct ieee80211_hw *hw, struct ieee80211_conf *conf)
  5814. {
  5815. struct iwl_priv *priv = hw->priv;
  5816. const struct iwl_channel_info *ch_info;
  5817. unsigned long flags;
  5818. mutex_lock(&priv->mutex);
  5819. IWL_DEBUG_MAC80211("enter to channel %d\n", conf->channel);
  5820. if (!iwl_is_ready(priv)) {
  5821. IWL_DEBUG_MAC80211("leave - not ready\n");
  5822. mutex_unlock(&priv->mutex);
  5823. return -EIO;
  5824. }
  5825. /* TODO: Figure out how to get ieee80211_local->sta_scanning w/ only
  5826. * what is exposed through include/ declarations */
  5827. if (unlikely(!iwl_param_disable_hw_scan &&
  5828. test_bit(STATUS_SCANNING, &priv->status))) {
  5829. IWL_DEBUG_MAC80211("leave - scanning\n");
  5830. mutex_unlock(&priv->mutex);
  5831. return 0;
  5832. }
  5833. spin_lock_irqsave(&priv->lock, flags);
  5834. ch_info = iwl_get_channel_info(priv, conf->phymode, conf->channel);
  5835. if (!is_channel_valid(ch_info)) {
  5836. IWL_DEBUG_SCAN("Channel %d [%d] is INVALID for this SKU.\n",
  5837. conf->channel, conf->phymode);
  5838. IWL_DEBUG_MAC80211("leave - invalid channel\n");
  5839. spin_unlock_irqrestore(&priv->lock, flags);
  5840. mutex_unlock(&priv->mutex);
  5841. return -EINVAL;
  5842. }
  5843. iwl_set_rxon_channel(priv, conf->phymode, conf->channel);
  5844. iwl_set_flags_for_phymode(priv, conf->phymode);
  5845. /* The list of supported rates and rate mask can be different
  5846. * for each phymode; since the phymode may have changed, reset
  5847. * the rate mask to what mac80211 lists */
  5848. iwl_set_rate(priv);
  5849. spin_unlock_irqrestore(&priv->lock, flags);
  5850. #ifdef IEEE80211_CONF_CHANNEL_SWITCH
  5851. if (conf->flags & IEEE80211_CONF_CHANNEL_SWITCH) {
  5852. iwl_hw_channel_switch(priv, conf->channel);
  5853. mutex_unlock(&priv->mutex);
  5854. return 0;
  5855. }
  5856. #endif
  5857. iwl_radio_kill_sw(priv, !conf->radio_enabled);
  5858. if (!conf->radio_enabled) {
  5859. IWL_DEBUG_MAC80211("leave - radio disabled\n");
  5860. mutex_unlock(&priv->mutex);
  5861. return 0;
  5862. }
  5863. if (iwl_is_rfkill(priv)) {
  5864. IWL_DEBUG_MAC80211("leave - RF kill\n");
  5865. mutex_unlock(&priv->mutex);
  5866. return -EIO;
  5867. }
  5868. iwl_set_rate(priv);
  5869. if (memcmp(&priv->active_rxon,
  5870. &priv->staging_rxon, sizeof(priv->staging_rxon)))
  5871. iwl_commit_rxon(priv);
  5872. else
  5873. IWL_DEBUG_INFO("No re-sending same RXON configuration.\n");
  5874. IWL_DEBUG_MAC80211("leave\n");
  5875. mutex_unlock(&priv->mutex);
  5876. return 0;
  5877. }
  5878. static void iwl_config_ap(struct iwl_priv *priv)
  5879. {
  5880. int rc = 0;
  5881. if (priv->status & STATUS_EXIT_PENDING)
  5882. return;
  5883. /* The following should be done only at AP bring up */
  5884. if ((priv->active_rxon.filter_flags & RXON_FILTER_ASSOC_MSK) == 0) {
  5885. /* RXON - unassoc (to set timing command) */
  5886. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  5887. iwl_commit_rxon(priv);
  5888. /* RXON Timing */
  5889. memset(&priv->rxon_timing, 0, sizeof(struct iwl_rxon_time_cmd));
  5890. iwl_setup_rxon_timing(priv);
  5891. rc = iwl_send_cmd_pdu(priv, REPLY_RXON_TIMING,
  5892. sizeof(priv->rxon_timing), &priv->rxon_timing);
  5893. if (rc)
  5894. IWL_WARNING("REPLY_RXON_TIMING failed - "
  5895. "Attempting to continue.\n");
  5896. /* FIXME: what should be the assoc_id for AP? */
  5897. priv->staging_rxon.assoc_id = cpu_to_le16(priv->assoc_id);
  5898. if (priv->assoc_capability & WLAN_CAPABILITY_SHORT_PREAMBLE)
  5899. priv->staging_rxon.flags |=
  5900. RXON_FLG_SHORT_PREAMBLE_MSK;
  5901. else
  5902. priv->staging_rxon.flags &=
  5903. ~RXON_FLG_SHORT_PREAMBLE_MSK;
  5904. if (priv->staging_rxon.flags & RXON_FLG_BAND_24G_MSK) {
  5905. if (priv->assoc_capability &
  5906. WLAN_CAPABILITY_SHORT_SLOT_TIME)
  5907. priv->staging_rxon.flags |=
  5908. RXON_FLG_SHORT_SLOT_MSK;
  5909. else
  5910. priv->staging_rxon.flags &=
  5911. ~RXON_FLG_SHORT_SLOT_MSK;
  5912. if (priv->iw_mode == IEEE80211_IF_TYPE_IBSS)
  5913. priv->staging_rxon.flags &=
  5914. ~RXON_FLG_SHORT_SLOT_MSK;
  5915. }
  5916. /* restore RXON assoc */
  5917. priv->staging_rxon.filter_flags |= RXON_FILTER_ASSOC_MSK;
  5918. iwl_commit_rxon(priv);
  5919. iwl_add_station(priv, BROADCAST_ADDR, 0, 0);
  5920. }
  5921. iwl_send_beacon_cmd(priv);
  5922. /* FIXME - we need to add code here to detect a totally new
  5923. * configuration, reset the AP, unassoc, rxon timing, assoc,
  5924. * clear sta table, add BCAST sta... */
  5925. }
  5926. static int iwl_mac_config_interface(struct ieee80211_hw *hw, int if_id,
  5927. struct ieee80211_if_conf *conf)
  5928. {
  5929. struct iwl_priv *priv = hw->priv;
  5930. DECLARE_MAC_BUF(mac);
  5931. unsigned long flags;
  5932. int rc;
  5933. if (conf == NULL)
  5934. return -EIO;
  5935. /* XXX: this MUST use conf->mac_addr */
  5936. if ((priv->iw_mode == IEEE80211_IF_TYPE_AP) &&
  5937. (!conf->beacon || !conf->ssid_len)) {
  5938. IWL_DEBUG_MAC80211
  5939. ("Leaving in AP mode because HostAPD is not ready.\n");
  5940. return 0;
  5941. }
  5942. mutex_lock(&priv->mutex);
  5943. IWL_DEBUG_MAC80211("enter: interface id %d\n", if_id);
  5944. if (conf->bssid)
  5945. IWL_DEBUG_MAC80211("bssid: %s\n",
  5946. print_mac(mac, conf->bssid));
  5947. /*
  5948. * very dubious code was here; the probe filtering flag is never set:
  5949. *
  5950. if (unlikely(test_bit(STATUS_SCANNING, &priv->status)) &&
  5951. !(priv->hw->flags & IEEE80211_HW_NO_PROBE_FILTERING)) {
  5952. */
  5953. if (unlikely(test_bit(STATUS_SCANNING, &priv->status))) {
  5954. IWL_DEBUG_MAC80211("leave - scanning\n");
  5955. mutex_unlock(&priv->mutex);
  5956. return 0;
  5957. }
  5958. if (priv->interface_id != if_id) {
  5959. IWL_DEBUG_MAC80211("leave - interface_id != if_id\n");
  5960. mutex_unlock(&priv->mutex);
  5961. return 0;
  5962. }
  5963. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) {
  5964. if (!conf->bssid) {
  5965. conf->bssid = priv->mac_addr;
  5966. memcpy(priv->bssid, priv->mac_addr, ETH_ALEN);
  5967. IWL_DEBUG_MAC80211("bssid was set to: %s\n",
  5968. print_mac(mac, conf->bssid));
  5969. }
  5970. if (priv->ibss_beacon)
  5971. dev_kfree_skb(priv->ibss_beacon);
  5972. priv->ibss_beacon = conf->beacon;
  5973. }
  5974. if (conf->bssid && !is_zero_ether_addr(conf->bssid) &&
  5975. !is_multicast_ether_addr(conf->bssid)) {
  5976. /* If there is currently a HW scan going on in the background
  5977. * then we need to cancel it else the RXON below will fail. */
  5978. if (iwl_scan_cancel_timeout(priv, 100)) {
  5979. IWL_WARNING("Aborted scan still in progress "
  5980. "after 100ms\n");
  5981. IWL_DEBUG_MAC80211("leaving - scan abort failed.\n");
  5982. mutex_unlock(&priv->mutex);
  5983. return -EAGAIN;
  5984. }
  5985. memcpy(priv->staging_rxon.bssid_addr, conf->bssid, ETH_ALEN);
  5986. /* TODO: Audit driver for usage of these members and see
  5987. * if mac80211 deprecates them (priv->bssid looks like it
  5988. * shouldn't be there, but I haven't scanned the IBSS code
  5989. * to verify) - jpk */
  5990. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  5991. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  5992. iwl_config_ap(priv);
  5993. else {
  5994. rc = iwl_commit_rxon(priv);
  5995. if ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && rc)
  5996. iwl_add_station(priv,
  5997. priv->active_rxon.bssid_addr, 1, 0);
  5998. }
  5999. } else {
  6000. iwl_scan_cancel_timeout(priv, 100);
  6001. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  6002. iwl_commit_rxon(priv);
  6003. }
  6004. spin_lock_irqsave(&priv->lock, flags);
  6005. if (!conf->ssid_len)
  6006. memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
  6007. else
  6008. memcpy(priv->essid, conf->ssid, conf->ssid_len);
  6009. priv->essid_len = conf->ssid_len;
  6010. spin_unlock_irqrestore(&priv->lock, flags);
  6011. IWL_DEBUG_MAC80211("leave\n");
  6012. mutex_unlock(&priv->mutex);
  6013. return 0;
  6014. }
  6015. static void iwl_configure_filter(struct ieee80211_hw *hw,
  6016. unsigned int changed_flags,
  6017. unsigned int *total_flags,
  6018. int mc_count, struct dev_addr_list *mc_list)
  6019. {
  6020. /*
  6021. * XXX: dummy
  6022. * see also iwl_connection_init_rx_config
  6023. */
  6024. *total_flags = 0;
  6025. }
  6026. static void iwl_mac_remove_interface(struct ieee80211_hw *hw,
  6027. struct ieee80211_if_init_conf *conf)
  6028. {
  6029. struct iwl_priv *priv = hw->priv;
  6030. IWL_DEBUG_MAC80211("enter\n");
  6031. mutex_lock(&priv->mutex);
  6032. iwl_scan_cancel_timeout(priv, 100);
  6033. cancel_delayed_work(&priv->post_associate);
  6034. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  6035. iwl_commit_rxon(priv);
  6036. if (priv->interface_id == conf->if_id) {
  6037. priv->interface_id = 0;
  6038. memset(priv->bssid, 0, ETH_ALEN);
  6039. memset(priv->essid, 0, IW_ESSID_MAX_SIZE);
  6040. priv->essid_len = 0;
  6041. }
  6042. mutex_unlock(&priv->mutex);
  6043. IWL_DEBUG_MAC80211("leave\n");
  6044. }
  6045. #define IWL_DELAY_NEXT_SCAN (HZ*2)
  6046. static int iwl_mac_hw_scan(struct ieee80211_hw *hw, u8 *ssid, size_t len)
  6047. {
  6048. int rc = 0;
  6049. unsigned long flags;
  6050. struct iwl_priv *priv = hw->priv;
  6051. IWL_DEBUG_MAC80211("enter\n");
  6052. mutex_lock(&priv->mutex);
  6053. spin_lock_irqsave(&priv->lock, flags);
  6054. if (!iwl_is_ready_rf(priv)) {
  6055. rc = -EIO;
  6056. IWL_DEBUG_MAC80211("leave - not ready or exit pending\n");
  6057. goto out_unlock;
  6058. }
  6059. if (priv->iw_mode == IEEE80211_IF_TYPE_AP) { /* APs don't scan */
  6060. rc = -EIO;
  6061. IWL_ERROR("ERROR: APs don't scan\n");
  6062. goto out_unlock;
  6063. }
  6064. /* if we just finished scan ask for delay */
  6065. if (priv->last_scan_jiffies &&
  6066. time_after(priv->last_scan_jiffies + IWL_DELAY_NEXT_SCAN,
  6067. jiffies)) {
  6068. rc = -EAGAIN;
  6069. goto out_unlock;
  6070. }
  6071. if (len) {
  6072. IWL_DEBUG_SCAN("direct scan for "
  6073. "%s [%d]\n ",
  6074. iwl_escape_essid(ssid, len), (int)len);
  6075. priv->one_direct_scan = 1;
  6076. priv->direct_ssid_len = (u8)
  6077. min((u8) len, (u8) IW_ESSID_MAX_SIZE);
  6078. memcpy(priv->direct_ssid, ssid, priv->direct_ssid_len);
  6079. } else
  6080. priv->one_direct_scan = 0;
  6081. rc = iwl_scan_initiate(priv);
  6082. IWL_DEBUG_MAC80211("leave\n");
  6083. out_unlock:
  6084. spin_unlock_irqrestore(&priv->lock, flags);
  6085. mutex_unlock(&priv->mutex);
  6086. return rc;
  6087. }
  6088. static int iwl_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  6089. const u8 *local_addr, const u8 *addr,
  6090. struct ieee80211_key_conf *key)
  6091. {
  6092. struct iwl_priv *priv = hw->priv;
  6093. int rc = 0;
  6094. u8 sta_id;
  6095. IWL_DEBUG_MAC80211("enter\n");
  6096. if (!iwl_param_hwcrypto) {
  6097. IWL_DEBUG_MAC80211("leave - hwcrypto disabled\n");
  6098. return -EOPNOTSUPP;
  6099. }
  6100. if (is_zero_ether_addr(addr))
  6101. /* only support pairwise keys */
  6102. return -EOPNOTSUPP;
  6103. sta_id = iwl_hw_find_station(priv, addr);
  6104. if (sta_id == IWL_INVALID_STATION) {
  6105. DECLARE_MAC_BUF(mac);
  6106. IWL_DEBUG_MAC80211("leave - %s not in station map.\n",
  6107. print_mac(mac, addr));
  6108. return -EINVAL;
  6109. }
  6110. mutex_lock(&priv->mutex);
  6111. iwl_scan_cancel_timeout(priv, 100);
  6112. switch (cmd) {
  6113. case SET_KEY:
  6114. rc = iwl_update_sta_key_info(priv, key, sta_id);
  6115. if (!rc) {
  6116. iwl_set_rxon_hwcrypto(priv, 1);
  6117. iwl_commit_rxon(priv);
  6118. key->hw_key_idx = sta_id;
  6119. IWL_DEBUG_MAC80211("set_key success, using hwcrypto\n");
  6120. key->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  6121. }
  6122. break;
  6123. case DISABLE_KEY:
  6124. rc = iwl_clear_sta_key_info(priv, sta_id);
  6125. if (!rc) {
  6126. iwl_set_rxon_hwcrypto(priv, 0);
  6127. iwl_commit_rxon(priv);
  6128. IWL_DEBUG_MAC80211("disable hwcrypto key\n");
  6129. }
  6130. break;
  6131. default:
  6132. rc = -EINVAL;
  6133. }
  6134. IWL_DEBUG_MAC80211("leave\n");
  6135. mutex_unlock(&priv->mutex);
  6136. return rc;
  6137. }
  6138. static int iwl_mac_conf_tx(struct ieee80211_hw *hw, int queue,
  6139. const struct ieee80211_tx_queue_params *params)
  6140. {
  6141. struct iwl_priv *priv = hw->priv;
  6142. #ifdef CONFIG_IWLWIFI_QOS
  6143. unsigned long flags;
  6144. int q;
  6145. #endif /* CONFIG_IWL_QOS */
  6146. IWL_DEBUG_MAC80211("enter\n");
  6147. if (!iwl_is_ready_rf(priv)) {
  6148. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  6149. return -EIO;
  6150. }
  6151. if (queue >= AC_NUM) {
  6152. IWL_DEBUG_MAC80211("leave - queue >= AC_NUM %d\n", queue);
  6153. return 0;
  6154. }
  6155. #ifdef CONFIG_IWLWIFI_QOS
  6156. if (!priv->qos_data.qos_enable) {
  6157. priv->qos_data.qos_active = 0;
  6158. IWL_DEBUG_MAC80211("leave - qos not enabled\n");
  6159. return 0;
  6160. }
  6161. q = AC_NUM - 1 - queue;
  6162. spin_lock_irqsave(&priv->lock, flags);
  6163. priv->qos_data.def_qos_parm.ac[q].cw_min = cpu_to_le16(params->cw_min);
  6164. priv->qos_data.def_qos_parm.ac[q].cw_max = cpu_to_le16(params->cw_max);
  6165. priv->qos_data.def_qos_parm.ac[q].aifsn = params->aifs;
  6166. priv->qos_data.def_qos_parm.ac[q].edca_txop =
  6167. cpu_to_le16((params->burst_time * 100));
  6168. priv->qos_data.def_qos_parm.ac[q].reserved1 = 0;
  6169. priv->qos_data.qos_active = 1;
  6170. spin_unlock_irqrestore(&priv->lock, flags);
  6171. mutex_lock(&priv->mutex);
  6172. if (priv->iw_mode == IEEE80211_IF_TYPE_AP)
  6173. iwl_activate_qos(priv, 1);
  6174. else if (priv->assoc_id && iwl_is_associated(priv))
  6175. iwl_activate_qos(priv, 0);
  6176. mutex_unlock(&priv->mutex);
  6177. #endif /*CONFIG_IWLWIFI_QOS */
  6178. IWL_DEBUG_MAC80211("leave\n");
  6179. return 0;
  6180. }
  6181. static int iwl_mac_get_tx_stats(struct ieee80211_hw *hw,
  6182. struct ieee80211_tx_queue_stats *stats)
  6183. {
  6184. struct iwl_priv *priv = hw->priv;
  6185. int i, avail;
  6186. struct iwl_tx_queue *txq;
  6187. struct iwl_queue *q;
  6188. unsigned long flags;
  6189. IWL_DEBUG_MAC80211("enter\n");
  6190. if (!iwl_is_ready_rf(priv)) {
  6191. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  6192. return -EIO;
  6193. }
  6194. spin_lock_irqsave(&priv->lock, flags);
  6195. for (i = 0; i < AC_NUM; i++) {
  6196. txq = &priv->txq[i];
  6197. q = &txq->q;
  6198. avail = iwl_queue_space(q);
  6199. stats->data[i].len = q->n_window - avail;
  6200. stats->data[i].limit = q->n_window - q->high_mark;
  6201. stats->data[i].count = q->n_window;
  6202. }
  6203. spin_unlock_irqrestore(&priv->lock, flags);
  6204. IWL_DEBUG_MAC80211("leave\n");
  6205. return 0;
  6206. }
  6207. static int iwl_mac_get_stats(struct ieee80211_hw *hw,
  6208. struct ieee80211_low_level_stats *stats)
  6209. {
  6210. IWL_DEBUG_MAC80211("enter\n");
  6211. IWL_DEBUG_MAC80211("leave\n");
  6212. return 0;
  6213. }
  6214. static u64 iwl_mac_get_tsf(struct ieee80211_hw *hw)
  6215. {
  6216. IWL_DEBUG_MAC80211("enter\n");
  6217. IWL_DEBUG_MAC80211("leave\n");
  6218. return 0;
  6219. }
  6220. static void iwl_mac_reset_tsf(struct ieee80211_hw *hw)
  6221. {
  6222. struct iwl_priv *priv = hw->priv;
  6223. unsigned long flags;
  6224. mutex_lock(&priv->mutex);
  6225. IWL_DEBUG_MAC80211("enter\n");
  6226. #ifdef CONFIG_IWLWIFI_QOS
  6227. iwl_reset_qos(priv);
  6228. #endif
  6229. cancel_delayed_work(&priv->post_associate);
  6230. spin_lock_irqsave(&priv->lock, flags);
  6231. priv->assoc_id = 0;
  6232. priv->assoc_capability = 0;
  6233. priv->call_post_assoc_from_beacon = 0;
  6234. /* new association get rid of ibss beacon skb */
  6235. if (priv->ibss_beacon)
  6236. dev_kfree_skb(priv->ibss_beacon);
  6237. priv->ibss_beacon = NULL;
  6238. priv->beacon_int = priv->hw->conf.beacon_int;
  6239. priv->timestamp1 = 0;
  6240. priv->timestamp0 = 0;
  6241. if ((priv->iw_mode == IEEE80211_IF_TYPE_STA))
  6242. priv->beacon_int = 0;
  6243. spin_unlock_irqrestore(&priv->lock, flags);
  6244. /* we are restarting association process
  6245. * clear RXON_FILTER_ASSOC_MSK bit
  6246. */
  6247. if (priv->iw_mode != IEEE80211_IF_TYPE_AP) {
  6248. iwl_scan_cancel_timeout(priv, 100);
  6249. priv->staging_rxon.filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  6250. iwl_commit_rxon(priv);
  6251. }
  6252. /* Per mac80211.h: This is only used in IBSS mode... */
  6253. if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
  6254. IWL_DEBUG_MAC80211("leave - not in IBSS\n");
  6255. mutex_unlock(&priv->mutex);
  6256. return;
  6257. }
  6258. if (!iwl_is_ready_rf(priv)) {
  6259. IWL_DEBUG_MAC80211("leave - not ready\n");
  6260. mutex_unlock(&priv->mutex);
  6261. return;
  6262. }
  6263. priv->only_active_channel = 0;
  6264. iwl_set_rate(priv);
  6265. mutex_unlock(&priv->mutex);
  6266. IWL_DEBUG_MAC80211("leave\n");
  6267. }
  6268. static int iwl_mac_beacon_update(struct ieee80211_hw *hw, struct sk_buff *skb,
  6269. struct ieee80211_tx_control *control)
  6270. {
  6271. struct iwl_priv *priv = hw->priv;
  6272. unsigned long flags;
  6273. mutex_lock(&priv->mutex);
  6274. IWL_DEBUG_MAC80211("enter\n");
  6275. if (!iwl_is_ready_rf(priv)) {
  6276. IWL_DEBUG_MAC80211("leave - RF not ready\n");
  6277. mutex_unlock(&priv->mutex);
  6278. return -EIO;
  6279. }
  6280. if (priv->iw_mode != IEEE80211_IF_TYPE_IBSS) {
  6281. IWL_DEBUG_MAC80211("leave - not IBSS\n");
  6282. mutex_unlock(&priv->mutex);
  6283. return -EIO;
  6284. }
  6285. spin_lock_irqsave(&priv->lock, flags);
  6286. if (priv->ibss_beacon)
  6287. dev_kfree_skb(priv->ibss_beacon);
  6288. priv->ibss_beacon = skb;
  6289. priv->assoc_id = 0;
  6290. IWL_DEBUG_MAC80211("leave\n");
  6291. spin_unlock_irqrestore(&priv->lock, flags);
  6292. #ifdef CONFIG_IWLWIFI_QOS
  6293. iwl_reset_qos(priv);
  6294. #endif
  6295. queue_work(priv->workqueue, &priv->post_associate.work);
  6296. mutex_unlock(&priv->mutex);
  6297. return 0;
  6298. }
  6299. /*****************************************************************************
  6300. *
  6301. * sysfs attributes
  6302. *
  6303. *****************************************************************************/
  6304. #ifdef CONFIG_IWLWIFI_DEBUG
  6305. /*
  6306. * The following adds a new attribute to the sysfs representation
  6307. * of this device driver (i.e. a new file in /sys/bus/pci/drivers/iwl/)
  6308. * used for controlling the debug level.
  6309. *
  6310. * See the level definitions in iwl for details.
  6311. */
  6312. static ssize_t show_debug_level(struct device_driver *d, char *buf)
  6313. {
  6314. return sprintf(buf, "0x%08X\n", iwl_debug_level);
  6315. }
  6316. static ssize_t store_debug_level(struct device_driver *d,
  6317. const char *buf, size_t count)
  6318. {
  6319. char *p = (char *)buf;
  6320. u32 val;
  6321. val = simple_strtoul(p, &p, 0);
  6322. if (p == buf)
  6323. printk(KERN_INFO DRV_NAME
  6324. ": %s is not in hex or decimal form.\n", buf);
  6325. else
  6326. iwl_debug_level = val;
  6327. return strnlen(buf, count);
  6328. }
  6329. static DRIVER_ATTR(debug_level, S_IWUSR | S_IRUGO,
  6330. show_debug_level, store_debug_level);
  6331. #endif /* CONFIG_IWLWIFI_DEBUG */
  6332. static ssize_t show_rf_kill(struct device *d,
  6333. struct device_attribute *attr, char *buf)
  6334. {
  6335. /*
  6336. * 0 - RF kill not enabled
  6337. * 1 - SW based RF kill active (sysfs)
  6338. * 2 - HW based RF kill active
  6339. * 3 - Both HW and SW based RF kill active
  6340. */
  6341. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  6342. int val = (test_bit(STATUS_RF_KILL_SW, &priv->status) ? 0x1 : 0x0) |
  6343. (test_bit(STATUS_RF_KILL_HW, &priv->status) ? 0x2 : 0x0);
  6344. return sprintf(buf, "%i\n", val);
  6345. }
  6346. static ssize_t store_rf_kill(struct device *d,
  6347. struct device_attribute *attr,
  6348. const char *buf, size_t count)
  6349. {
  6350. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  6351. mutex_lock(&priv->mutex);
  6352. iwl_radio_kill_sw(priv, buf[0] == '1');
  6353. mutex_unlock(&priv->mutex);
  6354. return count;
  6355. }
  6356. static DEVICE_ATTR(rf_kill, S_IWUSR | S_IRUGO, show_rf_kill, store_rf_kill);
  6357. static ssize_t show_temperature(struct device *d,
  6358. struct device_attribute *attr, char *buf)
  6359. {
  6360. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  6361. if (!iwl_is_alive(priv))
  6362. return -EAGAIN;
  6363. return sprintf(buf, "%d\n", iwl_hw_get_temperature(priv));
  6364. }
  6365. static DEVICE_ATTR(temperature, S_IRUGO, show_temperature, NULL);
  6366. static ssize_t show_rs_window(struct device *d,
  6367. struct device_attribute *attr,
  6368. char *buf)
  6369. {
  6370. struct iwl_priv *priv = d->driver_data;
  6371. return iwl_fill_rs_info(priv->hw, buf, IWL_AP_ID);
  6372. }
  6373. static DEVICE_ATTR(rs_window, S_IRUGO, show_rs_window, NULL);
  6374. static ssize_t show_tx_power(struct device *d,
  6375. struct device_attribute *attr, char *buf)
  6376. {
  6377. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  6378. return sprintf(buf, "%d\n", priv->user_txpower_limit);
  6379. }
  6380. static ssize_t store_tx_power(struct device *d,
  6381. struct device_attribute *attr,
  6382. const char *buf, size_t count)
  6383. {
  6384. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  6385. char *p = (char *)buf;
  6386. u32 val;
  6387. val = simple_strtoul(p, &p, 10);
  6388. if (p == buf)
  6389. printk(KERN_INFO DRV_NAME
  6390. ": %s is not in decimal form.\n", buf);
  6391. else
  6392. iwl_hw_reg_set_txpower(priv, val);
  6393. return count;
  6394. }
  6395. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, show_tx_power, store_tx_power);
  6396. static ssize_t show_flags(struct device *d,
  6397. struct device_attribute *attr, char *buf)
  6398. {
  6399. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  6400. return sprintf(buf, "0x%04X\n", priv->active_rxon.flags);
  6401. }
  6402. static ssize_t store_flags(struct device *d,
  6403. struct device_attribute *attr,
  6404. const char *buf, size_t count)
  6405. {
  6406. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  6407. u32 flags = simple_strtoul(buf, NULL, 0);
  6408. mutex_lock(&priv->mutex);
  6409. if (le32_to_cpu(priv->staging_rxon.flags) != flags) {
  6410. /* Cancel any currently running scans... */
  6411. if (iwl_scan_cancel_timeout(priv, 100))
  6412. IWL_WARNING("Could not cancel scan.\n");
  6413. else {
  6414. IWL_DEBUG_INFO("Committing rxon.flags = 0x%04X\n",
  6415. flags);
  6416. priv->staging_rxon.flags = cpu_to_le32(flags);
  6417. iwl_commit_rxon(priv);
  6418. }
  6419. }
  6420. mutex_unlock(&priv->mutex);
  6421. return count;
  6422. }
  6423. static DEVICE_ATTR(flags, S_IWUSR | S_IRUGO, show_flags, store_flags);
  6424. static ssize_t show_filter_flags(struct device *d,
  6425. struct device_attribute *attr, char *buf)
  6426. {
  6427. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  6428. return sprintf(buf, "0x%04X\n",
  6429. le32_to_cpu(priv->active_rxon.filter_flags));
  6430. }
  6431. static ssize_t store_filter_flags(struct device *d,
  6432. struct device_attribute *attr,
  6433. const char *buf, size_t count)
  6434. {
  6435. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  6436. u32 filter_flags = simple_strtoul(buf, NULL, 0);
  6437. mutex_lock(&priv->mutex);
  6438. if (le32_to_cpu(priv->staging_rxon.filter_flags) != filter_flags) {
  6439. /* Cancel any currently running scans... */
  6440. if (iwl_scan_cancel_timeout(priv, 100))
  6441. IWL_WARNING("Could not cancel scan.\n");
  6442. else {
  6443. IWL_DEBUG_INFO("Committing rxon.filter_flags = "
  6444. "0x%04X\n", filter_flags);
  6445. priv->staging_rxon.filter_flags =
  6446. cpu_to_le32(filter_flags);
  6447. iwl_commit_rxon(priv);
  6448. }
  6449. }
  6450. mutex_unlock(&priv->mutex);
  6451. return count;
  6452. }
  6453. static DEVICE_ATTR(filter_flags, S_IWUSR | S_IRUGO, show_filter_flags,
  6454. store_filter_flags);
  6455. static ssize_t show_tune(struct device *d,
  6456. struct device_attribute *attr, char *buf)
  6457. {
  6458. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  6459. return sprintf(buf, "0x%04X\n",
  6460. (priv->phymode << 8) |
  6461. le16_to_cpu(priv->active_rxon.channel));
  6462. }
  6463. static void iwl_set_flags_for_phymode(struct iwl_priv *priv, u8 phymode);
  6464. static ssize_t store_tune(struct device *d,
  6465. struct device_attribute *attr,
  6466. const char *buf, size_t count)
  6467. {
  6468. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  6469. char *p = (char *)buf;
  6470. u16 tune = simple_strtoul(p, &p, 0);
  6471. u8 phymode = (tune >> 8) & 0xff;
  6472. u16 channel = tune & 0xff;
  6473. IWL_DEBUG_INFO("Tune request to:%d channel:%d\n", phymode, channel);
  6474. mutex_lock(&priv->mutex);
  6475. if ((le16_to_cpu(priv->staging_rxon.channel) != channel) ||
  6476. (priv->phymode != phymode)) {
  6477. const struct iwl_channel_info *ch_info;
  6478. ch_info = iwl_get_channel_info(priv, phymode, channel);
  6479. if (!ch_info) {
  6480. IWL_WARNING("Requested invalid phymode/channel "
  6481. "combination: %d %d\n", phymode, channel);
  6482. mutex_unlock(&priv->mutex);
  6483. return -EINVAL;
  6484. }
  6485. /* Cancel any currently running scans... */
  6486. if (iwl_scan_cancel_timeout(priv, 100))
  6487. IWL_WARNING("Could not cancel scan.\n");
  6488. else {
  6489. IWL_DEBUG_INFO("Committing phymode and "
  6490. "rxon.channel = %d %d\n",
  6491. phymode, channel);
  6492. iwl_set_rxon_channel(priv, phymode, channel);
  6493. iwl_set_flags_for_phymode(priv, phymode);
  6494. iwl_set_rate(priv);
  6495. iwl_commit_rxon(priv);
  6496. }
  6497. }
  6498. mutex_unlock(&priv->mutex);
  6499. return count;
  6500. }
  6501. static DEVICE_ATTR(tune, S_IWUSR | S_IRUGO, show_tune, store_tune);
  6502. #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
  6503. static ssize_t show_measurement(struct device *d,
  6504. struct device_attribute *attr, char *buf)
  6505. {
  6506. struct iwl_priv *priv = dev_get_drvdata(d);
  6507. struct iwl_spectrum_notification measure_report;
  6508. u32 size = sizeof(measure_report), len = 0, ofs = 0;
  6509. u8 *data = (u8 *) & measure_report;
  6510. unsigned long flags;
  6511. spin_lock_irqsave(&priv->lock, flags);
  6512. if (!(priv->measurement_status & MEASUREMENT_READY)) {
  6513. spin_unlock_irqrestore(&priv->lock, flags);
  6514. return 0;
  6515. }
  6516. memcpy(&measure_report, &priv->measure_report, size);
  6517. priv->measurement_status = 0;
  6518. spin_unlock_irqrestore(&priv->lock, flags);
  6519. while (size && (PAGE_SIZE - len)) {
  6520. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  6521. PAGE_SIZE - len, 1);
  6522. len = strlen(buf);
  6523. if (PAGE_SIZE - len)
  6524. buf[len++] = '\n';
  6525. ofs += 16;
  6526. size -= min(size, 16U);
  6527. }
  6528. return len;
  6529. }
  6530. static ssize_t store_measurement(struct device *d,
  6531. struct device_attribute *attr,
  6532. const char *buf, size_t count)
  6533. {
  6534. struct iwl_priv *priv = dev_get_drvdata(d);
  6535. struct ieee80211_measurement_params params = {
  6536. .channel = le16_to_cpu(priv->active_rxon.channel),
  6537. .start_time = cpu_to_le64(priv->last_tsf),
  6538. .duration = cpu_to_le16(1),
  6539. };
  6540. u8 type = IWL_MEASURE_BASIC;
  6541. u8 buffer[32];
  6542. u8 channel;
  6543. if (count) {
  6544. char *p = buffer;
  6545. strncpy(buffer, buf, min(sizeof(buffer), count));
  6546. channel = simple_strtoul(p, NULL, 0);
  6547. if (channel)
  6548. params.channel = channel;
  6549. p = buffer;
  6550. while (*p && *p != ' ')
  6551. p++;
  6552. if (*p)
  6553. type = simple_strtoul(p + 1, NULL, 0);
  6554. }
  6555. IWL_DEBUG_INFO("Invoking measurement of type %d on "
  6556. "channel %d (for '%s')\n", type, params.channel, buf);
  6557. iwl_get_measurement(priv, &params, type);
  6558. return count;
  6559. }
  6560. static DEVICE_ATTR(measurement, S_IRUSR | S_IWUSR,
  6561. show_measurement, store_measurement);
  6562. #endif /* CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT */
  6563. static ssize_t show_rate(struct device *d,
  6564. struct device_attribute *attr, char *buf)
  6565. {
  6566. struct iwl_priv *priv = dev_get_drvdata(d);
  6567. unsigned long flags;
  6568. int i;
  6569. spin_lock_irqsave(&priv->sta_lock, flags);
  6570. if (priv->iw_mode == IEEE80211_IF_TYPE_STA)
  6571. i = priv->stations[IWL_AP_ID].current_rate.s.rate;
  6572. else
  6573. i = priv->stations[IWL_STA_ID].current_rate.s.rate;
  6574. spin_unlock_irqrestore(&priv->sta_lock, flags);
  6575. i = iwl_rate_index_from_plcp(i);
  6576. if (i == -1)
  6577. return sprintf(buf, "0\n");
  6578. return sprintf(buf, "%d%s\n",
  6579. (iwl_rates[i].ieee >> 1),
  6580. (iwl_rates[i].ieee & 0x1) ? ".5" : "");
  6581. }
  6582. static DEVICE_ATTR(rate, S_IRUSR, show_rate, NULL);
  6583. static ssize_t store_retry_rate(struct device *d,
  6584. struct device_attribute *attr,
  6585. const char *buf, size_t count)
  6586. {
  6587. struct iwl_priv *priv = dev_get_drvdata(d);
  6588. priv->retry_rate = simple_strtoul(buf, NULL, 0);
  6589. if (priv->retry_rate <= 0)
  6590. priv->retry_rate = 1;
  6591. return count;
  6592. }
  6593. static ssize_t show_retry_rate(struct device *d,
  6594. struct device_attribute *attr, char *buf)
  6595. {
  6596. struct iwl_priv *priv = dev_get_drvdata(d);
  6597. return sprintf(buf, "%d", priv->retry_rate);
  6598. }
  6599. static DEVICE_ATTR(retry_rate, S_IWUSR | S_IRUSR, show_retry_rate,
  6600. store_retry_rate);
  6601. static ssize_t store_power_level(struct device *d,
  6602. struct device_attribute *attr,
  6603. const char *buf, size_t count)
  6604. {
  6605. struct iwl_priv *priv = dev_get_drvdata(d);
  6606. int rc;
  6607. int mode;
  6608. mode = simple_strtoul(buf, NULL, 0);
  6609. mutex_lock(&priv->mutex);
  6610. if (!iwl_is_ready(priv)) {
  6611. rc = -EAGAIN;
  6612. goto out;
  6613. }
  6614. if ((mode < 1) || (mode > IWL_POWER_LIMIT) || (mode == IWL_POWER_AC))
  6615. mode = IWL_POWER_AC;
  6616. else
  6617. mode |= IWL_POWER_ENABLED;
  6618. if (mode != priv->power_mode) {
  6619. rc = iwl_send_power_mode(priv, IWL_POWER_LEVEL(mode));
  6620. if (rc) {
  6621. IWL_DEBUG_MAC80211("failed setting power mode.\n");
  6622. goto out;
  6623. }
  6624. priv->power_mode = mode;
  6625. }
  6626. rc = count;
  6627. out:
  6628. mutex_unlock(&priv->mutex);
  6629. return rc;
  6630. }
  6631. #define MAX_WX_STRING 80
  6632. /* Values are in microsecond */
  6633. static const s32 timeout_duration[] = {
  6634. 350000,
  6635. 250000,
  6636. 75000,
  6637. 37000,
  6638. 25000,
  6639. };
  6640. static const s32 period_duration[] = {
  6641. 400000,
  6642. 700000,
  6643. 1000000,
  6644. 1000000,
  6645. 1000000
  6646. };
  6647. static ssize_t show_power_level(struct device *d,
  6648. struct device_attribute *attr, char *buf)
  6649. {
  6650. struct iwl_priv *priv = dev_get_drvdata(d);
  6651. int level = IWL_POWER_LEVEL(priv->power_mode);
  6652. char *p = buf;
  6653. p += sprintf(p, "%d ", level);
  6654. switch (level) {
  6655. case IWL_POWER_MODE_CAM:
  6656. case IWL_POWER_AC:
  6657. p += sprintf(p, "(AC)");
  6658. break;
  6659. case IWL_POWER_BATTERY:
  6660. p += sprintf(p, "(BATTERY)");
  6661. break;
  6662. default:
  6663. p += sprintf(p,
  6664. "(Timeout %dms, Period %dms)",
  6665. timeout_duration[level - 1] / 1000,
  6666. period_duration[level - 1] / 1000);
  6667. }
  6668. if (!(priv->power_mode & IWL_POWER_ENABLED))
  6669. p += sprintf(p, " OFF\n");
  6670. else
  6671. p += sprintf(p, " \n");
  6672. return (p - buf + 1);
  6673. }
  6674. static DEVICE_ATTR(power_level, S_IWUSR | S_IRUSR, show_power_level,
  6675. store_power_level);
  6676. static ssize_t show_channels(struct device *d,
  6677. struct device_attribute *attr, char *buf)
  6678. {
  6679. struct iwl_priv *priv = dev_get_drvdata(d);
  6680. int len = 0, i;
  6681. struct ieee80211_channel *channels = NULL;
  6682. const struct ieee80211_hw_mode *hw_mode = NULL;
  6683. int count = 0;
  6684. if (!iwl_is_ready(priv))
  6685. return -EAGAIN;
  6686. hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211G);
  6687. if (!hw_mode)
  6688. hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211B);
  6689. if (hw_mode) {
  6690. channels = hw_mode->channels;
  6691. count = hw_mode->num_channels;
  6692. }
  6693. len +=
  6694. sprintf(&buf[len],
  6695. "Displaying %d channels in 2.4GHz band "
  6696. "(802.11bg):\n", count);
  6697. for (i = 0; i < count; i++)
  6698. len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
  6699. channels[i].chan,
  6700. channels[i].power_level,
  6701. channels[i].
  6702. flag & IEEE80211_CHAN_W_RADAR_DETECT ?
  6703. " (IEEE 802.11h required)" : "",
  6704. (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
  6705. || (channels[i].
  6706. flag &
  6707. IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
  6708. ", IBSS",
  6709. channels[i].
  6710. flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
  6711. "active/passive" : "passive only");
  6712. hw_mode = iwl_get_hw_mode(priv, MODE_IEEE80211A);
  6713. if (hw_mode) {
  6714. channels = hw_mode->channels;
  6715. count = hw_mode->num_channels;
  6716. } else {
  6717. channels = NULL;
  6718. count = 0;
  6719. }
  6720. len += sprintf(&buf[len], "Displaying %d channels in 5.2GHz band "
  6721. "(802.11a):\n", count);
  6722. for (i = 0; i < count; i++)
  6723. len += sprintf(&buf[len], "%d: %ddBm: BSS%s%s, %s.\n",
  6724. channels[i].chan,
  6725. channels[i].power_level,
  6726. channels[i].
  6727. flag & IEEE80211_CHAN_W_RADAR_DETECT ?
  6728. " (IEEE 802.11h required)" : "",
  6729. (!(channels[i].flag & IEEE80211_CHAN_W_IBSS)
  6730. || (channels[i].
  6731. flag &
  6732. IEEE80211_CHAN_W_RADAR_DETECT)) ? "" :
  6733. ", IBSS",
  6734. channels[i].
  6735. flag & IEEE80211_CHAN_W_ACTIVE_SCAN ?
  6736. "active/passive" : "passive only");
  6737. return len;
  6738. }
  6739. static DEVICE_ATTR(channels, S_IRUSR, show_channels, NULL);
  6740. static ssize_t show_statistics(struct device *d,
  6741. struct device_attribute *attr, char *buf)
  6742. {
  6743. struct iwl_priv *priv = dev_get_drvdata(d);
  6744. u32 size = sizeof(struct iwl_notif_statistics);
  6745. u32 len = 0, ofs = 0;
  6746. u8 *data = (u8 *) & priv->statistics;
  6747. int rc = 0;
  6748. if (!iwl_is_alive(priv))
  6749. return -EAGAIN;
  6750. mutex_lock(&priv->mutex);
  6751. rc = iwl_send_statistics_request(priv);
  6752. mutex_unlock(&priv->mutex);
  6753. if (rc) {
  6754. len = sprintf(buf,
  6755. "Error sending statistics request: 0x%08X\n", rc);
  6756. return len;
  6757. }
  6758. while (size && (PAGE_SIZE - len)) {
  6759. hex_dump_to_buffer(data + ofs, size, 16, 1, buf + len,
  6760. PAGE_SIZE - len, 1);
  6761. len = strlen(buf);
  6762. if (PAGE_SIZE - len)
  6763. buf[len++] = '\n';
  6764. ofs += 16;
  6765. size -= min(size, 16U);
  6766. }
  6767. return len;
  6768. }
  6769. static DEVICE_ATTR(statistics, S_IRUGO, show_statistics, NULL);
  6770. static ssize_t show_antenna(struct device *d,
  6771. struct device_attribute *attr, char *buf)
  6772. {
  6773. struct iwl_priv *priv = dev_get_drvdata(d);
  6774. if (!iwl_is_alive(priv))
  6775. return -EAGAIN;
  6776. return sprintf(buf, "%d\n", priv->antenna);
  6777. }
  6778. static ssize_t store_antenna(struct device *d,
  6779. struct device_attribute *attr,
  6780. const char *buf, size_t count)
  6781. {
  6782. int ant;
  6783. struct iwl_priv *priv = dev_get_drvdata(d);
  6784. if (count == 0)
  6785. return 0;
  6786. if (sscanf(buf, "%1i", &ant) != 1) {
  6787. IWL_DEBUG_INFO("not in hex or decimal form.\n");
  6788. return count;
  6789. }
  6790. if ((ant >= 0) && (ant <= 2)) {
  6791. IWL_DEBUG_INFO("Setting antenna select to %d.\n", ant);
  6792. priv->antenna = (enum iwl_antenna)ant;
  6793. } else
  6794. IWL_DEBUG_INFO("Bad antenna select value %d.\n", ant);
  6795. return count;
  6796. }
  6797. static DEVICE_ATTR(antenna, S_IWUSR | S_IRUGO, show_antenna, store_antenna);
  6798. static ssize_t show_status(struct device *d,
  6799. struct device_attribute *attr, char *buf)
  6800. {
  6801. struct iwl_priv *priv = (struct iwl_priv *)d->driver_data;
  6802. if (!iwl_is_alive(priv))
  6803. return -EAGAIN;
  6804. return sprintf(buf, "0x%08x\n", (int)priv->status);
  6805. }
  6806. static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
  6807. static ssize_t dump_error_log(struct device *d,
  6808. struct device_attribute *attr,
  6809. const char *buf, size_t count)
  6810. {
  6811. char *p = (char *)buf;
  6812. if (p[0] == '1')
  6813. iwl_dump_nic_error_log((struct iwl_priv *)d->driver_data);
  6814. return strnlen(buf, count);
  6815. }
  6816. static DEVICE_ATTR(dump_errors, S_IWUSR, NULL, dump_error_log);
  6817. static ssize_t dump_event_log(struct device *d,
  6818. struct device_attribute *attr,
  6819. const char *buf, size_t count)
  6820. {
  6821. char *p = (char *)buf;
  6822. if (p[0] == '1')
  6823. iwl_dump_nic_event_log((struct iwl_priv *)d->driver_data);
  6824. return strnlen(buf, count);
  6825. }
  6826. static DEVICE_ATTR(dump_events, S_IWUSR, NULL, dump_event_log);
  6827. /*****************************************************************************
  6828. *
  6829. * driver setup and teardown
  6830. *
  6831. *****************************************************************************/
  6832. static void iwl_setup_deferred_work(struct iwl_priv *priv)
  6833. {
  6834. priv->workqueue = create_workqueue(DRV_NAME);
  6835. init_waitqueue_head(&priv->wait_command_queue);
  6836. INIT_WORK(&priv->up, iwl_bg_up);
  6837. INIT_WORK(&priv->restart, iwl_bg_restart);
  6838. INIT_WORK(&priv->rx_replenish, iwl_bg_rx_replenish);
  6839. INIT_WORK(&priv->scan_completed, iwl_bg_scan_completed);
  6840. INIT_WORK(&priv->request_scan, iwl_bg_request_scan);
  6841. INIT_WORK(&priv->abort_scan, iwl_bg_abort_scan);
  6842. INIT_WORK(&priv->rf_kill, iwl_bg_rf_kill);
  6843. INIT_WORK(&priv->beacon_update, iwl_bg_beacon_update);
  6844. INIT_DELAYED_WORK(&priv->post_associate, iwl_bg_post_associate);
  6845. INIT_DELAYED_WORK(&priv->init_alive_start, iwl_bg_init_alive_start);
  6846. INIT_DELAYED_WORK(&priv->alive_start, iwl_bg_alive_start);
  6847. INIT_DELAYED_WORK(&priv->scan_check, iwl_bg_scan_check);
  6848. iwl_hw_setup_deferred_work(priv);
  6849. tasklet_init(&priv->irq_tasklet, (void (*)(unsigned long))
  6850. iwl_irq_tasklet, (unsigned long)priv);
  6851. }
  6852. static void iwl_cancel_deferred_work(struct iwl_priv *priv)
  6853. {
  6854. iwl_hw_cancel_deferred_work(priv);
  6855. cancel_delayed_work_sync(&priv->init_alive_start);
  6856. cancel_delayed_work(&priv->scan_check);
  6857. cancel_delayed_work(&priv->alive_start);
  6858. cancel_delayed_work(&priv->post_associate);
  6859. cancel_work_sync(&priv->beacon_update);
  6860. }
  6861. static struct attribute *iwl_sysfs_entries[] = {
  6862. &dev_attr_antenna.attr,
  6863. &dev_attr_channels.attr,
  6864. &dev_attr_dump_errors.attr,
  6865. &dev_attr_dump_events.attr,
  6866. &dev_attr_flags.attr,
  6867. &dev_attr_filter_flags.attr,
  6868. #ifdef CONFIG_IWLWIFI_SPECTRUM_MEASUREMENT
  6869. &dev_attr_measurement.attr,
  6870. #endif
  6871. &dev_attr_power_level.attr,
  6872. &dev_attr_rate.attr,
  6873. &dev_attr_retry_rate.attr,
  6874. &dev_attr_rf_kill.attr,
  6875. &dev_attr_rs_window.attr,
  6876. &dev_attr_statistics.attr,
  6877. &dev_attr_status.attr,
  6878. &dev_attr_temperature.attr,
  6879. &dev_attr_tune.attr,
  6880. &dev_attr_tx_power.attr,
  6881. NULL
  6882. };
  6883. static struct attribute_group iwl_attribute_group = {
  6884. .name = NULL, /* put in device directory */
  6885. .attrs = iwl_sysfs_entries,
  6886. };
  6887. static struct ieee80211_ops iwl_hw_ops = {
  6888. .tx = iwl_mac_tx,
  6889. .start = iwl_mac_start,
  6890. .stop = iwl_mac_stop,
  6891. .add_interface = iwl_mac_add_interface,
  6892. .remove_interface = iwl_mac_remove_interface,
  6893. .config = iwl_mac_config,
  6894. .config_interface = iwl_mac_config_interface,
  6895. .configure_filter = iwl_configure_filter,
  6896. .set_key = iwl_mac_set_key,
  6897. .get_stats = iwl_mac_get_stats,
  6898. .get_tx_stats = iwl_mac_get_tx_stats,
  6899. .conf_tx = iwl_mac_conf_tx,
  6900. .get_tsf = iwl_mac_get_tsf,
  6901. .reset_tsf = iwl_mac_reset_tsf,
  6902. .beacon_update = iwl_mac_beacon_update,
  6903. .hw_scan = iwl_mac_hw_scan
  6904. };
  6905. static int iwl_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  6906. {
  6907. int err = 0;
  6908. u32 pci_id;
  6909. struct iwl_priv *priv;
  6910. struct ieee80211_hw *hw;
  6911. int i;
  6912. if (iwl_param_disable_hw_scan) {
  6913. IWL_DEBUG_INFO("Disabling hw_scan\n");
  6914. iwl_hw_ops.hw_scan = NULL;
  6915. }
  6916. if ((iwl_param_queues_num > IWL_MAX_NUM_QUEUES) ||
  6917. (iwl_param_queues_num < IWL_MIN_NUM_QUEUES)) {
  6918. IWL_ERROR("invalid queues_num, should be between %d and %d\n",
  6919. IWL_MIN_NUM_QUEUES, IWL_MAX_NUM_QUEUES);
  6920. err = -EINVAL;
  6921. goto out;
  6922. }
  6923. /* mac80211 allocates memory for this device instance, including
  6924. * space for this driver's private structure */
  6925. hw = ieee80211_alloc_hw(sizeof(struct iwl_priv), &iwl_hw_ops);
  6926. if (hw == NULL) {
  6927. IWL_ERROR("Can not allocate network device\n");
  6928. err = -ENOMEM;
  6929. goto out;
  6930. }
  6931. SET_IEEE80211_DEV(hw, &pdev->dev);
  6932. hw->rate_control_algorithm = "iwl-3945-rs";
  6933. IWL_DEBUG_INFO("*** LOAD DRIVER ***\n");
  6934. priv = hw->priv;
  6935. priv->hw = hw;
  6936. priv->pci_dev = pdev;
  6937. priv->antenna = (enum iwl_antenna)iwl_param_antenna;
  6938. #ifdef CONFIG_IWLWIFI_DEBUG
  6939. iwl_debug_level = iwl_param_debug;
  6940. atomic_set(&priv->restrict_refcnt, 0);
  6941. #endif
  6942. priv->retry_rate = 1;
  6943. priv->ibss_beacon = NULL;
  6944. /* Tell mac80211 and its clients (e.g. Wireless Extensions)
  6945. * the range of signal quality values that we'll provide.
  6946. * Negative values for level/noise indicate that we'll provide dBm.
  6947. * For WE, at least, non-0 values here *enable* display of values
  6948. * in app (iwconfig). */
  6949. hw->max_rssi = -20; /* signal level, negative indicates dBm */
  6950. hw->max_noise = -20; /* noise level, negative indicates dBm */
  6951. hw->max_signal = 100; /* link quality indication (%) */
  6952. /* Tell mac80211 our Tx characteristics */
  6953. hw->flags = IEEE80211_HW_HOST_GEN_BEACON_TEMPLATE;
  6954. hw->queues = 4;
  6955. spin_lock_init(&priv->lock);
  6956. spin_lock_init(&priv->power_data.lock);
  6957. spin_lock_init(&priv->sta_lock);
  6958. spin_lock_init(&priv->hcmd_lock);
  6959. for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++)
  6960. INIT_LIST_HEAD(&priv->ibss_mac_hash[i]);
  6961. INIT_LIST_HEAD(&priv->free_frames);
  6962. mutex_init(&priv->mutex);
  6963. if (pci_enable_device(pdev)) {
  6964. err = -ENODEV;
  6965. goto out_ieee80211_free_hw;
  6966. }
  6967. pci_set_master(pdev);
  6968. iwl_clear_stations_table(priv);
  6969. priv->data_retry_limit = -1;
  6970. priv->ieee_channels = NULL;
  6971. priv->ieee_rates = NULL;
  6972. priv->phymode = -1;
  6973. err = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  6974. if (!err)
  6975. err = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  6976. if (err) {
  6977. printk(KERN_WARNING DRV_NAME ": No suitable DMA available.\n");
  6978. goto out_pci_disable_device;
  6979. }
  6980. pci_set_drvdata(pdev, priv);
  6981. err = pci_request_regions(pdev, DRV_NAME);
  6982. if (err)
  6983. goto out_pci_disable_device;
  6984. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  6985. * PCI Tx retries from interfering with C3 CPU state */
  6986. pci_write_config_byte(pdev, 0x41, 0x00);
  6987. priv->hw_base = pci_iomap(pdev, 0, 0);
  6988. if (!priv->hw_base) {
  6989. err = -ENODEV;
  6990. goto out_pci_release_regions;
  6991. }
  6992. IWL_DEBUG_INFO("pci_resource_len = 0x%08llx\n",
  6993. (unsigned long long) pci_resource_len(pdev, 0));
  6994. IWL_DEBUG_INFO("pci_resource_base = %p\n", priv->hw_base);
  6995. /* Initialize module parameter values here */
  6996. if (iwl_param_disable) {
  6997. set_bit(STATUS_RF_KILL_SW, &priv->status);
  6998. IWL_DEBUG_INFO("Radio disabled.\n");
  6999. }
  7000. priv->iw_mode = IEEE80211_IF_TYPE_STA;
  7001. pci_id =
  7002. (priv->pci_dev->device << 16) | priv->pci_dev->subsystem_device;
  7003. switch (pci_id) {
  7004. case 0x42221005: /* 0x4222 0x8086 0x1005 is BG SKU */
  7005. case 0x42221034: /* 0x4222 0x8086 0x1034 is BG SKU */
  7006. case 0x42271014: /* 0x4227 0x8086 0x1014 is BG SKU */
  7007. case 0x42221044: /* 0x4222 0x8086 0x1044 is BG SKU */
  7008. priv->is_abg = 0;
  7009. break;
  7010. /*
  7011. * Rest are assumed ABG SKU -- if this is not the
  7012. * case then the card will get the wrong 'Detected'
  7013. * line in the kernel log however the code that
  7014. * initializes the GEO table will detect no A-band
  7015. * channels and remove the is_abg mask.
  7016. */
  7017. default:
  7018. priv->is_abg = 1;
  7019. break;
  7020. }
  7021. printk(KERN_INFO DRV_NAME
  7022. ": Detected Intel PRO/Wireless 3945%sBG Network Connection\n",
  7023. priv->is_abg ? "A" : "");
  7024. /* Device-specific setup */
  7025. if (iwl_hw_set_hw_setting(priv)) {
  7026. IWL_ERROR("failed to set hw settings\n");
  7027. mutex_unlock(&priv->mutex);
  7028. goto out_iounmap;
  7029. }
  7030. #ifdef CONFIG_IWLWIFI_QOS
  7031. if (iwl_param_qos_enable)
  7032. priv->qos_data.qos_enable = 1;
  7033. iwl_reset_qos(priv);
  7034. priv->qos_data.qos_active = 0;
  7035. priv->qos_data.qos_cap.val = 0;
  7036. #endif /* CONFIG_IWLWIFI_QOS */
  7037. iwl_set_rxon_channel(priv, MODE_IEEE80211G, 6);
  7038. iwl_setup_deferred_work(priv);
  7039. iwl_setup_rx_handlers(priv);
  7040. priv->rates_mask = IWL_RATES_MASK;
  7041. /* If power management is turned on, default to AC mode */
  7042. priv->power_mode = IWL_POWER_AC;
  7043. priv->user_txpower_limit = IWL_DEFAULT_TX_POWER;
  7044. pci_enable_msi(pdev);
  7045. err = request_irq(pdev->irq, iwl_isr, IRQF_SHARED, DRV_NAME, priv);
  7046. if (err) {
  7047. IWL_ERROR("Error allocating IRQ %d\n", pdev->irq);
  7048. goto out_disable_msi;
  7049. }
  7050. mutex_lock(&priv->mutex);
  7051. err = sysfs_create_group(&pdev->dev.kobj, &iwl_attribute_group);
  7052. if (err) {
  7053. IWL_ERROR("failed to create sysfs device attributes\n");
  7054. mutex_unlock(&priv->mutex);
  7055. goto out_release_irq;
  7056. }
  7057. /* fetch ucode file from disk, alloc and copy to bus-master buffers ...
  7058. * ucode filename and max sizes are card-specific. */
  7059. err = iwl_read_ucode(priv);
  7060. if (err) {
  7061. IWL_ERROR("Could not read microcode: %d\n", err);
  7062. mutex_unlock(&priv->mutex);
  7063. goto out_pci_alloc;
  7064. }
  7065. mutex_unlock(&priv->mutex);
  7066. IWL_DEBUG_INFO("Queueing UP work.\n");
  7067. queue_work(priv->workqueue, &priv->up);
  7068. return 0;
  7069. out_pci_alloc:
  7070. iwl_dealloc_ucode_pci(priv);
  7071. sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
  7072. out_release_irq:
  7073. free_irq(pdev->irq, priv);
  7074. out_disable_msi:
  7075. pci_disable_msi(pdev);
  7076. destroy_workqueue(priv->workqueue);
  7077. priv->workqueue = NULL;
  7078. iwl_unset_hw_setting(priv);
  7079. out_iounmap:
  7080. pci_iounmap(pdev, priv->hw_base);
  7081. out_pci_release_regions:
  7082. pci_release_regions(pdev);
  7083. out_pci_disable_device:
  7084. pci_disable_device(pdev);
  7085. pci_set_drvdata(pdev, NULL);
  7086. out_ieee80211_free_hw:
  7087. ieee80211_free_hw(priv->hw);
  7088. out:
  7089. return err;
  7090. }
  7091. static void iwl_pci_remove(struct pci_dev *pdev)
  7092. {
  7093. struct iwl_priv *priv = pci_get_drvdata(pdev);
  7094. struct list_head *p, *q;
  7095. int i;
  7096. if (!priv)
  7097. return;
  7098. IWL_DEBUG_INFO("*** UNLOAD DRIVER ***\n");
  7099. set_bit(STATUS_EXIT_PENDING, &priv->status);
  7100. iwl_down(priv);
  7101. /* Free MAC hash list for ADHOC */
  7102. for (i = 0; i < IWL_IBSS_MAC_HASH_SIZE; i++) {
  7103. list_for_each_safe(p, q, &priv->ibss_mac_hash[i]) {
  7104. list_del(p);
  7105. kfree(list_entry(p, struct iwl_ibss_seq, list));
  7106. }
  7107. }
  7108. sysfs_remove_group(&pdev->dev.kobj, &iwl_attribute_group);
  7109. iwl_dealloc_ucode_pci(priv);
  7110. if (priv->rxq.bd)
  7111. iwl_rx_queue_free(priv, &priv->rxq);
  7112. iwl_hw_txq_ctx_free(priv);
  7113. iwl_unset_hw_setting(priv);
  7114. iwl_clear_stations_table(priv);
  7115. if (priv->mac80211_registered) {
  7116. ieee80211_unregister_hw(priv->hw);
  7117. iwl_rate_control_unregister(priv->hw);
  7118. }
  7119. /*netif_stop_queue(dev); */
  7120. flush_workqueue(priv->workqueue);
  7121. /* ieee80211_unregister_hw calls iwl_mac_stop, which flushes
  7122. * priv->workqueue... so we can't take down the workqueue
  7123. * until now... */
  7124. destroy_workqueue(priv->workqueue);
  7125. priv->workqueue = NULL;
  7126. free_irq(pdev->irq, priv);
  7127. pci_disable_msi(pdev);
  7128. pci_iounmap(pdev, priv->hw_base);
  7129. pci_release_regions(pdev);
  7130. pci_disable_device(pdev);
  7131. pci_set_drvdata(pdev, NULL);
  7132. kfree(priv->channel_info);
  7133. kfree(priv->ieee_channels);
  7134. kfree(priv->ieee_rates);
  7135. if (priv->ibss_beacon)
  7136. dev_kfree_skb(priv->ibss_beacon);
  7137. ieee80211_free_hw(priv->hw);
  7138. }
  7139. #ifdef CONFIG_PM
  7140. static int iwl_pci_suspend(struct pci_dev *pdev, pm_message_t state)
  7141. {
  7142. struct iwl_priv *priv = pci_get_drvdata(pdev);
  7143. set_bit(STATUS_IN_SUSPEND, &priv->status);
  7144. /* Take down the device; powers it off, etc. */
  7145. iwl_down(priv);
  7146. if (priv->mac80211_registered)
  7147. ieee80211_stop_queues(priv->hw);
  7148. pci_save_state(pdev);
  7149. pci_disable_device(pdev);
  7150. pci_set_power_state(pdev, PCI_D3hot);
  7151. return 0;
  7152. }
  7153. static void iwl_resume(struct iwl_priv *priv)
  7154. {
  7155. unsigned long flags;
  7156. /* The following it a temporary work around due to the
  7157. * suspend / resume not fully initializing the NIC correctly.
  7158. * Without all of the following, resume will not attempt to take
  7159. * down the NIC (it shouldn't really need to) and will just try
  7160. * and bring the NIC back up. However that fails during the
  7161. * ucode verification process. This then causes iwl_down to be
  7162. * called *after* iwl_hw_nic_init() has succeeded -- which
  7163. * then lets the next init sequence succeed. So, we've
  7164. * replicated all of that NIC init code here... */
  7165. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  7166. iwl_hw_nic_init(priv);
  7167. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  7168. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR,
  7169. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  7170. iwl_write32(priv, CSR_INT, 0xFFFFFFFF);
  7171. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  7172. iwl_write32(priv, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  7173. /* tell the device to stop sending interrupts */
  7174. iwl_disable_interrupts(priv);
  7175. spin_lock_irqsave(&priv->lock, flags);
  7176. iwl_clear_bit(priv, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  7177. if (!iwl_grab_restricted_access(priv)) {
  7178. iwl_write_restricted_reg(priv, APMG_CLK_DIS_REG,
  7179. APMG_CLK_VAL_DMA_CLK_RQT);
  7180. iwl_release_restricted_access(priv);
  7181. }
  7182. spin_unlock_irqrestore(&priv->lock, flags);
  7183. udelay(5);
  7184. iwl_hw_nic_reset(priv);
  7185. /* Bring the device back up */
  7186. clear_bit(STATUS_IN_SUSPEND, &priv->status);
  7187. queue_work(priv->workqueue, &priv->up);
  7188. }
  7189. static int iwl_pci_resume(struct pci_dev *pdev)
  7190. {
  7191. struct iwl_priv *priv = pci_get_drvdata(pdev);
  7192. int err;
  7193. printk(KERN_INFO "Coming out of suspend...\n");
  7194. pci_set_power_state(pdev, PCI_D0);
  7195. err = pci_enable_device(pdev);
  7196. pci_restore_state(pdev);
  7197. /*
  7198. * Suspend/Resume resets the PCI configuration space, so we have to
  7199. * re-disable the RETRY_TIMEOUT register (0x41) to keep PCI Tx retries
  7200. * from interfering with C3 CPU state. pci_restore_state won't help
  7201. * here since it only restores the first 64 bytes pci config header.
  7202. */
  7203. pci_write_config_byte(pdev, 0x41, 0x00);
  7204. iwl_resume(priv);
  7205. return 0;
  7206. }
  7207. #endif /* CONFIG_PM */
  7208. /*****************************************************************************
  7209. *
  7210. * driver and module entry point
  7211. *
  7212. *****************************************************************************/
  7213. static struct pci_driver iwl_driver = {
  7214. .name = DRV_NAME,
  7215. .id_table = iwl_hw_card_ids,
  7216. .probe = iwl_pci_probe,
  7217. .remove = __devexit_p(iwl_pci_remove),
  7218. #ifdef CONFIG_PM
  7219. .suspend = iwl_pci_suspend,
  7220. .resume = iwl_pci_resume,
  7221. #endif
  7222. };
  7223. static int __init iwl_init(void)
  7224. {
  7225. int ret;
  7226. printk(KERN_INFO DRV_NAME ": " DRV_DESCRIPTION ", " DRV_VERSION "\n");
  7227. printk(KERN_INFO DRV_NAME ": " DRV_COPYRIGHT "\n");
  7228. ret = pci_register_driver(&iwl_driver);
  7229. if (ret) {
  7230. IWL_ERROR("Unable to initialize PCI module\n");
  7231. return ret;
  7232. }
  7233. #ifdef CONFIG_IWLWIFI_DEBUG
  7234. ret = driver_create_file(&iwl_driver.driver, &driver_attr_debug_level);
  7235. if (ret) {
  7236. IWL_ERROR("Unable to create driver sysfs file\n");
  7237. pci_unregister_driver(&iwl_driver);
  7238. return ret;
  7239. }
  7240. #endif
  7241. return ret;
  7242. }
  7243. static void __exit iwl_exit(void)
  7244. {
  7245. #ifdef CONFIG_IWLWIFI_DEBUG
  7246. driver_remove_file(&iwl_driver.driver, &driver_attr_debug_level);
  7247. #endif
  7248. pci_unregister_driver(&iwl_driver);
  7249. }
  7250. module_param_named(antenna, iwl_param_antenna, int, 0444);
  7251. MODULE_PARM_DESC(antenna, "select antenna (1=Main, 2=Aux, default 0 [both])");
  7252. module_param_named(disable, iwl_param_disable, int, 0444);
  7253. MODULE_PARM_DESC(disable, "manually disable the radio (default 0 [radio on])");
  7254. module_param_named(hwcrypto, iwl_param_hwcrypto, int, 0444);
  7255. MODULE_PARM_DESC(hwcrypto,
  7256. "using hardware crypto engine (default 0 [software])\n");
  7257. module_param_named(debug, iwl_param_debug, int, 0444);
  7258. MODULE_PARM_DESC(debug, "debug output mask");
  7259. module_param_named(disable_hw_scan, iwl_param_disable_hw_scan, int, 0444);
  7260. MODULE_PARM_DESC(disable_hw_scan, "disable hardware scanning (default 0)");
  7261. module_param_named(queues_num, iwl_param_queues_num, int, 0444);
  7262. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  7263. /* QoS */
  7264. module_param_named(qos_enable, iwl_param_qos_enable, int, 0444);
  7265. MODULE_PARM_DESC(qos_enable, "enable all QoS functionality");
  7266. module_exit(iwl_exit);
  7267. module_init(iwl_init);