iwl4965-base.c 207 KB

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