4965-mac.c 185 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * Intel Linux Wireless <ilw@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/pci.h>
  34. #include <linux/pci-aspm.h>
  35. #include <linux/slab.h>
  36. #include <linux/dma-mapping.h>
  37. #include <linux/delay.h>
  38. #include <linux/sched.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/netdevice.h>
  41. #include <linux/firmware.h>
  42. #include <linux/etherdevice.h>
  43. #include <linux/if_arp.h>
  44. #include <net/mac80211.h>
  45. #include <asm/div64.h>
  46. #define DRV_NAME "iwl4965"
  47. #include "common.h"
  48. #include "4965.h"
  49. /******************************************************************************
  50. *
  51. * module boiler plate
  52. *
  53. ******************************************************************************/
  54. /*
  55. * module name, copyright, version, etc.
  56. */
  57. #define DRV_DESCRIPTION "Intel(R) Wireless WiFi 4965 driver for Linux"
  58. #ifdef CONFIG_IWLEGACY_DEBUG
  59. #define VD "d"
  60. #else
  61. #define VD
  62. #endif
  63. #define DRV_VERSION IWLWIFI_VERSION VD
  64. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  65. MODULE_VERSION(DRV_VERSION);
  66. MODULE_AUTHOR(DRV_COPYRIGHT " " DRV_AUTHOR);
  67. MODULE_LICENSE("GPL");
  68. MODULE_ALIAS("iwl4965");
  69. void
  70. il4965_check_abort_status(struct il_priv *il, u8 frame_count, u32 status)
  71. {
  72. if (frame_count == 1 && status == TX_STATUS_FAIL_RFKILL_FLUSH) {
  73. IL_ERR("Tx flush command to flush out all frames\n");
  74. if (!test_bit(S_EXIT_PENDING, &il->status))
  75. queue_work(il->workqueue, &il->tx_flush);
  76. }
  77. }
  78. /*
  79. * EEPROM
  80. */
  81. struct il_mod_params il4965_mod_params = {
  82. .amsdu_size_8K = 1,
  83. .restart_fw = 1,
  84. /* the rest are 0 by default */
  85. };
  86. void
  87. il4965_rx_queue_reset(struct il_priv *il, struct il_rx_queue *rxq)
  88. {
  89. unsigned long flags;
  90. int i;
  91. spin_lock_irqsave(&rxq->lock, flags);
  92. INIT_LIST_HEAD(&rxq->rx_free);
  93. INIT_LIST_HEAD(&rxq->rx_used);
  94. /* Fill the rx_used queue with _all_ of the Rx buffers */
  95. for (i = 0; i < RX_FREE_BUFFERS + RX_QUEUE_SIZE; i++) {
  96. /* In the reset function, these buffers may have been allocated
  97. * to an SKB, so we need to unmap and free potential storage */
  98. if (rxq->pool[i].page != NULL) {
  99. pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
  100. PAGE_SIZE << il->hw_params.rx_page_order,
  101. PCI_DMA_FROMDEVICE);
  102. __il_free_pages(il, rxq->pool[i].page);
  103. rxq->pool[i].page = NULL;
  104. }
  105. list_add_tail(&rxq->pool[i].list, &rxq->rx_used);
  106. }
  107. for (i = 0; i < RX_QUEUE_SIZE; i++)
  108. rxq->queue[i] = NULL;
  109. /* Set us so that we have processed and used all buffers, but have
  110. * not restocked the Rx queue with fresh buffers */
  111. rxq->read = rxq->write = 0;
  112. rxq->write_actual = 0;
  113. rxq->free_count = 0;
  114. spin_unlock_irqrestore(&rxq->lock, flags);
  115. }
  116. int
  117. il4965_rx_init(struct il_priv *il, struct il_rx_queue *rxq)
  118. {
  119. u32 rb_size;
  120. const u32 rfdnlog = RX_QUEUE_SIZE_LOG; /* 256 RBDs */
  121. u32 rb_timeout = 0;
  122. if (il->cfg->mod_params->amsdu_size_8K)
  123. rb_size = FH49_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_8K;
  124. else
  125. rb_size = FH49_RCSR_RX_CONFIG_REG_VAL_RB_SIZE_4K;
  126. /* Stop Rx DMA */
  127. il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  128. /* Reset driver's Rx queue write idx */
  129. il_wr(il, FH49_RSCSR_CHNL0_RBDCB_WPTR_REG, 0);
  130. /* Tell device where to find RBD circular buffer in DRAM */
  131. il_wr(il, FH49_RSCSR_CHNL0_RBDCB_BASE_REG, (u32) (rxq->bd_dma >> 8));
  132. /* Tell device where in DRAM to update its Rx status */
  133. il_wr(il, FH49_RSCSR_CHNL0_STTS_WPTR_REG, rxq->rb_stts_dma >> 4);
  134. /* Enable Rx DMA
  135. * Direct rx interrupts to hosts
  136. * Rx buffer size 4 or 8k
  137. * RB timeout 0x10
  138. * 256 RBDs
  139. */
  140. il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG,
  141. FH49_RCSR_RX_CONFIG_CHNL_EN_ENABLE_VAL |
  142. FH49_RCSR_CHNL0_RX_CONFIG_IRQ_DEST_INT_HOST_VAL |
  143. FH49_RCSR_CHNL0_RX_CONFIG_SINGLE_FRAME_MSK |
  144. rb_size |
  145. (rb_timeout << FH49_RCSR_RX_CONFIG_REG_IRQ_RBTH_POS) |
  146. (rfdnlog << FH49_RCSR_RX_CONFIG_RBDCB_SIZE_POS));
  147. /* Set interrupt coalescing timer to default (2048 usecs) */
  148. il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_TIMEOUT_DEF);
  149. return 0;
  150. }
  151. static void
  152. il4965_set_pwr_vmain(struct il_priv *il)
  153. {
  154. /*
  155. * (for documentation purposes)
  156. * to set power to V_AUX, do:
  157. if (pci_pme_capable(il->pci_dev, PCI_D3cold))
  158. il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
  159. APMG_PS_CTRL_VAL_PWR_SRC_VAUX,
  160. ~APMG_PS_CTRL_MSK_PWR_SRC);
  161. */
  162. il_set_bits_mask_prph(il, APMG_PS_CTRL_REG,
  163. APMG_PS_CTRL_VAL_PWR_SRC_VMAIN,
  164. ~APMG_PS_CTRL_MSK_PWR_SRC);
  165. }
  166. int
  167. il4965_hw_nic_init(struct il_priv *il)
  168. {
  169. unsigned long flags;
  170. struct il_rx_queue *rxq = &il->rxq;
  171. int ret;
  172. spin_lock_irqsave(&il->lock, flags);
  173. il_apm_init(il);
  174. /* Set interrupt coalescing calibration timer to default (512 usecs) */
  175. il_write8(il, CSR_INT_COALESCING, IL_HOST_INT_CALIB_TIMEOUT_DEF);
  176. spin_unlock_irqrestore(&il->lock, flags);
  177. il4965_set_pwr_vmain(il);
  178. il4965_nic_config(il);
  179. /* Allocate the RX queue, or reset if it is already allocated */
  180. if (!rxq->bd) {
  181. ret = il_rx_queue_alloc(il);
  182. if (ret) {
  183. IL_ERR("Unable to initialize Rx queue\n");
  184. return -ENOMEM;
  185. }
  186. } else
  187. il4965_rx_queue_reset(il, rxq);
  188. il4965_rx_replenish(il);
  189. il4965_rx_init(il, rxq);
  190. spin_lock_irqsave(&il->lock, flags);
  191. rxq->need_update = 1;
  192. il_rx_queue_update_write_ptr(il, rxq);
  193. spin_unlock_irqrestore(&il->lock, flags);
  194. /* Allocate or reset and init all Tx and Command queues */
  195. if (!il->txq) {
  196. ret = il4965_txq_ctx_alloc(il);
  197. if (ret)
  198. return ret;
  199. } else
  200. il4965_txq_ctx_reset(il);
  201. set_bit(S_INIT, &il->status);
  202. return 0;
  203. }
  204. /**
  205. * il4965_dma_addr2rbd_ptr - convert a DMA address to a uCode read buffer ptr
  206. */
  207. static inline __le32
  208. il4965_dma_addr2rbd_ptr(struct il_priv *il, dma_addr_t dma_addr)
  209. {
  210. return cpu_to_le32((u32) (dma_addr >> 8));
  211. }
  212. /**
  213. * il4965_rx_queue_restock - refill RX queue from pre-allocated pool
  214. *
  215. * If there are slots in the RX queue that need to be restocked,
  216. * and we have free pre-allocated buffers, fill the ranks as much
  217. * as we can, pulling from rx_free.
  218. *
  219. * This moves the 'write' idx forward to catch up with 'processed', and
  220. * also updates the memory address in the firmware to reference the new
  221. * target buffer.
  222. */
  223. void
  224. il4965_rx_queue_restock(struct il_priv *il)
  225. {
  226. struct il_rx_queue *rxq = &il->rxq;
  227. struct list_head *element;
  228. struct il_rx_buf *rxb;
  229. unsigned long flags;
  230. spin_lock_irqsave(&rxq->lock, flags);
  231. while (il_rx_queue_space(rxq) > 0 && rxq->free_count) {
  232. /* The overwritten rxb must be a used one */
  233. rxb = rxq->queue[rxq->write];
  234. BUG_ON(rxb && rxb->page);
  235. /* Get next free Rx buffer, remove from free list */
  236. element = rxq->rx_free.next;
  237. rxb = list_entry(element, struct il_rx_buf, list);
  238. list_del(element);
  239. /* Point to Rx buffer via next RBD in circular buffer */
  240. rxq->bd[rxq->write] =
  241. il4965_dma_addr2rbd_ptr(il, rxb->page_dma);
  242. rxq->queue[rxq->write] = rxb;
  243. rxq->write = (rxq->write + 1) & RX_QUEUE_MASK;
  244. rxq->free_count--;
  245. }
  246. spin_unlock_irqrestore(&rxq->lock, flags);
  247. /* If the pre-allocated buffer pool is dropping low, schedule to
  248. * refill it */
  249. if (rxq->free_count <= RX_LOW_WATERMARK)
  250. queue_work(il->workqueue, &il->rx_replenish);
  251. /* If we've added more space for the firmware to place data, tell it.
  252. * Increment device's write pointer in multiples of 8. */
  253. if (rxq->write_actual != (rxq->write & ~0x7)) {
  254. spin_lock_irqsave(&rxq->lock, flags);
  255. rxq->need_update = 1;
  256. spin_unlock_irqrestore(&rxq->lock, flags);
  257. il_rx_queue_update_write_ptr(il, rxq);
  258. }
  259. }
  260. /**
  261. * il4965_rx_replenish - Move all used packet from rx_used to rx_free
  262. *
  263. * When moving to rx_free an SKB is allocated for the slot.
  264. *
  265. * Also restock the Rx queue via il_rx_queue_restock.
  266. * This is called as a scheduled work item (except for during initialization)
  267. */
  268. static void
  269. il4965_rx_allocate(struct il_priv *il, gfp_t priority)
  270. {
  271. struct il_rx_queue *rxq = &il->rxq;
  272. struct list_head *element;
  273. struct il_rx_buf *rxb;
  274. struct page *page;
  275. dma_addr_t page_dma;
  276. unsigned long flags;
  277. gfp_t gfp_mask = priority;
  278. while (1) {
  279. spin_lock_irqsave(&rxq->lock, flags);
  280. if (list_empty(&rxq->rx_used)) {
  281. spin_unlock_irqrestore(&rxq->lock, flags);
  282. return;
  283. }
  284. spin_unlock_irqrestore(&rxq->lock, flags);
  285. if (rxq->free_count > RX_LOW_WATERMARK)
  286. gfp_mask |= __GFP_NOWARN;
  287. if (il->hw_params.rx_page_order > 0)
  288. gfp_mask |= __GFP_COMP;
  289. /* Alloc a new receive buffer */
  290. page = alloc_pages(gfp_mask, il->hw_params.rx_page_order);
  291. if (!page) {
  292. if (net_ratelimit())
  293. D_INFO("alloc_pages failed, " "order: %d\n",
  294. il->hw_params.rx_page_order);
  295. if (rxq->free_count <= RX_LOW_WATERMARK &&
  296. net_ratelimit())
  297. IL_ERR("Failed to alloc_pages with %s. "
  298. "Only %u free buffers remaining.\n",
  299. priority ==
  300. GFP_ATOMIC ? "GFP_ATOMIC" : "GFP_KERNEL",
  301. rxq->free_count);
  302. /* We don't reschedule replenish work here -- we will
  303. * call the restock method and if it still needs
  304. * more buffers it will schedule replenish */
  305. return;
  306. }
  307. /* Get physical address of the RB */
  308. page_dma =
  309. pci_map_page(il->pci_dev, page, 0,
  310. PAGE_SIZE << il->hw_params.rx_page_order,
  311. PCI_DMA_FROMDEVICE);
  312. if (unlikely(pci_dma_mapping_error(il->pci_dev, page_dma))) {
  313. __free_pages(page, il->hw_params.rx_page_order);
  314. break;
  315. }
  316. spin_lock_irqsave(&rxq->lock, flags);
  317. if (list_empty(&rxq->rx_used)) {
  318. spin_unlock_irqrestore(&rxq->lock, flags);
  319. pci_unmap_page(il->pci_dev, page_dma,
  320. PAGE_SIZE << il->hw_params.rx_page_order,
  321. PCI_DMA_FROMDEVICE);
  322. __free_pages(page, il->hw_params.rx_page_order);
  323. return;
  324. }
  325. element = rxq->rx_used.next;
  326. rxb = list_entry(element, struct il_rx_buf, list);
  327. list_del(element);
  328. BUG_ON(rxb->page);
  329. rxb->page = page;
  330. rxb->page_dma = page_dma;
  331. list_add_tail(&rxb->list, &rxq->rx_free);
  332. rxq->free_count++;
  333. il->alloc_rxb_page++;
  334. spin_unlock_irqrestore(&rxq->lock, flags);
  335. }
  336. }
  337. void
  338. il4965_rx_replenish(struct il_priv *il)
  339. {
  340. unsigned long flags;
  341. il4965_rx_allocate(il, GFP_KERNEL);
  342. spin_lock_irqsave(&il->lock, flags);
  343. il4965_rx_queue_restock(il);
  344. spin_unlock_irqrestore(&il->lock, flags);
  345. }
  346. void
  347. il4965_rx_replenish_now(struct il_priv *il)
  348. {
  349. il4965_rx_allocate(il, GFP_ATOMIC);
  350. il4965_rx_queue_restock(il);
  351. }
  352. /* Assumes that the skb field of the buffers in 'pool' is kept accurate.
  353. * If an SKB has been detached, the POOL needs to have its SKB set to NULL
  354. * This free routine walks the list of POOL entries and if SKB is set to
  355. * non NULL it is unmapped and freed
  356. */
  357. void
  358. il4965_rx_queue_free(struct il_priv *il, struct il_rx_queue *rxq)
  359. {
  360. int i;
  361. for (i = 0; i < RX_QUEUE_SIZE + RX_FREE_BUFFERS; i++) {
  362. if (rxq->pool[i].page != NULL) {
  363. pci_unmap_page(il->pci_dev, rxq->pool[i].page_dma,
  364. PAGE_SIZE << il->hw_params.rx_page_order,
  365. PCI_DMA_FROMDEVICE);
  366. __il_free_pages(il, rxq->pool[i].page);
  367. rxq->pool[i].page = NULL;
  368. }
  369. }
  370. dma_free_coherent(&il->pci_dev->dev, 4 * RX_QUEUE_SIZE, rxq->bd,
  371. rxq->bd_dma);
  372. dma_free_coherent(&il->pci_dev->dev, sizeof(struct il_rb_status),
  373. rxq->rb_stts, rxq->rb_stts_dma);
  374. rxq->bd = NULL;
  375. rxq->rb_stts = NULL;
  376. }
  377. int
  378. il4965_rxq_stop(struct il_priv *il)
  379. {
  380. int ret;
  381. _il_wr(il, FH49_MEM_RCSR_CHNL0_CONFIG_REG, 0);
  382. ret = _il_poll_bit(il, FH49_MEM_RSSR_RX_STATUS_REG,
  383. FH49_RSSR_CHNL0_RX_STATUS_CHNL_IDLE,
  384. FH49_RSSR_CHNL0_RX_STATUS_CHNL_IDLE,
  385. 1000);
  386. if (ret < 0)
  387. IL_ERR("Can't stop Rx DMA.\n");
  388. return 0;
  389. }
  390. int
  391. il4965_hwrate_to_mac80211_idx(u32 rate_n_flags, enum ieee80211_band band)
  392. {
  393. int idx = 0;
  394. int band_offset = 0;
  395. /* HT rate format: mac80211 wants an MCS number, which is just LSB */
  396. if (rate_n_flags & RATE_MCS_HT_MSK) {
  397. idx = (rate_n_flags & 0xff);
  398. return idx;
  399. /* Legacy rate format, search for match in table */
  400. } else {
  401. if (band == IEEE80211_BAND_5GHZ)
  402. band_offset = IL_FIRST_OFDM_RATE;
  403. for (idx = band_offset; idx < RATE_COUNT_LEGACY; idx++)
  404. if (il_rates[idx].plcp == (rate_n_flags & 0xFF))
  405. return idx - band_offset;
  406. }
  407. return -1;
  408. }
  409. static int
  410. il4965_calc_rssi(struct il_priv *il, struct il_rx_phy_res *rx_resp)
  411. {
  412. /* data from PHY/DSP regarding signal strength, etc.,
  413. * contents are always there, not configurable by host. */
  414. struct il4965_rx_non_cfg_phy *ncphy =
  415. (struct il4965_rx_non_cfg_phy *)rx_resp->non_cfg_phy_buf;
  416. u32 agc =
  417. (le16_to_cpu(ncphy->agc_info) & IL49_AGC_DB_MASK) >>
  418. IL49_AGC_DB_POS;
  419. u32 valid_antennae =
  420. (le16_to_cpu(rx_resp->phy_flags) & IL49_RX_PHY_FLAGS_ANTENNAE_MASK)
  421. >> IL49_RX_PHY_FLAGS_ANTENNAE_OFFSET;
  422. u8 max_rssi = 0;
  423. u32 i;
  424. /* Find max rssi among 3 possible receivers.
  425. * These values are measured by the digital signal processor (DSP).
  426. * They should stay fairly constant even as the signal strength varies,
  427. * if the radio's automatic gain control (AGC) is working right.
  428. * AGC value (see below) will provide the "interesting" info. */
  429. for (i = 0; i < 3; i++)
  430. if (valid_antennae & (1 << i))
  431. max_rssi = max(ncphy->rssi_info[i << 1], max_rssi);
  432. D_STATS("Rssi In A %d B %d C %d Max %d AGC dB %d\n",
  433. ncphy->rssi_info[0], ncphy->rssi_info[2], ncphy->rssi_info[4],
  434. max_rssi, agc);
  435. /* dBm = max_rssi dB - agc dB - constant.
  436. * Higher AGC (higher radio gain) means lower signal. */
  437. return max_rssi - agc - IL4965_RSSI_OFFSET;
  438. }
  439. static u32
  440. il4965_translate_rx_status(struct il_priv *il, u32 decrypt_in)
  441. {
  442. u32 decrypt_out = 0;
  443. if ((decrypt_in & RX_RES_STATUS_STATION_FOUND) ==
  444. RX_RES_STATUS_STATION_FOUND)
  445. decrypt_out |=
  446. (RX_RES_STATUS_STATION_FOUND |
  447. RX_RES_STATUS_NO_STATION_INFO_MISMATCH);
  448. decrypt_out |= (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK);
  449. /* packet was not encrypted */
  450. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  451. RX_RES_STATUS_SEC_TYPE_NONE)
  452. return decrypt_out;
  453. /* packet was encrypted with unknown alg */
  454. if ((decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) ==
  455. RX_RES_STATUS_SEC_TYPE_ERR)
  456. return decrypt_out;
  457. /* decryption was not done in HW */
  458. if ((decrypt_in & RX_MPDU_RES_STATUS_DEC_DONE_MSK) !=
  459. RX_MPDU_RES_STATUS_DEC_DONE_MSK)
  460. return decrypt_out;
  461. switch (decrypt_in & RX_RES_STATUS_SEC_TYPE_MSK) {
  462. case RX_RES_STATUS_SEC_TYPE_CCMP:
  463. /* alg is CCM: check MIC only */
  464. if (!(decrypt_in & RX_MPDU_RES_STATUS_MIC_OK))
  465. /* Bad MIC */
  466. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  467. else
  468. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  469. break;
  470. case RX_RES_STATUS_SEC_TYPE_TKIP:
  471. if (!(decrypt_in & RX_MPDU_RES_STATUS_TTAK_OK)) {
  472. /* Bad TTAK */
  473. decrypt_out |= RX_RES_STATUS_BAD_KEY_TTAK;
  474. break;
  475. }
  476. /* fall through if TTAK OK */
  477. default:
  478. if (!(decrypt_in & RX_MPDU_RES_STATUS_ICV_OK))
  479. decrypt_out |= RX_RES_STATUS_BAD_ICV_MIC;
  480. else
  481. decrypt_out |= RX_RES_STATUS_DECRYPT_OK;
  482. break;
  483. }
  484. D_RX("decrypt_in:0x%x decrypt_out = 0x%x\n", decrypt_in, decrypt_out);
  485. return decrypt_out;
  486. }
  487. static void
  488. il4965_pass_packet_to_mac80211(struct il_priv *il, struct ieee80211_hdr *hdr,
  489. u16 len, u32 ampdu_status, struct il_rx_buf *rxb,
  490. struct ieee80211_rx_status *stats)
  491. {
  492. struct sk_buff *skb;
  493. __le16 fc = hdr->frame_control;
  494. /* We only process data packets if the interface is open */
  495. if (unlikely(!il->is_open)) {
  496. D_DROP("Dropping packet while interface is not open.\n");
  497. return;
  498. }
  499. /* In case of HW accelerated crypto and bad decryption, drop */
  500. if (!il->cfg->mod_params->sw_crypto &&
  501. il_set_decrypted_flag(il, hdr, ampdu_status, stats))
  502. return;
  503. skb = dev_alloc_skb(128);
  504. if (!skb) {
  505. IL_ERR("dev_alloc_skb failed\n");
  506. return;
  507. }
  508. skb_add_rx_frag(skb, 0, rxb->page, (void *)hdr - rxb_addr(rxb), len,
  509. len);
  510. il_update_stats(il, false, fc, len);
  511. memcpy(IEEE80211_SKB_RXCB(skb), stats, sizeof(*stats));
  512. ieee80211_rx(il->hw, skb);
  513. il->alloc_rxb_page--;
  514. rxb->page = NULL;
  515. }
  516. /* Called for N_RX (legacy ABG frames), or
  517. * N_RX_MPDU (HT high-throughput N frames). */
  518. static void
  519. il4965_hdl_rx(struct il_priv *il, struct il_rx_buf *rxb)
  520. {
  521. struct ieee80211_hdr *header;
  522. struct ieee80211_rx_status rx_status = {};
  523. struct il_rx_pkt *pkt = rxb_addr(rxb);
  524. struct il_rx_phy_res *phy_res;
  525. __le32 rx_pkt_status;
  526. struct il_rx_mpdu_res_start *amsdu;
  527. u32 len;
  528. u32 ampdu_status;
  529. u32 rate_n_flags;
  530. /**
  531. * N_RX and N_RX_MPDU are handled differently.
  532. * N_RX: physical layer info is in this buffer
  533. * N_RX_MPDU: physical layer info was sent in separate
  534. * command and cached in il->last_phy_res
  535. *
  536. * Here we set up local variables depending on which command is
  537. * received.
  538. */
  539. if (pkt->hdr.cmd == N_RX) {
  540. phy_res = (struct il_rx_phy_res *)pkt->u.raw;
  541. header =
  542. (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*phy_res) +
  543. phy_res->cfg_phy_cnt);
  544. len = le16_to_cpu(phy_res->byte_count);
  545. rx_pkt_status =
  546. *(__le32 *) (pkt->u.raw + sizeof(*phy_res) +
  547. phy_res->cfg_phy_cnt + len);
  548. ampdu_status = le32_to_cpu(rx_pkt_status);
  549. } else {
  550. if (!il->_4965.last_phy_res_valid) {
  551. IL_ERR("MPDU frame without cached PHY data\n");
  552. return;
  553. }
  554. phy_res = &il->_4965.last_phy_res;
  555. amsdu = (struct il_rx_mpdu_res_start *)pkt->u.raw;
  556. header = (struct ieee80211_hdr *)(pkt->u.raw + sizeof(*amsdu));
  557. len = le16_to_cpu(amsdu->byte_count);
  558. rx_pkt_status = *(__le32 *) (pkt->u.raw + sizeof(*amsdu) + len);
  559. ampdu_status =
  560. il4965_translate_rx_status(il, le32_to_cpu(rx_pkt_status));
  561. }
  562. if ((unlikely(phy_res->cfg_phy_cnt > 20))) {
  563. D_DROP("dsp size out of range [0,20]: %d/n",
  564. phy_res->cfg_phy_cnt);
  565. return;
  566. }
  567. if (!(rx_pkt_status & RX_RES_STATUS_NO_CRC32_ERROR) ||
  568. !(rx_pkt_status & RX_RES_STATUS_NO_RXE_OVERFLOW)) {
  569. D_RX("Bad CRC or FIFO: 0x%08X.\n", le32_to_cpu(rx_pkt_status));
  570. return;
  571. }
  572. /* This will be used in several places later */
  573. rate_n_flags = le32_to_cpu(phy_res->rate_n_flags);
  574. /* rx_status carries information about the packet to mac80211 */
  575. rx_status.mactime = le64_to_cpu(phy_res->timestamp);
  576. rx_status.band =
  577. (phy_res->
  578. phy_flags & RX_RES_PHY_FLAGS_BAND_24_MSK) ? IEEE80211_BAND_2GHZ :
  579. IEEE80211_BAND_5GHZ;
  580. rx_status.freq =
  581. ieee80211_channel_to_frequency(le16_to_cpu(phy_res->channel),
  582. rx_status.band);
  583. rx_status.rate_idx =
  584. il4965_hwrate_to_mac80211_idx(rate_n_flags, rx_status.band);
  585. rx_status.flag = 0;
  586. /* TSF isn't reliable. In order to allow smooth user experience,
  587. * this W/A doesn't propagate it to the mac80211 */
  588. /*rx_status.flag |= RX_FLAG_MACTIME_START; */
  589. il->ucode_beacon_time = le32_to_cpu(phy_res->beacon_time_stamp);
  590. /* Find max signal strength (dBm) among 3 antenna/receiver chains */
  591. rx_status.signal = il4965_calc_rssi(il, phy_res);
  592. D_STATS("Rssi %d, TSF %llu\n", rx_status.signal,
  593. (unsigned long long)rx_status.mactime);
  594. /*
  595. * "antenna number"
  596. *
  597. * It seems that the antenna field in the phy flags value
  598. * is actually a bit field. This is undefined by radiotap,
  599. * it wants an actual antenna number but I always get "7"
  600. * for most legacy frames I receive indicating that the
  601. * same frame was received on all three RX chains.
  602. *
  603. * I think this field should be removed in favor of a
  604. * new 802.11n radiotap field "RX chains" that is defined
  605. * as a bitmask.
  606. */
  607. rx_status.antenna =
  608. (le16_to_cpu(phy_res->phy_flags) & RX_RES_PHY_FLAGS_ANTENNA_MSK) >>
  609. RX_RES_PHY_FLAGS_ANTENNA_POS;
  610. /* set the preamble flag if appropriate */
  611. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_SHORT_PREAMBLE_MSK)
  612. rx_status.flag |= RX_FLAG_SHORTPRE;
  613. /* Set up the HT phy flags */
  614. if (rate_n_flags & RATE_MCS_HT_MSK)
  615. rx_status.flag |= RX_FLAG_HT;
  616. if (rate_n_flags & RATE_MCS_HT40_MSK)
  617. rx_status.flag |= RX_FLAG_40MHZ;
  618. if (rate_n_flags & RATE_MCS_SGI_MSK)
  619. rx_status.flag |= RX_FLAG_SHORT_GI;
  620. if (phy_res->phy_flags & RX_RES_PHY_FLAGS_AGG_MSK) {
  621. /* We know which subframes of an A-MPDU belong
  622. * together since we get a single PHY response
  623. * from the firmware for all of them.
  624. */
  625. rx_status.flag |= RX_FLAG_AMPDU_DETAILS;
  626. rx_status.ampdu_reference = il->_4965.ampdu_ref;
  627. }
  628. il4965_pass_packet_to_mac80211(il, header, len, ampdu_status, rxb,
  629. &rx_status);
  630. }
  631. /* Cache phy data (Rx signal strength, etc) for HT frame (N_RX_PHY).
  632. * This will be used later in il_hdl_rx() for N_RX_MPDU. */
  633. static void
  634. il4965_hdl_rx_phy(struct il_priv *il, struct il_rx_buf *rxb)
  635. {
  636. struct il_rx_pkt *pkt = rxb_addr(rxb);
  637. il->_4965.last_phy_res_valid = true;
  638. il->_4965.ampdu_ref++;
  639. memcpy(&il->_4965.last_phy_res, pkt->u.raw,
  640. sizeof(struct il_rx_phy_res));
  641. }
  642. static int
  643. il4965_get_channels_for_scan(struct il_priv *il, struct ieee80211_vif *vif,
  644. enum ieee80211_band band, u8 is_active,
  645. u8 n_probes, struct il_scan_channel *scan_ch)
  646. {
  647. struct ieee80211_channel *chan;
  648. const struct ieee80211_supported_band *sband;
  649. const struct il_channel_info *ch_info;
  650. u16 passive_dwell = 0;
  651. u16 active_dwell = 0;
  652. int added, i;
  653. u16 channel;
  654. sband = il_get_hw_mode(il, band);
  655. if (!sband)
  656. return 0;
  657. active_dwell = il_get_active_dwell_time(il, band, n_probes);
  658. passive_dwell = il_get_passive_dwell_time(il, band, vif);
  659. if (passive_dwell <= active_dwell)
  660. passive_dwell = active_dwell + 1;
  661. for (i = 0, added = 0; i < il->scan_request->n_channels; i++) {
  662. chan = il->scan_request->channels[i];
  663. if (chan->band != band)
  664. continue;
  665. channel = chan->hw_value;
  666. scan_ch->channel = cpu_to_le16(channel);
  667. ch_info = il_get_channel_info(il, band, channel);
  668. if (!il_is_channel_valid(ch_info)) {
  669. D_SCAN("Channel %d is INVALID for this band.\n",
  670. channel);
  671. continue;
  672. }
  673. if (!is_active || il_is_channel_passive(ch_info) ||
  674. (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN))
  675. scan_ch->type = SCAN_CHANNEL_TYPE_PASSIVE;
  676. else
  677. scan_ch->type = SCAN_CHANNEL_TYPE_ACTIVE;
  678. if (n_probes)
  679. scan_ch->type |= IL_SCAN_PROBE_MASK(n_probes);
  680. scan_ch->active_dwell = cpu_to_le16(active_dwell);
  681. scan_ch->passive_dwell = cpu_to_le16(passive_dwell);
  682. /* Set txpower levels to defaults */
  683. scan_ch->dsp_atten = 110;
  684. /* NOTE: if we were doing 6Mb OFDM for scans we'd use
  685. * power level:
  686. * scan_ch->tx_gain = ((1 << 5) | (2 << 3)) | 3;
  687. */
  688. if (band == IEEE80211_BAND_5GHZ)
  689. scan_ch->tx_gain = ((1 << 5) | (3 << 3)) | 3;
  690. else
  691. scan_ch->tx_gain = ((1 << 5) | (5 << 3));
  692. D_SCAN("Scanning ch=%d prob=0x%X [%s %d]\n", channel,
  693. le32_to_cpu(scan_ch->type),
  694. (scan_ch->
  695. type & SCAN_CHANNEL_TYPE_ACTIVE) ? "ACTIVE" : "PASSIVE",
  696. (scan_ch->
  697. type & SCAN_CHANNEL_TYPE_ACTIVE) ? active_dwell :
  698. passive_dwell);
  699. scan_ch++;
  700. added++;
  701. }
  702. D_SCAN("total channels to scan %d\n", added);
  703. return added;
  704. }
  705. static void
  706. il4965_toggle_tx_ant(struct il_priv *il, u8 *ant, u8 valid)
  707. {
  708. int i;
  709. u8 ind = *ant;
  710. for (i = 0; i < RATE_ANT_NUM - 1; i++) {
  711. ind = (ind + 1) < RATE_ANT_NUM ? ind + 1 : 0;
  712. if (valid & BIT(ind)) {
  713. *ant = ind;
  714. return;
  715. }
  716. }
  717. }
  718. int
  719. il4965_request_scan(struct il_priv *il, struct ieee80211_vif *vif)
  720. {
  721. struct il_host_cmd cmd = {
  722. .id = C_SCAN,
  723. .len = sizeof(struct il_scan_cmd),
  724. .flags = CMD_SIZE_HUGE,
  725. };
  726. struct il_scan_cmd *scan;
  727. u32 rate_flags = 0;
  728. u16 cmd_len;
  729. u16 rx_chain = 0;
  730. enum ieee80211_band band;
  731. u8 n_probes = 0;
  732. u8 rx_ant = il->hw_params.valid_rx_ant;
  733. u8 rate;
  734. bool is_active = false;
  735. int chan_mod;
  736. u8 active_chains;
  737. u8 scan_tx_antennas = il->hw_params.valid_tx_ant;
  738. int ret;
  739. lockdep_assert_held(&il->mutex);
  740. if (!il->scan_cmd) {
  741. il->scan_cmd =
  742. kmalloc(sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE,
  743. GFP_KERNEL);
  744. if (!il->scan_cmd) {
  745. D_SCAN("fail to allocate memory for scan\n");
  746. return -ENOMEM;
  747. }
  748. }
  749. scan = il->scan_cmd;
  750. memset(scan, 0, sizeof(struct il_scan_cmd) + IL_MAX_SCAN_SIZE);
  751. scan->quiet_plcp_th = IL_PLCP_QUIET_THRESH;
  752. scan->quiet_time = IL_ACTIVE_QUIET_TIME;
  753. if (il_is_any_associated(il)) {
  754. u16 interval;
  755. u32 extra;
  756. u32 suspend_time = 100;
  757. u32 scan_suspend_time = 100;
  758. D_INFO("Scanning while associated...\n");
  759. interval = vif->bss_conf.beacon_int;
  760. scan->suspend_time = 0;
  761. scan->max_out_time = cpu_to_le32(200 * 1024);
  762. if (!interval)
  763. interval = suspend_time;
  764. extra = (suspend_time / interval) << 22;
  765. scan_suspend_time =
  766. (extra | ((suspend_time % interval) * 1024));
  767. scan->suspend_time = cpu_to_le32(scan_suspend_time);
  768. D_SCAN("suspend_time 0x%X beacon interval %d\n",
  769. scan_suspend_time, interval);
  770. }
  771. if (il->scan_request->n_ssids) {
  772. int i, p = 0;
  773. D_SCAN("Kicking off active scan\n");
  774. for (i = 0; i < il->scan_request->n_ssids; i++) {
  775. /* always does wildcard anyway */
  776. if (!il->scan_request->ssids[i].ssid_len)
  777. continue;
  778. scan->direct_scan[p].id = WLAN_EID_SSID;
  779. scan->direct_scan[p].len =
  780. il->scan_request->ssids[i].ssid_len;
  781. memcpy(scan->direct_scan[p].ssid,
  782. il->scan_request->ssids[i].ssid,
  783. il->scan_request->ssids[i].ssid_len);
  784. n_probes++;
  785. p++;
  786. }
  787. is_active = true;
  788. } else
  789. D_SCAN("Start passive scan.\n");
  790. scan->tx_cmd.tx_flags = TX_CMD_FLG_SEQ_CTL_MSK;
  791. scan->tx_cmd.sta_id = il->hw_params.bcast_id;
  792. scan->tx_cmd.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  793. switch (il->scan_band) {
  794. case IEEE80211_BAND_2GHZ:
  795. scan->flags = RXON_FLG_BAND_24G_MSK | RXON_FLG_AUTO_DETECT_MSK;
  796. chan_mod =
  797. le32_to_cpu(il->active.flags & RXON_FLG_CHANNEL_MODE_MSK) >>
  798. RXON_FLG_CHANNEL_MODE_POS;
  799. if (chan_mod == CHANNEL_MODE_PURE_40) {
  800. rate = RATE_6M_PLCP;
  801. } else {
  802. rate = RATE_1M_PLCP;
  803. rate_flags = RATE_MCS_CCK_MSK;
  804. }
  805. break;
  806. case IEEE80211_BAND_5GHZ:
  807. rate = RATE_6M_PLCP;
  808. break;
  809. default:
  810. IL_WARN("Invalid scan band\n");
  811. return -EIO;
  812. }
  813. /*
  814. * If active scanning is requested but a certain channel is
  815. * marked passive, we can do active scanning if we detect
  816. * transmissions.
  817. *
  818. * There is an issue with some firmware versions that triggers
  819. * a sysassert on a "good CRC threshold" of zero (== disabled),
  820. * on a radar channel even though this means that we should NOT
  821. * send probes.
  822. *
  823. * The "good CRC threshold" is the number of frames that we
  824. * need to receive during our dwell time on a channel before
  825. * sending out probes -- setting this to a huge value will
  826. * mean we never reach it, but at the same time work around
  827. * the aforementioned issue. Thus use IL_GOOD_CRC_TH_NEVER
  828. * here instead of IL_GOOD_CRC_TH_DISABLED.
  829. */
  830. scan->good_CRC_th =
  831. is_active ? IL_GOOD_CRC_TH_DEFAULT : IL_GOOD_CRC_TH_NEVER;
  832. band = il->scan_band;
  833. if (il->cfg->scan_rx_antennas[band])
  834. rx_ant = il->cfg->scan_rx_antennas[band];
  835. il4965_toggle_tx_ant(il, &il->scan_tx_ant[band], scan_tx_antennas);
  836. rate_flags |= BIT(il->scan_tx_ant[band]) << RATE_MCS_ANT_POS;
  837. scan->tx_cmd.rate_n_flags = cpu_to_le32(rate | rate_flags);
  838. /* In power save mode use one chain, otherwise use all chains */
  839. if (test_bit(S_POWER_PMI, &il->status)) {
  840. /* rx_ant has been set to all valid chains previously */
  841. active_chains =
  842. rx_ant & ((u8) (il->chain_noise_data.active_chains));
  843. if (!active_chains)
  844. active_chains = rx_ant;
  845. D_SCAN("chain_noise_data.active_chains: %u\n",
  846. il->chain_noise_data.active_chains);
  847. rx_ant = il4965_first_antenna(active_chains);
  848. }
  849. /* MIMO is not used here, but value is required */
  850. rx_chain |= il->hw_params.valid_rx_ant << RXON_RX_CHAIN_VALID_POS;
  851. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_MIMO_SEL_POS;
  852. rx_chain |= rx_ant << RXON_RX_CHAIN_FORCE_SEL_POS;
  853. rx_chain |= 0x1 << RXON_RX_CHAIN_DRIVER_FORCE_POS;
  854. scan->rx_chain = cpu_to_le16(rx_chain);
  855. cmd_len =
  856. il_fill_probe_req(il, (struct ieee80211_mgmt *)scan->data,
  857. vif->addr, il->scan_request->ie,
  858. il->scan_request->ie_len,
  859. IL_MAX_SCAN_SIZE - sizeof(*scan));
  860. scan->tx_cmd.len = cpu_to_le16(cmd_len);
  861. scan->filter_flags |=
  862. (RXON_FILTER_ACCEPT_GRP_MSK | RXON_FILTER_BCON_AWARE_MSK);
  863. scan->channel_count =
  864. il4965_get_channels_for_scan(il, vif, band, is_active, n_probes,
  865. (void *)&scan->data[cmd_len]);
  866. if (scan->channel_count == 0) {
  867. D_SCAN("channel count %d\n", scan->channel_count);
  868. return -EIO;
  869. }
  870. cmd.len +=
  871. le16_to_cpu(scan->tx_cmd.len) +
  872. scan->channel_count * sizeof(struct il_scan_channel);
  873. cmd.data = scan;
  874. scan->len = cpu_to_le16(cmd.len);
  875. set_bit(S_SCAN_HW, &il->status);
  876. ret = il_send_cmd_sync(il, &cmd);
  877. if (ret)
  878. clear_bit(S_SCAN_HW, &il->status);
  879. return ret;
  880. }
  881. int
  882. il4965_manage_ibss_station(struct il_priv *il, struct ieee80211_vif *vif,
  883. bool add)
  884. {
  885. struct il_vif_priv *vif_priv = (void *)vif->drv_priv;
  886. if (add)
  887. return il4965_add_bssid_station(il, vif->bss_conf.bssid,
  888. &vif_priv->ibss_bssid_sta_id);
  889. return il_remove_station(il, vif_priv->ibss_bssid_sta_id,
  890. vif->bss_conf.bssid);
  891. }
  892. void
  893. il4965_free_tfds_in_queue(struct il_priv *il, int sta_id, int tid, int freed)
  894. {
  895. lockdep_assert_held(&il->sta_lock);
  896. if (il->stations[sta_id].tid[tid].tfds_in_queue >= freed)
  897. il->stations[sta_id].tid[tid].tfds_in_queue -= freed;
  898. else {
  899. D_TX("free more than tfds_in_queue (%u:%d)\n",
  900. il->stations[sta_id].tid[tid].tfds_in_queue, freed);
  901. il->stations[sta_id].tid[tid].tfds_in_queue = 0;
  902. }
  903. }
  904. #define IL_TX_QUEUE_MSK 0xfffff
  905. static bool
  906. il4965_is_single_rx_stream(struct il_priv *il)
  907. {
  908. return il->current_ht_config.smps == IEEE80211_SMPS_STATIC ||
  909. il->current_ht_config.single_chain_sufficient;
  910. }
  911. #define IL_NUM_RX_CHAINS_MULTIPLE 3
  912. #define IL_NUM_RX_CHAINS_SINGLE 2
  913. #define IL_NUM_IDLE_CHAINS_DUAL 2
  914. #define IL_NUM_IDLE_CHAINS_SINGLE 1
  915. /*
  916. * Determine how many receiver/antenna chains to use.
  917. *
  918. * More provides better reception via diversity. Fewer saves power
  919. * at the expense of throughput, but only when not in powersave to
  920. * start with.
  921. *
  922. * MIMO (dual stream) requires at least 2, but works better with 3.
  923. * This does not determine *which* chains to use, just how many.
  924. */
  925. static int
  926. il4965_get_active_rx_chain_count(struct il_priv *il)
  927. {
  928. /* # of Rx chains to use when expecting MIMO. */
  929. if (il4965_is_single_rx_stream(il))
  930. return IL_NUM_RX_CHAINS_SINGLE;
  931. else
  932. return IL_NUM_RX_CHAINS_MULTIPLE;
  933. }
  934. /*
  935. * When we are in power saving mode, unless device support spatial
  936. * multiplexing power save, use the active count for rx chain count.
  937. */
  938. static int
  939. il4965_get_idle_rx_chain_count(struct il_priv *il, int active_cnt)
  940. {
  941. /* # Rx chains when idling, depending on SMPS mode */
  942. switch (il->current_ht_config.smps) {
  943. case IEEE80211_SMPS_STATIC:
  944. case IEEE80211_SMPS_DYNAMIC:
  945. return IL_NUM_IDLE_CHAINS_SINGLE;
  946. case IEEE80211_SMPS_OFF:
  947. return active_cnt;
  948. default:
  949. WARN(1, "invalid SMPS mode %d", il->current_ht_config.smps);
  950. return active_cnt;
  951. }
  952. }
  953. /* up to 4 chains */
  954. static u8
  955. il4965_count_chain_bitmap(u32 chain_bitmap)
  956. {
  957. u8 res;
  958. res = (chain_bitmap & BIT(0)) >> 0;
  959. res += (chain_bitmap & BIT(1)) >> 1;
  960. res += (chain_bitmap & BIT(2)) >> 2;
  961. res += (chain_bitmap & BIT(3)) >> 3;
  962. return res;
  963. }
  964. /**
  965. * il4965_set_rxon_chain - Set up Rx chain usage in "staging" RXON image
  966. *
  967. * Selects how many and which Rx receivers/antennas/chains to use.
  968. * This should not be used for scan command ... it puts data in wrong place.
  969. */
  970. void
  971. il4965_set_rxon_chain(struct il_priv *il)
  972. {
  973. bool is_single = il4965_is_single_rx_stream(il);
  974. bool is_cam = !test_bit(S_POWER_PMI, &il->status);
  975. u8 idle_rx_cnt, active_rx_cnt, valid_rx_cnt;
  976. u32 active_chains;
  977. u16 rx_chain;
  978. /* Tell uCode which antennas are actually connected.
  979. * Before first association, we assume all antennas are connected.
  980. * Just after first association, il4965_chain_noise_calibration()
  981. * checks which antennas actually *are* connected. */
  982. if (il->chain_noise_data.active_chains)
  983. active_chains = il->chain_noise_data.active_chains;
  984. else
  985. active_chains = il->hw_params.valid_rx_ant;
  986. rx_chain = active_chains << RXON_RX_CHAIN_VALID_POS;
  987. /* How many receivers should we use? */
  988. active_rx_cnt = il4965_get_active_rx_chain_count(il);
  989. idle_rx_cnt = il4965_get_idle_rx_chain_count(il, active_rx_cnt);
  990. /* correct rx chain count according hw settings
  991. * and chain noise calibration
  992. */
  993. valid_rx_cnt = il4965_count_chain_bitmap(active_chains);
  994. if (valid_rx_cnt < active_rx_cnt)
  995. active_rx_cnt = valid_rx_cnt;
  996. if (valid_rx_cnt < idle_rx_cnt)
  997. idle_rx_cnt = valid_rx_cnt;
  998. rx_chain |= active_rx_cnt << RXON_RX_CHAIN_MIMO_CNT_POS;
  999. rx_chain |= idle_rx_cnt << RXON_RX_CHAIN_CNT_POS;
  1000. il->staging.rx_chain = cpu_to_le16(rx_chain);
  1001. if (!is_single && active_rx_cnt >= IL_NUM_RX_CHAINS_SINGLE && is_cam)
  1002. il->staging.rx_chain |= RXON_RX_CHAIN_MIMO_FORCE_MSK;
  1003. else
  1004. il->staging.rx_chain &= ~RXON_RX_CHAIN_MIMO_FORCE_MSK;
  1005. D_ASSOC("rx_chain=0x%X active=%d idle=%d\n", il->staging.rx_chain,
  1006. active_rx_cnt, idle_rx_cnt);
  1007. WARN_ON(active_rx_cnt == 0 || idle_rx_cnt == 0 ||
  1008. active_rx_cnt < idle_rx_cnt);
  1009. }
  1010. static const char *
  1011. il4965_get_fh_string(int cmd)
  1012. {
  1013. switch (cmd) {
  1014. IL_CMD(FH49_RSCSR_CHNL0_STTS_WPTR_REG);
  1015. IL_CMD(FH49_RSCSR_CHNL0_RBDCB_BASE_REG);
  1016. IL_CMD(FH49_RSCSR_CHNL0_WPTR);
  1017. IL_CMD(FH49_MEM_RCSR_CHNL0_CONFIG_REG);
  1018. IL_CMD(FH49_MEM_RSSR_SHARED_CTRL_REG);
  1019. IL_CMD(FH49_MEM_RSSR_RX_STATUS_REG);
  1020. IL_CMD(FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV);
  1021. IL_CMD(FH49_TSSR_TX_STATUS_REG);
  1022. IL_CMD(FH49_TSSR_TX_ERROR_REG);
  1023. default:
  1024. return "UNKNOWN";
  1025. }
  1026. }
  1027. int
  1028. il4965_dump_fh(struct il_priv *il, char **buf, bool display)
  1029. {
  1030. int i;
  1031. #ifdef CONFIG_IWLEGACY_DEBUG
  1032. int pos = 0;
  1033. size_t bufsz = 0;
  1034. #endif
  1035. static const u32 fh_tbl[] = {
  1036. FH49_RSCSR_CHNL0_STTS_WPTR_REG,
  1037. FH49_RSCSR_CHNL0_RBDCB_BASE_REG,
  1038. FH49_RSCSR_CHNL0_WPTR,
  1039. FH49_MEM_RCSR_CHNL0_CONFIG_REG,
  1040. FH49_MEM_RSSR_SHARED_CTRL_REG,
  1041. FH49_MEM_RSSR_RX_STATUS_REG,
  1042. FH49_MEM_RSSR_RX_ENABLE_ERR_IRQ2DRV,
  1043. FH49_TSSR_TX_STATUS_REG,
  1044. FH49_TSSR_TX_ERROR_REG
  1045. };
  1046. #ifdef CONFIG_IWLEGACY_DEBUG
  1047. if (display) {
  1048. bufsz = ARRAY_SIZE(fh_tbl) * 48 + 40;
  1049. *buf = kmalloc(bufsz, GFP_KERNEL);
  1050. if (!*buf)
  1051. return -ENOMEM;
  1052. pos +=
  1053. scnprintf(*buf + pos, bufsz - pos, "FH register values:\n");
  1054. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1055. pos +=
  1056. scnprintf(*buf + pos, bufsz - pos,
  1057. " %34s: 0X%08x\n",
  1058. il4965_get_fh_string(fh_tbl[i]),
  1059. il_rd(il, fh_tbl[i]));
  1060. }
  1061. return pos;
  1062. }
  1063. #endif
  1064. IL_ERR("FH register values:\n");
  1065. for (i = 0; i < ARRAY_SIZE(fh_tbl); i++) {
  1066. IL_ERR(" %34s: 0X%08x\n", il4965_get_fh_string(fh_tbl[i]),
  1067. il_rd(il, fh_tbl[i]));
  1068. }
  1069. return 0;
  1070. }
  1071. static void
  1072. il4965_hdl_missed_beacon(struct il_priv *il, struct il_rx_buf *rxb)
  1073. {
  1074. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1075. struct il_missed_beacon_notif *missed_beacon;
  1076. missed_beacon = &pkt->u.missed_beacon;
  1077. if (le32_to_cpu(missed_beacon->consecutive_missed_beacons) >
  1078. il->missed_beacon_threshold) {
  1079. D_CALIB("missed bcn cnsq %d totl %d rcd %d expctd %d\n",
  1080. le32_to_cpu(missed_beacon->consecutive_missed_beacons),
  1081. le32_to_cpu(missed_beacon->total_missed_becons),
  1082. le32_to_cpu(missed_beacon->num_recvd_beacons),
  1083. le32_to_cpu(missed_beacon->num_expected_beacons));
  1084. if (!test_bit(S_SCANNING, &il->status))
  1085. il4965_init_sensitivity(il);
  1086. }
  1087. }
  1088. /* Calculate noise level, based on measurements during network silence just
  1089. * before arriving beacon. This measurement can be done only if we know
  1090. * exactly when to expect beacons, therefore only when we're associated. */
  1091. static void
  1092. il4965_rx_calc_noise(struct il_priv *il)
  1093. {
  1094. struct stats_rx_non_phy *rx_info;
  1095. int num_active_rx = 0;
  1096. int total_silence = 0;
  1097. int bcn_silence_a, bcn_silence_b, bcn_silence_c;
  1098. int last_rx_noise;
  1099. rx_info = &(il->_4965.stats.rx.general);
  1100. bcn_silence_a =
  1101. le32_to_cpu(rx_info->beacon_silence_rssi_a) & IN_BAND_FILTER;
  1102. bcn_silence_b =
  1103. le32_to_cpu(rx_info->beacon_silence_rssi_b) & IN_BAND_FILTER;
  1104. bcn_silence_c =
  1105. le32_to_cpu(rx_info->beacon_silence_rssi_c) & IN_BAND_FILTER;
  1106. if (bcn_silence_a) {
  1107. total_silence += bcn_silence_a;
  1108. num_active_rx++;
  1109. }
  1110. if (bcn_silence_b) {
  1111. total_silence += bcn_silence_b;
  1112. num_active_rx++;
  1113. }
  1114. if (bcn_silence_c) {
  1115. total_silence += bcn_silence_c;
  1116. num_active_rx++;
  1117. }
  1118. /* Average among active antennas */
  1119. if (num_active_rx)
  1120. last_rx_noise = (total_silence / num_active_rx) - 107;
  1121. else
  1122. last_rx_noise = IL_NOISE_MEAS_NOT_AVAILABLE;
  1123. D_CALIB("inband silence a %u, b %u, c %u, dBm %d\n", bcn_silence_a,
  1124. bcn_silence_b, bcn_silence_c, last_rx_noise);
  1125. }
  1126. #ifdef CONFIG_IWLEGACY_DEBUGFS
  1127. /*
  1128. * based on the assumption of all stats counter are in DWORD
  1129. * FIXME: This function is for debugging, do not deal with
  1130. * the case of counters roll-over.
  1131. */
  1132. static void
  1133. il4965_accumulative_stats(struct il_priv *il, __le32 * stats)
  1134. {
  1135. int i, size;
  1136. __le32 *prev_stats;
  1137. u32 *accum_stats;
  1138. u32 *delta, *max_delta;
  1139. struct stats_general_common *general, *accum_general;
  1140. struct stats_tx *tx, *accum_tx;
  1141. prev_stats = (__le32 *) &il->_4965.stats;
  1142. accum_stats = (u32 *) &il->_4965.accum_stats;
  1143. size = sizeof(struct il_notif_stats);
  1144. general = &il->_4965.stats.general.common;
  1145. accum_general = &il->_4965.accum_stats.general.common;
  1146. tx = &il->_4965.stats.tx;
  1147. accum_tx = &il->_4965.accum_stats.tx;
  1148. delta = (u32 *) &il->_4965.delta_stats;
  1149. max_delta = (u32 *) &il->_4965.max_delta;
  1150. for (i = sizeof(__le32); i < size;
  1151. i +=
  1152. sizeof(__le32), stats++, prev_stats++, delta++, max_delta++,
  1153. accum_stats++) {
  1154. if (le32_to_cpu(*stats) > le32_to_cpu(*prev_stats)) {
  1155. *delta =
  1156. (le32_to_cpu(*stats) - le32_to_cpu(*prev_stats));
  1157. *accum_stats += *delta;
  1158. if (*delta > *max_delta)
  1159. *max_delta = *delta;
  1160. }
  1161. }
  1162. /* reset accumulative stats for "no-counter" type stats */
  1163. accum_general->temperature = general->temperature;
  1164. accum_general->ttl_timestamp = general->ttl_timestamp;
  1165. }
  1166. #endif
  1167. static void
  1168. il4965_hdl_stats(struct il_priv *il, struct il_rx_buf *rxb)
  1169. {
  1170. const int recalib_seconds = 60;
  1171. bool change;
  1172. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1173. D_RX("Statistics notification received (%d vs %d).\n",
  1174. (int)sizeof(struct il_notif_stats),
  1175. le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK);
  1176. change =
  1177. ((il->_4965.stats.general.common.temperature !=
  1178. pkt->u.stats.general.common.temperature) ||
  1179. ((il->_4965.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK) !=
  1180. (pkt->u.stats.flag & STATS_REPLY_FLG_HT40_MODE_MSK)));
  1181. #ifdef CONFIG_IWLEGACY_DEBUGFS
  1182. il4965_accumulative_stats(il, (__le32 *) &pkt->u.stats);
  1183. #endif
  1184. /* TODO: reading some of stats is unneeded */
  1185. memcpy(&il->_4965.stats, &pkt->u.stats, sizeof(il->_4965.stats));
  1186. set_bit(S_STATS, &il->status);
  1187. /*
  1188. * Reschedule the stats timer to occur in recalib_seconds to ensure
  1189. * we get a thermal update even if the uCode doesn't give us one
  1190. */
  1191. mod_timer(&il->stats_periodic,
  1192. jiffies + msecs_to_jiffies(recalib_seconds * 1000));
  1193. if (unlikely(!test_bit(S_SCANNING, &il->status)) &&
  1194. (pkt->hdr.cmd == N_STATS)) {
  1195. il4965_rx_calc_noise(il);
  1196. queue_work(il->workqueue, &il->run_time_calib_work);
  1197. }
  1198. if (change)
  1199. il4965_temperature_calib(il);
  1200. }
  1201. static void
  1202. il4965_hdl_c_stats(struct il_priv *il, struct il_rx_buf *rxb)
  1203. {
  1204. struct il_rx_pkt *pkt = rxb_addr(rxb);
  1205. if (le32_to_cpu(pkt->u.stats.flag) & UCODE_STATS_CLEAR_MSK) {
  1206. #ifdef CONFIG_IWLEGACY_DEBUGFS
  1207. memset(&il->_4965.accum_stats, 0,
  1208. sizeof(struct il_notif_stats));
  1209. memset(&il->_4965.delta_stats, 0,
  1210. sizeof(struct il_notif_stats));
  1211. memset(&il->_4965.max_delta, 0, sizeof(struct il_notif_stats));
  1212. #endif
  1213. D_RX("Statistics have been cleared\n");
  1214. }
  1215. il4965_hdl_stats(il, rxb);
  1216. }
  1217. /*
  1218. * mac80211 queues, ACs, hardware queues, FIFOs.
  1219. *
  1220. * Cf. http://wireless.kernel.org/en/developers/Documentation/mac80211/queues
  1221. *
  1222. * Mac80211 uses the following numbers, which we get as from it
  1223. * by way of skb_get_queue_mapping(skb):
  1224. *
  1225. * VO 0
  1226. * VI 1
  1227. * BE 2
  1228. * BK 3
  1229. *
  1230. *
  1231. * Regular (not A-MPDU) frames are put into hardware queues corresponding
  1232. * to the FIFOs, see comments in iwl-prph.h. Aggregated frames get their
  1233. * own queue per aggregation session (RA/TID combination), such queues are
  1234. * set up to map into FIFOs too, for which we need an AC->FIFO mapping. In
  1235. * order to map frames to the right queue, we also need an AC->hw queue
  1236. * mapping. This is implemented here.
  1237. *
  1238. * Due to the way hw queues are set up (by the hw specific modules like
  1239. * 4965.c), the AC->hw queue mapping is the identity
  1240. * mapping.
  1241. */
  1242. static const u8 tid_to_ac[] = {
  1243. IEEE80211_AC_BE,
  1244. IEEE80211_AC_BK,
  1245. IEEE80211_AC_BK,
  1246. IEEE80211_AC_BE,
  1247. IEEE80211_AC_VI,
  1248. IEEE80211_AC_VI,
  1249. IEEE80211_AC_VO,
  1250. IEEE80211_AC_VO
  1251. };
  1252. static inline int
  1253. il4965_get_ac_from_tid(u16 tid)
  1254. {
  1255. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  1256. return tid_to_ac[tid];
  1257. /* no support for TIDs 8-15 yet */
  1258. return -EINVAL;
  1259. }
  1260. static inline int
  1261. il4965_get_fifo_from_tid(u16 tid)
  1262. {
  1263. const u8 ac_to_fifo[] = {
  1264. IL_TX_FIFO_VO,
  1265. IL_TX_FIFO_VI,
  1266. IL_TX_FIFO_BE,
  1267. IL_TX_FIFO_BK,
  1268. };
  1269. if (likely(tid < ARRAY_SIZE(tid_to_ac)))
  1270. return ac_to_fifo[tid_to_ac[tid]];
  1271. /* no support for TIDs 8-15 yet */
  1272. return -EINVAL;
  1273. }
  1274. /*
  1275. * handle build C_TX command notification.
  1276. */
  1277. static void
  1278. il4965_tx_cmd_build_basic(struct il_priv *il, struct sk_buff *skb,
  1279. struct il_tx_cmd *tx_cmd,
  1280. struct ieee80211_tx_info *info,
  1281. struct ieee80211_hdr *hdr, u8 std_id)
  1282. {
  1283. __le16 fc = hdr->frame_control;
  1284. __le32 tx_flags = tx_cmd->tx_flags;
  1285. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  1286. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  1287. tx_flags |= TX_CMD_FLG_ACK_MSK;
  1288. if (ieee80211_is_mgmt(fc))
  1289. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  1290. if (ieee80211_is_probe_resp(fc) &&
  1291. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  1292. tx_flags |= TX_CMD_FLG_TSF_MSK;
  1293. } else {
  1294. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  1295. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  1296. }
  1297. if (ieee80211_is_back_req(fc))
  1298. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  1299. tx_cmd->sta_id = std_id;
  1300. if (ieee80211_has_morefrags(fc))
  1301. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  1302. if (ieee80211_is_data_qos(fc)) {
  1303. u8 *qc = ieee80211_get_qos_ctl(hdr);
  1304. tx_cmd->tid_tspec = qc[0] & 0xf;
  1305. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  1306. } else {
  1307. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  1308. }
  1309. il_tx_cmd_protection(il, info, fc, &tx_flags);
  1310. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  1311. if (ieee80211_is_mgmt(fc)) {
  1312. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  1313. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  1314. else
  1315. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  1316. } else {
  1317. tx_cmd->timeout.pm_frame_timeout = 0;
  1318. }
  1319. tx_cmd->driver_txop = 0;
  1320. tx_cmd->tx_flags = tx_flags;
  1321. tx_cmd->next_frame_len = 0;
  1322. }
  1323. static void
  1324. il4965_tx_cmd_build_rate(struct il_priv *il,
  1325. struct il_tx_cmd *tx_cmd,
  1326. struct ieee80211_tx_info *info,
  1327. struct ieee80211_sta *sta,
  1328. __le16 fc)
  1329. {
  1330. const u8 rts_retry_limit = 60;
  1331. u32 rate_flags;
  1332. int rate_idx;
  1333. u8 data_retry_limit;
  1334. u8 rate_plcp;
  1335. /* Set retry limit on DATA packets and Probe Responses */
  1336. if (ieee80211_is_probe_resp(fc))
  1337. data_retry_limit = 3;
  1338. else
  1339. data_retry_limit = IL4965_DEFAULT_TX_RETRY;
  1340. tx_cmd->data_retry_limit = data_retry_limit;
  1341. /* Set retry limit on RTS packets */
  1342. tx_cmd->rts_retry_limit = min(data_retry_limit, rts_retry_limit);
  1343. /* DATA packets will use the uCode station table for rate/antenna
  1344. * selection */
  1345. if (ieee80211_is_data(fc)) {
  1346. tx_cmd->initial_rate_idx = 0;
  1347. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  1348. return;
  1349. }
  1350. /**
  1351. * If the current TX rate stored in mac80211 has the MCS bit set, it's
  1352. * not really a TX rate. Thus, we use the lowest supported rate for
  1353. * this band. Also use the lowest supported rate if the stored rate
  1354. * idx is invalid.
  1355. */
  1356. rate_idx = info->control.rates[0].idx;
  1357. if ((info->control.rates[0].flags & IEEE80211_TX_RC_MCS) || rate_idx < 0
  1358. || rate_idx > RATE_COUNT_LEGACY)
  1359. rate_idx = rate_lowest_index(&il->bands[info->band], sta);
  1360. /* For 5 GHZ band, remap mac80211 rate indices into driver indices */
  1361. if (info->band == IEEE80211_BAND_5GHZ)
  1362. rate_idx += IL_FIRST_OFDM_RATE;
  1363. /* Get PLCP rate for tx_cmd->rate_n_flags */
  1364. rate_plcp = il_rates[rate_idx].plcp;
  1365. /* Zero out flags for this packet */
  1366. rate_flags = 0;
  1367. /* Set CCK flag as needed */
  1368. if (rate_idx >= IL_FIRST_CCK_RATE && rate_idx <= IL_LAST_CCK_RATE)
  1369. rate_flags |= RATE_MCS_CCK_MSK;
  1370. /* Set up antennas */
  1371. il4965_toggle_tx_ant(il, &il->mgmt_tx_ant, il->hw_params.valid_tx_ant);
  1372. rate_flags |= BIT(il->mgmt_tx_ant) << RATE_MCS_ANT_POS;
  1373. /* Set the rate in the TX cmd */
  1374. tx_cmd->rate_n_flags = cpu_to_le32(rate_plcp | rate_flags);
  1375. }
  1376. static void
  1377. il4965_tx_cmd_build_hwcrypto(struct il_priv *il, struct ieee80211_tx_info *info,
  1378. struct il_tx_cmd *tx_cmd, struct sk_buff *skb_frag,
  1379. int sta_id)
  1380. {
  1381. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  1382. switch (keyconf->cipher) {
  1383. case WLAN_CIPHER_SUITE_CCMP:
  1384. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  1385. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  1386. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  1387. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  1388. D_TX("tx_cmd with AES hwcrypto\n");
  1389. break;
  1390. case WLAN_CIPHER_SUITE_TKIP:
  1391. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  1392. ieee80211_get_tkip_p2k(keyconf, skb_frag, tx_cmd->key);
  1393. D_TX("tx_cmd with tkip hwcrypto\n");
  1394. break;
  1395. case WLAN_CIPHER_SUITE_WEP104:
  1396. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  1397. /* fall through */
  1398. case WLAN_CIPHER_SUITE_WEP40:
  1399. tx_cmd->sec_ctl |=
  1400. (TX_CMD_SEC_WEP | (keyconf->keyidx & TX_CMD_SEC_MSK) <<
  1401. TX_CMD_SEC_SHIFT);
  1402. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  1403. D_TX("Configuring packet for WEP encryption " "with key %d\n",
  1404. keyconf->keyidx);
  1405. break;
  1406. default:
  1407. IL_ERR("Unknown encode cipher %x\n", keyconf->cipher);
  1408. break;
  1409. }
  1410. }
  1411. /*
  1412. * start C_TX command process
  1413. */
  1414. int
  1415. il4965_tx_skb(struct il_priv *il,
  1416. struct ieee80211_sta *sta,
  1417. struct sk_buff *skb)
  1418. {
  1419. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1420. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1421. struct il_station_priv *sta_priv = NULL;
  1422. struct il_tx_queue *txq;
  1423. struct il_queue *q;
  1424. struct il_device_cmd *out_cmd;
  1425. struct il_cmd_meta *out_meta;
  1426. struct il_tx_cmd *tx_cmd;
  1427. int txq_id;
  1428. dma_addr_t phys_addr;
  1429. dma_addr_t txcmd_phys;
  1430. dma_addr_t scratch_phys;
  1431. u16 len, firstlen, secondlen;
  1432. u16 seq_number = 0;
  1433. __le16 fc;
  1434. u8 hdr_len;
  1435. u8 sta_id;
  1436. u8 wait_write_ptr = 0;
  1437. u8 tid = 0;
  1438. u8 *qc = NULL;
  1439. unsigned long flags;
  1440. bool is_agg = false;
  1441. spin_lock_irqsave(&il->lock, flags);
  1442. if (il_is_rfkill(il)) {
  1443. D_DROP("Dropping - RF KILL\n");
  1444. goto drop_unlock;
  1445. }
  1446. fc = hdr->frame_control;
  1447. #ifdef CONFIG_IWLEGACY_DEBUG
  1448. if (ieee80211_is_auth(fc))
  1449. D_TX("Sending AUTH frame\n");
  1450. else if (ieee80211_is_assoc_req(fc))
  1451. D_TX("Sending ASSOC frame\n");
  1452. else if (ieee80211_is_reassoc_req(fc))
  1453. D_TX("Sending REASSOC frame\n");
  1454. #endif
  1455. hdr_len = ieee80211_hdrlen(fc);
  1456. /* For management frames use broadcast id to do not break aggregation */
  1457. if (!ieee80211_is_data(fc))
  1458. sta_id = il->hw_params.bcast_id;
  1459. else {
  1460. /* Find idx into station table for destination station */
  1461. sta_id = il_sta_id_or_broadcast(il, sta);
  1462. if (sta_id == IL_INVALID_STATION) {
  1463. D_DROP("Dropping - INVALID STATION: %pM\n", hdr->addr1);
  1464. goto drop_unlock;
  1465. }
  1466. }
  1467. D_TX("station Id %d\n", sta_id);
  1468. if (sta)
  1469. sta_priv = (void *)sta->drv_priv;
  1470. if (sta_priv && sta_priv->asleep &&
  1471. (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)) {
  1472. /*
  1473. * This sends an asynchronous command to the device,
  1474. * but we can rely on it being processed before the
  1475. * next frame is processed -- and the next frame to
  1476. * this station is the one that will consume this
  1477. * counter.
  1478. * For now set the counter to just 1 since we do not
  1479. * support uAPSD yet.
  1480. */
  1481. il4965_sta_modify_sleep_tx_count(il, sta_id, 1);
  1482. }
  1483. /* FIXME: remove me ? */
  1484. WARN_ON_ONCE(info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM);
  1485. /* Access category (AC) is also the queue number */
  1486. txq_id = skb_get_queue_mapping(skb);
  1487. /* irqs already disabled/saved above when locking il->lock */
  1488. spin_lock(&il->sta_lock);
  1489. if (ieee80211_is_data_qos(fc)) {
  1490. qc = ieee80211_get_qos_ctl(hdr);
  1491. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  1492. if (WARN_ON_ONCE(tid >= MAX_TID_COUNT)) {
  1493. spin_unlock(&il->sta_lock);
  1494. goto drop_unlock;
  1495. }
  1496. seq_number = il->stations[sta_id].tid[tid].seq_number;
  1497. seq_number &= IEEE80211_SCTL_SEQ;
  1498. hdr->seq_ctrl =
  1499. hdr->seq_ctrl & cpu_to_le16(IEEE80211_SCTL_FRAG);
  1500. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  1501. seq_number += 0x10;
  1502. /* aggregation is on for this <sta,tid> */
  1503. if (info->flags & IEEE80211_TX_CTL_AMPDU &&
  1504. il->stations[sta_id].tid[tid].agg.state == IL_AGG_ON) {
  1505. txq_id = il->stations[sta_id].tid[tid].agg.txq_id;
  1506. is_agg = true;
  1507. }
  1508. }
  1509. txq = &il->txq[txq_id];
  1510. q = &txq->q;
  1511. if (unlikely(il_queue_space(q) < q->high_mark)) {
  1512. spin_unlock(&il->sta_lock);
  1513. goto drop_unlock;
  1514. }
  1515. if (ieee80211_is_data_qos(fc)) {
  1516. il->stations[sta_id].tid[tid].tfds_in_queue++;
  1517. if (!ieee80211_has_morefrags(fc))
  1518. il->stations[sta_id].tid[tid].seq_number = seq_number;
  1519. }
  1520. spin_unlock(&il->sta_lock);
  1521. txq->skbs[q->write_ptr] = skb;
  1522. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  1523. out_cmd = txq->cmd[q->write_ptr];
  1524. out_meta = &txq->meta[q->write_ptr];
  1525. tx_cmd = &out_cmd->cmd.tx;
  1526. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  1527. memset(tx_cmd, 0, sizeof(struct il_tx_cmd));
  1528. /*
  1529. * Set up the Tx-command (not MAC!) header.
  1530. * Store the chosen Tx queue and TFD idx within the sequence field;
  1531. * after Tx, uCode's Tx response will return this value so driver can
  1532. * locate the frame within the tx queue and do post-tx processing.
  1533. */
  1534. out_cmd->hdr.cmd = C_TX;
  1535. out_cmd->hdr.sequence =
  1536. cpu_to_le16((u16)
  1537. (QUEUE_TO_SEQ(txq_id) | IDX_TO_SEQ(q->write_ptr)));
  1538. /* Copy MAC header from skb into command buffer */
  1539. memcpy(tx_cmd->hdr, hdr, hdr_len);
  1540. /* Total # bytes to be transmitted */
  1541. tx_cmd->len = cpu_to_le16((u16) skb->len);
  1542. if (info->control.hw_key)
  1543. il4965_tx_cmd_build_hwcrypto(il, info, tx_cmd, skb, sta_id);
  1544. /* TODO need this for burst mode later on */
  1545. il4965_tx_cmd_build_basic(il, skb, tx_cmd, info, hdr, sta_id);
  1546. il4965_tx_cmd_build_rate(il, tx_cmd, info, sta, fc);
  1547. /*
  1548. * Use the first empty entry in this queue's command buffer array
  1549. * to contain the Tx command and MAC header concatenated together
  1550. * (payload data will be in another buffer).
  1551. * Size of this varies, due to varying MAC header length.
  1552. * If end is not dword aligned, we'll have 2 extra bytes at the end
  1553. * of the MAC header (device reads on dword boundaries).
  1554. * We'll tell device about this padding later.
  1555. */
  1556. len = sizeof(struct il_tx_cmd) + sizeof(struct il_cmd_header) + hdr_len;
  1557. firstlen = (len + 3) & ~3;
  1558. /* Tell NIC about any 2-byte padding after MAC header */
  1559. if (firstlen != len)
  1560. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  1561. /* Physical address of this Tx command's header (not MAC header!),
  1562. * within command buffer array. */
  1563. txcmd_phys =
  1564. pci_map_single(il->pci_dev, &out_cmd->hdr, firstlen,
  1565. PCI_DMA_BIDIRECTIONAL);
  1566. if (unlikely(pci_dma_mapping_error(il->pci_dev, txcmd_phys)))
  1567. goto drop_unlock;
  1568. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  1569. * if any (802.11 null frames have no payload). */
  1570. secondlen = skb->len - hdr_len;
  1571. if (secondlen > 0) {
  1572. phys_addr =
  1573. pci_map_single(il->pci_dev, skb->data + hdr_len, secondlen,
  1574. PCI_DMA_TODEVICE);
  1575. if (unlikely(pci_dma_mapping_error(il->pci_dev, phys_addr)))
  1576. goto drop_unlock;
  1577. }
  1578. /* Add buffer containing Tx command and MAC(!) header to TFD's
  1579. * first entry */
  1580. il->ops->txq_attach_buf_to_tfd(il, txq, txcmd_phys, firstlen, 1, 0);
  1581. dma_unmap_addr_set(out_meta, mapping, txcmd_phys);
  1582. dma_unmap_len_set(out_meta, len, firstlen);
  1583. if (secondlen)
  1584. il->ops->txq_attach_buf_to_tfd(il, txq, phys_addr, secondlen,
  1585. 0, 0);
  1586. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  1587. txq->need_update = 1;
  1588. } else {
  1589. wait_write_ptr = 1;
  1590. txq->need_update = 0;
  1591. }
  1592. scratch_phys =
  1593. txcmd_phys + sizeof(struct il_cmd_header) +
  1594. offsetof(struct il_tx_cmd, scratch);
  1595. /* take back ownership of DMA buffer to enable update */
  1596. pci_dma_sync_single_for_cpu(il->pci_dev, txcmd_phys, firstlen,
  1597. PCI_DMA_BIDIRECTIONAL);
  1598. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  1599. tx_cmd->dram_msb_ptr = il_get_dma_hi_addr(scratch_phys);
  1600. il_update_stats(il, true, fc, skb->len);
  1601. D_TX("sequence nr = 0X%x\n", le16_to_cpu(out_cmd->hdr.sequence));
  1602. D_TX("tx_flags = 0X%x\n", le32_to_cpu(tx_cmd->tx_flags));
  1603. il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd, sizeof(*tx_cmd));
  1604. il_print_hex_dump(il, IL_DL_TX, (u8 *) tx_cmd->hdr, hdr_len);
  1605. /* Set up entry for this TFD in Tx byte-count array */
  1606. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  1607. il->ops->txq_update_byte_cnt_tbl(il, txq, le16_to_cpu(tx_cmd->len));
  1608. pci_dma_sync_single_for_device(il->pci_dev, txcmd_phys, firstlen,
  1609. PCI_DMA_BIDIRECTIONAL);
  1610. /* Tell device the write idx *just past* this latest filled TFD */
  1611. q->write_ptr = il_queue_inc_wrap(q->write_ptr, q->n_bd);
  1612. il_txq_update_write_ptr(il, txq);
  1613. spin_unlock_irqrestore(&il->lock, flags);
  1614. /*
  1615. * At this point the frame is "transmitted" successfully
  1616. * and we will get a TX status notification eventually,
  1617. * regardless of the value of ret. "ret" only indicates
  1618. * whether or not we should update the write pointer.
  1619. */
  1620. /*
  1621. * Avoid atomic ops if it isn't an associated client.
  1622. * Also, if this is a packet for aggregation, don't
  1623. * increase the counter because the ucode will stop
  1624. * aggregation queues when their respective station
  1625. * goes to sleep.
  1626. */
  1627. if (sta_priv && sta_priv->client && !is_agg)
  1628. atomic_inc(&sta_priv->pending_frames);
  1629. if (il_queue_space(q) < q->high_mark && il->mac80211_registered) {
  1630. if (wait_write_ptr) {
  1631. spin_lock_irqsave(&il->lock, flags);
  1632. txq->need_update = 1;
  1633. il_txq_update_write_ptr(il, txq);
  1634. spin_unlock_irqrestore(&il->lock, flags);
  1635. } else {
  1636. il_stop_queue(il, txq);
  1637. }
  1638. }
  1639. return 0;
  1640. drop_unlock:
  1641. spin_unlock_irqrestore(&il->lock, flags);
  1642. return -1;
  1643. }
  1644. static inline int
  1645. il4965_alloc_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr, size_t size)
  1646. {
  1647. ptr->addr =
  1648. dma_alloc_coherent(&il->pci_dev->dev, size, &ptr->dma, GFP_KERNEL);
  1649. if (!ptr->addr)
  1650. return -ENOMEM;
  1651. ptr->size = size;
  1652. return 0;
  1653. }
  1654. static inline void
  1655. il4965_free_dma_ptr(struct il_priv *il, struct il_dma_ptr *ptr)
  1656. {
  1657. if (unlikely(!ptr->addr))
  1658. return;
  1659. dma_free_coherent(&il->pci_dev->dev, ptr->size, ptr->addr, ptr->dma);
  1660. memset(ptr, 0, sizeof(*ptr));
  1661. }
  1662. /**
  1663. * il4965_hw_txq_ctx_free - Free TXQ Context
  1664. *
  1665. * Destroy all TX DMA queues and structures
  1666. */
  1667. void
  1668. il4965_hw_txq_ctx_free(struct il_priv *il)
  1669. {
  1670. int txq_id;
  1671. /* Tx queues */
  1672. if (il->txq) {
  1673. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  1674. if (txq_id == il->cmd_queue)
  1675. il_cmd_queue_free(il);
  1676. else
  1677. il_tx_queue_free(il, txq_id);
  1678. }
  1679. il4965_free_dma_ptr(il, &il->kw);
  1680. il4965_free_dma_ptr(il, &il->scd_bc_tbls);
  1681. /* free tx queue structure */
  1682. il_free_txq_mem(il);
  1683. }
  1684. /**
  1685. * il4965_txq_ctx_alloc - allocate TX queue context
  1686. * Allocate all Tx DMA structures and initialize them
  1687. *
  1688. * @param il
  1689. * @return error code
  1690. */
  1691. int
  1692. il4965_txq_ctx_alloc(struct il_priv *il)
  1693. {
  1694. int ret, txq_id;
  1695. unsigned long flags;
  1696. /* Free all tx/cmd queues and keep-warm buffer */
  1697. il4965_hw_txq_ctx_free(il);
  1698. ret =
  1699. il4965_alloc_dma_ptr(il, &il->scd_bc_tbls,
  1700. il->hw_params.scd_bc_tbls_size);
  1701. if (ret) {
  1702. IL_ERR("Scheduler BC Table allocation failed\n");
  1703. goto error_bc_tbls;
  1704. }
  1705. /* Alloc keep-warm buffer */
  1706. ret = il4965_alloc_dma_ptr(il, &il->kw, IL_KW_SIZE);
  1707. if (ret) {
  1708. IL_ERR("Keep Warm allocation failed\n");
  1709. goto error_kw;
  1710. }
  1711. /* allocate tx queue structure */
  1712. ret = il_alloc_txq_mem(il);
  1713. if (ret)
  1714. goto error;
  1715. spin_lock_irqsave(&il->lock, flags);
  1716. /* Turn off all Tx DMA fifos */
  1717. il4965_txq_set_sched(il, 0);
  1718. /* Tell NIC where to find the "keep warm" buffer */
  1719. il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
  1720. spin_unlock_irqrestore(&il->lock, flags);
  1721. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  1722. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++) {
  1723. ret = il_tx_queue_init(il, txq_id);
  1724. if (ret) {
  1725. IL_ERR("Tx %d queue init failed\n", txq_id);
  1726. goto error;
  1727. }
  1728. }
  1729. return ret;
  1730. error:
  1731. il4965_hw_txq_ctx_free(il);
  1732. il4965_free_dma_ptr(il, &il->kw);
  1733. error_kw:
  1734. il4965_free_dma_ptr(il, &il->scd_bc_tbls);
  1735. error_bc_tbls:
  1736. return ret;
  1737. }
  1738. void
  1739. il4965_txq_ctx_reset(struct il_priv *il)
  1740. {
  1741. int txq_id;
  1742. unsigned long flags;
  1743. spin_lock_irqsave(&il->lock, flags);
  1744. /* Turn off all Tx DMA fifos */
  1745. il4965_txq_set_sched(il, 0);
  1746. /* Tell NIC where to find the "keep warm" buffer */
  1747. il_wr(il, FH49_KW_MEM_ADDR_REG, il->kw.dma >> 4);
  1748. spin_unlock_irqrestore(&il->lock, flags);
  1749. /* Alloc and init all Tx queues, including the command queue (#4) */
  1750. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  1751. il_tx_queue_reset(il, txq_id);
  1752. }
  1753. static void
  1754. il4965_txq_ctx_unmap(struct il_priv *il)
  1755. {
  1756. int txq_id;
  1757. if (!il->txq)
  1758. return;
  1759. /* Unmap DMA from host system and free skb's */
  1760. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  1761. if (txq_id == il->cmd_queue)
  1762. il_cmd_queue_unmap(il);
  1763. else
  1764. il_tx_queue_unmap(il, txq_id);
  1765. }
  1766. /**
  1767. * il4965_txq_ctx_stop - Stop all Tx DMA channels
  1768. */
  1769. void
  1770. il4965_txq_ctx_stop(struct il_priv *il)
  1771. {
  1772. int ch, ret;
  1773. _il_wr_prph(il, IL49_SCD_TXFACT, 0);
  1774. /* Stop each Tx DMA channel, and wait for it to be idle */
  1775. for (ch = 0; ch < il->hw_params.dma_chnl_num; ch++) {
  1776. _il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  1777. ret =
  1778. _il_poll_bit(il, FH49_TSSR_TX_STATUS_REG,
  1779. FH49_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  1780. FH49_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch),
  1781. 1000);
  1782. if (ret < 0)
  1783. IL_ERR("Timeout stopping DMA channel %d [0x%08x]",
  1784. ch, _il_rd(il, FH49_TSSR_TX_STATUS_REG));
  1785. }
  1786. }
  1787. /*
  1788. * Find first available (lowest unused) Tx Queue, mark it "active".
  1789. * Called only when finding queue for aggregation.
  1790. * Should never return anything < 7, because they should already
  1791. * be in use as EDCA AC (0-3), Command (4), reserved (5, 6)
  1792. */
  1793. static int
  1794. il4965_txq_ctx_activate_free(struct il_priv *il)
  1795. {
  1796. int txq_id;
  1797. for (txq_id = 0; txq_id < il->hw_params.max_txq_num; txq_id++)
  1798. if (!test_and_set_bit(txq_id, &il->txq_ctx_active_msk))
  1799. return txq_id;
  1800. return -1;
  1801. }
  1802. /**
  1803. * il4965_tx_queue_stop_scheduler - Stop queue, but keep configuration
  1804. */
  1805. static void
  1806. il4965_tx_queue_stop_scheduler(struct il_priv *il, u16 txq_id)
  1807. {
  1808. /* Simply stop the queue, but don't change any configuration;
  1809. * the SCD_ACT_EN bit is the write-enable mask for the ACTIVE bit. */
  1810. il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
  1811. (0 << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  1812. (1 << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACT_EN));
  1813. }
  1814. /**
  1815. * il4965_tx_queue_set_q2ratid - Map unique receiver/tid combination to a queue
  1816. */
  1817. static int
  1818. il4965_tx_queue_set_q2ratid(struct il_priv *il, u16 ra_tid, u16 txq_id)
  1819. {
  1820. u32 tbl_dw_addr;
  1821. u32 tbl_dw;
  1822. u16 scd_q2ratid;
  1823. scd_q2ratid = ra_tid & IL_SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  1824. tbl_dw_addr =
  1825. il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(txq_id);
  1826. tbl_dw = il_read_targ_mem(il, tbl_dw_addr);
  1827. if (txq_id & 0x1)
  1828. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  1829. else
  1830. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  1831. il_write_targ_mem(il, tbl_dw_addr, tbl_dw);
  1832. return 0;
  1833. }
  1834. /**
  1835. * il4965_tx_queue_agg_enable - Set up & enable aggregation for selected queue
  1836. *
  1837. * NOTE: txq_id must be greater than IL49_FIRST_AMPDU_QUEUE,
  1838. * i.e. it must be one of the higher queues used for aggregation
  1839. */
  1840. static int
  1841. il4965_txq_agg_enable(struct il_priv *il, int txq_id, int tx_fifo, int sta_id,
  1842. int tid, u16 ssn_idx)
  1843. {
  1844. unsigned long flags;
  1845. u16 ra_tid;
  1846. int ret;
  1847. if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
  1848. (IL49_FIRST_AMPDU_QUEUE +
  1849. il->cfg->num_of_ampdu_queues <= txq_id)) {
  1850. IL_WARN("queue number out of range: %d, must be %d to %d\n",
  1851. txq_id, IL49_FIRST_AMPDU_QUEUE,
  1852. IL49_FIRST_AMPDU_QUEUE +
  1853. il->cfg->num_of_ampdu_queues - 1);
  1854. return -EINVAL;
  1855. }
  1856. ra_tid = BUILD_RAxTID(sta_id, tid);
  1857. /* Modify device's station table to Tx this TID */
  1858. ret = il4965_sta_tx_modify_enable_tid(il, sta_id, tid);
  1859. if (ret)
  1860. return ret;
  1861. spin_lock_irqsave(&il->lock, flags);
  1862. /* Stop this Tx queue before configuring it */
  1863. il4965_tx_queue_stop_scheduler(il, txq_id);
  1864. /* Map receiver-address / traffic-ID to this queue */
  1865. il4965_tx_queue_set_q2ratid(il, ra_tid, txq_id);
  1866. /* Set this queue as a chain-building queue */
  1867. il_set_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  1868. /* Place first TFD at idx corresponding to start sequence number.
  1869. * Assumes that ssn_idx is valid (!= 0xFFF) */
  1870. il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  1871. il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  1872. il4965_set_wr_ptrs(il, txq_id, ssn_idx);
  1873. /* Set up Tx win size and frame limit for this queue */
  1874. il_write_targ_mem(il,
  1875. il->scd_base_addr +
  1876. IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id),
  1877. (SCD_WIN_SIZE << IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS)
  1878. & IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  1879. il_write_targ_mem(il,
  1880. il->scd_base_addr +
  1881. IL49_SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  1882. (SCD_FRAME_LIMIT <<
  1883. IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  1884. IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  1885. il_set_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  1886. /* Set up Status area in SRAM, map to Tx DMA/FIFO, activate the queue */
  1887. il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 1);
  1888. spin_unlock_irqrestore(&il->lock, flags);
  1889. return 0;
  1890. }
  1891. int
  1892. il4965_tx_agg_start(struct il_priv *il, struct ieee80211_vif *vif,
  1893. struct ieee80211_sta *sta, u16 tid, u16 * ssn)
  1894. {
  1895. int sta_id;
  1896. int tx_fifo;
  1897. int txq_id;
  1898. int ret;
  1899. unsigned long flags;
  1900. struct il_tid_data *tid_data;
  1901. /* FIXME: warning if tx fifo not found ? */
  1902. tx_fifo = il4965_get_fifo_from_tid(tid);
  1903. if (unlikely(tx_fifo < 0))
  1904. return tx_fifo;
  1905. D_HT("%s on ra = %pM tid = %d\n", __func__, sta->addr, tid);
  1906. sta_id = il_sta_id(sta);
  1907. if (sta_id == IL_INVALID_STATION) {
  1908. IL_ERR("Start AGG on invalid station\n");
  1909. return -ENXIO;
  1910. }
  1911. if (unlikely(tid >= MAX_TID_COUNT))
  1912. return -EINVAL;
  1913. if (il->stations[sta_id].tid[tid].agg.state != IL_AGG_OFF) {
  1914. IL_ERR("Start AGG when state is not IL_AGG_OFF !\n");
  1915. return -ENXIO;
  1916. }
  1917. txq_id = il4965_txq_ctx_activate_free(il);
  1918. if (txq_id == -1) {
  1919. IL_ERR("No free aggregation queue available\n");
  1920. return -ENXIO;
  1921. }
  1922. spin_lock_irqsave(&il->sta_lock, flags);
  1923. tid_data = &il->stations[sta_id].tid[tid];
  1924. *ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  1925. tid_data->agg.txq_id = txq_id;
  1926. il_set_swq_id(&il->txq[txq_id], il4965_get_ac_from_tid(tid), txq_id);
  1927. spin_unlock_irqrestore(&il->sta_lock, flags);
  1928. ret = il4965_txq_agg_enable(il, txq_id, tx_fifo, sta_id, tid, *ssn);
  1929. if (ret)
  1930. return ret;
  1931. spin_lock_irqsave(&il->sta_lock, flags);
  1932. tid_data = &il->stations[sta_id].tid[tid];
  1933. if (tid_data->tfds_in_queue == 0) {
  1934. D_HT("HW queue is empty\n");
  1935. tid_data->agg.state = IL_AGG_ON;
  1936. ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  1937. } else {
  1938. D_HT("HW queue is NOT empty: %d packets in HW queue\n",
  1939. tid_data->tfds_in_queue);
  1940. tid_data->agg.state = IL_EMPTYING_HW_QUEUE_ADDBA;
  1941. }
  1942. spin_unlock_irqrestore(&il->sta_lock, flags);
  1943. return ret;
  1944. }
  1945. /**
  1946. * txq_id must be greater than IL49_FIRST_AMPDU_QUEUE
  1947. * il->lock must be held by the caller
  1948. */
  1949. static int
  1950. il4965_txq_agg_disable(struct il_priv *il, u16 txq_id, u16 ssn_idx, u8 tx_fifo)
  1951. {
  1952. if ((IL49_FIRST_AMPDU_QUEUE > txq_id) ||
  1953. (IL49_FIRST_AMPDU_QUEUE +
  1954. il->cfg->num_of_ampdu_queues <= txq_id)) {
  1955. IL_WARN("queue number out of range: %d, must be %d to %d\n",
  1956. txq_id, IL49_FIRST_AMPDU_QUEUE,
  1957. IL49_FIRST_AMPDU_QUEUE +
  1958. il->cfg->num_of_ampdu_queues - 1);
  1959. return -EINVAL;
  1960. }
  1961. il4965_tx_queue_stop_scheduler(il, txq_id);
  1962. il_clear_bits_prph(il, IL49_SCD_QUEUECHAIN_SEL, (1 << txq_id));
  1963. il->txq[txq_id].q.read_ptr = (ssn_idx & 0xff);
  1964. il->txq[txq_id].q.write_ptr = (ssn_idx & 0xff);
  1965. /* supposes that ssn_idx is valid (!= 0xFFF) */
  1966. il4965_set_wr_ptrs(il, txq_id, ssn_idx);
  1967. il_clear_bits_prph(il, IL49_SCD_INTERRUPT_MASK, (1 << txq_id));
  1968. il_txq_ctx_deactivate(il, txq_id);
  1969. il4965_tx_queue_set_status(il, &il->txq[txq_id], tx_fifo, 0);
  1970. return 0;
  1971. }
  1972. int
  1973. il4965_tx_agg_stop(struct il_priv *il, struct ieee80211_vif *vif,
  1974. struct ieee80211_sta *sta, u16 tid)
  1975. {
  1976. int tx_fifo_id, txq_id, sta_id, ssn;
  1977. struct il_tid_data *tid_data;
  1978. int write_ptr, read_ptr;
  1979. unsigned long flags;
  1980. /* FIXME: warning if tx_fifo_id not found ? */
  1981. tx_fifo_id = il4965_get_fifo_from_tid(tid);
  1982. if (unlikely(tx_fifo_id < 0))
  1983. return tx_fifo_id;
  1984. sta_id = il_sta_id(sta);
  1985. if (sta_id == IL_INVALID_STATION) {
  1986. IL_ERR("Invalid station for AGG tid %d\n", tid);
  1987. return -ENXIO;
  1988. }
  1989. spin_lock_irqsave(&il->sta_lock, flags);
  1990. tid_data = &il->stations[sta_id].tid[tid];
  1991. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  1992. txq_id = tid_data->agg.txq_id;
  1993. switch (il->stations[sta_id].tid[tid].agg.state) {
  1994. case IL_EMPTYING_HW_QUEUE_ADDBA:
  1995. /*
  1996. * This can happen if the peer stops aggregation
  1997. * again before we've had a chance to drain the
  1998. * queue we selected previously, i.e. before the
  1999. * session was really started completely.
  2000. */
  2001. D_HT("AGG stop before setup done\n");
  2002. goto turn_off;
  2003. case IL_AGG_ON:
  2004. break;
  2005. default:
  2006. IL_WARN("Stopping AGG while state not ON or starting\n");
  2007. }
  2008. write_ptr = il->txq[txq_id].q.write_ptr;
  2009. read_ptr = il->txq[txq_id].q.read_ptr;
  2010. /* The queue is not empty */
  2011. if (write_ptr != read_ptr) {
  2012. D_HT("Stopping a non empty AGG HW QUEUE\n");
  2013. il->stations[sta_id].tid[tid].agg.state =
  2014. IL_EMPTYING_HW_QUEUE_DELBA;
  2015. spin_unlock_irqrestore(&il->sta_lock, flags);
  2016. return 0;
  2017. }
  2018. D_HT("HW queue is empty\n");
  2019. turn_off:
  2020. il->stations[sta_id].tid[tid].agg.state = IL_AGG_OFF;
  2021. /* do not restore/save irqs */
  2022. spin_unlock(&il->sta_lock);
  2023. spin_lock(&il->lock);
  2024. /*
  2025. * the only reason this call can fail is queue number out of range,
  2026. * which can happen if uCode is reloaded and all the station
  2027. * information are lost. if it is outside the range, there is no need
  2028. * to deactivate the uCode queue, just return "success" to allow
  2029. * mac80211 to clean up it own data.
  2030. */
  2031. il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo_id);
  2032. spin_unlock_irqrestore(&il->lock, flags);
  2033. ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
  2034. return 0;
  2035. }
  2036. int
  2037. il4965_txq_check_empty(struct il_priv *il, int sta_id, u8 tid, int txq_id)
  2038. {
  2039. struct il_queue *q = &il->txq[txq_id].q;
  2040. u8 *addr = il->stations[sta_id].sta.sta.addr;
  2041. struct il_tid_data *tid_data = &il->stations[sta_id].tid[tid];
  2042. lockdep_assert_held(&il->sta_lock);
  2043. switch (il->stations[sta_id].tid[tid].agg.state) {
  2044. case IL_EMPTYING_HW_QUEUE_DELBA:
  2045. /* We are reclaiming the last packet of the */
  2046. /* aggregated HW queue */
  2047. if (txq_id == tid_data->agg.txq_id &&
  2048. q->read_ptr == q->write_ptr) {
  2049. u16 ssn = IEEE80211_SEQ_TO_SN(tid_data->seq_number);
  2050. int tx_fifo = il4965_get_fifo_from_tid(tid);
  2051. D_HT("HW queue empty: continue DELBA flow\n");
  2052. il4965_txq_agg_disable(il, txq_id, ssn, tx_fifo);
  2053. tid_data->agg.state = IL_AGG_OFF;
  2054. ieee80211_stop_tx_ba_cb_irqsafe(il->vif, addr, tid);
  2055. }
  2056. break;
  2057. case IL_EMPTYING_HW_QUEUE_ADDBA:
  2058. /* We are reclaiming the last packet of the queue */
  2059. if (tid_data->tfds_in_queue == 0) {
  2060. D_HT("HW queue empty: continue ADDBA flow\n");
  2061. tid_data->agg.state = IL_AGG_ON;
  2062. ieee80211_start_tx_ba_cb_irqsafe(il->vif, addr, tid);
  2063. }
  2064. break;
  2065. }
  2066. return 0;
  2067. }
  2068. static void
  2069. il4965_non_agg_tx_status(struct il_priv *il, const u8 *addr1)
  2070. {
  2071. struct ieee80211_sta *sta;
  2072. struct il_station_priv *sta_priv;
  2073. rcu_read_lock();
  2074. sta = ieee80211_find_sta(il->vif, addr1);
  2075. if (sta) {
  2076. sta_priv = (void *)sta->drv_priv;
  2077. /* avoid atomic ops if this isn't a client */
  2078. if (sta_priv->client &&
  2079. atomic_dec_return(&sta_priv->pending_frames) == 0)
  2080. ieee80211_sta_block_awake(il->hw, sta, false);
  2081. }
  2082. rcu_read_unlock();
  2083. }
  2084. static void
  2085. il4965_tx_status(struct il_priv *il, struct sk_buff *skb, bool is_agg)
  2086. {
  2087. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  2088. if (!is_agg)
  2089. il4965_non_agg_tx_status(il, hdr->addr1);
  2090. ieee80211_tx_status_irqsafe(il->hw, skb);
  2091. }
  2092. int
  2093. il4965_tx_queue_reclaim(struct il_priv *il, int txq_id, int idx)
  2094. {
  2095. struct il_tx_queue *txq = &il->txq[txq_id];
  2096. struct il_queue *q = &txq->q;
  2097. int nfreed = 0;
  2098. struct ieee80211_hdr *hdr;
  2099. struct sk_buff *skb;
  2100. if (idx >= q->n_bd || il_queue_used(q, idx) == 0) {
  2101. IL_ERR("Read idx for DMA queue txq id (%d), idx %d, "
  2102. "is out of range [0-%d] %d %d.\n", txq_id, idx, q->n_bd,
  2103. q->write_ptr, q->read_ptr);
  2104. return 0;
  2105. }
  2106. for (idx = il_queue_inc_wrap(idx, q->n_bd); q->read_ptr != idx;
  2107. q->read_ptr = il_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  2108. skb = txq->skbs[txq->q.read_ptr];
  2109. if (WARN_ON_ONCE(skb == NULL))
  2110. continue;
  2111. hdr = (struct ieee80211_hdr *) skb->data;
  2112. if (ieee80211_is_data_qos(hdr->frame_control))
  2113. nfreed++;
  2114. il4965_tx_status(il, skb, txq_id >= IL4965_FIRST_AMPDU_QUEUE);
  2115. txq->skbs[txq->q.read_ptr] = NULL;
  2116. il->ops->txq_free_tfd(il, txq);
  2117. }
  2118. return nfreed;
  2119. }
  2120. /**
  2121. * il4965_tx_status_reply_compressed_ba - Update tx status from block-ack
  2122. *
  2123. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  2124. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  2125. */
  2126. static int
  2127. il4965_tx_status_reply_compressed_ba(struct il_priv *il, struct il_ht_agg *agg,
  2128. struct il_compressed_ba_resp *ba_resp)
  2129. {
  2130. int i, sh, ack;
  2131. u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
  2132. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  2133. int successes = 0;
  2134. struct ieee80211_tx_info *info;
  2135. u64 bitmap, sent_bitmap;
  2136. if (unlikely(!agg->wait_for_ba)) {
  2137. if (unlikely(ba_resp->bitmap))
  2138. IL_ERR("Received BA when not expected\n");
  2139. return -EINVAL;
  2140. }
  2141. /* Mark that the expected block-ack response arrived */
  2142. agg->wait_for_ba = 0;
  2143. D_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
  2144. /* Calculate shift to align block-ack bits with our Tx win bits */
  2145. sh = agg->start_idx - SEQ_TO_IDX(seq_ctl >> 4);
  2146. if (sh < 0) /* tbw something is wrong with indices */
  2147. sh += 0x100;
  2148. if (agg->frame_count > (64 - sh)) {
  2149. D_TX_REPLY("more frames than bitmap size");
  2150. return -1;
  2151. }
  2152. /* don't use 64-bit values for now */
  2153. bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
  2154. /* check for success or failure according to the
  2155. * transmitted bitmap and block-ack bitmap */
  2156. sent_bitmap = bitmap & agg->bitmap;
  2157. /* For each frame attempted in aggregation,
  2158. * update driver's record of tx frame's status. */
  2159. i = 0;
  2160. while (sent_bitmap) {
  2161. ack = sent_bitmap & 1ULL;
  2162. successes += ack;
  2163. D_TX_REPLY("%s ON i=%d idx=%d raw=%d\n", ack ? "ACK" : "NACK",
  2164. i, (agg->start_idx + i) & 0xff, agg->start_idx + i);
  2165. sent_bitmap >>= 1;
  2166. ++i;
  2167. }
  2168. D_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
  2169. info = IEEE80211_SKB_CB(il->txq[scd_flow].skbs[agg->start_idx]);
  2170. memset(&info->status, 0, sizeof(info->status));
  2171. info->flags |= IEEE80211_TX_STAT_ACK;
  2172. info->flags |= IEEE80211_TX_STAT_AMPDU;
  2173. info->status.ampdu_ack_len = successes;
  2174. info->status.ampdu_len = agg->frame_count;
  2175. il4965_hwrate_to_tx_control(il, agg->rate_n_flags, info);
  2176. return 0;
  2177. }
  2178. static inline bool
  2179. il4965_is_tx_success(u32 status)
  2180. {
  2181. status &= TX_STATUS_MSK;
  2182. return (status == TX_STATUS_SUCCESS || status == TX_STATUS_DIRECT_DONE);
  2183. }
  2184. static u8
  2185. il4965_find_station(struct il_priv *il, const u8 *addr)
  2186. {
  2187. int i;
  2188. int start = 0;
  2189. int ret = IL_INVALID_STATION;
  2190. unsigned long flags;
  2191. if (il->iw_mode == NL80211_IFTYPE_ADHOC)
  2192. start = IL_STA_ID;
  2193. if (is_broadcast_ether_addr(addr))
  2194. return il->hw_params.bcast_id;
  2195. spin_lock_irqsave(&il->sta_lock, flags);
  2196. for (i = start; i < il->hw_params.max_stations; i++)
  2197. if (il->stations[i].used &&
  2198. ether_addr_equal(il->stations[i].sta.sta.addr, addr)) {
  2199. ret = i;
  2200. goto out;
  2201. }
  2202. D_ASSOC("can not find STA %pM total %d\n", addr, il->num_stations);
  2203. out:
  2204. /*
  2205. * It may be possible that more commands interacting with stations
  2206. * arrive before we completed processing the adding of
  2207. * station
  2208. */
  2209. if (ret != IL_INVALID_STATION &&
  2210. (!(il->stations[ret].used & IL_STA_UCODE_ACTIVE) ||
  2211. ((il->stations[ret].used & IL_STA_UCODE_ACTIVE) &&
  2212. (il->stations[ret].used & IL_STA_UCODE_INPROGRESS)))) {
  2213. IL_ERR("Requested station info for sta %d before ready.\n",
  2214. ret);
  2215. ret = IL_INVALID_STATION;
  2216. }
  2217. spin_unlock_irqrestore(&il->sta_lock, flags);
  2218. return ret;
  2219. }
  2220. static int
  2221. il4965_get_ra_sta_id(struct il_priv *il, struct ieee80211_hdr *hdr)
  2222. {
  2223. if (il->iw_mode == NL80211_IFTYPE_STATION)
  2224. return IL_AP_ID;
  2225. else {
  2226. u8 *da = ieee80211_get_DA(hdr);
  2227. return il4965_find_station(il, da);
  2228. }
  2229. }
  2230. static inline u32
  2231. il4965_get_scd_ssn(struct il4965_tx_resp *tx_resp)
  2232. {
  2233. return le32_to_cpup(&tx_resp->u.status +
  2234. tx_resp->frame_count) & IEEE80211_MAX_SN;
  2235. }
  2236. static inline u32
  2237. il4965_tx_status_to_mac80211(u32 status)
  2238. {
  2239. status &= TX_STATUS_MSK;
  2240. switch (status) {
  2241. case TX_STATUS_SUCCESS:
  2242. case TX_STATUS_DIRECT_DONE:
  2243. return IEEE80211_TX_STAT_ACK;
  2244. case TX_STATUS_FAIL_DEST_PS:
  2245. return IEEE80211_TX_STAT_TX_FILTERED;
  2246. default:
  2247. return 0;
  2248. }
  2249. }
  2250. /**
  2251. * il4965_tx_status_reply_tx - Handle Tx response for frames in aggregation queue
  2252. */
  2253. static int
  2254. il4965_tx_status_reply_tx(struct il_priv *il, struct il_ht_agg *agg,
  2255. struct il4965_tx_resp *tx_resp, int txq_id,
  2256. u16 start_idx)
  2257. {
  2258. u16 status;
  2259. struct agg_tx_status *frame_status = tx_resp->u.agg_status;
  2260. struct ieee80211_tx_info *info = NULL;
  2261. struct ieee80211_hdr *hdr = NULL;
  2262. u32 rate_n_flags = le32_to_cpu(tx_resp->rate_n_flags);
  2263. int i, sh, idx;
  2264. u16 seq;
  2265. if (agg->wait_for_ba)
  2266. D_TX_REPLY("got tx response w/o block-ack\n");
  2267. agg->frame_count = tx_resp->frame_count;
  2268. agg->start_idx = start_idx;
  2269. agg->rate_n_flags = rate_n_flags;
  2270. agg->bitmap = 0;
  2271. /* num frames attempted by Tx command */
  2272. if (agg->frame_count == 1) {
  2273. /* Only one frame was attempted; no block-ack will arrive */
  2274. status = le16_to_cpu(frame_status[0].status);
  2275. idx = start_idx;
  2276. D_TX_REPLY("FrameCnt = %d, StartIdx=%d idx=%d\n",
  2277. agg->frame_count, agg->start_idx, idx);
  2278. info = IEEE80211_SKB_CB(il->txq[txq_id].skbs[idx]);
  2279. info->status.rates[0].count = tx_resp->failure_frame + 1;
  2280. info->flags &= ~IEEE80211_TX_CTL_AMPDU;
  2281. info->flags |= il4965_tx_status_to_mac80211(status);
  2282. il4965_hwrate_to_tx_control(il, rate_n_flags, info);
  2283. D_TX_REPLY("1 Frame 0x%x failure :%d\n", status & 0xff,
  2284. tx_resp->failure_frame);
  2285. D_TX_REPLY("Rate Info rate_n_flags=%x\n", rate_n_flags);
  2286. agg->wait_for_ba = 0;
  2287. } else {
  2288. /* Two or more frames were attempted; expect block-ack */
  2289. u64 bitmap = 0;
  2290. int start = agg->start_idx;
  2291. struct sk_buff *skb;
  2292. /* Construct bit-map of pending frames within Tx win */
  2293. for (i = 0; i < agg->frame_count; i++) {
  2294. u16 sc;
  2295. status = le16_to_cpu(frame_status[i].status);
  2296. seq = le16_to_cpu(frame_status[i].sequence);
  2297. idx = SEQ_TO_IDX(seq);
  2298. txq_id = SEQ_TO_QUEUE(seq);
  2299. if (status &
  2300. (AGG_TX_STATE_FEW_BYTES_MSK |
  2301. AGG_TX_STATE_ABORT_MSK))
  2302. continue;
  2303. D_TX_REPLY("FrameCnt = %d, txq_id=%d idx=%d\n",
  2304. agg->frame_count, txq_id, idx);
  2305. skb = il->txq[txq_id].skbs[idx];
  2306. if (WARN_ON_ONCE(skb == NULL))
  2307. return -1;
  2308. hdr = (struct ieee80211_hdr *) skb->data;
  2309. sc = le16_to_cpu(hdr->seq_ctrl);
  2310. if (idx != (IEEE80211_SEQ_TO_SN(sc) & 0xff)) {
  2311. IL_ERR("BUG_ON idx doesn't match seq control"
  2312. " idx=%d, seq_idx=%d, seq=%d\n", idx,
  2313. IEEE80211_SEQ_TO_SN(sc), hdr->seq_ctrl);
  2314. return -1;
  2315. }
  2316. D_TX_REPLY("AGG Frame i=%d idx %d seq=%d\n", i, idx,
  2317. IEEE80211_SEQ_TO_SN(sc));
  2318. sh = idx - start;
  2319. if (sh > 64) {
  2320. sh = (start - idx) + 0xff;
  2321. bitmap = bitmap << sh;
  2322. sh = 0;
  2323. start = idx;
  2324. } else if (sh < -64)
  2325. sh = 0xff - (start - idx);
  2326. else if (sh < 0) {
  2327. sh = start - idx;
  2328. start = idx;
  2329. bitmap = bitmap << sh;
  2330. sh = 0;
  2331. }
  2332. bitmap |= 1ULL << sh;
  2333. D_TX_REPLY("start=%d bitmap=0x%llx\n", start,
  2334. (unsigned long long)bitmap);
  2335. }
  2336. agg->bitmap = bitmap;
  2337. agg->start_idx = start;
  2338. D_TX_REPLY("Frames %d start_idx=%d bitmap=0x%llx\n",
  2339. agg->frame_count, agg->start_idx,
  2340. (unsigned long long)agg->bitmap);
  2341. if (bitmap)
  2342. agg->wait_for_ba = 1;
  2343. }
  2344. return 0;
  2345. }
  2346. /**
  2347. * il4965_hdl_tx - Handle standard (non-aggregation) Tx response
  2348. */
  2349. static void
  2350. il4965_hdl_tx(struct il_priv *il, struct il_rx_buf *rxb)
  2351. {
  2352. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2353. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  2354. int txq_id = SEQ_TO_QUEUE(sequence);
  2355. int idx = SEQ_TO_IDX(sequence);
  2356. struct il_tx_queue *txq = &il->txq[txq_id];
  2357. struct sk_buff *skb;
  2358. struct ieee80211_hdr *hdr;
  2359. struct ieee80211_tx_info *info;
  2360. struct il4965_tx_resp *tx_resp = (void *)&pkt->u.raw[0];
  2361. u32 status = le32_to_cpu(tx_resp->u.status);
  2362. int uninitialized_var(tid);
  2363. int sta_id;
  2364. int freed;
  2365. u8 *qc = NULL;
  2366. unsigned long flags;
  2367. if (idx >= txq->q.n_bd || il_queue_used(&txq->q, idx) == 0) {
  2368. IL_ERR("Read idx for DMA queue txq_id (%d) idx %d "
  2369. "is out of range [0-%d] %d %d\n", txq_id, idx,
  2370. txq->q.n_bd, txq->q.write_ptr, txq->q.read_ptr);
  2371. return;
  2372. }
  2373. txq->time_stamp = jiffies;
  2374. skb = txq->skbs[txq->q.read_ptr];
  2375. info = IEEE80211_SKB_CB(skb);
  2376. memset(&info->status, 0, sizeof(info->status));
  2377. hdr = (struct ieee80211_hdr *) skb->data;
  2378. if (ieee80211_is_data_qos(hdr->frame_control)) {
  2379. qc = ieee80211_get_qos_ctl(hdr);
  2380. tid = qc[0] & 0xf;
  2381. }
  2382. sta_id = il4965_get_ra_sta_id(il, hdr);
  2383. if (txq->sched_retry && unlikely(sta_id == IL_INVALID_STATION)) {
  2384. IL_ERR("Station not known\n");
  2385. return;
  2386. }
  2387. spin_lock_irqsave(&il->sta_lock, flags);
  2388. if (txq->sched_retry) {
  2389. const u32 scd_ssn = il4965_get_scd_ssn(tx_resp);
  2390. struct il_ht_agg *agg = NULL;
  2391. WARN_ON(!qc);
  2392. agg = &il->stations[sta_id].tid[tid].agg;
  2393. il4965_tx_status_reply_tx(il, agg, tx_resp, txq_id, idx);
  2394. /* check if BAR is needed */
  2395. if (tx_resp->frame_count == 1 &&
  2396. !il4965_is_tx_success(status))
  2397. info->flags |= IEEE80211_TX_STAT_AMPDU_NO_BACK;
  2398. if (txq->q.read_ptr != (scd_ssn & 0xff)) {
  2399. idx = il_queue_dec_wrap(scd_ssn & 0xff, txq->q.n_bd);
  2400. D_TX_REPLY("Retry scheduler reclaim scd_ssn "
  2401. "%d idx %d\n", scd_ssn, idx);
  2402. freed = il4965_tx_queue_reclaim(il, txq_id, idx);
  2403. if (qc)
  2404. il4965_free_tfds_in_queue(il, sta_id, tid,
  2405. freed);
  2406. if (il->mac80211_registered &&
  2407. il_queue_space(&txq->q) > txq->q.low_mark &&
  2408. agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
  2409. il_wake_queue(il, txq);
  2410. }
  2411. } else {
  2412. info->status.rates[0].count = tx_resp->failure_frame + 1;
  2413. info->flags |= il4965_tx_status_to_mac80211(status);
  2414. il4965_hwrate_to_tx_control(il,
  2415. le32_to_cpu(tx_resp->rate_n_flags),
  2416. info);
  2417. D_TX_REPLY("TXQ %d status %s (0x%08x) "
  2418. "rate_n_flags 0x%x retries %d\n", txq_id,
  2419. il4965_get_tx_fail_reason(status), status,
  2420. le32_to_cpu(tx_resp->rate_n_flags),
  2421. tx_resp->failure_frame);
  2422. freed = il4965_tx_queue_reclaim(il, txq_id, idx);
  2423. if (qc && likely(sta_id != IL_INVALID_STATION))
  2424. il4965_free_tfds_in_queue(il, sta_id, tid, freed);
  2425. else if (sta_id == IL_INVALID_STATION)
  2426. D_TX_REPLY("Station not known\n");
  2427. if (il->mac80211_registered &&
  2428. il_queue_space(&txq->q) > txq->q.low_mark)
  2429. il_wake_queue(il, txq);
  2430. }
  2431. if (qc && likely(sta_id != IL_INVALID_STATION))
  2432. il4965_txq_check_empty(il, sta_id, tid, txq_id);
  2433. il4965_check_abort_status(il, tx_resp->frame_count, status);
  2434. spin_unlock_irqrestore(&il->sta_lock, flags);
  2435. }
  2436. /**
  2437. * translate ucode response to mac80211 tx status control values
  2438. */
  2439. void
  2440. il4965_hwrate_to_tx_control(struct il_priv *il, u32 rate_n_flags,
  2441. struct ieee80211_tx_info *info)
  2442. {
  2443. struct ieee80211_tx_rate *r = &info->status.rates[0];
  2444. info->status.antenna =
  2445. ((rate_n_flags & RATE_MCS_ANT_ABC_MSK) >> RATE_MCS_ANT_POS);
  2446. if (rate_n_flags & RATE_MCS_HT_MSK)
  2447. r->flags |= IEEE80211_TX_RC_MCS;
  2448. if (rate_n_flags & RATE_MCS_GF_MSK)
  2449. r->flags |= IEEE80211_TX_RC_GREEN_FIELD;
  2450. if (rate_n_flags & RATE_MCS_HT40_MSK)
  2451. r->flags |= IEEE80211_TX_RC_40_MHZ_WIDTH;
  2452. if (rate_n_flags & RATE_MCS_DUP_MSK)
  2453. r->flags |= IEEE80211_TX_RC_DUP_DATA;
  2454. if (rate_n_flags & RATE_MCS_SGI_MSK)
  2455. r->flags |= IEEE80211_TX_RC_SHORT_GI;
  2456. r->idx = il4965_hwrate_to_mac80211_idx(rate_n_flags, info->band);
  2457. }
  2458. /**
  2459. * il4965_hdl_compressed_ba - Handler for N_COMPRESSED_BA
  2460. *
  2461. * Handles block-acknowledge notification from device, which reports success
  2462. * of frames sent via aggregation.
  2463. */
  2464. static void
  2465. il4965_hdl_compressed_ba(struct il_priv *il, struct il_rx_buf *rxb)
  2466. {
  2467. struct il_rx_pkt *pkt = rxb_addr(rxb);
  2468. struct il_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  2469. struct il_tx_queue *txq = NULL;
  2470. struct il_ht_agg *agg;
  2471. int idx;
  2472. int sta_id;
  2473. int tid;
  2474. unsigned long flags;
  2475. /* "flow" corresponds to Tx queue */
  2476. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  2477. /* "ssn" is start of block-ack Tx win, corresponds to idx
  2478. * (in Tx queue's circular buffer) of first TFD/frame in win */
  2479. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  2480. if (scd_flow >= il->hw_params.max_txq_num) {
  2481. IL_ERR("BUG_ON scd_flow is bigger than number of queues\n");
  2482. return;
  2483. }
  2484. txq = &il->txq[scd_flow];
  2485. sta_id = ba_resp->sta_id;
  2486. tid = ba_resp->tid;
  2487. agg = &il->stations[sta_id].tid[tid].agg;
  2488. if (unlikely(agg->txq_id != scd_flow)) {
  2489. /*
  2490. * FIXME: this is a uCode bug which need to be addressed,
  2491. * log the information and return for now!
  2492. * since it is possible happen very often and in order
  2493. * not to fill the syslog, don't enable the logging by default
  2494. */
  2495. D_TX_REPLY("BA scd_flow %d does not match txq_id %d\n",
  2496. scd_flow, agg->txq_id);
  2497. return;
  2498. }
  2499. /* Find idx just before block-ack win */
  2500. idx = il_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  2501. spin_lock_irqsave(&il->sta_lock, flags);
  2502. D_TX_REPLY("N_COMPRESSED_BA [%d] Received from %pM, " "sta_id = %d\n",
  2503. agg->wait_for_ba, (u8 *) &ba_resp->sta_addr_lo32,
  2504. ba_resp->sta_id);
  2505. D_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx," "scd_flow = "
  2506. "%d, scd_ssn = %d\n", ba_resp->tid, ba_resp->seq_ctl,
  2507. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  2508. ba_resp->scd_flow, ba_resp->scd_ssn);
  2509. D_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx\n", agg->start_idx,
  2510. (unsigned long long)agg->bitmap);
  2511. /* Update driver's record of ACK vs. not for each frame in win */
  2512. il4965_tx_status_reply_compressed_ba(il, agg, ba_resp);
  2513. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  2514. * block-ack win (we assume that they've been successfully
  2515. * transmitted ... if not, it's too late anyway). */
  2516. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
  2517. /* calculate mac80211 ampdu sw queue to wake */
  2518. int freed = il4965_tx_queue_reclaim(il, scd_flow, idx);
  2519. il4965_free_tfds_in_queue(il, sta_id, tid, freed);
  2520. if (il_queue_space(&txq->q) > txq->q.low_mark &&
  2521. il->mac80211_registered &&
  2522. agg->state != IL_EMPTYING_HW_QUEUE_DELBA)
  2523. il_wake_queue(il, txq);
  2524. il4965_txq_check_empty(il, sta_id, tid, scd_flow);
  2525. }
  2526. spin_unlock_irqrestore(&il->sta_lock, flags);
  2527. }
  2528. #ifdef CONFIG_IWLEGACY_DEBUG
  2529. const char *
  2530. il4965_get_tx_fail_reason(u32 status)
  2531. {
  2532. #define TX_STATUS_FAIL(x) case TX_STATUS_FAIL_ ## x: return #x
  2533. #define TX_STATUS_POSTPONE(x) case TX_STATUS_POSTPONE_ ## x: return #x
  2534. switch (status & TX_STATUS_MSK) {
  2535. case TX_STATUS_SUCCESS:
  2536. return "SUCCESS";
  2537. TX_STATUS_POSTPONE(DELAY);
  2538. TX_STATUS_POSTPONE(FEW_BYTES);
  2539. TX_STATUS_POSTPONE(QUIET_PERIOD);
  2540. TX_STATUS_POSTPONE(CALC_TTAK);
  2541. TX_STATUS_FAIL(INTERNAL_CROSSED_RETRY);
  2542. TX_STATUS_FAIL(SHORT_LIMIT);
  2543. TX_STATUS_FAIL(LONG_LIMIT);
  2544. TX_STATUS_FAIL(FIFO_UNDERRUN);
  2545. TX_STATUS_FAIL(DRAIN_FLOW);
  2546. TX_STATUS_FAIL(RFKILL_FLUSH);
  2547. TX_STATUS_FAIL(LIFE_EXPIRE);
  2548. TX_STATUS_FAIL(DEST_PS);
  2549. TX_STATUS_FAIL(HOST_ABORTED);
  2550. TX_STATUS_FAIL(BT_RETRY);
  2551. TX_STATUS_FAIL(STA_INVALID);
  2552. TX_STATUS_FAIL(FRAG_DROPPED);
  2553. TX_STATUS_FAIL(TID_DISABLE);
  2554. TX_STATUS_FAIL(FIFO_FLUSHED);
  2555. TX_STATUS_FAIL(INSUFFICIENT_CF_POLL);
  2556. TX_STATUS_FAIL(PASSIVE_NO_RX);
  2557. TX_STATUS_FAIL(NO_BEACON_ON_RADAR);
  2558. }
  2559. return "UNKNOWN";
  2560. #undef TX_STATUS_FAIL
  2561. #undef TX_STATUS_POSTPONE
  2562. }
  2563. #endif /* CONFIG_IWLEGACY_DEBUG */
  2564. static struct il_link_quality_cmd *
  2565. il4965_sta_alloc_lq(struct il_priv *il, u8 sta_id)
  2566. {
  2567. int i, r;
  2568. struct il_link_quality_cmd *link_cmd;
  2569. u32 rate_flags = 0;
  2570. __le32 rate_n_flags;
  2571. link_cmd = kzalloc(sizeof(struct il_link_quality_cmd), GFP_KERNEL);
  2572. if (!link_cmd) {
  2573. IL_ERR("Unable to allocate memory for LQ cmd.\n");
  2574. return NULL;
  2575. }
  2576. /* Set up the rate scaling to start at selected rate, fall back
  2577. * all the way down to 1M in IEEE order, and then spin on 1M */
  2578. if (il->band == IEEE80211_BAND_5GHZ)
  2579. r = RATE_6M_IDX;
  2580. else
  2581. r = RATE_1M_IDX;
  2582. if (r >= IL_FIRST_CCK_RATE && r <= IL_LAST_CCK_RATE)
  2583. rate_flags |= RATE_MCS_CCK_MSK;
  2584. rate_flags |=
  2585. il4965_first_antenna(il->hw_params.
  2586. valid_tx_ant) << RATE_MCS_ANT_POS;
  2587. rate_n_flags = cpu_to_le32(il_rates[r].plcp | rate_flags);
  2588. for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
  2589. link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
  2590. link_cmd->general_params.single_stream_ant_msk =
  2591. il4965_first_antenna(il->hw_params.valid_tx_ant);
  2592. link_cmd->general_params.dual_stream_ant_msk =
  2593. il->hw_params.valid_tx_ant & ~il4965_first_antenna(il->hw_params.
  2594. valid_tx_ant);
  2595. if (!link_cmd->general_params.dual_stream_ant_msk) {
  2596. link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
  2597. } else if (il4965_num_of_ant(il->hw_params.valid_tx_ant) == 2) {
  2598. link_cmd->general_params.dual_stream_ant_msk =
  2599. il->hw_params.valid_tx_ant;
  2600. }
  2601. link_cmd->agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
  2602. link_cmd->agg_params.agg_time_limit =
  2603. cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
  2604. link_cmd->sta_id = sta_id;
  2605. return link_cmd;
  2606. }
  2607. /*
  2608. * il4965_add_bssid_station - Add the special IBSS BSSID station
  2609. *
  2610. * Function sleeps.
  2611. */
  2612. int
  2613. il4965_add_bssid_station(struct il_priv *il, const u8 *addr, u8 *sta_id_r)
  2614. {
  2615. int ret;
  2616. u8 sta_id;
  2617. struct il_link_quality_cmd *link_cmd;
  2618. unsigned long flags;
  2619. if (sta_id_r)
  2620. *sta_id_r = IL_INVALID_STATION;
  2621. ret = il_add_station_common(il, addr, 0, NULL, &sta_id);
  2622. if (ret) {
  2623. IL_ERR("Unable to add station %pM\n", addr);
  2624. return ret;
  2625. }
  2626. if (sta_id_r)
  2627. *sta_id_r = sta_id;
  2628. spin_lock_irqsave(&il->sta_lock, flags);
  2629. il->stations[sta_id].used |= IL_STA_LOCAL;
  2630. spin_unlock_irqrestore(&il->sta_lock, flags);
  2631. /* Set up default rate scaling table in device's station table */
  2632. link_cmd = il4965_sta_alloc_lq(il, sta_id);
  2633. if (!link_cmd) {
  2634. IL_ERR("Unable to initialize rate scaling for station %pM.\n",
  2635. addr);
  2636. return -ENOMEM;
  2637. }
  2638. ret = il_send_lq_cmd(il, link_cmd, CMD_SYNC, true);
  2639. if (ret)
  2640. IL_ERR("Link quality command failed (%d)\n", ret);
  2641. spin_lock_irqsave(&il->sta_lock, flags);
  2642. il->stations[sta_id].lq = link_cmd;
  2643. spin_unlock_irqrestore(&il->sta_lock, flags);
  2644. return 0;
  2645. }
  2646. static int
  2647. il4965_static_wepkey_cmd(struct il_priv *il, bool send_if_empty)
  2648. {
  2649. int i;
  2650. u8 buff[sizeof(struct il_wep_cmd) +
  2651. sizeof(struct il_wep_key) * WEP_KEYS_MAX];
  2652. struct il_wep_cmd *wep_cmd = (struct il_wep_cmd *)buff;
  2653. size_t cmd_size = sizeof(struct il_wep_cmd);
  2654. struct il_host_cmd cmd = {
  2655. .id = C_WEPKEY,
  2656. .data = wep_cmd,
  2657. .flags = CMD_SYNC,
  2658. };
  2659. bool not_empty = false;
  2660. might_sleep();
  2661. memset(wep_cmd, 0,
  2662. cmd_size + (sizeof(struct il_wep_key) * WEP_KEYS_MAX));
  2663. for (i = 0; i < WEP_KEYS_MAX; i++) {
  2664. u8 key_size = il->_4965.wep_keys[i].key_size;
  2665. wep_cmd->key[i].key_idx = i;
  2666. if (key_size) {
  2667. wep_cmd->key[i].key_offset = i;
  2668. not_empty = true;
  2669. } else
  2670. wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
  2671. wep_cmd->key[i].key_size = key_size;
  2672. memcpy(&wep_cmd->key[i].key[3], il->_4965.wep_keys[i].key, key_size);
  2673. }
  2674. wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
  2675. wep_cmd->num_keys = WEP_KEYS_MAX;
  2676. cmd_size += sizeof(struct il_wep_key) * WEP_KEYS_MAX;
  2677. cmd.len = cmd_size;
  2678. if (not_empty || send_if_empty)
  2679. return il_send_cmd(il, &cmd);
  2680. else
  2681. return 0;
  2682. }
  2683. int
  2684. il4965_restore_default_wep_keys(struct il_priv *il)
  2685. {
  2686. lockdep_assert_held(&il->mutex);
  2687. return il4965_static_wepkey_cmd(il, false);
  2688. }
  2689. int
  2690. il4965_remove_default_wep_key(struct il_priv *il,
  2691. struct ieee80211_key_conf *keyconf)
  2692. {
  2693. int ret;
  2694. int idx = keyconf->keyidx;
  2695. lockdep_assert_held(&il->mutex);
  2696. D_WEP("Removing default WEP key: idx=%d\n", idx);
  2697. memset(&il->_4965.wep_keys[idx], 0, sizeof(struct il_wep_key));
  2698. if (il_is_rfkill(il)) {
  2699. D_WEP("Not sending C_WEPKEY command due to RFKILL.\n");
  2700. /* but keys in device are clear anyway so return success */
  2701. return 0;
  2702. }
  2703. ret = il4965_static_wepkey_cmd(il, 1);
  2704. D_WEP("Remove default WEP key: idx=%d ret=%d\n", idx, ret);
  2705. return ret;
  2706. }
  2707. int
  2708. il4965_set_default_wep_key(struct il_priv *il,
  2709. struct ieee80211_key_conf *keyconf)
  2710. {
  2711. int ret;
  2712. int len = keyconf->keylen;
  2713. int idx = keyconf->keyidx;
  2714. lockdep_assert_held(&il->mutex);
  2715. if (len != WEP_KEY_LEN_128 && len != WEP_KEY_LEN_64) {
  2716. D_WEP("Bad WEP key length %d\n", keyconf->keylen);
  2717. return -EINVAL;
  2718. }
  2719. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  2720. keyconf->hw_key_idx = HW_KEY_DEFAULT;
  2721. il->stations[IL_AP_ID].keyinfo.cipher = keyconf->cipher;
  2722. il->_4965.wep_keys[idx].key_size = len;
  2723. memcpy(&il->_4965.wep_keys[idx].key, &keyconf->key, len);
  2724. ret = il4965_static_wepkey_cmd(il, false);
  2725. D_WEP("Set default WEP key: len=%d idx=%d ret=%d\n", len, idx, ret);
  2726. return ret;
  2727. }
  2728. static int
  2729. il4965_set_wep_dynamic_key_info(struct il_priv *il,
  2730. struct ieee80211_key_conf *keyconf, u8 sta_id)
  2731. {
  2732. unsigned long flags;
  2733. __le16 key_flags = 0;
  2734. struct il_addsta_cmd sta_cmd;
  2735. lockdep_assert_held(&il->mutex);
  2736. keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
  2737. key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
  2738. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  2739. key_flags &= ~STA_KEY_FLG_INVALID;
  2740. if (keyconf->keylen == WEP_KEY_LEN_128)
  2741. key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
  2742. if (sta_id == il->hw_params.bcast_id)
  2743. key_flags |= STA_KEY_MULTICAST_MSK;
  2744. spin_lock_irqsave(&il->sta_lock, flags);
  2745. il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  2746. il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  2747. il->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
  2748. memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
  2749. memcpy(&il->stations[sta_id].sta.key.key[3], keyconf->key,
  2750. keyconf->keylen);
  2751. if ((il->stations[sta_id].sta.key.
  2752. key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
  2753. il->stations[sta_id].sta.key.key_offset =
  2754. il_get_free_ucode_key_idx(il);
  2755. /* else, we are overriding an existing key => no need to allocated room
  2756. * in uCode. */
  2757. WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  2758. "no space for a new key");
  2759. il->stations[sta_id].sta.key.key_flags = key_flags;
  2760. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  2761. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  2762. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  2763. sizeof(struct il_addsta_cmd));
  2764. spin_unlock_irqrestore(&il->sta_lock, flags);
  2765. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  2766. }
  2767. static int
  2768. il4965_set_ccmp_dynamic_key_info(struct il_priv *il,
  2769. struct ieee80211_key_conf *keyconf, u8 sta_id)
  2770. {
  2771. unsigned long flags;
  2772. __le16 key_flags = 0;
  2773. struct il_addsta_cmd sta_cmd;
  2774. lockdep_assert_held(&il->mutex);
  2775. key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
  2776. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  2777. key_flags &= ~STA_KEY_FLG_INVALID;
  2778. if (sta_id == il->hw_params.bcast_id)
  2779. key_flags |= STA_KEY_MULTICAST_MSK;
  2780. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2781. spin_lock_irqsave(&il->sta_lock, flags);
  2782. il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  2783. il->stations[sta_id].keyinfo.keylen = keyconf->keylen;
  2784. memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, keyconf->keylen);
  2785. memcpy(il->stations[sta_id].sta.key.key, keyconf->key, keyconf->keylen);
  2786. if ((il->stations[sta_id].sta.key.
  2787. key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
  2788. il->stations[sta_id].sta.key.key_offset =
  2789. il_get_free_ucode_key_idx(il);
  2790. /* else, we are overriding an existing key => no need to allocated room
  2791. * in uCode. */
  2792. WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  2793. "no space for a new key");
  2794. il->stations[sta_id].sta.key.key_flags = key_flags;
  2795. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  2796. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  2797. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  2798. sizeof(struct il_addsta_cmd));
  2799. spin_unlock_irqrestore(&il->sta_lock, flags);
  2800. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  2801. }
  2802. static int
  2803. il4965_set_tkip_dynamic_key_info(struct il_priv *il,
  2804. struct ieee80211_key_conf *keyconf, u8 sta_id)
  2805. {
  2806. unsigned long flags;
  2807. int ret = 0;
  2808. __le16 key_flags = 0;
  2809. key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
  2810. key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
  2811. key_flags &= ~STA_KEY_FLG_INVALID;
  2812. if (sta_id == il->hw_params.bcast_id)
  2813. key_flags |= STA_KEY_MULTICAST_MSK;
  2814. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
  2815. keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
  2816. spin_lock_irqsave(&il->sta_lock, flags);
  2817. il->stations[sta_id].keyinfo.cipher = keyconf->cipher;
  2818. il->stations[sta_id].keyinfo.keylen = 16;
  2819. if ((il->stations[sta_id].sta.key.
  2820. key_flags & STA_KEY_FLG_ENCRYPT_MSK) == STA_KEY_FLG_NO_ENC)
  2821. il->stations[sta_id].sta.key.key_offset =
  2822. il_get_free_ucode_key_idx(il);
  2823. /* else, we are overriding an existing key => no need to allocated room
  2824. * in uCode. */
  2825. WARN(il->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
  2826. "no space for a new key");
  2827. il->stations[sta_id].sta.key.key_flags = key_flags;
  2828. /* This copy is acutally not needed: we get the key with each TX */
  2829. memcpy(il->stations[sta_id].keyinfo.key, keyconf->key, 16);
  2830. memcpy(il->stations[sta_id].sta.key.key, keyconf->key, 16);
  2831. spin_unlock_irqrestore(&il->sta_lock, flags);
  2832. return ret;
  2833. }
  2834. void
  2835. il4965_update_tkip_key(struct il_priv *il, struct ieee80211_key_conf *keyconf,
  2836. struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
  2837. {
  2838. u8 sta_id;
  2839. unsigned long flags;
  2840. int i;
  2841. if (il_scan_cancel(il)) {
  2842. /* cancel scan failed, just live w/ bad key and rely
  2843. briefly on SW decryption */
  2844. return;
  2845. }
  2846. sta_id = il_sta_id_or_broadcast(il, sta);
  2847. if (sta_id == IL_INVALID_STATION)
  2848. return;
  2849. spin_lock_irqsave(&il->sta_lock, flags);
  2850. il->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
  2851. for (i = 0; i < 5; i++)
  2852. il->stations[sta_id].sta.key.tkip_rx_ttak[i] =
  2853. cpu_to_le16(phase1key[i]);
  2854. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  2855. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  2856. il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
  2857. spin_unlock_irqrestore(&il->sta_lock, flags);
  2858. }
  2859. int
  2860. il4965_remove_dynamic_key(struct il_priv *il,
  2861. struct ieee80211_key_conf *keyconf, u8 sta_id)
  2862. {
  2863. unsigned long flags;
  2864. u16 key_flags;
  2865. u8 keyidx;
  2866. struct il_addsta_cmd sta_cmd;
  2867. lockdep_assert_held(&il->mutex);
  2868. il->_4965.key_mapping_keys--;
  2869. spin_lock_irqsave(&il->sta_lock, flags);
  2870. key_flags = le16_to_cpu(il->stations[sta_id].sta.key.key_flags);
  2871. keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
  2872. D_WEP("Remove dynamic key: idx=%d sta=%d\n", keyconf->keyidx, sta_id);
  2873. if (keyconf->keyidx != keyidx) {
  2874. /* We need to remove a key with idx different that the one
  2875. * in the uCode. This means that the key we need to remove has
  2876. * been replaced by another one with different idx.
  2877. * Don't do anything and return ok
  2878. */
  2879. spin_unlock_irqrestore(&il->sta_lock, flags);
  2880. return 0;
  2881. }
  2882. if (il->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_INVALID) {
  2883. IL_WARN("Removing wrong key %d 0x%x\n", keyconf->keyidx,
  2884. key_flags);
  2885. spin_unlock_irqrestore(&il->sta_lock, flags);
  2886. return 0;
  2887. }
  2888. if (!test_and_clear_bit
  2889. (il->stations[sta_id].sta.key.key_offset, &il->ucode_key_table))
  2890. IL_ERR("idx %d not used in uCode key table.\n",
  2891. il->stations[sta_id].sta.key.key_offset);
  2892. memset(&il->stations[sta_id].keyinfo, 0, sizeof(struct il_hw_key));
  2893. memset(&il->stations[sta_id].sta.key, 0, sizeof(struct il4965_keyinfo));
  2894. il->stations[sta_id].sta.key.key_flags =
  2895. STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
  2896. il->stations[sta_id].sta.key.key_offset = keyconf->hw_key_idx;
  2897. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
  2898. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  2899. if (il_is_rfkill(il)) {
  2900. D_WEP
  2901. ("Not sending C_ADD_STA command because RFKILL enabled.\n");
  2902. spin_unlock_irqrestore(&il->sta_lock, flags);
  2903. return 0;
  2904. }
  2905. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  2906. sizeof(struct il_addsta_cmd));
  2907. spin_unlock_irqrestore(&il->sta_lock, flags);
  2908. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  2909. }
  2910. int
  2911. il4965_set_dynamic_key(struct il_priv *il, struct ieee80211_key_conf *keyconf,
  2912. u8 sta_id)
  2913. {
  2914. int ret;
  2915. lockdep_assert_held(&il->mutex);
  2916. il->_4965.key_mapping_keys++;
  2917. keyconf->hw_key_idx = HW_KEY_DYNAMIC;
  2918. switch (keyconf->cipher) {
  2919. case WLAN_CIPHER_SUITE_CCMP:
  2920. ret =
  2921. il4965_set_ccmp_dynamic_key_info(il, keyconf, sta_id);
  2922. break;
  2923. case WLAN_CIPHER_SUITE_TKIP:
  2924. ret =
  2925. il4965_set_tkip_dynamic_key_info(il, keyconf, sta_id);
  2926. break;
  2927. case WLAN_CIPHER_SUITE_WEP40:
  2928. case WLAN_CIPHER_SUITE_WEP104:
  2929. ret = il4965_set_wep_dynamic_key_info(il, keyconf, sta_id);
  2930. break;
  2931. default:
  2932. IL_ERR("Unknown alg: %s cipher = %x\n", __func__,
  2933. keyconf->cipher);
  2934. ret = -EINVAL;
  2935. }
  2936. D_WEP("Set dynamic key: cipher=%x len=%d idx=%d sta=%d ret=%d\n",
  2937. keyconf->cipher, keyconf->keylen, keyconf->keyidx, sta_id, ret);
  2938. return ret;
  2939. }
  2940. /**
  2941. * il4965_alloc_bcast_station - add broadcast station into driver's station table.
  2942. *
  2943. * This adds the broadcast station into the driver's station table
  2944. * and marks it driver active, so that it will be restored to the
  2945. * device at the next best time.
  2946. */
  2947. int
  2948. il4965_alloc_bcast_station(struct il_priv *il)
  2949. {
  2950. struct il_link_quality_cmd *link_cmd;
  2951. unsigned long flags;
  2952. u8 sta_id;
  2953. spin_lock_irqsave(&il->sta_lock, flags);
  2954. sta_id = il_prep_station(il, il_bcast_addr, false, NULL);
  2955. if (sta_id == IL_INVALID_STATION) {
  2956. IL_ERR("Unable to prepare broadcast station\n");
  2957. spin_unlock_irqrestore(&il->sta_lock, flags);
  2958. return -EINVAL;
  2959. }
  2960. il->stations[sta_id].used |= IL_STA_DRIVER_ACTIVE;
  2961. il->stations[sta_id].used |= IL_STA_BCAST;
  2962. spin_unlock_irqrestore(&il->sta_lock, flags);
  2963. link_cmd = il4965_sta_alloc_lq(il, sta_id);
  2964. if (!link_cmd) {
  2965. IL_ERR
  2966. ("Unable to initialize rate scaling for bcast station.\n");
  2967. return -ENOMEM;
  2968. }
  2969. spin_lock_irqsave(&il->sta_lock, flags);
  2970. il->stations[sta_id].lq = link_cmd;
  2971. spin_unlock_irqrestore(&il->sta_lock, flags);
  2972. return 0;
  2973. }
  2974. /**
  2975. * il4965_update_bcast_station - update broadcast station's LQ command
  2976. *
  2977. * Only used by iwl4965. Placed here to have all bcast station management
  2978. * code together.
  2979. */
  2980. static int
  2981. il4965_update_bcast_station(struct il_priv *il)
  2982. {
  2983. unsigned long flags;
  2984. struct il_link_quality_cmd *link_cmd;
  2985. u8 sta_id = il->hw_params.bcast_id;
  2986. link_cmd = il4965_sta_alloc_lq(il, sta_id);
  2987. if (!link_cmd) {
  2988. IL_ERR("Unable to initialize rate scaling for bcast sta.\n");
  2989. return -ENOMEM;
  2990. }
  2991. spin_lock_irqsave(&il->sta_lock, flags);
  2992. if (il->stations[sta_id].lq)
  2993. kfree(il->stations[sta_id].lq);
  2994. else
  2995. D_INFO("Bcast sta rate scaling has not been initialized.\n");
  2996. il->stations[sta_id].lq = link_cmd;
  2997. spin_unlock_irqrestore(&il->sta_lock, flags);
  2998. return 0;
  2999. }
  3000. int
  3001. il4965_update_bcast_stations(struct il_priv *il)
  3002. {
  3003. return il4965_update_bcast_station(il);
  3004. }
  3005. /**
  3006. * il4965_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
  3007. */
  3008. int
  3009. il4965_sta_tx_modify_enable_tid(struct il_priv *il, int sta_id, int tid)
  3010. {
  3011. unsigned long flags;
  3012. struct il_addsta_cmd sta_cmd;
  3013. lockdep_assert_held(&il->mutex);
  3014. /* Remove "disable" flag, to enable Tx for this TID */
  3015. spin_lock_irqsave(&il->sta_lock, flags);
  3016. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
  3017. il->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
  3018. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3019. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  3020. sizeof(struct il_addsta_cmd));
  3021. spin_unlock_irqrestore(&il->sta_lock, flags);
  3022. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  3023. }
  3024. int
  3025. il4965_sta_rx_agg_start(struct il_priv *il, struct ieee80211_sta *sta, int tid,
  3026. u16 ssn)
  3027. {
  3028. unsigned long flags;
  3029. int sta_id;
  3030. struct il_addsta_cmd sta_cmd;
  3031. lockdep_assert_held(&il->mutex);
  3032. sta_id = il_sta_id(sta);
  3033. if (sta_id == IL_INVALID_STATION)
  3034. return -ENXIO;
  3035. spin_lock_irqsave(&il->sta_lock, flags);
  3036. il->stations[sta_id].sta.station_flags_msk = 0;
  3037. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
  3038. il->stations[sta_id].sta.add_immediate_ba_tid = (u8) tid;
  3039. il->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
  3040. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3041. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  3042. sizeof(struct il_addsta_cmd));
  3043. spin_unlock_irqrestore(&il->sta_lock, flags);
  3044. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  3045. }
  3046. int
  3047. il4965_sta_rx_agg_stop(struct il_priv *il, struct ieee80211_sta *sta, int tid)
  3048. {
  3049. unsigned long flags;
  3050. int sta_id;
  3051. struct il_addsta_cmd sta_cmd;
  3052. lockdep_assert_held(&il->mutex);
  3053. sta_id = il_sta_id(sta);
  3054. if (sta_id == IL_INVALID_STATION) {
  3055. IL_ERR("Invalid station for AGG tid %d\n", tid);
  3056. return -ENXIO;
  3057. }
  3058. spin_lock_irqsave(&il->sta_lock, flags);
  3059. il->stations[sta_id].sta.station_flags_msk = 0;
  3060. il->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
  3061. il->stations[sta_id].sta.remove_immediate_ba_tid = (u8) tid;
  3062. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3063. memcpy(&sta_cmd, &il->stations[sta_id].sta,
  3064. sizeof(struct il_addsta_cmd));
  3065. spin_unlock_irqrestore(&il->sta_lock, flags);
  3066. return il_send_add_sta(il, &sta_cmd, CMD_SYNC);
  3067. }
  3068. void
  3069. il4965_sta_modify_sleep_tx_count(struct il_priv *il, int sta_id, int cnt)
  3070. {
  3071. unsigned long flags;
  3072. spin_lock_irqsave(&il->sta_lock, flags);
  3073. il->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
  3074. il->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
  3075. il->stations[sta_id].sta.sta.modify_mask =
  3076. STA_MODIFY_SLEEP_TX_COUNT_MSK;
  3077. il->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
  3078. il->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
  3079. il_send_add_sta(il, &il->stations[sta_id].sta, CMD_ASYNC);
  3080. spin_unlock_irqrestore(&il->sta_lock, flags);
  3081. }
  3082. void
  3083. il4965_update_chain_flags(struct il_priv *il)
  3084. {
  3085. if (il->ops->set_rxon_chain) {
  3086. il->ops->set_rxon_chain(il);
  3087. if (il->active.rx_chain != il->staging.rx_chain)
  3088. il_commit_rxon(il);
  3089. }
  3090. }
  3091. static void
  3092. il4965_clear_free_frames(struct il_priv *il)
  3093. {
  3094. struct list_head *element;
  3095. D_INFO("%d frames on pre-allocated heap on clear.\n", il->frames_count);
  3096. while (!list_empty(&il->free_frames)) {
  3097. element = il->free_frames.next;
  3098. list_del(element);
  3099. kfree(list_entry(element, struct il_frame, list));
  3100. il->frames_count--;
  3101. }
  3102. if (il->frames_count) {
  3103. IL_WARN("%d frames still in use. Did we lose one?\n",
  3104. il->frames_count);
  3105. il->frames_count = 0;
  3106. }
  3107. }
  3108. static struct il_frame *
  3109. il4965_get_free_frame(struct il_priv *il)
  3110. {
  3111. struct il_frame *frame;
  3112. struct list_head *element;
  3113. if (list_empty(&il->free_frames)) {
  3114. frame = kzalloc(sizeof(*frame), GFP_KERNEL);
  3115. if (!frame) {
  3116. IL_ERR("Could not allocate frame!\n");
  3117. return NULL;
  3118. }
  3119. il->frames_count++;
  3120. return frame;
  3121. }
  3122. element = il->free_frames.next;
  3123. list_del(element);
  3124. return list_entry(element, struct il_frame, list);
  3125. }
  3126. static void
  3127. il4965_free_frame(struct il_priv *il, struct il_frame *frame)
  3128. {
  3129. memset(frame, 0, sizeof(*frame));
  3130. list_add(&frame->list, &il->free_frames);
  3131. }
  3132. static u32
  3133. il4965_fill_beacon_frame(struct il_priv *il, struct ieee80211_hdr *hdr,
  3134. int left)
  3135. {
  3136. lockdep_assert_held(&il->mutex);
  3137. if (!il->beacon_skb)
  3138. return 0;
  3139. if (il->beacon_skb->len > left)
  3140. return 0;
  3141. memcpy(hdr, il->beacon_skb->data, il->beacon_skb->len);
  3142. return il->beacon_skb->len;
  3143. }
  3144. /* Parse the beacon frame to find the TIM element and set tim_idx & tim_size */
  3145. static void
  3146. il4965_set_beacon_tim(struct il_priv *il,
  3147. struct il_tx_beacon_cmd *tx_beacon_cmd, u8 * beacon,
  3148. u32 frame_size)
  3149. {
  3150. u16 tim_idx;
  3151. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
  3152. /*
  3153. * The idx is relative to frame start but we start looking at the
  3154. * variable-length part of the beacon.
  3155. */
  3156. tim_idx = mgmt->u.beacon.variable - beacon;
  3157. /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
  3158. while ((tim_idx < (frame_size - 2)) &&
  3159. (beacon[tim_idx] != WLAN_EID_TIM))
  3160. tim_idx += beacon[tim_idx + 1] + 2;
  3161. /* If TIM field was found, set variables */
  3162. if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
  3163. tx_beacon_cmd->tim_idx = cpu_to_le16(tim_idx);
  3164. tx_beacon_cmd->tim_size = beacon[tim_idx + 1];
  3165. } else
  3166. IL_WARN("Unable to find TIM Element in beacon\n");
  3167. }
  3168. static unsigned int
  3169. il4965_hw_get_beacon_cmd(struct il_priv *il, struct il_frame *frame)
  3170. {
  3171. struct il_tx_beacon_cmd *tx_beacon_cmd;
  3172. u32 frame_size;
  3173. u32 rate_flags;
  3174. u32 rate;
  3175. /*
  3176. * We have to set up the TX command, the TX Beacon command, and the
  3177. * beacon contents.
  3178. */
  3179. lockdep_assert_held(&il->mutex);
  3180. if (!il->beacon_enabled) {
  3181. IL_ERR("Trying to build beacon without beaconing enabled\n");
  3182. return 0;
  3183. }
  3184. /* Initialize memory */
  3185. tx_beacon_cmd = &frame->u.beacon;
  3186. memset(tx_beacon_cmd, 0, sizeof(*tx_beacon_cmd));
  3187. /* Set up TX beacon contents */
  3188. frame_size =
  3189. il4965_fill_beacon_frame(il, tx_beacon_cmd->frame,
  3190. sizeof(frame->u) - sizeof(*tx_beacon_cmd));
  3191. if (WARN_ON_ONCE(frame_size > MAX_MPDU_SIZE))
  3192. return 0;
  3193. if (!frame_size)
  3194. return 0;
  3195. /* Set up TX command fields */
  3196. tx_beacon_cmd->tx.len = cpu_to_le16((u16) frame_size);
  3197. tx_beacon_cmd->tx.sta_id = il->hw_params.bcast_id;
  3198. tx_beacon_cmd->tx.stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  3199. tx_beacon_cmd->tx.tx_flags =
  3200. TX_CMD_FLG_SEQ_CTL_MSK | TX_CMD_FLG_TSF_MSK |
  3201. TX_CMD_FLG_STA_RATE_MSK;
  3202. /* Set up TX beacon command fields */
  3203. il4965_set_beacon_tim(il, tx_beacon_cmd, (u8 *) tx_beacon_cmd->frame,
  3204. frame_size);
  3205. /* Set up packet rate and flags */
  3206. rate = il_get_lowest_plcp(il);
  3207. il4965_toggle_tx_ant(il, &il->mgmt_tx_ant, il->hw_params.valid_tx_ant);
  3208. rate_flags = BIT(il->mgmt_tx_ant) << RATE_MCS_ANT_POS;
  3209. if ((rate >= IL_FIRST_CCK_RATE) && (rate <= IL_LAST_CCK_RATE))
  3210. rate_flags |= RATE_MCS_CCK_MSK;
  3211. tx_beacon_cmd->tx.rate_n_flags = cpu_to_le32(rate | rate_flags);
  3212. return sizeof(*tx_beacon_cmd) + frame_size;
  3213. }
  3214. int
  3215. il4965_send_beacon_cmd(struct il_priv *il)
  3216. {
  3217. struct il_frame *frame;
  3218. unsigned int frame_size;
  3219. int rc;
  3220. frame = il4965_get_free_frame(il);
  3221. if (!frame) {
  3222. IL_ERR("Could not obtain free frame buffer for beacon "
  3223. "command.\n");
  3224. return -ENOMEM;
  3225. }
  3226. frame_size = il4965_hw_get_beacon_cmd(il, frame);
  3227. if (!frame_size) {
  3228. IL_ERR("Error configuring the beacon command\n");
  3229. il4965_free_frame(il, frame);
  3230. return -EINVAL;
  3231. }
  3232. rc = il_send_cmd_pdu(il, C_TX_BEACON, frame_size, &frame->u.cmd[0]);
  3233. il4965_free_frame(il, frame);
  3234. return rc;
  3235. }
  3236. static inline dma_addr_t
  3237. il4965_tfd_tb_get_addr(struct il_tfd *tfd, u8 idx)
  3238. {
  3239. struct il_tfd_tb *tb = &tfd->tbs[idx];
  3240. dma_addr_t addr = get_unaligned_le32(&tb->lo);
  3241. if (sizeof(dma_addr_t) > sizeof(u32))
  3242. addr |=
  3243. ((dma_addr_t) (le16_to_cpu(tb->hi_n_len) & 0xF) << 16) <<
  3244. 16;
  3245. return addr;
  3246. }
  3247. static inline u16
  3248. il4965_tfd_tb_get_len(struct il_tfd *tfd, u8 idx)
  3249. {
  3250. struct il_tfd_tb *tb = &tfd->tbs[idx];
  3251. return le16_to_cpu(tb->hi_n_len) >> 4;
  3252. }
  3253. static inline void
  3254. il4965_tfd_set_tb(struct il_tfd *tfd, u8 idx, dma_addr_t addr, u16 len)
  3255. {
  3256. struct il_tfd_tb *tb = &tfd->tbs[idx];
  3257. u16 hi_n_len = len << 4;
  3258. put_unaligned_le32(addr, &tb->lo);
  3259. if (sizeof(dma_addr_t) > sizeof(u32))
  3260. hi_n_len |= ((addr >> 16) >> 16) & 0xF;
  3261. tb->hi_n_len = cpu_to_le16(hi_n_len);
  3262. tfd->num_tbs = idx + 1;
  3263. }
  3264. static inline u8
  3265. il4965_tfd_get_num_tbs(struct il_tfd *tfd)
  3266. {
  3267. return tfd->num_tbs & 0x1f;
  3268. }
  3269. /**
  3270. * il4965_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  3271. * @il - driver ilate data
  3272. * @txq - tx queue
  3273. *
  3274. * Does NOT advance any TFD circular buffer read/write idxes
  3275. * Does NOT free the TFD itself (which is within circular buffer)
  3276. */
  3277. void
  3278. il4965_hw_txq_free_tfd(struct il_priv *il, struct il_tx_queue *txq)
  3279. {
  3280. struct il_tfd *tfd_tmp = (struct il_tfd *)txq->tfds;
  3281. struct il_tfd *tfd;
  3282. struct pci_dev *dev = il->pci_dev;
  3283. int idx = txq->q.read_ptr;
  3284. int i;
  3285. int num_tbs;
  3286. tfd = &tfd_tmp[idx];
  3287. /* Sanity check on number of chunks */
  3288. num_tbs = il4965_tfd_get_num_tbs(tfd);
  3289. if (num_tbs >= IL_NUM_OF_TBS) {
  3290. IL_ERR("Too many chunks: %i\n", num_tbs);
  3291. /* @todo issue fatal error, it is quite serious situation */
  3292. return;
  3293. }
  3294. /* Unmap tx_cmd */
  3295. if (num_tbs)
  3296. pci_unmap_single(dev, dma_unmap_addr(&txq->meta[idx], mapping),
  3297. dma_unmap_len(&txq->meta[idx], len),
  3298. PCI_DMA_BIDIRECTIONAL);
  3299. /* Unmap chunks, if any. */
  3300. for (i = 1; i < num_tbs; i++)
  3301. pci_unmap_single(dev, il4965_tfd_tb_get_addr(tfd, i),
  3302. il4965_tfd_tb_get_len(tfd, i),
  3303. PCI_DMA_TODEVICE);
  3304. /* free SKB */
  3305. if (txq->skbs) {
  3306. struct sk_buff *skb = txq->skbs[txq->q.read_ptr];
  3307. /* can be called from irqs-disabled context */
  3308. if (skb) {
  3309. dev_kfree_skb_any(skb);
  3310. txq->skbs[txq->q.read_ptr] = NULL;
  3311. }
  3312. }
  3313. }
  3314. int
  3315. il4965_hw_txq_attach_buf_to_tfd(struct il_priv *il, struct il_tx_queue *txq,
  3316. dma_addr_t addr, u16 len, u8 reset, u8 pad)
  3317. {
  3318. struct il_queue *q;
  3319. struct il_tfd *tfd, *tfd_tmp;
  3320. u32 num_tbs;
  3321. q = &txq->q;
  3322. tfd_tmp = (struct il_tfd *)txq->tfds;
  3323. tfd = &tfd_tmp[q->write_ptr];
  3324. if (reset)
  3325. memset(tfd, 0, sizeof(*tfd));
  3326. num_tbs = il4965_tfd_get_num_tbs(tfd);
  3327. /* Each TFD can point to a maximum 20 Tx buffers */
  3328. if (num_tbs >= IL_NUM_OF_TBS) {
  3329. IL_ERR("Error can not send more than %d chunks\n",
  3330. IL_NUM_OF_TBS);
  3331. return -EINVAL;
  3332. }
  3333. BUG_ON(addr & ~DMA_BIT_MASK(36));
  3334. if (unlikely(addr & ~IL_TX_DMA_MASK))
  3335. IL_ERR("Unaligned address = %llx\n", (unsigned long long)addr);
  3336. il4965_tfd_set_tb(tfd, num_tbs, addr, len);
  3337. return 0;
  3338. }
  3339. /*
  3340. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  3341. * given Tx queue, and enable the DMA channel used for that queue.
  3342. *
  3343. * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
  3344. * channels supported in hardware.
  3345. */
  3346. int
  3347. il4965_hw_tx_queue_init(struct il_priv *il, struct il_tx_queue *txq)
  3348. {
  3349. int txq_id = txq->q.id;
  3350. /* Circular buffer (TFD queue in DRAM) physical base address */
  3351. il_wr(il, FH49_MEM_CBBC_QUEUE(txq_id), txq->q.dma_addr >> 8);
  3352. return 0;
  3353. }
  3354. /******************************************************************************
  3355. *
  3356. * Generic RX handler implementations
  3357. *
  3358. ******************************************************************************/
  3359. static void
  3360. il4965_hdl_alive(struct il_priv *il, struct il_rx_buf *rxb)
  3361. {
  3362. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3363. struct il_alive_resp *palive;
  3364. struct delayed_work *pwork;
  3365. palive = &pkt->u.alive_frame;
  3366. D_INFO("Alive ucode status 0x%08X revision " "0x%01X 0x%01X\n",
  3367. palive->is_valid, palive->ver_type, palive->ver_subtype);
  3368. if (palive->ver_subtype == INITIALIZE_SUBTYPE) {
  3369. D_INFO("Initialization Alive received.\n");
  3370. memcpy(&il->card_alive_init, &pkt->u.alive_frame,
  3371. sizeof(struct il_init_alive_resp));
  3372. pwork = &il->init_alive_start;
  3373. } else {
  3374. D_INFO("Runtime Alive received.\n");
  3375. memcpy(&il->card_alive, &pkt->u.alive_frame,
  3376. sizeof(struct il_alive_resp));
  3377. pwork = &il->alive_start;
  3378. }
  3379. /* We delay the ALIVE response by 5ms to
  3380. * give the HW RF Kill time to activate... */
  3381. if (palive->is_valid == UCODE_VALID_OK)
  3382. queue_delayed_work(il->workqueue, pwork, msecs_to_jiffies(5));
  3383. else
  3384. IL_WARN("uCode did not respond OK.\n");
  3385. }
  3386. /**
  3387. * il4965_bg_stats_periodic - Timer callback to queue stats
  3388. *
  3389. * This callback is provided in order to send a stats request.
  3390. *
  3391. * This timer function is continually reset to execute within
  3392. * 60 seconds since the last N_STATS was received. We need to
  3393. * ensure we receive the stats in order to update the temperature
  3394. * used for calibrating the TXPOWER.
  3395. */
  3396. static void
  3397. il4965_bg_stats_periodic(unsigned long data)
  3398. {
  3399. struct il_priv *il = (struct il_priv *)data;
  3400. if (test_bit(S_EXIT_PENDING, &il->status))
  3401. return;
  3402. /* dont send host command if rf-kill is on */
  3403. if (!il_is_ready_rf(il))
  3404. return;
  3405. il_send_stats_request(il, CMD_ASYNC, false);
  3406. }
  3407. static void
  3408. il4965_hdl_beacon(struct il_priv *il, struct il_rx_buf *rxb)
  3409. {
  3410. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3411. struct il4965_beacon_notif *beacon =
  3412. (struct il4965_beacon_notif *)pkt->u.raw;
  3413. #ifdef CONFIG_IWLEGACY_DEBUG
  3414. u8 rate = il4965_hw_get_rate(beacon->beacon_notify_hdr.rate_n_flags);
  3415. D_RX("beacon status %x retries %d iss %d tsf:0x%.8x%.8x rate %d\n",
  3416. le32_to_cpu(beacon->beacon_notify_hdr.u.status) & TX_STATUS_MSK,
  3417. beacon->beacon_notify_hdr.failure_frame,
  3418. le32_to_cpu(beacon->ibss_mgr_status),
  3419. le32_to_cpu(beacon->high_tsf), le32_to_cpu(beacon->low_tsf), rate);
  3420. #endif
  3421. il->ibss_manager = le32_to_cpu(beacon->ibss_mgr_status);
  3422. }
  3423. static void
  3424. il4965_perform_ct_kill_task(struct il_priv *il)
  3425. {
  3426. unsigned long flags;
  3427. D_POWER("Stop all queues\n");
  3428. if (il->mac80211_registered)
  3429. ieee80211_stop_queues(il->hw);
  3430. _il_wr(il, CSR_UCODE_DRV_GP1_SET,
  3431. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  3432. _il_rd(il, CSR_UCODE_DRV_GP1);
  3433. spin_lock_irqsave(&il->reg_lock, flags);
  3434. if (likely(_il_grab_nic_access(il)))
  3435. _il_release_nic_access(il);
  3436. spin_unlock_irqrestore(&il->reg_lock, flags);
  3437. }
  3438. /* Handle notification from uCode that card's power state is changing
  3439. * due to software, hardware, or critical temperature RFKILL */
  3440. static void
  3441. il4965_hdl_card_state(struct il_priv *il, struct il_rx_buf *rxb)
  3442. {
  3443. struct il_rx_pkt *pkt = rxb_addr(rxb);
  3444. u32 flags = le32_to_cpu(pkt->u.card_state_notif.flags);
  3445. unsigned long status = il->status;
  3446. D_RF_KILL("Card state received: HW:%s SW:%s CT:%s\n",
  3447. (flags & HW_CARD_DISABLED) ? "Kill" : "On",
  3448. (flags & SW_CARD_DISABLED) ? "Kill" : "On",
  3449. (flags & CT_CARD_DISABLED) ? "Reached" : "Not reached");
  3450. if (flags & (SW_CARD_DISABLED | HW_CARD_DISABLED | CT_CARD_DISABLED)) {
  3451. _il_wr(il, CSR_UCODE_DRV_GP1_SET,
  3452. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  3453. il_wr(il, HBUS_TARG_MBX_C, HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  3454. if (!(flags & RXON_CARD_DISABLED)) {
  3455. _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
  3456. CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  3457. il_wr(il, HBUS_TARG_MBX_C,
  3458. HBUS_TARG_MBX_C_REG_BIT_CMD_BLOCKED);
  3459. }
  3460. }
  3461. if (flags & CT_CARD_DISABLED)
  3462. il4965_perform_ct_kill_task(il);
  3463. if (flags & HW_CARD_DISABLED)
  3464. set_bit(S_RFKILL, &il->status);
  3465. else
  3466. clear_bit(S_RFKILL, &il->status);
  3467. if (!(flags & RXON_CARD_DISABLED))
  3468. il_scan_cancel(il);
  3469. if ((test_bit(S_RFKILL, &status) !=
  3470. test_bit(S_RFKILL, &il->status)))
  3471. wiphy_rfkill_set_hw_state(il->hw->wiphy,
  3472. test_bit(S_RFKILL, &il->status));
  3473. else
  3474. wake_up(&il->wait_command_queue);
  3475. }
  3476. /**
  3477. * il4965_setup_handlers - Initialize Rx handler callbacks
  3478. *
  3479. * Setup the RX handlers for each of the reply types sent from the uCode
  3480. * to the host.
  3481. *
  3482. * This function chains into the hardware specific files for them to setup
  3483. * any hardware specific handlers as well.
  3484. */
  3485. static void
  3486. il4965_setup_handlers(struct il_priv *il)
  3487. {
  3488. il->handlers[N_ALIVE] = il4965_hdl_alive;
  3489. il->handlers[N_ERROR] = il_hdl_error;
  3490. il->handlers[N_CHANNEL_SWITCH] = il_hdl_csa;
  3491. il->handlers[N_SPECTRUM_MEASUREMENT] = il_hdl_spectrum_measurement;
  3492. il->handlers[N_PM_SLEEP] = il_hdl_pm_sleep;
  3493. il->handlers[N_PM_DEBUG_STATS] = il_hdl_pm_debug_stats;
  3494. il->handlers[N_BEACON] = il4965_hdl_beacon;
  3495. /*
  3496. * The same handler is used for both the REPLY to a discrete
  3497. * stats request from the host as well as for the periodic
  3498. * stats notifications (after received beacons) from the uCode.
  3499. */
  3500. il->handlers[C_STATS] = il4965_hdl_c_stats;
  3501. il->handlers[N_STATS] = il4965_hdl_stats;
  3502. il_setup_rx_scan_handlers(il);
  3503. /* status change handler */
  3504. il->handlers[N_CARD_STATE] = il4965_hdl_card_state;
  3505. il->handlers[N_MISSED_BEACONS] = il4965_hdl_missed_beacon;
  3506. /* Rx handlers */
  3507. il->handlers[N_RX_PHY] = il4965_hdl_rx_phy;
  3508. il->handlers[N_RX_MPDU] = il4965_hdl_rx;
  3509. il->handlers[N_RX] = il4965_hdl_rx;
  3510. /* block ack */
  3511. il->handlers[N_COMPRESSED_BA] = il4965_hdl_compressed_ba;
  3512. /* Tx response */
  3513. il->handlers[C_TX] = il4965_hdl_tx;
  3514. }
  3515. /**
  3516. * il4965_rx_handle - Main entry function for receiving responses from uCode
  3517. *
  3518. * Uses the il->handlers callback function array to invoke
  3519. * the appropriate handlers, including command responses,
  3520. * frame-received notifications, and other notifications.
  3521. */
  3522. void
  3523. il4965_rx_handle(struct il_priv *il)
  3524. {
  3525. struct il_rx_buf *rxb;
  3526. struct il_rx_pkt *pkt;
  3527. struct il_rx_queue *rxq = &il->rxq;
  3528. u32 r, i;
  3529. int reclaim;
  3530. unsigned long flags;
  3531. u8 fill_rx = 0;
  3532. u32 count = 8;
  3533. int total_empty;
  3534. /* uCode's read idx (stored in shared DRAM) indicates the last Rx
  3535. * buffer that the driver may process (last buffer filled by ucode). */
  3536. r = le16_to_cpu(rxq->rb_stts->closed_rb_num) & 0x0FFF;
  3537. i = rxq->read;
  3538. /* Rx interrupt, but nothing sent from uCode */
  3539. if (i == r)
  3540. D_RX("r = %d, i = %d\n", r, i);
  3541. /* calculate total frames need to be restock after handling RX */
  3542. total_empty = r - rxq->write_actual;
  3543. if (total_empty < 0)
  3544. total_empty += RX_QUEUE_SIZE;
  3545. if (total_empty > (RX_QUEUE_SIZE / 2))
  3546. fill_rx = 1;
  3547. while (i != r) {
  3548. int len;
  3549. rxb = rxq->queue[i];
  3550. /* If an RXB doesn't have a Rx queue slot associated with it,
  3551. * then a bug has been introduced in the queue refilling
  3552. * routines -- catch it here */
  3553. BUG_ON(rxb == NULL);
  3554. rxq->queue[i] = NULL;
  3555. pci_unmap_page(il->pci_dev, rxb->page_dma,
  3556. PAGE_SIZE << il->hw_params.rx_page_order,
  3557. PCI_DMA_FROMDEVICE);
  3558. pkt = rxb_addr(rxb);
  3559. len = le32_to_cpu(pkt->len_n_flags) & IL_RX_FRAME_SIZE_MSK;
  3560. len += sizeof(u32); /* account for status word */
  3561. /* Reclaim a command buffer only if this packet is a response
  3562. * to a (driver-originated) command.
  3563. * If the packet (e.g. Rx frame) originated from uCode,
  3564. * there is no command buffer to reclaim.
  3565. * Ucode should set SEQ_RX_FRAME bit if ucode-originated,
  3566. * but apparently a few don't get set; catch them here. */
  3567. reclaim = !(pkt->hdr.sequence & SEQ_RX_FRAME) &&
  3568. (pkt->hdr.cmd != N_RX_PHY) && (pkt->hdr.cmd != N_RX) &&
  3569. (pkt->hdr.cmd != N_RX_MPDU) &&
  3570. (pkt->hdr.cmd != N_COMPRESSED_BA) &&
  3571. (pkt->hdr.cmd != N_STATS) && (pkt->hdr.cmd != C_TX);
  3572. /* Based on type of command response or notification,
  3573. * handle those that need handling via function in
  3574. * handlers table. See il4965_setup_handlers() */
  3575. if (il->handlers[pkt->hdr.cmd]) {
  3576. D_RX("r = %d, i = %d, %s, 0x%02x\n", r, i,
  3577. il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  3578. il->isr_stats.handlers[pkt->hdr.cmd]++;
  3579. il->handlers[pkt->hdr.cmd] (il, rxb);
  3580. } else {
  3581. /* No handling needed */
  3582. D_RX("r %d i %d No handler needed for %s, 0x%02x\n", r,
  3583. i, il_get_cmd_string(pkt->hdr.cmd), pkt->hdr.cmd);
  3584. }
  3585. /*
  3586. * XXX: After here, we should always check rxb->page
  3587. * against NULL before touching it or its virtual
  3588. * memory (pkt). Because some handler might have
  3589. * already taken or freed the pages.
  3590. */
  3591. if (reclaim) {
  3592. /* Invoke any callbacks, transfer the buffer to caller,
  3593. * and fire off the (possibly) blocking il_send_cmd()
  3594. * as we reclaim the driver command queue */
  3595. if (rxb->page)
  3596. il_tx_cmd_complete(il, rxb);
  3597. else
  3598. IL_WARN("Claim null rxb?\n");
  3599. }
  3600. /* Reuse the page if possible. For notification packets and
  3601. * SKBs that fail to Rx correctly, add them back into the
  3602. * rx_free list for reuse later. */
  3603. spin_lock_irqsave(&rxq->lock, flags);
  3604. if (rxb->page != NULL) {
  3605. rxb->page_dma =
  3606. pci_map_page(il->pci_dev, rxb->page, 0,
  3607. PAGE_SIZE << il->hw_params.
  3608. rx_page_order, PCI_DMA_FROMDEVICE);
  3609. if (unlikely(pci_dma_mapping_error(il->pci_dev,
  3610. rxb->page_dma))) {
  3611. __il_free_pages(il, rxb->page);
  3612. rxb->page = NULL;
  3613. list_add_tail(&rxb->list, &rxq->rx_used);
  3614. } else {
  3615. list_add_tail(&rxb->list, &rxq->rx_free);
  3616. rxq->free_count++;
  3617. }
  3618. } else
  3619. list_add_tail(&rxb->list, &rxq->rx_used);
  3620. spin_unlock_irqrestore(&rxq->lock, flags);
  3621. i = (i + 1) & RX_QUEUE_MASK;
  3622. /* If there are a lot of unused frames,
  3623. * restock the Rx queue so ucode wont assert. */
  3624. if (fill_rx) {
  3625. count++;
  3626. if (count >= 8) {
  3627. rxq->read = i;
  3628. il4965_rx_replenish_now(il);
  3629. count = 0;
  3630. }
  3631. }
  3632. }
  3633. /* Backtrack one entry */
  3634. rxq->read = i;
  3635. if (fill_rx)
  3636. il4965_rx_replenish_now(il);
  3637. else
  3638. il4965_rx_queue_restock(il);
  3639. }
  3640. /* call this function to flush any scheduled tasklet */
  3641. static inline void
  3642. il4965_synchronize_irq(struct il_priv *il)
  3643. {
  3644. /* wait to make sure we flush pending tasklet */
  3645. synchronize_irq(il->pci_dev->irq);
  3646. tasklet_kill(&il->irq_tasklet);
  3647. }
  3648. static void
  3649. il4965_irq_tasklet(struct il_priv *il)
  3650. {
  3651. u32 inta, handled = 0;
  3652. u32 inta_fh;
  3653. unsigned long flags;
  3654. u32 i;
  3655. #ifdef CONFIG_IWLEGACY_DEBUG
  3656. u32 inta_mask;
  3657. #endif
  3658. spin_lock_irqsave(&il->lock, flags);
  3659. /* Ack/clear/reset pending uCode interrupts.
  3660. * Note: Some bits in CSR_INT are "OR" of bits in CSR_FH_INT_STATUS,
  3661. * and will clear only when CSR_FH_INT_STATUS gets cleared. */
  3662. inta = _il_rd(il, CSR_INT);
  3663. _il_wr(il, CSR_INT, inta);
  3664. /* Ack/clear/reset pending flow-handler (DMA) interrupts.
  3665. * Any new interrupts that happen after this, either while we're
  3666. * in this tasklet, or later, will show up in next ISR/tasklet. */
  3667. inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
  3668. _il_wr(il, CSR_FH_INT_STATUS, inta_fh);
  3669. #ifdef CONFIG_IWLEGACY_DEBUG
  3670. if (il_get_debug_level(il) & IL_DL_ISR) {
  3671. /* just for debug */
  3672. inta_mask = _il_rd(il, CSR_INT_MASK);
  3673. D_ISR("inta 0x%08x, enabled 0x%08x, fh 0x%08x\n", inta,
  3674. inta_mask, inta_fh);
  3675. }
  3676. #endif
  3677. spin_unlock_irqrestore(&il->lock, flags);
  3678. /* Since CSR_INT and CSR_FH_INT_STATUS reads and clears are not
  3679. * atomic, make sure that inta covers all the interrupts that
  3680. * we've discovered, even if FH interrupt came in just after
  3681. * reading CSR_INT. */
  3682. if (inta_fh & CSR49_FH_INT_RX_MASK)
  3683. inta |= CSR_INT_BIT_FH_RX;
  3684. if (inta_fh & CSR49_FH_INT_TX_MASK)
  3685. inta |= CSR_INT_BIT_FH_TX;
  3686. /* Now service all interrupt bits discovered above. */
  3687. if (inta & CSR_INT_BIT_HW_ERR) {
  3688. IL_ERR("Hardware error detected. Restarting.\n");
  3689. /* Tell the device to stop sending interrupts */
  3690. il_disable_interrupts(il);
  3691. il->isr_stats.hw++;
  3692. il_irq_handle_error(il);
  3693. handled |= CSR_INT_BIT_HW_ERR;
  3694. return;
  3695. }
  3696. #ifdef CONFIG_IWLEGACY_DEBUG
  3697. if (il_get_debug_level(il) & (IL_DL_ISR)) {
  3698. /* NIC fires this, but we don't use it, redundant with WAKEUP */
  3699. if (inta & CSR_INT_BIT_SCD) {
  3700. D_ISR("Scheduler finished to transmit "
  3701. "the frame/frames.\n");
  3702. il->isr_stats.sch++;
  3703. }
  3704. /* Alive notification via Rx interrupt will do the real work */
  3705. if (inta & CSR_INT_BIT_ALIVE) {
  3706. D_ISR("Alive interrupt\n");
  3707. il->isr_stats.alive++;
  3708. }
  3709. }
  3710. #endif
  3711. /* Safely ignore these bits for debug checks below */
  3712. inta &= ~(CSR_INT_BIT_SCD | CSR_INT_BIT_ALIVE);
  3713. /* HW RF KILL switch toggled */
  3714. if (inta & CSR_INT_BIT_RF_KILL) {
  3715. int hw_rf_kill = 0;
  3716. if (!(_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW))
  3717. hw_rf_kill = 1;
  3718. IL_WARN("RF_KILL bit toggled to %s.\n",
  3719. hw_rf_kill ? "disable radio" : "enable radio");
  3720. il->isr_stats.rfkill++;
  3721. /* driver only loads ucode once setting the interface up.
  3722. * the driver allows loading the ucode even if the radio
  3723. * is killed. Hence update the killswitch state here. The
  3724. * rfkill handler will care about restarting if needed.
  3725. */
  3726. if (!test_bit(S_ALIVE, &il->status)) {
  3727. if (hw_rf_kill)
  3728. set_bit(S_RFKILL, &il->status);
  3729. else
  3730. clear_bit(S_RFKILL, &il->status);
  3731. wiphy_rfkill_set_hw_state(il->hw->wiphy, hw_rf_kill);
  3732. }
  3733. handled |= CSR_INT_BIT_RF_KILL;
  3734. }
  3735. /* Chip got too hot and stopped itself */
  3736. if (inta & CSR_INT_BIT_CT_KILL) {
  3737. IL_ERR("Microcode CT kill error detected.\n");
  3738. il->isr_stats.ctkill++;
  3739. handled |= CSR_INT_BIT_CT_KILL;
  3740. }
  3741. /* Error detected by uCode */
  3742. if (inta & CSR_INT_BIT_SW_ERR) {
  3743. IL_ERR("Microcode SW error detected. " " Restarting 0x%X.\n",
  3744. inta);
  3745. il->isr_stats.sw++;
  3746. il_irq_handle_error(il);
  3747. handled |= CSR_INT_BIT_SW_ERR;
  3748. }
  3749. /*
  3750. * uCode wakes up after power-down sleep.
  3751. * Tell device about any new tx or host commands enqueued,
  3752. * and about any Rx buffers made available while asleep.
  3753. */
  3754. if (inta & CSR_INT_BIT_WAKEUP) {
  3755. D_ISR("Wakeup interrupt\n");
  3756. il_rx_queue_update_write_ptr(il, &il->rxq);
  3757. for (i = 0; i < il->hw_params.max_txq_num; i++)
  3758. il_txq_update_write_ptr(il, &il->txq[i]);
  3759. il->isr_stats.wakeup++;
  3760. handled |= CSR_INT_BIT_WAKEUP;
  3761. }
  3762. /* All uCode command responses, including Tx command responses,
  3763. * Rx "responses" (frame-received notification), and other
  3764. * notifications from uCode come through here*/
  3765. if (inta & (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX)) {
  3766. il4965_rx_handle(il);
  3767. il->isr_stats.rx++;
  3768. handled |= (CSR_INT_BIT_FH_RX | CSR_INT_BIT_SW_RX);
  3769. }
  3770. /* This "Tx" DMA channel is used only for loading uCode */
  3771. if (inta & CSR_INT_BIT_FH_TX) {
  3772. D_ISR("uCode load interrupt\n");
  3773. il->isr_stats.tx++;
  3774. handled |= CSR_INT_BIT_FH_TX;
  3775. /* Wake up uCode load routine, now that load is complete */
  3776. il->ucode_write_complete = 1;
  3777. wake_up(&il->wait_command_queue);
  3778. }
  3779. if (inta & ~handled) {
  3780. IL_ERR("Unhandled INTA bits 0x%08x\n", inta & ~handled);
  3781. il->isr_stats.unhandled++;
  3782. }
  3783. if (inta & ~(il->inta_mask)) {
  3784. IL_WARN("Disabled INTA bits 0x%08x were pending\n",
  3785. inta & ~il->inta_mask);
  3786. IL_WARN(" with FH49_INT = 0x%08x\n", inta_fh);
  3787. }
  3788. /* Re-enable all interrupts */
  3789. /* only Re-enable if disabled by irq */
  3790. if (test_bit(S_INT_ENABLED, &il->status))
  3791. il_enable_interrupts(il);
  3792. /* Re-enable RF_KILL if it occurred */
  3793. else if (handled & CSR_INT_BIT_RF_KILL)
  3794. il_enable_rfkill_int(il);
  3795. #ifdef CONFIG_IWLEGACY_DEBUG
  3796. if (il_get_debug_level(il) & (IL_DL_ISR)) {
  3797. inta = _il_rd(il, CSR_INT);
  3798. inta_mask = _il_rd(il, CSR_INT_MASK);
  3799. inta_fh = _il_rd(il, CSR_FH_INT_STATUS);
  3800. D_ISR("End inta 0x%08x, enabled 0x%08x, fh 0x%08x, "
  3801. "flags 0x%08lx\n", inta, inta_mask, inta_fh, flags);
  3802. }
  3803. #endif
  3804. }
  3805. /*****************************************************************************
  3806. *
  3807. * sysfs attributes
  3808. *
  3809. *****************************************************************************/
  3810. #ifdef CONFIG_IWLEGACY_DEBUG
  3811. /*
  3812. * The following adds a new attribute to the sysfs representation
  3813. * of this device driver (i.e. a new file in /sys/class/net/wlan0/device/)
  3814. * used for controlling the debug level.
  3815. *
  3816. * See the level definitions in iwl for details.
  3817. *
  3818. * The debug_level being managed using sysfs below is a per device debug
  3819. * level that is used instead of the global debug level if it (the per
  3820. * device debug level) is set.
  3821. */
  3822. static ssize_t
  3823. il4965_show_debug_level(struct device *d, struct device_attribute *attr,
  3824. char *buf)
  3825. {
  3826. struct il_priv *il = dev_get_drvdata(d);
  3827. return sprintf(buf, "0x%08X\n", il_get_debug_level(il));
  3828. }
  3829. static ssize_t
  3830. il4965_store_debug_level(struct device *d, struct device_attribute *attr,
  3831. const char *buf, size_t count)
  3832. {
  3833. struct il_priv *il = dev_get_drvdata(d);
  3834. unsigned long val;
  3835. int ret;
  3836. ret = strict_strtoul(buf, 0, &val);
  3837. if (ret)
  3838. IL_ERR("%s is not in hex or decimal form.\n", buf);
  3839. else
  3840. il->debug_level = val;
  3841. return strnlen(buf, count);
  3842. }
  3843. static DEVICE_ATTR(debug_level, S_IWUSR | S_IRUGO, il4965_show_debug_level,
  3844. il4965_store_debug_level);
  3845. #endif /* CONFIG_IWLEGACY_DEBUG */
  3846. static ssize_t
  3847. il4965_show_temperature(struct device *d, struct device_attribute *attr,
  3848. char *buf)
  3849. {
  3850. struct il_priv *il = dev_get_drvdata(d);
  3851. if (!il_is_alive(il))
  3852. return -EAGAIN;
  3853. return sprintf(buf, "%d\n", il->temperature);
  3854. }
  3855. static DEVICE_ATTR(temperature, S_IRUGO, il4965_show_temperature, NULL);
  3856. static ssize_t
  3857. il4965_show_tx_power(struct device *d, struct device_attribute *attr, char *buf)
  3858. {
  3859. struct il_priv *il = dev_get_drvdata(d);
  3860. if (!il_is_ready_rf(il))
  3861. return sprintf(buf, "off\n");
  3862. else
  3863. return sprintf(buf, "%d\n", il->tx_power_user_lmt);
  3864. }
  3865. static ssize_t
  3866. il4965_store_tx_power(struct device *d, struct device_attribute *attr,
  3867. const char *buf, size_t count)
  3868. {
  3869. struct il_priv *il = dev_get_drvdata(d);
  3870. unsigned long val;
  3871. int ret;
  3872. ret = strict_strtoul(buf, 10, &val);
  3873. if (ret)
  3874. IL_INFO("%s is not in decimal form.\n", buf);
  3875. else {
  3876. ret = il_set_tx_power(il, val, false);
  3877. if (ret)
  3878. IL_ERR("failed setting tx power (0x%d).\n", ret);
  3879. else
  3880. ret = count;
  3881. }
  3882. return ret;
  3883. }
  3884. static DEVICE_ATTR(tx_power, S_IWUSR | S_IRUGO, il4965_show_tx_power,
  3885. il4965_store_tx_power);
  3886. static struct attribute *il_sysfs_entries[] = {
  3887. &dev_attr_temperature.attr,
  3888. &dev_attr_tx_power.attr,
  3889. #ifdef CONFIG_IWLEGACY_DEBUG
  3890. &dev_attr_debug_level.attr,
  3891. #endif
  3892. NULL
  3893. };
  3894. static struct attribute_group il_attribute_group = {
  3895. .name = NULL, /* put in device directory */
  3896. .attrs = il_sysfs_entries,
  3897. };
  3898. /******************************************************************************
  3899. *
  3900. * uCode download functions
  3901. *
  3902. ******************************************************************************/
  3903. static void
  3904. il4965_dealloc_ucode_pci(struct il_priv *il)
  3905. {
  3906. il_free_fw_desc(il->pci_dev, &il->ucode_code);
  3907. il_free_fw_desc(il->pci_dev, &il->ucode_data);
  3908. il_free_fw_desc(il->pci_dev, &il->ucode_data_backup);
  3909. il_free_fw_desc(il->pci_dev, &il->ucode_init);
  3910. il_free_fw_desc(il->pci_dev, &il->ucode_init_data);
  3911. il_free_fw_desc(il->pci_dev, &il->ucode_boot);
  3912. }
  3913. static void
  3914. il4965_nic_start(struct il_priv *il)
  3915. {
  3916. /* Remove all resets to allow NIC to operate */
  3917. _il_wr(il, CSR_RESET, 0);
  3918. }
  3919. static void il4965_ucode_callback(const struct firmware *ucode_raw,
  3920. void *context);
  3921. static int il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length);
  3922. static int __must_check
  3923. il4965_request_firmware(struct il_priv *il, bool first)
  3924. {
  3925. const char *name_pre = il->cfg->fw_name_pre;
  3926. char tag[8];
  3927. if (first) {
  3928. il->fw_idx = il->cfg->ucode_api_max;
  3929. sprintf(tag, "%d", il->fw_idx);
  3930. } else {
  3931. il->fw_idx--;
  3932. sprintf(tag, "%d", il->fw_idx);
  3933. }
  3934. if (il->fw_idx < il->cfg->ucode_api_min) {
  3935. IL_ERR("no suitable firmware found!\n");
  3936. return -ENOENT;
  3937. }
  3938. sprintf(il->firmware_name, "%s%s%s", name_pre, tag, ".ucode");
  3939. D_INFO("attempting to load firmware '%s'\n", il->firmware_name);
  3940. return request_firmware_nowait(THIS_MODULE, 1, il->firmware_name,
  3941. &il->pci_dev->dev, GFP_KERNEL, il,
  3942. il4965_ucode_callback);
  3943. }
  3944. struct il4965_firmware_pieces {
  3945. const void *inst, *data, *init, *init_data, *boot;
  3946. size_t inst_size, data_size, init_size, init_data_size, boot_size;
  3947. };
  3948. static int
  3949. il4965_load_firmware(struct il_priv *il, const struct firmware *ucode_raw,
  3950. struct il4965_firmware_pieces *pieces)
  3951. {
  3952. struct il_ucode_header *ucode = (void *)ucode_raw->data;
  3953. u32 api_ver, hdr_size;
  3954. const u8 *src;
  3955. il->ucode_ver = le32_to_cpu(ucode->ver);
  3956. api_ver = IL_UCODE_API(il->ucode_ver);
  3957. switch (api_ver) {
  3958. default:
  3959. case 0:
  3960. case 1:
  3961. case 2:
  3962. hdr_size = 24;
  3963. if (ucode_raw->size < hdr_size) {
  3964. IL_ERR("File size too small!\n");
  3965. return -EINVAL;
  3966. }
  3967. pieces->inst_size = le32_to_cpu(ucode->v1.inst_size);
  3968. pieces->data_size = le32_to_cpu(ucode->v1.data_size);
  3969. pieces->init_size = le32_to_cpu(ucode->v1.init_size);
  3970. pieces->init_data_size = le32_to_cpu(ucode->v1.init_data_size);
  3971. pieces->boot_size = le32_to_cpu(ucode->v1.boot_size);
  3972. src = ucode->v1.data;
  3973. break;
  3974. }
  3975. /* Verify size of file vs. image size info in file's header */
  3976. if (ucode_raw->size !=
  3977. hdr_size + pieces->inst_size + pieces->data_size +
  3978. pieces->init_size + pieces->init_data_size + pieces->boot_size) {
  3979. IL_ERR("uCode file size %d does not match expected size\n",
  3980. (int)ucode_raw->size);
  3981. return -EINVAL;
  3982. }
  3983. pieces->inst = src;
  3984. src += pieces->inst_size;
  3985. pieces->data = src;
  3986. src += pieces->data_size;
  3987. pieces->init = src;
  3988. src += pieces->init_size;
  3989. pieces->init_data = src;
  3990. src += pieces->init_data_size;
  3991. pieces->boot = src;
  3992. src += pieces->boot_size;
  3993. return 0;
  3994. }
  3995. /**
  3996. * il4965_ucode_callback - callback when firmware was loaded
  3997. *
  3998. * If loaded successfully, copies the firmware into buffers
  3999. * for the card to fetch (via DMA).
  4000. */
  4001. static void
  4002. il4965_ucode_callback(const struct firmware *ucode_raw, void *context)
  4003. {
  4004. struct il_priv *il = context;
  4005. struct il_ucode_header *ucode;
  4006. int err;
  4007. struct il4965_firmware_pieces pieces;
  4008. const unsigned int api_max = il->cfg->ucode_api_max;
  4009. const unsigned int api_min = il->cfg->ucode_api_min;
  4010. u32 api_ver;
  4011. u32 max_probe_length = 200;
  4012. u32 standard_phy_calibration_size =
  4013. IL_DEFAULT_STANDARD_PHY_CALIBRATE_TBL_SIZE;
  4014. memset(&pieces, 0, sizeof(pieces));
  4015. if (!ucode_raw) {
  4016. if (il->fw_idx <= il->cfg->ucode_api_max)
  4017. IL_ERR("request for firmware file '%s' failed.\n",
  4018. il->firmware_name);
  4019. goto try_again;
  4020. }
  4021. D_INFO("Loaded firmware file '%s' (%zd bytes).\n", il->firmware_name,
  4022. ucode_raw->size);
  4023. /* Make sure that we got at least the API version number */
  4024. if (ucode_raw->size < 4) {
  4025. IL_ERR("File size way too small!\n");
  4026. goto try_again;
  4027. }
  4028. /* Data from ucode file: header followed by uCode images */
  4029. ucode = (struct il_ucode_header *)ucode_raw->data;
  4030. err = il4965_load_firmware(il, ucode_raw, &pieces);
  4031. if (err)
  4032. goto try_again;
  4033. api_ver = IL_UCODE_API(il->ucode_ver);
  4034. /*
  4035. * api_ver should match the api version forming part of the
  4036. * firmware filename ... but we don't check for that and only rely
  4037. * on the API version read from firmware header from here on forward
  4038. */
  4039. if (api_ver < api_min || api_ver > api_max) {
  4040. IL_ERR("Driver unable to support your firmware API. "
  4041. "Driver supports v%u, firmware is v%u.\n", api_max,
  4042. api_ver);
  4043. goto try_again;
  4044. }
  4045. if (api_ver != api_max)
  4046. IL_ERR("Firmware has old API version. Expected v%u, "
  4047. "got v%u. New firmware can be obtained "
  4048. "from http://www.intellinuxwireless.org.\n", api_max,
  4049. api_ver);
  4050. IL_INFO("loaded firmware version %u.%u.%u.%u\n",
  4051. IL_UCODE_MAJOR(il->ucode_ver), IL_UCODE_MINOR(il->ucode_ver),
  4052. IL_UCODE_API(il->ucode_ver), IL_UCODE_SERIAL(il->ucode_ver));
  4053. snprintf(il->hw->wiphy->fw_version, sizeof(il->hw->wiphy->fw_version),
  4054. "%u.%u.%u.%u", IL_UCODE_MAJOR(il->ucode_ver),
  4055. IL_UCODE_MINOR(il->ucode_ver), IL_UCODE_API(il->ucode_ver),
  4056. IL_UCODE_SERIAL(il->ucode_ver));
  4057. /*
  4058. * For any of the failures below (before allocating pci memory)
  4059. * we will try to load a version with a smaller API -- maybe the
  4060. * user just got a corrupted version of the latest API.
  4061. */
  4062. D_INFO("f/w package hdr ucode version raw = 0x%x\n", il->ucode_ver);
  4063. D_INFO("f/w package hdr runtime inst size = %Zd\n", pieces.inst_size);
  4064. D_INFO("f/w package hdr runtime data size = %Zd\n", pieces.data_size);
  4065. D_INFO("f/w package hdr init inst size = %Zd\n", pieces.init_size);
  4066. D_INFO("f/w package hdr init data size = %Zd\n", pieces.init_data_size);
  4067. D_INFO("f/w package hdr boot inst size = %Zd\n", pieces.boot_size);
  4068. /* Verify that uCode images will fit in card's SRAM */
  4069. if (pieces.inst_size > il->hw_params.max_inst_size) {
  4070. IL_ERR("uCode instr len %Zd too large to fit in\n",
  4071. pieces.inst_size);
  4072. goto try_again;
  4073. }
  4074. if (pieces.data_size > il->hw_params.max_data_size) {
  4075. IL_ERR("uCode data len %Zd too large to fit in\n",
  4076. pieces.data_size);
  4077. goto try_again;
  4078. }
  4079. if (pieces.init_size > il->hw_params.max_inst_size) {
  4080. IL_ERR("uCode init instr len %Zd too large to fit in\n",
  4081. pieces.init_size);
  4082. goto try_again;
  4083. }
  4084. if (pieces.init_data_size > il->hw_params.max_data_size) {
  4085. IL_ERR("uCode init data len %Zd too large to fit in\n",
  4086. pieces.init_data_size);
  4087. goto try_again;
  4088. }
  4089. if (pieces.boot_size > il->hw_params.max_bsm_size) {
  4090. IL_ERR("uCode boot instr len %Zd too large to fit in\n",
  4091. pieces.boot_size);
  4092. goto try_again;
  4093. }
  4094. /* Allocate ucode buffers for card's bus-master loading ... */
  4095. /* Runtime instructions and 2 copies of data:
  4096. * 1) unmodified from disk
  4097. * 2) backup cache for save/restore during power-downs */
  4098. il->ucode_code.len = pieces.inst_size;
  4099. il_alloc_fw_desc(il->pci_dev, &il->ucode_code);
  4100. il->ucode_data.len = pieces.data_size;
  4101. il_alloc_fw_desc(il->pci_dev, &il->ucode_data);
  4102. il->ucode_data_backup.len = pieces.data_size;
  4103. il_alloc_fw_desc(il->pci_dev, &il->ucode_data_backup);
  4104. if (!il->ucode_code.v_addr || !il->ucode_data.v_addr ||
  4105. !il->ucode_data_backup.v_addr)
  4106. goto err_pci_alloc;
  4107. /* Initialization instructions and data */
  4108. if (pieces.init_size && pieces.init_data_size) {
  4109. il->ucode_init.len = pieces.init_size;
  4110. il_alloc_fw_desc(il->pci_dev, &il->ucode_init);
  4111. il->ucode_init_data.len = pieces.init_data_size;
  4112. il_alloc_fw_desc(il->pci_dev, &il->ucode_init_data);
  4113. if (!il->ucode_init.v_addr || !il->ucode_init_data.v_addr)
  4114. goto err_pci_alloc;
  4115. }
  4116. /* Bootstrap (instructions only, no data) */
  4117. if (pieces.boot_size) {
  4118. il->ucode_boot.len = pieces.boot_size;
  4119. il_alloc_fw_desc(il->pci_dev, &il->ucode_boot);
  4120. if (!il->ucode_boot.v_addr)
  4121. goto err_pci_alloc;
  4122. }
  4123. /* Now that we can no longer fail, copy information */
  4124. il->sta_key_max_num = STA_KEY_MAX_NUM;
  4125. /* Copy images into buffers for card's bus-master reads ... */
  4126. /* Runtime instructions (first block of data in file) */
  4127. D_INFO("Copying (but not loading) uCode instr len %Zd\n",
  4128. pieces.inst_size);
  4129. memcpy(il->ucode_code.v_addr, pieces.inst, pieces.inst_size);
  4130. D_INFO("uCode instr buf vaddr = 0x%p, paddr = 0x%08x\n",
  4131. il->ucode_code.v_addr, (u32) il->ucode_code.p_addr);
  4132. /*
  4133. * Runtime data
  4134. * NOTE: Copy into backup buffer will be done in il_up()
  4135. */
  4136. D_INFO("Copying (but not loading) uCode data len %Zd\n",
  4137. pieces.data_size);
  4138. memcpy(il->ucode_data.v_addr, pieces.data, pieces.data_size);
  4139. memcpy(il->ucode_data_backup.v_addr, pieces.data, pieces.data_size);
  4140. /* Initialization instructions */
  4141. if (pieces.init_size) {
  4142. D_INFO("Copying (but not loading) init instr len %Zd\n",
  4143. pieces.init_size);
  4144. memcpy(il->ucode_init.v_addr, pieces.init, pieces.init_size);
  4145. }
  4146. /* Initialization data */
  4147. if (pieces.init_data_size) {
  4148. D_INFO("Copying (but not loading) init data len %Zd\n",
  4149. pieces.init_data_size);
  4150. memcpy(il->ucode_init_data.v_addr, pieces.init_data,
  4151. pieces.init_data_size);
  4152. }
  4153. /* Bootstrap instructions */
  4154. D_INFO("Copying (but not loading) boot instr len %Zd\n",
  4155. pieces.boot_size);
  4156. memcpy(il->ucode_boot.v_addr, pieces.boot, pieces.boot_size);
  4157. /*
  4158. * figure out the offset of chain noise reset and gain commands
  4159. * base on the size of standard phy calibration commands table size
  4160. */
  4161. il->_4965.phy_calib_chain_noise_reset_cmd =
  4162. standard_phy_calibration_size;
  4163. il->_4965.phy_calib_chain_noise_gain_cmd =
  4164. standard_phy_calibration_size + 1;
  4165. /**************************************************
  4166. * This is still part of probe() in a sense...
  4167. *
  4168. * 9. Setup and register with mac80211 and debugfs
  4169. **************************************************/
  4170. err = il4965_mac_setup_register(il, max_probe_length);
  4171. if (err)
  4172. goto out_unbind;
  4173. err = il_dbgfs_register(il, DRV_NAME);
  4174. if (err)
  4175. IL_ERR("failed to create debugfs files. Ignoring error: %d\n",
  4176. err);
  4177. err = sysfs_create_group(&il->pci_dev->dev.kobj, &il_attribute_group);
  4178. if (err) {
  4179. IL_ERR("failed to create sysfs device attributes\n");
  4180. goto out_unbind;
  4181. }
  4182. /* We have our copies now, allow OS release its copies */
  4183. release_firmware(ucode_raw);
  4184. complete(&il->_4965.firmware_loading_complete);
  4185. return;
  4186. try_again:
  4187. /* try next, if any */
  4188. if (il4965_request_firmware(il, false))
  4189. goto out_unbind;
  4190. release_firmware(ucode_raw);
  4191. return;
  4192. err_pci_alloc:
  4193. IL_ERR("failed to allocate pci memory\n");
  4194. il4965_dealloc_ucode_pci(il);
  4195. out_unbind:
  4196. complete(&il->_4965.firmware_loading_complete);
  4197. device_release_driver(&il->pci_dev->dev);
  4198. release_firmware(ucode_raw);
  4199. }
  4200. static const char *const desc_lookup_text[] = {
  4201. "OK",
  4202. "FAIL",
  4203. "BAD_PARAM",
  4204. "BAD_CHECKSUM",
  4205. "NMI_INTERRUPT_WDG",
  4206. "SYSASSERT",
  4207. "FATAL_ERROR",
  4208. "BAD_COMMAND",
  4209. "HW_ERROR_TUNE_LOCK",
  4210. "HW_ERROR_TEMPERATURE",
  4211. "ILLEGAL_CHAN_FREQ",
  4212. "VCC_NOT_STBL",
  4213. "FH49_ERROR",
  4214. "NMI_INTERRUPT_HOST",
  4215. "NMI_INTERRUPT_ACTION_PT",
  4216. "NMI_INTERRUPT_UNKNOWN",
  4217. "UCODE_VERSION_MISMATCH",
  4218. "HW_ERROR_ABS_LOCK",
  4219. "HW_ERROR_CAL_LOCK_FAIL",
  4220. "NMI_INTERRUPT_INST_ACTION_PT",
  4221. "NMI_INTERRUPT_DATA_ACTION_PT",
  4222. "NMI_TRM_HW_ER",
  4223. "NMI_INTERRUPT_TRM",
  4224. "NMI_INTERRUPT_BREAK_POINT",
  4225. "DEBUG_0",
  4226. "DEBUG_1",
  4227. "DEBUG_2",
  4228. "DEBUG_3",
  4229. };
  4230. static struct {
  4231. char *name;
  4232. u8 num;
  4233. } advanced_lookup[] = {
  4234. {
  4235. "NMI_INTERRUPT_WDG", 0x34}, {
  4236. "SYSASSERT", 0x35}, {
  4237. "UCODE_VERSION_MISMATCH", 0x37}, {
  4238. "BAD_COMMAND", 0x38}, {
  4239. "NMI_INTERRUPT_DATA_ACTION_PT", 0x3C}, {
  4240. "FATAL_ERROR", 0x3D}, {
  4241. "NMI_TRM_HW_ERR", 0x46}, {
  4242. "NMI_INTERRUPT_TRM", 0x4C}, {
  4243. "NMI_INTERRUPT_BREAK_POINT", 0x54}, {
  4244. "NMI_INTERRUPT_WDG_RXF_FULL", 0x5C}, {
  4245. "NMI_INTERRUPT_WDG_NO_RBD_RXF_FULL", 0x64}, {
  4246. "NMI_INTERRUPT_HOST", 0x66}, {
  4247. "NMI_INTERRUPT_ACTION_PT", 0x7C}, {
  4248. "NMI_INTERRUPT_UNKNOWN", 0x84}, {
  4249. "NMI_INTERRUPT_INST_ACTION_PT", 0x86}, {
  4250. "ADVANCED_SYSASSERT", 0},};
  4251. static const char *
  4252. il4965_desc_lookup(u32 num)
  4253. {
  4254. int i;
  4255. int max = ARRAY_SIZE(desc_lookup_text);
  4256. if (num < max)
  4257. return desc_lookup_text[num];
  4258. max = ARRAY_SIZE(advanced_lookup) - 1;
  4259. for (i = 0; i < max; i++) {
  4260. if (advanced_lookup[i].num == num)
  4261. break;
  4262. }
  4263. return advanced_lookup[i].name;
  4264. }
  4265. #define ERROR_START_OFFSET (1 * sizeof(u32))
  4266. #define ERROR_ELEM_SIZE (7 * sizeof(u32))
  4267. void
  4268. il4965_dump_nic_error_log(struct il_priv *il)
  4269. {
  4270. u32 data2, line;
  4271. u32 desc, time, count, base, data1;
  4272. u32 blink1, blink2, ilink1, ilink2;
  4273. u32 pc, hcmd;
  4274. if (il->ucode_type == UCODE_INIT)
  4275. base = le32_to_cpu(il->card_alive_init.error_event_table_ptr);
  4276. else
  4277. base = le32_to_cpu(il->card_alive.error_event_table_ptr);
  4278. if (!il->ops->is_valid_rtc_data_addr(base)) {
  4279. IL_ERR("Not valid error log pointer 0x%08X for %s uCode\n",
  4280. base, (il->ucode_type == UCODE_INIT) ? "Init" : "RT");
  4281. return;
  4282. }
  4283. count = il_read_targ_mem(il, base);
  4284. if (ERROR_START_OFFSET <= count * ERROR_ELEM_SIZE) {
  4285. IL_ERR("Start IWL Error Log Dump:\n");
  4286. IL_ERR("Status: 0x%08lX, count: %d\n", il->status, count);
  4287. }
  4288. desc = il_read_targ_mem(il, base + 1 * sizeof(u32));
  4289. il->isr_stats.err_code = desc;
  4290. pc = il_read_targ_mem(il, base + 2 * sizeof(u32));
  4291. blink1 = il_read_targ_mem(il, base + 3 * sizeof(u32));
  4292. blink2 = il_read_targ_mem(il, base + 4 * sizeof(u32));
  4293. ilink1 = il_read_targ_mem(il, base + 5 * sizeof(u32));
  4294. ilink2 = il_read_targ_mem(il, base + 6 * sizeof(u32));
  4295. data1 = il_read_targ_mem(il, base + 7 * sizeof(u32));
  4296. data2 = il_read_targ_mem(il, base + 8 * sizeof(u32));
  4297. line = il_read_targ_mem(il, base + 9 * sizeof(u32));
  4298. time = il_read_targ_mem(il, base + 11 * sizeof(u32));
  4299. hcmd = il_read_targ_mem(il, base + 22 * sizeof(u32));
  4300. IL_ERR("Desc Time "
  4301. "data1 data2 line\n");
  4302. IL_ERR("%-28s (0x%04X) %010u 0x%08X 0x%08X %u\n",
  4303. il4965_desc_lookup(desc), desc, time, data1, data2, line);
  4304. IL_ERR("pc blink1 blink2 ilink1 ilink2 hcmd\n");
  4305. IL_ERR("0x%05X 0x%05X 0x%05X 0x%05X 0x%05X 0x%05X\n", pc, blink1,
  4306. blink2, ilink1, ilink2, hcmd);
  4307. }
  4308. static void
  4309. il4965_rf_kill_ct_config(struct il_priv *il)
  4310. {
  4311. struct il_ct_kill_config cmd;
  4312. unsigned long flags;
  4313. int ret = 0;
  4314. spin_lock_irqsave(&il->lock, flags);
  4315. _il_wr(il, CSR_UCODE_DRV_GP1_CLR,
  4316. CSR_UCODE_DRV_GP1_REG_BIT_CT_KILL_EXIT);
  4317. spin_unlock_irqrestore(&il->lock, flags);
  4318. cmd.critical_temperature_R =
  4319. cpu_to_le32(il->hw_params.ct_kill_threshold);
  4320. ret = il_send_cmd_pdu(il, C_CT_KILL_CONFIG, sizeof(cmd), &cmd);
  4321. if (ret)
  4322. IL_ERR("C_CT_KILL_CONFIG failed\n");
  4323. else
  4324. D_INFO("C_CT_KILL_CONFIG " "succeeded, "
  4325. "critical temperature is %d\n",
  4326. il->hw_params.ct_kill_threshold);
  4327. }
  4328. static const s8 default_queue_to_tx_fifo[] = {
  4329. IL_TX_FIFO_VO,
  4330. IL_TX_FIFO_VI,
  4331. IL_TX_FIFO_BE,
  4332. IL_TX_FIFO_BK,
  4333. IL49_CMD_FIFO_NUM,
  4334. IL_TX_FIFO_UNUSED,
  4335. IL_TX_FIFO_UNUSED,
  4336. };
  4337. #define IL_MASK(lo, hi) ((1 << (hi)) | ((1 << (hi)) - (1 << (lo))))
  4338. static int
  4339. il4965_alive_notify(struct il_priv *il)
  4340. {
  4341. u32 a;
  4342. unsigned long flags;
  4343. int i, chan;
  4344. u32 reg_val;
  4345. spin_lock_irqsave(&il->lock, flags);
  4346. /* Clear 4965's internal Tx Scheduler data base */
  4347. il->scd_base_addr = il_rd_prph(il, IL49_SCD_SRAM_BASE_ADDR);
  4348. a = il->scd_base_addr + IL49_SCD_CONTEXT_DATA_OFFSET;
  4349. for (; a < il->scd_base_addr + IL49_SCD_TX_STTS_BITMAP_OFFSET; a += 4)
  4350. il_write_targ_mem(il, a, 0);
  4351. for (; a < il->scd_base_addr + IL49_SCD_TRANSLATE_TBL_OFFSET; a += 4)
  4352. il_write_targ_mem(il, a, 0);
  4353. for (;
  4354. a <
  4355. il->scd_base_addr +
  4356. IL49_SCD_TRANSLATE_TBL_OFFSET_QUEUE(il->hw_params.max_txq_num);
  4357. a += 4)
  4358. il_write_targ_mem(il, a, 0);
  4359. /* Tel 4965 where to find Tx byte count tables */
  4360. il_wr_prph(il, IL49_SCD_DRAM_BASE_ADDR, il->scd_bc_tbls.dma >> 10);
  4361. /* Enable DMA channel */
  4362. for (chan = 0; chan < FH49_TCSR_CHNL_NUM; chan++)
  4363. il_wr(il, FH49_TCSR_CHNL_TX_CONFIG_REG(chan),
  4364. FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  4365. FH49_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  4366. /* Update FH chicken bits */
  4367. reg_val = il_rd(il, FH49_TX_CHICKEN_BITS_REG);
  4368. il_wr(il, FH49_TX_CHICKEN_BITS_REG,
  4369. reg_val | FH49_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  4370. /* Disable chain mode for all queues */
  4371. il_wr_prph(il, IL49_SCD_QUEUECHAIN_SEL, 0);
  4372. /* Initialize each Tx queue (including the command queue) */
  4373. for (i = 0; i < il->hw_params.max_txq_num; i++) {
  4374. /* TFD circular buffer read/write idxes */
  4375. il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(i), 0);
  4376. il_wr(il, HBUS_TARG_WRPTR, 0 | (i << 8));
  4377. /* Max Tx Window size for Scheduler-ACK mode */
  4378. il_write_targ_mem(il,
  4379. il->scd_base_addr +
  4380. IL49_SCD_CONTEXT_QUEUE_OFFSET(i),
  4381. (SCD_WIN_SIZE <<
  4382. IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_POS) &
  4383. IL49_SCD_QUEUE_CTX_REG1_WIN_SIZE_MSK);
  4384. /* Frame limit */
  4385. il_write_targ_mem(il,
  4386. il->scd_base_addr +
  4387. IL49_SCD_CONTEXT_QUEUE_OFFSET(i) +
  4388. sizeof(u32),
  4389. (SCD_FRAME_LIMIT <<
  4390. IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  4391. IL49_SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK);
  4392. }
  4393. il_wr_prph(il, IL49_SCD_INTERRUPT_MASK,
  4394. (1 << il->hw_params.max_txq_num) - 1);
  4395. /* Activate all Tx DMA/FIFO channels */
  4396. il4965_txq_set_sched(il, IL_MASK(0, 6));
  4397. il4965_set_wr_ptrs(il, IL_DEFAULT_CMD_QUEUE_NUM, 0);
  4398. /* make sure all queue are not stopped */
  4399. memset(&il->queue_stopped[0], 0, sizeof(il->queue_stopped));
  4400. for (i = 0; i < 4; i++)
  4401. atomic_set(&il->queue_stop_count[i], 0);
  4402. /* reset to 0 to enable all the queue first */
  4403. il->txq_ctx_active_msk = 0;
  4404. /* Map each Tx/cmd queue to its corresponding fifo */
  4405. BUILD_BUG_ON(ARRAY_SIZE(default_queue_to_tx_fifo) != 7);
  4406. for (i = 0; i < ARRAY_SIZE(default_queue_to_tx_fifo); i++) {
  4407. int ac = default_queue_to_tx_fifo[i];
  4408. il_txq_ctx_activate(il, i);
  4409. if (ac == IL_TX_FIFO_UNUSED)
  4410. continue;
  4411. il4965_tx_queue_set_status(il, &il->txq[i], ac, 0);
  4412. }
  4413. spin_unlock_irqrestore(&il->lock, flags);
  4414. return 0;
  4415. }
  4416. /**
  4417. * il4965_alive_start - called after N_ALIVE notification received
  4418. * from protocol/runtime uCode (initialization uCode's
  4419. * Alive gets handled by il_init_alive_start()).
  4420. */
  4421. static void
  4422. il4965_alive_start(struct il_priv *il)
  4423. {
  4424. int ret = 0;
  4425. D_INFO("Runtime Alive received.\n");
  4426. if (il->card_alive.is_valid != UCODE_VALID_OK) {
  4427. /* We had an error bringing up the hardware, so take it
  4428. * all the way back down so we can try again */
  4429. D_INFO("Alive failed.\n");
  4430. goto restart;
  4431. }
  4432. /* Initialize uCode has loaded Runtime uCode ... verify inst image.
  4433. * This is a paranoid check, because we would not have gotten the
  4434. * "runtime" alive if code weren't properly loaded. */
  4435. if (il4965_verify_ucode(il)) {
  4436. /* Runtime instruction load was bad;
  4437. * take it all the way back down so we can try again */
  4438. D_INFO("Bad runtime uCode load.\n");
  4439. goto restart;
  4440. }
  4441. ret = il4965_alive_notify(il);
  4442. if (ret) {
  4443. IL_WARN("Could not complete ALIVE transition [ntf]: %d\n", ret);
  4444. goto restart;
  4445. }
  4446. /* After the ALIVE response, we can send host commands to the uCode */
  4447. set_bit(S_ALIVE, &il->status);
  4448. /* Enable watchdog to monitor the driver tx queues */
  4449. il_setup_watchdog(il);
  4450. if (il_is_rfkill(il))
  4451. return;
  4452. ieee80211_wake_queues(il->hw);
  4453. il->active_rate = RATES_MASK;
  4454. if (il_is_associated(il)) {
  4455. struct il_rxon_cmd *active_rxon =
  4456. (struct il_rxon_cmd *)&il->active;
  4457. /* apply any changes in staging */
  4458. il->staging.filter_flags |= RXON_FILTER_ASSOC_MSK;
  4459. active_rxon->filter_flags &= ~RXON_FILTER_ASSOC_MSK;
  4460. } else {
  4461. /* Initialize our rx_config data */
  4462. il_connection_init_rx_config(il);
  4463. if (il->ops->set_rxon_chain)
  4464. il->ops->set_rxon_chain(il);
  4465. }
  4466. /* Configure bluetooth coexistence if enabled */
  4467. il_send_bt_config(il);
  4468. il4965_reset_run_time_calib(il);
  4469. set_bit(S_READY, &il->status);
  4470. /* Configure the adapter for unassociated operation */
  4471. il_commit_rxon(il);
  4472. /* At this point, the NIC is initialized and operational */
  4473. il4965_rf_kill_ct_config(il);
  4474. D_INFO("ALIVE processing complete.\n");
  4475. wake_up(&il->wait_command_queue);
  4476. il_power_update_mode(il, true);
  4477. D_INFO("Updated power mode\n");
  4478. return;
  4479. restart:
  4480. queue_work(il->workqueue, &il->restart);
  4481. }
  4482. static void il4965_cancel_deferred_work(struct il_priv *il);
  4483. static void
  4484. __il4965_down(struct il_priv *il)
  4485. {
  4486. unsigned long flags;
  4487. int exit_pending;
  4488. D_INFO(DRV_NAME " is going down\n");
  4489. il_scan_cancel_timeout(il, 200);
  4490. exit_pending = test_and_set_bit(S_EXIT_PENDING, &il->status);
  4491. /* Stop TX queues watchdog. We need to have S_EXIT_PENDING bit set
  4492. * to prevent rearm timer */
  4493. del_timer_sync(&il->watchdog);
  4494. il_clear_ucode_stations(il);
  4495. /* FIXME: race conditions ? */
  4496. spin_lock_irq(&il->sta_lock);
  4497. /*
  4498. * Remove all key information that is not stored as part
  4499. * of station information since mac80211 may not have had
  4500. * a chance to remove all the keys. When device is
  4501. * reconfigured by mac80211 after an error all keys will
  4502. * be reconfigured.
  4503. */
  4504. memset(il->_4965.wep_keys, 0, sizeof(il->_4965.wep_keys));
  4505. il->_4965.key_mapping_keys = 0;
  4506. spin_unlock_irq(&il->sta_lock);
  4507. il_dealloc_bcast_stations(il);
  4508. il_clear_driver_stations(il);
  4509. /* Unblock any waiting calls */
  4510. wake_up_all(&il->wait_command_queue);
  4511. /* Wipe out the EXIT_PENDING status bit if we are not actually
  4512. * exiting the module */
  4513. if (!exit_pending)
  4514. clear_bit(S_EXIT_PENDING, &il->status);
  4515. /* stop and reset the on-board processor */
  4516. _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  4517. /* tell the device to stop sending interrupts */
  4518. spin_lock_irqsave(&il->lock, flags);
  4519. il_disable_interrupts(il);
  4520. spin_unlock_irqrestore(&il->lock, flags);
  4521. il4965_synchronize_irq(il);
  4522. if (il->mac80211_registered)
  4523. ieee80211_stop_queues(il->hw);
  4524. /* If we have not previously called il_init() then
  4525. * clear all bits but the RF Kill bit and return */
  4526. if (!il_is_init(il)) {
  4527. il->status =
  4528. test_bit(S_RFKILL, &il->status) << S_RFKILL |
  4529. test_bit(S_GEO_CONFIGURED, &il->status) << S_GEO_CONFIGURED |
  4530. test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
  4531. goto exit;
  4532. }
  4533. /* ...otherwise clear out all the status bits but the RF Kill
  4534. * bit and continue taking the NIC down. */
  4535. il->status &=
  4536. test_bit(S_RFKILL, &il->status) << S_RFKILL |
  4537. test_bit(S_GEO_CONFIGURED, &il->status) << S_GEO_CONFIGURED |
  4538. test_bit(S_FW_ERROR, &il->status) << S_FW_ERROR |
  4539. test_bit(S_EXIT_PENDING, &il->status) << S_EXIT_PENDING;
  4540. /*
  4541. * We disabled and synchronized interrupt, and priv->mutex is taken, so
  4542. * here is the only thread which will program device registers, but
  4543. * still have lockdep assertions, so we are taking reg_lock.
  4544. */
  4545. spin_lock_irq(&il->reg_lock);
  4546. /* FIXME: il_grab_nic_access if rfkill is off ? */
  4547. il4965_txq_ctx_stop(il);
  4548. il4965_rxq_stop(il);
  4549. /* Power-down device's busmaster DMA clocks */
  4550. _il_wr_prph(il, APMG_CLK_DIS_REG, APMG_CLK_VAL_DMA_CLK_RQT);
  4551. udelay(5);
  4552. /* Make sure (redundant) we've released our request to stay awake */
  4553. _il_clear_bit(il, CSR_GP_CNTRL, CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  4554. /* Stop the device, and put it in low power state */
  4555. _il_apm_stop(il);
  4556. spin_unlock_irq(&il->reg_lock);
  4557. il4965_txq_ctx_unmap(il);
  4558. exit:
  4559. memset(&il->card_alive, 0, sizeof(struct il_alive_resp));
  4560. dev_kfree_skb(il->beacon_skb);
  4561. il->beacon_skb = NULL;
  4562. /* clear out any free frames */
  4563. il4965_clear_free_frames(il);
  4564. }
  4565. static void
  4566. il4965_down(struct il_priv *il)
  4567. {
  4568. mutex_lock(&il->mutex);
  4569. __il4965_down(il);
  4570. mutex_unlock(&il->mutex);
  4571. il4965_cancel_deferred_work(il);
  4572. }
  4573. static void
  4574. il4965_set_hw_ready(struct il_priv *il)
  4575. {
  4576. int ret;
  4577. il_set_bit(il, CSR_HW_IF_CONFIG_REG,
  4578. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY);
  4579. /* See if we got it */
  4580. ret = _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
  4581. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  4582. CSR_HW_IF_CONFIG_REG_BIT_NIC_READY,
  4583. 100);
  4584. if (ret >= 0)
  4585. il->hw_ready = true;
  4586. D_INFO("hardware %s ready\n", (il->hw_ready) ? "" : "not");
  4587. }
  4588. static void
  4589. il4965_prepare_card_hw(struct il_priv *il)
  4590. {
  4591. int ret;
  4592. il->hw_ready = false;
  4593. il4965_set_hw_ready(il);
  4594. if (il->hw_ready)
  4595. return;
  4596. /* If HW is not ready, prepare the conditions to check again */
  4597. il_set_bit(il, CSR_HW_IF_CONFIG_REG, CSR_HW_IF_CONFIG_REG_PREPARE);
  4598. ret =
  4599. _il_poll_bit(il, CSR_HW_IF_CONFIG_REG,
  4600. ~CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE,
  4601. CSR_HW_IF_CONFIG_REG_BIT_NIC_PREPARE_DONE, 150000);
  4602. /* HW should be ready by now, check again. */
  4603. if (ret != -ETIMEDOUT)
  4604. il4965_set_hw_ready(il);
  4605. }
  4606. #define MAX_HW_RESTARTS 5
  4607. static int
  4608. __il4965_up(struct il_priv *il)
  4609. {
  4610. int i;
  4611. int ret;
  4612. if (test_bit(S_EXIT_PENDING, &il->status)) {
  4613. IL_WARN("Exit pending; will not bring the NIC up\n");
  4614. return -EIO;
  4615. }
  4616. if (!il->ucode_data_backup.v_addr || !il->ucode_data.v_addr) {
  4617. IL_ERR("ucode not available for device bringup\n");
  4618. return -EIO;
  4619. }
  4620. ret = il4965_alloc_bcast_station(il);
  4621. if (ret) {
  4622. il_dealloc_bcast_stations(il);
  4623. return ret;
  4624. }
  4625. il4965_prepare_card_hw(il);
  4626. if (!il->hw_ready) {
  4627. IL_ERR("HW not ready\n");
  4628. return -EIO;
  4629. }
  4630. /* If platform's RF_KILL switch is NOT set to KILL */
  4631. if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  4632. clear_bit(S_RFKILL, &il->status);
  4633. else {
  4634. set_bit(S_RFKILL, &il->status);
  4635. wiphy_rfkill_set_hw_state(il->hw->wiphy, true);
  4636. il_enable_rfkill_int(il);
  4637. IL_WARN("Radio disabled by HW RF Kill switch\n");
  4638. return 0;
  4639. }
  4640. _il_wr(il, CSR_INT, 0xFFFFFFFF);
  4641. /* must be initialised before il_hw_nic_init */
  4642. il->cmd_queue = IL_DEFAULT_CMD_QUEUE_NUM;
  4643. ret = il4965_hw_nic_init(il);
  4644. if (ret) {
  4645. IL_ERR("Unable to init nic\n");
  4646. return ret;
  4647. }
  4648. /* make sure rfkill handshake bits are cleared */
  4649. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4650. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_DRV_GP1_BIT_CMD_BLOCKED);
  4651. /* clear (again), then enable host interrupts */
  4652. _il_wr(il, CSR_INT, 0xFFFFFFFF);
  4653. il_enable_interrupts(il);
  4654. /* really make sure rfkill handshake bits are cleared */
  4655. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4656. _il_wr(il, CSR_UCODE_DRV_GP1_CLR, CSR_UCODE_SW_BIT_RFKILL);
  4657. /* Copy original ucode data image from disk into backup cache.
  4658. * This will be used to initialize the on-board processor's
  4659. * data SRAM for a clean start when the runtime program first loads. */
  4660. memcpy(il->ucode_data_backup.v_addr, il->ucode_data.v_addr,
  4661. il->ucode_data.len);
  4662. for (i = 0; i < MAX_HW_RESTARTS; i++) {
  4663. /* load bootstrap state machine,
  4664. * load bootstrap program into processor's memory,
  4665. * prepare to load the "initialize" uCode */
  4666. ret = il->ops->load_ucode(il);
  4667. if (ret) {
  4668. IL_ERR("Unable to set up bootstrap uCode: %d\n", ret);
  4669. continue;
  4670. }
  4671. /* start card; "initialize" will load runtime ucode */
  4672. il4965_nic_start(il);
  4673. D_INFO(DRV_NAME " is coming up\n");
  4674. return 0;
  4675. }
  4676. set_bit(S_EXIT_PENDING, &il->status);
  4677. __il4965_down(il);
  4678. clear_bit(S_EXIT_PENDING, &il->status);
  4679. /* tried to restart and config the device for as long as our
  4680. * patience could withstand */
  4681. IL_ERR("Unable to initialize device after %d attempts.\n", i);
  4682. return -EIO;
  4683. }
  4684. /*****************************************************************************
  4685. *
  4686. * Workqueue callbacks
  4687. *
  4688. *****************************************************************************/
  4689. static void
  4690. il4965_bg_init_alive_start(struct work_struct *data)
  4691. {
  4692. struct il_priv *il =
  4693. container_of(data, struct il_priv, init_alive_start.work);
  4694. mutex_lock(&il->mutex);
  4695. if (test_bit(S_EXIT_PENDING, &il->status))
  4696. goto out;
  4697. il->ops->init_alive_start(il);
  4698. out:
  4699. mutex_unlock(&il->mutex);
  4700. }
  4701. static void
  4702. il4965_bg_alive_start(struct work_struct *data)
  4703. {
  4704. struct il_priv *il =
  4705. container_of(data, struct il_priv, alive_start.work);
  4706. mutex_lock(&il->mutex);
  4707. if (test_bit(S_EXIT_PENDING, &il->status))
  4708. goto out;
  4709. il4965_alive_start(il);
  4710. out:
  4711. mutex_unlock(&il->mutex);
  4712. }
  4713. static void
  4714. il4965_bg_run_time_calib_work(struct work_struct *work)
  4715. {
  4716. struct il_priv *il = container_of(work, struct il_priv,
  4717. run_time_calib_work);
  4718. mutex_lock(&il->mutex);
  4719. if (test_bit(S_EXIT_PENDING, &il->status) ||
  4720. test_bit(S_SCANNING, &il->status)) {
  4721. mutex_unlock(&il->mutex);
  4722. return;
  4723. }
  4724. if (il->start_calib) {
  4725. il4965_chain_noise_calibration(il, (void *)&il->_4965.stats);
  4726. il4965_sensitivity_calibration(il, (void *)&il->_4965.stats);
  4727. }
  4728. mutex_unlock(&il->mutex);
  4729. }
  4730. static void
  4731. il4965_bg_restart(struct work_struct *data)
  4732. {
  4733. struct il_priv *il = container_of(data, struct il_priv, restart);
  4734. if (test_bit(S_EXIT_PENDING, &il->status))
  4735. return;
  4736. if (test_and_clear_bit(S_FW_ERROR, &il->status)) {
  4737. mutex_lock(&il->mutex);
  4738. il->is_open = 0;
  4739. __il4965_down(il);
  4740. mutex_unlock(&il->mutex);
  4741. il4965_cancel_deferred_work(il);
  4742. ieee80211_restart_hw(il->hw);
  4743. } else {
  4744. il4965_down(il);
  4745. mutex_lock(&il->mutex);
  4746. if (test_bit(S_EXIT_PENDING, &il->status)) {
  4747. mutex_unlock(&il->mutex);
  4748. return;
  4749. }
  4750. __il4965_up(il);
  4751. mutex_unlock(&il->mutex);
  4752. }
  4753. }
  4754. static void
  4755. il4965_bg_rx_replenish(struct work_struct *data)
  4756. {
  4757. struct il_priv *il = container_of(data, struct il_priv, rx_replenish);
  4758. if (test_bit(S_EXIT_PENDING, &il->status))
  4759. return;
  4760. mutex_lock(&il->mutex);
  4761. il4965_rx_replenish(il);
  4762. mutex_unlock(&il->mutex);
  4763. }
  4764. /*****************************************************************************
  4765. *
  4766. * mac80211 entry point functions
  4767. *
  4768. *****************************************************************************/
  4769. #define UCODE_READY_TIMEOUT (4 * HZ)
  4770. /*
  4771. * Not a mac80211 entry point function, but it fits in with all the
  4772. * other mac80211 functions grouped here.
  4773. */
  4774. static int
  4775. il4965_mac_setup_register(struct il_priv *il, u32 max_probe_length)
  4776. {
  4777. int ret;
  4778. struct ieee80211_hw *hw = il->hw;
  4779. hw->rate_control_algorithm = "iwl-4965-rs";
  4780. /* Tell mac80211 our characteristics */
  4781. hw->flags =
  4782. IEEE80211_HW_SIGNAL_DBM | IEEE80211_HW_AMPDU_AGGREGATION |
  4783. IEEE80211_HW_NEED_DTIM_BEFORE_ASSOC | IEEE80211_HW_SPECTRUM_MGMT |
  4784. IEEE80211_HW_REPORTS_TX_ACK_STATUS | IEEE80211_HW_SUPPORTS_PS |
  4785. IEEE80211_HW_SUPPORTS_DYNAMIC_PS;
  4786. if (il->cfg->sku & IL_SKU_N)
  4787. hw->flags |=
  4788. IEEE80211_HW_SUPPORTS_DYNAMIC_SMPS |
  4789. IEEE80211_HW_SUPPORTS_STATIC_SMPS;
  4790. hw->sta_data_size = sizeof(struct il_station_priv);
  4791. hw->vif_data_size = sizeof(struct il_vif_priv);
  4792. hw->wiphy->interface_modes =
  4793. BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_ADHOC);
  4794. hw->wiphy->flags |=
  4795. WIPHY_FLAG_CUSTOM_REGULATORY | WIPHY_FLAG_DISABLE_BEACON_HINTS |
  4796. WIPHY_FLAG_IBSS_RSN;
  4797. /*
  4798. * For now, disable PS by default because it affects
  4799. * RX performance significantly.
  4800. */
  4801. hw->wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
  4802. hw->wiphy->max_scan_ssids = PROBE_OPTION_MAX;
  4803. /* we create the 802.11 header and a zero-length SSID element */
  4804. hw->wiphy->max_scan_ie_len = max_probe_length - 24 - 2;
  4805. /* Default value; 4 EDCA QOS priorities */
  4806. hw->queues = 4;
  4807. hw->max_listen_interval = IL_CONN_MAX_LISTEN_INTERVAL;
  4808. if (il->bands[IEEE80211_BAND_2GHZ].n_channels)
  4809. il->hw->wiphy->bands[IEEE80211_BAND_2GHZ] =
  4810. &il->bands[IEEE80211_BAND_2GHZ];
  4811. if (il->bands[IEEE80211_BAND_5GHZ].n_channels)
  4812. il->hw->wiphy->bands[IEEE80211_BAND_5GHZ] =
  4813. &il->bands[IEEE80211_BAND_5GHZ];
  4814. il_leds_init(il);
  4815. ret = ieee80211_register_hw(il->hw);
  4816. if (ret) {
  4817. IL_ERR("Failed to register hw (error %d)\n", ret);
  4818. return ret;
  4819. }
  4820. il->mac80211_registered = 1;
  4821. return 0;
  4822. }
  4823. int
  4824. il4965_mac_start(struct ieee80211_hw *hw)
  4825. {
  4826. struct il_priv *il = hw->priv;
  4827. int ret;
  4828. D_MAC80211("enter\n");
  4829. /* we should be verifying the device is ready to be opened */
  4830. mutex_lock(&il->mutex);
  4831. ret = __il4965_up(il);
  4832. mutex_unlock(&il->mutex);
  4833. if (ret)
  4834. return ret;
  4835. if (il_is_rfkill(il))
  4836. goto out;
  4837. D_INFO("Start UP work done.\n");
  4838. /* Wait for START_ALIVE from Run Time ucode. Otherwise callbacks from
  4839. * mac80211 will not be run successfully. */
  4840. ret = wait_event_timeout(il->wait_command_queue,
  4841. test_bit(S_READY, &il->status),
  4842. UCODE_READY_TIMEOUT);
  4843. if (!ret) {
  4844. if (!test_bit(S_READY, &il->status)) {
  4845. IL_ERR("START_ALIVE timeout after %dms.\n",
  4846. jiffies_to_msecs(UCODE_READY_TIMEOUT));
  4847. return -ETIMEDOUT;
  4848. }
  4849. }
  4850. il4965_led_enable(il);
  4851. out:
  4852. il->is_open = 1;
  4853. D_MAC80211("leave\n");
  4854. return 0;
  4855. }
  4856. void
  4857. il4965_mac_stop(struct ieee80211_hw *hw)
  4858. {
  4859. struct il_priv *il = hw->priv;
  4860. D_MAC80211("enter\n");
  4861. if (!il->is_open)
  4862. return;
  4863. il->is_open = 0;
  4864. il4965_down(il);
  4865. flush_workqueue(il->workqueue);
  4866. /* User space software may expect getting rfkill changes
  4867. * even if interface is down */
  4868. _il_wr(il, CSR_INT, 0xFFFFFFFF);
  4869. il_enable_rfkill_int(il);
  4870. D_MAC80211("leave\n");
  4871. }
  4872. void
  4873. il4965_mac_tx(struct ieee80211_hw *hw,
  4874. struct ieee80211_tx_control *control,
  4875. struct sk_buff *skb)
  4876. {
  4877. struct il_priv *il = hw->priv;
  4878. D_MACDUMP("enter\n");
  4879. D_TX("dev->xmit(%d bytes) at rate 0x%02x\n", skb->len,
  4880. ieee80211_get_tx_rate(hw, IEEE80211_SKB_CB(skb))->bitrate);
  4881. if (il4965_tx_skb(il, control->sta, skb))
  4882. dev_kfree_skb_any(skb);
  4883. D_MACDUMP("leave\n");
  4884. }
  4885. void
  4886. il4965_mac_update_tkip_key(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4887. struct ieee80211_key_conf *keyconf,
  4888. struct ieee80211_sta *sta, u32 iv32, u16 * phase1key)
  4889. {
  4890. struct il_priv *il = hw->priv;
  4891. D_MAC80211("enter\n");
  4892. il4965_update_tkip_key(il, keyconf, sta, iv32, phase1key);
  4893. D_MAC80211("leave\n");
  4894. }
  4895. int
  4896. il4965_mac_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  4897. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  4898. struct ieee80211_key_conf *key)
  4899. {
  4900. struct il_priv *il = hw->priv;
  4901. int ret;
  4902. u8 sta_id;
  4903. bool is_default_wep_key = false;
  4904. D_MAC80211("enter\n");
  4905. if (il->cfg->mod_params->sw_crypto) {
  4906. D_MAC80211("leave - hwcrypto disabled\n");
  4907. return -EOPNOTSUPP;
  4908. }
  4909. /*
  4910. * To support IBSS RSN, don't program group keys in IBSS, the
  4911. * hardware will then not attempt to decrypt the frames.
  4912. */
  4913. if (vif->type == NL80211_IFTYPE_ADHOC &&
  4914. !(key->flags & IEEE80211_KEY_FLAG_PAIRWISE)) {
  4915. D_MAC80211("leave - ad-hoc group key\n");
  4916. return -EOPNOTSUPP;
  4917. }
  4918. sta_id = il_sta_id_or_broadcast(il, sta);
  4919. if (sta_id == IL_INVALID_STATION)
  4920. return -EINVAL;
  4921. mutex_lock(&il->mutex);
  4922. il_scan_cancel_timeout(il, 100);
  4923. /*
  4924. * If we are getting WEP group key and we didn't receive any key mapping
  4925. * so far, we are in legacy wep mode (group key only), otherwise we are
  4926. * in 1X mode.
  4927. * In legacy wep mode, we use another host command to the uCode.
  4928. */
  4929. if ((key->cipher == WLAN_CIPHER_SUITE_WEP40 ||
  4930. key->cipher == WLAN_CIPHER_SUITE_WEP104) && !sta) {
  4931. if (cmd == SET_KEY)
  4932. is_default_wep_key = !il->_4965.key_mapping_keys;
  4933. else
  4934. is_default_wep_key =
  4935. (key->hw_key_idx == HW_KEY_DEFAULT);
  4936. }
  4937. switch (cmd) {
  4938. case SET_KEY:
  4939. if (is_default_wep_key)
  4940. ret = il4965_set_default_wep_key(il, key);
  4941. else
  4942. ret = il4965_set_dynamic_key(il, key, sta_id);
  4943. D_MAC80211("enable hwcrypto key\n");
  4944. break;
  4945. case DISABLE_KEY:
  4946. if (is_default_wep_key)
  4947. ret = il4965_remove_default_wep_key(il, key);
  4948. else
  4949. ret = il4965_remove_dynamic_key(il, key, sta_id);
  4950. D_MAC80211("disable hwcrypto key\n");
  4951. break;
  4952. default:
  4953. ret = -EINVAL;
  4954. }
  4955. mutex_unlock(&il->mutex);
  4956. D_MAC80211("leave\n");
  4957. return ret;
  4958. }
  4959. int
  4960. il4965_mac_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  4961. enum ieee80211_ampdu_mlme_action action,
  4962. struct ieee80211_sta *sta, u16 tid, u16 * ssn,
  4963. u8 buf_size)
  4964. {
  4965. struct il_priv *il = hw->priv;
  4966. int ret = -EINVAL;
  4967. D_HT("A-MPDU action on addr %pM tid %d\n", sta->addr, tid);
  4968. if (!(il->cfg->sku & IL_SKU_N))
  4969. return -EACCES;
  4970. mutex_lock(&il->mutex);
  4971. switch (action) {
  4972. case IEEE80211_AMPDU_RX_START:
  4973. D_HT("start Rx\n");
  4974. ret = il4965_sta_rx_agg_start(il, sta, tid, *ssn);
  4975. break;
  4976. case IEEE80211_AMPDU_RX_STOP:
  4977. D_HT("stop Rx\n");
  4978. ret = il4965_sta_rx_agg_stop(il, sta, tid);
  4979. if (test_bit(S_EXIT_PENDING, &il->status))
  4980. ret = 0;
  4981. break;
  4982. case IEEE80211_AMPDU_TX_START:
  4983. D_HT("start Tx\n");
  4984. ret = il4965_tx_agg_start(il, vif, sta, tid, ssn);
  4985. break;
  4986. case IEEE80211_AMPDU_TX_STOP_CONT:
  4987. case IEEE80211_AMPDU_TX_STOP_FLUSH:
  4988. case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
  4989. D_HT("stop Tx\n");
  4990. ret = il4965_tx_agg_stop(il, vif, sta, tid);
  4991. if (test_bit(S_EXIT_PENDING, &il->status))
  4992. ret = 0;
  4993. break;
  4994. case IEEE80211_AMPDU_TX_OPERATIONAL:
  4995. ret = 0;
  4996. break;
  4997. }
  4998. mutex_unlock(&il->mutex);
  4999. return ret;
  5000. }
  5001. int
  5002. il4965_mac_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  5003. struct ieee80211_sta *sta)
  5004. {
  5005. struct il_priv *il = hw->priv;
  5006. struct il_station_priv *sta_priv = (void *)sta->drv_priv;
  5007. bool is_ap = vif->type == NL80211_IFTYPE_STATION;
  5008. int ret;
  5009. u8 sta_id;
  5010. D_INFO("received request to add station %pM\n", sta->addr);
  5011. mutex_lock(&il->mutex);
  5012. D_INFO("proceeding to add station %pM\n", sta->addr);
  5013. sta_priv->common.sta_id = IL_INVALID_STATION;
  5014. atomic_set(&sta_priv->pending_frames, 0);
  5015. ret =
  5016. il_add_station_common(il, sta->addr, is_ap, sta, &sta_id);
  5017. if (ret) {
  5018. IL_ERR("Unable to add station %pM (%d)\n", sta->addr, ret);
  5019. /* Should we return success if return code is EEXIST ? */
  5020. mutex_unlock(&il->mutex);
  5021. return ret;
  5022. }
  5023. sta_priv->common.sta_id = sta_id;
  5024. /* Initialize rate scaling */
  5025. D_INFO("Initializing rate scaling for station %pM\n", sta->addr);
  5026. il4965_rs_rate_init(il, sta, sta_id);
  5027. mutex_unlock(&il->mutex);
  5028. return 0;
  5029. }
  5030. void
  5031. il4965_mac_channel_switch(struct ieee80211_hw *hw,
  5032. struct ieee80211_channel_switch *ch_switch)
  5033. {
  5034. struct il_priv *il = hw->priv;
  5035. const struct il_channel_info *ch_info;
  5036. struct ieee80211_conf *conf = &hw->conf;
  5037. struct ieee80211_channel *channel = ch_switch->channel;
  5038. struct il_ht_config *ht_conf = &il->current_ht_config;
  5039. u16 ch;
  5040. D_MAC80211("enter\n");
  5041. mutex_lock(&il->mutex);
  5042. if (il_is_rfkill(il))
  5043. goto out;
  5044. if (test_bit(S_EXIT_PENDING, &il->status) ||
  5045. test_bit(S_SCANNING, &il->status) ||
  5046. test_bit(S_CHANNEL_SWITCH_PENDING, &il->status))
  5047. goto out;
  5048. if (!il_is_associated(il))
  5049. goto out;
  5050. if (!il->ops->set_channel_switch)
  5051. goto out;
  5052. ch = channel->hw_value;
  5053. if (le16_to_cpu(il->active.channel) == ch)
  5054. goto out;
  5055. ch_info = il_get_channel_info(il, channel->band, ch);
  5056. if (!il_is_channel_valid(ch_info)) {
  5057. D_MAC80211("invalid channel\n");
  5058. goto out;
  5059. }
  5060. spin_lock_irq(&il->lock);
  5061. il->current_ht_config.smps = conf->smps_mode;
  5062. /* Configure HT40 channels */
  5063. il->ht.enabled = conf_is_ht(conf);
  5064. if (il->ht.enabled) {
  5065. if (conf_is_ht40_minus(conf)) {
  5066. il->ht.extension_chan_offset =
  5067. IEEE80211_HT_PARAM_CHA_SEC_BELOW;
  5068. il->ht.is_40mhz = true;
  5069. } else if (conf_is_ht40_plus(conf)) {
  5070. il->ht.extension_chan_offset =
  5071. IEEE80211_HT_PARAM_CHA_SEC_ABOVE;
  5072. il->ht.is_40mhz = true;
  5073. } else {
  5074. il->ht.extension_chan_offset =
  5075. IEEE80211_HT_PARAM_CHA_SEC_NONE;
  5076. il->ht.is_40mhz = false;
  5077. }
  5078. } else
  5079. il->ht.is_40mhz = false;
  5080. if ((le16_to_cpu(il->staging.channel) != ch))
  5081. il->staging.flags = 0;
  5082. il_set_rxon_channel(il, channel);
  5083. il_set_rxon_ht(il, ht_conf);
  5084. il_set_flags_for_band(il, channel->band, il->vif);
  5085. spin_unlock_irq(&il->lock);
  5086. il_set_rate(il);
  5087. /*
  5088. * at this point, staging_rxon has the
  5089. * configuration for channel switch
  5090. */
  5091. set_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
  5092. il->switch_channel = cpu_to_le16(ch);
  5093. if (il->ops->set_channel_switch(il, ch_switch)) {
  5094. clear_bit(S_CHANNEL_SWITCH_PENDING, &il->status);
  5095. il->switch_channel = 0;
  5096. ieee80211_chswitch_done(il->vif, false);
  5097. }
  5098. out:
  5099. mutex_unlock(&il->mutex);
  5100. D_MAC80211("leave\n");
  5101. }
  5102. void
  5103. il4965_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
  5104. unsigned int *total_flags, u64 multicast)
  5105. {
  5106. struct il_priv *il = hw->priv;
  5107. __le32 filter_or = 0, filter_nand = 0;
  5108. #define CHK(test, flag) do { \
  5109. if (*total_flags & (test)) \
  5110. filter_or |= (flag); \
  5111. else \
  5112. filter_nand |= (flag); \
  5113. } while (0)
  5114. D_MAC80211("Enter: changed: 0x%x, total: 0x%x\n", changed_flags,
  5115. *total_flags);
  5116. CHK(FIF_OTHER_BSS | FIF_PROMISC_IN_BSS, RXON_FILTER_PROMISC_MSK);
  5117. /* Setting _just_ RXON_FILTER_CTL2HOST_MSK causes FH errors */
  5118. CHK(FIF_CONTROL, RXON_FILTER_CTL2HOST_MSK | RXON_FILTER_PROMISC_MSK);
  5119. CHK(FIF_BCN_PRBRESP_PROMISC, RXON_FILTER_BCON_AWARE_MSK);
  5120. #undef CHK
  5121. mutex_lock(&il->mutex);
  5122. il->staging.filter_flags &= ~filter_nand;
  5123. il->staging.filter_flags |= filter_or;
  5124. /*
  5125. * Not committing directly because hardware can perform a scan,
  5126. * but we'll eventually commit the filter flags change anyway.
  5127. */
  5128. mutex_unlock(&il->mutex);
  5129. /*
  5130. * Receiving all multicast frames is always enabled by the
  5131. * default flags setup in il_connection_init_rx_config()
  5132. * since we currently do not support programming multicast
  5133. * filters into the device.
  5134. */
  5135. *total_flags &=
  5136. FIF_OTHER_BSS | FIF_ALLMULTI | FIF_PROMISC_IN_BSS |
  5137. FIF_BCN_PRBRESP_PROMISC | FIF_CONTROL;
  5138. }
  5139. /*****************************************************************************
  5140. *
  5141. * driver setup and teardown
  5142. *
  5143. *****************************************************************************/
  5144. static void
  5145. il4965_bg_txpower_work(struct work_struct *work)
  5146. {
  5147. struct il_priv *il = container_of(work, struct il_priv,
  5148. txpower_work);
  5149. mutex_lock(&il->mutex);
  5150. /* If a scan happened to start before we got here
  5151. * then just return; the stats notification will
  5152. * kick off another scheduled work to compensate for
  5153. * any temperature delta we missed here. */
  5154. if (test_bit(S_EXIT_PENDING, &il->status) ||
  5155. test_bit(S_SCANNING, &il->status))
  5156. goto out;
  5157. /* Regardless of if we are associated, we must reconfigure the
  5158. * TX power since frames can be sent on non-radar channels while
  5159. * not associated */
  5160. il->ops->send_tx_power(il);
  5161. /* Update last_temperature to keep is_calib_needed from running
  5162. * when it isn't needed... */
  5163. il->last_temperature = il->temperature;
  5164. out:
  5165. mutex_unlock(&il->mutex);
  5166. }
  5167. static void
  5168. il4965_setup_deferred_work(struct il_priv *il)
  5169. {
  5170. il->workqueue = create_singlethread_workqueue(DRV_NAME);
  5171. init_waitqueue_head(&il->wait_command_queue);
  5172. INIT_WORK(&il->restart, il4965_bg_restart);
  5173. INIT_WORK(&il->rx_replenish, il4965_bg_rx_replenish);
  5174. INIT_WORK(&il->run_time_calib_work, il4965_bg_run_time_calib_work);
  5175. INIT_DELAYED_WORK(&il->init_alive_start, il4965_bg_init_alive_start);
  5176. INIT_DELAYED_WORK(&il->alive_start, il4965_bg_alive_start);
  5177. il_setup_scan_deferred_work(il);
  5178. INIT_WORK(&il->txpower_work, il4965_bg_txpower_work);
  5179. init_timer(&il->stats_periodic);
  5180. il->stats_periodic.data = (unsigned long)il;
  5181. il->stats_periodic.function = il4965_bg_stats_periodic;
  5182. init_timer(&il->watchdog);
  5183. il->watchdog.data = (unsigned long)il;
  5184. il->watchdog.function = il_bg_watchdog;
  5185. tasklet_init(&il->irq_tasklet,
  5186. (void (*)(unsigned long))il4965_irq_tasklet,
  5187. (unsigned long)il);
  5188. }
  5189. static void
  5190. il4965_cancel_deferred_work(struct il_priv *il)
  5191. {
  5192. cancel_work_sync(&il->txpower_work);
  5193. cancel_delayed_work_sync(&il->init_alive_start);
  5194. cancel_delayed_work(&il->alive_start);
  5195. cancel_work_sync(&il->run_time_calib_work);
  5196. il_cancel_scan_deferred_work(il);
  5197. del_timer_sync(&il->stats_periodic);
  5198. }
  5199. static void
  5200. il4965_init_hw_rates(struct il_priv *il, struct ieee80211_rate *rates)
  5201. {
  5202. int i;
  5203. for (i = 0; i < RATE_COUNT_LEGACY; i++) {
  5204. rates[i].bitrate = il_rates[i].ieee * 5;
  5205. rates[i].hw_value = i; /* Rate scaling will work on idxes */
  5206. rates[i].hw_value_short = i;
  5207. rates[i].flags = 0;
  5208. if ((i >= IL_FIRST_CCK_RATE) && (i <= IL_LAST_CCK_RATE)) {
  5209. /*
  5210. * If CCK != 1M then set short preamble rate flag.
  5211. */
  5212. rates[i].flags |=
  5213. (il_rates[i].plcp ==
  5214. RATE_1M_PLCP) ? 0 : IEEE80211_RATE_SHORT_PREAMBLE;
  5215. }
  5216. }
  5217. }
  5218. /*
  5219. * Acquire il->lock before calling this function !
  5220. */
  5221. void
  5222. il4965_set_wr_ptrs(struct il_priv *il, int txq_id, u32 idx)
  5223. {
  5224. il_wr(il, HBUS_TARG_WRPTR, (idx & 0xff) | (txq_id << 8));
  5225. il_wr_prph(il, IL49_SCD_QUEUE_RDPTR(txq_id), idx);
  5226. }
  5227. void
  5228. il4965_tx_queue_set_status(struct il_priv *il, struct il_tx_queue *txq,
  5229. int tx_fifo_id, int scd_retry)
  5230. {
  5231. int txq_id = txq->q.id;
  5232. /* Find out whether to activate Tx queue */
  5233. int active = test_bit(txq_id, &il->txq_ctx_active_msk) ? 1 : 0;
  5234. /* Set up and activate */
  5235. il_wr_prph(il, IL49_SCD_QUEUE_STATUS_BITS(txq_id),
  5236. (active << IL49_SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  5237. (tx_fifo_id << IL49_SCD_QUEUE_STTS_REG_POS_TXF) |
  5238. (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_WSL) |
  5239. (scd_retry << IL49_SCD_QUEUE_STTS_REG_POS_SCD_ACK) |
  5240. IL49_SCD_QUEUE_STTS_REG_MSK);
  5241. txq->sched_retry = scd_retry;
  5242. D_INFO("%s %s Queue %d on AC %d\n", active ? "Activate" : "Deactivate",
  5243. scd_retry ? "BA" : "AC", txq_id, tx_fifo_id);
  5244. }
  5245. static const struct ieee80211_ops il4965_mac_ops = {
  5246. .tx = il4965_mac_tx,
  5247. .start = il4965_mac_start,
  5248. .stop = il4965_mac_stop,
  5249. .add_interface = il_mac_add_interface,
  5250. .remove_interface = il_mac_remove_interface,
  5251. .change_interface = il_mac_change_interface,
  5252. .config = il_mac_config,
  5253. .configure_filter = il4965_configure_filter,
  5254. .set_key = il4965_mac_set_key,
  5255. .update_tkip_key = il4965_mac_update_tkip_key,
  5256. .conf_tx = il_mac_conf_tx,
  5257. .reset_tsf = il_mac_reset_tsf,
  5258. .bss_info_changed = il_mac_bss_info_changed,
  5259. .ampdu_action = il4965_mac_ampdu_action,
  5260. .hw_scan = il_mac_hw_scan,
  5261. .sta_add = il4965_mac_sta_add,
  5262. .sta_remove = il_mac_sta_remove,
  5263. .channel_switch = il4965_mac_channel_switch,
  5264. .tx_last_beacon = il_mac_tx_last_beacon,
  5265. .flush = il_mac_flush,
  5266. };
  5267. static int
  5268. il4965_init_drv(struct il_priv *il)
  5269. {
  5270. int ret;
  5271. spin_lock_init(&il->sta_lock);
  5272. spin_lock_init(&il->hcmd_lock);
  5273. INIT_LIST_HEAD(&il->free_frames);
  5274. mutex_init(&il->mutex);
  5275. il->ieee_channels = NULL;
  5276. il->ieee_rates = NULL;
  5277. il->band = IEEE80211_BAND_2GHZ;
  5278. il->iw_mode = NL80211_IFTYPE_STATION;
  5279. il->current_ht_config.smps = IEEE80211_SMPS_STATIC;
  5280. il->missed_beacon_threshold = IL_MISSED_BEACON_THRESHOLD_DEF;
  5281. /* initialize force reset */
  5282. il->force_reset.reset_duration = IL_DELAY_NEXT_FORCE_FW_RELOAD;
  5283. /* Choose which receivers/antennas to use */
  5284. if (il->ops->set_rxon_chain)
  5285. il->ops->set_rxon_chain(il);
  5286. il_init_scan_params(il);
  5287. ret = il_init_channel_map(il);
  5288. if (ret) {
  5289. IL_ERR("initializing regulatory failed: %d\n", ret);
  5290. goto err;
  5291. }
  5292. ret = il_init_geos(il);
  5293. if (ret) {
  5294. IL_ERR("initializing geos failed: %d\n", ret);
  5295. goto err_free_channel_map;
  5296. }
  5297. il4965_init_hw_rates(il, il->ieee_rates);
  5298. return 0;
  5299. err_free_channel_map:
  5300. il_free_channel_map(il);
  5301. err:
  5302. return ret;
  5303. }
  5304. static void
  5305. il4965_uninit_drv(struct il_priv *il)
  5306. {
  5307. il_free_geos(il);
  5308. il_free_channel_map(il);
  5309. kfree(il->scan_cmd);
  5310. }
  5311. static void
  5312. il4965_hw_detect(struct il_priv *il)
  5313. {
  5314. il->hw_rev = _il_rd(il, CSR_HW_REV);
  5315. il->hw_wa_rev = _il_rd(il, CSR_HW_REV_WA_REG);
  5316. il->rev_id = il->pci_dev->revision;
  5317. D_INFO("HW Revision ID = 0x%X\n", il->rev_id);
  5318. }
  5319. static struct il_sensitivity_ranges il4965_sensitivity = {
  5320. .min_nrg_cck = 97,
  5321. .max_nrg_cck = 0, /* not used, set to 0 */
  5322. .auto_corr_min_ofdm = 85,
  5323. .auto_corr_min_ofdm_mrc = 170,
  5324. .auto_corr_min_ofdm_x1 = 105,
  5325. .auto_corr_min_ofdm_mrc_x1 = 220,
  5326. .auto_corr_max_ofdm = 120,
  5327. .auto_corr_max_ofdm_mrc = 210,
  5328. .auto_corr_max_ofdm_x1 = 140,
  5329. .auto_corr_max_ofdm_mrc_x1 = 270,
  5330. .auto_corr_min_cck = 125,
  5331. .auto_corr_max_cck = 200,
  5332. .auto_corr_min_cck_mrc = 200,
  5333. .auto_corr_max_cck_mrc = 400,
  5334. .nrg_th_cck = 100,
  5335. .nrg_th_ofdm = 100,
  5336. .barker_corr_th_min = 190,
  5337. .barker_corr_th_min_mrc = 390,
  5338. .nrg_th_cca = 62,
  5339. };
  5340. static void
  5341. il4965_set_hw_params(struct il_priv *il)
  5342. {
  5343. il->hw_params.bcast_id = IL4965_BROADCAST_ID;
  5344. il->hw_params.max_rxq_size = RX_QUEUE_SIZE;
  5345. il->hw_params.max_rxq_log = RX_QUEUE_SIZE_LOG;
  5346. if (il->cfg->mod_params->amsdu_size_8K)
  5347. il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_8K);
  5348. else
  5349. il->hw_params.rx_page_order = get_order(IL_RX_BUF_SIZE_4K);
  5350. il->hw_params.max_beacon_itrvl = IL_MAX_UCODE_BEACON_INTERVAL;
  5351. if (il->cfg->mod_params->disable_11n)
  5352. il->cfg->sku &= ~IL_SKU_N;
  5353. if (il->cfg->mod_params->num_of_queues >= IL_MIN_NUM_QUEUES &&
  5354. il->cfg->mod_params->num_of_queues <= IL49_NUM_QUEUES)
  5355. il->cfg->num_of_queues =
  5356. il->cfg->mod_params->num_of_queues;
  5357. il->hw_params.max_txq_num = il->cfg->num_of_queues;
  5358. il->hw_params.dma_chnl_num = FH49_TCSR_CHNL_NUM;
  5359. il->hw_params.scd_bc_tbls_size =
  5360. il->cfg->num_of_queues *
  5361. sizeof(struct il4965_scd_bc_tbl);
  5362. il->hw_params.tfd_size = sizeof(struct il_tfd);
  5363. il->hw_params.max_stations = IL4965_STATION_COUNT;
  5364. il->hw_params.max_data_size = IL49_RTC_DATA_SIZE;
  5365. il->hw_params.max_inst_size = IL49_RTC_INST_SIZE;
  5366. il->hw_params.max_bsm_size = BSM_SRAM_SIZE;
  5367. il->hw_params.ht40_channel = BIT(IEEE80211_BAND_5GHZ);
  5368. il->hw_params.rx_wrt_ptr_reg = FH49_RSCSR_CHNL0_WPTR;
  5369. il->hw_params.tx_chains_num = il4965_num_of_ant(il->cfg->valid_tx_ant);
  5370. il->hw_params.rx_chains_num = il4965_num_of_ant(il->cfg->valid_rx_ant);
  5371. il->hw_params.valid_tx_ant = il->cfg->valid_tx_ant;
  5372. il->hw_params.valid_rx_ant = il->cfg->valid_rx_ant;
  5373. il->hw_params.ct_kill_threshold =
  5374. CELSIUS_TO_KELVIN(CT_KILL_THRESHOLD_LEGACY);
  5375. il->hw_params.sens = &il4965_sensitivity;
  5376. il->hw_params.beacon_time_tsf_bits = IL4965_EXT_BEACON_TIME_POS;
  5377. }
  5378. static int
  5379. il4965_pci_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  5380. {
  5381. int err = 0;
  5382. struct il_priv *il;
  5383. struct ieee80211_hw *hw;
  5384. struct il_cfg *cfg = (struct il_cfg *)(ent->driver_data);
  5385. unsigned long flags;
  5386. u16 pci_cmd;
  5387. /************************
  5388. * 1. Allocating HW data
  5389. ************************/
  5390. hw = ieee80211_alloc_hw(sizeof(struct il_priv), &il4965_mac_ops);
  5391. if (!hw) {
  5392. err = -ENOMEM;
  5393. goto out;
  5394. }
  5395. il = hw->priv;
  5396. il->hw = hw;
  5397. SET_IEEE80211_DEV(hw, &pdev->dev);
  5398. D_INFO("*** LOAD DRIVER ***\n");
  5399. il->cfg = cfg;
  5400. il->ops = &il4965_ops;
  5401. #ifdef CONFIG_IWLEGACY_DEBUGFS
  5402. il->debugfs_ops = &il4965_debugfs_ops;
  5403. #endif
  5404. il->pci_dev = pdev;
  5405. il->inta_mask = CSR_INI_SET_MASK;
  5406. /**************************
  5407. * 2. Initializing PCI bus
  5408. **************************/
  5409. pci_disable_link_state(pdev,
  5410. PCIE_LINK_STATE_L0S | PCIE_LINK_STATE_L1 |
  5411. PCIE_LINK_STATE_CLKPM);
  5412. if (pci_enable_device(pdev)) {
  5413. err = -ENODEV;
  5414. goto out_ieee80211_free_hw;
  5415. }
  5416. pci_set_master(pdev);
  5417. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(36));
  5418. if (!err)
  5419. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(36));
  5420. if (err) {
  5421. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  5422. if (!err)
  5423. err =
  5424. pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  5425. /* both attempts failed: */
  5426. if (err) {
  5427. IL_WARN("No suitable DMA available.\n");
  5428. goto out_pci_disable_device;
  5429. }
  5430. }
  5431. err = pci_request_regions(pdev, DRV_NAME);
  5432. if (err)
  5433. goto out_pci_disable_device;
  5434. pci_set_drvdata(pdev, il);
  5435. /***********************
  5436. * 3. Read REV register
  5437. ***********************/
  5438. il->hw_base = pci_ioremap_bar(pdev, 0);
  5439. if (!il->hw_base) {
  5440. err = -ENODEV;
  5441. goto out_pci_release_regions;
  5442. }
  5443. D_INFO("pci_resource_len = 0x%08llx\n",
  5444. (unsigned long long)pci_resource_len(pdev, 0));
  5445. D_INFO("pci_resource_base = %p\n", il->hw_base);
  5446. /* these spin locks will be used in apm_ops.init and EEPROM access
  5447. * we should init now
  5448. */
  5449. spin_lock_init(&il->reg_lock);
  5450. spin_lock_init(&il->lock);
  5451. /*
  5452. * stop and reset the on-board processor just in case it is in a
  5453. * strange state ... like being left stranded by a primary kernel
  5454. * and this is now the kdump kernel trying to start up
  5455. */
  5456. _il_wr(il, CSR_RESET, CSR_RESET_REG_FLAG_NEVO_RESET);
  5457. il4965_hw_detect(il);
  5458. IL_INFO("Detected %s, REV=0x%X\n", il->cfg->name, il->hw_rev);
  5459. /* We disable the RETRY_TIMEOUT register (0x41) to keep
  5460. * PCI Tx retries from interfering with C3 CPU state */
  5461. pci_write_config_byte(pdev, PCI_CFG_RETRY_TIMEOUT, 0x00);
  5462. il4965_prepare_card_hw(il);
  5463. if (!il->hw_ready) {
  5464. IL_WARN("Failed, HW not ready\n");
  5465. err = -EIO;
  5466. goto out_iounmap;
  5467. }
  5468. /*****************
  5469. * 4. Read EEPROM
  5470. *****************/
  5471. /* Read the EEPROM */
  5472. err = il_eeprom_init(il);
  5473. if (err) {
  5474. IL_ERR("Unable to init EEPROM\n");
  5475. goto out_iounmap;
  5476. }
  5477. err = il4965_eeprom_check_version(il);
  5478. if (err)
  5479. goto out_free_eeprom;
  5480. /* extract MAC Address */
  5481. il4965_eeprom_get_mac(il, il->addresses[0].addr);
  5482. D_INFO("MAC address: %pM\n", il->addresses[0].addr);
  5483. il->hw->wiphy->addresses = il->addresses;
  5484. il->hw->wiphy->n_addresses = 1;
  5485. /************************
  5486. * 5. Setup HW constants
  5487. ************************/
  5488. il4965_set_hw_params(il);
  5489. /*******************
  5490. * 6. Setup il
  5491. *******************/
  5492. err = il4965_init_drv(il);
  5493. if (err)
  5494. goto out_free_eeprom;
  5495. /* At this point both hw and il are initialized. */
  5496. /********************
  5497. * 7. Setup services
  5498. ********************/
  5499. spin_lock_irqsave(&il->lock, flags);
  5500. il_disable_interrupts(il);
  5501. spin_unlock_irqrestore(&il->lock, flags);
  5502. pci_enable_msi(il->pci_dev);
  5503. err = request_irq(il->pci_dev->irq, il_isr, IRQF_SHARED, DRV_NAME, il);
  5504. if (err) {
  5505. IL_ERR("Error allocating IRQ %d\n", il->pci_dev->irq);
  5506. goto out_disable_msi;
  5507. }
  5508. il4965_setup_deferred_work(il);
  5509. il4965_setup_handlers(il);
  5510. /*********************************************
  5511. * 8. Enable interrupts and read RFKILL state
  5512. *********************************************/
  5513. /* enable rfkill interrupt: hw bug w/a */
  5514. pci_read_config_word(il->pci_dev, PCI_COMMAND, &pci_cmd);
  5515. if (pci_cmd & PCI_COMMAND_INTX_DISABLE) {
  5516. pci_cmd &= ~PCI_COMMAND_INTX_DISABLE;
  5517. pci_write_config_word(il->pci_dev, PCI_COMMAND, pci_cmd);
  5518. }
  5519. il_enable_rfkill_int(il);
  5520. /* If platform's RF_KILL switch is NOT set to KILL */
  5521. if (_il_rd(il, CSR_GP_CNTRL) & CSR_GP_CNTRL_REG_FLAG_HW_RF_KILL_SW)
  5522. clear_bit(S_RFKILL, &il->status);
  5523. else
  5524. set_bit(S_RFKILL, &il->status);
  5525. wiphy_rfkill_set_hw_state(il->hw->wiphy,
  5526. test_bit(S_RFKILL, &il->status));
  5527. il_power_initialize(il);
  5528. init_completion(&il->_4965.firmware_loading_complete);
  5529. err = il4965_request_firmware(il, true);
  5530. if (err)
  5531. goto out_destroy_workqueue;
  5532. return 0;
  5533. out_destroy_workqueue:
  5534. destroy_workqueue(il->workqueue);
  5535. il->workqueue = NULL;
  5536. free_irq(il->pci_dev->irq, il);
  5537. out_disable_msi:
  5538. pci_disable_msi(il->pci_dev);
  5539. il4965_uninit_drv(il);
  5540. out_free_eeprom:
  5541. il_eeprom_free(il);
  5542. out_iounmap:
  5543. iounmap(il->hw_base);
  5544. out_pci_release_regions:
  5545. pci_set_drvdata(pdev, NULL);
  5546. pci_release_regions(pdev);
  5547. out_pci_disable_device:
  5548. pci_disable_device(pdev);
  5549. out_ieee80211_free_hw:
  5550. ieee80211_free_hw(il->hw);
  5551. out:
  5552. return err;
  5553. }
  5554. static void
  5555. il4965_pci_remove(struct pci_dev *pdev)
  5556. {
  5557. struct il_priv *il = pci_get_drvdata(pdev);
  5558. unsigned long flags;
  5559. if (!il)
  5560. return;
  5561. wait_for_completion(&il->_4965.firmware_loading_complete);
  5562. D_INFO("*** UNLOAD DRIVER ***\n");
  5563. il_dbgfs_unregister(il);
  5564. sysfs_remove_group(&pdev->dev.kobj, &il_attribute_group);
  5565. /* ieee80211_unregister_hw call wil cause il_mac_stop to
  5566. * to be called and il4965_down since we are removing the device
  5567. * we need to set S_EXIT_PENDING bit.
  5568. */
  5569. set_bit(S_EXIT_PENDING, &il->status);
  5570. il_leds_exit(il);
  5571. if (il->mac80211_registered) {
  5572. ieee80211_unregister_hw(il->hw);
  5573. il->mac80211_registered = 0;
  5574. } else {
  5575. il4965_down(il);
  5576. }
  5577. /*
  5578. * Make sure device is reset to low power before unloading driver.
  5579. * This may be redundant with il4965_down(), but there are paths to
  5580. * run il4965_down() without calling apm_ops.stop(), and there are
  5581. * paths to avoid running il4965_down() at all before leaving driver.
  5582. * This (inexpensive) call *makes sure* device is reset.
  5583. */
  5584. il_apm_stop(il);
  5585. /* make sure we flush any pending irq or
  5586. * tasklet for the driver
  5587. */
  5588. spin_lock_irqsave(&il->lock, flags);
  5589. il_disable_interrupts(il);
  5590. spin_unlock_irqrestore(&il->lock, flags);
  5591. il4965_synchronize_irq(il);
  5592. il4965_dealloc_ucode_pci(il);
  5593. if (il->rxq.bd)
  5594. il4965_rx_queue_free(il, &il->rxq);
  5595. il4965_hw_txq_ctx_free(il);
  5596. il_eeprom_free(il);
  5597. /*netif_stop_queue(dev); */
  5598. flush_workqueue(il->workqueue);
  5599. /* ieee80211_unregister_hw calls il_mac_stop, which flushes
  5600. * il->workqueue... so we can't take down the workqueue
  5601. * until now... */
  5602. destroy_workqueue(il->workqueue);
  5603. il->workqueue = NULL;
  5604. free_irq(il->pci_dev->irq, il);
  5605. pci_disable_msi(il->pci_dev);
  5606. iounmap(il->hw_base);
  5607. pci_release_regions(pdev);
  5608. pci_disable_device(pdev);
  5609. pci_set_drvdata(pdev, NULL);
  5610. il4965_uninit_drv(il);
  5611. dev_kfree_skb(il->beacon_skb);
  5612. ieee80211_free_hw(il->hw);
  5613. }
  5614. /*
  5615. * Activate/Deactivate Tx DMA/FIFO channels according tx fifos mask
  5616. * must be called under il->lock and mac access
  5617. */
  5618. void
  5619. il4965_txq_set_sched(struct il_priv *il, u32 mask)
  5620. {
  5621. il_wr_prph(il, IL49_SCD_TXFACT, mask);
  5622. }
  5623. /*****************************************************************************
  5624. *
  5625. * driver and module entry point
  5626. *
  5627. *****************************************************************************/
  5628. /* Hardware specific file defines the PCI IDs table for that hardware module */
  5629. static DEFINE_PCI_DEVICE_TABLE(il4965_hw_card_ids) = {
  5630. {IL_PCI_DEVICE(0x4229, PCI_ANY_ID, il4965_cfg)},
  5631. {IL_PCI_DEVICE(0x4230, PCI_ANY_ID, il4965_cfg)},
  5632. {0}
  5633. };
  5634. MODULE_DEVICE_TABLE(pci, il4965_hw_card_ids);
  5635. static struct pci_driver il4965_driver = {
  5636. .name = DRV_NAME,
  5637. .id_table = il4965_hw_card_ids,
  5638. .probe = il4965_pci_probe,
  5639. .remove = il4965_pci_remove,
  5640. .driver.pm = IL_LEGACY_PM_OPS,
  5641. };
  5642. static int __init
  5643. il4965_init(void)
  5644. {
  5645. int ret;
  5646. pr_info(DRV_DESCRIPTION ", " DRV_VERSION "\n");
  5647. pr_info(DRV_COPYRIGHT "\n");
  5648. ret = il4965_rate_control_register();
  5649. if (ret) {
  5650. pr_err("Unable to register rate control algorithm: %d\n", ret);
  5651. return ret;
  5652. }
  5653. ret = pci_register_driver(&il4965_driver);
  5654. if (ret) {
  5655. pr_err("Unable to initialize PCI module\n");
  5656. goto error_register;
  5657. }
  5658. return ret;
  5659. error_register:
  5660. il4965_rate_control_unregister();
  5661. return ret;
  5662. }
  5663. static void __exit
  5664. il4965_exit(void)
  5665. {
  5666. pci_unregister_driver(&il4965_driver);
  5667. il4965_rate_control_unregister();
  5668. }
  5669. module_exit(il4965_exit);
  5670. module_init(il4965_init);
  5671. #ifdef CONFIG_IWLEGACY_DEBUG
  5672. module_param_named(debug, il_debug_level, uint, S_IRUGO | S_IWUSR);
  5673. MODULE_PARM_DESC(debug, "debug output mask");
  5674. #endif
  5675. module_param_named(swcrypto, il4965_mod_params.sw_crypto, int, S_IRUGO);
  5676. MODULE_PARM_DESC(swcrypto, "using crypto in software (default 0 [hardware])");
  5677. module_param_named(queues_num, il4965_mod_params.num_of_queues, int, S_IRUGO);
  5678. MODULE_PARM_DESC(queues_num, "number of hw queues.");
  5679. module_param_named(11n_disable, il4965_mod_params.disable_11n, int, S_IRUGO);
  5680. MODULE_PARM_DESC(11n_disable, "disable 11n functionality");
  5681. module_param_named(amsdu_size_8K, il4965_mod_params.amsdu_size_8K, int,
  5682. S_IRUGO);
  5683. MODULE_PARM_DESC(amsdu_size_8K, "enable 8K amsdu size");
  5684. module_param_named(fw_restart, il4965_mod_params.restart_fw, int, S_IRUGO);
  5685. MODULE_PARM_DESC(fw_restart, "restart firmware in case of error");